1 /* shared.h: shared info for cygwin
3 Copyright 1998, 1999, 2000 Cygnus Solutions.
5 This file is part of Cygwin.
7 This software is a copyrighted work licensed under the terms of the
8 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
11 /******** Functions declarations for use in methods below ********/
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);
18 /******** Deletion Queue Class ********/
20 /* First pass at a file deletion queue structure.
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.
26 Perhaps the FILE_FLAG_DELETE_ON_CLOSE would be ok,
27 but brief experimentation didn't get too far.
30 #define MAX_DELQUEUES_PENDING 100
34 char name[MAX_DELQUEUES_PENDING][MAX_PATH];
35 char inuse[MAX_DELQUEUES_PENDING];
40 void queue_file (const char *dosname);
41 void process_queue ();
44 /******** Process Table ********/
46 /* Signal constants (have to define them here, unfortunately) */
52 __SIGCHILDSTOPPED = 0,
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. */
68 /* dwProcessId contains the processid used for sending signals. It
69 * will be reset in a child process when it is capable of receiving
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
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 */
86 char username[MAX_USER_NAME]; /* user's name */
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 */
96 /* token is needed if sexec should be called. It can be set by a call
97 to `set_impersonation_token()'. */
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 */
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];
110 /* Non-zero if process was stopped by a signal. */
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 *);
121 /* Resources used by process. */
123 struct rusage rusage_self;
124 struct rusage rusage_children;
127 struct sigaction sigs[NSIG];
128 sigset_t sig_mask; /* one set for everything to ignore. */
129 LONG _sigtodo[NSIG + __SIGOFFSET];
131 ThreadItem* thread2signal; // NULL means means thread any other means a pthread
136 /* Pointer to mmap'ed areas for this process. Set up by fork. */
139 /* Used to spawn a child for fork(), among other things. */
140 char progname[MAX_PATH];
142 #define PINFO_ZERO ((((pinfo *) NULL)->progname + 1) - ((char *) NULL))
144 /* Anything below this point is not zeroed automatically by allocate_pid */
146 /* The pid stays the same, while the hProcess moves due to execs. */
148 /* Parent process id. */
151 /* Various flags indicating the state of the process. See PID_
155 void record_death (int lock = 1);
158 #define ISSTATE(p, f) (!!((p)->process_state & f))
159 #define NOTSTATE(p, f) (!((p)->process_state & f))
168 char lock_info[MAX_PATH + 1];
169 pinfo * operator[] (pid_t x);
170 int size (void) { return PSIZE; }
171 pinfo *allocate_pid (void);
175 void __stdcall pinfo_init (PBYTE);
176 pinfo *__stdcall procinfo (int n);
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
188 #define PROC_MAGIC_MASK 0xff00f000
189 #define PROC_MAGIC_GENERIC 0xaf00f000
190 #define PROC_MAGIC_VER_MASK 0x0ff0000
192 #define EXEC_MAGIC_SIZE sizeof(child_info)
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
203 HANDLE parent_alive; // handle of thread used to track children
206 class child_info_fork: public child_info
209 HANDLE forker_finished;// for synchronization with child
210 DWORD stacksize; // size of parent stack
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
219 void __stdcall init_child_info (DWORD, child_info *, int, HANDLE);
221 extern child_info_fork *child_proc_info;
223 /* Process info for this process */
224 extern pinfo *myself;
226 /* non-NULL if this process is a child of a cygwin process */
227 extern HANDLE parent_alive;
229 /******** Registry Access ********/
240 reg_key (HKEY toplev, REGSAM access, ...);
241 reg_key (REGSAM access, ...);
242 reg_key (REGSAM access = KEY_ALL_ACCESS);
244 void *operator new (size_t, void *p) {return p;}
245 void build_reg (HKEY key, REGSAM access, va_list av);
247 int error () {return key == (HKEY) INVALID_HANDLE_VALUE;}
249 int kill (const char *child);
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);
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);
264 /******** Mount Table ********/
266 /* Mount table entry */
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]. */
274 /* Win32-style mounted partition source ("C:\foo\bar").
275 native_path[0] == 0 for unused entries. */
276 char native_path[MAX_PATH];
279 /* POSIX-style mount point ("/foo/bar") */
280 char posix_path[MAX_PATH];
285 void init (const char *dev, const char *path, unsigned flags);
287 struct mntent *getmntent ();
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
298 int posix_sorted[MAX_MOUNTS];
299 int native_sorted[MAX_MOUNTS];
302 mount_item mount[MAX_MOUNTS];
304 /* Strings used by getmntent(). */
307 char mnt_fsname[MAX_PATH];
308 char mnt_dir[MAX_PATH];
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];
315 unsigned cygdrive_flags;
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;
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);
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);
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,
336 struct mntent *getmntent (int x);
338 int write_cygdrive_info_to_registry (const char *cygdrive_prefix, unsigned flags);
340 void import_v1_mounts ();
345 void read_mounts (reg_key& r);
346 void read_v1_mounts (reg_key r, unsigned which);
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 ();
356 /******** TTY Support ********/
360 #define INP_BUFFER_SIZE 256
361 #define OUT_BUFFER_SIZE 256
363 #define TTY_CONSOLE 0x40000000
364 #define tty_attached(p) ((p)->ctty >= 0 && (p)->ctty != TTY_CONSOLE)
366 /* Input/Output/ioctl events */
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"
376 #include <sys/termios.h>
380 TTY_INITIALIZED = 1, /* Set if tty is initialized */
381 TTY_RSTCONS = 2 /* Set if console needs to be set to "non-cooked" */
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)
389 #ifndef MIN_CTRL_C_SLOP
390 #define MIN_CTRL_C_SLOP 50
395 pid_t sid; /* Session ID of tty */
401 DWORD last_ctrl_c; // tick count of last ctrl-c
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;}
410 struct winsize winsize;
412 /* ioctl requests buffer */
416 struct termios termios;
417 struct winsize winsize;
421 /* XXX_retval variables holds master's completion codes. Error are stored as
429 class fhandler_pty_master;
431 class tty: public tty_min
433 HANDLE get_event (const char *fmt, BOOL inherit);
435 HWND hwnd; /* Console window handle tty belongs to */
437 DWORD master_pid; /* Win32 PID of tty master process */
439 HANDLE from_master, to_slave;
440 HANDLE from_slave, to_master;
443 BOOL was_opened; /* True if opened at least once. */
446 HANDLE create_inuse (const char *);
447 BOOL common_init (fhandler_pty_master *);
448 BOOL alive (const char *fmt);
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)
457 __small_sprintf (buf, OUTPUT_MUTEX, ntty);
458 return OpenMutex (MUTEX_ALL_ACCESS, inherit, buf);
462 HANDLE h = open_output_mutex ();
468 return slave_alive ();
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);
482 tty_min *get_tty (int n);
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);
491 /******** Shared Info ********/
492 /* Data accessible to all tasks */
501 /* FIXME: Doesn't work if more than one user on system. */
504 int heap_chunk_in_mb;
505 unsigned heap_chunk_size (void);
508 delqueue_list delqueue;
509 void initialize (void);
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);
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 ();
522 void __stdcall shared_init (void);
523 void __stdcall shared_terminate (void);
525 char *__stdcall shared_name (const char *, int);
526 void *__stdcall open_shared (const char *name, HANDLE &shared_h, DWORD size, void *addr);
529 /* This is for programs that want to access the shared data. */
530 class shared_info *cygwin_getshared (void);
532 struct cygwin_version_info
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];
545 extern cygwin_version_info cygwin_version;
546 extern const char *cygwin_version_strings;