pg_config_os.h 13 KB

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