OSDN Git Service

* fhandler.cc (off_current): Define local in fhandler_base::raw_write.
[pf3gnuchains/pf3gnuchains4x.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, 2008, 2009, 2010, 2011 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 NO_COPY __attribute__((nocommon)) __attribute__((section(".data_cygwin_nocopy")))
25 #define NO_COPY_INIT __attribute__((section(".data_cygwin_nocopy")))
26 #define _RDATA __attribute__ ((section(".rdata")))
27
28 #define EXPORT_ALIAS(sym,symalias) extern "C" __typeof (sym) symalias __attribute__ ((alias(#sym)));
29
30 #define WINVER 0x0601
31 #define _NO_W32_PSEUDO_MODIFIERS
32
33 #include <sys/types.h>
34 #include <sys/strace.h>
35
36 /* Declarations for functions used in C and C++ code. */
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 __uid32_t getuid32 ();
41 __uid32_t geteuid32 ();
42 int seteuid32 (__uid32_t);
43 __gid32_t getegid32 (void);
44 struct passwd *getpwuid32 (__uid32_t);
45 struct passwd *getpwnam (const char *);
46 struct __sFILE64 *fopen64 (const char *, const char *);
47 struct hostent *cygwin_gethostbyname (const char *name);
48 /* Don't enforce definition of in_addr_t. */
49 uint32_t cygwin_inet_addr (const char *cp);
50 int fcntl64 (int fd, int cmd, ...);
51 #ifdef __cplusplus
52 }
53 #endif
54
55 /* Note that MAX_PATH is defined in the windows headers */
56 /* There is also PATH_MAX and MAXPATHLEN.
57    PATH_MAX is from Posix and does include the trailing NUL.
58    MAXPATHLEN is from Unix.
59
60    Thou shalt *not* use CYG_MAX_PATH anymore.  Use NT_MAX_PATH or
61    dynamic allocation instead when accessing real files.  Use
62    MAX_PATH in case you need a convenient small buffer when creating
63    names for synchronization objects or named pipes. */
64 #define CYG_MAX_PATH (MAX_PATH)
65
66 /* There's no define for the maximum path length the NT kernel can handle.
67    That's why we define our own to use in path length test and for path
68    buffer sizes.  As MAX_PATH and PATH_MAX, this is defined including the
69    trailing 0.  Internal buffers and internal path routines should use
70    NT_MAX_PATH.  PATH_MAX as defined in limits.h is the maximum length of
71    application provided path strings we handle. */
72 #define NT_MAX_PATH 32768
73
74 /* This definition allows to define wide char strings using macros as
75    parameters.  See the definition of __CONCAT in newlib's sys/cdefs.h
76    and accompanying comment. */
77 #define __WIDE(a) L ## a
78 #define _WIDE(a) __WIDE(a)
79
80 #ifdef __cplusplus
81
82 extern const char case_folded_lower[];
83 #define cyg_tolower(c) (case_folded_lower[(unsigned char)(c)])
84 extern const char case_folded_upper[];
85 #define cyg_toupper(c) (case_folded_upper[(unsigned char)(c)])
86
87 #ifndef MALLOC_DEBUG
88 #define cfree newlib_cfree_dont_use
89 #endif
90
91 #include "winlean.h"
92 #include "wincap.h"
93
94 /* The one function we use from winuser.h most of the time */
95 extern "C" DWORD WINAPI GetLastError (void);
96
97 /* Used as type by sys_wcstombs_alloc and sys_mbstowcs_alloc.  For a
98    description see there. */
99 #define HEAP_NOTHEAP -1
100
101 /* Used to check if Cygwin DLL is dynamically loaded. */
102
103 extern int cygserver_running;
104
105 #define _MT_SAFE        // DELETEME someday
106
107 #define TITLESIZE 1024
108
109 #include "debug.h"
110
111 #include <wchar.h>
112
113 /**************************** Convenience ******************************/
114
115 /* Used to define status flag accessor methods */
116 #define IMPLEMENT_STATUS_FLAG(type,flag) \
117   type flag (type val) { return (type) (status.flag = (val)); } \
118   type flag () const { return (type) status.flag; }
119
120 /* Used when treating / and \ as equivalent. */
121 #define iswdirsep(ch) \
122     ({ \
123         WCHAR __c = (ch); \
124         ((__c) == L'/' || (__c) == L'\\'); \
125     })
126
127 #define isdirsep(ch) \
128     ({ \
129         char __c = (ch); \
130         ((__c) == '/' || (__c) == '\\'); \
131     })
132
133 /* Convert a signal to a signal mask */
134 #define SIGTOMASK(sig)  (1 << ((sig) - 1))
135
136 #define set_api_fatal_return(n) do {extern int __api_fatal_exit_val; __api_fatal_exit_val = (n);} while (0)
137
138 #undef issep
139 #define issep(ch) (strchr (" \t\n\r", (ch)) != NULL)
140
141 /* Every path beginning with / or \, as well as every path being X:
142    or starting with X:/ or X:\ */
143 #define isabspath_u(p) \
144   ((p)->Length && \
145    (iswdirsep ((p)->Buffer[0]) || \
146     ((p)->Length > sizeof (WCHAR) && iswalpha ((p)->Buffer[0]) \
147     && (p)->Buffer[1] == L':' && \
148     ((p)->Length == 2 * sizeof (WCHAR) || iswdirsep ((p)->Buffer[2])))))
149
150 #define iswabspath(p) \
151   (iswdirsep (*(p)) || (iswalpha (*(p)) && (p)[1] == L':' && (!(p)[2] || iswdirsep ((p)[2]))))
152
153 #define isabspath(p) \
154   (isdirsep (*(p)) || (isalpha (*(p)) && (p)[1] == ':' && (!(p)[2] || isdirsep ((p)[2]))))
155
156 /******************** Initialization/Termination **********************/
157
158 class per_process;
159 /* cygwin .dll initialization */
160 void dll_crt0 (per_process *) __asm__ ("_dll_crt0__FP11per_process");
161 extern "C" void __stdcall _dll_crt0 ();
162 void dll_crt0_1 (void *);
163 void dll_dllcrt0_1 (void *);
164 int spawn_guts (const char * prog_arg, const char *const *argv,
165                 const char *const envp[], int mode, int __stdin = -1,
166                 int __stdout = -1) __attribute__ ((regparm(3)));
167
168 /* dynamically loaded dll initialization */
169 extern "C" int dll_dllcrt0 (HMODULE, per_process *);
170
171 void _pei386_runtime_relocator (per_process *);
172
173 /* dynamically loaded dll initialization for non-cygwin apps */
174 extern "C" int dll_noncygwin_dllcrt0 (HMODULE, per_process *);
175 void __stdcall do_exit (int) __attribute__ ((regparm (1), noreturn));
176
177 /* libstdc++ malloc operator wrapper support.  */
178 extern struct per_process_cxx_malloc default_cygwin_cxx_malloc;
179
180 /* UID/GID */
181 void uinfo_init ();
182
183 #define ILLEGAL_UID16 ((__uid16_t)-1)
184 #define ILLEGAL_UID ((__uid32_t)-1)
185 #define ILLEGAL_GID16 ((__gid16_t)-1)
186 #define ILLEGAL_GID ((__gid32_t)-1)
187 #define ILLEGAL_SEEK ((_off64_t)-1)
188
189 #define uid16touid32(u16)  ((u16)==ILLEGAL_UID16?ILLEGAL_UID:(__uid32_t)(u16))
190 #define gid16togid32(g16)  ((g16)==ILLEGAL_GID16?ILLEGAL_GID:(__gid32_t)(g16))
191
192 /* Convert LARGE_INTEGER into long long */
193 #define get_ll(pl)  (((long long) (pl).HighPart << 32) | (pl).LowPart)
194
195 /* various events */
196 void events_init ();
197 void events_terminate ();
198
199 void __stdcall close_all_files (bool = false);
200
201 /* debug_on_trap support. see exceptions.cc:try_to_debug() */
202 extern "C" void error_start_init (const char*);
203 extern "C" int try_to_debug (bool waitloop = 1);
204
205 void ld_preload ();
206 const char *find_first_notloaded_dll (class path_conv &);
207
208 extern bool cygwin_finished_initializing;
209
210 /**************************** Miscellaneous ******************************/
211
212 void __stdcall set_std_handle (int);
213 int __stdcall stat_dev (DWORD, int, unsigned long, struct __stat64 *);
214
215 __ino64_t __stdcall hash_path_name (__ino64_t hash, PUNICODE_STRING name) __attribute__ ((regparm(2)));
216 __ino64_t __stdcall hash_path_name (__ino64_t hash, PCWSTR name) __attribute__ ((regparm(2)));
217 __ino64_t __stdcall hash_path_name (__ino64_t hash, const char *name) __attribute__ ((regparm(2)));
218 void __stdcall nofinalslash (const char *src, char *dst) __attribute__ ((regparm(2)));
219
220 void *hook_or_detect_cygwin (const char *, const void *, WORD&, HANDLE h = NULL) __attribute__ ((regparm (3)));
221
222 /* Time related */
223 void __stdcall totimeval (struct timeval *, FILETIME *, int, int);
224 long __stdcall to_time_t (FILETIME *);
225 void __stdcall to_timestruc_t (FILETIME *, timestruc_t *);
226 void __stdcall time_as_timestruc_t (timestruc_t *);
227 void __stdcall timespec_to_filetime (const struct timespec *, FILETIME *);
228 void __stdcall timeval_to_filetime (const struct timeval *, FILETIME *);
229
230 /* Console related */
231 void __stdcall set_console_title (char *);
232 void init_console_handler (bool);
233
234 void init_global_security ();
235
236 void __set_winsock_errno (const char *fn, int ln) __attribute__ ((regparm(2)));
237 #define set_winsock_errno() __set_winsock_errno (__FUNCTION__, __LINE__)
238
239 extern bool wsock_started;
240
241 /* Printf type functions */
242 extern "C" void vapi_fatal (const char *, va_list ap) __attribute__ ((noreturn));
243 extern "C" void api_fatal (const char *, ...) __attribute__ ((noreturn));
244 int __small_sprintf (char *dst, const char *fmt, ...) /*__attribute__ ((regparm (2)))*/;
245 int __small_vsprintf (char *dst, const char *fmt, va_list ap) /*__attribute__ ((regparm (3)))*/;
246 int __small_swprintf (PWCHAR dst, const WCHAR *fmt, ...) /*__attribute__ ((regparm (2)))*/;
247 int __small_vswprintf (PWCHAR dst, const WCHAR *fmt, va_list ap) /*__attribute__ ((regparm (3)))*/;
248 void multiple_cygwin_problem (const char *, unsigned, unsigned);
249
250 extern "C" void vklog (int priority, const char *message, va_list ap);
251 extern "C" void klog (int priority, const char *message, ...);
252 bool child_copy (HANDLE, bool, ...);
253
254 int symlink_worker (const char *, const char *, bool, bool)
255   __attribute__ ((regparm (3)));
256
257 class path_conv;
258
259 int __stdcall stat_worker (path_conv &pc, struct __stat64 *buf) __attribute__ ((regparm (2)));
260
261 __ino64_t __stdcall readdir_get_ino (const char *path, bool dot_dot) __attribute__ ((regparm (2)));
262
263 /* Returns the real page size, not the allocation size. */
264 size_t getsystempagesize ();
265
266 /* mmap functions. */
267 enum mmap_region_status
268   {
269     MMAP_NONE,
270     MMAP_RAISE_SIGBUS,
271     MMAP_NORESERVE_COMMITED
272   };
273 mmap_region_status mmap_is_attached_or_noreserve (void *addr, size_t len);
274 bool is_mmapped_region (caddr_t start_addr, caddr_t end_address);
275
276 inline bool flush_file_buffers (HANDLE h)
277 {
278   return (GetFileType (h) != FILE_TYPE_PIPE) ? FlushFileBuffers (h) : true;
279 }
280 #define FlushFileBuffers flush_file_buffers
281
282 /**************************** Exports ******************************/
283
284 extern "C" {
285 int cygwin_select (int , fd_set *, fd_set *, fd_set *,
286                    struct timeval *to);
287 int cygwin_gethostname (char *__name, size_t __len);
288 };
289
290 /*************************** Unsorted ******************************/
291
292 #define WM_ASYNCIO      0x8000          // WM_APP
293
294
295 #define STD_RBITS (S_IRUSR | S_IRGRP | S_IROTH)
296 #define STD_WBITS (S_IWUSR)
297 #define STD_XBITS (S_IXUSR | S_IXGRP | S_IXOTH)
298 #define NO_W ~(S_IWUSR | S_IWGRP | S_IWOTH)
299 #define NO_R ~(S_IRUSR | S_IRGRP | S_IROTH)
300 #define NO_X ~(S_IXUSR | S_IXGRP | S_IXOTH)
301
302
303 extern "C" char _data_start__, _data_end__, _bss_start__, _bss_end__;
304 extern "C" void (*__CTOR_LIST__) (void);
305 extern "C" void (*__DTOR_LIST__) (void);
306
307 #if !defined(_GLOBALS_H)
308 #include "globals.h"
309 inline void clear_procimptoken ()
310 {
311   if (hProcImpToken)
312     {
313       HANDLE old_procimp = hProcImpToken;
314       hProcImpToken = NULL;
315       CloseHandle (old_procimp);
316     }
317 }
318 #endif
319
320 #endif /* defined __cplusplus */