OSDN Git Service

* autoload.cc: Call _api_fatal in asm.
[pf3gnuchains/pf3gnuchains4x.git] / winsup / cygwin / tty.h
1 /* tty.h: shared tty info for cygwin
2
3    Copyright 2000, 2001, 2002, 2003, 2004, 2006, 2009, 2010, 2011 Red Hat, Inc.
4
5 This file is part of Cygwin.
6
7 This software is a copyrighted work licensed under the terms of the
8 Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
9 details. */
10
11 /* tty tables */
12
13 #define INP_BUFFER_SIZE 256
14 #define OUT_BUFFER_SIZE 256
15 #define NTTYS           128
16 #define real_tty_attached(p)    ((p)->ctty >= 0 && !iscons_dev ((p)->ctty))
17
18 /* Input/Output/ioctl events */
19
20 #define OUTPUT_DONE_EVENT       "cygtty.output.done"
21 #define IOCTL_REQUEST_EVENT     "cygtty.ioctl.request"
22 #define IOCTL_DONE_EVENT        "cygtty.ioctl.done"
23 #define RESTART_OUTPUT_EVENT    "cygtty.output.restart"
24 #define INPUT_AVAILABLE_EVENT   "cygtty.input.avail"
25 #define OUTPUT_MUTEX            "cygtty.output.mutex"
26 #define INPUT_MUTEX             "cygtty.input.mutex"
27 #define TTY_SLAVE_ALIVE         "cygtty.slave_alive"
28 #define TTY_MASTER_ALIVE        "cygtty.master_alive"
29
30 #include <sys/termios.h>
31
32 #ifndef MIN_CTRL_C_SLOP
33 #define MIN_CTRL_C_SLOP 50
34 #endif
35
36 #include <devices.h>
37 class tty_min
38 {
39   pid_t sid;    /* Session ID of tty */
40   struct status_flags
41   {
42     unsigned initialized : 1; /* Set if tty is initialized */
43     unsigned rstcons     : 1; /* Set if console needs to be set to "non-cooked" */
44   } status;
45
46 public:
47   pid_t pgid;
48   int output_stopped;
49   fh_devices ntty;
50   DWORD last_ctrl_c;    /* tick count of last ctrl-c */
51   HWND hwnd;            /* Console window handle tty belongs to */
52
53   IMPLEMENT_STATUS_FLAG (bool, initialized)
54   IMPLEMENT_STATUS_FLAG (bool, rstcons)
55
56   struct termios ti;
57   struct winsize winsize;
58
59   /* ioctl requests buffer */
60   int cmd;
61   union
62   {
63     struct termios termios;
64     struct winsize winsize;
65     int value;
66     pid_t pid;
67   } arg;
68   /* XXX_retval variables holds master's completion codes. Error are stored as
69    * -ERRNO
70    */
71   int ioctl_retval;
72   int write_error;
73
74   void setntty (_major_t t, int n) {ntty = (fh_devices) FHDEV (t, n);} 
75   int getntty () const {return ntty;}
76   int get_unit () const {return device::minor (ntty);}
77   pid_t getpgid () const {return pgid;}
78   void setpgid (int pid) {pgid = pid;}
79   int getsid () const {return sid;}
80   void setsid (pid_t tsid) {sid = tsid;}
81   void kill_pgrp (int sig);
82   HWND gethwnd () const {return hwnd;}
83   void sethwnd (HWND wnd) {hwnd = wnd;}
84   const char *ttyname () __attribute ((regparm (1)));
85 };
86
87 class fhandler_pty_master;
88
89 class tty: public tty_min
90 {
91   HANDLE get_event (const char *fmt, PSECURITY_ATTRIBUTES sa,
92                     BOOL manual_reset = FALSE);
93     __attribute__ ((regparm (3)));
94 public:
95   pid_t master_pid;     /* PID of tty master process */
96
97   HANDLE from_master, to_master;
98
99   int read_retval;
100   bool was_opened;      /* True if opened at least once. */
101
102   void init ();
103   HANDLE open_inuse (ACCESS_MASK access);
104   HANDLE create_inuse (PSECURITY_ATTRIBUTES);
105   bool slave_alive ();
106   HANDLE open_mutex (const char *mutex, ACCESS_MASK access);
107   inline HANDLE open_output_mutex (ACCESS_MASK access)
108     { return open_mutex (OUTPUT_MUTEX, access); }
109   inline HANDLE open_input_mutex (ACCESS_MASK access)
110     { return open_mutex (INPUT_MUTEX, access); }
111   bool exists ();
112   void set_master_closed () {master_pid = -1;}
113   static void __stdcall create_master (int);
114   static void __stdcall init_session ();
115   friend class fhandler_pty_master;
116 };
117
118 class tty_list
119 {
120   tty ttys[NTTYS];
121   static HANDLE mutex;
122
123 public:
124   tty * operator [](int n) {return ttys + device::minor (n);}
125   int allocate (bool); /* true if allocate a tty, pty otherwise */
126   int connect (int);
127   void terminate ();
128   void init ();
129   tty_min *get_cttyp ();
130   int __stdcall attach (int n) __attribute__ ((regparm (2)));
131   static void __stdcall init_session ();
132   friend class lock_ttys;
133 };
134
135 class lock_ttys
136 {
137   bool release_me;
138 public:
139   lock_ttys (DWORD = INFINITE);
140   static void release ();
141   void dont_release () {release_me = false;}
142   ~lock_ttys ()
143   {
144     if (release_me)
145       release ();
146   }
147 };
148
149 extern "C" int ttyslot (void);