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