OSDN Git Service

* security.cc (set_process_privileges): Removed `static'.
[pf3gnuchains/sourceware.git] / winsup / cygwin / winsup.h
1 /* winsup.h: main Cygwin header file.
2
3    Copyright 1996, 1997, 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 #ifdef HAVE_CONFIG_H
12 # include "config.h"
13 #endif
14
15 #define __INSIDE_CYGWIN__
16
17 #define alloca(x) __builtin_alloca (x)
18 #define strlen __builtin_strlen
19 #define strcpy __builtin_strcpy
20 #define memcpy __builtin_memcpy
21 #define memcmp __builtin_memcmp
22 #ifdef HAVE_BUILTIN_MEMSET
23 # define memset __builtin_memset
24 #endif
25
26 #include <sys/types.h>
27 #include <sys/strace.h>
28 #include <sys/resource.h>
29 #include <setjmp.h>
30 #include <signal.h>
31 #include <string.h>
32
33 #undef strchr
34 #define strchr cygwin_strchr
35 extern inline char * strchr(const char * s, int c)
36 {
37 register char * __res;
38 __asm__ __volatile__(
39         "movb %%al,%%ah\n"
40         "1:\tmovb (%1),%%al\n\t"
41         "cmpb %%ah,%%al\n\t"
42         "je 2f\n\t"
43         "incl %1\n\t"
44         "testb %%al,%%al\n\t"
45         "jne 1b\n\t"
46         "xorl %1,%1\n"
47         "2:\tmovl %1,%0\n\t"
48         :"=a" (__res), "=r" (s)
49         :"0" (c),      "1"  (s));
50 return __res;
51 }
52
53 #include <windows.h>
54
55 /* Used for runtime OS check/decisions. */
56 enum os_type {winNT = 1, win95, win98, win32s, unknown};
57 extern os_type os_being_run;
58
59 /* Used to check if Cygwin DLL is dynamically loaded. */
60 extern int dynamically_loaded;
61
62 #include <cygwin/version.h>
63
64 #define TITLESIZE 1024
65 #define MAX_USER_NAME 20
66 #define DEFAULT_UID 500
67 #define DEFAULT_GID 544
68
69 /* status bit manipulation */
70 #define __ISSETF(what, x, prefix) \
71   ((what)->status & prefix##_##x)
72 #define __SETF(what, x, prefix) \
73   ((what)->status |= prefix##_##x)
74 #define __CLEARF(what, x, prefix) \
75   ((what)->status &= ~prefix##_##x)
76 #define __CONDSETF(n, what, x, prefix) \
77   ((n) ? __SETF (what, x, prefix) : __CLEARF (what, x, prefix))
78
79 #include "thread.h"
80 #include "shared.h"
81
82 extern HANDLE hMainThread;
83 extern HANDLE hMainProc;
84
85 /* Now that pinfo has been defined, include... */
86 #include "debug.h"
87 #include "sigproc.h"
88 #include "fhandler.h"
89 #include "path.h"
90 #include <sys/cygwin.h>
91
92 /********************** Application Interface **************************/
93
94 /* This lives in the app and is initialized before jumping into the DLL.
95    It should only contain stuff which the user's process needs to see, or
96    which is needed before the user pointer is initialized, or is needed to
97    carry inheritance information from parent to child.  Note that it cannot
98    be used to carry inheritance information across exec!
99
100    Remember, this structure is linked into the application's executable.
101    Changes to this can invalidate existing executables, so we go to extra
102    lengths to avoid having to do it.
103
104    When adding/deleting members, remember to adjust {public,internal}_reserved.
105    The size of the class shouldn't change [unless you really are prepared to
106    invalidate all existing executables].  The program does a check (using
107    SIZEOF_PER_PROCESS) to make sure you remember to make the adjustment.
108 */
109
110 class per_process
111 {
112  public:
113   char *initial_sp;
114
115   /* The offset of these 3 values can never change. */
116   /* magic_biscuit is the size of this class and should never change. */
117   DWORD magic_biscuit;
118   DWORD dll_major;
119   DWORD dll_minor;
120
121   struct _reent **impure_ptr_ptr;
122   char ***envptr;
123
124   /* Used to point to the memory machine we should use.  Usually these
125      point back into the dll, but they can be overridden by the user. */
126   void *(*malloc)(size_t);
127   void (*free)(void *);
128   void *(*realloc)(void *, size_t);
129
130   int *fmode_ptr;
131
132   int (*main)(int, char **, char **);
133   void (**ctors)(void);
134   void (**dtors)(void);
135
136   /* For fork */
137   void *data_start;
138   void *data_end;
139   void *bss_start;
140   void *bss_end;
141
142   void *(*calloc)(size_t, size_t);
143   /* For future expansion of values set by the app. */
144   void *public_reserved[4];
145
146   /* The rest are *internal* to cygwin.dll.
147      Those that are here because we want the child to inherit the value from
148      the parent (which happens when bss is copied) are marked as such. */
149
150   /* non-zero of ctors have been run.  Inherited from parent. */
151   int run_ctors_p;
152
153   /* These will be non-zero if the above (malloc,free,realloc) have been
154      overridden. */
155   /* FIXME: not currently used */
156   int __imp_malloc;
157   int __imp_free;
158   int __imp_realloc;
159
160   /* Heap management.  Inherited from parent. */
161   void *heapbase;               /* bottom of the heap */
162   void *heapptr;                /* current index into heap */
163   void *heaptop;                /* current top of heap */
164
165   HANDLE reserved1;             /* unused */
166
167   /* Non-zero means the task was forked.  The value is the pid.
168      Inherited from parent. */
169   int forkee;
170
171   HMODULE hmodule;
172
173   DWORD api_major;              /* API version that this program was */
174   DWORD api_minor;              /*  linked with */
175   /* For future expansion, so apps won't have to be relinked if we
176      add an item. */
177 #ifdef _MT_SAFE
178   ResourceLocks *resourcelocks;
179   MTinterface *threadinterface;
180   void *internal_reserved[6];
181 #else
182   void *internal_reserved[8];
183 #endif
184 };
185
186 extern per_process *user_data; /* Pointer into application's static data */
187
188 /* We use the following to test that sizeof hasn't changed.  When adding
189    or deleting members, insert fillers or use the reserved entries.
190    Do not change this value. */
191 #define SIZEOF_PER_PROCESS (42 * 4)
192
193 class hinfo
194 {
195   fhandler_base **fds;
196   fhandler_base **fds_on_hold;
197   int first_fd_for_open;
198 public:
199   size_t size;
200   hinfo () {first_fd_for_open = 3;}
201   int vfork_child_dup ();
202   void vfork_parent_restore ();
203   fhandler_base *dup_worker (fhandler_base *oldfh);
204   int extend (int howmuch);
205   void fixup_after_fork (HANDLE parent);
206   fhandler_base *build_fhandler (int fd, DWORD dev, const char *name,
207                                  int unit = -1);
208   fhandler_base *build_fhandler (int fd, const char *name, HANDLE h);
209   int not_open (int n);
210   int find_unused_handle (int start);
211   int find_unused_handle () { return find_unused_handle (first_fd_for_open);}
212   void release (int fd);
213   void init_std_file_from_handle (int fd, HANDLE handle, DWORD access, const char *name);
214   int dup2 (int oldfd, int newfd);
215   int linearize_fd_array (unsigned char *buf, int buflen);
216   LPBYTE de_linearize_fd_array (LPBYTE buf);
217   fhandler_base *operator [](int fd) { return fds[fd]; }
218   select_record *select_read (int fd, select_record *s);
219   select_record *select_write (int fd, select_record *s);
220   select_record *select_except (int fd, select_record *s);
221 };
222
223 /******************* Host-dependent constants **********************/
224 /* Portions of the cygwin DLL require special constants whose values
225    are dependent on the host system.  Rather than dynamically
226    determine those values whenever they are required, initialize these
227    values once at process start-up. */
228
229 class host_dependent_constants
230 {
231  public:
232   void init (void);
233
234   /* Used by fhandler_disk_file::lock which needs a platform-specific
235      upper word value for locking entire files. */
236   DWORD win32_upper;
237
238   /* fhandler_base::open requires host dependent file sharing
239      attributes. */
240   int shared;
241 };
242
243 extern host_dependent_constants host_dependent;
244
245 /* Events/mutexes */
246 extern HANDLE pinfo_mutex;
247 extern HANDLE title_mutex;
248
249
250
251 /*************************** Per Thread ******************************/
252
253 #define PER_THREAD_FORK_CLEAR ((void *)0xffffffff)
254 class per_thread
255 {
256   DWORD tls;
257   int clear_on_fork_p;
258 public:
259   per_thread (int forkval = 1) {tls = TlsAlloc (); clear_on_fork_p = forkval;}
260   DWORD get_tls () {return tls;}
261   int clear_on_fork () {return clear_on_fork_p;}
262
263   virtual void *get () {return TlsGetValue (get_tls ());}
264   virtual size_t size () {return 0;}
265   virtual void set (void *s = NULL);
266   virtual void set (int n) {TlsSetValue (get_tls (), (void *)n);}
267   virtual void *create ()
268   {
269     void *s = new char [size ()];
270     memset (s, 0, size ());
271     set (s);
272     return s;
273   }
274 };
275
276 class per_thread_waitq : public per_thread
277 {
278 public:
279   per_thread_waitq () : per_thread (0) {}
280   void *get () {return (waitq *) this->per_thread::get ();}
281   void *create () {return (waitq *) this->per_thread::create ();}
282   size_t size () {return sizeof (waitq);}
283 };
284
285 struct vfork_save
286 {
287   int pid;
288   jmp_buf j;
289   char **vfork_ebp;
290   char *caller_ebp;
291   char *retaddr;
292   int is_active () { return pid < 0; }
293 };
294
295 class per_thread_vfork : public per_thread
296 {
297 public:
298   vfork_save *val () { return (vfork_save *) this->per_thread::get (); }
299   vfork_save *create () {return (vfork_save *) this->per_thread::create ();}
300   size_t size () {return sizeof (vfork_save);}
301 };
302
303 extern "C" {
304 struct signal_dispatch
305 {
306   int arg;
307   void (*func) (int);
308   int sig;
309   int saved_errno;
310   DWORD ebp;
311   DWORD oldmask;
312   DWORD retaddr;
313 };
314 };
315
316 struct per_thread_signal_dispatch : public per_thread
317 {
318   signal_dispatch *get () { return (signal_dispatch *) this->per_thread::get (); }
319   signal_dispatch *create () {return (signal_dispatch *) this->per_thread::create ();}
320   size_t size () {return sizeof (signal_dispatch);}
321 };
322
323 extern per_thread_waitq waitq_storage;
324 extern per_thread_vfork vfork_storage;
325 extern per_thread_signal_dispatch signal_dispatch_storage;
326
327 extern per_thread *threadstuff[];
328
329 /**************************** Convenience ******************************/
330
331 #define NO_COPY __attribute__((section(".data_cygwin_nocopy")))
332
333 /* Used when treating / and \ as equivalent. */
334 #define SLASH_P(ch) \
335     ({ \
336         char __c = (ch); \
337         ((__c) == '/' || (__c) == '\\'); \
338     })
339
340 /* Convert a signal to a signal mask */
341 #define SIGTOMASK(sig)  (1<<((sig) - signal_shift_subtract))
342 extern unsigned int signal_shift_subtract;
343
344 #ifdef NOSTRACE
345 #define MARK() 0
346 #else
347 #define MARK() mark (__FILE__,__LINE__)
348 #endif
349
350 #define api_fatal(fmt, args...) \
351   __api_fatal ("%P: *** " fmt, ## args)
352
353 #undef issep
354 #define issep(ch) (strchr (" \t\n\r", (ch)) != NULL)
355
356 #define isdirsep SLASH_P
357 #define isabspath(p) \
358   (isdirsep (*(p)) || (isalpha (*(p)) && (p)[1] == ':'))
359
360 /******************** Initialization/Termination **********************/
361
362 /* cygwin .dll initialization */
363 void dll_crt0 (per_process *);
364
365 /* dynamically loaded dll initialization */
366 extern "C" int dll_dllcrt0 (HMODULE,per_process*);
367
368 /* dynamically loaded dll initialization for non-cygwin apps */
369 extern "C" int dll_noncygwin_dllcrt0 (HMODULE, per_process *);
370
371 /* exit the program */
372 extern "C" void __stdcall do_exit (int) __attribute__ ((noreturn));
373
374 /* Initialize the environment */
375 void environ_init (void);
376
377 /* Heap management. */
378 void heap_init (void);
379 void malloc_init (void);
380
381 /* fd table */
382 void dtable_init (void);
383 void hinfo_init (void);
384 extern hinfo dtable;
385
386 /* UID/GID */
387 void uinfo_init (void);
388
389 /* various events */
390 void events_init (void);
391 void events_terminate (void);
392
393 void __stdcall close_all_files (void);
394
395 extern class strace strace;
396
397 /* Invisible window initialization/termination. */
398 HWND __stdcall gethwnd (void);
399 void __stdcall window_terminate (void);
400
401 /* Globals that handle initialization of winsock in a child process. */
402 extern HANDLE wsock32_handle;
403
404 /* Globals that handle initialization of netapi in a child process. */
405 extern HANDLE netapi32_handle;
406
407 /* debug_on_trap support. see exceptions.cc:try_to_debug() */
408 extern "C" void error_start_init (const char*);
409 extern "C" int try_to_debug ();
410
411 /**************************** Miscellaneous ******************************/
412
413 const char * __stdcall find_exec (const char *name, char *buf, const char *winenv = "PATH=",
414                         int null_if_notfound = 0, const char **known_suffix = NULL);
415
416 /* File manipulation */
417 int __stdcall set_process_privileges ();
418 int __stdcall get_file_attribute (int, const char *, int *,
419                                   uid_t * = NULL, gid_t * = NULL);
420 int __stdcall set_file_attribute (int, const char *, int);
421 int __stdcall set_file_attribute (int, const char *, uid_t, gid_t, int, const char *);
422 void __stdcall set_std_handle (int);
423 int __stdcall writable_directory (const char *file);
424 int __stdcall stat_dev (DWORD, int, unsigned long, struct stat *);
425 extern BOOL allow_ntsec;
426
427 /* `lookup_name' should be called instead of LookupAccountName.
428  * logsrv may be NULL, in this case only the local system is used for lookup.
429  * The buffer for ret_sid (40 Bytes) has to be allocated by the caller! */
430 BOOL __stdcall lookup_name (const char *name, const char *logsrv, PSID ret_sid);
431
432 unsigned long __stdcall hash_path_name (unsigned long hash, const char *name);
433 void __stdcall nofinalslash (const char *src, char *dst);
434 extern "C" char *__stdcall rootdir (char *full_path);
435
436 void __stdcall mark (const char *, int);
437
438 extern "C" int _spawnve (HANDLE hToken, int mode, const char *path,
439                          const char *const *argv, const char *const *envp);
440 int __stdcall spawn_guts (HANDLE hToken, const char *prog_arg,
441                    const char *const *argv, const char *const envp[],
442                    pinfo *child, int mode);
443
444 /* For mmaps across fork(). */
445 int __stdcall recreate_mmaps_after_fork (void *);
446 void __stdcall set_child_mmap_ptr (pinfo *);
447
448 /* String manipulation */
449 char *__stdcall strccpy (char *s1, const char **s2, char c);
450 int __stdcall strcasematch (const char *s1, const char *s2);
451 int __stdcall strncasematch (const char *s1, const char *s2, size_t n);
452 char *__stdcall strcasestr (const char *searchee, const char *lookfor);
453
454 /* Time related */
455 void __stdcall totimeval (struct timeval *dst, FILETIME * src, int sub, int flag);
456 long __stdcall to_time_t (FILETIME * ptr);
457
458 /* pinfo table manipulation */
459 #ifndef lock_pinfo_for_update
460 int __stdcall lock_pinfo_for_update (DWORD timeout);
461 #endif
462 void unlock_pinfo (void);
463 pinfo *__stdcall set_myself (pinfo *);
464
465 /* Retrieve a security descriptor that allows all access */
466 SECURITY_DESCRIPTOR *__stdcall get_null_sd (void);
467
468 int __stdcall get_id_from_sid (PSID, BOOL);
469 extern inline int get_uid_from_sid (PSID psid) { return get_id_from_sid (psid, FALSE);}
470 extern inline int get_gid_from_sid (PSID psid) { return get_id_from_sid (psid, TRUE); }
471
472 int __stdcall NTReadEA (const char *file, const char *attrname, char *buf, int len);
473 BOOL __stdcall NTWriteEA (const char *file, const char *attrname, char *buf, int len);
474
475 void __stdcall set_console_title (char *);
476 void set_console_handler ();
477
478 void __stdcall fill_rusage (struct rusage *, HANDLE);
479 void __stdcall add_rusage (struct rusage *, struct rusage *);
480
481 void set_winsock_errno ();
482
483 /**************************** Exports ******************************/
484
485 extern "C" {
486 int cygwin_select (int , fd_set *, fd_set *, fd_set *,
487                    struct timeval *to);
488 int cygwin_gethostname (char *__name, size_t __len);
489
490 int kill_pgrp (pid_t, int);
491 int _kill (int, int);
492 int _raise (int sig);
493
494 int getdtablesize ();
495 void setdtablesize (int);
496
497 extern char _data_start__, _data_end__, _bss_start__, _bss_end__;
498 extern void (*__CTOR_LIST__) (void);
499 extern void (*__DTOR_LIST__) (void);
500 };
501
502 /*************************** Unsorted ******************************/
503
504 /* The size of the console title */
505 #define TITLESIZE 1024
506
507 #define WM_ASYNCIO      0x8000          // WM_APP
508
509 /* Note that MAX_PATH is defined in the windows headers */
510 /* There is also PATH_MAX and MAXPATHLEN.
511    PATH_MAX is from Posix and does *not* include the trailing NUL.
512    MAXPATHLEN is from Unix.
513
514    Thou shalt use MAX_PATH throughout.  It avoids the NUL vs no-NUL
515    issue and is neither of the Unixy ones [so we can punt on which
516    one is the right one to use].  */
517
518 /* Initial and increment values for cygwin's fd table */
519 #define NOFILE_INCR    32
520
521 #ifdef __cplusplus
522 extern "C" {
523 #endif
524 #include <sys/reent.h>
525
526 #define STD_RBITS S_IRUSR | S_IRGRP | S_IROTH
527 #define STD_WBITS S_IWUSR
528 #define STD_XBITS S_IXUSR | S_IXGRP | S_IXOTH
529
530 #define O_NOSYMLINK 0x080000
531 #define O_DIROPEN   0x100000
532
533 #ifdef __cplusplus
534 }
535 #endif
536
537 /*************************** Environment ******************************/
538
539 /* The structure below is used to control conversion to/from posix-style
540  * file specs.  Currently, only PATH and HOME are converted, but PATH
541  * needs to use a "convert path list" function while HOME needs a simple
542  * "convert to posix/win32".  For the simple case, where a calculated length
543  * is required, just return MAX_PATH.  *FIXME*
544  */
545 struct win_env
546   {
547     const char *name;
548     size_t namelen;
549     char *posix;
550     char *native;
551     int (*toposix) (const char *, char *);
552     int (*towin32) (const char *, char *);
553     int (*posix_len) (const char *);
554     int (*win32_len) (const char *);
555     void add_cache (const char *in_posix, const char *in_native = NULL);
556     const char * get_native () {return native ? native + namelen : NULL;}
557   };
558
559 win_env * __stdcall getwinenv (const char *name, const char *posix = NULL);
560
561 char * __stdcall winenv (const char * const *);
562 extern char **__cygwin_environ;
563
564 /* The title on program start. */
565 extern char *old_title;
566 extern BOOL display_title;
567
568
569 /*************************** errno manipulation ******************************/
570
571 void seterrno_from_win_error (const char *file, int line, int code);
572 void seterrno (const char *, int line);
573
574 #define __seterrno() seterrno (__FILE__, __LINE__)
575 #define __seterrno_from_win_error(val) seterrno_from_win_error (__FILE__, __LINE__, val)
576 #undef errno
577 #define errno dont_use_this_since_were_in_a_shared library
578 #define set_errno(val) (_impure_ptr->_errno = (val))
579 #define get_errno()  (_impure_ptr->_errno)
580 extern "C" void __stdcall set_sig_errno (int e);
581
582 class save_errno
583   {
584     int saved;
585   public:
586     save_errno () {saved = get_errno ();}
587     save_errno (int what) {saved = get_errno (); set_errno (what); }
588     void set (int what) {set_errno (what); saved = what;}
589     void reset () {saved = get_errno ();}
590     ~save_errno () {set_errno (saved);}
591   };
592
593 extern const char *__sp_fn;
594 extern int __sp_ln;