]> granicus.if.org Git - postgresql/blob - src/include/port/win32.h
Now that core functionality is depending on autoconf's AC_C_BIGENDIAN to be
[postgresql] / src / include / port / win32.h
1 /* $PostgreSQL: pgsql/src/include/port/win32.h,v 1.74 2007/04/06 05:36:51 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 /*
81  *      Signal stuff
82  *
83  *      For WIN32, there is no wait() call so there are no wait() macros
84  *      to interpret the return value of system().  Instead, system()
85  *      return values < 0x100 are used for exit() termination, and higher
86  *      values are used to indicated non-exit() termination, which is
87  *      similar to a unix-style signal exit (think SIGSEGV ==
88  *      STATUS_ACCESS_VIOLATION).  Return values are broken up into groups:
89  *
90  *      http://msdn2.microsoft.com/en-gb/library/aa489609.aspx
91  *
92  *              NT_SUCCESS                      0 - 0x3FFFFFFF
93  *              NT_INFORMATION          0x40000000 - 0x7FFFFFFF
94  *              NT_WARNING                      0x80000000 - 0xBFFFFFFF
95  *              NT_ERROR                        0xC0000000 - 0xFFFFFFFF
96  *
97  *      Effectively, we don't care on the severity of the return value from
98  *      system(), we just need to know if it was because of exit() or generated
99  *      by the system, and it seems values >= 0x100 are system-generated.
100  *      See this URL for a list of WIN32 STATUS_* values:
101  *
102  *              Wine (URL used in our error messages) -
103  *                      http://source.winehq.org/source/include/ntstatus.h
104  *              Descriptions - http://www.comp.nus.edu.sg/~wuyongzh/my_doc/ntstatus.txt
105  *              MS SDK - http://www.nologs.com/ntstatus.html
106  *
107  *      It seems the exception lists are in both ntstatus.h and winnt.h, but
108  *      ntstatus.h has a more comprehensive list, and it only contains
109  *      exception values, rather than winnt, which contains lots of other
110  *      things:
111  *
112  *              http://www.microsoft.com/msj/0197/exception/exception.aspx
113  *
114  *              The ExceptionCode parameter is the number that the operating system
115  *              assigned to the exception. You can see a list of various exception codes
116  *              in WINNT.H by searching for #defines that start with "STATUS_". For
117  *              example, the code for the all-too-familiar STATUS_ACCESS_VIOLATION is
118  *              0xC0000005. A more complete set of exception codes can be found in
119  *              NTSTATUS.H from the Windows NT DDK.
120  *                                               
121  *      Some day we might want to print descriptions for the most common
122  *      exceptions, rather than printing an include file name.  We could use
123  *      RtlNtStatusToDosError() and pass to FormatMessage(), which can print
124  *      the text of error values, but MinGW does not support
125  *      RtlNtStatusToDosError().
126  */
127 #define WIFEXITED(w)    (((w) & 0XFFFFFF00) == 0)
128 #define WIFSIGNALED(w)  (!WIFEXITED(w))
129 #define WEXITSTATUS(w)  (w)
130 #define WTERMSIG(w)     (w)
131
132 #define sigmask(sig) ( 1 << ((sig)-1) )
133
134 /* Signal function return values */
135 #undef SIG_DFL
136 #undef SIG_ERR
137 #undef SIG_IGN
138 #define SIG_DFL ((pqsigfunc)0)
139 #define SIG_ERR ((pqsigfunc)-1)
140 #define SIG_IGN ((pqsigfunc)1)
141
142 /* Some extra signals */
143 #define SIGHUP                          1
144 #define SIGQUIT                         3
145 #define SIGTRAP                         5
146 #define SIGABRT                         22      /* Set to match W32 value -- not UNIX value */
147 #define SIGKILL                         9
148 #define SIGPIPE                         13
149 #define SIGALRM                         14
150 #define SIGSTOP                         17
151 #define SIGTSTP                         18
152 #define SIGCONT                         19
153 #define SIGCHLD                         20
154 #define SIGTTIN                         21
155 #define SIGTTOU                         22      /* Same as SIGABRT -- no problem, I hope */
156 #define SIGWINCH                        28
157 #ifndef __BORLANDC__
158 #define SIGUSR1                         30
159 #define SIGUSR2                         31
160 #endif
161
162 struct timezone
163 {
164         int                     tz_minuteswest; /* Minutes west of GMT.  */
165         int                     tz_dsttime;             /* Nonzero if DST is ever in effect.  */
166 };
167
168 /* for setitimer in backend/port/win32/timer.c */
169 #define ITIMER_REAL 0
170 struct itimerval
171 {
172         struct timeval it_interval;
173         struct timeval it_value;
174 };
175 int                     setitimer(int which, const struct itimerval * value, struct itimerval * ovalue);
176
177
178 /*
179  * Supplement to <sys/types.h>.
180  *
181  * Perl already has typedefs for uid_t and gid_t.
182  */
183 #ifndef PLPERL_HAVE_UID_GID
184 typedef int uid_t;
185 typedef int gid_t;
186 #endif
187 typedef long key_t;
188
189 #ifdef WIN32_ONLY_COMPILER
190 typedef int pid_t;
191 #endif
192
193 /*
194  * Supplement to <sys/stat.h>.
195  */
196 #define lstat(path, sb) stat((path), (sb))
197
198 /*
199  * Supplement to <fcntl.h>.
200  * This is the same value as _O_NOINHERIT in the MS header file. This is
201  * to ensure that we don't collide with a future definition. It means
202  * we cannot use _O_NOINHERIT ourselves.
203  */
204 #define O_DSYNC 0x0080
205
206 /*
207  * Supplement to <errno.h>.
208  */
209 #undef EAGAIN
210 #undef EINTR
211 #define EINTR WSAEINTR
212 #define EAGAIN WSAEWOULDBLOCK
213 #define EMSGSIZE WSAEMSGSIZE
214 #define EAFNOSUPPORT WSAEAFNOSUPPORT
215 #define EWOULDBLOCK WSAEWOULDBLOCK
216 #define ECONNRESET WSAECONNRESET
217 #define EINPROGRESS WSAEINPROGRESS
218 #define ENOBUFS WSAENOBUFS
219 #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
220 #define ECONNREFUSED WSAECONNREFUSED
221 #define EBADFD WSAENOTSOCK
222 #define EOPNOTSUPP WSAEOPNOTSUPP
223
224
225 /* In backend/port/win32/signal.c */
226 extern DLLIMPORT volatile int pg_signal_queue;
227 extern DLLIMPORT int pg_signal_mask;
228 extern HANDLE pgwin32_signal_event;
229 extern HANDLE pgwin32_initial_signal_pipe;
230
231 #define UNBLOCKED_SIGNAL_QUEUE()        (pg_signal_queue & ~pg_signal_mask)
232
233
234 void            pgwin32_signal_initialize(void);
235 HANDLE          pgwin32_create_signal_listener(pid_t pid);
236 void            pgwin32_dispatch_queued_signals(void);
237 void            pg_queue_signal(int signum);
238
239 /* In backend/port/win32/socket.c */
240 #ifndef FRONTEND
241 #define socket(af, type, protocol) pgwin32_socket(af, type, protocol)
242 #define accept(s, addr, addrlen) pgwin32_accept(s, addr, addrlen)
243 #define connect(s, name, namelen) pgwin32_connect(s, name, namelen)
244 #define select(n, r, w, e, timeout) pgwin32_select(n, r, w, e, timeout)
245 #define recv(s, buf, len, flags) pgwin32_recv(s, buf, len, flags)
246 #define send(s, buf, len, flags) pgwin32_send(s, buf, len, flags)
247
248 SOCKET          pgwin32_socket(int af, int type, int protocol);
249 SOCKET          pgwin32_accept(SOCKET s, struct sockaddr * addr, int *addrlen);
250 int                     pgwin32_connect(SOCKET s, const struct sockaddr * name, int namelen);
251 int                     pgwin32_select(int nfds, fd_set *readfs, fd_set *writefds, fd_set *exceptfds, const struct timeval * timeout);
252 int                     pgwin32_recv(SOCKET s, char *buf, int len, int flags);
253 int                     pgwin32_send(SOCKET s, char *buf, int len, int flags);
254
255 const char *pgwin32_socket_strerror(int err);
256 int                     pgwin32_waitforsinglesocket(SOCKET s, int what, int timeout);
257
258 /* in backend/port/win32/security.c */
259 extern int      pgwin32_is_admin(void);
260 extern int      pgwin32_is_service(void);
261 #endif
262
263 /* in port/win32error.c */
264 extern void _dosmaperr(unsigned long);
265
266
267 /* Things that exist in MingW headers, but need to be added to MSVC */
268 #ifdef WIN32_ONLY_COMPILER
269 #ifndef __BORLANDC__
270 typedef long ssize_t;
271 typedef unsigned short mode_t;
272 #endif
273
274 /*
275  *      Certain "standard edition" versions of MSVC throw a warning
276  *      that later generates an error for "inline" statements, but
277  *      __inline seems to work.  e.g.  Microsoft Visual C++ .NET
278  *      Version 7.1.3088
279  */
280 #define inline __inline
281 #define __inline__ __inline
282
283 #ifndef __BORLANDC__
284 #define _S_IRWXU        (_S_IREAD | _S_IWRITE | _S_IEXEC)
285 #define _S_IXUSR        _S_IEXEC
286 #define _S_IWUSR        _S_IWRITE
287 #define _S_IRUSR        _S_IREAD
288 #define S_IRUSR         _S_IRUSR
289 #define S_IWUSR         _S_IWUSR
290 #define S_IXUSR         _S_IXUSR
291 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
292 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
293 #endif
294
295 #define F_OK 0
296 #define W_OK 2
297 #define R_OK 4
298
299 #define isinf(x) ((_fpclass(x) == _FPCLASS_PINF) || (_fpclass(x) == _FPCLASS_NINF))
300 #define isnan(x) _isnan(x)
301
302 /* Pulled from Makefile.port in mingw */
303 #define DLSUFFIX ".dll"
304
305 #endif