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