PostgreSQL Source Code  git master
win32_port.h File Reference
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
#include <ntstatus.h>
#include <winternl.h>
#include <process.h>
#include <signal.h>
#include <direct.h>
#include <sys/stat.h>
Include dependency graph for win32_port.h:

Go to the source code of this file.

Data Structures

struct  itimerval
 
struct  stat
 

Macros

#define ENABLE_SSPI   1
 
#define _WINSOCKAPI_
 
#define WIN32_LEAN_AND_MEAN
 
#define UMDF_USING_NTSTATUS
 
#define fstat   microsoft_native_fstat
 
#define stat   microsoft_native_stat
 
#define mkdir(a, b)   mkdir(a)
 
#define ftruncate(a, b)   chsize(a,b)
 
#define fsync(fd)   _commit(fd)
 
#define USES_WINSOCK
 
#define HAVE_UNION_SEMUN   1
 
#define IPC_RMID   256
 
#define IPC_CREAT   512
 
#define IPC_EXCL   1024
 
#define IPC_PRIVATE   234564
 
#define IPC_NOWAIT   2048
 
#define IPC_STAT   4096
 
#define EACCESS   2048
 
#define EIDRM   4096
 
#define SETALL   8192
 
#define GETNCNT   16384
 
#define GETVAL   65536
 
#define SETVAL   131072
 
#define GETPID   262144
 
#define WIFEXITED(w)   (((w) & 0XFFFFFF00) == 0)
 
#define WIFSIGNALED(w)   (!WIFEXITED(w))
 
#define WEXITSTATUS(w)   (w)
 
#define WTERMSIG(w)   (w)
 
#define sigmask(sig)   ( 1 << ((sig)-1) )
 
#define SIG_DFL   ((pqsigfunc)0)
 
#define SIG_ERR   ((pqsigfunc)-1)
 
#define SIG_IGN   ((pqsigfunc)1)
 
#define SIGHUP   1
 
#define SIGQUIT   3
 
#define SIGTRAP   5
 
#define SIGABRT   22 /* Set to match W32 value -- not UNIX value */
 
#define SIGKILL   9
 
#define SIGPIPE   13
 
#define SIGALRM   14
 
#define SIGSTOP   17
 
#define SIGTSTP   18
 
#define SIGCONT   19
 
#define SIGCHLD   20
 
#define SIGWINCH   28
 
#define SIGUSR1   30
 
#define SIGUSR2   31
 
#define ITIMER_REAL   0
 
#define pgoff_t   __int64
 
#define fseeko(stream, offset, origin)   fseeko64(stream, offset, origin)
 
#define ftello(stream)   ftello64(stream)
 
#define symlink(oldpath, newpath)   pgsymlink(oldpath, newpath)
 
#define readlink(path, buf, size)   pgreadlink(path, buf, size)
 
#define fstat(fileno, sb)   _pgfstat64(fileno, sb)
 
#define stat(path, sb)   _pgstat64(path, sb)
 
#define lstat(path, sb)   _pglstat64(path, sb)
 
#define S_IRUSR   _S_IREAD
 
#define S_IWUSR   _S_IWRITE
 
#define S_IXUSR   _S_IEXEC
 
#define S_IRWXU   (S_IRUSR | S_IWUSR | S_IXUSR)
 
#define S_IRGRP   0
 
#define S_IWGRP   0
 
#define S_IXGRP   0
 
#define S_IRWXG   0
 
#define S_IROTH   0
 
#define S_IWOTH   0
 
#define S_IXOTH   0
 
#define S_IRWXO   0
 
#define S_ISDIR(m)   (((m) & S_IFMT) == S_IFDIR)
 
#define S_ISREG(m)   (((m) & S_IFMT) == S_IFREG)
 
#define S_IFLNK   S_IFCHR
 
#define S_ISLNK(m)   (((m) & S_IFLNK) == S_IFLNK)
 
#define O_DSYNC   0x0080
 
#define O_CLOEXEC   0
 
#define EAGAIN   WSAEWOULDBLOCK
 
#define EINTR   WSAEINTR
 
#define EMSGSIZE   WSAEMSGSIZE
 
#define EAFNOSUPPORT   WSAEAFNOSUPPORT
 
#define EWOULDBLOCK   WSAEWOULDBLOCK
 
#define ECONNABORTED   WSAECONNABORTED
 
#define ECONNRESET   WSAECONNRESET
 
#define EINPROGRESS   WSAEINPROGRESS
 
#define EISCONN   WSAEISCONN
 
#define ENOBUFS   WSAENOBUFS
 
#define EPROTONOSUPPORT   WSAEPROTONOSUPPORT
 
#define ECONNREFUSED   WSAECONNREFUSED
 
#define ENOTSOCK   WSAENOTSOCK
 
#define EOPNOTSUPP   WSAEOPNOTSUPP
 
#define EADDRINUSE   WSAEADDRINUSE
 
#define EADDRNOTAVAIL   WSAEADDRNOTAVAIL
 
#define EHOSTDOWN   WSAEHOSTDOWN
 
#define EHOSTUNREACH   WSAEHOSTUNREACH
 
#define ENETDOWN   WSAENETDOWN
 
#define ENETRESET   WSAENETRESET
 
#define ENETUNREACH   WSAENETUNREACH
 
#define ENOTCONN   WSAENOTCONN
 
#define ETIMEDOUT   WSAETIMEDOUT
 
#define locale_t   _locale_t
 
#define tolower_l   _tolower_l
 
#define toupper_l   _toupper_l
 
#define towlower_l   _towlower_l
 
#define towupper_l   _towupper_l
 
#define isdigit_l   _isdigit_l
 
#define iswdigit_l   _iswdigit_l
 
#define isalpha_l   _isalpha_l
 
#define iswalpha_l   _iswalpha_l
 
#define isalnum_l   _isalnum_l
 
#define iswalnum_l   _iswalnum_l
 
#define isupper_l   _isupper_l
 
#define iswupper_l   _iswupper_l
 
#define islower_l   _islower_l
 
#define iswlower_l   _iswlower_l
 
#define isgraph_l   _isgraph_l
 
#define iswgraph_l   _iswgraph_l
 
#define isprint_l   _isprint_l
 
#define iswprint_l   _iswprint_l
 
#define ispunct_l   _ispunct_l
 
#define iswpunct_l   _iswpunct_l
 
#define isspace_l   _isspace_l
 
#define iswspace_l   _iswspace_l
 
#define strcoll_l   _strcoll_l
 
#define strxfrm_l   _strxfrm_l
 
#define wcscoll_l   _wcscoll_l
 
#define setlocale(a, b)   pgwin32_setlocale(a,b)
 
#define UNBLOCKED_SIGNAL_QUEUE()   (pg_signal_queue & ~pg_signal_mask)
 
#define PG_SIGNAL_COUNT   32
 
#define kill(pid, sig)   pgkill(pid,sig)
 
#define socket(af, type, protocol)   pgwin32_socket(af, type, protocol)
 
#define bind(s, addr, addrlen)   pgwin32_bind(s, addr, addrlen)
 
#define listen(s, backlog)   pgwin32_listen(s, backlog)
 
#define accept(s, addr, addrlen)   pgwin32_accept(s, addr, addrlen)
 
#define connect(s, name, namelen)   pgwin32_connect(s, name, namelen)
 
#define select(n, r, w, e, timeout)   pgwin32_select(n, r, w, e, timeout)
 
#define recv(s, buf, len, flags)   pgwin32_recv(s, buf, len, flags)
 
#define send(s, buf, len, flags)   pgwin32_send(s, buf, len, flags)
 
#define RTLD_NOW   1
 
#define RTLD_GLOBAL   0
 
#define putenv(x)   pgwin32_putenv(x)
 
#define setenv(x, y, z)   pgwin32_setenv(x,y,z)
 
#define unsetenv(x)   pgwin32_unsetenv(x)
 

Typedefs

typedef int uid_t
 
typedef int gid_t
 
typedef long key_t
 

Functions

int setitimer (int which, const struct itimerval *value, struct itimerval *ovalue)
 
DWORD pgwin32_get_file_type (HANDLE hFile)
 
int pgsymlink (const char *oldpath, const char *newpath)
 
int pgreadlink (const char *path, char *buf, size_t size)
 
int _pgfstat64 (int fileno, struct stat *buf)
 
int _pgstat64 (const char *name, struct stat *buf)
 
int _pglstat64 (const char *name, struct stat *buf)
 
char * pgwin32_setlocale (int category, const char *locale)
 
void pgwin32_signal_initialize (void)
 
HANDLE pgwin32_create_signal_listener (pid_t pid)
 
void pgwin32_dispatch_queued_signals (void)
 
void pg_queue_signal (int signum)
 
int pgkill (int pid, int sig)
 
SOCKET pgwin32_socket (int af, int type, int protocol)
 
int pgwin32_bind (SOCKET s, struct sockaddr *addr, int addrlen)
 
int pgwin32_listen (SOCKET s, int backlog)
 
SOCKET pgwin32_accept (SOCKET s, struct sockaddr *addr, int *addrlen)
 
int pgwin32_connect (SOCKET s, const struct sockaddr *name, int namelen)
 
int pgwin32_select (int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, const struct timeval *timeout)
 
int pgwin32_recv (SOCKET s, char *buf, int len, int flags)
 
int pgwin32_send (SOCKET s, const void *buf, int len, int flags)
 
int pgwin32_waitforsinglesocket (SOCKET s, int what, int timeout)
 
int pgwin32_ReserveSharedMemoryRegion (HANDLE)
 
void pgwin32_install_crashdump_handler (void)
 
void * dlopen (const char *file, int mode)
 
void * dlsym (void *handle, const char *symbol)
 
int dlclose (void *handle)
 
char * dlerror (void)
 
void _dosmaperr (unsigned long)
 
int pgwin32_putenv (const char *)
 
int pgwin32_setenv (const char *name, const char *value, int overwrite)
 
int pgwin32_unsetenv (const char *name)
 
int pgwin32_is_service (void)
 
int pgwin32_is_admin (void)
 
BOOL AddUserToTokenDacl (HANDLE hToken)
 
ssize_t pg_pread (int fd, void *buf, size_t nbyte, off_t offset)
 
ssize_t pg_pwrite (int fd, const void *buf, size_t nbyte, off_t offset)
 

Variables

PGDLLIMPORT volatile int pg_signal_queue
 
PGDLLIMPORT int pg_signal_mask
 
PGDLLIMPORT HANDLE pgwin32_signal_event
 
PGDLLIMPORT HANDLE pgwin32_initial_signal_pipe
 
PGDLLIMPORT int pgwin32_noblock
 

Macro Definition Documentation

◆ _WINSOCKAPI_

#define _WINSOCKAPI_

Definition at line 43 of file win32_port.h.

◆ accept

#define accept (   s,
  addr,
  addrlen 
)    pgwin32_accept(s, addr, addrlen)

Definition at line 493 of file win32_port.h.

◆ bind

#define bind (   s,
  addr,
  addrlen 
)    pgwin32_bind(s, addr, addrlen)

Definition at line 491 of file win32_port.h.

◆ connect

#define connect (   s,
  name,
  namelen 
)    pgwin32_connect(s, name, namelen)

Definition at line 494 of file win32_port.h.

◆ EACCESS

#define EACCESS   2048

Definition at line 102 of file win32_port.h.

◆ EADDRINUSE

#define EADDRINUSE   WSAEADDRINUSE

Definition at line 400 of file win32_port.h.

◆ EADDRNOTAVAIL

#define EADDRNOTAVAIL   WSAEADDRNOTAVAIL

Definition at line 402 of file win32_port.h.

◆ EAFNOSUPPORT

#define EAFNOSUPPORT   WSAEAFNOSUPPORT

Definition at line 378 of file win32_port.h.

◆ EAGAIN

#define EAGAIN   WSAEWOULDBLOCK

Definition at line 372 of file win32_port.h.

◆ ECONNABORTED

#define ECONNABORTED   WSAECONNABORTED

Definition at line 382 of file win32_port.h.

◆ ECONNREFUSED

#define ECONNREFUSED   WSAECONNREFUSED

Definition at line 394 of file win32_port.h.

◆ ECONNRESET

#define ECONNRESET   WSAECONNRESET

Definition at line 384 of file win32_port.h.

◆ EHOSTDOWN

#define EHOSTDOWN   WSAEHOSTDOWN

Definition at line 404 of file win32_port.h.

◆ EHOSTUNREACH

#define EHOSTUNREACH   WSAEHOSTUNREACH

Definition at line 406 of file win32_port.h.

◆ EIDRM

#define EIDRM   4096

Definition at line 104 of file win32_port.h.

◆ EINPROGRESS

#define EINPROGRESS   WSAEINPROGRESS

Definition at line 386 of file win32_port.h.

◆ EINTR

#define EINTR   WSAEINTR

Definition at line 374 of file win32_port.h.

◆ EISCONN

#define EISCONN   WSAEISCONN

Definition at line 388 of file win32_port.h.

◆ EMSGSIZE

#define EMSGSIZE   WSAEMSGSIZE

Definition at line 376 of file win32_port.h.

◆ ENABLE_SSPI

#define ENABLE_SSPI   1

Definition at line 23 of file win32_port.h.

◆ ENETDOWN

#define ENETDOWN   WSAENETDOWN

Definition at line 408 of file win32_port.h.

◆ ENETRESET

#define ENETRESET   WSAENETRESET

Definition at line 410 of file win32_port.h.

◆ ENETUNREACH

#define ENETUNREACH   WSAENETUNREACH

Definition at line 412 of file win32_port.h.

◆ ENOBUFS

#define ENOBUFS   WSAENOBUFS

Definition at line 390 of file win32_port.h.

◆ ENOTCONN

#define ENOTCONN   WSAENOTCONN

Definition at line 414 of file win32_port.h.

◆ ENOTSOCK

#define ENOTSOCK   WSAENOTSOCK

Definition at line 396 of file win32_port.h.

◆ EOPNOTSUPP

#define EOPNOTSUPP   WSAEOPNOTSUPP

Definition at line 398 of file win32_port.h.

◆ EPROTONOSUPPORT

#define EPROTONOSUPPORT   WSAEPROTONOSUPPORT

Definition at line 392 of file win32_port.h.

◆ ETIMEDOUT

#define ETIMEDOUT   WSAETIMEDOUT

Definition at line 416 of file win32_port.h.

◆ EWOULDBLOCK

#define EWOULDBLOCK   WSAEWOULDBLOCK

Definition at line 380 of file win32_port.h.

◆ fseeko

#define fseeko (   stream,
  offset,
  origin 
)    fseeko64(stream, offset, origin)

Definition at line 216 of file win32_port.h.

◆ fstat [1/2]

#define fstat   microsoft_native_fstat

Definition at line 283 of file win32_port.h.

◆ fstat [2/2]

#define fstat (   fileno,
  sb 
)    _pgfstat64(fileno, sb)

Definition at line 283 of file win32_port.h.

◆ fsync

#define fsync (   fd)    _commit(fd)

Definition at line 85 of file win32_port.h.

◆ ftello

#define ftello (   stream)    ftello64(stream)

Definition at line 219 of file win32_port.h.

◆ ftruncate

#define ftruncate (   a,
  b 
)    chsize(a,b)

Definition at line 82 of file win32_port.h.

◆ GETNCNT

#define GETNCNT   16384

Definition at line 108 of file win32_port.h.

◆ GETPID

#define GETPID   262144

Definition at line 111 of file win32_port.h.

◆ GETVAL

#define GETVAL   65536

Definition at line 109 of file win32_port.h.

◆ HAVE_UNION_SEMUN

#define HAVE_UNION_SEMUN   1

Definition at line 93 of file win32_port.h.

◆ IPC_CREAT

#define IPC_CREAT   512

Definition at line 96 of file win32_port.h.

◆ IPC_EXCL

#define IPC_EXCL   1024

Definition at line 97 of file win32_port.h.

◆ IPC_NOWAIT

#define IPC_NOWAIT   2048

Definition at line 99 of file win32_port.h.

◆ IPC_PRIVATE

#define IPC_PRIVATE   234564

Definition at line 98 of file win32_port.h.

◆ IPC_RMID

#define IPC_RMID   256

Definition at line 95 of file win32_port.h.

◆ IPC_STAT

#define IPC_STAT   4096

Definition at line 100 of file win32_port.h.

◆ isalnum_l

#define isalnum_l   _isalnum_l

Definition at line 433 of file win32_port.h.

◆ isalpha_l

#define isalpha_l   _isalpha_l

Definition at line 431 of file win32_port.h.

◆ isdigit_l

#define isdigit_l   _isdigit_l

Definition at line 429 of file win32_port.h.

◆ isgraph_l

#define isgraph_l   _isgraph_l

Definition at line 439 of file win32_port.h.

◆ islower_l

#define islower_l   _islower_l

Definition at line 437 of file win32_port.h.

◆ isprint_l

#define isprint_l   _isprint_l

Definition at line 441 of file win32_port.h.

◆ ispunct_l

#define ispunct_l   _ispunct_l

Definition at line 443 of file win32_port.h.

◆ isspace_l

#define isspace_l   _isspace_l

Definition at line 445 of file win32_port.h.

◆ isupper_l

#define isupper_l   _isupper_l

Definition at line 435 of file win32_port.h.

◆ iswalnum_l

#define iswalnum_l   _iswalnum_l

Definition at line 434 of file win32_port.h.

◆ iswalpha_l

#define iswalpha_l   _iswalpha_l

Definition at line 432 of file win32_port.h.

◆ iswdigit_l

#define iswdigit_l   _iswdigit_l

Definition at line 430 of file win32_port.h.

◆ iswgraph_l

#define iswgraph_l   _iswgraph_l

Definition at line 440 of file win32_port.h.

◆ iswlower_l

#define iswlower_l   _iswlower_l

Definition at line 438 of file win32_port.h.

◆ iswprint_l

#define iswprint_l   _iswprint_l

Definition at line 442 of file win32_port.h.

◆ iswpunct_l

#define iswpunct_l   _iswpunct_l

Definition at line 444 of file win32_port.h.

◆ iswspace_l

#define iswspace_l   _iswspace_l

Definition at line 446 of file win32_port.h.

◆ iswupper_l

#define iswupper_l   _iswupper_l

Definition at line 436 of file win32_port.h.

◆ ITIMER_REAL

#define ITIMER_REAL   0

Definition at line 190 of file win32_port.h.

◆ kill

#define kill (   pid,
  sig 
)    pgkill(pid,sig)

Definition at line 485 of file win32_port.h.

◆ listen

#define listen (   s,
  backlog 
)    pgwin32_listen(s, backlog)

Definition at line 492 of file win32_port.h.

◆ locale_t

#define locale_t   _locale_t

Definition at line 424 of file win32_port.h.

◆ lstat

#define lstat (   path,
  sb 
)    _pglstat64(path, sb)

Definition at line 285 of file win32_port.h.

◆ mkdir

#define mkdir (   a,
  b 
)    mkdir(a)

Definition at line 80 of file win32_port.h.

◆ O_CLOEXEC

#define O_CLOEXEC   0

Definition at line 359 of file win32_port.h.

◆ O_DSYNC

#define O_DSYNC   0x0080

Definition at line 352 of file win32_port.h.

◆ PG_SIGNAL_COUNT

#define PG_SIGNAL_COUNT   32

Definition at line 477 of file win32_port.h.

◆ pgoff_t

#define pgoff_t   __int64

Definition at line 207 of file win32_port.h.

◆ putenv

#define putenv (   x)    pgwin32_putenv(x)

Definition at line 536 of file win32_port.h.

◆ readlink

#define readlink (   path,
  buf,
  size 
)    pgreadlink(path, buf, size)

Definition at line 236 of file win32_port.h.

◆ recv

#define recv (   s,
  buf,
  len,
  flags 
)    pgwin32_recv(s, buf, len, flags)

Definition at line 496 of file win32_port.h.

◆ RTLD_GLOBAL

#define RTLD_GLOBAL   0

Definition at line 526 of file win32_port.h.

◆ RTLD_NOW

#define RTLD_NOW   1

Definition at line 525 of file win32_port.h.

◆ S_IFLNK

#define S_IFLNK   S_IFCHR

Definition at line 343 of file win32_port.h.

◆ S_IRGRP

#define S_IRGRP   0

Definition at line 301 of file win32_port.h.

◆ S_IROTH

#define S_IROTH   0

Definition at line 313 of file win32_port.h.

◆ S_IRUSR

#define S_IRUSR   _S_IREAD

Definition at line 289 of file win32_port.h.

◆ S_IRWXG

#define S_IRWXG   0

Definition at line 310 of file win32_port.h.

◆ S_IRWXO

#define S_IRWXO   0

Definition at line 322 of file win32_port.h.

◆ S_IRWXU

#define S_IRWXU   (S_IRUSR | S_IWUSR | S_IXUSR)

Definition at line 298 of file win32_port.h.

◆ S_ISDIR

#define S_ISDIR (   m)    (((m) & S_IFMT) == S_IFDIR)

Definition at line 325 of file win32_port.h.

◆ S_ISLNK

#define S_ISLNK (   m)    (((m) & S_IFLNK) == S_IFLNK)

Definition at line 344 of file win32_port.h.

◆ S_ISREG

#define S_ISREG (   m)    (((m) & S_IFMT) == S_IFREG)

Definition at line 328 of file win32_port.h.

◆ S_IWGRP

#define S_IWGRP   0

Definition at line 304 of file win32_port.h.

◆ S_IWOTH

#define S_IWOTH   0

Definition at line 316 of file win32_port.h.

◆ S_IWUSR

#define S_IWUSR   _S_IWRITE

Definition at line 292 of file win32_port.h.

◆ S_IXGRP

#define S_IXGRP   0

Definition at line 307 of file win32_port.h.

◆ S_IXOTH

#define S_IXOTH   0

Definition at line 319 of file win32_port.h.

◆ S_IXUSR

#define S_IXUSR   _S_IEXEC

Definition at line 295 of file win32_port.h.

◆ select

#define select (   n,
  r,
  w,
  e,
  timeout 
)    pgwin32_select(n, r, w, e, timeout)

Definition at line 495 of file win32_port.h.

◆ send

#define send (   s,
  buf,
  len,
  flags 
)    pgwin32_send(s, buf, len, flags)

Definition at line 497 of file win32_port.h.

◆ SETALL

#define SETALL   8192

Definition at line 107 of file win32_port.h.

◆ setenv

#define setenv (   x,
  y,
 
)    pgwin32_setenv(x,y,z)

Definition at line 537 of file win32_port.h.

◆ setlocale

#define setlocale (   a,
  b 
)    pgwin32_setlocale(a,b)

Definition at line 467 of file win32_port.h.

◆ SETVAL

#define SETVAL   131072

Definition at line 110 of file win32_port.h.

◆ SIG_DFL

#define SIG_DFL   ((pqsigfunc)0)

Definition at line 163 of file win32_port.h.

◆ SIG_ERR

#define SIG_ERR   ((pqsigfunc)-1)

Definition at line 164 of file win32_port.h.

◆ SIG_IGN

#define SIG_IGN   ((pqsigfunc)1)

Definition at line 165 of file win32_port.h.

◆ SIGABRT

#define SIGABRT   22 /* Set to match W32 value -- not UNIX value */

Definition at line 171 of file win32_port.h.

◆ SIGALRM

#define SIGALRM   14

Definition at line 174 of file win32_port.h.

◆ SIGCHLD

#define SIGCHLD   20

Definition at line 178 of file win32_port.h.

◆ SIGCONT

#define SIGCONT   19

Definition at line 177 of file win32_port.h.

◆ SIGHUP

#define SIGHUP   1

Definition at line 168 of file win32_port.h.

◆ SIGKILL

#define SIGKILL   9

Definition at line 172 of file win32_port.h.

◆ sigmask

#define sigmask (   sig)    ( 1 << ((sig)-1) )

Definition at line 157 of file win32_port.h.

◆ SIGPIPE

#define SIGPIPE   13

Definition at line 173 of file win32_port.h.

◆ SIGQUIT

#define SIGQUIT   3

Definition at line 169 of file win32_port.h.

◆ SIGSTOP

#define SIGSTOP   17

Definition at line 175 of file win32_port.h.

◆ SIGTRAP

#define SIGTRAP   5

Definition at line 170 of file win32_port.h.

◆ SIGTSTP

#define SIGTSTP   18

Definition at line 176 of file win32_port.h.

◆ SIGUSR1

#define SIGUSR1   30

Definition at line 180 of file win32_port.h.

◆ SIGUSR2

#define SIGUSR2   31

Definition at line 181 of file win32_port.h.

◆ SIGWINCH

#define SIGWINCH   28

Definition at line 179 of file win32_port.h.

◆ socket

#define socket (   af,
  type,
  protocol 
)    pgwin32_socket(af, type, protocol)

Definition at line 490 of file win32_port.h.

◆ stat [1/2]

#define stat   microsoft_native_stat

Definition at line 284 of file win32_port.h.

◆ stat [2/2]

#define stat (   path,
  sb 
)    _pgstat64(path, sb)

Definition at line 284 of file win32_port.h.

◆ strcoll_l

#define strcoll_l   _strcoll_l

Definition at line 447 of file win32_port.h.

◆ strxfrm_l

#define strxfrm_l   _strxfrm_l

Definition at line 448 of file win32_port.h.

◆ symlink

#define symlink (   oldpath,
  newpath 
)    pgsymlink(oldpath, newpath)

Definition at line 235 of file win32_port.h.

◆ tolower_l

#define tolower_l   _tolower_l

Definition at line 425 of file win32_port.h.

◆ toupper_l

#define toupper_l   _toupper_l

Definition at line 426 of file win32_port.h.

◆ towlower_l

#define towlower_l   _towlower_l

Definition at line 427 of file win32_port.h.

◆ towupper_l

#define towupper_l   _towupper_l

Definition at line 428 of file win32_port.h.

◆ UMDF_USING_NTSTATUS

#define UMDF_USING_NTSTATUS

Definition at line 58 of file win32_port.h.

◆ UNBLOCKED_SIGNAL_QUEUE

#define UNBLOCKED_SIGNAL_QUEUE ( )    (pg_signal_queue & ~pg_signal_mask)

Definition at line 476 of file win32_port.h.

◆ unsetenv

#define unsetenv (   x)    pgwin32_unsetenv(x)

Definition at line 538 of file win32_port.h.

◆ USES_WINSOCK

#define USES_WINSOCK

Definition at line 87 of file win32_port.h.

◆ wcscoll_l

#define wcscoll_l   _wcscoll_l

Definition at line 449 of file win32_port.h.

◆ WEXITSTATUS

#define WEXITSTATUS (   w)    (w)

Definition at line 154 of file win32_port.h.

◆ WIFEXITED

#define WIFEXITED (   w)    (((w) & 0XFFFFFF00) == 0)

Definition at line 152 of file win32_port.h.

◆ WIFSIGNALED

#define WIFSIGNALED (   w)    (!WIFEXITED(w))

Definition at line 153 of file win32_port.h.

◆ WIN32_LEAN_AND_MEAN

#define WIN32_LEAN_AND_MEAN

Definition at line 57 of file win32_port.h.

◆ WTERMSIG

#define WTERMSIG (   w)    (w)

Definition at line 155 of file win32_port.h.

Typedef Documentation

◆ gid_t

typedef int gid_t

Definition at line 245 of file win32_port.h.

◆ key_t

typedef long key_t

Definition at line 247 of file win32_port.h.

◆ uid_t

typedef int uid_t

Definition at line 244 of file win32_port.h.

Function Documentation

◆ _dosmaperr()

void _dosmaperr ( unsigned long  e)

Definition at line 177 of file win32error.c.

178 {
179  int i;
180 
181  if (e == 0)
182  {
183  errno = 0;
184  return;
185  }
186 
187  for (i = 0; i < lengthof(doserrors); i++)
188  {
189  if (doserrors[i].winerr == e)
190  {
191  int doserr = doserrors[i].doserr;
192 
193 #ifndef FRONTEND
194  ereport(DEBUG5,
195  (errmsg_internal("mapped win32 error code %lu to %d",
196  e, doserr)));
197 #elif defined(FRONTEND_DEBUG)
198  fprintf(stderr, "mapped win32 error code %lu to %d", e, doserr);
199 #endif
200  errno = doserr;
201  return;
202  }
203  }
204 
205 #ifndef FRONTEND
206  ereport(LOG,
207  (errmsg_internal("unrecognized win32 error code: %lu",
208  e)));
209 #else
210  fprintf(stderr, "unrecognized win32 error code: %lu", e);
211 #endif
212 
213  errno = EINVAL;
214 }
#define lengthof(array)
Definition: c.h:788
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1159
#define LOG
Definition: elog.h:31
#define ereport(elevel,...)
Definition: elog.h:149
#define DEBUG5
Definition: elog.h:26
int i
Definition: isn.c:73
#define fprintf
Definition: port.h:242
e
Definition: preproc-init.c:82
int doserr
Definition: win32error.c:23
static const struct @182 doserrors[]
DWORD winerr
Definition: win32error.c:22

References DEBUG5, doserr, doserrors, ereport, errmsg_internal(), fprintf, i, lengthof, LOG, and winerr.

Referenced by BaseBackup(), copyFile(), dsm_impl_pin_segment(), dsm_impl_unpin_segment(), fdatasync(), fileinfo_to_stat(), FileReadV(), FileWriteV(), getrusage(), initialize_ntdll(), link(), pg_import_system_collations(), pg_pread(), pg_pwrite(), pgwin32_get_file_type(), and readdir().

◆ _pgfstat64()

int _pgfstat64 ( int  fileno,
struct stat buf 
)

Definition at line 255 of file win32stat.c.

256 {
257  HANDLE hFile = (HANDLE) _get_osfhandle(fileno);
258  DWORD fileType = FILE_TYPE_UNKNOWN;
259  unsigned short st_mode;
260 
261  if (buf == NULL)
262  {
263  errno = EINVAL;
264  return -1;
265  }
266 
267  fileType = pgwin32_get_file_type(hFile);
268  if (errno != 0)
269  return -1;
270 
271  switch (fileType)
272  {
273  /* The specified file is a disk file */
274  case FILE_TYPE_DISK:
275  return fileinfo_to_stat(hFile, buf);
276 
277  /*
278  * The specified file is a socket, a named pipe, or an anonymous
279  * pipe.
280  */
281  case FILE_TYPE_PIPE:
282  st_mode = _S_IFIFO;
283  break;
284  /* The specified file is a character file */
285  case FILE_TYPE_CHAR:
286  st_mode = _S_IFCHR;
287  break;
288  /* Unused flag and unknown file type */
289  case FILE_TYPE_REMOTE:
290  case FILE_TYPE_UNKNOWN:
291  default:
292  errno = EINVAL;
293  return -1;
294  }
295 
296  memset(buf, 0, sizeof(*buf));
297  buf->st_mode = st_mode;
298  buf->st_dev = fileno;
299  buf->st_rdev = fileno;
300  buf->st_nlink = 1;
301  return 0;
302 }
static char * buf
Definition: pg_test_fsync.c:73
DWORD pgwin32_get_file_type(HANDLE hFile)
Definition: win32common.c:31
static int fileinfo_to_stat(HANDLE hFile, struct stat *buf)
Definition: win32stat.c:68

References buf, fileinfo_to_stat(), and pgwin32_get_file_type().

◆ _pglstat64()

int _pglstat64 ( const char *  name,
struct stat buf 
)

Definition at line 113 of file win32stat.c.

114 {
115  /*
116  * Our open wrapper will report STATUS_DELETE_PENDING as ENOENT. We
117  * request FILE_FLAG_BACKUP_SEMANTICS so that we can open directories too,
118  * for limited purposes. We use the private handle-based version, so we
119  * don't risk running out of fds.
120  */
121  HANDLE hFile;
122  int ret;
123 
124  hFile = pgwin32_open_handle(name, O_RDONLY, true);
125  if (hFile == INVALID_HANDLE_VALUE)
126  {
127  if (errno == ENOENT)
128  {
129  /*
130  * If it's a junction point pointing to a non-existent path, we'll
131  * have ENOENT here (because pgwin32_open_handle does not use
132  * FILE_FLAG_OPEN_REPARSE_POINT). In that case, we'll try again
133  * with readlink() below, which will distinguish true ENOENT from
134  * pseudo-symlink.
135  */
136  memset(buf, 0, sizeof(*buf));
137  ret = 0;
138  }
139  else
140  return -1;
141  }
142  else
143  ret = fileinfo_to_stat(hFile, buf);
144 
145  /*
146  * Junction points appear as directories to fileinfo_to_stat(), so we'll
147  * need to do a bit more work to distinguish them.
148  */
149  if ((ret == 0 && S_ISDIR(buf->st_mode)) || hFile == INVALID_HANDLE_VALUE)
150  {
151  char next[MAXPGPATH];
152  ssize_t size;
153 
154  /*
155  * POSIX says we need to put the length of the target path into
156  * st_size. Use readlink() to get it, or learn that this is not a
157  * junction point.
158  */
159  size = readlink(name, next, sizeof(next));
160  if (size < 0)
161  {
162  if (errno == EACCES &&
163  pg_RtlGetLastNtStatus() == STATUS_DELETE_PENDING)
164  {
165  /* Unlinked underneath us. */
166  errno = ENOENT;
167  ret = -1;
168  }
169  else if (errno == EINVAL)
170  {
171  /* It's not a junction point, nothing to do. */
172  }
173  else
174  {
175  /* Some other failure. */
176  ret = -1;
177  }
178  }
179  else
180  {
181  /* It's a junction point, so report it as a symlink. */
182  buf->st_mode &= ~S_IFDIR;
183  buf->st_mode |= S_IFLNK;
184  buf->st_size = size;
185  ret = 0;
186  }
187  }
188 
189  if (hFile != INVALID_HANDLE_VALUE)
190  CloseHandle(hFile);
191  return ret;
192 }
static int32 next
Definition: blutils.c:221
#define MAXPGPATH
static pg_noinline void Size size
Definition: slab.c:607
const char * name
#define S_ISDIR(m)
Definition: win32_port.h:325
#define readlink(path, buf, size)
Definition: win32_port.h:236
#define S_IFLNK
Definition: win32_port.h:343
PGDLLIMPORT RtlGetLastNtStatus_t pg_RtlGetLastNtStatus
Definition: win32ntdll.c:20

References buf, fileinfo_to_stat(), MAXPGPATH, name, next, pg_RtlGetLastNtStatus, readlink, S_IFLNK, S_ISDIR, and size.

Referenced by _pgstat64().

◆ _pgstat64()

int _pgstat64 ( const char *  name,
struct stat buf 
)

Definition at line 198 of file win32stat.c.

199 {
200  int loops = 0;
201  int ret;
202  char curr[MAXPGPATH];
203 
204  ret = _pglstat64(name, buf);
205 
206  strlcpy(curr, name, MAXPGPATH);
207 
208  /* Do we need to follow a symlink (junction point)? */
209  while (ret == 0 && S_ISLNK(buf->st_mode))
210  {
211  char next[MAXPGPATH];
212  ssize_t size;
213 
214  if (++loops > 8)
215  {
216  errno = ELOOP;
217  return -1;
218  }
219 
220  /*
221  * _pglstat64() already called readlink() once to be able to fill in
222  * st_size, and now we need to do it again to get the path to follow.
223  * That could be optimized, but stat() on symlinks is probably rare
224  * and this way is simple.
225  */
226  size = readlink(curr, next, sizeof(next));
227  if (size < 0)
228  {
229  if (errno == EACCES &&
230  pg_RtlGetLastNtStatus() == STATUS_DELETE_PENDING)
231  {
232  /* Unlinked underneath us. */
233  errno = ENOENT;
234  }
235  return -1;
236  }
237  if (size >= sizeof(next))
238  {
239  errno = ENAMETOOLONG;
240  return -1;
241  }
242  next[size] = 0;
243 
244  ret = _pglstat64(next, buf);
245  strcpy(curr, next);
246  }
247 
248  return ret;
249 }
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
#define S_ISLNK(m)
Definition: win32_port.h:344
int _pglstat64(const char *name, struct stat *buf)
Definition: win32stat.c:113

References _pglstat64(), buf, MAXPGPATH, name, next, pg_RtlGetLastNtStatus, readlink, S_ISLNK, size, and strlcpy().

◆ AddUserToTokenDacl()

BOOL AddUserToTokenDacl ( HANDLE  hToken)

◆ dlclose()

int dlclose ( void *  handle)

Definition at line 49 of file win32dlopen.c.

50 {
51  if (!FreeLibrary((HMODULE) handle))
52  {
53  set_dl_error();
54  return 1;
55  }
56  last_dyn_error[0] = 0;
57  return 0;
58 }
static char last_dyn_error[512]
Definition: win32dlopen.c:18
static void set_dl_error(void)
Definition: win32dlopen.c:21

References last_dyn_error, and set_dl_error().

Referenced by internal_load_library().

◆ dlerror()

char* dlerror ( void  )

Definition at line 40 of file win32dlopen.c.

41 {
42  if (last_dyn_error[0])
43  return last_dyn_error;
44  else
45  return NULL;
46 }

References last_dyn_error.

Referenced by internal_load_library().

◆ dlopen()

void* dlopen ( const char *  file,
int  mode 
)

Definition at line 76 of file win32dlopen.c.

77 {
78  HMODULE h;
79  int prevmode;
80 
81  /* Disable popup error messages when loading DLLs */
82  prevmode = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
83  h = LoadLibrary(file);
84  SetErrorMode(prevmode);
85 
86  if (!h)
87  {
88  set_dl_error();
89  return NULL;
90  }
91  last_dyn_error[0] = 0;
92  return (void *) h;
93 }

References last_dyn_error, and set_dl_error().

Referenced by internal_load_library().

◆ dlsym()

void* dlsym ( void *  handle,
const char *  symbol 
)

Definition at line 61 of file win32dlopen.c.

62 {
63  void *ptr;
64 
65  ptr = GetProcAddress((HMODULE) handle, symbol);
66  if (!ptr)
67  {
68  set_dl_error();
69  return NULL;
70  }
71  last_dyn_error[0] = 0;
72  return ptr;
73 }
unsigned char symbol
Definition: api.h:2

References last_dyn_error, and set_dl_error().

Referenced by internal_load_library(), load_external_function(), and lookup_external_function().

◆ pg_pread()

ssize_t pg_pread ( int  fd,
void *  buf,
size_t  nbyte,
off_t  offset 
)

Definition at line 20 of file win32pread.c.

21 {
22  OVERLAPPED overlapped = {0};
23  HANDLE handle;
24  DWORD result;
25 
26  handle = (HANDLE) _get_osfhandle(fd);
27  if (handle == INVALID_HANDLE_VALUE)
28  {
29  errno = EBADF;
30  return -1;
31  }
32 
33  /* Avoid overflowing DWORD. */
34  size = Min(size, 1024 * 1024 * 1024);
35 
36  /* Note that this changes the file position, despite not using it. */
37  overlapped.Offset = offset;
38  if (!ReadFile(handle, buf, size, &result, &overlapped))
39  {
40  if (GetLastError() == ERROR_HANDLE_EOF)
41  return 0;
42 
43  _dosmaperr(GetLastError());
44  return -1;
45  }
46 
47  return result;
48 }
#define Min(x, y)
Definition: c.h:1004
static int fd(const char *x, int i)
Definition: preproc-init.c:105
void _dosmaperr(unsigned long)
Definition: win32error.c:177

References _dosmaperr(), buf, fd(), Min, and size.

◆ pg_pwrite()

ssize_t pg_pwrite ( int  fd,
const void *  buf,
size_t  nbyte,
off_t  offset 
)

Definition at line 20 of file win32pwrite.c.

21 {
22  OVERLAPPED overlapped = {0};
23  HANDLE handle;
24  DWORD result;
25 
26  handle = (HANDLE) _get_osfhandle(fd);
27  if (handle == INVALID_HANDLE_VALUE)
28  {
29  errno = EBADF;
30  return -1;
31  }
32 
33  /* Avoid overflowing DWORD. */
34  size = Min(size, 1024 * 1024 * 1024);
35 
36  /* Note that this changes the file position, despite not using it. */
37  overlapped.Offset = offset;
38  if (!WriteFile(handle, buf, size, &result, &overlapped))
39  {
40  _dosmaperr(GetLastError());
41  return -1;
42  }
43 
44  return result;
45 }

References _dosmaperr(), buf, fd(), Min, and size.

◆ pg_queue_signal()

void pg_queue_signal ( int  signum)

Definition at line 259 of file signal.c.

260 {
261  Assert(pgwin32_signal_event != NULL);
262  if (signum >= PG_SIGNAL_COUNT || signum <= 0)
263  return; /* ignore any bad signal number */
264 
265  EnterCriticalSection(&pg_signal_crit_sec);
266  pg_signal_queue |= sigmask(signum);
267  LeaveCriticalSection(&pg_signal_crit_sec);
268 
269  SetEvent(pgwin32_signal_event);
270 }
#define Assert(condition)
Definition: c.h:858
volatile int pg_signal_queue
Definition: signal.c:24
static CRITICAL_SECTION pg_signal_crit_sec
Definition: signal.c:34
HANDLE pgwin32_signal_event
Definition: signal.c:27
#define sigmask(sig)
Definition: win32_port.h:157
#define PG_SIGNAL_COUNT
Definition: win32_port.h:477

References Assert, PG_SIGNAL_COUNT, pg_signal_crit_sec, pg_signal_queue, pgwin32_signal_event, and sigmask.

Referenced by pg_console_handler(), pg_signal_thread(), and pg_timer_thread().

◆ pgkill()

int pgkill ( int  pid,
int  sig 
)

◆ pgreadlink()

int pgreadlink ( const char *  path,
char *  buf,
size_t  size 
)

◆ pgsymlink()

int pgsymlink ( const char *  oldpath,
const char *  newpath 
)

◆ pgwin32_accept()

SOCKET pgwin32_accept ( SOCKET  s,
struct sockaddr *  addr,
int *  addrlen 
)

Definition at line 337 of file socket.c.

338 {
339  SOCKET rs;
340 
341  /*
342  * Poll for signals, but don't return with EINTR, since we don't handle
343  * that in pqcomm.c
344  */
346 
347  rs = WSAAccept(s, addr, addrlen, NULL, 0);
348  if (rs == INVALID_SOCKET)
349  {
351  return INVALID_SOCKET;
352  }
353  return rs;
354 }
static int pgwin32_poll_signals(void)
Definition: socket.c:157
static void TranslateSocketError(void)
Definition: socket.c:56

References pgwin32_poll_signals(), and TranslateSocketError().

◆ pgwin32_bind()

int pgwin32_bind ( SOCKET  s,
struct sockaddr *  addr,
int  addrlen 
)

Definition at line 315 of file socket.c.

316 {
317  int res;
318 
319  res = bind(s, addr, addrlen);
320  if (res < 0)
322  return res;
323 }
#define bind(s, addr, addrlen)
Definition: win32_port.h:491

References bind, res, and TranslateSocketError().

◆ pgwin32_connect()

int pgwin32_connect ( SOCKET  s,
const struct sockaddr *  name,
int  namelen 
)

Definition at line 359 of file socket.c.

360 {
361  int r;
362 
363  r = WSAConnect(s, addr, addrlen, NULL, NULL, NULL, NULL);
364  if (r == 0)
365  return 0;
366 
367  if (WSAGetLastError() != WSAEWOULDBLOCK)
368  {
370  return -1;
371  }
372 
373  while (pgwin32_waitforsinglesocket(s, FD_CONNECT, INFINITE) == 0)
374  {
375  /* Loop endlessly as long as we are just delivering signals */
376  }
377 
378  return 0;
379 }
int pgwin32_waitforsinglesocket(SOCKET s, int what, int timeout)
Definition: socket.c:181

References pgwin32_waitforsinglesocket(), and TranslateSocketError().

◆ pgwin32_create_signal_listener()

HANDLE pgwin32_create_signal_listener ( pid_t  pid)

Definition at line 227 of file signal.c.

228 {
229  char pipename[128];
230  HANDLE pipe;
231 
232  snprintf(pipename, sizeof(pipename), "\\\\.\\pipe\\pgsignal_%u", (int) pid);
233 
234  pipe = CreateNamedPipe(pipename, PIPE_ACCESS_DUPLEX,
235  PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
236  PIPE_UNLIMITED_INSTANCES, 16, 16, 1000, NULL);
237 
238  if (pipe == INVALID_HANDLE_VALUE)
239  ereport(ERROR,
240  (errmsg("could not create signal listener pipe for PID %d: error code %lu",
241  (int) pid, GetLastError())));
242 
243  return pipe;
244 }
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define ERROR
Definition: elog.h:39
#define snprintf
Definition: port.h:238

References ereport, errmsg(), ERROR, and snprintf.

◆ pgwin32_dispatch_queued_signals()

void pgwin32_dispatch_queued_signals ( void  )

Definition at line 120 of file signal.c.

121 {
122  int exec_mask;
123 
124  Assert(pgwin32_signal_event != NULL);
125  EnterCriticalSection(&pg_signal_crit_sec);
126  while ((exec_mask = UNBLOCKED_SIGNAL_QUEUE()) != 0)
127  {
128  /* One or more unblocked signals queued for execution */
129  int i;
130 
131  for (i = 1; i < PG_SIGNAL_COUNT; i++)
132  {
133  if (exec_mask & sigmask(i))
134  {
135  /* Execute this signal */
136  struct sigaction *act = &pg_signal_array[i];
137  pqsigfunc sig = act->sa_handler;
138 
139  if (sig == SIG_DFL)
142  if (sig != SIG_ERR && sig != SIG_IGN && sig != SIG_DFL)
143  {
144  sigset_t block_mask;
145  sigset_t save_mask;
146 
147  LeaveCriticalSection(&pg_signal_crit_sec);
148 
149  block_mask = act->sa_mask;
150  if ((act->sa_flags & SA_NODEFER) == 0)
151  block_mask |= sigmask(i);
152 
153  sigprocmask(SIG_BLOCK, &block_mask, &save_mask);
154  sig(i);
155  sigprocmask(SIG_SETMASK, &save_mask, NULL);
156 
157  EnterCriticalSection(&pg_signal_crit_sec);
158  break; /* Restart outer loop, in case signal mask or
159  * queue has been modified inside signal
160  * handler */
161  }
162  }
163  }
164  }
165  ResetEvent(pgwin32_signal_event);
166  LeaveCriticalSection(&pg_signal_crit_sec);
167 }
static int sig
Definition: pg_ctl.c:79
void(* pqsigfunc)(SIGNAL_ARGS)
Definition: port.h:492
static struct sigaction pg_signal_array[PG_SIGNAL_COUNT]
Definition: signal.c:37
static pqsigfunc pg_signal_defaults[PG_SIGNAL_COUNT]
Definition: signal.c:38
#define UNBLOCKED_SIGNAL_QUEUE()
Definition: win32_port.h:476
#define SIG_DFL
Definition: win32_port.h:163
#define SIG_ERR
Definition: win32_port.h:164
#define SIG_IGN
Definition: win32_port.h:165

References Assert, i, pg_signal_array, PG_SIGNAL_COUNT, pg_signal_crit_sec, pg_signal_defaults, pg_signal_queue, pgwin32_signal_event, sig, SIG_DFL, SIG_ERR, SIG_IGN, sigmask, and UNBLOCKED_SIGNAL_QUEUE.

Referenced by pg_usleep(), PGSemaphoreLock(), pgwin32_poll_signals(), pgwin32_select(), pgwin32_waitforsinglesocket(), pqsigprocmask(), and WaitEventSetWait().

◆ pgwin32_get_file_type()

DWORD pgwin32_get_file_type ( HANDLE  hFile)

Definition at line 31 of file win32common.c.

32 {
33  DWORD fileType = FILE_TYPE_UNKNOWN;
34  DWORD lastError;
35 
36  errno = 0;
37 
38  /*
39  * When stdin, stdout, and stderr aren't associated with a stream the
40  * special value -2 is returned:
41  * https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/get-osfhandle
42  */
43  if (hFile == INVALID_HANDLE_VALUE || hFile == (HANDLE) -2)
44  {
45  errno = EINVAL;
46  return FILE_TYPE_UNKNOWN;
47  }
48 
49  fileType = GetFileType(hFile);
50  lastError = GetLastError();
51 
52  /*
53  * Invoke GetLastError in order to distinguish between a "valid" return of
54  * FILE_TYPE_UNKNOWN and its return due to a calling error. In case of
55  * success, GetLastError() returns NO_ERROR.
56  */
57  if (fileType == FILE_TYPE_UNKNOWN && lastError != NO_ERROR)
58  {
59  _dosmaperr(lastError);
60  return FILE_TYPE_UNKNOWN;
61  }
62 
63  return fileType;
64 }

References _dosmaperr().

Referenced by _pgfstat64().

◆ pgwin32_install_crashdump_handler()

void pgwin32_install_crashdump_handler ( void  )

Definition at line 178 of file crashdump.c.

179 {
180  SetUnhandledExceptionFilter(crashDumpHandler);
181 }
static LONG WINAPI crashDumpHandler(struct _EXCEPTION_POINTERS *pExceptionInfo)
Definition: crashdump.c:90

References crashDumpHandler().

Referenced by main().

◆ pgwin32_is_admin()

int pgwin32_is_admin ( void  )

Definition at line 49 of file win32security.c.

50 {
51  PSID AdministratorsSid;
52  PSID PowerUsersSid;
53  SID_IDENTIFIER_AUTHORITY NtAuthority = {SECURITY_NT_AUTHORITY};
54  BOOL IsAdministrators;
55  BOOL IsPowerUsers;
56 
57  if (!AllocateAndInitializeSid(&NtAuthority, 2,
58  SECURITY_BUILTIN_DOMAIN_RID,
59  DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0,
60  0, &AdministratorsSid))
61  {
62  log_error(_("could not get SID for Administrators group: error code %lu\n"),
63  GetLastError());
64  exit(1);
65  }
66 
67  if (!AllocateAndInitializeSid(&NtAuthority, 2,
68  SECURITY_BUILTIN_DOMAIN_RID,
69  DOMAIN_ALIAS_RID_POWER_USERS, 0, 0, 0, 0, 0,
70  0, &PowerUsersSid))
71  {
72  log_error(_("could not get SID for PowerUsers group: error code %lu\n"),
73  GetLastError());
74  exit(1);
75  }
76 
77  if (!CheckTokenMembership(NULL, AdministratorsSid, &IsAdministrators) ||
78  !CheckTokenMembership(NULL, PowerUsersSid, &IsPowerUsers))
79  {
80  log_error(_("could not check access token membership: error code %lu\n"),
81  GetLastError());
82  exit(1);
83  }
84 
85  FreeSid(AdministratorsSid);
86  FreeSid(PowerUsersSid);
87 
88  if (IsAdministrators || IsPowerUsers)
89  return 1;
90  else
91  return 0;
92 }
#define _(x)
Definition: elog.c:90
exit(1)
static void log_error(const char *fmt,...) pg_attribute_printf(1
Definition: win32security.c:28

References _, exit(), and log_error().

Referenced by check_root().

◆ pgwin32_is_service()

int pgwin32_is_service ( void  )

Definition at line 120 of file win32security.c.

121 {
122  static int _is_service = -1;
123  BOOL IsMember;
124  PSID ServiceSid;
125  PSID LocalSystemSid;
126  SID_IDENTIFIER_AUTHORITY NtAuthority = {SECURITY_NT_AUTHORITY};
127  HANDLE stderr_handle;
128 
129  /* Only check the first time */
130  if (_is_service != -1)
131  return _is_service;
132 
133  /* Check if standard error is not valid */
134  stderr_handle = GetStdHandle(STD_ERROR_HANDLE);
135  if (stderr_handle != INVALID_HANDLE_VALUE && stderr_handle != NULL)
136  {
137  _is_service = 0;
138  return _is_service;
139  }
140 
141  /* Check if running as LocalSystem */
142  if (!AllocateAndInitializeSid(&NtAuthority, 1,
143  SECURITY_LOCAL_SYSTEM_RID, 0, 0, 0, 0, 0, 0, 0,
144  &LocalSystemSid))
145  {
146  fprintf(stderr, "could not get SID for local system account\n");
147  return -1;
148  }
149 
150  if (!CheckTokenMembership(NULL, LocalSystemSid, &IsMember))
151  {
152  fprintf(stderr, "could not check access token membership: error code %lu\n",
153  GetLastError());
154  FreeSid(LocalSystemSid);
155  return -1;
156  }
157  FreeSid(LocalSystemSid);
158 
159  if (IsMember)
160  {
161  _is_service = 1;
162  return _is_service;
163  }
164 
165  /* Check for service group membership */
166  if (!AllocateAndInitializeSid(&NtAuthority, 1,
167  SECURITY_SERVICE_RID, 0, 0, 0, 0, 0, 0, 0,
168  &ServiceSid))
169  {
170  fprintf(stderr, "could not get SID for service group: error code %lu\n",
171  GetLastError());
172  return -1;
173  }
174 
175  if (!CheckTokenMembership(NULL, ServiceSid, &IsMember))
176  {
177  fprintf(stderr, "could not check access token membership: error code %lu\n",
178  GetLastError());
179  FreeSid(ServiceSid);
180  return -1;
181  }
182  FreeSid(ServiceSid);
183 
184  if (IsMember)
185  _is_service = 1;
186  else
187  _is_service = 0;
188 
189  return _is_service;
190 }

References fprintf.

Referenced by send_message_to_server_log(), and write_stderr().

◆ pgwin32_listen()

int pgwin32_listen ( SOCKET  s,
int  backlog 
)

Definition at line 326 of file socket.c.

327 {
328  int res;
329 
330  res = listen(s, backlog);
331  if (res < 0)
333  return res;
334 }
#define listen(s, backlog)
Definition: win32_port.h:492

References listen, res, and TranslateSocketError().

◆ pgwin32_putenv()

int pgwin32_putenv ( const char *  envval)

Definition at line 27 of file win32env.c.

28 {
29  char *envcpy;
30  char *cp;
31  typedef int (_cdecl * PUTENVPROC) (const char *);
32  static const char *const modulenames[] = {
33  "msvcrt", /* Visual Studio 6.0 / MinGW */
34  "msvcrtd",
35  "msvcr70", /* Visual Studio 2002 */
36  "msvcr70d",
37  "msvcr71", /* Visual Studio 2003 */
38  "msvcr71d",
39  "msvcr80", /* Visual Studio 2005 */
40  "msvcr80d",
41  "msvcr90", /* Visual Studio 2008 */
42  "msvcr90d",
43  "msvcr100", /* Visual Studio 2010 */
44  "msvcr100d",
45  "msvcr110", /* Visual Studio 2012 */
46  "msvcr110d",
47  "msvcr120", /* Visual Studio 2013 */
48  "msvcr120d",
49  "ucrtbase", /* Visual Studio 2015 and later */
50  "ucrtbased",
51  NULL
52  };
53  int i;
54 
55  /*
56  * Update process environment, making this change visible to child
57  * processes and to CRTs initializing in the future. Do this before the
58  * _putenv() loop, for the benefit of any CRT that initializes during this
59  * pgwin32_putenv() execution, after the loop checks that CRT.
60  *
61  * Need a copy of the string so we can modify it.
62  */
63  envcpy = strdup(envval);
64  if (!envcpy)
65  return -1;
66  cp = strchr(envcpy, '=');
67  if (cp == NULL)
68  {
69  free(envcpy);
70  return -1;
71  }
72  *cp = '\0';
73  cp++;
74  if (*cp)
75  {
76  /*
77  * Only call SetEnvironmentVariable() when we are adding a variable,
78  * not when removing it. Calling it on both crashes on at least
79  * certain versions of MinGW.
80  */
81  if (!SetEnvironmentVariable(envcpy, cp))
82  {
83  free(envcpy);
84  return -1;
85  }
86  }
87  free(envcpy);
88 
89  /*
90  * Each CRT has its own _putenv() symbol and copy of the environment.
91  * Update the environment in each CRT module currently loaded, so every
92  * third-party library sees this change regardless of the CRT it links
93  * against. Addresses within these modules may become invalid the moment
94  * we call FreeLibrary(), so don't cache them.
95  */
96  for (i = 0; modulenames[i]; i++)
97  {
98  HMODULE hmodule = NULL;
99  BOOL res = GetModuleHandleEx(0, modulenames[i], &hmodule);
100 
101  if (res != 0 && hmodule != NULL)
102  {
103  PUTENVPROC putenvFunc;
104 
105  putenvFunc = (PUTENVPROC) (pg_funcptr_t) GetProcAddress(hmodule, "_putenv");
106  if (putenvFunc)
107  putenvFunc(envval);
108  FreeLibrary(hmodule);
109  }
110  }
111 
112  /*
113  * Finally, update our "own" cache. This is redundant with the loop
114  * above, except when PostgreSQL itself links to a CRT not listed above.
115  * Ideally, the loop does visit all possible CRTs, making this redundant.
116  */
117  return _putenv(envval);
118 }
void(* pg_funcptr_t)(void)
Definition: c.h:388
#define free(a)
Definition: header.h:65

References free, i, and res.

Referenced by pgwin32_setenv(), and pgwin32_unsetenv().

◆ pgwin32_recv()

int pgwin32_recv ( SOCKET  s,
char *  buf,
int  len,
int  flags 
)

Definition at line 382 of file socket.c.

383 {
384  WSABUF wbuf;
385  int r;
386  DWORD b;
387  DWORD flags = f;
388  int n;
389 
390  if (pgwin32_poll_signals())
391  return -1;
392 
393  wbuf.len = len;
394  wbuf.buf = buf;
395 
396  r = WSARecv(s, &wbuf, 1, &b, &flags, NULL, NULL);
397  if (r != SOCKET_ERROR)
398  return b; /* success */
399 
400  if (WSAGetLastError() != WSAEWOULDBLOCK)
401  {
403  return -1;
404  }
405 
406  if (pgwin32_noblock)
407  {
408  /*
409  * No data received, and we are in "emulated non-blocking mode", so
410  * return indicating that we'd block if we were to continue.
411  */
412  errno = EWOULDBLOCK;
413  return -1;
414  }
415 
416  /* We're in blocking mode, so wait for data */
417 
418  for (n = 0; n < 5; n++)
419  {
420  if (pgwin32_waitforsinglesocket(s, FD_READ | FD_CLOSE | FD_ACCEPT,
421  INFINITE) == 0)
422  return -1; /* errno already set */
423 
424  r = WSARecv(s, &wbuf, 1, &b, &flags, NULL, NULL);
425  if (r != SOCKET_ERROR)
426  return b; /* success */
427  if (WSAGetLastError() != WSAEWOULDBLOCK)
428  {
430  return -1;
431  }
432 
433  /*
434  * There seem to be cases on win2k (at least) where WSARecv can return
435  * WSAEWOULDBLOCK even when pgwin32_waitforsinglesocket claims the
436  * socket is readable. In this case, just sleep for a moment and try
437  * again. We try up to 5 times - if it fails more than that it's not
438  * likely to ever come back.
439  */
440  pg_usleep(10000);
441  }
442  ereport(NOTICE,
443  (errmsg_internal("could not read from ready socket (after retries)")));
444  errno = EWOULDBLOCK;
445  return -1;
446 }
#define NOTICE
Definition: elog.h:35
int b
Definition: isn.c:70
const void size_t len
void pg_usleep(long microsec)
Definition: signal.c:53
int pgwin32_noblock
Definition: socket.c:28
#define EWOULDBLOCK
Definition: win32_port.h:380

References b, buf, ereport, errmsg_internal(), EWOULDBLOCK, len, NOTICE, pg_usleep(), pgwin32_noblock, pgwin32_poll_signals(), pgwin32_waitforsinglesocket(), and TranslateSocketError().

◆ pgwin32_ReserveSharedMemoryRegion()

int pgwin32_ReserveSharedMemoryRegion ( HANDLE  hChild)

Definition at line 573 of file win32_shmem.c.

574 {
575  void *address;
576 
577  Assert(ShmemProtectiveRegion != NULL);
578  Assert(UsedShmemSegAddr != NULL);
579  Assert(UsedShmemSegSize != 0);
580 
581  /* ShmemProtectiveRegion */
582  address = VirtualAllocEx(hChild, ShmemProtectiveRegion,
584  MEM_RESERVE, PAGE_NOACCESS);
585  if (address == NULL)
586  {
587  /* Don't use FATAL since we're running in the postmaster */
588  elog(LOG, "could not reserve shared memory region (addr=%p) for child %p: error code %lu",
589  ShmemProtectiveRegion, hChild, GetLastError());
590  return false;
591  }
592  if (address != ShmemProtectiveRegion)
593  {
594  /*
595  * Should never happen - in theory if allocation granularity causes
596  * strange effects it could, so check just in case.
597  *
598  * Don't use FATAL since we're running in the postmaster.
599  */
600  elog(LOG, "reserved shared memory region got incorrect address %p, expected %p",
601  address, ShmemProtectiveRegion);
602  return false;
603  }
604 
605  /* UsedShmemSegAddr */
606  address = VirtualAllocEx(hChild, UsedShmemSegAddr, UsedShmemSegSize,
607  MEM_RESERVE, PAGE_READWRITE);
608  if (address == NULL)
609  {
610  elog(LOG, "could not reserve shared memory region (addr=%p) for child %p: error code %lu",
611  UsedShmemSegAddr, hChild, GetLastError());
612  return false;
613  }
614  if (address != UsedShmemSegAddr)
615  {
616  elog(LOG, "reserved shared memory region got incorrect address %p, expected %p",
617  address, UsedShmemSegAddr);
618  return false;
619  }
620 
621  return true;
622 }
#define elog(elevel,...)
Definition: elog.h:224
#define PROTECTIVE_REGION_SIZE
Definition: win32_shmem.c:41
void * ShmemProtectiveRegion
Definition: win32_shmem.c:42
void * UsedShmemSegAddr
Definition: win32_shmem.c:45
static Size UsedShmemSegSize
Definition: win32_shmem.c:46

References Assert, elog, LOG, PROTECTIVE_REGION_SIZE, ShmemProtectiveRegion, UsedShmemSegAddr, and UsedShmemSegSize.

◆ pgwin32_select()

int pgwin32_select ( int  nfds,
fd_set *  readfds,
fd_set *  writefds,
fd_set *  exceptfds,
const struct timeval *  timeout 
)

Definition at line 517 of file socket.c.

518 {
519  WSAEVENT events[FD_SETSIZE * 2]; /* worst case is readfds totally
520  * different from writefds, so
521  * 2*FD_SETSIZE sockets */
522  SOCKET sockets[FD_SETSIZE * 2];
523  int numevents = 0;
524  int i;
525  int r;
526  DWORD timeoutval = WSA_INFINITE;
527  FD_SET outreadfds;
528  FD_SET outwritefds;
529  int nummatches = 0;
530 
531  Assert(exceptfds == NULL);
532 
533  if (pgwin32_poll_signals())
534  return -1;
535 
536  FD_ZERO(&outreadfds);
537  FD_ZERO(&outwritefds);
538 
539  /*
540  * Windows does not guarantee to log an FD_WRITE network event indicating
541  * that more data can be sent unless the previous send() failed with
542  * WSAEWOULDBLOCK. While our caller might well have made such a call, we
543  * cannot assume that here. Therefore, if waiting for write-ready, force
544  * the issue by doing a dummy send(). If the dummy send() succeeds,
545  * assume that the socket is in fact write-ready, and return immediately.
546  * Also, if it fails with something other than WSAEWOULDBLOCK, return a
547  * write-ready indication to let our caller deal with the error condition.
548  */
549  if (writefds != NULL)
550  {
551  for (i = 0; i < writefds->fd_count; i++)
552  {
553  char c;
554  WSABUF buf;
555  DWORD sent;
556 
557  buf.buf = &c;
558  buf.len = 0;
559 
560  r = WSASend(writefds->fd_array[i], &buf, 1, &sent, 0, NULL, NULL);
561  if (r == 0 || WSAGetLastError() != WSAEWOULDBLOCK)
562  FD_SET(writefds->fd_array[i], &outwritefds);
563  }
564 
565  /* If we found any write-ready sockets, just return them immediately */
566  if (outwritefds.fd_count > 0)
567  {
568  memcpy(writefds, &outwritefds, sizeof(fd_set));
569  if (readfds)
570  FD_ZERO(readfds);
571  return outwritefds.fd_count;
572  }
573  }
574 
575 
576  /* Now set up for an actual select */
577 
578  if (timeout != NULL)
579  {
580  /* timeoutval is in milliseconds */
581  timeoutval = timeout->tv_sec * 1000 + timeout->tv_usec / 1000;
582  }
583 
584  if (readfds != NULL)
585  {
586  for (i = 0; i < readfds->fd_count; i++)
587  {
588  events[numevents] = WSACreateEvent();
589  sockets[numevents] = readfds->fd_array[i];
590  numevents++;
591  }
592  }
593  if (writefds != NULL)
594  {
595  for (i = 0; i < writefds->fd_count; i++)
596  {
597  if (!readfds ||
598  !FD_ISSET(writefds->fd_array[i], readfds))
599  {
600  /* If the socket is not in the read list */
601  events[numevents] = WSACreateEvent();
602  sockets[numevents] = writefds->fd_array[i];
603  numevents++;
604  }
605  }
606  }
607 
608  for (i = 0; i < numevents; i++)
609  {
610  int flags = 0;
611 
612  if (readfds && FD_ISSET(sockets[i], readfds))
613  flags |= FD_READ | FD_ACCEPT | FD_CLOSE;
614 
615  if (writefds && FD_ISSET(sockets[i], writefds))
616  flags |= FD_WRITE | FD_CLOSE;
617 
618  if (WSAEventSelect(sockets[i], events[i], flags) != 0)
619  {
621  /* release already-assigned event objects */
622  while (--i >= 0)
623  WSAEventSelect(sockets[i], NULL, 0);
624  for (i = 0; i < numevents; i++)
625  WSACloseEvent(events[i]);
626  return -1;
627  }
628  }
629 
630  events[numevents] = pgwin32_signal_event;
631  r = WaitForMultipleObjectsEx(numevents + 1, events, FALSE, timeoutval, TRUE);
632  if (r != WAIT_TIMEOUT && r != WAIT_IO_COMPLETION && r != (WAIT_OBJECT_0 + numevents))
633  {
634  /*
635  * We scan all events, even those not signaled, in case more than one
636  * event has been tagged but Wait.. can only return one.
637  */
638  WSANETWORKEVENTS resEvents;
639 
640  for (i = 0; i < numevents; i++)
641  {
642  ZeroMemory(&resEvents, sizeof(resEvents));
643  if (WSAEnumNetworkEvents(sockets[i], events[i], &resEvents) != 0)
644  elog(ERROR, "failed to enumerate network events: error code %d",
645  WSAGetLastError());
646  /* Read activity? */
647  if (readfds && FD_ISSET(sockets[i], readfds))
648  {
649  if ((resEvents.lNetworkEvents & FD_READ) ||
650  (resEvents.lNetworkEvents & FD_ACCEPT) ||
651  (resEvents.lNetworkEvents & FD_CLOSE))
652  {
653  FD_SET(sockets[i], &outreadfds);
654 
655  nummatches++;
656  }
657  }
658  /* Write activity? */
659  if (writefds && FD_ISSET(sockets[i], writefds))
660  {
661  if ((resEvents.lNetworkEvents & FD_WRITE) ||
662  (resEvents.lNetworkEvents & FD_CLOSE))
663  {
664  FD_SET(sockets[i], &outwritefds);
665 
666  nummatches++;
667  }
668  }
669  }
670  }
671 
672  /* Clean up all the event objects */
673  for (i = 0; i < numevents; i++)
674  {
675  WSAEventSelect(sockets[i], NULL, 0);
676  WSACloseEvent(events[i]);
677  }
678 
679  if (r == WSA_WAIT_TIMEOUT)
680  {
681  if (readfds)
682  FD_ZERO(readfds);
683  if (writefds)
684  FD_ZERO(writefds);
685  return 0;
686  }
687 
688  /* Signal-like events. */
689  if (r == WAIT_OBJECT_0 + numevents || r == WAIT_IO_COMPLETION)
690  {
692  errno = EINTR;
693  if (readfds)
694  FD_ZERO(readfds);
695  if (writefds)
696  FD_ZERO(writefds);
697  return -1;
698  }
699 
700  /* Overwrite socket sets with our resulting values */
701  if (readfds)
702  memcpy(readfds, &outreadfds, sizeof(fd_set));
703  if (writefds)
704  memcpy(writefds, &outwritefds, sizeof(fd_set));
705  return nummatches;
706 }
char * c
void pgwin32_dispatch_queued_signals(void)
Definition: signal.c:120
#define EINTR
Definition: win32_port.h:374

References Assert, buf, EINTR, elog, ERROR, i, pgwin32_dispatch_queued_signals(), pgwin32_poll_signals(), pgwin32_signal_event, and TranslateSocketError().

◆ pgwin32_send()

int pgwin32_send ( SOCKET  s,
const void *  buf,
int  len,
int  flags 
)

Definition at line 459 of file socket.c.

460 {
461  WSABUF wbuf;
462  int r;
463  DWORD b;
464 
465  if (pgwin32_poll_signals())
466  return -1;
467 
468  wbuf.len = len;
469  wbuf.buf = (char *) buf;
470 
471  /*
472  * Readiness of socket to send data to UDP socket may be not true: socket
473  * can become busy again! So loop until send or error occurs.
474  */
475  for (;;)
476  {
477  r = WSASend(s, &wbuf, 1, &b, flags, NULL, NULL);
478  if (r != SOCKET_ERROR && b > 0)
479  /* Write succeeded right away */
480  return b;
481 
482  if (r == SOCKET_ERROR &&
483  WSAGetLastError() != WSAEWOULDBLOCK)
484  {
486  return -1;
487  }
488 
489  if (pgwin32_noblock)
490  {
491  /*
492  * No data sent, and we are in "emulated non-blocking mode", so
493  * return indicating that we'd block if we were to continue.
494  */
495  errno = EWOULDBLOCK;
496  return -1;
497  }
498 
499  /* No error, zero bytes */
500 
501  if (pgwin32_waitforsinglesocket(s, FD_WRITE | FD_CLOSE, INFINITE) == 0)
502  return -1;
503  }
504 
505  return -1;
506 }

References b, buf, EWOULDBLOCK, len, pgwin32_noblock, pgwin32_poll_signals(), pgwin32_waitforsinglesocket(), and TranslateSocketError().

◆ pgwin32_setenv()

int pgwin32_setenv ( const char *  name,
const char *  value,
int  overwrite 
)

Definition at line 121 of file win32env.c.

122 {
123  int res;
124  char *envstr;
125 
126  /* Error conditions, per POSIX */
127  if (name == NULL || name[0] == '\0' || strchr(name, '=') != NULL ||
128  value == NULL)
129  {
130  errno = EINVAL;
131  return -1;
132  }
133 
134  /* No work if variable exists and we're not to replace it */
135  if (overwrite == 0 && getenv(name) != NULL)
136  return 0;
137 
138  envstr = (char *) malloc(strlen(name) + strlen(value) + 2);
139  if (!envstr) /* not much we can do if no memory */
140  return -1;
141 
142  sprintf(envstr, "%s=%s", name, value);
143 
144  res = pgwin32_putenv(envstr);
145  free(envstr);
146  return res;
147 }
#define malloc(a)
Definition: header.h:50
static struct @155 value
#define sprintf
Definition: port.h:240
static void overwrite(PGconn *conn, Oid lobjId, int start, int len)
Definition: testlo.c:108
int pgwin32_putenv(const char *envval)
Definition: win32env.c:27

References free, malloc, name, overwrite(), pgwin32_putenv(), res, sprintf, and value.

◆ pgwin32_setlocale()

char* pgwin32_setlocale ( int  category,
const char *  locale 
)

Definition at line 172 of file win32setlocale.c.

173 {
174  const char *argument;
175  char *result;
176 
177  if (locale == NULL)
178  argument = NULL;
179  else
181 
182  /* Call the real setlocale() function */
183  result = setlocale(category, argument);
184 
185  /*
186  * setlocale() is specified to return a "char *" that the caller is
187  * forbidden to modify, so casting away the "const" is innocuous.
188  */
189  if (result)
190  result = unconstify(char *, map_locale(locale_map_result, result));
191 
192  return result;
193 }
#define unconstify(underlying_type, expr)
Definition: c.h:1245
static char * locale
Definition: initdb.c:140
#define setlocale(a, b)
Definition: win32_port.h:467
static const char * map_locale(const struct locale_map *map, const char *locale)
static const struct locale_map locale_map_argument[]
static const struct locale_map locale_map_result[]

References locale, locale_map_argument, locale_map_result, map_locale(), setlocale, and unconstify.

◆ pgwin32_signal_initialize()

void pgwin32_signal_initialize ( void  )

Definition at line 79 of file signal.c.

80 {
81  int i;
82  HANDLE signal_thread_handle;
83 
84  InitializeCriticalSection(&pg_signal_crit_sec);
85 
86  for (i = 0; i < PG_SIGNAL_COUNT; i++)
87  {
88  pg_signal_array[i].sa_handler = SIG_DFL;
89  pg_signal_array[i].sa_mask = 0;
90  pg_signal_array[i].sa_flags = 0;
92  }
93  pg_signal_mask = 0;
94  pg_signal_queue = 0;
95 
96  /* Create the global event handle used to flag signals */
97  pgwin32_signal_event = CreateEvent(NULL, TRUE, FALSE, NULL);
98  if (pgwin32_signal_event == NULL)
99  ereport(FATAL,
100  (errmsg_internal("could not create signal event: error code %lu", GetLastError())));
101 
102  /* Create thread for handling signals */
103  signal_thread_handle = CreateThread(NULL, 0, pg_signal_thread, NULL, 0, NULL);
104  if (signal_thread_handle == NULL)
105  ereport(FATAL,
106  (errmsg_internal("could not create signal handler thread")));
107 
108  /* Create console control handle to pick up Ctrl-C etc */
109  if (!SetConsoleCtrlHandler(pg_console_handler, TRUE))
110  ereport(FATAL,
111  (errmsg_internal("could not set console control handler")));
112 }
#define FATAL
Definition: elog.h:41
static DWORD WINAPI pg_signal_thread(LPVOID param)
Definition: signal.c:274
static BOOL WINAPI pg_console_handler(DWORD dwCtrlType)
Definition: signal.c:377
int pg_signal_mask
Definition: signal.c:25

References ereport, errmsg_internal(), FATAL, i, pg_console_handler(), pg_signal_array, PG_SIGNAL_COUNT, pg_signal_crit_sec, pg_signal_defaults, pg_signal_mask, pg_signal_queue, pg_signal_thread(), pgwin32_signal_event, SIG_DFL, and SIG_IGN.

Referenced by InitPostmasterChild(), InitStandaloneProcess(), and PostmasterMain().

◆ pgwin32_socket()

SOCKET pgwin32_socket ( int  af,
int  type,
int  protocol 
)

Definition at line 291 of file socket.c.

292 {
293  SOCKET s;
294  unsigned long on = 1;
295 
296  s = WSASocket(af, type, protocol, NULL, 0, WSA_FLAG_OVERLAPPED);
297  if (s == INVALID_SOCKET)
298  {
300  return INVALID_SOCKET;
301  }
302 
303  if (ioctlsocket(s, FIONBIO, &on))
304  {
306  closesocket(s);
307  return INVALID_SOCKET;
308  }
309  errno = 0;
310 
311  return s;
312 }
#define closesocket
Definition: port.h:349
const char * type

References closesocket, TranslateSocketError(), and type.

◆ pgwin32_unsetenv()

int pgwin32_unsetenv ( const char *  name)

Definition at line 150 of file win32env.c.

151 {
152  int res;
153  char *envbuf;
154 
155  envbuf = (char *) malloc(strlen(name) + 2);
156  if (!envbuf)
157  return -1;
158 
159  sprintf(envbuf, "%s=", name);
160  res = pgwin32_putenv(envbuf);
161  free(envbuf);
162  return res;
163 }

References free, malloc, name, pgwin32_putenv(), res, and sprintf.

◆ pgwin32_waitforsinglesocket()

int pgwin32_waitforsinglesocket ( SOCKET  s,
int  what,
int  timeout 
)

Definition at line 181 of file socket.c.

182 {
183  static HANDLE waitevent = INVALID_HANDLE_VALUE;
184  static SOCKET current_socket = INVALID_SOCKET;
185  static int isUDP = 0;
186  HANDLE events[2];
187  int r;
188 
189  /* Create an event object just once and use it on all future calls */
190  if (waitevent == INVALID_HANDLE_VALUE)
191  {
192  waitevent = CreateEvent(NULL, TRUE, FALSE, NULL);
193 
194  if (waitevent == INVALID_HANDLE_VALUE)
195  ereport(ERROR,
196  (errmsg_internal("could not create socket waiting event: error code %lu", GetLastError())));
197  }
198  else if (!ResetEvent(waitevent))
199  ereport(ERROR,
200  (errmsg_internal("could not reset socket waiting event: error code %lu", GetLastError())));
201 
202  /*
203  * Track whether socket is UDP or not. (NB: most likely, this is both
204  * useless and wrong; there is no reason to think that the behavior of
205  * WSAEventSelect is different for TCP and UDP.)
206  */
207  if (current_socket != s)
208  isUDP = isDataGram(s);
209  current_socket = s;
210 
211  /*
212  * Attach event to socket. NOTE: we must detach it again before
213  * returning, since other bits of code may try to attach other events to
214  * the socket.
215  */
216  if (WSAEventSelect(s, waitevent, what) != 0)
217  {
219  return 0;
220  }
221 
222  events[0] = pgwin32_signal_event;
223  events[1] = waitevent;
224 
225  /*
226  * Just a workaround of unknown locking problem with writing in UDP socket
227  * under high load: Client's pgsql backend sleeps infinitely in
228  * WaitForMultipleObjectsEx, pgstat process sleeps in pgwin32_select().
229  * So, we will wait with small timeout(0.1 sec) and if socket is still
230  * blocked, try WSASend (see comments in pgwin32_select) and wait again.
231  */
232  if ((what & FD_WRITE) && isUDP)
233  {
234  for (;;)
235  {
236  r = WaitForMultipleObjectsEx(2, events, FALSE, 100, TRUE);
237 
238  if (r == WAIT_TIMEOUT)
239  {
240  char c;
241  WSABUF buf;
242  DWORD sent;
243 
244  buf.buf = &c;
245  buf.len = 0;
246 
247  r = WSASend(s, &buf, 1, &sent, 0, NULL, NULL);
248  if (r == 0) /* Completed - means things are fine! */
249  {
250  WSAEventSelect(s, NULL, 0);
251  return 1;
252  }
253  else if (WSAGetLastError() != WSAEWOULDBLOCK)
254  {
256  WSAEventSelect(s, NULL, 0);
257  return 0;
258  }
259  }
260  else
261  break;
262  }
263  }
264  else
265  r = WaitForMultipleObjectsEx(2, events, FALSE, timeout, TRUE);
266 
267  WSAEventSelect(s, NULL, 0);
268 
269  if (r == WAIT_OBJECT_0 || r == WAIT_IO_COMPLETION)
270  {
272  errno = EINTR;
273  return 0;
274  }
275  if (r == WAIT_OBJECT_0 + 1)
276  return 1;
277  if (r == WAIT_TIMEOUT)
278  {
279  errno = EWOULDBLOCK;
280  return 0;
281  }
282  ereport(ERROR,
283  (errmsg_internal("unrecognized return value from WaitForMultipleObjects: %d (error code %lu)", r, GetLastError())));
284  return 0;
285 }
static int isDataGram(SOCKET s)
Definition: socket.c:169

References buf, EINTR, ereport, errmsg_internal(), ERROR, EWOULDBLOCK, isDataGram(), pgwin32_dispatch_queued_signals(), pgwin32_signal_event, and TranslateSocketError().

Referenced by pgwin32_connect(), pgwin32_recv(), and pgwin32_send().

◆ setitimer()

int setitimer ( int  which,
const struct itimerval value,
struct itimerval ovalue 
)

Definition at line 86 of file timer.c.

87 {
88  Assert(value != NULL);
89  Assert(value->it_interval.tv_sec == 0 && value->it_interval.tv_usec == 0);
90  Assert(which == ITIMER_REAL);
91 
92  if (timerThreadHandle == INVALID_HANDLE_VALUE)
93  {
94  /* First call in this backend, create event and the timer thread */
95  timerCommArea.event = CreateEvent(NULL, TRUE, FALSE, NULL);
96  if (timerCommArea.event == NULL)
97  ereport(FATAL,
98  (errmsg_internal("could not create timer event: error code %lu",
99  GetLastError())));
100 
101  MemSet(&timerCommArea.value, 0, sizeof(struct itimerval));
102 
103  InitializeCriticalSection(&timerCommArea.crit_sec);
104 
105  timerThreadHandle = CreateThread(NULL, 0, pg_timer_thread, NULL, 0, NULL);
106  if (timerThreadHandle == INVALID_HANDLE_VALUE)
107  ereport(FATAL,
108  (errmsg_internal("could not create timer thread: error code %lu",
109  GetLastError())));
110  }
111 
112  /* Request the timer thread to change settings */
113  EnterCriticalSection(&timerCommArea.crit_sec);
114  if (ovalue)
115  *ovalue = timerCommArea.value;
117  LeaveCriticalSection(&timerCommArea.crit_sec);
118  SetEvent(timerCommArea.event);
119 
120  return 0;
121 }
#define MemSet(start, val, len)
Definition: c.h:1020
CRITICAL_SECTION crit_sec
Definition: timer.c:27
struct itimerval value
Definition: timer.c:25
HANDLE event
Definition: timer.c:26
static timerCA timerCommArea
Definition: timer.c:30
static DWORD WINAPI pg_timer_thread(LPVOID param)
Definition: timer.c:36
static HANDLE timerThreadHandle
Definition: timer.c:31
#define ITIMER_REAL
Definition: win32_port.h:190

Referenced by do_watch(), fork_process(), and schedule_alarm().

Variable Documentation

◆ pg_signal_mask

PGDLLIMPORT int pg_signal_mask
extern

Definition at line 25 of file signal.c.

Referenced by pgwin32_signal_initialize(), and pqsigprocmask().

◆ pg_signal_queue

PGDLLIMPORT volatile int pg_signal_queue
extern

◆ pgwin32_initial_signal_pipe

PGDLLIMPORT HANDLE pgwin32_initial_signal_pipe
extern

Definition at line 28 of file signal.c.

Referenced by pg_signal_thread().

◆ pgwin32_noblock

PGDLLIMPORT int pgwin32_noblock
extern

Definition at line 28 of file socket.c.

Referenced by pgwin32_recv(), pgwin32_send(), secure_raw_read(), and secure_raw_write().

◆ pgwin32_signal_event