]> granicus.if.org Git - postgresql/blob - src/include/port/win32.h
Change win32 child-death tracking code to use a threadpool to wait for
[postgresql] / src / include / port / win32.h
1 /* $PostgreSQL: pgsql/src/include/port/win32.h,v 1.77 2007/10/26 21:50:10 mha Exp $ */
2
3 #if defined(_MSC_VER) || defined(__BORLANDC__)
4 #define WIN32_ONLY_COMPILER
5 #endif
6
7 #define _WIN32_WINNT 0x0500
8 /*
9  * Always build with SSPI support. Keep it as a #define in case 
10  * we want a switch to disable it sometime in the future.
11  */
12 #define ENABLE_SSPI 1
13
14
15 /* undefine and redefine after #include */
16 #undef mkdir
17
18 #undef ERROR
19 #define _WINSOCKAPI_
20 #include <windows.h>
21 #include <winsock2.h>
22 #include <ws2tcpip.h>
23 #undef small
24 #include <process.h>
25 #include <signal.h>
26 #include <errno.h>
27 #include <direct.h>
28 #ifndef __BORLANDC__
29 #include <sys/utime.h>                  /* for non-unicode version */
30 #endif
31 #undef near
32
33 /* Must be here to avoid conflicting with prototype in windows.h */
34 #define mkdir(a,b)      mkdir(a)
35
36 #define HAVE_FSYNC_WRITETHROUGH
37 #define HAVE_FSYNC_WRITETHROUGH_ONLY
38 #define ftruncate(a,b)  chsize(a,b)
39 /*
40  *      Even though we don't support 'fsync' as a wal_sync_method,
41  *      we do fsync() a few other places where _commit() is just fine.
42  */
43 #define fsync(fd) _commit(fd)
44
45 #define USES_WINSOCK
46
47 /* defines for dynamic linking on Win32 platform */
48 #if defined(WIN32) || defined(__CYGWIN__)
49
50 #if __GNUC__ && ! defined (__declspec)
51 #error You need egcs 1.1 or newer for compiling!
52 #endif
53
54 #ifdef BUILDING_DLL
55 #define PGDLLIMPORT __declspec (dllexport)
56 #else                                                   /* not BUILDING_DLL */
57 #define PGDLLIMPORT __declspec (dllimport)
58 #endif
59 #else                                                   /* not CYGWIN, not MSVC, not MingW */
60
61 #define PGDLLIMPORT
62 #endif
63
64
65 /*
66  *      IPC defines
67  */
68 #undef HAVE_UNION_SEMUN
69 #define HAVE_UNION_SEMUN 1
70
71 #define IPC_RMID 256
72 #define IPC_CREAT 512
73 #define IPC_EXCL 1024
74 #define IPC_PRIVATE 234564
75 #define IPC_NOWAIT      2048
76 #define IPC_STAT 4096
77
78 #define EACCESS 2048
79 #define EIDRM 4096
80
81 #define SETALL 8192
82 #define GETNCNT 16384
83 #define GETVAL 65536
84 #define SETVAL 131072
85 #define GETPID 262144
86
87
88 /*
89  *      Signal stuff
90  *
91  *      For WIN32, there is no wait() call so there are no wait() macros
92  *      to interpret the return value of system().  Instead, system()
93  *      return values < 0x100 are used for exit() termination, and higher
94  *      values are used to indicated non-exit() termination, which is
95  *      similar to a unix-style signal exit (think SIGSEGV ==
96  *      STATUS_ACCESS_VIOLATION).  Return values are broken up into groups:
97  *
98  *      http://msdn2.microsoft.com/en-gb/library/aa489609.aspx
99  *
100  *              NT_SUCCESS                      0 - 0x3FFFFFFF
101  *              NT_INFORMATION          0x40000000 - 0x7FFFFFFF
102  *              NT_WARNING                      0x80000000 - 0xBFFFFFFF
103  *              NT_ERROR                        0xC0000000 - 0xFFFFFFFF
104  *
105  *      Effectively, we don't care on the severity of the return value from
106  *      system(), we just need to know if it was because of exit() or generated
107  *      by the system, and it seems values >= 0x100 are system-generated.
108  *      See this URL for a list of WIN32 STATUS_* values:
109  *
110  *              Wine (URL used in our error messages) -
111  *                      http://source.winehq.org/source/include/ntstatus.h
112  *              Descriptions - http://www.comp.nus.edu.sg/~wuyongzh/my_doc/ntstatus.txt
113  *              MS SDK - http://www.nologs.com/ntstatus.html
114  *
115  *      It seems the exception lists are in both ntstatus.h and winnt.h, but
116  *      ntstatus.h has a more comprehensive list, and it only contains
117  *      exception values, rather than winnt, which contains lots of other
118  *      things:
119  *
120  *              http://www.microsoft.com/msj/0197/exception/exception.aspx
121  *
122  *              The ExceptionCode parameter is the number that the operating system
123  *              assigned to the exception. You can see a list of various exception codes
124  *              in WINNT.H by searching for #defines that start with "STATUS_". For
125  *              example, the code for the all-too-familiar STATUS_ACCESS_VIOLATION is
126  *              0xC0000005. A more complete set of exception codes can be found in
127  *              NTSTATUS.H from the Windows NT DDK.
128  *                                               
129  *      Some day we might want to print descriptions for the most common
130  *      exceptions, rather than printing an include file name.  We could use
131  *      RtlNtStatusToDosError() and pass to FormatMessage(), which can print
132  *      the text of error values, but MinGW does not support
133  *      RtlNtStatusToDosError().
134  */
135 #define WIFEXITED(w)    (((w) & 0XFFFFFF00) == 0)
136 #define WIFSIGNALED(w)  (!WIFEXITED(w))
137 #define WEXITSTATUS(w)  (w)
138 #define WTERMSIG(w)     (w)
139
140 #define sigmask(sig) ( 1 << ((sig)-1) )
141
142 /* Signal function return values */
143 #undef SIG_DFL
144 #undef SIG_ERR
145 #undef SIG_IGN
146 #define SIG_DFL ((pqsigfunc)0)
147 #define SIG_ERR ((pqsigfunc)-1)
148 #define SIG_IGN ((pqsigfunc)1)
149
150 /* Some extra signals */
151 #define SIGHUP                          1
152 #define SIGQUIT                         3
153 #define SIGTRAP                         5
154 #define SIGABRT                         22      /* Set to match W32 value -- not UNIX value */
155 #define SIGKILL                         9
156 #define SIGPIPE                         13
157 #define SIGALRM                         14
158 #define SIGSTOP                         17
159 #define SIGTSTP                         18
160 #define SIGCONT                         19
161 #define SIGCHLD                         20
162 #define SIGTTIN                         21
163 #define SIGTTOU                         22      /* Same as SIGABRT -- no problem, I hope */
164 #define SIGWINCH                        28
165 #ifndef __BORLANDC__
166 #define SIGUSR1                         30
167 #define SIGUSR2                         31
168 #endif
169
170 struct timezone
171 {
172         int                     tz_minuteswest; /* Minutes west of GMT.  */
173         int                     tz_dsttime;             /* Nonzero if DST is ever in effect.  */
174 };
175
176 /* for setitimer in backend/port/win32/timer.c */
177 #define ITIMER_REAL 0
178 struct itimerval
179 {
180         struct timeval it_interval;
181         struct timeval it_value;
182 };
183 int                     setitimer(int which, const struct itimerval * value, struct itimerval * ovalue);
184
185
186 /*
187  * Supplement to <sys/types.h>.
188  *
189  * Perl already has typedefs for uid_t and gid_t.
190  */
191 #ifndef PLPERL_HAVE_UID_GID
192 typedef int uid_t;
193 typedef int gid_t;
194 #endif
195 typedef long key_t;
196
197 #ifdef WIN32_ONLY_COMPILER
198 typedef int pid_t;
199 #endif
200
201 /*
202  * Supplement to <sys/stat.h>.
203  */
204 #define lstat(path, sb) stat((path), (sb))
205
206 /*
207  * Supplement to <fcntl.h>.
208  * This is the same value as _O_NOINHERIT in the MS header file. This is
209  * to ensure that we don't collide with a future definition. It means
210  * we cannot use _O_NOINHERIT ourselves.
211  */
212 #define O_DSYNC 0x0080
213
214 /*
215  * Supplement to <errno.h>.
216  */
217 #undef EAGAIN
218 #undef EINTR
219 #define EINTR WSAEINTR
220 #define EAGAIN WSAEWOULDBLOCK
221 #define EMSGSIZE WSAEMSGSIZE
222 #define EAFNOSUPPORT WSAEAFNOSUPPORT
223 #define EWOULDBLOCK WSAEWOULDBLOCK
224 #define ECONNRESET WSAECONNRESET
225 #define EINPROGRESS WSAEINPROGRESS
226 #define ENOBUFS WSAENOBUFS
227 #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
228 #define ECONNREFUSED WSAECONNREFUSED
229 #define EBADFD WSAENOTSOCK
230 #define EOPNOTSUPP WSAEOPNOTSUPP
231
232
233 /* In backend/port/win32/signal.c */
234 extern PGDLLIMPORT volatile int pg_signal_queue;
235 extern PGDLLIMPORT int pg_signal_mask;
236 extern HANDLE pgwin32_signal_event;
237 extern HANDLE pgwin32_initial_signal_pipe;
238
239 #define UNBLOCKED_SIGNAL_QUEUE()        (pg_signal_queue & ~pg_signal_mask)
240
241
242 void            pgwin32_signal_initialize(void);
243 HANDLE          pgwin32_create_signal_listener(pid_t pid);
244 void            pgwin32_dispatch_queued_signals(void);
245 void            pg_queue_signal(int signum);
246
247 /* In backend/port/win32/socket.c */
248 #ifndef FRONTEND
249 #define socket(af, type, protocol) pgwin32_socket(af, type, protocol)
250 #define accept(s, addr, addrlen) pgwin32_accept(s, addr, addrlen)
251 #define connect(s, name, namelen) pgwin32_connect(s, name, namelen)
252 #define select(n, r, w, e, timeout) pgwin32_select(n, r, w, e, timeout)
253 #define recv(s, buf, len, flags) pgwin32_recv(s, buf, len, flags)
254 #define send(s, buf, len, flags) pgwin32_send(s, buf, len, flags)
255
256 SOCKET          pgwin32_socket(int af, int type, int protocol);
257 SOCKET          pgwin32_accept(SOCKET s, struct sockaddr * addr, int *addrlen);
258 int                     pgwin32_connect(SOCKET s, const struct sockaddr * name, int namelen);
259 int                     pgwin32_select(int nfds, fd_set *readfs, fd_set *writefds, fd_set *exceptfds, const struct timeval * timeout);
260 int                     pgwin32_recv(SOCKET s, char *buf, int len, int flags);
261 int                     pgwin32_send(SOCKET s, char *buf, int len, int flags);
262
263 const char *pgwin32_socket_strerror(int err);
264 int                     pgwin32_waitforsinglesocket(SOCKET s, int what, int timeout);
265
266 /* in backend/port/win32/security.c */
267 extern int      pgwin32_is_admin(void);
268 extern int      pgwin32_is_service(void);
269 #endif
270
271 /* in port/win32error.c */
272 extern void _dosmaperr(unsigned long);
273
274
275 /* Things that exist in MingW headers, but need to be added to MSVC */
276 #ifdef WIN32_ONLY_COMPILER
277 #ifndef __BORLANDC__
278 typedef long ssize_t;
279 typedef unsigned short mode_t;
280 #endif
281
282 /*
283  *      Certain "standard edition" versions of MSVC throw a warning
284  *      that later generates an error for "inline" statements, but
285  *      __inline seems to work.  e.g.  Microsoft Visual C++ .NET
286  *      Version 7.1.3088
287  */
288 #define inline __inline
289 #define __inline__ __inline
290
291 #ifndef __BORLANDC__
292 #define _S_IRWXU        (_S_IREAD | _S_IWRITE | _S_IEXEC)
293 #define _S_IXUSR        _S_IEXEC
294 #define _S_IWUSR        _S_IWRITE
295 #define _S_IRUSR        _S_IREAD
296 #define S_IRUSR         _S_IRUSR
297 #define S_IWUSR         _S_IWUSR
298 #define S_IXUSR         _S_IXUSR
299 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
300 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
301 #endif
302
303 #define F_OK 0
304 #define W_OK 2
305 #define R_OK 4
306
307 #define isinf(x) ((_fpclass(x) == _FPCLASS_PINF) || (_fpclass(x) == _FPCLASS_NINF))
308 #define isnan(x) _isnan(x)
309
310 /* Pulled from Makefile.port in mingw */
311 #define DLSUFFIX ".dll"
312
313 #endif