]> sourceware.org Git - newlib-cygwin.git/blob - winsup/cygwin/winsup.h
* environ.cc: Use new definition of "environ" throughout.
[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 #include <wincrypt.h>
55
56 /* Used for runtime OS check/decisions. */
57 enum os_type {winNT = 1, win95, win98, win32s, unknown};
58 extern os_type os_being_run;
59
60 /* Used to check if Cygwin DLL is dynamically loaded. */
61 extern int dynamically_loaded;
62
63 #include <cygwin/version.h>
64
65 #define TITLESIZE 1024
66 #define MAX_USER_NAME 20
67 #define DEFAULT_UID 500
68 #define DEFAULT_GID 544
69
70 #define MAX_SID_LEN 40
71 #define MAX_HOST_NAME 256
72
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))
82
83 #include "thread.h"
84 #include "shared.h"
85
86 extern HANDLE hMainThread;
87 extern HANDLE hMainProc;
88
89 /* Now that pinfo has been defined, include... */
90 #include "debug.h"
91 #include "sync.h"
92 #include "sigproc.h"
93 #include "fhandler.h"
94 #include "path.h"
95 #include <sys/cygwin.h>
96
97 /********************** Application Interface **************************/
98
99 extern "C" per_process __cygwin_user_data; /* Pointer into application's static data */
100 #define user_data (&__cygwin_user_data)
101
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)
106
107 class hinfo
108 {
109 fhandler_base **fds;
110 fhandler_base **fds_on_hold;
111 int first_fd_for_open;
112 public:
113 size_t size;
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,
121 int unit = -1);
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);
135 };
136
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. */
142
143 class host_dependent_constants
144 {
145 public:
146 void init (void);
147
148 /* Used by fhandler_disk_file::lock which needs a platform-specific
149 upper word value for locking entire files. */
150 DWORD win32_upper;
151
152 /* fhandler_base::open requires host dependent file sharing
153 attributes. */
154 int shared;
155 };
156
157 extern host_dependent_constants host_dependent;
158
159 /* Events/mutexes */
160 extern HANDLE pinfo_mutex;
161 extern HANDLE title_mutex;
162
163
164
165 /*************************** Per Thread ******************************/
166
167 #define PER_THREAD_FORK_CLEAR ((void *)0xffffffff)
168 class per_thread
169 {
170 DWORD tls;
171 int clear_on_fork_p;
172 public:
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;}
176
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 ()
182 {
183 void *s = new char [size ()];
184 memset (s, 0, size ());
185 set (s);
186 return s;
187 }
188 };
189
190 class per_thread_waitq : public per_thread
191 {
192 public:
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);}
197 };
198
199 struct vfork_save
200 {
201 int pid;
202 jmp_buf j;
203 char **vfork_ebp;
204 char *caller_ebp;
205 char *retaddr;
206 int is_active () { return pid < 0; }
207 };
208
209 class per_thread_vfork : public per_thread
210 {
211 public:
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);}
215 };
216
217 extern "C" {
218 struct signal_dispatch
219 {
220 int arg;
221 void (*func) (int);
222 int sig;
223 int saved_errno;
224 DWORD oldmask;
225 DWORD retaddr;
226 DWORD *retaddr_on_stack;
227 };
228 };
229
230 struct per_thread_signal_dispatch : public per_thread
231 {
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);}
235 };
236
237 extern per_thread_waitq waitq_storage;
238 extern per_thread_vfork vfork_storage;
239 extern per_thread_signal_dispatch signal_dispatch_storage;
240
241 extern per_thread *threadstuff[];
242
243 /**************************** Convenience ******************************/
244
245 #define NO_COPY __attribute__((section(".data_cygwin_nocopy")))
246
247 /* Used when treating / and \ as equivalent. */
248 #define SLASH_P(ch) \
249 ({ \
250 char __c = (ch); \
251 ((__c) == '/' || (__c) == '\\'); \
252 })
253
254 /* Convert a signal to a signal mask */
255 #define SIGTOMASK(sig) (1<<((sig) - signal_shift_subtract))
256 extern unsigned int signal_shift_subtract;
257
258 #ifdef NOSTRACE
259 #define MARK() 0
260 #else
261 #define MARK() mark (__FILE__,__LINE__)
262 #endif
263
264 #define api_fatal(fmt, args...) \
265 __api_fatal ("%P: *** " fmt, ## args)
266
267 #undef issep
268 #define issep(ch) (strchr (" \t\n\r", (ch)) != NULL)
269
270 #define isdirsep SLASH_P
271 #define isabspath(p) \
272 (isdirsep (*(p)) || (isalpha (*(p)) && (p)[1] == ':' && (!(p)[2] || isdirsep ((p)[2]))))
273
274 /******************** Initialization/Termination **********************/
275
276 /* cygwin .dll initialization */
277 void dll_crt0 (per_process *);
278 extern "C" void __stdcall _dll_crt0 ();
279
280 /* dynamically loaded dll initialization */
281 extern "C" int dll_dllcrt0 (HMODULE, per_process*);
282
283 /* dynamically loaded dll initialization for non-cygwin apps */
284 extern "C" int dll_noncygwin_dllcrt0 (HMODULE, per_process *);
285
286 /* exit the program */
287 extern "C" void __stdcall do_exit (int) __attribute__ ((noreturn));
288
289 /* Initialize the environment */
290 void environ_init (int);
291
292 /* Heap management. */
293 void heap_init (void);
294 void malloc_init (void);
295
296 /* fd table */
297 void dtable_init (void);
298 void hinfo_init (void);
299 extern hinfo dtable;
300
301 /* UID/GID */
302 void uinfo_init (void);
303
304 /* various events */
305 void events_init (void);
306 void events_terminate (void);
307
308 void __stdcall close_all_files (void);
309
310 extern class strace strace;
311
312 /* Invisible window initialization/termination. */
313 HWND __stdcall gethwnd (void);
314 void __stdcall window_terminate (void);
315
316 /* Globals that handle initialization of winsock in a child process. */
317 extern HANDLE wsock32_handle;
318
319 /* Globals that handle initialization of netapi in a child process. */
320 extern HANDLE netapi32_handle;
321
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 ();
325
326 extern int cygwin_finished_initializing;
327
328 /**************************** Miscellaneous ******************************/
329
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);
332
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;
343
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 *);
351
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);
355
356 void __stdcall mark (const char *, int);
357
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);
363
364 /* For mmaps across fork(). */
365 int __stdcall recreate_mmaps_after_fork (void *);
366 void __stdcall set_child_mmap_ptr (pinfo *);
367
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);
373
374 /* Time related */
375 void __stdcall totimeval (struct timeval *dst, FILETIME * src, int sub, int flag);
376 long __stdcall to_time_t (FILETIME * ptr);
377
378 /* pinfo table manipulation */
379 #ifndef lock_pinfo_for_update
380 int __stdcall lock_pinfo_for_update (DWORD timeout);
381 #endif
382 void unlock_pinfo (void);
383 pinfo *__stdcall set_myself (pinfo *);
384
385 /* Retrieve a security descriptor that allows all access */
386 SECURITY_DESCRIPTOR *__stdcall get_null_sd (void);
387
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); }
391
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);
394
395 void __stdcall set_console_title (char *);
396 void set_console_handler ();
397
398 void __stdcall fill_rusage (struct rusage *, HANDLE);
399 void __stdcall add_rusage (struct rusage *, struct rusage *);
400
401 void set_winsock_errno ();
402
403 /**************************** Exports ******************************/
404
405 extern "C" {
406 int cygwin_select (int , fd_set *, fd_set *, fd_set *,
407 struct timeval *to);
408 int cygwin_gethostname (char *__name, size_t __len);
409
410 int kill_pgrp (pid_t, int);
411 int _kill (int, int);
412 int _raise (int sig);
413
414 int getdtablesize ();
415 void setdtablesize (int);
416
417 extern char _data_start__, _data_end__, _bss_start__, _bss_end__;
418 extern void (*__CTOR_LIST__) (void);
419 extern void (*__DTOR_LIST__) (void);
420 };
421
422 /*************************** Unsorted ******************************/
423
424 #define WM_ASYNCIO 0x8000 // WM_APP
425
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.
430
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]. */
434
435 /* Initial and increment values for cygwin's fd table */
436 #define NOFILE_INCR 32
437
438 #ifdef __cplusplus
439 extern "C" {
440 #endif
441 #include <sys/reent.h>
442
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)
446
447 #define O_NOSYMLINK 0x080000
448 #define O_DIROPEN 0x100000
449
450 #ifdef __cplusplus
451 }
452 #endif
453
454 /*************************** Environment ******************************/
455
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*
461 */
462 struct win_env
463 {
464 const char *name;
465 size_t namelen;
466 char *posix;
467 char *native;
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;}
474 };
475
476 win_env * __stdcall getwinenv (const char *name, const char *posix = NULL);
477
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 ())
483
484 /* The title on program start. */
485 extern char *old_title;
486 extern BOOL display_title;
487
488
489 /*************************** errno manipulation ******************************/
490
491 void seterrno_from_win_error (const char *file, int line, int code);
492 void seterrno (const char *, int line);
493
494 #define __seterrno() seterrno (__FILE__, __LINE__)
495 #define __seterrno_from_win_error(val) seterrno_from_win_error (__FILE__, __LINE__, val)
496 #undef errno
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);
501
502 class save_errno
503 {
504 int saved;
505 public:
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);}
511 };
512
513 extern const char *__sp_fn;
514 extern int __sp_ln;
This page took 0.065697 seconds and 6 git commands to generate.