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