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
)
56 /* Used for runtime OS check/decisions. */
57 enum os_type
{winNT
= 1, win95
, win98
, win32s
, unknown
};
58 extern os_type os_being_run
;
60 /* Used to check if Cygwin DLL is dynamically loaded. */
61 extern int dynamically_loaded
;
63 #include <cygwin/version.h>
65 #define TITLESIZE 1024
66 #define MAX_USER_NAME 20
67 #define DEFAULT_UID 500
68 #define DEFAULT_GID 544
70 #define MAX_SID_LEN 40
71 #define MAX_HOST_NAME 256
73 /* status bit manipulation */
74 #define __ISSETF(what, x, prefix) \
75 ((what)->status & prefix##_##x)
76 #define __SETF(what, x, prefix) \
77 ((what)->status |= prefix##_##x)
78 #define __CLEARF(what, x, prefix) \
79 ((what)->status &= ~prefix##_##x)
80 #define __CONDSETF(n, what, x, prefix) \
81 ((n) ? __SETF (what, x, prefix) : __CLEARF (what, x, prefix))
86 extern HANDLE hMainThread
;
87 extern HANDLE hMainProc
;
89 /* Now that pinfo has been defined, include... */
95 #include <sys/cygwin.h>
97 /********************** Application Interface **************************/
99 extern "C" per_process __cygwin_user_data
; /* Pointer into application's static data */
100 #define user_data (&__cygwin_user_data)
102 /* We use the following to test that sizeof hasn't changed. When adding
103 or deleting members, insert fillers or use the reserved entries.
104 Do not change this value. */
105 #define SIZEOF_PER_PROCESS (42 * 4)
110 fhandler_base
**fds_on_hold
;
111 int first_fd_for_open
;
114 hinfo () {first_fd_for_open
= 3;}
115 int vfork_child_dup ();
116 void vfork_parent_restore ();
117 fhandler_base
*dup_worker (fhandler_base
*oldfh
);
118 int extend (int howmuch
);
119 void fixup_after_fork (HANDLE parent
);
120 fhandler_base
*build_fhandler (int fd
, DWORD dev
, const char *name
,
122 fhandler_base
*build_fhandler (int fd
, const char *name
, HANDLE h
);
123 int not_open (int n
);
124 int find_unused_handle (int start
);
125 int find_unused_handle () { return find_unused_handle (first_fd_for_open
);}
126 void release (int fd
);
127 void init_std_file_from_handle (int fd
, HANDLE handle
, DWORD access
, const char *name
);
128 int dup2 (int oldfd
, int newfd
);
129 int linearize_fd_array (unsigned char *buf
, int buflen
);
130 LPBYTE
de_linearize_fd_array (LPBYTE buf
);
131 fhandler_base
*operator [](int fd
) { return fds
[fd
]; }
132 select_record
*select_read (int fd
, select_record
*s
);
133 select_record
*select_write (int fd
, select_record
*s
);
134 select_record
*select_except (int fd
, select_record
*s
);
137 /******************* Host-dependent constants **********************/
138 /* Portions of the cygwin DLL require special constants whose values
139 are dependent on the host system. Rather than dynamically
140 determine those values whenever they are required, initialize these
141 values once at process start-up. */
143 class host_dependent_constants
148 /* Used by fhandler_disk_file::lock which needs a platform-specific
149 upper word value for locking entire files. */
152 /* fhandler_base::open requires host dependent file sharing
157 extern host_dependent_constants host_dependent
;
160 extern HANDLE pinfo_mutex
;
161 extern HANDLE title_mutex
;
165 /*************************** Per Thread ******************************/
167 #define PER_THREAD_FORK_CLEAR ((void *)0xffffffff)
173 per_thread (int forkval
= 1) {tls
= TlsAlloc (); clear_on_fork_p
= forkval
;}
174 DWORD
get_tls () {return tls
;}
175 int clear_on_fork () {return clear_on_fork_p
;}
177 virtual void *get () {return TlsGetValue (get_tls ());}
178 virtual size_t size () {return 0;}
179 virtual void set (void *s
= NULL
);
180 virtual void set (int n
) {TlsSetValue (get_tls (), (void *)n
);}
181 virtual void *create ()
183 void *s
= new char [size ()];
184 memset (s
, 0, size ());
190 class per_thread_waitq
: public per_thread
193 per_thread_waitq () : per_thread (0) {}
194 void *get () {return (waitq
*) this->per_thread::get ();}
195 void *create () {return (waitq
*) this->per_thread::create ();}
196 size_t size () {return sizeof (waitq
);}
206 int is_active () { return pid
< 0; }
209 class per_thread_vfork
: public per_thread
212 vfork_save
*val () { return (vfork_save
*) this->per_thread::get (); }
213 vfork_save
*create () {return (vfork_save
*) this->per_thread::create ();}
214 size_t size () {return sizeof (vfork_save
);}
218 struct signal_dispatch
226 DWORD
*retaddr_on_stack
;
230 struct per_thread_signal_dispatch
: public per_thread
232 signal_dispatch
*get () { return (signal_dispatch
*) this->per_thread::get (); }
233 signal_dispatch
*create () {return (signal_dispatch
*) this->per_thread::create ();}
234 size_t size () {return sizeof (signal_dispatch
);}
237 extern per_thread_waitq waitq_storage
;
238 extern per_thread_vfork vfork_storage
;
239 extern per_thread_signal_dispatch signal_dispatch_storage
;
241 extern per_thread
*threadstuff
[];
243 /**************************** Convenience ******************************/
245 #define NO_COPY __attribute__((section(".data_cygwin_nocopy")))
247 /* Used when treating / and \ as equivalent. */
248 #define SLASH_P(ch) \
251 ((__c) == '/' || (__c) == '\\'); \
254 /* Convert a signal to a signal mask */
255 #define SIGTOMASK(sig) (1<<((sig) - signal_shift_subtract))
256 extern unsigned int signal_shift_subtract
;
261 #define MARK() mark (__FILE__,__LINE__)
264 #define api_fatal(fmt, args...) \
265 __api_fatal ("%P: *** " fmt, ## args)
268 #define issep(ch) (strchr (" \t\n\r", (ch)) != NULL)
270 #define isdirsep SLASH_P
271 #define isabspath(p) \
272 (isdirsep (*(p)) || (isalpha (*(p)) && (p)[1] == ':' && (!(p)[2] || isdirsep ((p)[2]))))
274 /******************** Initialization/Termination **********************/
276 /* cygwin .dll initialization */
277 void dll_crt0 (per_process
*);
278 extern "C" void __stdcall
_dll_crt0 ();
280 /* dynamically loaded dll initialization */
281 extern "C" int dll_dllcrt0 (HMODULE
, per_process
*);
283 /* dynamically loaded dll initialization for non-cygwin apps */
284 extern "C" int dll_noncygwin_dllcrt0 (HMODULE
, per_process
*);
286 /* exit the program */
287 extern "C" void __stdcall
do_exit (int) __attribute__ ((noreturn
));
289 /* Initialize the environment */
290 void environ_init (int);
292 /* Heap management. */
293 void heap_init (void);
294 void malloc_init (void);
297 void dtable_init (void);
298 void hinfo_init (void);
302 void uinfo_init (void);
305 void events_init (void);
306 void events_terminate (void);
308 void __stdcall
close_all_files (void);
310 extern class strace strace
;
312 /* Invisible window initialization/termination. */
313 HWND __stdcall
gethwnd (void);
314 void __stdcall
window_terminate (void);
316 /* Globals that handle initialization of winsock in a child process. */
317 extern HANDLE wsock32_handle
;
319 /* Globals that handle initialization of netapi in a child process. */
320 extern HANDLE netapi32_handle
;
322 /* debug_on_trap support. see exceptions.cc:try_to_debug() */
323 extern "C" void error_start_init (const char*);
324 extern "C" int try_to_debug ();
326 extern int cygwin_finished_initializing
;
328 /**************************** Miscellaneous ******************************/
330 const char * __stdcall
find_exec (const char *name
, path_conv
& buf
, const char *winenv
= "PATH=",
331 int null_if_notfound
= 0, const char **known_suffix
= NULL
);
333 /* File manipulation */
334 int __stdcall
set_process_privileges ();
335 int __stdcall
get_file_attribute (int, const char *, int *,
336 uid_t
* = NULL
, gid_t
* = NULL
);
337 int __stdcall
set_file_attribute (int, const char *, int);
338 int __stdcall
set_file_attribute (int, const char *, uid_t
, gid_t
, int, const char *);
339 void __stdcall
set_std_handle (int);
340 int __stdcall
writable_directory (const char *file
);
341 int __stdcall
stat_dev (DWORD
, int, unsigned long, struct stat
*);
342 extern BOOL allow_ntsec
;
344 /* `lookup_name' should be called instead of LookupAccountName.
345 * logsrv may be NULL, in this case only the local system is used for lookup.
346 * The buffer for ret_sid (40 Bytes) has to be allocated by the caller! */
347 BOOL __stdcall
lookup_name (const char *, const char *, PSID
);
348 char *__stdcall
convert_sid_to_string_sid (PSID
, char *);
349 PSID __stdcall
convert_string_sid_to_sid (PSID
, const char *);
350 BOOL __stdcall
get_pw_sid (PSID
, struct passwd
*);
352 unsigned long __stdcall
hash_path_name (unsigned long hash
, const char *name
);
353 void __stdcall
nofinalslash (const char *src
, char *dst
);
354 extern "C" char *__stdcall
rootdir (char *full_path
);
356 void __stdcall
mark (const char *, int);
358 extern "C" int _spawnve (HANDLE hToken
, int mode
, const char *path
,
359 const char *const *argv
, const char *const *envp
);
360 int __stdcall
spawn_guts (HANDLE hToken
, const char *prog_arg
,
361 const char *const *argv
, const char *const envp
[],
362 pinfo
*child
, int mode
);
364 /* For mmaps across fork(). */
365 int __stdcall
recreate_mmaps_after_fork (void *);
366 void __stdcall
set_child_mmap_ptr (pinfo
*);
368 /* String manipulation */
369 char *__stdcall
strccpy (char *s1
, const char **s2
, char c
);
370 int __stdcall
strcasematch (const char *s1
, const char *s2
);
371 int __stdcall
strncasematch (const char *s1
, const char *s2
, size_t n
);
372 char *__stdcall
strcasestr (const char *searchee
, const char *lookfor
);
375 void __stdcall
totimeval (struct timeval
*dst
, FILETIME
* src
, int sub
, int flag
);
376 long __stdcall
to_time_t (FILETIME
* ptr
);
378 /* pinfo table manipulation */
379 #ifndef lock_pinfo_for_update
380 int __stdcall
lock_pinfo_for_update (DWORD timeout
);
382 void unlock_pinfo (void);
383 pinfo
*__stdcall
set_myself (pinfo
*);
385 /* Retrieve a security descriptor that allows all access */
386 SECURITY_DESCRIPTOR
*__stdcall
get_null_sd (void);
388 int __stdcall
get_id_from_sid (PSID
, BOOL
);
389 extern inline int get_uid_from_sid (PSID psid
) { return get_id_from_sid (psid
, FALSE
);}
390 extern inline int get_gid_from_sid (PSID psid
) { return get_id_from_sid (psid
, TRUE
); }
392 int __stdcall
NTReadEA (const char *file
, const char *attrname
, char *buf
, int len
);
393 BOOL __stdcall
NTWriteEA (const char *file
, const char *attrname
, char *buf
, int len
);
395 void __stdcall
set_console_title (char *);
396 void set_console_handler ();
398 void __stdcall
fill_rusage (struct rusage
*, HANDLE
);
399 void __stdcall
add_rusage (struct rusage
*, struct rusage
*);
401 void set_winsock_errno ();
403 /**************************** Exports ******************************/
406 int cygwin_select (int , fd_set
*, fd_set
*, fd_set
*,
408 int cygwin_gethostname (char *__name
, size_t __len
);
410 int kill_pgrp (pid_t
, int);
411 int _kill (int, int);
412 int _raise (int sig
);
414 int getdtablesize ();
415 void setdtablesize (int);
417 extern char _data_start__
, _data_end__
, _bss_start__
, _bss_end__
;
418 extern void (*__CTOR_LIST__
) (void);
419 extern void (*__DTOR_LIST__
) (void);
422 /*************************** Unsorted ******************************/
424 #define WM_ASYNCIO 0x8000 // WM_APP
426 /* Note that MAX_PATH is defined in the windows headers */
427 /* There is also PATH_MAX and MAXPATHLEN.
428 PATH_MAX is from Posix and does *not* include the trailing NUL.
429 MAXPATHLEN is from Unix.
431 Thou shalt use MAX_PATH throughout. It avoids the NUL vs no-NUL
432 issue and is neither of the Unixy ones [so we can punt on which
433 one is the right one to use]. */
435 /* Initial and increment values for cygwin's fd table */
436 #define NOFILE_INCR 32
441 #include <sys/reent.h>
443 #define STD_RBITS (S_IRUSR | S_IRGRP | S_IROTH)
444 #define STD_WBITS (S_IWUSR)
445 #define STD_XBITS (S_IXUSR | S_IXGRP | S_IXOTH)
447 #define O_NOSYMLINK 0x080000
448 #define O_DIROPEN 0x100000
454 /*************************** Environment ******************************/
456 /* The structure below is used to control conversion to/from posix-style
457 * file specs. Currently, only PATH and HOME are converted, but PATH
458 * needs to use a "convert path list" function while HOME needs a simple
459 * "convert to posix/win32". For the simple case, where a calculated length
460 * is required, just return MAX_PATH. *FIXME*
468 int (*toposix
) (const char *, char *);
469 int (*towin32
) (const char *, char *);
470 int (*posix_len
) (const char *);
471 int (*win32_len
) (const char *);
472 void add_cache (const char *in_posix
, const char *in_native
= NULL
);
473 const char * get_native () {return native
? native
+ namelen
: NULL
;}
476 win_env
* __stdcall
getwinenv (const char *name
, const char *posix
= NULL
);
478 void __stdcall
update_envptrs ();
479 char * __stdcall
winenv (const char * const *, int);
480 extern char **__cygwin_environ
, ***main_environ
;
481 extern char __stdcall
**cur_environ ();
482 #define environ (cur_environ ())
484 /* The title on program start. */
485 extern char *old_title
;
486 extern BOOL display_title
;
489 /*************************** errno manipulation ******************************/
491 void seterrno_from_win_error (const char *file
, int line
, int code
);
492 void seterrno (const char *, int line
);
494 #define __seterrno() seterrno (__FILE__, __LINE__)
495 #define __seterrno_from_win_error(val) seterrno_from_win_error (__FILE__, __LINE__, val)
497 #define errno dont_use_this_since_were_in_a_shared library
498 #define set_errno(val) (_impure_ptr->_errno = (val))
499 #define get_errno() (_impure_ptr->_errno)
500 extern "C" void __stdcall
set_sig_errno (int e
);
506 save_errno () {saved
= get_errno ();}
507 save_errno (int what
) {saved
= get_errno (); set_errno (what
); }
508 void set (int what
) {set_errno (what
); saved
= what
;}
509 void reset () {saved
= get_errno ();}
510 ~save_errno () {set_errno (saved
);}
513 extern const char *__sp_fn
;