]> sourceware.org Git - newlib-cygwin.git/blob - winsup/cygwin/winsup.h
* exceptions.cc (interruptible): Make a little more structured.
[newlib-cygwin.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 CONTEXT *cx;
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 /* Strace facility. See strace.cc, sys/strace.h and utils/strace.cc. */
396 extern DWORD strace_active;
397
398 /* Invisible window initialization/termination. */
399 HWND __stdcall gethwnd (void);
400 void __stdcall window_terminate (void);
401
402 /* Globals that handle initialization of winsock in a child process. */
403 extern HANDLE wsock32_handle;
404
405 /* Globals that handle initialization of netapi in a child process. */
406 extern HANDLE netapi32_handle;
407
408 /* debug_on_trap support. see exceptions.cc:try_to_debug() */
409 extern "C" void error_start_init (const char*);
410 extern "C" int try_to_debug ();
411
412 /**************************** Miscellaneous ******************************/
413
414 const char * __stdcall find_exec (const char *name, char *buf, const char *winenv = "PATH=",
415 int null_if_notfound = 0, const char **known_suffix = NULL);
416
417 /* File manipulation */
418 int __stdcall get_file_attribute (int, const char *, int *);
419 int __stdcall set_file_attribute (int, const char *, int);
420 int __stdcall set_file_attribute (int, const char *, uid_t, gid_t, int, const char *);
421 void __stdcall set_std_handle (int);
422 int __stdcall writable_directory (const char *file);
423 int __stdcall stat_dev (DWORD, int, unsigned long, struct stat *);
424 extern BOOL allow_ntsec;
425
426 /* `lookup_name' should be called instead of LookupAccountName.
427 * logsrv may be NULL, in this case only the local system is used for lookup.
428 * The buffer for ret_sid (40 Bytes) has to be allocated by the caller! */
429 BOOL __stdcall lookup_name (const char *name, const char *logsrv, PSID ret_sid);
430
431 unsigned long __stdcall hash_path_name (unsigned long hash, const char *name);
432 void __stdcall nofinalslash (const char *src, char *dst);
433 extern "C" char *__stdcall rootdir (char *full_path);
434
435 void __stdcall mark (const char *, int);
436
437 extern "C" int _spawnve (HANDLE hToken, int mode, const char *path,
438 const char *const *argv, const char *const *envp);
439 int __stdcall spawn_guts (HANDLE hToken, const char *prog_arg,
440 const char *const *argv, const char *const envp[],
441 pinfo *child, int mode);
442
443 /* For mmaps across fork(). */
444 int __stdcall recreate_mmaps_after_fork (void *);
445 void __stdcall set_child_mmap_ptr (pinfo *);
446
447 /* String manipulation */
448 char *__stdcall strccpy (char *s1, const char **s2, char c);
449 int __stdcall strcasematch (const char *s1, const char *s2);
450 int __stdcall strncasematch (const char *s1, const char *s2, size_t n);
451 char *__stdcall strcasestr (const char *searchee, const char *lookfor);
452
453 /* Time related */
454 void __stdcall totimeval (struct timeval *dst, FILETIME * src, int sub, int flag);
455 long __stdcall to_time_t (FILETIME * ptr);
456
457 /* pinfo table manipulation */
458 #ifndef lock_pinfo_for_update
459 int __stdcall lock_pinfo_for_update (DWORD timeout);
460 #endif
461 void unlock_pinfo (void);
462 pinfo *__stdcall set_myself (pinfo *);
463
464 /* Retrieve a security descriptor that allows all access */
465 SECURITY_DESCRIPTOR *__stdcall get_null_sd (void);
466
467 int __stdcall get_id_from_sid (PSID, BOOL);
468 extern inline int get_uid_from_sid (PSID psid) { return get_id_from_sid (psid, FALSE);}
469 extern inline int get_gid_from_sid (PSID psid) { return get_id_from_sid (psid, TRUE); }
470
471 int __stdcall NTReadEA (const char *file, const char *attrname, char *buf, int len);
472 BOOL __stdcall NTWriteEA (const char *file, const char *attrname, char *buf, int len);
473
474 void __stdcall set_console_title (char *);
475 void set_console_handler ();
476
477 void __stdcall fill_rusage (struct rusage *, HANDLE);
478 void __stdcall add_rusage (struct rusage *, struct rusage *);
479
480 void set_winsock_errno ();
481
482 /**************************** Exports ******************************/
483
484 extern "C" {
485 int cygwin_select (int , fd_set *, fd_set *, fd_set *,
486 struct timeval *to);
487 int cygwin_gethostname (char *__name, size_t __len);
488
489 int kill_pgrp (pid_t, int);
490 int _kill (int, int);
491 int _raise (int sig);
492
493 int getdtablesize ();
494 void setdtablesize (int);
495
496 extern char _data_start__, _data_end__, _bss_start__, _bss_end__;
497 extern void (*__CTOR_LIST__) (void);
498 extern void (*__DTOR_LIST__) (void);
499 };
500
501 /*************************** Unsorted ******************************/
502
503 /* The size of the console title */
504 #define TITLESIZE 1024
505
506 #define WM_ASYNCIO 0x8000 // WM_APP
507
508 /* Note that MAX_PATH is defined in the windows headers */
509 /* There is also PATH_MAX and MAXPATHLEN.
510 PATH_MAX is from Posix and does *not* include the trailing NUL.
511 MAXPATHLEN is from Unix.
512
513 Thou shalt use MAX_PATH throughout. It avoids the NUL vs no-NUL
514 issue and is neither of the Unixy ones [so we can punt on which
515 one is the right one to use]. */
516
517 /* Initial and increment values for cygwin's fd table */
518 #define NOFILE_INCR 32
519
520 #ifdef __cplusplus
521 extern "C" {
522 #endif
523 #include <sys/reent.h>
524
525 #define STD_RBITS S_IRUSR | S_IRGRP | S_IROTH
526 #define STD_WBITS S_IWUSR
527 #define STD_XBITS S_IXUSR | S_IXGRP | S_IXOTH
528
529 #define O_NOSYMLINK 0x080000
530 #define O_DIROPEN 0x100000
531
532 #ifdef __cplusplus
533 }
534 #endif
535
536 /*************************** Environment ******************************/
537
538 /* The structure below is used to control conversion to/from posix-style
539 * file specs. Currently, only PATH and HOME are converted, but PATH
540 * needs to use a "convert path list" function while HOME needs a simple
541 * "convert to posix/win32". For the simple case, where a calculated length
542 * is required, just return MAX_PATH. *FIXME*
543 */
544 struct win_env
545 {
546 const char *name;
547 size_t namelen;
548 char *posix;
549 char *native;
550 int (*toposix) (const char *, char *);
551 int (*towin32) (const char *, char *);
552 int (*posix_len) (const char *);
553 int (*win32_len) (const char *);
554 void add_cache (const char *in_posix, const char *in_native = NULL);
555 const char * get_native () {return native ? native + namelen : NULL;}
556 };
557
558 win_env * __stdcall getwinenv (const char *name, const char *posix = NULL);
559
560 char * __stdcall winenv (const char * const *);
561 extern char **__cygwin_environ;
562
563 /* The title on program start. */
564 extern char *old_title;
565 extern BOOL display_title;
566
567
568 /*************************** errno manipulation ******************************/
569
570 void seterrno_from_win_error (const char *file, int line, int code);
571 void seterrno (const char *, int line);
572
573 #define __seterrno() seterrno (__FILE__, __LINE__)
574 #define __seterrno_from_win_error(val) seterrno_from_win_error (__FILE__, __LINE__, val)
575 #undef errno
576 #define errno dont_use_this_since_were_in_a_shared library
577 #define set_errno(val) (_impure_ptr->_errno = (val))
578 #define get_errno() (_impure_ptr->_errno)
579 extern "C" void __stdcall set_sig_errno (int e);
580
581 class save_errno
582 {
583 int saved;
584 public:
585 save_errno () {saved = get_errno ();}
586 save_errno (int what) {saved = get_errno (); set_errno (what); }
587 void set (int what) {set_errno (what); saved = what;}
588 void reset () {saved = get_errno ();}
589 ~save_errno () {set_errno (saved);}
590 };
591
592 extern const char *__sp_fn;
593 extern int __sp_ln;
This page took 0.0653 seconds and 6 git commands to generate.