PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
win32.h File Reference
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
#include <process.h>
#include <signal.h>
#include <errno.h>
#include <direct.h>
#include <sys/utime.h>
Include dependency graph for win32.h:

Go to the source code of this file.

Data Structures

struct  timezone
 
struct  itimerval
 

Macros

#define MIN_WINNT   0x0501
 
#define _WIN32_WINNT   MIN_WINNT
 
#define ENABLE_SSPI   1
 
#define _WINSOCKAPI_
 
#define mkdir(a, b)   mkdir(a)
 
#define ftruncate(a, b)   chsize(a,b)
 
#define fsync(fd)   _commit(fd)
 
#define HAVE_FSYNC_WRITETHROUGH
 
#define FSYNC_WRITETHROUGH_IS_FSYNC
 
#define USES_WINSOCK
 
#define PGDLLIMPORT
 
#define PGDLLEXPORT
 
#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 SIGTTIN   21
 
#define SIGTTOU   22 /* Same as SIGABRT -- no problem, I hope */
 
#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 lstat(path, sb)   stat((path), (sb))
 
#define O_DSYNC   0x0080
 
#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 EHOSTUNREACH   WSAEHOSTUNREACH
 
#define ENOTCONN   WSAENOTCONN
 
#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 wcstombs_l   _wcstombs_l
 
#define mbstowcs_l   _mbstowcs_l
 
#define UNBLOCKED_SIGNAL_QUEUE()   (pg_signal_queue & ~pg_signal_mask)
 
#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 putenv(x)   pgwin32_putenv(x)
 
#define unsetenv(x)   pgwin32_unsetenv(x)
 
#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
 

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)
 
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)
 
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 *readfs, 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)
 
const char * pgwin32_socket_strerror (int err)
 
int pgwin32_waitforsinglesocket (SOCKET s, int what, int timeout)
 
int pgwin32_ReserveSharedMemoryRegion (HANDLE)
 
void pgwin32_install_crashdump_handler (void)
 
void _dosmaperr (unsigned long)
 
int pgwin32_putenv (const char *)
 
void pgwin32_unsetenv (const char *)
 
int pgwin32_is_service (void)
 
int pgwin32_is_admin (void)
 

Variables

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

Macro Definition Documentation

#define _WIN32_WINNT   MIN_WINNT

Definition at line 28 of file win32.h.

#define _WINSOCKAPI_

Definition at line 49 of file win32.h.

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

Definition at line 382 of file win32.h.

Referenced by StreamConnection().

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

Definition at line 380 of file win32.h.

Referenced by CheckRADIUSAuth(), ident_inet(), pgstat_init(), pgwin32_bind(), and StreamServerPort().

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

Definition at line 383 of file win32.h.

Referenced by ident_inet(), internal_cancel(), pgstat_init(), and PQconnectPoll().

#define EACCESS   2048

Definition at line 121 of file win32.h.

#define EADDRINUSE   WSAEADDRINUSE

Definition at line 321 of file win32.h.

Referenced by get_errno_symbol(), and TranslateSocketError().

#define EADDRNOTAVAIL   WSAEADDRNOTAVAIL

Definition at line 323 of file win32.h.

Referenced by get_errno_symbol(), and TranslateSocketError().

#define EAFNOSUPPORT   WSAEAFNOSUPPORT
#define ECONNABORTED   WSAECONNABORTED

Definition at line 303 of file win32.h.

Referenced by get_errno_symbol(), and TranslateSocketError().

#define ECONNREFUSED   WSAECONNREFUSED

Definition at line 315 of file win32.h.

Referenced by get_errno_symbol(), and TranslateSocketError().

#define EHOSTUNREACH   WSAEHOSTUNREACH

Definition at line 325 of file win32.h.

Referenced by get_errno_symbol(), and TranslateSocketError().

#define EIDRM   4096
#define EINPROGRESS   WSAEINPROGRESS

Definition at line 307 of file win32.h.

Referenced by get_errno_symbol(), PQconnectPoll(), and TranslateSocketError().

#define EISCONN   WSAEISCONN

Definition at line 309 of file win32.h.

Referenced by get_errno_symbol(), and TranslateSocketError().

#define ENABLE_SSPI   1

Definition at line 36 of file win32.h.

#define ENOBUFS   WSAENOBUFS

Definition at line 311 of file win32.h.

Referenced by get_errno_symbol(), and TranslateSocketError().

#define ENOTCONN   WSAENOTCONN

Definition at line 327 of file win32.h.

Referenced by get_errno_symbol(), and TranslateSocketError().

#define ENOTSOCK   WSAENOTSOCK

Definition at line 317 of file win32.h.

Referenced by get_errno_symbol(), and TranslateSocketError().

#define EOPNOTSUPP   WSAEOPNOTSUPP

Definition at line 319 of file win32.h.

Referenced by get_errno_symbol(), and TranslateSocketError().

#define EPROTONOSUPPORT   WSAEPROTONOSUPPORT

Definition at line 313 of file win32.h.

Referenced by get_errno_symbol(), and TranslateSocketError().

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

Definition at line 78 of file win32.h.

#define ftello (   stream)    ftello64(stream)
#define ftruncate (   a,
 
)    chsize(a,b)
#define GETNCNT   16384

Definition at line 127 of file win32.h.

#define GETPID   262144

Definition at line 130 of file win32.h.

Referenced by IpcSemaphoreGetLastPID().

#define GETVAL   65536

Definition at line 128 of file win32.h.

Referenced by IpcSemaphoreGetValue().

#define HAVE_FSYNC_WRITETHROUGH

Definition at line 77 of file win32.h.

#define HAVE_UNION_SEMUN   1

Definition at line 112 of file win32.h.

#define IPC_CREAT   512

Definition at line 115 of file win32.h.

Referenced by dsm_impl_sysv(), InternalIpcMemoryCreate(), and InternalIpcSemaphoreCreate().

#define IPC_EXCL   1024

Definition at line 116 of file win32.h.

Referenced by dsm_impl_sysv(), InternalIpcMemoryCreate(), and InternalIpcSemaphoreCreate().

#define IPC_NOWAIT   2048

Definition at line 118 of file win32.h.

Referenced by PGSemaphoreTryLock().

#define IPC_PRIVATE   234564

Definition at line 117 of file win32.h.

Referenced by dsm_impl_sysv().

#define IPC_RMID   256
#define IPC_STAT   4096

Definition at line 119 of file win32.h.

Referenced by dsm_impl_sysv(), and PGSharedMemoryIsInUse().

#define isalnum_l   _isalnum_l

Definition at line 342 of file win32.h.

Referenced by pg_wc_isalnum(), and str_initcap().

#define isalpha_l   _isalpha_l

Definition at line 340 of file win32.h.

Referenced by pattern_char_isalpha(), and pg_wc_isalpha().

#define isdigit_l   _isdigit_l

Definition at line 338 of file win32.h.

Referenced by pg_wc_isdigit().

#define isgraph_l   _isgraph_l

Definition at line 348 of file win32.h.

Referenced by pg_wc_isgraph().

#define islower_l   _islower_l

Definition at line 346 of file win32.h.

Referenced by pg_wc_islower().

#define isprint_l   _isprint_l

Definition at line 350 of file win32.h.

Referenced by pg_wc_isprint().

#define ispunct_l   _ispunct_l

Definition at line 352 of file win32.h.

Referenced by pg_wc_ispunct().

#define isspace_l   _isspace_l

Definition at line 354 of file win32.h.

Referenced by pg_wc_isspace().

#define isupper_l   _isupper_l

Definition at line 344 of file win32.h.

Referenced by pg_wc_isupper().

#define iswalnum_l   _iswalnum_l

Definition at line 343 of file win32.h.

Referenced by pg_wc_isalnum(), and str_initcap().

#define iswalpha_l   _iswalpha_l

Definition at line 341 of file win32.h.

Referenced by pg_wc_isalpha().

#define iswdigit_l   _iswdigit_l

Definition at line 339 of file win32.h.

Referenced by pg_wc_isdigit().

#define iswgraph_l   _iswgraph_l

Definition at line 349 of file win32.h.

Referenced by pg_wc_isgraph().

#define iswlower_l   _iswlower_l

Definition at line 347 of file win32.h.

Referenced by pg_wc_islower().

#define iswprint_l   _iswprint_l

Definition at line 351 of file win32.h.

Referenced by pg_wc_isprint().

#define iswpunct_l   _iswpunct_l

Definition at line 353 of file win32.h.

Referenced by pg_wc_ispunct().

#define iswspace_l   _iswspace_l

Definition at line 355 of file win32.h.

Referenced by pg_wc_isspace().

#define iswupper_l   _iswupper_l

Definition at line 345 of file win32.h.

Referenced by pg_wc_isupper().

#define ITIMER_REAL   0

Definition at line 228 of file win32.h.

Referenced by disable_all_timeouts(), schedule_alarm(), and setitimer().

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

Definition at line 381 of file win32.h.

Referenced by pgwin32_listen(), and StreamServerPort().

#define locale_t   _locale_t

Definition at line 333 of file win32.h.

Referenced by pg_newlocale_from_collation().

#define mbstowcs_l   _mbstowcs_l

Definition at line 360 of file win32.h.

#define MIN_WINNT   0x0501

Definition at line 20 of file win32.h.

#define O_DSYNC   0x0080

Definition at line 280 of file win32.h.

Referenced by test_sync().

#define PGDLLEXPORT

Definition at line 104 of file win32.h.

#define PGDLLIMPORT

Definition at line 103 of file win32.h.

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

Definition at line 477 of file win32.h.

Referenced by be_lo_export(), and PostmasterMain().

#define S_IROTH   0

Definition at line 481 of file win32.h.

Referenced by be_lo_export(), and PostmasterMain().

#define S_IWGRP   0

Definition at line 478 of file win32.h.

Referenced by be_lo_export(), be_tls_init(), BeginCopyTo(), and main().

#define S_IWOTH   0

Definition at line 482 of file win32.h.

Referenced by be_lo_export(), BeginCopyTo(), and main().

#define S_IXGRP   0

Definition at line 479 of file win32.h.

Referenced by be_tls_init().

#define S_IXOTH   0

Definition at line 483 of file win32.h.

#define select (   n,
  r,
  w,
  e,
  timeout 
)    pgwin32_select(n, r, w, e, timeout)
#define send (   s,
  buf,
  len,
  flags 
)    pgwin32_send(s, buf, len, flags)
#define SETALL   8192

Definition at line 126 of file win32.h.

#define SETVAL   131072

Definition at line 129 of file win32.h.

Referenced by IpcSemaphoreInitialize().

#define SIG_ERR   ((pqsigfunc)-1)

Definition at line 192 of file win32.h.

Referenced by pgwin32_dispatch_queued_signals(), and pqsignal().

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

Definition at line 199 of file win32.h.

Referenced by pqinitmask(), and set_sig().

#define SIGKILL   9

Definition at line 200 of file win32.h.

Referenced by regression_main(), ServerLoop(), set_sig(), signal_child(), and TerminateChildren().

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

Definition at line 185 of file win32.h.

Referenced by pg_queue_signal(), and pgwin32_dispatch_queued_signals().

#define SIGSTOP   17

Definition at line 203 of file win32.h.

Referenced by HandleChildCrash(), and signal_child().

#define SIGTRAP   5

Definition at line 198 of file win32.h.

Referenced by pqinitmask().

#define SIGTSTP   18

Definition at line 204 of file win32.h.

#define SIGTTOU   22 /* Same as SIGABRT -- no problem, I hope */
#define socket (   af,
  type,
  protocol 
)    pgwin32_socket(af, type, protocol)
#define strcoll_l   _strcoll_l

Definition at line 356 of file win32.h.

Referenced by varstr_cmp(), and varstrfastcmp_locale().

#define strxfrm_l   _strxfrm_l

Definition at line 357 of file win32.h.

Referenced by varstr_abbrev_convert().

#define tolower_l   _tolower_l

Definition at line 334 of file win32.h.

Referenced by pg_wc_tolower(), SB_lower_char(), str_initcap(), and str_tolower().

#define toupper_l   _toupper_l

Definition at line 335 of file win32.h.

Referenced by pg_wc_toupper(), str_initcap(), and str_toupper().

#define towlower_l   _towlower_l

Definition at line 336 of file win32.h.

Referenced by pg_wc_tolower(), str_initcap(), and str_tolower().

#define towupper_l   _towupper_l

Definition at line 337 of file win32.h.

Referenced by pg_wc_toupper(), str_initcap(), and str_toupper().

#define UNBLOCKED_SIGNAL_QUEUE ( )    (pg_signal_queue & ~pg_signal_mask)

Definition at line 369 of file win32.h.

Referenced by pgwin32_dispatch_queued_signals(), and pgwin32_poll_signals().

#define unsetenv (   x)    pgwin32_unsetenv(x)
#define USES_WINSOCK

Definition at line 80 of file win32.h.

#define wcscoll_l   _wcscoll_l

Definition at line 358 of file win32.h.

Referenced by varstr_cmp().

#define wcstombs_l   _wcstombs_l

Definition at line 359 of file win32.h.

#define WIFEXITED (   w)    (((w) & 0XFFFFFF00) == 0)
#define WIFSIGNALED (   w)    (!WIFEXITED(w))
#define WTERMSIG (   w)    (w)

Typedef Documentation

Definition at line 261 of file win32.h.

typedef long key_t

Definition at line 263 of file win32.h.

Definition at line 260 of file win32.h.

Function Documentation

void _dosmaperr ( unsigned  long)

Definition at line 171 of file win32error.c.

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

Referenced by BaseBackup(), copyFile(), dsm_impl_pin_segment(), dsm_impl_unpin_segment(), FileRead(), FileWrite(), getrusage(), and readdir().

172 {
173  int i;
174 
175  if (e == 0)
176  {
177  errno = 0;
178  return;
179  }
180 
181  for (i = 0; i < lengthof(doserrors); i++)
182  {
183  if (doserrors[i].winerr == e)
184  {
185  int doserr = doserrors[i].doserr;
186 
187 #ifndef FRONTEND
188  ereport(DEBUG5,
189  (errmsg_internal("mapped win32 error code %lu to %d",
190  e, doserr)));
191 #elif FRONTEND_DEBUG
192  fprintf(stderr, "mapped win32 error code %lu to %d", e, doserr);
193 #endif
194  errno = doserr;
195  return;
196  }
197  }
198 
199 #ifndef FRONTEND
200  ereport(LOG,
201  (errmsg_internal("unrecognized win32 error code: %lu",
202  e)));
203 #else
204  fprintf(stderr, "unrecognized win32 error code: %lu", e);
205 #endif
206 
207  errno = EINVAL;
208  return;
209 }
DWORD winerr
Definition: win32error.c:22
#define lengthof(array)
Definition: c.h:557
#define LOG
Definition: elog.h:26
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg_internal(const char *fmt,...)
Definition: elog.c:827
e
Definition: preproc-init.c:82
int i
#define DEBUG5
Definition: elog.h:20
static const struct @97 doserrors[]
int doserr
Definition: win32error.c:23
void pg_queue_signal ( int  signum)

Definition at line 210 of file signal.c.

References pg_signal_crit_sec, pg_signal_queue, pgwin32_signal_event, and sigmask.

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

211 {
212  if (signum >= PG_SIGNAL_COUNT || signum <= 0)
213  return;
214 
215  EnterCriticalSection(&pg_signal_crit_sec);
216  pg_signal_queue |= sigmask(signum);
217  LeaveCriticalSection(&pg_signal_crit_sec);
218 
219  SetEvent(pgwin32_signal_event);
220 }
volatile int pg_signal_queue
Definition: signal.c:24
HANDLE pgwin32_signal_event
Definition: signal.c:27
static CRITICAL_SECTION pg_signal_crit_sec
Definition: signal.c:34
#define sigmask(sig)
Definition: win32.h:185
SOCKET pgwin32_accept ( SOCKET  s,
struct sockaddr *  addr,
int *  addrlen 
)

Definition at line 323 of file socket.c.

References NULL, pgwin32_poll_signals(), and TranslateSocketError().

324 {
325  SOCKET rs;
326 
327  /*
328  * Poll for signals, but don't return with EINTR, since we don't handle
329  * that in pqcomm.c
330  */
332 
333  rs = WSAAccept(s, addr, addrlen, NULL, 0);
334  if (rs == INVALID_SOCKET)
335  {
337  return INVALID_SOCKET;
338  }
339  return rs;
340 }
static int pgwin32_poll_signals(void)
Definition: socket.c:144
static void TranslateSocketError(void)
Definition: socket.c:56
#define NULL
Definition: c.h:226
int pgwin32_bind ( SOCKET  s,
struct sockaddr *  addr,
int  addrlen 
)

Definition at line 301 of file socket.c.

References bind, and TranslateSocketError().

302 {
303  int res;
304 
305  res = bind(s, addr, addrlen);
306  if (res < 0)
308  return res;
309 }
#define bind(s, addr, addrlen)
Definition: win32.h:380
static void TranslateSocketError(void)
Definition: socket.c:56
int pgwin32_connect ( SOCKET  s,
const struct sockaddr *  name,
int  namelen 
)

Definition at line 345 of file socket.c.

References NULL, pgwin32_waitforsinglesocket(), and TranslateSocketError().

346 {
347  int r;
348 
349  r = WSAConnect(s, addr, addrlen, NULL, NULL, NULL, NULL);
350  if (r == 0)
351  return 0;
352 
353  if (WSAGetLastError() != WSAEWOULDBLOCK)
354  {
356  return -1;
357  }
358 
359  while (pgwin32_waitforsinglesocket(s, FD_CONNECT, INFINITE) == 0)
360  {
361  /* Loop endlessly as long as we are just delivering signals */
362  }
363 
364  return 0;
365 }
int pgwin32_waitforsinglesocket(SOCKET s, int what, int timeout)
Definition: socket.c:168
static void TranslateSocketError(void)
Definition: socket.c:56
#define NULL
Definition: c.h:226
HANDLE pgwin32_create_signal_listener ( pid_t  pid)

Definition at line 181 of file signal.c.

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

182 {
183  char pipename[128];
184  HANDLE pipe;
185 
186  snprintf(pipename, sizeof(pipename), "\\\\.\\pipe\\pgsignal_%u", (int) pid);
187 
188  pipe = CreateNamedPipe(pipename, PIPE_ACCESS_DUPLEX,
189  PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
190  PIPE_UNLIMITED_INSTANCES, 16, 16, 1000, NULL);
191 
192  if (pipe == INVALID_HANDLE_VALUE)
193  ereport(ERROR,
194  (errmsg("could not create signal listener pipe for PID %d: error code %lu",
195  (int) pid, GetLastError())));
196 
197  return pipe;
198 }
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define NULL
Definition: c.h:226
int errmsg(const char *fmt,...)
Definition: elog.c:797
void pgwin32_dispatch_queued_signals ( void  )

Definition at line 107 of file signal.c.

References i, pg_signal_array, 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(), pqsigsetmask(), and WaitEventSetWait().

108 {
109  int exec_mask;
110 
111  EnterCriticalSection(&pg_signal_crit_sec);
112  while ((exec_mask = UNBLOCKED_SIGNAL_QUEUE()) != 0)
113  {
114  /* One or more unblocked signals queued for execution */
115  int i;
116 
117  for (i = 1; i < PG_SIGNAL_COUNT; i++)
118  {
119  if (exec_mask & sigmask(i))
120  {
121  /* Execute this signal */
123 
124  if (sig == SIG_DFL)
125  sig = pg_signal_defaults[i];
126  pg_signal_queue &= ~sigmask(i);
127  if (sig != SIG_ERR && sig != SIG_IGN && sig != SIG_DFL)
128  {
129  LeaveCriticalSection(&pg_signal_crit_sec);
130  sig(i);
131  EnterCriticalSection(&pg_signal_crit_sec);
132  break; /* Restart outer loop, in case signal mask or
133  * queue has been modified inside signal
134  * handler */
135  }
136  }
137  }
138  }
139  ResetEvent(pgwin32_signal_event);
140  LeaveCriticalSection(&pg_signal_crit_sec);
141 }
volatile int pg_signal_queue
Definition: signal.c:24
HANDLE pgwin32_signal_event
Definition: signal.c:27
#define SIG_IGN
Definition: win32.h:193
static CRITICAL_SECTION pg_signal_crit_sec
Definition: signal.c:34
void(* pqsigfunc)(int signo)
Definition: port.h:470
#define SIG_ERR
Definition: win32.h:192
#define UNBLOCKED_SIGNAL_QUEUE()
Definition: win32.h:369
#define sigmask(sig)
Definition: win32.h:185
static pqsigfunc pg_signal_defaults[PG_SIGNAL_COUNT]
Definition: signal.c:38
static int sig
Definition: pg_ctl.c:79
#define SIG_DFL
Definition: win32.h:191
static pqsigfunc pg_signal_array[PG_SIGNAL_COUNT]
Definition: signal.c:37
int i
void pgwin32_install_crashdump_handler ( void  )

Definition at line 181 of file crashdump.c.

References crashDumpHandler().

Referenced by main().

182 {
183  SetUnhandledExceptionFilter(crashDumpHandler);
184 }
static LONG WINAPI crashDumpHandler(struct _EXCEPTION_POINTERS *pExceptionInfo)
Definition: crashdump.c:93
int pgwin32_is_admin ( void  )

Definition at line 54 of file win32security.c.

References _, BOOL(), FALSE, free, log_error, NULL, pgwin32_get_dynamic_tokeninfo(), success, and TRUE.

Referenced by check_root().

55 {
56  HANDLE AccessToken;
57  char *InfoBuffer = NULL;
58  char errbuf[256];
59  PTOKEN_GROUPS Groups;
60  PSID AdministratorsSid;
61  PSID PowerUsersSid;
62  SID_IDENTIFIER_AUTHORITY NtAuthority = {SECURITY_NT_AUTHORITY};
63  UINT x;
64  BOOL success;
65 
66  if (!OpenProcessToken(GetCurrentProcess(), TOKEN_READ, &AccessToken))
67  {
68  log_error(_("could not open process token: error code %lu\n"),
69  GetLastError());
70  exit(1);
71  }
72 
73  if (!pgwin32_get_dynamic_tokeninfo(AccessToken, TokenGroups,
74  &InfoBuffer, errbuf, sizeof(errbuf)))
75  {
76  log_error("%s", errbuf);
77  exit(1);
78  }
79 
80  Groups = (PTOKEN_GROUPS) InfoBuffer;
81 
82  CloseHandle(AccessToken);
83 
84  if (!AllocateAndInitializeSid(&NtAuthority, 2,
85  SECURITY_BUILTIN_DOMAIN_RID,
86  DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0,
87  0, &AdministratorsSid))
88  {
89  log_error(_("could not get SID for Administrators group: error code %lu\n"),
90  GetLastError());
91  exit(1);
92  }
93 
94  if (!AllocateAndInitializeSid(&NtAuthority, 2,
95  SECURITY_BUILTIN_DOMAIN_RID,
96  DOMAIN_ALIAS_RID_POWER_USERS, 0, 0, 0, 0, 0,
97  0, &PowerUsersSid))
98  {
99  log_error(_("could not get SID for PowerUsers group: error code %lu\n"),
100  GetLastError());
101  exit(1);
102  }
103 
104  success = FALSE;
105 
106  for (x = 0; x < Groups->GroupCount; x++)
107  {
108  if ((EqualSid(AdministratorsSid, Groups->Groups[x].Sid) &&
109  (Groups->Groups[x].Attributes & SE_GROUP_ENABLED)) ||
110  (EqualSid(PowerUsersSid, Groups->Groups[x].Sid) &&
111  (Groups->Groups[x].Attributes & SE_GROUP_ENABLED)))
112  {
113  success = TRUE;
114  break;
115  }
116  }
117 
118  free(InfoBuffer);
119  FreeSid(AdministratorsSid);
120  FreeSid(PowerUsersSid);
121  return success;
122 }
#define log_error(str, param)
Definition: exec.c:31
#define FALSE
Definition: c.h:218
static bool success
Definition: pg_basebackup.c:99
#define free(a)
Definition: header.h:60
#define NULL
Definition: c.h:226
#define TRUE
Definition: c.h:214
typedef BOOL(WINAPI *MINIDUMPWRITEDUMP)(HANDLE hProcess
#define _(x)
Definition: elog.c:84
static BOOL pgwin32_get_dynamic_tokeninfo(HANDLE token, TOKEN_INFORMATION_CLASS class, char **InfoBuffer, char *errbuf, int errsize)
int pgwin32_is_service ( void  )

Definition at line 143 of file win32security.c.

References free, NULL, and pgwin32_get_dynamic_tokeninfo().

Referenced by send_message_to_server_log(), and write_stderr().

144 {
145  static int _is_service = -1;
146  HANDLE AccessToken;
147  char *InfoBuffer = NULL;
148  char errbuf[256];
149  PTOKEN_GROUPS Groups;
150  PTOKEN_USER User;
151  PSID ServiceSid;
152  PSID LocalSystemSid;
153  SID_IDENTIFIER_AUTHORITY NtAuthority = {SECURITY_NT_AUTHORITY};
154  UINT x;
155 
156  /* Only check the first time */
157  if (_is_service != -1)
158  return _is_service;
159 
160  if (!OpenProcessToken(GetCurrentProcess(), TOKEN_READ, &AccessToken))
161  {
162  fprintf(stderr, "could not open process token: error code %lu\n",
163  GetLastError());
164  return -1;
165  }
166 
167  /* First check for local system */
168  if (!pgwin32_get_dynamic_tokeninfo(AccessToken, TokenUser, &InfoBuffer,
169  errbuf, sizeof(errbuf)))
170  {
171  fprintf(stderr, "%s", errbuf);
172  return -1;
173  }
174 
175  User = (PTOKEN_USER) InfoBuffer;
176 
177  if (!AllocateAndInitializeSid(&NtAuthority, 1,
178  SECURITY_LOCAL_SYSTEM_RID, 0, 0, 0, 0, 0, 0, 0,
179  &LocalSystemSid))
180  {
181  fprintf(stderr, "could not get SID for local system account\n");
182  CloseHandle(AccessToken);
183  return -1;
184  }
185 
186  if (EqualSid(LocalSystemSid, User->User.Sid))
187  {
188  FreeSid(LocalSystemSid);
189  free(InfoBuffer);
190  CloseHandle(AccessToken);
191  _is_service = 1;
192  return _is_service;
193  }
194 
195  FreeSid(LocalSystemSid);
196  free(InfoBuffer);
197 
198  /* Now check for group SID */
199  if (!pgwin32_get_dynamic_tokeninfo(AccessToken, TokenGroups, &InfoBuffer,
200  errbuf, sizeof(errbuf)))
201  {
202  fprintf(stderr, "%s", errbuf);
203  return -1;
204  }
205 
206  Groups = (PTOKEN_GROUPS) InfoBuffer;
207 
208  if (!AllocateAndInitializeSid(&NtAuthority, 1,
209  SECURITY_SERVICE_RID, 0, 0, 0, 0, 0, 0, 0,
210  &ServiceSid))
211  {
212  fprintf(stderr, "could not get SID for service group\n");
213  free(InfoBuffer);
214  CloseHandle(AccessToken);
215  return -1;
216  }
217 
218  _is_service = 0;
219  for (x = 0; x < Groups->GroupCount; x++)
220  {
221  if (EqualSid(ServiceSid, Groups->Groups[x].Sid))
222  {
223  _is_service = 1;
224  break;
225  }
226  }
227 
228  free(InfoBuffer);
229  FreeSid(ServiceSid);
230 
231  CloseHandle(AccessToken);
232 
233  return _is_service;
234 }
#define free(a)
Definition: header.h:60
#define NULL
Definition: c.h:226
static BOOL pgwin32_get_dynamic_tokeninfo(HANDLE token, TOKEN_INFORMATION_CLASS class, char **InfoBuffer, char *errbuf, int errsize)
int pgwin32_listen ( SOCKET  s,
int  backlog 
)

Definition at line 312 of file socket.c.

References listen, and TranslateSocketError().

313 {
314  int res;
315 
316  res = listen(s, backlog);
317  if (res < 0)
319  return res;
320 }
static void TranslateSocketError(void)
Definition: socket.c:56
#define listen(s, backlog)
Definition: win32.h:381
int pgwin32_putenv ( const char *  )

Definition at line 20 of file win32env.c.

References BOOL(), free, i, and NULL.

Referenced by pgwin32_unsetenv().

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

Definition at line 368 of file socket.c.

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

369 {
370  WSABUF wbuf;
371  int r;
372  DWORD b;
373  DWORD flags = f;
374  int n;
375 
376  if (pgwin32_poll_signals())
377  return -1;
378 
379  wbuf.len = len;
380  wbuf.buf = buf;
381 
382  r = WSARecv(s, &wbuf, 1, &b, &flags, NULL, NULL);
383  if (r != SOCKET_ERROR)
384  return b; /* success */
385 
386  if (WSAGetLastError() != WSAEWOULDBLOCK)
387  {
389  return -1;
390  }
391 
392  if (pgwin32_noblock)
393  {
394  /*
395  * No data received, and we are in "emulated non-blocking mode", so
396  * return indicating that we'd block if we were to continue.
397  */
398  errno = EWOULDBLOCK;
399  return -1;
400  }
401 
402  /* We're in blocking mode, so wait for data */
403 
404  for (n = 0; n < 5; n++)
405  {
406  if (pgwin32_waitforsinglesocket(s, FD_READ | FD_CLOSE | FD_ACCEPT,
407  INFINITE) == 0)
408  return -1; /* errno already set */
409 
410  r = WSARecv(s, &wbuf, 1, &b, &flags, NULL, NULL);
411  if (r != SOCKET_ERROR)
412  return b; /* success */
413  if (WSAGetLastError() != WSAEWOULDBLOCK)
414  {
416  return -1;
417  }
418 
419  /*
420  * There seem to be cases on win2k (at least) where WSARecv can return
421  * WSAEWOULDBLOCK even when pgwin32_waitforsinglesocket claims the
422  * socket is readable. In this case, just sleep for a moment and try
423  * again. We try up to 5 times - if it fails more than that it's not
424  * likely to ever come back.
425  */
426  pg_usleep(10000);
427  }
428  ereport(NOTICE,
429  (errmsg_internal("could not read from ready socket (after retries)")));
430  errno = EWOULDBLOCK;
431  return -1;
432 }
#define EWOULDBLOCK
Definition: win32.h:301
int pgwin32_noblock
Definition: socket.c:28
void pg_usleep(long microsec)
Definition: signal.c:53
static int pgwin32_poll_signals(void)
Definition: socket.c:144
static char * buf
Definition: pg_test_fsync.c:65
int pgwin32_waitforsinglesocket(SOCKET s, int what, int timeout)
Definition: socket.c:168
#define ereport(elevel, rest)
Definition: elog.h:122
static void TranslateSocketError(void)
Definition: socket.c:56
#define NOTICE
Definition: elog.h:37
int errmsg_internal(const char *fmt,...)
Definition: elog.c:827
#define NULL
Definition: c.h:226
int pgwin32_ReserveSharedMemoryRegion ( HANDLE  )

Definition at line 402 of file win32_shmem.c.

References Assert, elog, LOG, NULL, UsedShmemSegAddr, and UsedShmemSegSize.

403 {
404  void *address;
405 
407  Assert(UsedShmemSegSize != 0);
408 
409  address = VirtualAllocEx(hChild, UsedShmemSegAddr, UsedShmemSegSize,
410  MEM_RESERVE, PAGE_READWRITE);
411  if (address == NULL)
412  {
413  /* Don't use FATAL since we're running in the postmaster */
414  elog(LOG, "could not reserve shared memory region (addr=%p) for child %p: error code %lu",
415  UsedShmemSegAddr, hChild, GetLastError());
416  return false;
417  }
418  if (address != UsedShmemSegAddr)
419  {
420  /*
421  * Should never happen - in theory if allocation granularity causes
422  * strange effects it could, so check just in case.
423  *
424  * Don't use FATAL since we're running in the postmaster.
425  */
426  elog(LOG, "reserved shared memory region got incorrect address %p, expected %p",
427  address, UsedShmemSegAddr);
428  VirtualFreeEx(hChild, address, 0, MEM_RELEASE);
429  return false;
430  }
431 
432  return true;
433 }
#define LOG
Definition: elog.h:26
static Size UsedShmemSegSize
Definition: win32_shmem.c:22
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:670
#define elog
Definition: elog.h:219
void * UsedShmemSegAddr
Definition: win32_shmem.c:21
int pgwin32_select ( int  nfds,
fd_set *  readfs,
fd_set *  writefds,
fd_set *  exceptfds,
const struct timeval *  timeout 
)

Definition at line 503 of file socket.c.

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

504 {
505  WSAEVENT events[FD_SETSIZE * 2]; /* worst case is readfds totally
506  * different from writefds, so
507  * 2*FD_SETSIZE sockets */
508  SOCKET sockets[FD_SETSIZE * 2];
509  int numevents = 0;
510  int i;
511  int r;
512  DWORD timeoutval = WSA_INFINITE;
513  FD_SET outreadfds;
514  FD_SET outwritefds;
515  int nummatches = 0;
516 
517  Assert(exceptfds == NULL);
518 
519  if (pgwin32_poll_signals())
520  return -1;
521 
522  FD_ZERO(&outreadfds);
523  FD_ZERO(&outwritefds);
524 
525  /*
526  * Write FDs are different in the way that it is only flagged by
527  * WSASelectEvent() if we have tried to write to them first. So try an
528  * empty write
529  */
530  if (writefds)
531  {
532  for (i = 0; i < writefds->fd_count; i++)
533  {
534  char c;
535  WSABUF buf;
536  DWORD sent;
537 
538  buf.buf = &c;
539  buf.len = 0;
540 
541  r = WSASend(writefds->fd_array[i], &buf, 1, &sent, 0, NULL, NULL);
542  if (r == 0) /* Completed - means things are fine! */
543  FD_SET(writefds->fd_array[i], &outwritefds);
544 
545  else
546  { /* Not completed */
547  if (WSAGetLastError() != WSAEWOULDBLOCK)
548 
549  /*
550  * Not completed, and not just "would block", so an error
551  * occurred
552  */
553  FD_SET(writefds->fd_array[i], &outwritefds);
554  }
555  }
556  if (outwritefds.fd_count > 0)
557  {
558  memcpy(writefds, &outwritefds, sizeof(fd_set));
559  if (readfds)
560  FD_ZERO(readfds);
561  return outwritefds.fd_count;
562  }
563  }
564 
565 
566  /* Now set up for an actual select */
567 
568  if (timeout != NULL)
569  {
570  /* timeoutval is in milliseconds */
571  timeoutval = timeout->tv_sec * 1000 + timeout->tv_usec / 1000;
572  }
573 
574  if (readfds != NULL)
575  {
576  for (i = 0; i < readfds->fd_count; i++)
577  {
578  events[numevents] = WSACreateEvent();
579  sockets[numevents] = readfds->fd_array[i];
580  numevents++;
581  }
582  }
583  if (writefds != NULL)
584  {
585  for (i = 0; i < writefds->fd_count; i++)
586  {
587  if (!readfds ||
588  !FD_ISSET(writefds->fd_array[i], readfds))
589  {
590  /* If the socket is not in the read list */
591  events[numevents] = WSACreateEvent();
592  sockets[numevents] = writefds->fd_array[i];
593  numevents++;
594  }
595  }
596  }
597 
598  for (i = 0; i < numevents; i++)
599  {
600  int flags = 0;
601 
602  if (readfds && FD_ISSET(sockets[i], readfds))
603  flags |= FD_READ | FD_ACCEPT | FD_CLOSE;
604 
605  if (writefds && FD_ISSET(sockets[i], writefds))
606  flags |= FD_WRITE | FD_CLOSE;
607 
608  if (WSAEventSelect(sockets[i], events[i], flags) != 0)
609  {
611  /* release already-assigned event objects */
612  while (--i >= 0)
613  WSAEventSelect(sockets[i], NULL, 0);
614  for (i = 0; i < numevents; i++)
615  WSACloseEvent(events[i]);
616  return -1;
617  }
618  }
619 
620  events[numevents] = pgwin32_signal_event;
621  r = WaitForMultipleObjectsEx(numevents + 1, events, FALSE, timeoutval, TRUE);
622  if (r != WAIT_TIMEOUT && r != WAIT_IO_COMPLETION && r != (WAIT_OBJECT_0 + numevents))
623  {
624  /*
625  * We scan all events, even those not signalled, in case more than one
626  * event has been tagged but Wait.. can only return one.
627  */
628  WSANETWORKEVENTS resEvents;
629 
630  for (i = 0; i < numevents; i++)
631  {
632  ZeroMemory(&resEvents, sizeof(resEvents));
633  if (WSAEnumNetworkEvents(sockets[i], events[i], &resEvents) != 0)
634  elog(ERROR, "failed to enumerate network events: error code %u",
635  WSAGetLastError());
636  /* Read activity? */
637  if (readfds && FD_ISSET(sockets[i], readfds))
638  {
639  if ((resEvents.lNetworkEvents & FD_READ) ||
640  (resEvents.lNetworkEvents & FD_ACCEPT) ||
641  (resEvents.lNetworkEvents & FD_CLOSE))
642  {
643  FD_SET(sockets[i], &outreadfds);
644 
645  nummatches++;
646  }
647  }
648  /* Write activity? */
649  if (writefds && FD_ISSET(sockets[i], writefds))
650  {
651  if ((resEvents.lNetworkEvents & FD_WRITE) ||
652  (resEvents.lNetworkEvents & FD_CLOSE))
653  {
654  FD_SET(sockets[i], &outwritefds);
655 
656  nummatches++;
657  }
658  }
659  }
660  }
661 
662  /* Clean up all the event objects */
663  for (i = 0; i < numevents; i++)
664  {
665  WSAEventSelect(sockets[i], NULL, 0);
666  WSACloseEvent(events[i]);
667  }
668 
669  if (r == WSA_WAIT_TIMEOUT)
670  {
671  if (readfds)
672  FD_ZERO(readfds);
673  if (writefds)
674  FD_ZERO(writefds);
675  return 0;
676  }
677 
678  /* Signal-like events. */
679  if (r == WAIT_OBJECT_0 + numevents || r == WAIT_IO_COMPLETION)
680  {
682  errno = EINTR;
683  if (readfds)
684  FD_ZERO(readfds);
685  if (writefds)
686  FD_ZERO(writefds);
687  return -1;
688  }
689 
690  /* Overwrite socket sets with our resulting values */
691  if (readfds)
692  memcpy(readfds, &outreadfds, sizeof(fd_set));
693  if (writefds)
694  memcpy(writefds, &outwritefds, sizeof(fd_set));
695  return nummatches;
696 }
HANDLE pgwin32_signal_event
Definition: signal.c:27
void pgwin32_dispatch_queued_signals(void)
Definition: signal.c:107
#define ERROR
Definition: elog.h:43
#define FALSE
Definition: c.h:218
static int pgwin32_poll_signals(void)
Definition: socket.c:144
char * c
static char * buf
Definition: pg_test_fsync.c:65
static void TranslateSocketError(void)
Definition: socket.c:56
#define EINTR
Definition: win32.h:295
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:670
int i
#define TRUE
Definition: c.h:214
#define elog
Definition: elog.h:219
int pgwin32_send ( SOCKET  s,
const void *  buf,
int  len,
int  flags 
)

Definition at line 445 of file socket.c.

References EWOULDBLOCK, NULL, pgwin32_noblock, pgwin32_poll_signals(), pgwin32_waitforsinglesocket(), and TranslateSocketError().

446 {
447  WSABUF wbuf;
448  int r;
449  DWORD b;
450 
451  if (pgwin32_poll_signals())
452  return -1;
453 
454  wbuf.len = len;
455  wbuf.buf = (char *) buf;
456 
457  /*
458  * Readiness of socket to send data to UDP socket may be not true: socket
459  * can become busy again! So loop until send or error occurs.
460  */
461  for (;;)
462  {
463  r = WSASend(s, &wbuf, 1, &b, flags, NULL, NULL);
464  if (r != SOCKET_ERROR && b > 0)
465  /* Write succeeded right away */
466  return b;
467 
468  if (r == SOCKET_ERROR &&
469  WSAGetLastError() != WSAEWOULDBLOCK)
470  {
472  return -1;
473  }
474 
475  if (pgwin32_noblock)
476  {
477  /*
478  * No data sent, and we are in "emulated non-blocking mode", so
479  * return indicating that we'd block if we were to continue.
480  */
481  errno = EWOULDBLOCK;
482  return -1;
483  }
484 
485  /* No error, zero bytes (win2000+) or error+WSAEWOULDBLOCK (<=nt4) */
486 
487  if (pgwin32_waitforsinglesocket(s, FD_WRITE | FD_CLOSE, INFINITE) == 0)
488  return -1;
489  }
490 
491  return -1;
492 }
#define EWOULDBLOCK
Definition: win32.h:301
int pgwin32_noblock
Definition: socket.c:28
static int pgwin32_poll_signals(void)
Definition: socket.c:144
static char * buf
Definition: pg_test_fsync.c:65
int pgwin32_waitforsinglesocket(SOCKET s, int what, int timeout)
Definition: socket.c:168
static void TranslateSocketError(void)
Definition: socket.c:56
#define NULL
Definition: c.h:226
void pgwin32_signal_initialize ( void  )

Definition at line 68 of file signal.c.

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

Referenced by main().

69 {
70  int i;
71  HANDLE signal_thread_handle;
72 
73  InitializeCriticalSection(&pg_signal_crit_sec);
74 
75  for (i = 0; i < PG_SIGNAL_COUNT; i++)
76  {
79  }
80  pg_signal_mask = 0;
81  pg_signal_queue = 0;
82 
83  /* Create the global event handle used to flag signals */
84  pgwin32_signal_event = CreateEvent(NULL, TRUE, FALSE, NULL);
86  ereport(FATAL,
87  (errmsg_internal("could not create signal event: error code %lu", GetLastError())));
88 
89  /* Create thread for handling signals */
90  signal_thread_handle = CreateThread(NULL, 0, pg_signal_thread, NULL, 0, NULL);
91  if (signal_thread_handle == NULL)
92  ereport(FATAL,
93  (errmsg_internal("could not create signal handler thread")));
94 
95  /* Create console control handle to pick up Ctrl-C etc */
96  if (!SetConsoleCtrlHandler(pg_console_handler, TRUE))
97  ereport(FATAL,
98  (errmsg_internal("could not set console control handler")));
99 }
int pg_signal_mask
Definition: signal.c:25
static BOOL WINAPI pg_console_handler(DWORD dwCtrlType)
Definition: signal.c:348
volatile int pg_signal_queue
Definition: signal.c:24
HANDLE pgwin32_signal_event
Definition: signal.c:27
#define SIG_IGN
Definition: win32.h:193
#define FALSE
Definition: c.h:218
#define FATAL
Definition: elog.h:52
static CRITICAL_SECTION pg_signal_crit_sec
Definition: signal.c:34
#define ereport(elevel, rest)
Definition: elog.h:122
static pqsigfunc pg_signal_defaults[PG_SIGNAL_COUNT]
Definition: signal.c:38
#define SIG_DFL
Definition: win32.h:191
int errmsg_internal(const char *fmt,...)
Definition: elog.c:827
#define NULL
Definition: c.h:226
static pqsigfunc pg_signal_array[PG_SIGNAL_COUNT]
Definition: signal.c:37
int i
static DWORD WINAPI pg_signal_thread(LPVOID param)
Definition: signal.c:254
#define TRUE
Definition: c.h:214
SOCKET pgwin32_socket ( int  af,
int  type,
int  protocol 
)

Definition at line 278 of file socket.c.

References NULL, and TranslateSocketError().

279 {
280  SOCKET s;
281  unsigned long on = 1;
282 
283  s = WSASocket(af, type, protocol, NULL, 0, WSA_FLAG_OVERLAPPED);
284  if (s == INVALID_SOCKET)
285  {
287  return INVALID_SOCKET;
288  }
289 
290  if (ioctlsocket(s, FIONBIO, &on))
291  {
293  return INVALID_SOCKET;
294  }
295  errno = 0;
296 
297  return s;
298 }
static void TranslateSocketError(void)
Definition: socket.c:56
#define NULL
Definition: c.h:226
const char* pgwin32_socket_strerror ( int  err)

Definition at line 705 of file socket.c.

References ereport, errmsg_internal(), FATAL, NULL, and wserrbuf.

Referenced by useful_strerror().

706 {
707  static HANDLE handleDLL = INVALID_HANDLE_VALUE;
708 
709  if (handleDLL == INVALID_HANDLE_VALUE)
710  {
711  handleDLL = LoadLibraryEx("netmsg.dll", NULL, DONT_RESOLVE_DLL_REFERENCES | LOAD_LIBRARY_AS_DATAFILE);
712  if (handleDLL == NULL)
713  ereport(FATAL,
714  (errmsg_internal("could not load netmsg.dll: error code %lu", GetLastError())));
715  }
716 
717  ZeroMemory(&wserrbuf, sizeof(wserrbuf));
718  if (FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS |
719  FORMAT_MESSAGE_FROM_SYSTEM |
720  FORMAT_MESSAGE_FROM_HMODULE,
721  handleDLL,
722  err,
723  MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
724  wserrbuf,
725  sizeof(wserrbuf) - 1,
726  NULL) == 0)
727  {
728  /* Failed to get id */
729  sprintf(wserrbuf, "unrecognized winsock error %d", err);
730  }
731  return wserrbuf;
732 }
#define FATAL
Definition: elog.h:52
static char wserrbuf[256]
Definition: socket.c:703
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg_internal(const char *fmt,...)
Definition: elog.c:827
#define NULL
Definition: c.h:226
void pgwin32_unsetenv ( const char *  )

Definition at line 114 of file win32env.c.

References free, malloc, and pgwin32_putenv().

115 {
116  char *envbuf;
117 
118  envbuf = (char *) malloc(strlen(name) + 2);
119  if (!envbuf)
120  return;
121 
122  sprintf(envbuf, "%s=", name);
123  pgwin32_putenv(envbuf);
124  free(envbuf);
125 }
#define malloc(a)
Definition: header.h:45
#define free(a)
Definition: header.h:60
const char * name
Definition: encode.c:521
int pgwin32_putenv(const char *envval)
Definition: win32env.c:20
int pgwin32_waitforsinglesocket ( SOCKET  s,
int  what,
int  timeout 
)

Definition at line 168 of file socket.c.

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

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

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

Definition at line 86 of file timer.c.

References Assert, timerCA::crit_sec, ereport, errmsg_internal(), timerCA::event, FALSE, FATAL, itimerval::it_interval, ITIMER_REAL, MemSet, NULL, pg_timer_thread(), timerThreadHandle, TRUE, timerCA::value, and value.

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

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 ITIMER_REAL
Definition: win32.h:228
static struct @76 value
#define MemSet(start, val, len)
Definition: c.h:852
HANDLE event
Definition: timer.c:26
static DWORD WINAPI pg_timer_thread(LPVOID param)
Definition: timer.c:36
#define FALSE
Definition: c.h:218
#define FATAL
Definition: elog.h:52
static timerCA timerCommArea
Definition: timer.c:30
#define ereport(elevel, rest)
Definition: elog.h:122
struct itimerval value
Definition: timer.c:25
int errmsg_internal(const char *fmt,...)
Definition: elog.c:827
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:670
CRITICAL_SECTION crit_sec
Definition: timer.c:27
struct timeval it_interval
Definition: win32.h:231
static HANDLE timerThreadHandle
Definition: timer.c:31
#define TRUE
Definition: c.h:214

Variable Documentation

PGDLLIMPORT int pg_signal_mask

Definition at line 25 of file signal.c.

Referenced by pgwin32_signal_initialize(), and pqsigsetmask().

PGDLLIMPORT volatile int pg_signal_queue
HANDLE pgwin32_initial_signal_pipe

Definition at line 28 of file signal.c.

Referenced by pg_signal_thread().

int pgwin32_noblock