1 /* winsup.h: main Cygwin header file.
3 Copyright 1996, 1997, 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
15 #define __INSIDE_CYGWIN__
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
26 #include <sys/types.h>
27 #include <sys/strace.h>
28 #include <sys/resource.h>
34 #define strchr cygwin_strchr
35 extern inline char * strchr(const char * s
, int c
)
37 register char * __res
;
40 "1:\tmovb (%1),%%al\n\t"
48 :"=a" (__res
), "=r" (s
)
55 /* Used for runtime OS check/decisions. */
56 enum os_type
{winNT
= 1, win95
, win98
, win32s
, unknown
};
57 extern os_type os_being_run
;
59 /* Used to check if Cygwin DLL is dynamically loaded. */
60 extern int dynamically_loaded
;
62 #include <cygwin/version.h>
64 #define TITLESIZE 1024
65 #define MAX_USER_NAME 20
66 #define DEFAULT_UID 500
67 #define DEFAULT_GID 544
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))
82 extern HANDLE hMainThread
;
83 extern HANDLE hMainProc
;
85 /* Now that pinfo has been defined, include... */
90 #include <sys/cygwin.h>
92 /********************** Application Interface **************************/
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!
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.
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.
115 /* The offset of these 3 values can never change. */
116 /* magic_biscuit is the size of this class and should never change. */
121 struct _reent
**impure_ptr_ptr
;
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);
132 int (*main
)(int, char **, char **);
133 void (**ctors
)(void);
134 void (**dtors
)(void);
142 void *(*calloc
)(size_t, size_t);
143 /* For future expansion of values set by the app. */
144 void *public_reserved
[4];
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. */
150 /* non-zero of ctors have been run. Inherited from parent. */
153 /* These will be non-zero if the above (malloc,free,realloc) have been
155 /* FIXME: not currently used */
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 */
165 HANDLE reserved1
; /* unused */
167 /* Non-zero means the task was forked. The value is the pid.
168 Inherited from parent. */
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
178 ResourceLocks
*resourcelocks
;
179 MTinterface
*threadinterface
;
180 void *internal_reserved
[6];
182 void *internal_reserved
[8];
186 extern per_process
*user_data
; /* Pointer into application's static data */
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)
196 fhandler_base
**fds_on_hold
;
197 int first_fd_for_open
;
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
,
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
);
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. */
229 class host_dependent_constants
234 /* Used by fhandler_disk_file::lock which needs a platform-specific
235 upper word value for locking entire files. */
238 /* fhandler_base::open requires host dependent file sharing
243 extern host_dependent_constants host_dependent
;
246 extern HANDLE pinfo_mutex
;
247 extern HANDLE title_mutex
;
251 /*************************** Per Thread ******************************/
253 #define PER_THREAD_FORK_CLEAR ((void *)0xffffffff)
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
;}
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 ()
269 void *s
= new char [size ()];
270 memset (s
, 0, size ());
276 class per_thread_waitq
: public per_thread
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
);}
292 int is_active () { return pid
< 0; }
295 class per_thread_vfork
: public per_thread
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
);}
304 struct signal_dispatch
316 struct per_thread_signal_dispatch
: public per_thread
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
);}
323 extern per_thread_waitq waitq_storage
;
324 extern per_thread_vfork vfork_storage
;
325 extern per_thread_signal_dispatch signal_dispatch_storage
;
327 extern per_thread
*threadstuff
[];
329 /**************************** Convenience ******************************/
331 #define NO_COPY __attribute__((section(".data_cygwin_nocopy")))
333 /* Used when treating / and \ as equivalent. */
334 #define SLASH_P(ch) \
337 ((__c) == '/' || (__c) == '\\'); \
340 /* Convert a signal to a signal mask */
341 #define SIGTOMASK(sig) (1<<((sig) - signal_shift_subtract))
342 extern unsigned int signal_shift_subtract
;
347 #define MARK() mark (__FILE__,__LINE__)
350 #define api_fatal(fmt, args...) \
351 __api_fatal ("%P: *** " fmt, ## args)
354 #define issep(ch) (strchr (" \t\n\r", (ch)) != NULL)
356 #define isdirsep SLASH_P
357 #define isabspath(p) \
358 (isdirsep (*(p)) || (isalpha (*(p)) && (p)[1] == ':'))
360 /******************** Initialization/Termination **********************/
362 /* cygwin .dll initialization */
363 void dll_crt0 (per_process
*);
365 /* dynamically loaded dll initialization */
366 extern "C" int dll_dllcrt0 (HMODULE
,per_process
*);
368 /* dynamically loaded dll initialization for non-cygwin apps */
369 extern "C" int dll_noncygwin_dllcrt0 (HMODULE
, per_process
*);
371 /* exit the program */
372 extern "C" void __stdcall
do_exit (int) __attribute__ ((noreturn
));
374 /* Initialize the environment */
375 void environ_init (void);
377 /* Heap management. */
378 void heap_init (void);
379 void malloc_init (void);
382 void dtable_init (void);
383 void hinfo_init (void);
387 void uinfo_init (void);
390 void events_init (void);
391 void events_terminate (void);
393 void __stdcall
close_all_files (void);
395 /* Strace facility. See strace.cc, sys/strace.h and utils/strace.cc. */
396 extern DWORD strace_active
;
398 /* Invisible window initialization/termination. */
399 HWND __stdcall
gethwnd (void);
400 void __stdcall
window_terminate (void);
402 /* Globals that handle initialization of winsock in a child process. */
403 extern HANDLE wsock32_handle
;
405 /* Globals that handle initialization of netapi in a child process. */
406 extern HANDLE netapi32_handle
;
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 ();
412 /**************************** Miscellaneous ******************************/
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
);
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
;
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
);
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
);
435 void __stdcall
mark (const char *, int);
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
);
443 /* For mmaps across fork(). */
444 int __stdcall
recreate_mmaps_after_fork (void *);
445 void __stdcall
set_child_mmap_ptr (pinfo
*);
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
);
454 void __stdcall
totimeval (struct timeval
*dst
, FILETIME
* src
, int sub
, int flag
);
455 long __stdcall
to_time_t (FILETIME
* ptr
);
457 /* pinfo table manipulation */
458 #ifndef lock_pinfo_for_update
459 int __stdcall
lock_pinfo_for_update (DWORD timeout
);
461 void unlock_pinfo (void);
462 pinfo
*__stdcall
set_myself (pinfo
*);
464 /* Retrieve a security descriptor that allows all access */
465 SECURITY_DESCRIPTOR
*__stdcall
get_null_sd (void);
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
); }
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
);
474 void __stdcall
set_console_title (char *);
475 void set_console_handler ();
477 void __stdcall
fill_rusage (struct rusage
*, HANDLE
);
478 void __stdcall
add_rusage (struct rusage
*, struct rusage
*);
480 void set_winsock_errno ();
482 /**************************** Exports ******************************/
485 int cygwin_select (int , fd_set
*, fd_set
*, fd_set
*,
487 int cygwin_gethostname (char *__name
, size_t __len
);
489 int kill_pgrp (pid_t
, int);
490 int _kill (int, int);
491 int _raise (int sig
);
493 int getdtablesize ();
494 void setdtablesize (int);
496 extern char _data_start__
, _data_end__
, _bss_start__
, _bss_end__
;
497 extern void (*__CTOR_LIST__
) (void);
498 extern void (*__DTOR_LIST__
) (void);
501 /*************************** Unsorted ******************************/
503 /* The size of the console title */
504 #define TITLESIZE 1024
506 #define WM_ASYNCIO 0x8000 // WM_APP
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.
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]. */
517 /* Initial and increment values for cygwin's fd table */
518 #define NOFILE_INCR 32
523 #include <sys/reent.h>
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
529 #define O_NOSYMLINK 0x080000
530 #define O_DIROPEN 0x100000
536 /*************************** Environment ******************************/
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*
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
;}
558 win_env
* __stdcall
getwinenv (const char *name
, const char *posix
= NULL
);
560 char * __stdcall
winenv (const char * const *);
561 extern char **__cygwin_environ
;
563 /* The title on program start. */
564 extern char *old_title
;
565 extern BOOL display_title
;
568 /*************************** errno manipulation ******************************/
570 void seterrno_from_win_error (const char *file
, int line
, int code
);
571 void seterrno (const char *, int line
);
573 #define __seterrno() seterrno (__FILE__, __LINE__)
574 #define __seterrno_from_win_error(val) seterrno_from_win_error (__FILE__, __LINE__, val)
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
);
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
);}
592 extern const char *__sp_fn
;