]> granicus.if.org Git - postgresql/blob - src/include/port/win32.h
Rearrange MSVC errcode hack, fix incorrect _MSC_VER test. Magnus
[postgresql] / src / include / port / win32.h
1 /* $PostgreSQL: pgsql/src/include/port/win32.h,v 1.61 2006/10/03 20:33:20 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 #include <sys/utime.h> /* for non-unicode version */
21 #undef near
22
23 /* Must be here to avoid conflicting with prototype in windows.h */
24 #define mkdir(a,b)      mkdir(a)
25
26 #define HAVE_FSYNC_WRITETHROUGH
27 #define HAVE_FSYNC_WRITETHROUGH_ONLY
28 #define ftruncate(a,b)  chsize(a,b)
29 /*
30  *      Even though we don't support 'fsync' as a wal_sync_method,
31  *      we do fsync() a few other places where _commit() is just fine.
32  */
33 #define fsync(fd) _commit(fd)
34
35 #define USES_WINSOCK
36
37 /* defines for dynamic linking on Win32 platform */
38 #if defined(WIN32) || defined(__CYGWIN__)
39
40 #if __GNUC__ && ! defined (__declspec)
41 #error You need egcs 1.1 or newer for compiling!
42 #endif
43
44 #ifdef BUILDING_DLL
45 #define DLLIMPORT __declspec (dllexport)
46 #else                                                   /* not BUILDING_DLL */
47 #define DLLIMPORT __declspec (dllimport)
48 #endif
49
50 #else                                                   /* not CYGWIN, not MSVC, not MingW */
51
52 #define DLLIMPORT
53 #endif
54
55
56 /*
57  *      IPC defines
58  */
59 #undef HAVE_UNION_SEMUN
60 #define HAVE_UNION_SEMUN 1
61
62 #define IPC_RMID 256
63 #define IPC_CREAT 512
64 #define IPC_EXCL 1024
65 #define IPC_PRIVATE 234564
66 #define IPC_NOWAIT      2048
67 #define IPC_STAT 4096
68
69 #define EACCESS 2048
70 #define EIDRM 4096
71
72 #define SETALL 8192
73 #define GETNCNT 16384
74 #define GETVAL 65536
75 #define SETVAL 131072
76 #define GETPID 262144
77
78 /*
79  *      Shared memory
80  */
81 struct shmid_ds
82 {
83         int                     dummy;
84         int                     shm_nattch;
85 };
86
87 int                     shmdt(const void *shmaddr);
88 void       *shmat(int memId, void *shmaddr, int flag);
89 int                     shmctl(int shmid, int flag, struct shmid_ds * dummy);
90 int                     shmget(int memKey, int size, int flag);
91
92
93 /*
94  *      Semaphores
95  */
96 union semun
97 {
98         int                     val;
99         struct semid_ds *buf;
100         unsigned short *array;
101 };
102
103 struct sembuf
104 {
105         int                     sem_flg;
106         int                     sem_op;
107         int                     sem_num;
108 };
109
110 int                     semctl(int semId, int semNum, int flag, union semun);
111 int                     semget(int semKey, int semNum, int flags);
112 int                     semop(int semId, struct sembuf * sops, int flag);
113
114
115 /*
116  *      Signal stuff
117  *      WIN32 doesn't have wait(), so the return value for children
118  *      is simply the return value specified by the child, without
119  *      any additional information on whether the child terminated
120  *      on its own or via a signal.  These macros are also used
121  *      to interpret the return value of system().
122  */
123 #define WEXITSTATUS(w)  (w)
124 #define WIFEXITED(w)    (true)
125 #define WIFSIGNALED(w)  (false)
126 #define WTERMSIG(w)             (0)
127
128 #define sigmask(sig) ( 1 << ((sig)-1) )
129
130 /* Signal function return values */
131 #undef SIG_DFL
132 #undef SIG_ERR
133 #undef SIG_IGN
134 #define SIG_DFL ((pqsigfunc)0)
135 #define SIG_ERR ((pqsigfunc)-1)
136 #define SIG_IGN ((pqsigfunc)1)
137
138 /* Some extra signals */
139 #define SIGHUP                          1
140 #define SIGQUIT                         3
141 #define SIGTRAP                         5
142 #define SIGABRT                         22      /* Set to match W32 value -- not UNIX value */
143 #define SIGKILL                         9
144 #define SIGPIPE                         13
145 #define SIGALRM                         14
146 #define SIGSTOP                         17
147 #define SIGTSTP                         18
148 #define SIGCONT                         19
149 #define SIGCHLD                         20
150 #define SIGTTIN                         21
151 #define SIGTTOU                         22      /* Same as SIGABRT -- no problem, I hope */
152 #define SIGWINCH                        28
153 #define SIGUSR1                         30
154 #define SIGUSR2                         31
155
156 struct timezone
157 {
158         int                     tz_minuteswest; /* Minutes west of GMT.  */
159         int                     tz_dsttime;             /* Nonzero if DST is ever in effect.  */
160 };
161
162 /* for setitimer in backend/port/win32/timer.c */
163 #define ITIMER_REAL 0
164 struct itimerval
165 {
166         struct timeval it_interval;
167         struct timeval it_value;
168 };
169 int                     setitimer(int which, const struct itimerval *value, struct itimerval *ovalue);
170
171
172 /*
173  * Supplement to <sys/types.h>.
174  *
175  * Perl already has typedefs for uid_t and gid_t.
176  */
177 #ifndef PLPERL_HAVE_UID_GID
178 typedef int uid_t;
179 typedef int gid_t;
180 #endif
181 typedef long key_t;
182
183 #ifdef WIN32_ONLY_COMPILER
184 typedef int pid_t;
185 #endif
186
187 /*
188  * Supplement to <sys/stat.h>.
189  */
190 #define lstat(path, sb) stat((path), (sb))
191
192 /*
193  * Supplement to <fcntl.h>.
194  * This is the same value as _O_NOINHERIT in the MS header file. This is
195  * to ensure that we don't collide with a future definition. It means
196  * we cannot use _O_NOINHERIT ourselves.
197  */
198 #define O_DSYNC 0x0080
199
200 /*
201  * Supplement to <errno.h>.
202  */
203 #undef EAGAIN
204 #undef EINTR
205 #define EINTR WSAEINTR
206 #define EAGAIN WSAEWOULDBLOCK
207 #define EMSGSIZE WSAEMSGSIZE
208 #define EAFNOSUPPORT WSAEAFNOSUPPORT
209 #define EWOULDBLOCK WSAEWOULDBLOCK
210 #define ECONNRESET WSAECONNRESET
211 #define EINPROGRESS WSAEINPROGRESS
212 #define ENOBUFS WSAENOBUFS
213 #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
214 #define ECONNREFUSED WSAECONNREFUSED
215 #define EBADFD WSAENOTSOCK
216 #define EOPNOTSUPP WSAEOPNOTSUPP
217
218
219 /* In backend/port/win32/signal.c */
220 extern DLLIMPORT volatile int pg_signal_queue;
221 extern DLLIMPORT int pg_signal_mask;
222 extern HANDLE pgwin32_signal_event;
223 extern HANDLE pgwin32_initial_signal_pipe;
224
225 #define UNBLOCKED_SIGNAL_QUEUE()        (pg_signal_queue & ~pg_signal_mask)
226
227
228 void            pgwin32_signal_initialize(void);
229 HANDLE          pgwin32_create_signal_listener(pid_t pid);
230 void            pgwin32_dispatch_queued_signals(void);
231 void            pg_queue_signal(int signum);
232
233 /* In backend/port/win32/socket.c */
234 #ifndef FRONTEND
235 #define socket(af, type, protocol) pgwin32_socket(af, type, protocol)
236 #define accept(s, addr, addrlen) pgwin32_accept(s, addr, addrlen)
237 #define connect(s, name, namelen) pgwin32_connect(s, name, namelen)
238 #define select(n, r, w, e, timeout) pgwin32_select(n, r, w, e, timeout)
239 #define recv(s, buf, len, flags) pgwin32_recv(s, buf, len, flags)
240 #define send(s, buf, len, flags) pgwin32_send(s, buf, len, flags)
241
242 SOCKET          pgwin32_socket(int af, int type, int protocol);
243 SOCKET          pgwin32_accept(SOCKET s, struct sockaddr * addr, int *addrlen);
244 int                     pgwin32_connect(SOCKET s, const struct sockaddr * name, int namelen);
245 int                     pgwin32_select(int nfds, fd_set *readfs, fd_set *writefds, fd_set *exceptfds, const struct timeval * timeout);
246 int                     pgwin32_recv(SOCKET s, char *buf, int len, int flags);
247 int                     pgwin32_send(SOCKET s, char *buf, int len, int flags);
248
249 const char *pgwin32_socket_strerror(int err);
250 int                     pgwin32_waitforsinglesocket(SOCKET s, int what);
251
252 /* in backend/port/win32/security.c */
253 extern int      pgwin32_is_admin(void);
254 extern int      pgwin32_is_service(void);
255 #endif
256
257 /* in port/win32error.c */
258 extern void _dosmaperr(unsigned long);
259
260
261 /* Things that exist in MingW headers, but need to be added to MSVC */
262 #ifdef WIN32_ONLY_COMPILER
263 typedef long ssize_t;
264 typedef unsigned short mode_t;
265
266 /*
267  *      Certain "standard edition" versions of MSVC throw a warning
268  *      that later generates an error for "inline" statements, but
269  *      __inline seems to work.  e.g.  Microsoft Visual C++ .NET
270  *      Version 7.1.3088
271  */
272 #define inline __inline
273 #define __inline__ __inline
274
275 #define _S_IRWXU        (_S_IREAD | _S_IWRITE | _S_IEXEC)
276 #define _S_IXUSR        _S_IEXEC
277 #define _S_IWUSR        _S_IWRITE
278 #define _S_IRUSR        _S_IREAD
279 #define S_IRUSR         _S_IRUSR
280 #define S_IWUSR         _S_IWUSR
281 #define S_IXUSR         _S_IXUSR
282 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
283 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
284
285 #define F_OK 0
286 #define W_OK 2
287 #define R_OK 4
288
289 #define isinf(x) ((_fpclass(x) == _FPCLASS_PINF) || (_fpclass(x) == _FPCLASS_NINF))
290 #define isnan(x) _isnan(x)
291 #define finite(x) _finite(x)
292
293
294 /* Pulled from Makefile.port in mingw */
295 #define DLSUFFIX ".dll"
296 #endif