PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
win32.h
Go to the documentation of this file.
1 /* src/include/port/win32.h */
2 
3 #if defined(_MSC_VER) || defined(__BORLANDC__)
4 #define WIN32_ONLY_COMPILER
5 #endif
6 
7 /*
8  * Make sure _WIN32_WINNT has the minimum required value.
9  * Leave a higher value in place. When building with at least Visual
10  * Studio 2015 the minimum requirement is Windows Vista (0x0600) to
11  * get support for GetLocaleInfoEx() with locales. For everything else
12  * the minimum version is Windows XP (0x0501).
13  * Also for VS2015, add a define that stops compiler complaints about
14  * using the old Winsock API.
15  */
16 #if defined(_MSC_VER) && _MSC_VER >= 1900
17 #define _WINSOCK_DEPRECATED_NO_WARNINGS
18 #define MIN_WINNT 0x0600
19 #else
20 #define MIN_WINNT 0x0501
21 #endif
22 
23 #if defined(_WIN32_WINNT) && _WIN32_WINNT < MIN_WINNT
24 #undef _WIN32_WINNT
25 #endif
26 
27 #ifndef _WIN32_WINNT
28 #define _WIN32_WINNT MIN_WINNT
29 #endif
30 
31 /*
32  * Always build with SSPI support. Keep it as a #define in case
33  * we want a switch to disable it sometime in the future.
34  */
35 #ifndef __BORLANDC__
36 #define ENABLE_SSPI 1
37 #endif
38 
39 /* undefine and redefine after #include */
40 #undef mkdir
41 
42 #undef ERROR
43 
44 /*
45  * The Mingw64 headers choke if this is already defined - they
46  * define it themselves.
47  */
48 #if !defined(__MINGW64_VERSION_MAJOR) || defined(WIN32_ONLY_COMPILER)
49 #define _WINSOCKAPI_
50 #endif
51 #include <winsock2.h>
52 #include <ws2tcpip.h>
53 #include <windows.h>
54 #undef small
55 #include <process.h>
56 #include <signal.h>
57 #include <errno.h>
58 #include <direct.h>
59 #ifndef __BORLANDC__
60 #include <sys/utime.h> /* for non-unicode version */
61 #endif
62 #undef near
63 
64 /* Must be here to avoid conflicting with prototype in windows.h */
65 #define mkdir(a,b) mkdir(a)
66 
67 #define ftruncate(a,b) chsize(a,b)
68 
69 /* Windows doesn't have fsync() as such, use _commit() */
70 #define fsync(fd) _commit(fd)
71 
72 /*
73  * For historical reasons, we allow setting wal_sync_method to
74  * fsync_writethrough on Windows, even though it's really identical to fsync
75  * (both code paths wind up at _commit()).
76  */
77 #define HAVE_FSYNC_WRITETHROUGH
78 #define FSYNC_WRITETHROUGH_IS_FSYNC
79 
80 #define USES_WINSOCK
81 
82 /* defines for dynamic linking on Win32 platform
83  *
84  * http://support.microsoft.com/kb/132044
85  * http://msdn.microsoft.com/en-us/library/8fskxacy(v=vs.80).aspx
86  * http://msdn.microsoft.com/en-us/library/a90k134d(v=vs.80).aspx
87  */
88 
89 #if defined(WIN32) || defined(__CYGWIN__)
90 
91 #ifdef BUILDING_DLL
92 #define PGDLLIMPORT __declspec (dllexport)
93 #else /* not BUILDING_DLL */
94 #define PGDLLIMPORT __declspec (dllimport)
95 #endif
96 
97 #ifdef _MSC_VER
98 #define PGDLLEXPORT __declspec (dllexport)
99 #else
100 #define PGDLLEXPORT
101 #endif
102 #else /* not CYGWIN, not MSVC, not MingW */
103 #define PGDLLIMPORT
104 #define PGDLLEXPORT
105 #endif
106 
107 
108 /*
109  * IPC defines
110  */
111 #undef HAVE_UNION_SEMUN
112 #define HAVE_UNION_SEMUN 1
113 
114 #define IPC_RMID 256
115 #define IPC_CREAT 512
116 #define IPC_EXCL 1024
117 #define IPC_PRIVATE 234564
118 #define IPC_NOWAIT 2048
119 #define IPC_STAT 4096
120 
121 #define EACCESS 2048
122 #ifndef EIDRM
123 #define EIDRM 4096
124 #endif
125 
126 #define SETALL 8192
127 #define GETNCNT 16384
128 #define GETVAL 65536
129 #define SETVAL 131072
130 #define GETPID 262144
131 
132 
133 /*
134  * Signal stuff
135  *
136  * For WIN32, there is no wait() call so there are no wait() macros
137  * to interpret the return value of system(). Instead, system()
138  * return values < 0x100 are used for exit() termination, and higher
139  * values are used to indicated non-exit() termination, which is
140  * similar to a unix-style signal exit (think SIGSEGV ==
141  * STATUS_ACCESS_VIOLATION). Return values are broken up into groups:
142  *
143  * http://msdn2.microsoft.com/en-gb/library/aa489609.aspx
144  *
145  * NT_SUCCESS 0 - 0x3FFFFFFF
146  * NT_INFORMATION 0x40000000 - 0x7FFFFFFF
147  * NT_WARNING 0x80000000 - 0xBFFFFFFF
148  * NT_ERROR 0xC0000000 - 0xFFFFFFFF
149  *
150  * Effectively, we don't care on the severity of the return value from
151  * system(), we just need to know if it was because of exit() or generated
152  * by the system, and it seems values >= 0x100 are system-generated.
153  * See this URL for a list of WIN32 STATUS_* values:
154  *
155  * Wine (URL used in our error messages) -
156  * http://source.winehq.org/source/include/ntstatus.h
157  * Descriptions - http://www.comp.nus.edu.sg/~wuyongzh/my_doc/ntstatus.txt
158  * MS SDK - http://www.nologs.com/ntstatus.html
159  *
160  * It seems the exception lists are in both ntstatus.h and winnt.h, but
161  * ntstatus.h has a more comprehensive list, and it only contains
162  * exception values, rather than winnt, which contains lots of other
163  * things:
164  *
165  * http://www.microsoft.com/msj/0197/exception/exception.aspx
166  *
167  * The ExceptionCode parameter is the number that the operating system
168  * assigned to the exception. You can see a list of various exception codes
169  * in WINNT.H by searching for #defines that start with "STATUS_". For
170  * example, the code for the all-too-familiar STATUS_ACCESS_VIOLATION is
171  * 0xC0000005. A more complete set of exception codes can be found in
172  * NTSTATUS.H from the Windows NT DDK.
173  *
174  * Some day we might want to print descriptions for the most common
175  * exceptions, rather than printing an include file name. We could use
176  * RtlNtStatusToDosError() and pass to FormatMessage(), which can print
177  * the text of error values, but MinGW does not support
178  * RtlNtStatusToDosError().
179  */
180 #define WIFEXITED(w) (((w) & 0XFFFFFF00) == 0)
181 #define WIFSIGNALED(w) (!WIFEXITED(w))
182 #define WEXITSTATUS(w) (w)
183 #define WTERMSIG(w) (w)
184 
185 #define sigmask(sig) ( 1 << ((sig)-1) )
186 
187 /* Signal function return values */
188 #undef SIG_DFL
189 #undef SIG_ERR
190 #undef SIG_IGN
191 #define SIG_DFL ((pqsigfunc)0)
192 #define SIG_ERR ((pqsigfunc)-1)
193 #define SIG_IGN ((pqsigfunc)1)
194 
195 /* Some extra signals */
196 #define SIGHUP 1
197 #define SIGQUIT 3
198 #define SIGTRAP 5
199 #define SIGABRT 22 /* Set to match W32 value -- not UNIX value */
200 #define SIGKILL 9
201 #define SIGPIPE 13
202 #define SIGALRM 14
203 #define SIGSTOP 17
204 #define SIGTSTP 18
205 #define SIGCONT 19
206 #define SIGCHLD 20
207 #define SIGTTIN 21
208 #define SIGTTOU 22 /* Same as SIGABRT -- no problem, I hope */
209 #define SIGWINCH 28
210 #ifndef __BORLANDC__
211 #define SIGUSR1 30
212 #define SIGUSR2 31
213 #endif
214 
215 /*
216  * New versions of mingw have gettimeofday() and also declare
217  * struct timezone to support it.
218  */
219 #ifndef HAVE_GETTIMEOFDAY
220 struct timezone
221 {
222  int tz_minuteswest; /* Minutes west of GMT. */
223  int tz_dsttime; /* Nonzero if DST is ever in effect. */
224 };
225 #endif
226 
227 /* for setitimer in backend/port/win32/timer.c */
228 #define ITIMER_REAL 0
229 struct itimerval
230 {
231  struct timeval it_interval;
232  struct timeval it_value;
233 };
234 
235 int setitimer(int which, const struct itimerval * value, struct itimerval * ovalue);
236 
237 /*
238  * WIN32 does not provide 64-bit off_t, but does provide the functions operating
239  * with 64-bit offsets.
240  */
241 #define pgoff_t __int64
242 #ifdef WIN32_ONLY_COMPILER
243 #define fseeko(stream, offset, origin) _fseeki64(stream, offset, origin)
244 #define ftello(stream) _ftelli64(stream)
245 #else
246 #ifndef fseeko
247 #define fseeko(stream, offset, origin) fseeko64(stream, offset, origin)
248 #endif
249 #ifndef ftello
250 #define ftello(stream) ftello64(stream)
251 #endif
252 #endif
253 
254 /*
255  * Supplement to <sys/types.h>.
256  *
257  * Perl already has typedefs for uid_t and gid_t.
258  */
259 #ifndef PLPERL_HAVE_UID_GID
260 typedef int uid_t;
261 typedef int gid_t;
262 #endif
263 typedef long key_t;
264 
265 #ifdef WIN32_ONLY_COMPILER
266 typedef int pid_t;
267 #endif
268 
269 /*
270  * Supplement to <sys/stat.h>.
271  */
272 #define lstat(path, sb) stat((path), (sb))
273 
274 /*
275  * Supplement to <fcntl.h>.
276  * This is the same value as _O_NOINHERIT in the MS header file. This is
277  * to ensure that we don't collide with a future definition. It means
278  * we cannot use _O_NOINHERIT ourselves.
279  */
280 #define O_DSYNC 0x0080
281 
282 /*
283  * Supplement to <errno.h>.
284  *
285  * We redefine network-related Berkeley error symbols as the corresponding WSA
286  * constants. This allows elog.c to recognize them as being in the Winsock
287  * error code range and pass them off to pgwin32_socket_strerror(), since
288  * Windows' version of plain strerror() won't cope. Note that this will break
289  * if these names are used for anything else besides Windows Sockets errors.
290  * See TranslateSocketError() when changing this list.
291  */
292 #undef EAGAIN
293 #define EAGAIN WSAEWOULDBLOCK
294 #undef EINTR
295 #define EINTR WSAEINTR
296 #undef EMSGSIZE
297 #define EMSGSIZE WSAEMSGSIZE
298 #undef EAFNOSUPPORT
299 #define EAFNOSUPPORT WSAEAFNOSUPPORT
300 #undef EWOULDBLOCK
301 #define EWOULDBLOCK WSAEWOULDBLOCK
302 #undef ECONNABORTED
303 #define ECONNABORTED WSAECONNABORTED
304 #undef ECONNRESET
305 #define ECONNRESET WSAECONNRESET
306 #undef EINPROGRESS
307 #define EINPROGRESS WSAEINPROGRESS
308 #undef EISCONN
309 #define EISCONN WSAEISCONN
310 #undef ENOBUFS
311 #define ENOBUFS WSAENOBUFS
312 #undef EPROTONOSUPPORT
313 #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
314 #undef ECONNREFUSED
315 #define ECONNREFUSED WSAECONNREFUSED
316 #undef ENOTSOCK
317 #define ENOTSOCK WSAENOTSOCK
318 #undef EOPNOTSUPP
319 #define EOPNOTSUPP WSAEOPNOTSUPP
320 #undef EADDRINUSE
321 #define EADDRINUSE WSAEADDRINUSE
322 #undef EADDRNOTAVAIL
323 #define EADDRNOTAVAIL WSAEADDRNOTAVAIL
324 #undef EHOSTUNREACH
325 #define EHOSTUNREACH WSAEHOSTUNREACH
326 #undef ENOTCONN
327 #define ENOTCONN WSAENOTCONN
328 
329 /*
330  * Extended locale functions with gratuitous underscore prefixes.
331  * (These APIs are nevertheless fully documented by Microsoft.)
332  */
333 #define locale_t _locale_t
334 #define tolower_l _tolower_l
335 #define toupper_l _toupper_l
336 #define towlower_l _towlower_l
337 #define towupper_l _towupper_l
338 #define isdigit_l _isdigit_l
339 #define iswdigit_l _iswdigit_l
340 #define isalpha_l _isalpha_l
341 #define iswalpha_l _iswalpha_l
342 #define isalnum_l _isalnum_l
343 #define iswalnum_l _iswalnum_l
344 #define isupper_l _isupper_l
345 #define iswupper_l _iswupper_l
346 #define islower_l _islower_l
347 #define iswlower_l _iswlower_l
348 #define isgraph_l _isgraph_l
349 #define iswgraph_l _iswgraph_l
350 #define isprint_l _isprint_l
351 #define iswprint_l _iswprint_l
352 #define ispunct_l _ispunct_l
353 #define iswpunct_l _iswpunct_l
354 #define isspace_l _isspace_l
355 #define iswspace_l _iswspace_l
356 #define strcoll_l _strcoll_l
357 #define strxfrm_l _strxfrm_l
358 #define wcscoll_l _wcscoll_l
359 #define wcstombs_l _wcstombs_l
360 #define mbstowcs_l _mbstowcs_l
361 
362 
363 /* In backend/port/win32/signal.c */
364 extern PGDLLIMPORT volatile int pg_signal_queue;
365 extern PGDLLIMPORT int pg_signal_mask;
366 extern HANDLE pgwin32_signal_event;
367 extern HANDLE pgwin32_initial_signal_pipe;
368 
369 #define UNBLOCKED_SIGNAL_QUEUE() (pg_signal_queue & ~pg_signal_mask)
370 
371 
372 void pgwin32_signal_initialize(void);
373 HANDLE pgwin32_create_signal_listener(pid_t pid);
375 void pg_queue_signal(int signum);
376 
377 /* In backend/port/win32/socket.c */
378 #ifndef FRONTEND
379 #define socket(af, type, protocol) pgwin32_socket(af, type, protocol)
380 #define bind(s, addr, addrlen) pgwin32_bind(s, addr, addrlen)
381 #define listen(s, backlog) pgwin32_listen(s, backlog)
382 #define accept(s, addr, addrlen) pgwin32_accept(s, addr, addrlen)
383 #define connect(s, name, namelen) pgwin32_connect(s, name, namelen)
384 #define select(n, r, w, e, timeout) pgwin32_select(n, r, w, e, timeout)
385 #define recv(s, buf, len, flags) pgwin32_recv(s, buf, len, flags)
386 #define send(s, buf, len, flags) pgwin32_send(s, buf, len, flags)
387 
388 SOCKET pgwin32_socket(int af, int type, int protocol);
389 int pgwin32_bind(SOCKET s, struct sockaddr * addr, int addrlen);
390 int pgwin32_listen(SOCKET s, int backlog);
391 SOCKET pgwin32_accept(SOCKET s, struct sockaddr * addr, int *addrlen);
392 int pgwin32_connect(SOCKET s, const struct sockaddr * name, int namelen);
393 int pgwin32_select(int nfds, fd_set *readfs, fd_set *writefds, fd_set *exceptfds, const struct timeval * timeout);
394 int pgwin32_recv(SOCKET s, char *buf, int len, int flags);
395 int pgwin32_send(SOCKET s, const void *buf, int len, int flags);
396 
397 const char *pgwin32_socket_strerror(int err);
398 int pgwin32_waitforsinglesocket(SOCKET s, int what, int timeout);
399 
400 extern int pgwin32_noblock;
401 
402 #endif
403 
404 /* in backend/port/win32_shmem.c */
405 extern int pgwin32_ReserveSharedMemoryRegion(HANDLE);
406 
407 /* in backend/port/win32/crashdump.c */
408 extern void pgwin32_install_crashdump_handler(void);
409 
410 /* in port/win32error.c */
411 extern void _dosmaperr(unsigned long);
412 
413 /* in port/win32env.c */
414 extern int pgwin32_putenv(const char *);
415 extern void pgwin32_unsetenv(const char *);
416 
417 /* in port/win32security.c */
418 extern int pgwin32_is_service(void);
419 extern int pgwin32_is_admin(void);
420 
421 #define putenv(x) pgwin32_putenv(x)
422 #define unsetenv(x) pgwin32_unsetenv(x)
423 
424 /* Things that exist in MingW headers, but need to be added to MSVC & BCC */
425 #ifdef WIN32_ONLY_COMPILER
426 
427 #ifndef _WIN64
428 typedef long ssize_t;
429 #else
430 typedef __int64 ssize_t;
431 #endif
432 
433 #ifndef __BORLANDC__
434 typedef unsigned short mode_t;
435 
436 #define S_IRUSR _S_IREAD
437 #define S_IWUSR _S_IWRITE
438 #define S_IXUSR _S_IEXEC
439 #define S_IRWXU (S_IRUSR | S_IWUSR | S_IXUSR)
440 /* see also S_IRGRP etc below */
441 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
442 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
443 #endif /* __BORLANDC__ */
444 
445 #define F_OK 0
446 #define W_OK 2
447 #define R_OK 4
448 
449 #if (_MSC_VER < 1800)
450 #define isinf(x) ((_fpclass(x) == _FPCLASS_PINF) || (_fpclass(x) == _FPCLASS_NINF))
451 #define isnan(x) _isnan(x)
452 #endif
453 
454 /* Pulled from Makefile.port in mingw */
455 #define DLSUFFIX ".dll"
456 
457 #ifdef __BORLANDC__
458 
459 /* for port/dirent.c */
460 #ifndef INVALID_FILE_ATTRIBUTES
461 #define INVALID_FILE_ATTRIBUTES ((DWORD) -1)
462 #endif
463 
464 /* for port/open.c */
465 #ifndef O_RANDOM
466 #define O_RANDOM 0x0010 /* File access is primarily random */
467 #define O_SEQUENTIAL 0x0020 /* File access is primarily sequential */
468 #define O_TEMPORARY 0x0040 /* Temporary file bit */
469 #define O_SHORT_LIVED 0x1000 /* Temporary storage file, try not to flush */
470 #define _O_SHORT_LIVED O_SHORT_LIVED
471 #endif /* ifndef O_RANDOM */
472 #endif /* __BORLANDC__ */
473 #endif /* WIN32_ONLY_COMPILER */
474 
475 /* These aren't provided by either MingW or MSVC */
476 #ifndef __BORLANDC__
477 #define S_IRGRP 0
478 #define S_IWGRP 0
479 #define S_IXGRP 0
480 #define S_IRWXG 0
481 #define S_IROTH 0
482 #define S_IWOTH 0
483 #define S_IXOTH 0
484 #define S_IRWXO 0
485 
486 #endif /* __BORLANDC__ */
PGDLLIMPORT volatile int pg_signal_queue
Definition: signal.c:24
int pgwin32_is_service(void)
int tz_dsttime
Definition: win32.h:223
static struct @76 value
PGDLLIMPORT int pg_signal_mask
Definition: signal.c:25
int gid_t
Definition: win32.h:261
void pgwin32_unsetenv(const char *)
Definition: win32env.c:114
int uid_t
Definition: win32.h:260
#define PGDLLIMPORT
Definition: win32.h:103
struct timeval it_value
Definition: win32.h:232
int pgwin32_listen(SOCKET s, int backlog)
Definition: socket.c:312
int pgwin32_waitforsinglesocket(SOCKET s, int what, int timeout)
Definition: socket.c:168
void pgwin32_signal_initialize(void)
Definition: signal.c:68
int pgwin32_putenv(const char *)
Definition: win32env.c:20
int pgwin32_connect(SOCKET s, const struct sockaddr *name, int namelen)
Definition: socket.c:345
long key_t
Definition: win32.h:263
int pgwin32_bind(SOCKET s, struct sockaddr *addr, int addrlen)
Definition: socket.c:301
int pgwin32_send(SOCKET s, const void *buf, int len, int flags)
Definition: socket.c:445
const char * pgwin32_socket_strerror(int err)
Definition: socket.c:705
int setitimer(int which, const struct itimerval *value, struct itimerval *ovalue)
Definition: timer.c:86
HANDLE pgwin32_initial_signal_pipe
Definition: signal.c:28
static char * buf
Definition: pg_test_fsync.c:65
int pgwin32_recv(SOCKET s, char *buf, int len, int flags)
Definition: socket.c:368
SOCKET pgwin32_accept(SOCKET s, struct sockaddr *addr, int *addrlen)
Definition: socket.c:323
HANDLE pgwin32_create_signal_listener(pid_t pid)
Definition: signal.c:181
int pgwin32_ReserveSharedMemoryRegion(HANDLE)
Definition: win32_shmem.c:402
void pgwin32_dispatch_queued_signals(void)
Definition: signal.c:107
void pgwin32_install_crashdump_handler(void)
Definition: crashdump.c:181
void _dosmaperr(unsigned long)
Definition: win32error.c:171
HANDLE pgwin32_signal_event
Definition: signal.c:27
struct timeval it_interval
Definition: win32.h:231
const char * name
Definition: encode.c:521
int pgwin32_noblock
Definition: socket.c:28
int pgwin32_is_admin(void)
Definition: win32security.c:54
int pgwin32_select(int nfds, fd_set *readfs, fd_set *writefds, fd_set *exceptfds, const struct timeval *timeout)
Definition: socket.c:503
SOCKET pgwin32_socket(int af, int type, int protocol)
Definition: socket.c:278
void pg_queue_signal(int signum)
Definition: signal.c:210
int tz_minuteswest
Definition: win32.h:222