OSDN Git Service

Perform whitespace cleanup throughout.
[pf3gnuchains/pf3gnuchains3x.git] / winsup / cygwin / winsup.h
1 /* winsup.h: main Cygwin header file.
2
3    Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4    2005, 2006, 2007 Red Hat, Inc.
5
6 This file is part of Cygwin.
7
8 This software is a copyrighted work licensed under the terms of the
9 Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
10 details. */
11
12 #ifdef DEBUGIT
13 #define spf(a, b, c) small_printf (a, b, c)
14 #else
15 #define spf(a, b, c) do {} while (0)
16 #endif
17
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
21
22 #define __INSIDE_CYGWIN__
23
24 #define strlen __builtin_strlen
25 #define strcmp __builtin_strcmp
26 #define strcpy __builtin_strcpy
27 #define memcpy __builtin_memcpy
28 #define memcmp __builtin_memcmp
29 #ifdef HAVE_BUILTIN_MEMSET
30 # define memset __builtin_memset
31 #endif
32
33 #define NO_COPY __attribute__((nocommon)) __attribute__((section(".data_cygwin_nocopy")))
34 #define NO_COPY_INIT __attribute__((section(".data_cygwin_nocopy")))
35
36 #define EXPORT_ALIAS(sym,symalias) extern "C" __typeof (sym) symalias __attribute__ ((alias(#sym)));
37
38 #define WINVER 0x0600
39 #define _NO_W32_PSEUDO_MODIFIERS
40
41 #include <sys/types.h>
42 #include <sys/strace.h>
43
44 /* Declarations for functions used in C and C++ code. */
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48 extern __uid32_t getuid32 (void);
49 extern __uid32_t geteuid32 (void);
50 extern int seteuid32 (__uid32_t);
51 extern __gid32_t getegid32 (void);
52 extern struct passwd *getpwuid32 (__uid32_t);
53 extern struct passwd *getpwnam (const char *);
54 extern struct __sFILE64 *fopen64 (const char *, const char *);
55 extern struct hostent *cygwin_gethostbyname (const char *name);
56 extern unsigned long cygwin_inet_addr (const char *cp);
57
58 #ifdef __cplusplus
59 }
60 #endif
61
62 /* Note that MAX_PATH is defined in the windows headers */
63 /* There is also PATH_MAX and MAXPATHLEN.
64    PATH_MAX is from Posix and does include the trailing NUL.
65    MAXPATHLEN is from Unix.
66
67    Thou shalt use CYG_MAX_PATH throughout.  It avoids the NUL vs no-NUL
68    issue and is neither of the Unixy ones [so we can punt on which
69    one is the right one to use].
70
71    Windows ANSI calls are limited to MAX_PATH in length. Cygwin calls that
72    thunk through to Windows Wide calls are limited to 32K. We define
73    CYG_MAX_PATH as a convenient, not to short, not too long 'happy medium'.
74
75    */
76
77 #define CYG_MAX_PATH (MAX_PATH)
78
79 /* There's no define for the maximum path length the NT kernel can handle.
80    That's why we define our own to use in path length test and for path
81    buffer sizes.  As MAX_PATH and PATH_MAX, this is defined including the
82    trailing 0.  Internal buffers and internal path routines should use
83    NT_MAX_PATH.  PATH_MAX as defined in limits.h is the maximum length of
84    application provided path strings we handle.  */
85 /* FIXME: The name is preliminary and TBD. */
86 #define NT_MAX_PATH 32768
87
88 #ifdef __cplusplus
89
90 extern const char case_folded_lower[];
91 #define cyg_tolower(c) (case_folded_lower[(unsigned char)(c)])
92 extern const char case_folded_upper[];
93 #define cyg_toupper(c) (case_folded_upper[(unsigned char)(c)])
94
95 #ifndef MALLOC_DEBUG
96 #define cfree newlib_cfree_dont_use
97 #endif
98
99 #define WIN32_LEAN_AND_MEAN 1
100 #define _WINGDI_H
101 #define _WINUSER_H
102 #define _WINNLS_H
103 #define _WINVER_H
104 #define _WINNETWK_H
105 #define _WINSVC_H
106 #include <windows.h>
107 #include <wincrypt.h>
108 #include <lmcons.h>
109 #include <ntdef.h>
110 #undef _WINGDI_H
111 #undef _WINUSER_H
112 #undef _WINNLS_H
113 #undef _WINVER_H
114 #undef _WINNETWK_H
115 #undef _WINSVC_H
116
117 #include "wincap.h"
118
119 /* The one function we use from winuser.h most of the time */
120 extern "C" DWORD WINAPI GetLastError (void);
121
122 /* Codepage and multibyte string specific stuff. */
123 enum codepage_type {ansi_cp, oem_cp, utf8_cp};
124 extern codepage_type current_codepage;
125 extern UINT active_codepage;
126
127 void codepage_init (const char *buf);
128 UINT get_cp ();
129 bool is_cp_multibyte (UINT cp);
130 const unsigned char *next_char (UINT cp, const unsigned char *str,
131                                 const unsigned char *end);
132
133 /* Used as type by sys_wcstombs_alloc and sys_mbstowcs_alloc.  For a
134    description see there. */
135 #define HEAP_NOTHEAP -1
136
137 int __stdcall sys_wcstombs (char *, int, const PWCHAR, int = -1)
138   __attribute__ ((regparm(3)));
139 int __stdcall sys_wcstombs_alloc (char **, int, const PWCHAR, int = -1)
140   __attribute__ ((regparm(3)));
141
142 int __stdcall sys_mbstowcs (PWCHAR, const char *, int)
143   __attribute__ ((regparm(3)));
144 int __stdcall sys_mbstowcs_alloc (PWCHAR *, int, const char *)
145   __attribute__ ((regparm(3)));
146
147 /* Used to check if Cygwin DLL is dynamically loaded. */
148 extern int dynamically_loaded;
149
150 extern int cygserver_running;
151
152 #define _MT_SAFE        // DELTEME someday
153
154 #define TITLESIZE 1024
155
156 #include "debug.h"
157
158 /* Events/mutexes */
159 extern HANDLE tty_mutex;
160
161 /**************************** Convenience ******************************/
162
163 /* Used to define status flag accessor methods */
164 #define IMPLEMENT_STATUS_FLAG(type,flag) \
165   void flag (type val) { status.flag = (val); } \
166   type flag () const { return (type) status.flag; }
167
168 /* Used when treating / and \ as equivalent. */
169 #define iswdirsep(ch) \
170     ({ \
171         WCHAR __c = (ch); \
172         ((__c) == L'/' || (__c) == L'\\'); \
173     })
174
175 #define isdirsep(ch) \
176     ({ \
177         char __c = (ch); \
178         ((__c) == '/' || (__c) == '\\'); \
179     })
180
181 /* Convert a signal to a signal mask */
182 #define SIGTOMASK(sig)  (1 << ((sig) - 1))
183
184 extern int __api_fatal_exit_val;
185 #define set_api_fatal_return(n) do {extern int __api_fatal_exit_val; __api_fatal_exit_val = (n);} while (0)
186 #define api_fatal(fmt, args...) __api_fatal (fmt,## args)
187
188 #undef issep
189 #define issep(ch) (strchr (" \t\n\r", (ch)) != NULL)
190
191 /* Every path beginning with / or \, as well as every path being X:
192    or starting with X:/ or X:\ */
193 #define isabspath_u(p) \
194   ((p)->Length && \
195    (iswdirsep ((p)->Buffer[0]) || \
196     ((p)->Length > sizeof (WCHAR) && iswalpha ((p)->Buffer[0]) \
197     && (p)->Buffer[1] == L':' && \
198     ((p)->Length == 2 * sizeof (WCHAR) || iswdirsep ((p)->Buffer[2])))))
199
200 #define iswabspath(p) \
201   (iswdirsep (*(p)) || (iswalpha (*(p)) && (p)[1] == L':' && (!(p)[2] || iswdirsep ((p)[2]))))
202
203 #define isabspath(p) \
204   (isdirsep (*(p)) || (isalpha (*(p)) && (p)[1] == ':' && (!(p)[2] || isdirsep ((p)[2]))))
205
206 /******************** Initialization/Termination **********************/
207
208 class per_process;
209 /* cygwin .dll initialization */
210 void dll_crt0 (per_process *) __asm__ ("_dll_crt0__FP11per_process");
211 extern "C" void __stdcall _dll_crt0 ();
212 extern void dll_crt0_1 (void *);
213 extern void dll_dllcrt0_1 (void *);
214 extern int __stdcall spawn_guts (const char * prog_arg, const char *const *argv,
215                                  const char *const envp[], int mode,
216                                  int __stdin = -1, int __stdout = -1);
217
218 /* dynamically loaded dll initialization */
219 extern "C" int dll_dllcrt0 (HMODULE, per_process *);
220
221 /* dynamically loaded dll initialization for non-cygwin apps */
222 extern "C" int dll_noncygwin_dllcrt0 (HMODULE, per_process *);
223
224 /* exit the program */
225
226 enum exit_states
227   {
228     ES_NOT_EXITING = 0,
229     ES_PROCESS_LOCKED,
230     ES_GLOBAL_DTORS,
231     ES_EVENTS_TERMINATE,
232     ES_THREADTERM,
233     ES_SIGNAL,
234     ES_CLOSEALL,
235     ES_HUP_PGRP,
236     ES_HUP_SID,
237     ES_EXEC_EXIT,
238     ES_TITLE,
239     ES_TTY_TERMINATE,
240     ES_FINAL
241   };
242
243 extern exit_states exit_state;
244 void __stdcall do_exit (int) __attribute__ ((regparm (1), noreturn));
245
246 /* UID/GID */
247 void uinfo_init ();
248
249 #define ILLEGAL_UID16 ((__uid16_t)-1)
250 #define ILLEGAL_UID ((__uid32_t)-1)
251 #define ILLEGAL_GID16 ((__gid16_t)-1)
252 #define ILLEGAL_GID ((__gid32_t)-1)
253 #define ILLEGAL_SEEK ((_off64_t)-1)
254
255 #define uid16touid32(u16)  ((u16)==ILLEGAL_UID16?ILLEGAL_UID:(__uid32_t)(u16))
256 #define gid16togid32(g16)  ((g16)==ILLEGAL_GID16?ILLEGAL_GID:(__gid32_t)(g16))
257
258 /* Convert LARGE_INTEGER into long long */
259 #define get_ll(pl)  (((long long) (pl).HighPart << 32) | (pl).LowPart)
260
261 /* various events */
262 void events_init ();
263 void events_terminate ();
264
265 void __stdcall close_all_files (bool = false);
266
267 /* debug_on_trap support. see exceptions.cc:try_to_debug() */
268 extern "C" void error_start_init (const char*);
269 extern "C" int try_to_debug (bool waitloop = 1);
270
271 extern void ld_preload ();
272
273 void set_file_api_mode (codepage_type);
274
275 extern bool cygwin_finished_initializing;
276
277 /**************************** Miscellaneous ******************************/
278
279 void __stdcall set_std_handle (int);
280 int __stdcall stat_dev (DWORD, int, unsigned long, struct __stat64 *);
281
282 __ino64_t __stdcall hash_path_name (__ino64_t hash, PUNICODE_STRING name) __attribute__ ((regparm(2)));
283 __ino64_t __stdcall hash_path_name (__ino64_t hash, PCWSTR name) __attribute__ ((regparm(2)));
284 __ino64_t __stdcall hash_path_name (__ino64_t hash, const char *name) __attribute__ ((regparm(2)));
285 void __stdcall nofinalslash (const char *src, char *dst) __attribute__ ((regparm(2)));
286
287 /* String manipulation */
288 extern "C" char *__stdcall strccpy (char *s1, const char **s2, char c);
289
290 void *hook_or_detect_cygwin (const char *, const void *, WORD&) __attribute__ ((regparm (3)));
291
292 /* Time related */
293 void __stdcall totimeval (struct timeval *, FILETIME *, int, int);
294 long __stdcall to_time_t (FILETIME *);
295 void __stdcall to_timestruc_t (FILETIME *, timestruc_t *);
296 void __stdcall time_as_timestruc_t (timestruc_t *);
297 void __stdcall timeval_to_filetime (const struct timeval *, FILETIME *);
298
299 /* Console related */
300 void __stdcall set_console_title (char *);
301 void init_console_handler (bool);
302
303 void init_global_security ();
304
305 int __stdcall check_invalid_virtual_addr (const void *s, unsigned sz) __attribute__ ((regparm(2)));
306
307 ssize_t check_iovec (const struct iovec *, int, bool) __attribute__ ((regparm(3)));
308 #define check_iovec_for_read(a, b) check_iovec ((a), (b), false)
309 #define check_iovec_for_write(a, b) check_iovec ((a), (b), true)
310
311 #define set_winsock_errno() __set_winsock_errno (__FUNCTION__, __LINE__)
312 void __set_winsock_errno (const char *fn, int ln) __attribute__ ((regparm(2)));
313
314 extern bool wsock_started;
315
316 /* Printf type functions */
317 extern "C" void __api_fatal (const char *, ...) __attribute__ ((noreturn));
318 extern "C" int __small_sprintf (char *dst, const char *fmt, ...) /*__attribute__ ((regparm (2)))*/;
319 extern "C" int __small_vsprintf (char *dst, const char *fmt, va_list ap) /*__attribute__ ((regparm (3)))*/;
320 extern void multiple_cygwin_problem (const char *, unsigned, unsigned);
321
322 extern "C" void vklog (int priority, const char *message, va_list ap);
323 extern "C" void klog (int priority, const char *message, ...);
324 bool child_copy (HANDLE, bool, ...);
325
326 int symlink_worker (const char *, const char *, bool, bool)
327   __attribute__ ((regparm (3)));
328
329 class path_conv;
330
331 int fcntl_worker (int fd, int cmd, void *arg);
332 int __stdcall stat_worker (path_conv &pc, struct __stat64 *buf) __attribute__ ((regparm (2)));
333
334 __ino64_t __stdcall readdir_get_ino (const char *path, bool dot_dot) __attribute__ ((regparm (2)));
335
336 extern "C" int low_priority_sleep (DWORD) __attribute__ ((regparm (1)));
337 #define SLEEP_0_STAY_LOW INFINITE
338
339 /* Returns the real page size, not the allocation size. */
340 size_t getsystempagesize ();
341
342 /* mmap functions. */
343 enum mmap_region_status
344   {
345     MMAP_NONE,
346     MMAP_RAISE_SIGBUS,
347     MMAP_NORESERVE_COMMITED
348   };
349 mmap_region_status mmap_is_attached_or_noreserve (void *addr, size_t len);
350
351 int winprio_to_nice (DWORD) __attribute__ ((regparm (1)));
352 DWORD nice_to_winprio (int &) __attribute__ ((regparm (1)));
353
354 bool __stdcall create_pipe (PHANDLE, PHANDLE, LPSECURITY_ATTRIBUTES, DWORD)
355   __attribute__ ((regparm (3)));
356 #define CreatePipe create_pipe
357
358 inline bool flush_file_buffers (HANDLE h)
359 {
360   return (GetFileType (h) != FILE_TYPE_PIPE) ? FlushFileBuffers (h) : true;
361 }
362 #define FlushFileBuffers flush_file_buffers
363
364 /**************************** Exports ******************************/
365
366 extern "C" {
367 int cygwin_select (int , fd_set *, fd_set *, fd_set *,
368                    struct timeval *to);
369 int cygwin_gethostname (char *__name, size_t __len);
370
371 extern DWORD binmode;
372 extern char _data_start__, _data_end__, _bss_start__, _bss_end__;
373 extern void (*__CTOR_LIST__) (void);
374 extern void (*__DTOR_LIST__) (void);
375 extern SYSTEM_INFO system_info;
376 };
377
378 /*************************** Unsorted ******************************/
379
380 #define WM_ASYNCIO      0x8000          // WM_APP
381
382
383 #define STD_RBITS (S_IRUSR | S_IRGRP | S_IROTH)
384 #define STD_WBITS (S_IWUSR)
385 #define STD_XBITS (S_IXUSR | S_IXGRP | S_IXOTH)
386 #define NO_W ~(S_IWUSR | S_IWGRP | S_IWOTH)
387 #define NO_R ~(S_IRUSR | S_IRGRP | S_IROTH)
388 #define NO_X ~(S_IXUSR | S_IXGRP | S_IXOTH)
389
390 /* The title on program start. */
391 extern char *old_title;
392 extern bool display_title;
393 extern bool transparent_exe;
394
395 extern bool in_forkee;
396
397 extern HANDLE hMainThread;
398 extern HANDLE hMainProc;
399 extern HANDLE hProcToken;
400 extern HANDLE hProcImpToken;
401
402 inline void clear_procimptoken ()
403 {
404   if (hProcImpToken)
405     {
406       HANDLE old_procimp = hProcImpToken;
407       hProcImpToken = NULL;
408       CloseHandle (old_procimp);
409     }
410 }
411
412 extern HANDLE hExeced;
413 extern HMODULE cygwin_hmodule;
414
415 extern bool cygwin_testing;
416
417 extern char almost_null[];
418
419 #endif /* defined __cplusplus */