OSDN Git Service

d6e465544b94f7e382de5a9694716d70191e4745
[pf3gnuchains/pf3gnuchains4x.git] / winsup / cygwin / shared.h
1 /* shared.h: shared info for cygwin
2
3    Copyright 1998, 1999, 2000 Cygnus Solutions.
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 /******** Functions declarations for use in methods below ********/
12
13 /* Printf type functions */
14 extern "C" void __api_fatal (const char *, ...) __attribute__ ((noreturn));
15 extern "C" int __small_sprintf (char *dst, const char *fmt, ...);
16 extern "C" int __small_vsprintf (char *dst, const char *fmt, va_list ap);
17
18 /******** Deletion Queue Class ********/
19
20 /* First pass at a file deletion queue structure.
21
22    We can't keep this list in the per-process info, since
23    one process may open a file, and outlive a process which
24    wanted to unlink the file - and the data would go away.
25
26    Perhaps the FILE_FLAG_DELETE_ON_CLOSE would be ok,
27    but brief experimentation didn't get too far.
28 */
29
30 #define MAX_DELQUEUES_PENDING 100
31
32 class delqueue_list
33 {
34   char name[MAX_DELQUEUES_PENDING][MAX_PATH];
35   char inuse[MAX_DELQUEUES_PENDING];
36   int empty;
37
38 public:
39   void init ();
40   void queue_file (const char *dosname);
41   void process_queue ();
42 };
43
44 /******** Process Table ********/
45
46 /* Signal constants (have to define them here, unfortunately) */
47
48 enum
49 {
50   __SIGFLUSH        = -2,
51   __SIGSTRACE       = -1,
52   __SIGCHILDSTOPPED =  0,
53   __SIGOFFSET       =  3
54 };
55
56 class pinfo
57 {
58  public:
59
60   /* If hProcess is set, it's because it came from a
61      CreateProcess call.  This means it's process relative
62      to the thing which created the process.  That's ok because
63      we only use this handle from the parent. */
64   HANDLE hProcess;
65
66   HANDLE parent_alive;
67
68   /* dwProcessId contains the processid used for sending signals.  It
69    * will be reset in a child process when it is capable of receiving
70    * signals.
71    */
72   DWORD dwProcessId;
73
74   /* User information.
75      The information is derived from the GetUserName system call,
76      with the name looked up in /etc/passwd and assigned a default value
77      if not found.  This data resides in the shared data area (allowing
78      tasks to store whatever they want here) so it's for informational
79      purposes only. */
80   uid_t uid;        /* User ID */
81   gid_t gid;        /* Group ID */
82   pid_t pgid;       /* Process group ID */
83   pid_t sid;        /* Session ID */
84   int ctty;         /* Control tty */
85   mode_t umask;
86   char username[MAX_USER_NAME]; /* user's name */
87
88   /* Extendend user information.
89      The information is derived from the internal_getlogin call
90      when on a NT system. */
91   PSID psid;        /* user's SID */
92   char sidbuf[MAX_SID_LEN];  /* buffer for user's SID */
93   char logsrv[MAX_HOST_NAME]; /* Logon server, may be FQDN */
94   char domain[MAX_COMPUTERNAME_LENGTH+1]; /* Logon domain of the user */
95
96   /* token is needed if sexec should be called. It can be set by a call
97      to `set_impersonation_token()'. */
98   HANDLE token;
99   BOOL impersonated;
100   uid_t orig_uid;        /* Remains intact also after impersonation */
101   uid_t orig_gid;        /* Ditto */
102   uid_t real_uid;        /* Remains intact on seteuid, replaced by setuid */
103   gid_t real_gid;        /* Ditto */
104
105   /* Filled when chroot() is called by the process or one of it's parents.
106      Saved without trailing backslash. */
107   char root[MAX_PATH+1];
108   size_t rootlen;
109
110   /* Non-zero if process was stopped by a signal. */
111   char stopsig;
112
113   struct sigaction& getsig (int);
114   void copysigs (pinfo *);
115   sigset_t& getsigmask ();
116   void setsigmask (sigset_t);
117   LONG* getsigtodo (int);
118   HANDLE getthread2signal ();
119   void setthread2signal (void *);
120
121   /* Resources used by process. */
122   long start_time;
123   struct rusage rusage_self;
124   struct rusage rusage_children;
125
126 private:
127   struct sigaction sigs[NSIG];
128   sigset_t sig_mask;            /* one set for everything to ignore. */
129   LONG _sigtodo[NSIG + __SIGOFFSET];
130 #ifdef _MT_SAFE
131   ThreadItem* thread2signal;  // NULL means means thread any other means a pthread
132 #endif
133
134 public:
135
136   /* Pointer to mmap'ed areas for this process.  Set up by fork. */
137   void *mmap_ptr;
138
139   /* Used to spawn a child for fork(), among other things. */
140   char progname[MAX_PATH];
141
142   #define PINFO_ZERO ((((pinfo *) NULL)->progname + 1) - ((char *) NULL))
143
144   /* Anything below this point is not zeroed automatically by allocate_pid */
145
146   /* The pid stays the same, while the hProcess moves due to execs. */
147   pid_t pid;
148   /* Parent process id.  */
149   pid_t ppid;
150
151   /* Various flags indicating the state of the process.  See PID_
152      constants below. */
153   DWORD process_state;
154
155   void record_death (int lock = 1);
156 };
157
158 #define ISSTATE(p, f)   (!!((p)->process_state & f))
159 #define NOTSTATE(p, f)  (!((p)->process_state & f))
160
161 #define PSIZE 128
162
163 class pinfo_list
164 {
165  public:
166   int next_pid;
167   pinfo vec[PSIZE];
168   char lock_info[MAX_PATH + 1];
169   pinfo * operator[] (pid_t x);
170   int size (void) { return PSIZE; }
171   pinfo *allocate_pid (void);
172   void init (void);
173 };
174
175 void __stdcall pinfo_init (PBYTE);
176 pinfo *__stdcall procinfo (int n);
177
178 enum
179 {
180   PROC_MAGIC = 0xaf08f000,
181   PROC_FORK = PROC_MAGIC + 1,
182   PROC_EXEC = PROC_MAGIC + 2,
183   PROC_SPAWN = PROC_MAGIC + 3,
184   PROC_FORK1 = PROC_MAGIC + 4   // Newer versions provide stack
185                                 // location information
186 };
187
188 #define PROC_MAGIC_MASK 0xff00f000
189 #define PROC_MAGIC_GENERIC 0xaf00f000
190 #define PROC_MAGIC_VER_MASK 0x0ff0000
191
192 #define EXEC_MAGIC_SIZE sizeof(child_info)
193 class child_info
194 {
195 public:
196   DWORD zero[1];        // must be zeroed
197   DWORD cb;             // size of this record
198   DWORD type;           // type of record
199   int cygpid;           // cygwin pid of child process
200   HANDLE subproc_ready; // used for synchronization with parent
201   HANDLE shared_h;
202   HANDLE console_h;
203   HANDLE parent_alive;  // handle of thread used to track children
204 };
205
206 class child_info_fork: public child_info
207 {
208 public:
209   HANDLE forker_finished;// for synchronization with child
210   DWORD stacksize;      // size of parent stack
211   void *heaptop;
212   void *heapbase;
213   void *heapptr;
214   jmp_buf jmp;          // where child will jump to
215   void *stacktop;       // location of top of parent stack
216   void *stackbottom;    // location of bottom of parent stack
217 };
218
219 void __stdcall init_child_info (DWORD, child_info *, int, HANDLE);
220
221 extern child_info_fork *child_proc_info;
222
223 /* Process info for this process */
224 extern pinfo *myself;
225
226 /* non-NULL if this process is a child of a cygwin process */
227 extern HANDLE parent_alive;
228
229 /******** Registry Access ********/
230
231 class reg_key
232 {
233 private:
234
235   HKEY key;
236   LONG key_is_invalid;
237
238 public:
239
240   reg_key (HKEY toplev, REGSAM access, ...);
241   reg_key (REGSAM access, ...);
242   reg_key (REGSAM access = KEY_ALL_ACCESS);
243
244   void *operator new (size_t, void *p) {return p;}
245   void build_reg (HKEY key, REGSAM access, va_list av);
246
247   int error () {return key == (HKEY) INVALID_HANDLE_VALUE;}
248
249   int kill (const char *child);
250
251   HKEY get_key ();
252   int get_int (const char *,int def);
253   int get_string (const char *, char *buf, size_t len, const char *def);
254   int set_string (const char *,const char *);
255   int set_int (const char *, int val);
256
257   ~reg_key ();
258 };
259
260 /* Evaluates path to the directory of the local user registry hive */
261 char *__stdcall get_registry_hive_path (const PSID psid, char *path);
262 void __stdcall load_registry_hive (PSID psid);
263
264 /******** Mount Table ********/
265
266 /* Mount table entry */
267
268 class mount_item
269 {
270 public:
271   /* FIXME: Nasty static allocation.  Need to have a heap in the shared
272      area [with the user being able to configure at runtime the max size].  */
273
274   /* Win32-style mounted partition source ("C:\foo\bar").
275      native_path[0] == 0 for unused entries.  */
276   char native_path[MAX_PATH];
277   int native_pathlen;
278
279   /* POSIX-style mount point ("/foo/bar") */
280   char posix_path[MAX_PATH];
281   int posix_pathlen;
282
283   unsigned flags;
284
285   void init (const char *dev, const char *path, unsigned flags);
286
287   struct mntent *getmntent ();
288 };
289
290 /* Warning: Decreasing this value will cause cygwin.dll to ignore existing
291    higher numbered registry entries.  Don't change this number willy-nilly.
292    What we need is to have a more dynamic allocation scheme, but the current
293    scheme should be satisfactory for a long while yet.  */
294 #define MAX_MOUNTS 30
295
296 class mount_info
297 {
298   int posix_sorted[MAX_MOUNTS];
299   int native_sorted[MAX_MOUNTS];
300 public:
301   int nmounts;
302   mount_item mount[MAX_MOUNTS];
303
304   /* Strings used by getmntent(). */
305   char mnt_type[20];
306   char mnt_opts[20];
307   char mnt_fsname[MAX_PATH];
308   char mnt_dir[MAX_PATH];
309
310   /* cygdrive_prefix is used as the root of the path automatically
311      prepended to a path when the path has no associated mount.
312      cygdrive_flags are the default flags for the cygdrives. */
313   char cygdrive[MAX_PATH];
314   size_t cygdrive_len;
315   unsigned cygdrive_flags;
316
317   /* Increment when setting up a reg_key if mounts area had to be
318      created so we know when we need to import old mount tables. */
319   int had_to_create_mount_areas;
320
321   void init ();
322   int add_item (const char *dev, const char *path, unsigned flags, int reg_p);
323   int del_item (const char *path, unsigned flags, int reg_p);
324
325   void from_registry ();
326   int add_reg_mount (const char * native_path, const char * posix_path,
327                       unsigned mountflags);
328   int del_reg_mount (const char * posix_path, unsigned mountflags);
329
330   unsigned set_flags_from_win32_path (const char *path);
331   int conv_to_win32_path (const char *src_path, char *win32_path,
332                           char *full_win32_path, DWORD &devn, int &unit,
333                           unsigned *flags = NULL);
334   int conv_to_posix_path (const char *src_path, char *posix_path,
335                           int keep_rel_p);
336   struct mntent *getmntent (int x);
337
338   int write_cygdrive_info_to_registry (const char *cygdrive_prefix, unsigned flags);
339
340   void import_v1_mounts ();
341
342 private:
343
344   void sort ();
345   void read_mounts (reg_key& r);
346   void read_v1_mounts (reg_key r, unsigned which);
347   void mount_slash ();
348   void to_registry ();
349
350   int cygdrive_win32_path (const char *src, char *dst, int trailing_slash_p);
351   void cygdrive_posix_path (const char *src, char *dst, int trailing_slash_p);
352   void slash_drive_to_win32_path (const char *path, char *buf, int trailing_slash_p);
353   void read_cygdrive_info_from_registry ();
354 };
355
356 /******** TTY Support ********/
357
358 /* tty tables */
359
360 #define INP_BUFFER_SIZE 256
361 #define OUT_BUFFER_SIZE 256
362 #define NTTYS           128
363 #define TTY_CONSOLE     0x40000000
364 #define tty_attached(p) ((p)->ctty >= 0 && (p)->ctty != TTY_CONSOLE)
365
366 /* Input/Output/ioctl events */
367
368 #define OUTPUT_DONE_EVENT       "cygtty%d.output.done"
369 #define IOCTL_REQUEST_EVENT     "cygtty%d.ioctl.request"
370 #define IOCTL_DONE_EVENT        "cygtty%d.ioctl.done"
371 #define RESTART_OUTPUT_EVENT    "cygtty%d.output.restart"
372 #define OUTPUT_MUTEX            "cygtty%d.output.mutex"
373 #define TTY_SLAVE_ALIVE         "cygtty%x.slave_alive"
374 #define TTY_MASTER_ALIVE        "cygtty%x.master_alive"
375
376 #include <sys/termios.h>
377
378 enum
379 {
380   TTY_INITIALIZED = 1,          /* Set if tty is initialized */
381   TTY_RSTCONS = 2               /* Set if console needs to be set to "non-cooked" */
382 };
383
384 #define TTYISSETF(x)    __ISSETF (tc, x, TTY)
385 #define TTYSETF(x)      __SETF (tc, x, TTY)
386 #define TTYCLEARF(x)    __CLEARF (tc, x, TTY)
387 #define TTYCONDSETF(n, x) __CONDSETF(n, tc, x, TTY)
388
389 #ifndef MIN_CTRL_C_SLOP
390 #define MIN_CTRL_C_SLOP 50
391 #endif
392
393 class tty_min
394 {
395   pid_t sid;    /* Session ID of tty */
396 public:
397   DWORD status;
398   pid_t pgid;
399   int OutputStopped;
400   int ntty;
401   DWORD last_ctrl_c;    // tick count of last ctrl-c
402
403   tty_min (int t = -1, pid_t s = -1) : sid (s), ntty (t) {}
404   void setntty (int n) {ntty = n;}
405   pid_t getpgid () {return pgid;}
406   void setpgid (int pid) {pgid = pid;}
407   int getsid () {return sid;}
408   void setsid (pid_t tsid) {sid = tsid;}
409   struct termios ti;
410   struct winsize winsize;
411
412   /* ioctl requests buffer */
413   int cmd;
414   union
415   {
416     struct termios termios;
417     struct winsize winsize;
418     int value;
419     pid_t pid;
420   } arg;
421   /* XXX_retval variables holds master's completion codes. Error are stored as
422    * -ERRNO
423    */
424   int ioctl_retval;
425
426   int write_retval;
427 };
428
429 class fhandler_pty_master;
430
431 class tty: public tty_min
432 {
433   HANDLE get_event (const char *fmt, BOOL inherit);
434 public:
435   HWND  hwnd;   /* Console window handle tty belongs to */
436
437   DWORD master_pid;     /* Win32 PID of tty master process */
438
439   HANDLE from_master, to_slave;
440   HANDLE from_slave, to_master;
441
442   int read_retval;
443   BOOL was_opened;      /* True if opened at least once. */
444
445   void init ();
446   HANDLE create_inuse (const char *);
447   BOOL common_init (fhandler_pty_master *);
448   BOOL alive (const char *fmt);
449   BOOL slave_alive ();
450   BOOL master_alive ();
451   HWND gethwnd () {return hwnd;}
452   void sethwnd (HWND wnd) {hwnd = wnd;}
453   int make_pipes (fhandler_pty_master *ptym);
454   HANDLE open_output_mutex (BOOL inherit = FALSE)
455   {
456     char buf[80];
457     __small_sprintf (buf, OUTPUT_MUTEX, ntty);
458     return OpenMutex (MUTEX_ALL_ACCESS, inherit, buf);
459   }
460   BOOL exists ()
461   {
462     HANDLE h = open_output_mutex ();
463     if (h)
464       {
465         CloseHandle (h);
466         return 1;
467       }
468     return slave_alive ();
469   }
470 };
471
472 class tty_list
473 {
474   tty ttys[NTTYS];
475
476 public:
477   tty * operator [](int n) {return ttys + n;}
478   int allocate_tty (int n); /* n non zero if allocate a tty, pty otherwise */
479   int connect_tty (int);
480   void terminate ();
481   void init ();
482   tty_min *get_tty (int n);
483 };
484
485 void __stdcall tty_init ();
486 void __stdcall tty_terminate ();
487 int __stdcall attach_tty (int);
488 void __stdcall create_tty_master (int);
489 extern "C" int ttyslot (void);
490
491 /******** Shared Info ********/
492 /* Data accessible to all tasks */
493
494 class shared_info
495 {
496   DWORD inited;
497
498 public:
499   pinfo_list p;
500
501   /* FIXME: Doesn't work if more than one user on system. */
502   mount_info mount;
503
504   int heap_chunk_in_mb;
505   unsigned heap_chunk_size (void);
506
507   tty_list tty;
508   delqueue_list delqueue;
509   void initialize (void);
510 };
511
512 /* Various types of security attributes for use in Create* functions. */
513 extern SECURITY_ATTRIBUTES sec_none, sec_none_nih, sec_all, sec_all_nih;
514 extern SECURITY_ATTRIBUTES *__stdcall sec_user (PVOID sa_buf, PSID sid2 = NULL, BOOL inherit = TRUE);
515 extern SECURITY_ATTRIBUTES *__stdcall sec_user_nih (PVOID sa_buf, PSID sid2 = NULL);
516
517 extern shared_info *cygwin_shared;
518 extern HANDLE cygwin_shared_h;
519 extern HANDLE console_shared_h;
520 extern int __stdcall set_console_state_for_spawn ();
521
522 void __stdcall shared_init (void);
523 void __stdcall shared_terminate (void);
524
525 char *__stdcall shared_name (const char *, int);
526 void *__stdcall open_shared (const char *name, HANDLE &shared_h, DWORD size, void *addr);
527
528 extern "C" {
529 /* This is for programs that want to access the shared data. */
530 class shared_info *cygwin_getshared (void);
531
532 struct cygwin_version_info
533 {
534   unsigned short api_major;
535   unsigned short api_minor;
536   unsigned short dll_major;
537   unsigned short dll_minor;
538   unsigned short shared_data;
539   unsigned short mount_registry;
540   const char *dll_build_date;
541   char shared_id[sizeof (CYGWIN_VERSION_DLL_IDENTIFIER) + 64];
542 };
543 }
544
545 extern cygwin_version_info cygwin_version;
546 extern const char *cygwin_version_strings;