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