Header And Logo

PostgreSQL
| The world's most advanced open source database.

win32.h

Go to the documentation of this file.
00001 /* src/include/port/win32.h */
00002 
00003 #if defined(_MSC_VER) || defined(__BORLANDC__)
00004 #define WIN32_ONLY_COMPILER
00005 #endif
00006 
00007 /*
00008  * Make sure _WIN32_WINNT has the minimum required value.
00009  * Leave a higher value in place.
00010 */
00011 #if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0501
00012 #undef _WIN32_WINNT
00013 #endif
00014 #ifndef _WIN32_WINNT
00015 #define _WIN32_WINNT 0x0501
00016 #endif
00017 /*
00018  * Always build with SSPI support. Keep it as a #define in case
00019  * we want a switch to disable it sometime in the future.
00020  */
00021 #ifndef __BORLANDC__
00022 #define ENABLE_SSPI 1
00023 #endif
00024 
00025 /* undefine and redefine after #include */
00026 #undef mkdir
00027 
00028 #undef ERROR
00029 
00030 /*
00031  * The Mingw64 headers choke if this is already defined - they
00032  * define it themselves.
00033  */
00034 #if !defined(__MINGW64_VERSION_MAJOR) || defined(WIN32_ONLY_COMPILER)
00035 #define _WINSOCKAPI_
00036 #endif
00037 #include <winsock2.h>
00038 #include <ws2tcpip.h>
00039 #include <windows.h>
00040 #undef small
00041 #include <process.h>
00042 #include <signal.h>
00043 #include <errno.h>
00044 #include <direct.h>
00045 #ifndef __BORLANDC__
00046 #include <sys/utime.h>          /* for non-unicode version */
00047 #endif
00048 #undef near
00049 
00050 /* Must be here to avoid conflicting with prototype in windows.h */
00051 #define mkdir(a,b)  mkdir(a)
00052 
00053 #define ftruncate(a,b)  chsize(a,b)
00054 
00055 /* Windows doesn't have fsync() as such, use _commit() */
00056 #define fsync(fd) _commit(fd)
00057 
00058 /*
00059  * For historical reasons, we allow setting wal_sync_method to
00060  * fsync_writethrough on Windows, even though it's really identical to fsync
00061  * (both code paths wind up at _commit()).
00062  */
00063 #define HAVE_FSYNC_WRITETHROUGH
00064 #define FSYNC_WRITETHROUGH_IS_FSYNC
00065 
00066 #define USES_WINSOCK
00067 
00068 /* defines for dynamic linking on Win32 platform
00069  *
00070  *  http://support.microsoft.com/kb/132044
00071  *  http://msdn.microsoft.com/en-us/library/8fskxacy(v=vs.80).aspx
00072  *  http://msdn.microsoft.com/en-us/library/a90k134d(v=vs.80).aspx
00073  */
00074  
00075 #if defined(WIN32) || defined(__CYGWIN__)
00076 
00077 #ifdef BUILDING_DLL
00078 #define PGDLLIMPORT __declspec (dllexport)
00079 #else                           /* not BUILDING_DLL */
00080 #define PGDLLIMPORT __declspec (dllimport)
00081 #endif
00082 
00083 #ifdef _MSC_VER
00084 #define PGDLLEXPORT __declspec (dllexport)
00085 #else
00086 #define PGDLLEXPORT
00087 #endif
00088 #else                           /* not CYGWIN, not MSVC, not MingW */
00089 #define PGDLLIMPORT
00090 #define PGDLLEXPORT
00091 #endif
00092 
00093 
00094 /*
00095  *  IPC defines
00096  */
00097 #undef HAVE_UNION_SEMUN
00098 #define HAVE_UNION_SEMUN 1
00099 
00100 #define IPC_RMID 256
00101 #define IPC_CREAT 512
00102 #define IPC_EXCL 1024
00103 #define IPC_PRIVATE 234564
00104 #define IPC_NOWAIT  2048
00105 #define IPC_STAT 4096
00106 
00107 #define EACCESS 2048
00108 #ifndef EIDRM
00109 #define EIDRM 4096
00110 #endif
00111 
00112 #define SETALL 8192
00113 #define GETNCNT 16384
00114 #define GETVAL 65536
00115 #define SETVAL 131072
00116 #define GETPID 262144
00117 
00118 
00119 /*
00120  *  Signal stuff
00121  *
00122  *  For WIN32, there is no wait() call so there are no wait() macros
00123  *  to interpret the return value of system().  Instead, system()
00124  *  return values < 0x100 are used for exit() termination, and higher
00125  *  values are used to indicated non-exit() termination, which is
00126  *  similar to a unix-style signal exit (think SIGSEGV ==
00127  *  STATUS_ACCESS_VIOLATION).  Return values are broken up into groups:
00128  *
00129  *  http://msdn2.microsoft.com/en-gb/library/aa489609.aspx
00130  *
00131  *      NT_SUCCESS          0 - 0x3FFFFFFF
00132  *      NT_INFORMATION      0x40000000 - 0x7FFFFFFF
00133  *      NT_WARNING          0x80000000 - 0xBFFFFFFF
00134  *      NT_ERROR            0xC0000000 - 0xFFFFFFFF
00135  *
00136  *  Effectively, we don't care on the severity of the return value from
00137  *  system(), we just need to know if it was because of exit() or generated
00138  *  by the system, and it seems values >= 0x100 are system-generated.
00139  *  See this URL for a list of WIN32 STATUS_* values:
00140  *
00141  *      Wine (URL used in our error messages) -
00142  *          http://source.winehq.org/source/include/ntstatus.h
00143  *      Descriptions - http://www.comp.nus.edu.sg/~wuyongzh/my_doc/ntstatus.txt
00144  *      MS SDK - http://www.nologs.com/ntstatus.html
00145  *
00146  *  It seems the exception lists are in both ntstatus.h and winnt.h, but
00147  *  ntstatus.h has a more comprehensive list, and it only contains
00148  *  exception values, rather than winnt, which contains lots of other
00149  *  things:
00150  *
00151  *      http://www.microsoft.com/msj/0197/exception/exception.aspx
00152  *
00153  *      The ExceptionCode parameter is the number that the operating system
00154  *      assigned to the exception. You can see a list of various exception codes
00155  *      in WINNT.H by searching for #defines that start with "STATUS_". For
00156  *      example, the code for the all-too-familiar STATUS_ACCESS_VIOLATION is
00157  *      0xC0000005. A more complete set of exception codes can be found in
00158  *      NTSTATUS.H from the Windows NT DDK.
00159  *
00160  *  Some day we might want to print descriptions for the most common
00161  *  exceptions, rather than printing an include file name.  We could use
00162  *  RtlNtStatusToDosError() and pass to FormatMessage(), which can print
00163  *  the text of error values, but MinGW does not support
00164  *  RtlNtStatusToDosError().
00165  */
00166 #define WIFEXITED(w)    (((w) & 0XFFFFFF00) == 0)
00167 #define WIFSIGNALED(w)  (!WIFEXITED(w))
00168 #define WEXITSTATUS(w)  (w)
00169 #define WTERMSIG(w)     (w)
00170 
00171 #define sigmask(sig) ( 1 << ((sig)-1) )
00172 
00173 /* Signal function return values */
00174 #undef SIG_DFL
00175 #undef SIG_ERR
00176 #undef SIG_IGN
00177 #define SIG_DFL ((pqsigfunc)0)
00178 #define SIG_ERR ((pqsigfunc)-1)
00179 #define SIG_IGN ((pqsigfunc)1)
00180 
00181 /* Some extra signals */
00182 #define SIGHUP              1
00183 #define SIGQUIT             3
00184 #define SIGTRAP             5
00185 #define SIGABRT             22  /* Set to match W32 value -- not UNIX value */
00186 #define SIGKILL             9
00187 #define SIGPIPE             13
00188 #define SIGALRM             14
00189 #define SIGSTOP             17
00190 #define SIGTSTP             18
00191 #define SIGCONT             19
00192 #define SIGCHLD             20
00193 #define SIGTTIN             21
00194 #define SIGTTOU             22  /* Same as SIGABRT -- no problem, I hope */
00195 #define SIGWINCH            28
00196 #ifndef __BORLANDC__
00197 #define SIGUSR1             30
00198 #define SIGUSR2             31
00199 #endif
00200 
00201 /*
00202  * New versions of mingw have gettimeofday() and also declare
00203  * struct timezone to support it.
00204  */
00205 #ifndef HAVE_GETTIMEOFDAY
00206 struct timezone
00207 {
00208     int         tz_minuteswest; /* Minutes west of GMT.  */
00209     int         tz_dsttime;     /* Nonzero if DST is ever in effect.  */
00210 };
00211 #endif
00212 
00213 /* for setitimer in backend/port/win32/timer.c */
00214 #define ITIMER_REAL 0
00215 struct itimerval
00216 {
00217     struct timeval it_interval;
00218     struct timeval it_value;
00219 };
00220 
00221 int         setitimer(int which, const struct itimerval * value, struct itimerval * ovalue);
00222 
00223 /*
00224  * WIN32 does not provide 64-bit off_t, but does provide the functions operating
00225  * with 64-bit offsets.
00226  */
00227 #define pgoff_t __int64
00228 #ifdef WIN32_ONLY_COMPILER
00229 #define fseeko(stream, offset, origin) _fseeki64(stream, offset, origin)
00230 #define ftello(stream) _ftelli64(stream)
00231 #else
00232 #ifndef fseeko
00233 #define fseeko(stream, offset, origin) fseeko64(stream, offset, origin)
00234 #endif
00235 #ifndef ftello
00236 #define ftello(stream) ftello64(stream)
00237 #endif
00238 #endif
00239 
00240 /*
00241  * Supplement to <sys/types.h>.
00242  *
00243  * Perl already has typedefs for uid_t and gid_t.
00244  */
00245 #ifndef PLPERL_HAVE_UID_GID
00246 typedef int uid_t;
00247 typedef int gid_t;
00248 #endif
00249 typedef long key_t;
00250 
00251 #ifdef WIN32_ONLY_COMPILER
00252 typedef int pid_t;
00253 #endif
00254 
00255 /*
00256  * Supplement to <sys/stat.h>.
00257  */
00258 #define lstat(path, sb) stat((path), (sb))
00259 
00260 /*
00261  * Supplement to <fcntl.h>.
00262  * This is the same value as _O_NOINHERIT in the MS header file. This is
00263  * to ensure that we don't collide with a future definition. It means
00264  * we cannot use _O_NOINHERIT ourselves.
00265  */
00266 #define O_DSYNC 0x0080
00267 
00268 /*
00269  * Supplement to <errno.h>.
00270  */
00271 #undef EAGAIN
00272 #undef EINTR
00273 #define EINTR WSAEINTR
00274 #define EAGAIN WSAEWOULDBLOCK
00275 #ifndef EMSGSIZE
00276 #define EMSGSIZE WSAEMSGSIZE
00277 #endif
00278 #ifndef EAFNOSUPPORT
00279 #define EAFNOSUPPORT WSAEAFNOSUPPORT
00280 #endif
00281 #ifndef EWOULDBLOCK
00282 #define EWOULDBLOCK WSAEWOULDBLOCK
00283 #endif
00284 #ifndef ECONNRESET
00285 #define ECONNRESET WSAECONNRESET
00286 #endif
00287 #ifndef EINPROGRESS
00288 #define EINPROGRESS WSAEINPROGRESS
00289 #endif
00290 #ifndef ENOBUFS
00291 #define ENOBUFS WSAENOBUFS
00292 #endif
00293 #ifndef EPROTONOSUPPORT
00294 #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
00295 #endif
00296 #ifndef ECONNREFUSED
00297 #define ECONNREFUSED WSAECONNREFUSED
00298 #endif
00299 #ifndef EBADFD
00300 #define EBADFD WSAENOTSOCK
00301 #endif
00302 #ifndef EOPNOTSUPP
00303 #define EOPNOTSUPP WSAEOPNOTSUPP
00304 #endif
00305 
00306 /*
00307  * For Microsoft Visual Studio 2010 and above we intentionally redefine
00308  * the regular Berkeley error constants and set them to the WSA constants.
00309  * Note that this will break if those constants are used for anything else
00310  * than Windows Sockets errors.
00311  */
00312 #if _MSC_VER >= 1600
00313 #pragma warning(disable:4005)
00314 #define EMSGSIZE WSAEMSGSIZE
00315 #define EAFNOSUPPORT WSAEAFNOSUPPORT
00316 #define EWOULDBLOCK WSAEWOULDBLOCK
00317 #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
00318 #define ECONNRESET WSAECONNRESET
00319 #define EINPROGRESS WSAEINPROGRESS
00320 #define ENOBUFS WSAENOBUFS
00321 #define ECONNREFUSED WSAECONNREFUSED
00322 #define EOPNOTSUPP WSAEOPNOTSUPP
00323 #pragma warning(default:4005)
00324 #endif
00325 
00326 /*
00327  * Extended locale functions with gratuitous underscore prefixes.
00328  * (These APIs are nevertheless fully documented by Microsoft.)
00329  */
00330 #define locale_t _locale_t
00331 #define tolower_l _tolower_l
00332 #define toupper_l _toupper_l
00333 #define towlower_l _towlower_l
00334 #define towupper_l _towupper_l
00335 #define isdigit_l _isdigit_l
00336 #define iswdigit_l _iswdigit_l
00337 #define isalpha_l _isalpha_l
00338 #define iswalpha_l _iswalpha_l
00339 #define isalnum_l _isalnum_l
00340 #define iswalnum_l _iswalnum_l
00341 #define isupper_l _isupper_l
00342 #define iswupper_l _iswupper_l
00343 #define islower_l _islower_l
00344 #define iswlower_l _iswlower_l
00345 #define isgraph_l _isgraph_l
00346 #define iswgraph_l _iswgraph_l
00347 #define isprint_l _isprint_l
00348 #define iswprint_l _iswprint_l
00349 #define ispunct_l _ispunct_l
00350 #define iswpunct_l _iswpunct_l
00351 #define isspace_l _isspace_l
00352 #define iswspace_l _iswspace_l
00353 #define strcoll_l _strcoll_l
00354 #define wcscoll_l _wcscoll_l
00355 #define wcstombs_l _wcstombs_l
00356 #define mbstowcs_l _mbstowcs_l
00357 
00358 
00359 /* In backend/port/win32/signal.c */
00360 extern PGDLLIMPORT volatile int pg_signal_queue;
00361 extern PGDLLIMPORT int pg_signal_mask;
00362 extern HANDLE pgwin32_signal_event;
00363 extern HANDLE pgwin32_initial_signal_pipe;
00364 
00365 #define UNBLOCKED_SIGNAL_QUEUE()    (pg_signal_queue & ~pg_signal_mask)
00366 
00367 
00368 void        pgwin32_signal_initialize(void);
00369 HANDLE      pgwin32_create_signal_listener(pid_t pid);
00370 void        pgwin32_dispatch_queued_signals(void);
00371 void        pg_queue_signal(int signum);
00372 
00373 /* In backend/port/win32/socket.c */
00374 #ifndef FRONTEND
00375 #define socket(af, type, protocol) pgwin32_socket(af, type, protocol)
00376 #define accept(s, addr, addrlen) pgwin32_accept(s, addr, addrlen)
00377 #define connect(s, name, namelen) pgwin32_connect(s, name, namelen)
00378 #define select(n, r, w, e, timeout) pgwin32_select(n, r, w, e, timeout)
00379 #define recv(s, buf, len, flags) pgwin32_recv(s, buf, len, flags)
00380 #define send(s, buf, len, flags) pgwin32_send(s, buf, len, flags)
00381 
00382 SOCKET      pgwin32_socket(int af, int type, int protocol);
00383 SOCKET      pgwin32_accept(SOCKET s, struct sockaddr * addr, int *addrlen);
00384 int         pgwin32_connect(SOCKET s, const struct sockaddr * name, int namelen);
00385 int         pgwin32_select(int nfds, fd_set *readfs, fd_set *writefds, fd_set *exceptfds, const struct timeval * timeout);
00386 int         pgwin32_recv(SOCKET s, char *buf, int len, int flags);
00387 int         pgwin32_send(SOCKET s, const void *buf, int len, int flags);
00388 
00389 const char *pgwin32_socket_strerror(int err);
00390 int         pgwin32_waitforsinglesocket(SOCKET s, int what, int timeout);
00391 
00392 extern int  pgwin32_noblock;
00393 
00394 /* in backend/port/win32/security.c */
00395 extern int  pgwin32_is_admin(void);
00396 extern int  pgwin32_is_service(void);
00397 #endif
00398 
00399 /* in backend/port/win32_shmem.c */
00400 extern int  pgwin32_ReserveSharedMemoryRegion(HANDLE);
00401 
00402 /* in backend/port/win32/crashdump.c */
00403 extern void pgwin32_install_crashdump_handler(void);
00404 
00405 /* in port/win32error.c */
00406 extern void _dosmaperr(unsigned long);
00407 
00408 /* in port/win32env.c */
00409 extern int  pgwin32_putenv(const char *);
00410 extern void pgwin32_unsetenv(const char *);
00411 
00412 #define putenv(x) pgwin32_putenv(x)
00413 #define unsetenv(x) pgwin32_unsetenv(x)
00414 
00415 /* Things that exist in MingW headers, but need to be added to MSVC & BCC */
00416 #ifdef WIN32_ONLY_COMPILER
00417 
00418 #ifndef _WIN64
00419 typedef long ssize_t;
00420 #else
00421 typedef __int64 ssize_t;
00422 #endif
00423 
00424 #ifndef __BORLANDC__
00425 typedef unsigned short mode_t;
00426 
00427 #define S_IRUSR _S_IREAD
00428 #define S_IWUSR _S_IWRITE
00429 #define S_IXUSR _S_IEXEC
00430 #define S_IRWXU (S_IRUSR | S_IWUSR | S_IXUSR)
00431 /* see also S_IRGRP etc below */
00432 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
00433 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
00434 #endif   /* __BORLANDC__ */
00435 
00436 #define F_OK 0
00437 #define W_OK 2
00438 #define R_OK 4
00439 
00440 #define isinf(x) ((_fpclass(x) == _FPCLASS_PINF) || (_fpclass(x) == _FPCLASS_NINF))
00441 #define isnan(x) _isnan(x)
00442 
00443 /* Pulled from Makefile.port in mingw */
00444 #define DLSUFFIX ".dll"
00445 
00446 #ifdef __BORLANDC__
00447 
00448 /* for port/dirent.c */
00449 #ifndef INVALID_FILE_ATTRIBUTES
00450 #define INVALID_FILE_ATTRIBUTES ((DWORD) -1)
00451 #endif
00452 
00453 /* for port/open.c */
00454 #ifndef O_RANDOM
00455 #define O_RANDOM        0x0010  /* File access is primarily random */
00456 #define O_SEQUENTIAL    0x0020  /* File access is primarily sequential */
00457 #define O_TEMPORARY     0x0040  /* Temporary file bit */
00458 #define O_SHORT_LIVED   0x1000  /* Temporary storage file, try not to flush */
00459 #define _O_SHORT_LIVED  O_SHORT_LIVED
00460 #endif   /* ifndef O_RANDOM */
00461 #endif   /* __BORLANDC__ */
00462 #endif   /* WIN32_ONLY_COMPILER */
00463 
00464 /* These aren't provided by either MingW or MSVC */
00465 #ifndef __BORLANDC__
00466 #define S_IRGRP 0
00467 #define S_IWGRP 0
00468 #define S_IXGRP 0
00469 #define S_IRWXG 0
00470 #define S_IROTH 0
00471 #define S_IWOTH 0
00472 #define S_IXOTH 0
00473 #define S_IRWXO 0
00474 
00475 #endif   /* __BORLANDC__ */