Debian Codegraph @

src/include/port/win32_port.h

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