Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
win32_port.h595 linesDownload Raw Back to port
1/*-------------------------------------------------------------------------2 *3 * win32_port.h4 *	  Windows-specific compatibility stuff.5 *6 * Note this is read in MinGW as well as native Windows builds,7 * but not in Cygwin builds.8 *9 * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group10 * Portions Copyright (c) 1994, Regents of the University of California11 *12 * src/include/port/win32_port.h13 *14 *-------------------------------------------------------------------------15 */16#ifndef PG_WIN32_PORT_H17#define PG_WIN32_PORT_H18 19/*20 * Always build with SSPI support. Keep it as a #define in case21 * we want a switch to disable it sometime in the future.22 */23#define ENABLE_SSPI 124 25/* undefine and redefine after #include */26#undef mkdir27 28#undef ERROR29 30/*31 * VS2013 and later issue warnings about using the old Winsock API,32 * which we don't really want to hear about.33 */34#ifdef _MSC_VER35#define _WINSOCK_DEPRECATED_NO_WARNINGS36#endif37 38/*39 * The MinGW64 headers choke if this is already defined - they40 * define it themselves.41 */42#if !defined(__MINGW64_VERSION_MAJOR) || defined(_MSC_VER)43#define _WINSOCKAPI_44#endif45 46/*47 * windows.h includes a lot of other headers, slowing down compilation48 * significantly.  WIN32_LEAN_AND_MEAN reduces that a bit. It'd be better to49 * remove the include of windows.h (as well as indirect inclusions of it) from50 * such a central place, but until then...51 *52 * To be able to include ntstatus.h tell windows.h to not declare NTSTATUS by53 * temporarily defining UMDF_USING_NTSTATUS, otherwise we'll get warning about54 * macro redefinitions, as windows.h also defines NTSTATUS (yuck). That in55 * turn requires including ntstatus.h, winternl.h to get common symbols.56 */57#define WIN32_LEAN_AND_MEAN58#define UMDF_USING_NTSTATUS59 60#include <winsock2.h>61#include <ws2tcpip.h>62#include <windows.h>63#include <ntstatus.h>64#include <winternl.h>65 66#undef small67#include <process.h>68#include <signal.h>69#include <direct.h>70#undef near71 72/* needed before sys/stat hacking below: */73#define fstat microsoft_native_fstat74#define stat microsoft_native_stat75#include <sys/stat.h>76#undef fstat77#undef stat78 79/* Must be here to avoid conflicting with prototype in windows.h */80#define mkdir(a,b)	mkdir(a)81 82#define ftruncate(a,b)	chsize(a,b)83 84/* Windows doesn't have fsync() as such, use _commit() */85#define fsync(fd) _commit(fd)86 87/*88 * For historical reasons, we allow setting wal_sync_method to89 * fsync_writethrough on Windows, even though it's really identical to fsync90 * (both code paths wind up at _commit()).91 */92#define HAVE_FSYNC_WRITETHROUGH93#define FSYNC_WRITETHROUGH_IS_FSYNC94 95#define USES_WINSOCK96 97/*98 *	IPC defines99 */100#undef HAVE_UNION_SEMUN101#define HAVE_UNION_SEMUN 1102 103#define IPC_RMID 256104#define IPC_CREAT 512105#define IPC_EXCL 1024106#define IPC_PRIVATE 234564107#define IPC_NOWAIT	2048108#define IPC_STAT 4096109 110#define EACCESS 2048111#ifndef EIDRM112#define EIDRM 4096113#endif114 115#define SETALL 8192116#define GETNCNT 16384117#define GETVAL 65536118#define SETVAL 131072119#define GETPID 262144120 121 122/*123 *	Signal stuff124 *125 *	For WIN32, there is no wait() call so there are no wait() macros126 *	to interpret the return value of system().  Instead, system()127 *	return values < 0x100 are used for exit() termination, and higher128 *	values are used to indicate non-exit() termination, which is129 *	similar to a unix-style signal exit (think SIGSEGV ==130 *	STATUS_ACCESS_VIOLATION).  Return values are broken up into groups:131 *132 *	https://docs.microsoft.com/en-us/windows-hardware/drivers/kernel/using-ntstatus-values133 *134 *		NT_SUCCESS			0 - 0x3FFFFFFF135 *		NT_INFORMATION		0x40000000 - 0x7FFFFFFF136 *		NT_WARNING			0x80000000 - 0xBFFFFFFF137 *		NT_ERROR			0xC0000000 - 0xFFFFFFFF138 *139 *	Effectively, we don't care on the severity of the return value from140 *	system(), we just need to know if it was because of exit() or generated141 *	by the system, and it seems values >= 0x100 are system-generated.142 *	See this URL for a list of WIN32 STATUS_* values:143 *144 *		Wine (URL used in our error messages) -145 *			http://source.winehq.org/source/include/ntstatus.h146 *		Descriptions -147 *			https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-erref/596a1078-e883-4972-9bbc-49e60bebca55148 *149 *	The comprehensive exception list is included in ntstatus.h from the150 *	Windows Driver Kit (WDK).  A subset of the list is also included in151 *	winnt.h from the Windows SDK.  Defining WIN32_NO_STATUS before including152 *	windows.h helps to avoid any conflicts.153 *154 *	Some day we might want to print descriptions for the most common155 *	exceptions, rather than printing an include file name.  We could use156 *	RtlNtStatusToDosError() and pass to FormatMessage(), which can print157 *	the text of error values, but MinGW does not support158 *	RtlNtStatusToDosError().159 */160#define WIFEXITED(w)	(((w) & 0XFFFFFF00) == 0)161#define WIFSIGNALED(w)	(!WIFEXITED(w))162#define WEXITSTATUS(w)	(w)163#define WTERMSIG(w)		(w)164 165#define sigmask(sig) ( 1 << ((sig)-1) )166 167/* Signal function return values */168#undef SIG_DFL169#undef SIG_ERR170#undef SIG_IGN171#define SIG_DFL ((pqsigfunc)0)172#define SIG_ERR ((pqsigfunc)-1)173#define SIG_IGN ((pqsigfunc)1)174 175/* Some extra signals */176#define SIGHUP				1177#define SIGQUIT				3178#define SIGTRAP				5179#define SIGABRT				22	/* Set to match W32 value -- not UNIX value */180#define SIGKILL				9181#define SIGPIPE				13182#define SIGALRM				14183#define SIGSTOP				17184#define SIGTSTP				18185#define SIGCONT				19186#define SIGCHLD				20187#define SIGWINCH			28188#define SIGUSR1				30189#define SIGUSR2				31190 191/* MinGW has gettimeofday(), but MSVC doesn't */192#ifdef _MSC_VER193/* Last parameter not used */194extern int	gettimeofday(struct timeval *tp, void *tzp);195#endif196 197/* for setitimer in backend/port/win32/timer.c */198#define ITIMER_REAL 0199struct itimerval200{201	struct timeval it_interval;202	struct timeval it_value;203};204 205int			setitimer(int which, const struct itimerval *value, struct itimerval *ovalue);206 207/* Convenience wrapper for GetFileType() */208extern DWORD pgwin32_get_file_type(HANDLE hFile);209 210/*211 * WIN32 does not provide 64-bit off_t, but does provide the functions operating212 * with 64-bit offsets.  Also, fseek() might not give an error for unseekable213 * streams, so harden that function with our version.214 */215#define pgoff_t __int64216 217#ifdef _MSC_VER218extern int	_pgfseeko64(FILE *stream, pgoff_t offset, int origin);219extern pgoff_t _pgftello64(FILE *stream);220#define fseeko(stream, offset, origin) _pgfseeko64(stream, offset, origin)221#define ftello(stream) _pgftello64(stream)222#else223#ifndef fseeko224#define fseeko(stream, offset, origin) fseeko64(stream, offset, origin)225#endif226#ifndef ftello227#define ftello(stream) ftello64(stream)228#endif229#endif230 231/*232 *	Win32 also doesn't have symlinks, but we can emulate them with233 *	junction points on newer Win32 versions.234 *235 *	Cygwin has its own symlinks which work on Win95/98/ME where236 *	junction points don't, so use those instead.  We have no way of237 *	knowing what type of system Cygwin binaries will be run on.238 *		Note: Some CYGWIN includes might #define WIN32.239 */240extern int	pgsymlink(const char *oldpath, const char *newpath);241extern int	pgreadlink(const char *path, char *buf, size_t size);242 243#define symlink(oldpath, newpath)	pgsymlink(oldpath, newpath)244#define readlink(path, buf, size)	pgreadlink(path, buf, size)245 246/*247 * Supplement to <sys/types.h>.248 *249 * Perl already has typedefs for uid_t and gid_t.250 */251#ifndef PLPERL_HAVE_UID_GID252typedef int uid_t;253typedef int gid_t;254#endif255typedef long key_t;256 257#ifdef _MSC_VER258typedef int pid_t;259#endif260 261/*262 * Supplement to <sys/stat.h>.263 *264 * We must pull in sys/stat.h before this part, else our overrides lose.265 *266 * stat() is not guaranteed to set the st_size field on win32, so we267 * redefine it to our own implementation.  See src/port/win32stat.c.268 *269 * The struct stat is 32 bit in MSVC, so we redefine it as a copy of270 * struct __stat64.  This also fixes the struct size for MINGW builds.271 */272struct stat						/* This should match struct __stat64 */273{274	_dev_t		st_dev;275	_ino_t		st_ino;276	unsigned short st_mode;277	short		st_nlink;278	short		st_uid;279	short		st_gid;280	_dev_t		st_rdev;281	__int64		st_size;282	__time64_t	st_atime;283	__time64_t	st_mtime;284	__time64_t	st_ctime;285};286 287extern int	_pgfstat64(int fileno, struct stat *buf);288extern int	_pgstat64(const char *name, struct stat *buf);289extern int	_pglstat64(const char *name, struct stat *buf);290 291#define fstat(fileno, sb)	_pgfstat64(fileno, sb)292#define stat(path, sb)		_pgstat64(path, sb)293#define lstat(path, sb)		_pglstat64(path, sb)294 295/* These macros are not provided by older MinGW, nor by MSVC */296#ifndef S_IRUSR297#define S_IRUSR _S_IREAD298#endif299#ifndef S_IWUSR300#define S_IWUSR _S_IWRITE301#endif302#ifndef S_IXUSR303#define S_IXUSR _S_IEXEC304#endif305#ifndef S_IRWXU306#define S_IRWXU (S_IRUSR | S_IWUSR | S_IXUSR)307#endif308#ifndef S_IRGRP309#define S_IRGRP 0310#endif311#ifndef S_IWGRP312#define S_IWGRP 0313#endif314#ifndef S_IXGRP315#define S_IXGRP 0316#endif317#ifndef S_IRWXG318#define S_IRWXG 0319#endif320#ifndef S_IROTH321#define S_IROTH 0322#endif323#ifndef S_IWOTH324#define S_IWOTH 0325#endif326#ifndef S_IXOTH327#define S_IXOTH 0328#endif329#ifndef S_IRWXO330#define S_IRWXO 0331#endif332#ifndef S_ISDIR333#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)334#endif335#ifndef S_ISREG336#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)337#endif338 339/*340 * In order for lstat() to be able to report junction points as symlinks, we341 * need to hijack a bit in st_mode, since neither MSVC nor MinGW provides342 * S_ISLNK and there aren't any spare bits.  We'll steal the one for character343 * devices, because we don't otherwise make use of those.344 */345#ifdef S_ISLNK346#error "S_ISLNK is already defined"347#endif348#ifdef S_IFLNK349#error "S_IFLNK is already defined"350#endif351#define S_IFLNK S_IFCHR352#define S_ISLNK(m) (((m) & S_IFLNK) == S_IFLNK)353 354/*355 * Supplement to <fcntl.h>.356 * This is the same value as _O_NOINHERIT in the MS header file. This is357 * to ensure that we don't collide with a future definition. It means358 * we cannot use _O_NOINHERIT ourselves.359 */360#define O_DSYNC 0x0080361 362/*363 * Our open() replacement does not create inheritable handles, so it is safe to364 * ignore O_CLOEXEC.  (If we were using Windows' own open(), it might be365 * necessary to convert this to _O_NOINHERIT.)366 */367#define O_CLOEXEC 0368 369/*370 * Supplement to <errno.h>.371 *372 * We redefine network-related Berkeley error symbols as the corresponding WSA373 * constants. This allows strerror.c to recognize them as being in the Winsock374 * error code range and pass them off to win32_socket_strerror(), since375 * Windows' version of plain strerror() won't cope.  Note that this will break376 * if these names are used for anything else besides Windows Sockets errors.377 * See TranslateSocketError() when changing this list.378 */379#undef EAGAIN380#define EAGAIN WSAEWOULDBLOCK381#undef EINTR382#define EINTR WSAEINTR383#undef EMSGSIZE384#define EMSGSIZE WSAEMSGSIZE385#undef EAFNOSUPPORT386#define EAFNOSUPPORT WSAEAFNOSUPPORT387#undef EWOULDBLOCK388#define EWOULDBLOCK WSAEWOULDBLOCK389#undef ECONNABORTED390#define ECONNABORTED WSAECONNABORTED391#undef ECONNRESET392#define ECONNRESET WSAECONNRESET393#undef EINPROGRESS394#define EINPROGRESS WSAEINPROGRESS395#undef EISCONN396#define EISCONN WSAEISCONN397#undef ENOBUFS398#define ENOBUFS WSAENOBUFS399#undef EPROTONOSUPPORT400#define EPROTONOSUPPORT WSAEPROTONOSUPPORT401#undef ECONNREFUSED402#define ECONNREFUSED WSAECONNREFUSED403#undef ENOTSOCK404#define ENOTSOCK WSAENOTSOCK405#undef EOPNOTSUPP406#define EOPNOTSUPP WSAEOPNOTSUPP407#undef EADDRINUSE408#define EADDRINUSE WSAEADDRINUSE409#undef EADDRNOTAVAIL410#define EADDRNOTAVAIL WSAEADDRNOTAVAIL411#undef EHOSTDOWN412#define EHOSTDOWN WSAEHOSTDOWN413#undef EHOSTUNREACH414#define EHOSTUNREACH WSAEHOSTUNREACH415#undef ENETDOWN416#define ENETDOWN WSAENETDOWN417#undef ENETRESET418#define ENETRESET WSAENETRESET419#undef ENETUNREACH420#define ENETUNREACH WSAENETUNREACH421#undef ENOTCONN422#define ENOTCONN WSAENOTCONN423#undef ETIMEDOUT424#define ETIMEDOUT WSAETIMEDOUT425 426/*427 * Locale stuff.428 *429 * Extended locale functions with gratuitous underscore prefixes.430 * (These APIs are nevertheless fully documented by Microsoft.)431 */432#define locale_t _locale_t433#define tolower_l _tolower_l434#define toupper_l _toupper_l435#define towlower_l _towlower_l436#define towupper_l _towupper_l437#define isdigit_l _isdigit_l438#define iswdigit_l _iswdigit_l439#define isalpha_l _isalpha_l440#define iswalpha_l _iswalpha_l441#define isalnum_l _isalnum_l442#define iswalnum_l _iswalnum_l443#define isupper_l _isupper_l444#define iswupper_l _iswupper_l445#define islower_l _islower_l446#define iswlower_l _iswlower_l447#define isgraph_l _isgraph_l448#define iswgraph_l _iswgraph_l449#define isprint_l _isprint_l450#define iswprint_l _iswprint_l451#define ispunct_l _ispunct_l452#define iswpunct_l _iswpunct_l453#define isspace_l _isspace_l454#define iswspace_l _iswspace_l455#define strcoll_l _strcoll_l456#define strxfrm_l _strxfrm_l457#define wcscoll_l _wcscoll_l458#define wcstombs_l _wcstombs_l459#define mbstowcs_l _mbstowcs_l460 461/*462 * Versions of libintl >= 0.18? try to replace setlocale() with a macro463 * to their own versions.  Remove the macro, if it exists, because it464 * ends up calling the wrong version when the backend and libintl use465 * different versions of msvcrt.466 */467#if defined(setlocale)468#undef setlocale469#endif470 471/*472 * Define our own wrapper macro around setlocale() to work around bugs in473 * Windows' native setlocale() function.474 */475extern char *pgwin32_setlocale(int category, const char *locale);476 477#define setlocale(a,b) pgwin32_setlocale(a,b)478 479 480/* In backend/port/win32/signal.c */481extern PGDLLIMPORT volatile int pg_signal_queue;482extern PGDLLIMPORT int pg_signal_mask;483extern PGDLLIMPORT HANDLE pgwin32_signal_event;484extern PGDLLIMPORT HANDLE pgwin32_initial_signal_pipe;485 486#define UNBLOCKED_SIGNAL_QUEUE()	(pg_signal_queue & ~pg_signal_mask)487#define PG_SIGNAL_COUNT 32488 489extern void pgwin32_signal_initialize(void);490extern HANDLE pgwin32_create_signal_listener(pid_t pid);491extern void pgwin32_dispatch_queued_signals(void);492extern void pg_queue_signal(int signum);493 494/* In src/port/kill.c */495#define kill(pid,sig)	pgkill(pid,sig)496extern int	pgkill(int pid, int sig);497 498/* In backend/port/win32/socket.c */499#ifndef FRONTEND500#define socket(af, type, protocol) pgwin32_socket(af, type, protocol)501#define bind(s, addr, addrlen) pgwin32_bind(s, addr, addrlen)502#define listen(s, backlog) pgwin32_listen(s, backlog)503#define accept(s, addr, addrlen) pgwin32_accept(s, addr, addrlen)504#define connect(s, name, namelen) pgwin32_connect(s, name, namelen)505#define select(n, r, w, e, timeout) pgwin32_select(n, r, w, e, timeout)506#define recv(s, buf, len, flags) pgwin32_recv(s, buf, len, flags)507#define send(s, buf, len, flags) pgwin32_send(s, buf, len, flags)508 509extern SOCKET pgwin32_socket(int af, int type, int protocol);510extern int	pgwin32_bind(SOCKET s, struct sockaddr *addr, int addrlen);511extern int	pgwin32_listen(SOCKET s, int backlog);512extern SOCKET pgwin32_accept(SOCKET s, struct sockaddr *addr, int *addrlen);513extern int	pgwin32_connect(SOCKET s, const struct sockaddr *name, int namelen);514extern int	pgwin32_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, const struct timeval *timeout);515extern int	pgwin32_recv(SOCKET s, char *buf, int len, int flags);516extern int	pgwin32_send(SOCKET s, const void *buf, int len, int flags);517extern int	pgwin32_waitforsinglesocket(SOCKET s, int what, int timeout);518 519extern PGDLLIMPORT int pgwin32_noblock;520 521#endif							/* FRONTEND */522 523/* in backend/port/win32_shmem.c */524extern int	pgwin32_ReserveSharedMemoryRegion(HANDLE);525 526/* in backend/port/win32/crashdump.c */527extern void pgwin32_install_crashdump_handler(void);528 529/* in port/win32dlopen.c */530extern void *dlopen(const char *file, int mode);531extern void *dlsym(void *handle, const char *symbol);532extern int	dlclose(void *handle);533extern char *dlerror(void);534 535#define RTLD_NOW 1536#define RTLD_GLOBAL 0537 538/* in port/win32error.c */539extern void _dosmaperr(unsigned long);540 541/* in port/win32env.c */542extern int	pgwin32_putenv(const char *);543extern int	pgwin32_setenv(const char *name, const char *value, int overwrite);544extern int	pgwin32_unsetenv(const char *name);545 546#define putenv(x) pgwin32_putenv(x)547#define setenv(x,y,z) pgwin32_setenv(x,y,z)548#define unsetenv(x) pgwin32_unsetenv(x)549 550/* in port/win32security.c */551extern int	pgwin32_is_service(void);552extern int	pgwin32_is_admin(void);553 554/* Windows security token manipulation (in src/common/exec.c) */555extern BOOL AddUserToTokenDacl(HANDLE hToken);556 557/* Things that exist in MinGW headers, but need to be added to MSVC */558#ifdef _MSC_VER559 560#ifndef _WIN64561typedef long ssize_t;562#else563typedef __int64 ssize_t;564#endif565 566typedef unsigned short mode_t;567 568#define F_OK 0569#define W_OK 2570#define R_OK 4571 572#endif							/* _MSC_VER */573 574#if defined(__MINGW32__) || defined(__MINGW64__)575/*576 * Mingw claims to have a strtof, and my reading of its source code suggests577 * that it ought to work (and not need this hack), but the regression test578 * results disagree with me; whether this is a version issue or not is not579 * clear. However, using our wrapper (and the misrounded-input variant file,580 * already required for supporting ancient systems) can't make things any581 * worse, except for a tiny performance loss when reading zeros.582 *583 * See also cygwin.h for another instance of this.584 */585#define HAVE_BUGGY_STRTOF 1586#endif587 588/* in port/win32pread.c */589extern ssize_t pg_pread(int fd, void *buf, size_t nbyte, off_t offset);590 591/* in port/win32pwrite.c */592extern ssize_t pg_pwrite(int fd, const void *buf, size_t nbyte, off_t offset);593 594#endif							/* PG_WIN32_PORT_H */595 
codekingpro/portable-devtools · Team Ai