win32_port.h 16 KB

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