PostgreSQL Source Code  git master
win32_port.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * win32_port.h
4  * Windows-specific compatibility stuff.
5  *
6  * Note this is read in MinGW as well as native Windows builds,
7  * but not in Cygwin builds.
8  *
9  * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
10  * Portions Copyright (c) 1994, Regents of the University of California
11  *
12  * src/include/port/win32_port.h
13  *
14  *-------------------------------------------------------------------------
15  */
16 #ifndef PG_WIN32_PORT_H
17 #define PG_WIN32_PORT_H
18 
19 /*
20  * Always build with SSPI support. Keep it as a #define in case
21  * we want a switch to disable it sometime in the future.
22  */
23 #define ENABLE_SSPI 1
24 
25 /* undefine and redefine after #include */
26 #undef mkdir
27 
28 #undef ERROR
29 
30 /*
31  * VS2013 and later issue warnings about using the old Winsock API,
32  * which we don't really want to hear about.
33  */
34 #ifdef _MSC_VER
35 #define _WINSOCK_DEPRECATED_NO_WARNINGS
36 #endif
37 
38 /*
39  * The MinGW64 headers choke if this is already defined - they
40  * define it themselves.
41  */
42 #if !defined(__MINGW64_VERSION_MAJOR) || defined(_MSC_VER)
43 #define _WINSOCKAPI_
44 #endif
45 
46 #include <winsock2.h>
47 #include <ws2tcpip.h>
48 #include <windows.h>
49 #undef small
50 #include <process.h>
51 #include <signal.h>
52 #include <direct.h>
53 #include <sys/utime.h> /* for non-unicode version */
54 #undef near
55 #include <sys/stat.h> /* needed before sys/stat hacking below */
56 
57 /* Must be here to avoid conflicting with prototype in windows.h */
58 #define mkdir(a,b) mkdir(a)
59 
60 #define ftruncate(a,b) chsize(a,b)
61 
62 /* Windows doesn't have fsync() as such, use _commit() */
63 #define fsync(fd) _commit(fd)
64 
65 /*
66  * For historical reasons, we allow setting wal_sync_method to
67  * fsync_writethrough on Windows, even though it's really identical to fsync
68  * (both code paths wind up at _commit()).
69  */
70 #define HAVE_FSYNC_WRITETHROUGH
71 #define FSYNC_WRITETHROUGH_IS_FSYNC
72 
73 #define USES_WINSOCK
74 
75 /*
76  * IPC defines
77  */
78 #undef HAVE_UNION_SEMUN
79 #define HAVE_UNION_SEMUN 1
80 
81 #define IPC_RMID 256
82 #define IPC_CREAT 512
83 #define IPC_EXCL 1024
84 #define IPC_PRIVATE 234564
85 #define IPC_NOWAIT 2048
86 #define IPC_STAT 4096
87 
88 #define EACCESS 2048
89 #ifndef EIDRM
90 #define EIDRM 4096
91 #endif
92 
93 #define SETALL 8192
94 #define GETNCNT 16384
95 #define GETVAL 65536
96 #define SETVAL 131072
97 #define GETPID 262144
98 
99 
100 /*
101  * Signal stuff
102  *
103  * For WIN32, there is no wait() call so there are no wait() macros
104  * to interpret the return value of system(). Instead, system()
105  * return values < 0x100 are used for exit() termination, and higher
106  * values are used to indicated non-exit() termination, which is
107  * similar to a unix-style signal exit (think SIGSEGV ==
108  * STATUS_ACCESS_VIOLATION). Return values are broken up into groups:
109  *
110  * http://msdn2.microsoft.com/en-gb/library/aa489609.aspx
111  *
112  * NT_SUCCESS 0 - 0x3FFFFFFF
113  * NT_INFORMATION 0x40000000 - 0x7FFFFFFF
114  * NT_WARNING 0x80000000 - 0xBFFFFFFF
115  * NT_ERROR 0xC0000000 - 0xFFFFFFFF
116  *
117  * Effectively, we don't care on the severity of the return value from
118  * system(), we just need to know if it was because of exit() or generated
119  * by the system, and it seems values >= 0x100 are system-generated.
120  * See this URL for a list of WIN32 STATUS_* values:
121  *
122  * Wine (URL used in our error messages) -
123  * http://source.winehq.org/source/include/ntstatus.h
124  * Descriptions - http://www.comp.nus.edu.sg/~wuyongzh/my_doc/ntstatus.txt
125  * MS SDK - http://www.nologs.com/ntstatus.html
126  *
127  * It seems the exception lists are in both ntstatus.h and winnt.h, but
128  * ntstatus.h has a more comprehensive list, and it only contains
129  * exception values, rather than winnt, which contains lots of other
130  * things:
131  *
132  * http://www.microsoft.com/msj/0197/exception/exception.aspx
133  *
134  * The ExceptionCode parameter is the number that the operating system
135  * assigned to the exception. You can see a list of various exception codes
136  * in WINNT.H by searching for #defines that start with "STATUS_". For
137  * example, the code for the all-too-familiar STATUS_ACCESS_VIOLATION is
138  * 0xC0000005. A more complete set of exception codes can be found in
139  * NTSTATUS.H from the Windows NT DDK.
140  *
141  * Some day we might want to print descriptions for the most common
142  * exceptions, rather than printing an include file name. We could use
143  * RtlNtStatusToDosError() and pass to FormatMessage(), which can print
144  * the text of error values, but MinGW does not support
145  * RtlNtStatusToDosError().
146  */
147 #define WIFEXITED(w) (((w) & 0XFFFFFF00) == 0)
148 #define WIFSIGNALED(w) (!WIFEXITED(w))
149 #define WEXITSTATUS(w) (w)
150 #define WTERMSIG(w) (w)
151 
152 #define sigmask(sig) ( 1 << ((sig)-1) )
153 
154 /* Signal function return values */
155 #undef SIG_DFL
156 #undef SIG_ERR
157 #undef SIG_IGN
158 #define SIG_DFL ((pqsigfunc)0)
159 #define SIG_ERR ((pqsigfunc)-1)
160 #define SIG_IGN ((pqsigfunc)1)
161 
162 /* Some extra signals */
163 #define SIGHUP 1
164 #define SIGQUIT 3
165 #define SIGTRAP 5
166 #define SIGABRT 22 /* Set to match W32 value -- not UNIX value */
167 #define SIGKILL 9
168 #define SIGPIPE 13
169 #define SIGALRM 14
170 #define SIGSTOP 17
171 #define SIGTSTP 18
172 #define SIGCONT 19
173 #define SIGCHLD 20
174 #define SIGWINCH 28
175 #define SIGUSR1 30
176 #define SIGUSR2 31
177 
178 /*
179  * New versions of MinGW have gettimeofday() and also declare
180  * struct timezone to support it.
181  */
182 #ifndef HAVE_GETTIMEOFDAY
183 struct timezone
184 {
185  int tz_minuteswest; /* Minutes west of GMT. */
186  int tz_dsttime; /* Nonzero if DST is ever in effect. */
187 };
188 #endif
189 
190 /* for setitimer in backend/port/win32/timer.c */
191 #define ITIMER_REAL 0
192 struct itimerval
193 {
194  struct timeval it_interval;
195  struct timeval it_value;
196 };
197 
198 int setitimer(int which, const struct itimerval *value, struct itimerval *ovalue);
199 
200 /*
201  * WIN32 does not provide 64-bit off_t, but does provide the functions operating
202  * with 64-bit offsets.
203  */
204 #define pgoff_t __int64
205 #ifdef _MSC_VER
206 #define fseeko(stream, offset, origin) _fseeki64(stream, offset, origin)
207 #define ftello(stream) _ftelli64(stream)
208 #else
209 #ifndef fseeko
210 #define fseeko(stream, offset, origin) fseeko64(stream, offset, origin)
211 #endif
212 #ifndef ftello
213 #define ftello(stream) ftello64(stream)
214 #endif
215 #endif
216 
217 /*
218  * Win32 also doesn't have symlinks, but we can emulate them with
219  * junction points on newer Win32 versions.
220  *
221  * Cygwin has its own symlinks which work on Win95/98/ME where
222  * junction points don't, so use those instead. We have no way of
223  * knowing what type of system Cygwin binaries will be run on.
224  * Note: Some CYGWIN includes might #define WIN32.
225  */
226 extern int pgsymlink(const char *oldpath, const char *newpath);
227 extern int pgreadlink(const char *path, char *buf, size_t size);
228 extern bool pgwin32_is_junction(const char *path);
229 
230 #define symlink(oldpath, newpath) pgsymlink(oldpath, newpath)
231 #define readlink(path, buf, size) pgreadlink(path, buf, size)
232 
233 /*
234  * Supplement to <sys/types.h>.
235  *
236  * Perl already has typedefs for uid_t and gid_t.
237  */
238 #ifndef PLPERL_HAVE_UID_GID
239 typedef int uid_t;
240 typedef int gid_t;
241 #endif
242 typedef long key_t;
243 
244 #ifdef _MSC_VER
245 typedef int pid_t;
246 #endif
247 
248 /*
249  * Supplement to <sys/stat.h>.
250  *
251  * We must pull in sys/stat.h before this part, else our overrides lose.
252  */
253 #define lstat(path, sb) stat(path, sb)
254 
255 /*
256  * stat() is not guaranteed to set the st_size field on win32, so we
257  * redefine it to our own implementation that is.
258  *
259  * Some frontends don't need the size from stat, so if UNSAFE_STAT_OK
260  * is defined we don't bother with this.
261  */
262 #ifndef UNSAFE_STAT_OK
263 extern int pgwin32_safestat(const char *path, struct stat *buf);
264 #define stat(a,b) pgwin32_safestat(a,b)
265 #endif
266 
267 /* These macros are not provided by older MinGW, nor by MSVC */
268 #ifndef S_IRUSR
269 #define S_IRUSR _S_IREAD
270 #endif
271 #ifndef S_IWUSR
272 #define S_IWUSR _S_IWRITE
273 #endif
274 #ifndef S_IXUSR
275 #define S_IXUSR _S_IEXEC
276 #endif
277 #ifndef S_IRWXU
278 #define S_IRWXU (S_IRUSR | S_IWUSR | S_IXUSR)
279 #endif
280 #ifndef S_IRGRP
281 #define S_IRGRP 0
282 #endif
283 #ifndef S_IWGRP
284 #define S_IWGRP 0
285 #endif
286 #ifndef S_IXGRP
287 #define S_IXGRP 0
288 #endif
289 #ifndef S_IRWXG
290 #define S_IRWXG 0
291 #endif
292 #ifndef S_IROTH
293 #define S_IROTH 0
294 #endif
295 #ifndef S_IWOTH
296 #define S_IWOTH 0
297 #endif
298 #ifndef S_IXOTH
299 #define S_IXOTH 0
300 #endif
301 #ifndef S_IRWXO
302 #define S_IRWXO 0
303 #endif
304 #ifndef S_ISDIR
305 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
306 #endif
307 #ifndef S_ISREG
308 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
309 #endif
310 
311 /*
312  * Supplement to <fcntl.h>.
313  * This is the same value as _O_NOINHERIT in the MS header file. This is
314  * to ensure that we don't collide with a future definition. It means
315  * we cannot use _O_NOINHERIT ourselves.
316  */
317 #define O_DSYNC 0x0080
318 
319 /*
320  * Supplement to <errno.h>.
321  *
322  * We redefine network-related Berkeley error symbols as the corresponding WSA
323  * constants. This allows strerror.c to recognize them as being in the Winsock
324  * error code range and pass them off to win32_socket_strerror(), since
325  * Windows' version of plain strerror() won't cope. Note that this will break
326  * if these names are used for anything else besides Windows Sockets errors.
327  * See TranslateSocketError() when changing this list.
328  */
329 #undef EAGAIN
330 #define EAGAIN WSAEWOULDBLOCK
331 #undef EINTR
332 #define EINTR WSAEINTR
333 #undef EMSGSIZE
334 #define EMSGSIZE WSAEMSGSIZE
335 #undef EAFNOSUPPORT
336 #define EAFNOSUPPORT WSAEAFNOSUPPORT
337 #undef EWOULDBLOCK
338 #define EWOULDBLOCK WSAEWOULDBLOCK
339 #undef ECONNABORTED
340 #define ECONNABORTED WSAECONNABORTED
341 #undef ECONNRESET
342 #define ECONNRESET WSAECONNRESET
343 #undef EINPROGRESS
344 #define EINPROGRESS WSAEINPROGRESS
345 #undef EISCONN
346 #define EISCONN WSAEISCONN
347 #undef ENOBUFS
348 #define ENOBUFS WSAENOBUFS
349 #undef EPROTONOSUPPORT
350 #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
351 #undef ECONNREFUSED
352 #define ECONNREFUSED WSAECONNREFUSED
353 #undef ENOTSOCK
354 #define ENOTSOCK WSAENOTSOCK
355 #undef EOPNOTSUPP
356 #define EOPNOTSUPP WSAEOPNOTSUPP
357 #undef EADDRINUSE
358 #define EADDRINUSE WSAEADDRINUSE
359 #undef EADDRNOTAVAIL
360 #define EADDRNOTAVAIL WSAEADDRNOTAVAIL
361 #undef EHOSTUNREACH
362 #define EHOSTUNREACH WSAEHOSTUNREACH
363 #undef ENOTCONN
364 #define ENOTCONN WSAENOTCONN
365 
366 /*
367  * Locale stuff.
368  *
369  * Extended locale functions with gratuitous underscore prefixes.
370  * (These APIs are nevertheless fully documented by Microsoft.)
371  */
372 #define locale_t _locale_t
373 #define tolower_l _tolower_l
374 #define toupper_l _toupper_l
375 #define towlower_l _towlower_l
376 #define towupper_l _towupper_l
377 #define isdigit_l _isdigit_l
378 #define iswdigit_l _iswdigit_l
379 #define isalpha_l _isalpha_l
380 #define iswalpha_l _iswalpha_l
381 #define isalnum_l _isalnum_l
382 #define iswalnum_l _iswalnum_l
383 #define isupper_l _isupper_l
384 #define iswupper_l _iswupper_l
385 #define islower_l _islower_l
386 #define iswlower_l _iswlower_l
387 #define isgraph_l _isgraph_l
388 #define iswgraph_l _iswgraph_l
389 #define isprint_l _isprint_l
390 #define iswprint_l _iswprint_l
391 #define ispunct_l _ispunct_l
392 #define iswpunct_l _iswpunct_l
393 #define isspace_l _isspace_l
394 #define iswspace_l _iswspace_l
395 #define strcoll_l _strcoll_l
396 #define strxfrm_l _strxfrm_l
397 #define wcscoll_l _wcscoll_l
398 #define wcstombs_l _wcstombs_l
399 #define mbstowcs_l _mbstowcs_l
400 
401 /*
402  * Versions of libintl >= 0.18? try to replace setlocale() with a macro
403  * to their own versions. Remove the macro, if it exists, because it
404  * ends up calling the wrong version when the backend and libintl use
405  * different versions of msvcrt.
406  */
407 #if defined(setlocale)
408 #undef setlocale
409 #endif
410 
411 /*
412  * Define our own wrapper macro around setlocale() to work around bugs in
413  * Windows' native setlocale() function.
414  */
415 extern char *pgwin32_setlocale(int category, const char *locale);
416 
417 #define setlocale(a,b) pgwin32_setlocale(a,b)
418 
419 
420 /* In backend/port/win32/signal.c */
421 extern PGDLLIMPORT volatile int pg_signal_queue;
422 extern PGDLLIMPORT int pg_signal_mask;
423 extern HANDLE pgwin32_signal_event;
424 extern HANDLE pgwin32_initial_signal_pipe;
425 
426 #define UNBLOCKED_SIGNAL_QUEUE() (pg_signal_queue & ~pg_signal_mask)
427 #define PG_SIGNAL_COUNT 32
428 
429 void pgwin32_signal_initialize(void);
430 HANDLE pgwin32_create_signal_listener(pid_t pid);
432 void pg_queue_signal(int signum);
433 
434 /* In src/port/kill.c */
435 #define kill(pid,sig) pgkill(pid,sig)
436 extern int pgkill(int pid, int sig);
437 
438 /* In backend/port/win32/socket.c */
439 #ifndef FRONTEND
440 #define socket(af, type, protocol) pgwin32_socket(af, type, protocol)
441 #define bind(s, addr, addrlen) pgwin32_bind(s, addr, addrlen)
442 #define listen(s, backlog) pgwin32_listen(s, backlog)
443 #define accept(s, addr, addrlen) pgwin32_accept(s, addr, addrlen)
444 #define connect(s, name, namelen) pgwin32_connect(s, name, namelen)
445 #define select(n, r, w, e, timeout) pgwin32_select(n, r, w, e, timeout)
446 #define recv(s, buf, len, flags) pgwin32_recv(s, buf, len, flags)
447 #define send(s, buf, len, flags) pgwin32_send(s, buf, len, flags)
448 
449 SOCKET pgwin32_socket(int af, int type, int protocol);
450 int pgwin32_bind(SOCKET s, struct sockaddr *addr, int addrlen);
451 int pgwin32_listen(SOCKET s, int backlog);
452 SOCKET pgwin32_accept(SOCKET s, struct sockaddr *addr, int *addrlen);
453 int pgwin32_connect(SOCKET s, const struct sockaddr *name, int namelen);
454 int pgwin32_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, const struct timeval *timeout);
455 int pgwin32_recv(SOCKET s, char *buf, int len, int flags);
456 int pgwin32_send(SOCKET s, const void *buf, int len, int flags);
457 int pgwin32_waitforsinglesocket(SOCKET s, int what, int timeout);
458 
459 extern int pgwin32_noblock;
460 
461 #endif /* FRONTEND */
462 
463 /* in backend/port/win32_shmem.c */
464 extern int pgwin32_ReserveSharedMemoryRegion(HANDLE);
465 
466 /* in backend/port/win32/crashdump.c */
467 extern void pgwin32_install_crashdump_handler(void);
468 
469 /* in port/win32error.c */
470 extern void _dosmaperr(unsigned long);
471 
472 /* in port/win32env.c */
473 extern int pgwin32_putenv(const char *);
474 extern void pgwin32_unsetenv(const char *);
475 
476 /* in port/win32security.c */
477 extern int pgwin32_is_service(void);
478 extern int pgwin32_is_admin(void);
479 
480 /* Windows security token manipulation (in src/common/exec.c) */
481 extern BOOL AddUserToTokenDacl(HANDLE hToken);
482 
483 #define putenv(x) pgwin32_putenv(x)
484 #define unsetenv(x) pgwin32_unsetenv(x)
485 
486 /* Things that exist in MinGW headers, but need to be added to MSVC */
487 #ifdef _MSC_VER
488 
489 #ifndef _WIN64
490 typedef long ssize_t;
491 #else
492 typedef __int64 ssize_t;
493 #endif
494 
495 typedef unsigned short mode_t;
496 
497 #define F_OK 0
498 #define W_OK 2
499 #define R_OK 4
500 
501 /*
502  * isinf() and isnan() should per spec be in <math.h>, but MSVC older than
503  * 2013 does not have them there. It does have _fpclass() and _isnan(), but
504  * they're in <float.h>, so include that here even though it means float.h
505  * percolates to our whole tree. Recent versions don't require any of this.
506  */
507 #if (_MSC_VER < 1800)
508 #include <float.h>
509 #define isinf(x) ((_fpclass(x) == _FPCLASS_PINF) || (_fpclass(x) == _FPCLASS_NINF))
510 #define isnan(x) _isnan(x)
511 #endif
512 
513 /* Pulled from Makefile.port in MinGW */
514 #define DLSUFFIX ".dll"
515 
516 #endif /* _MSC_VER */
517 
518 #if (defined(_MSC_VER) && (_MSC_VER < 1900)) || \
519  defined(__MINGW32__) || defined(__MINGW64__)
520 /*
521  * VS2013 has a strtof() that seems to give correct answers for valid input,
522  * even on the rounding edge cases, but which doesn't handle out-of-range
523  * input correctly. Work around that.
524  *
525  * Mingw claims to have a strtof, and my reading of its source code suggests
526  * that it ought to work (and not need this hack), but the regression test
527  * results disagree with me; whether this is a version issue or not is not
528  * clear. However, using our wrapper (and the misrounded-input variant file,
529  * already required for supporting ancient systems) can't make things any
530  * worse, except for a tiny performance loss when reading zeros.
531  *
532  * See also cygwin.h for another instance of this.
533  */
534 #define HAVE_BUGGY_STRTOF 1
535 #endif
536 
537 #endif /* PG_WIN32_PORT_H */
int setitimer(int which, const struct itimerval *value, struct itimerval *ovalue)
Definition: timer.c:86
int tz_dsttime
Definition: win32_port.h:186
int pgwin32_noblock
Definition: socket.c:28
HANDLE pgwin32_signal_event
Definition: signal.c:27
int pgwin32_listen(SOCKET s, int backlog)
Definition: socket.c:312
int pgwin32_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, const struct timeval *timeout)
Definition: socket.c:503
void pgwin32_install_crashdump_handler(void)
Definition: crashdump.c:180
static struct @144 value
void pgwin32_dispatch_queued_signals(void)
Definition: signal.c:108
HANDLE pgwin32_initial_signal_pipe
Definition: signal.c:28
int pgwin32_putenv(const char *)
Definition: win32env.c:20
void _dosmaperr(unsigned long)
Definition: win32error.c:171
void pg_queue_signal(int signum)
Definition: signal.c:212
SOCKET pgwin32_socket(int af, int type, int protocol)
Definition: socket.c:278
BOOL AddUserToTokenDacl(HANDLE hToken)
#define PGDLLIMPORT
Definition: c.h:1243
int pgwin32_ReserveSharedMemoryRegion(HANDLE)
Definition: win32_shmem.c:550
PGDLLIMPORT int pg_signal_mask
Definition: signal.c:25
int pgwin32_bind(SOCKET s, struct sockaddr *addr, int addrlen)
Definition: socket.c:301
static char * buf
Definition: pg_test_fsync.c:68
int pgkill(int pid, int sig)
int pgwin32_is_service(void)
int pgreadlink(const char *path, char *buf, size_t size)
int pgwin32_is_admin(void)
Definition: win32security.c:49
int pgwin32_waitforsinglesocket(SOCKET s, int what, int timeout)
Definition: socket.c:168
PGDLLIMPORT volatile int pg_signal_queue
Definition: signal.c:24
#define stat(a, b)
Definition: win32_port.h:264
int pgwin32_send(SOCKET s, const void *buf, int len, int flags)
Definition: socket.c:445
int pgwin32_recv(SOCKET s, char *buf, int len, int flags)
Definition: socket.c:368
void pgwin32_signal_initialize(void)
Definition: signal.c:69
char * pgwin32_setlocale(int category, const char *locale)
static int sig
Definition: pg_ctl.c:84
HANDLE pgwin32_create_signal_listener(pid_t pid)
Definition: signal.c:183
int pgwin32_connect(SOCKET s, const struct sockaddr *name, int namelen)
Definition: socket.c:345
long key_t
Definition: win32_port.h:242
int uid_t
Definition: win32_port.h:239
const char * name
Definition: encode.c:521
void pgwin32_unsetenv(const char *)
Definition: win32env.c:114
int gid_t
Definition: win32_port.h:240
int pgwin32_safestat(const char *path, struct stat *buf)
static char * locale
Definition: initdb.c:125
SOCKET pgwin32_accept(SOCKET s, struct sockaddr *addr, int *addrlen)
Definition: socket.c:323
bool pgwin32_is_junction(const char *path)
int tz_minuteswest
Definition: win32_port.h:185
int pgsymlink(const char *oldpath, const char *newpath)