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