PostgreSQL Source Code  git master
signal.c File Reference
#include "postgres.h"
#include "libpq/pqsignal.h"
Include dependency graph for signal.c:

Go to the source code of this file.

Functions

static DWORD WINAPI pg_signal_thread (LPVOID param)
 
static BOOL WINAPI pg_console_handler (DWORD dwCtrlType)
 
void pg_usleep (long microsec)
 
void pgwin32_signal_initialize (void)
 
void pgwin32_dispatch_queued_signals (void)
 
int pqsigsetmask (int mask)
 
pqsigfunc pqsignal (int signum, pqsigfunc handler)
 
HANDLE pgwin32_create_signal_listener (pid_t pid)
 
void pg_queue_signal (int signum)
 

Variables

volatile int pg_signal_queue
 
int pg_signal_mask
 
HANDLE pgwin32_signal_event
 
HANDLE pgwin32_initial_signal_pipe = INVALID_HANDLE_VALUE
 
static CRITICAL_SECTION pg_signal_crit_sec
 
static pqsigfunc pg_signal_array [PG_SIGNAL_COUNT]
 
static pqsigfunc pg_signal_defaults [PG_SIGNAL_COUNT]
 

Function Documentation

◆ pg_console_handler()

static BOOL WINAPI pg_console_handler ( DWORD  dwCtrlType)
static

Definition at line 343 of file signal.c.

344 {
345  if (dwCtrlType == CTRL_C_EVENT ||
346  dwCtrlType == CTRL_BREAK_EVENT ||
347  dwCtrlType == CTRL_CLOSE_EVENT ||
348  dwCtrlType == CTRL_SHUTDOWN_EVENT)
349  {
350  pg_queue_signal(SIGINT);
351  return TRUE;
352  }
353  return FALSE;
354 }
void pg_queue_signal(int signum)
Definition: signal.c:225

References pg_queue_signal().

Referenced by pgwin32_signal_initialize().

◆ pg_queue_signal()

void pg_queue_signal ( int  signum)

Definition at line 225 of file signal.c.

226 {
227  Assert(pgwin32_signal_event != NULL);
228  if (signum >= PG_SIGNAL_COUNT || signum <= 0)
229  return; /* ignore any bad signal number */
230 
231  EnterCriticalSection(&pg_signal_crit_sec);
232  pg_signal_queue |= sigmask(signum);
233  LeaveCriticalSection(&pg_signal_crit_sec);
234 
235  SetEvent(pgwin32_signal_event);
236 }
Assert(fmt[strlen(fmt) - 1] !='\n')
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:156
#define PG_SIGNAL_COUNT
Definition: win32_port.h:456

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().

◆ pg_signal_thread()

static DWORD WINAPI pg_signal_thread ( LPVOID  param)
static

Definition at line 240 of file signal.c.

241 {
242  char pipename[128];
243  HANDLE pipe = pgwin32_initial_signal_pipe;
244 
245  /* Set up pipe name, in case we have to re-create the pipe. */
246  snprintf(pipename, sizeof(pipename), "\\\\.\\pipe\\pgsignal_%lu", GetCurrentProcessId());
247 
248  for (;;)
249  {
250  BOOL fConnected;
251 
252  /* Create a new pipe instance if we don't have one. */
253  if (pipe == INVALID_HANDLE_VALUE)
254  {
255  pipe = CreateNamedPipe(pipename, PIPE_ACCESS_DUPLEX,
256  PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
257  PIPE_UNLIMITED_INSTANCES, 16, 16, 1000, NULL);
258 
259  if (pipe == INVALID_HANDLE_VALUE)
260  {
261  write_stderr("could not create signal listener pipe: error code %lu; retrying\n", GetLastError());
262  SleepEx(500, FALSE);
263  continue;
264  }
265  }
266 
267  /*
268  * Wait for a client to connect. If something connects before we
269  * reach here, we'll get back a "failure" with ERROR_PIPE_CONNECTED,
270  * which is actually a success (way to go, Microsoft).
271  */
272  fConnected = ConnectNamedPipe(pipe, NULL) ? TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);
273  if (fConnected)
274  {
275  /*
276  * We have a connection from a would-be signal sender. Process it.
277  */
278  BYTE sigNum;
279  DWORD bytes;
280 
281  if (ReadFile(pipe, &sigNum, 1, &bytes, NULL) &&
282  bytes == 1)
283  {
284  /*
285  * Queue the signal before responding to the client. In this
286  * way, it's guaranteed that once kill() has returned in the
287  * signal sender, the next CHECK_FOR_INTERRUPTS() in the
288  * signal recipient will see the signal. (This is a stronger
289  * guarantee than POSIX makes; maybe we don't need it? But
290  * without it, we've seen timing bugs on Windows that do not
291  * manifest on any known Unix.)
292  */
293  pg_queue_signal(sigNum);
294 
295  /*
296  * Write something back to the client, allowing its
297  * CallNamedPipe() call to terminate.
298  */
299  WriteFile(pipe, &sigNum, 1, &bytes, NULL); /* Don't care if it
300  * works or not */
301 
302  /*
303  * We must wait for the client to read the data before we can
304  * disconnect, else the data will be lost. (If the WriteFile
305  * call failed, there'll be nothing in the buffer, so this
306  * shouldn't block.)
307  */
308  FlushFileBuffers(pipe);
309  }
310  else
311  {
312  /*
313  * If we fail to read a byte from the client, assume it's the
314  * client's problem and do nothing. Perhaps it'd be better to
315  * force a pipe close and reopen?
316  */
317  }
318 
319  /* Disconnect from client so that we can re-use the pipe. */
320  DisconnectNamedPipe(pipe);
321  }
322  else
323  {
324  /*
325  * Connection failed. Cleanup and try again.
326  *
327  * This should never happen. If it does, there's a window where
328  * we'll miss signals until we manage to re-create the pipe.
329  * However, just trying to use the same pipe again is probably not
330  * going to work, so we have little choice.
331  */
332  CloseHandle(pipe);
333  pipe = INVALID_HANDLE_VALUE;
334  }
335  }
336  return 0;
337 }
#define write_stderr(str)
Definition: parallel.c:186
#define snprintf
Definition: port.h:225
HANDLE pgwin32_initial_signal_pipe
Definition: signal.c:28

References pg_queue_signal(), pgwin32_initial_signal_pipe, snprintf, and write_stderr.

Referenced by pgwin32_signal_initialize().

◆ pg_usleep()

void pg_usleep ( long  microsec)

Definition at line 53 of file signal.c.

54 {
55  if (unlikely(pgwin32_signal_event == NULL))
56  {
57  /*
58  * If we're reached by pgwin32_open_handle() early in startup before
59  * the signal event is set up, just fall back to a regular
60  * non-interruptible sleep.
61  */
62  SleepEx((microsec < 500 ? 1 : (microsec + 500) / 1000), FALSE);
63  return;
64  }
65 
66  if (WaitForSingleObject(pgwin32_signal_event,
67  (microsec < 500 ? 1 : (microsec + 500) / 1000))
68  == WAIT_OBJECT_0)
69  {
71  errno = EINTR;
72  return;
73  }
74 }
#define unlikely(x)
Definition: c.h:273
void pgwin32_dispatch_queued_signals(void)
Definition: signal.c:118
#define EINTR
Definition: win32_port.h:351

References EINTR, pgwin32_dispatch_queued_signals(), pgwin32_signal_event, and unlikely.

Referenced by _bt_pendingfsm_finalize(), auth_delay_checks(), AutoVacLauncherMain(), AutoVacWorkerMain(), BackendInitialize(), BackgroundWriterMain(), CheckpointerMain(), ConditionalXactLockTableWait(), CountOtherDBBackends(), CreateCheckPoint(), do_watch(), exec_prog(), FileRead(), FileWrite(), GetMultiXactIdMembers(), InitPostgres(), main(), perform_spin_delay(), pgarch_ArchiverCopyLoop(), pgwin32_recv(), read_local_xlog_page_guts(), regression_main(), RequestCheckpoint(), ResolveRecoveryConflictWithDatabase(), ResolveRecoveryConflictWithVirtualXIDs(), ServerLoop(), StartBackgroundWorker(), StartupXLOG(), StreamConnection(), threadRun(), wait_for_postmaster_promote(), wait_for_postmaster_start(), wait_for_postmaster_stop(), wait_pid(), WaitExceedsMaxStandbyDelay(), WALDumpOpenSegment(), WalSndWaitStopping(), WalWriterMain(), XactLockTableWait(), and XLogFlush().

◆ pgwin32_create_signal_listener()

HANDLE pgwin32_create_signal_listener ( pid_t  pid)

Definition at line 193 of file signal.c.

194 {
195  char pipename[128];
196  HANDLE pipe;
197 
198  snprintf(pipename, sizeof(pipename), "\\\\.\\pipe\\pgsignal_%u", (int) pid);
199 
200  pipe = CreateNamedPipe(pipename, PIPE_ACCESS_DUPLEX,
201  PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
202  PIPE_UNLIMITED_INSTANCES, 16, 16, 1000, NULL);
203 
204  if (pipe == INVALID_HANDLE_VALUE)
205  ereport(ERROR,
206  (errmsg("could not create signal listener pipe for PID %d: error code %lu",
207  (int) pid, GetLastError())));
208 
209  return pipe;
210 }
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define ERROR
Definition: elog.h:33
#define ereport(elevel,...)
Definition: elog.h:143

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

◆ pgwin32_dispatch_queued_signals()

void pgwin32_dispatch_queued_signals ( void  )

Definition at line 118 of file signal.c.

119 {
120  int exec_mask;
121 
122  Assert(pgwin32_signal_event != NULL);
123  EnterCriticalSection(&pg_signal_crit_sec);
124  while ((exec_mask = UNBLOCKED_SIGNAL_QUEUE()) != 0)
125  {
126  /* One or more unblocked signals queued for execution */
127  int i;
128 
129  for (i = 1; i < PG_SIGNAL_COUNT; i++)
130  {
131  if (exec_mask & sigmask(i))
132  {
133  /* Execute this signal */
135 
136  if (sig == SIG_DFL)
139  if (sig != SIG_ERR && sig != SIG_IGN && sig != SIG_DFL)
140  {
141  LeaveCriticalSection(&pg_signal_crit_sec);
142  sig(i);
143  EnterCriticalSection(&pg_signal_crit_sec);
144  break; /* Restart outer loop, in case signal mask or
145  * queue has been modified inside signal
146  * handler */
147  }
148  }
149  }
150  }
151  ResetEvent(pgwin32_signal_event);
152  LeaveCriticalSection(&pg_signal_crit_sec);
153 }
int i
Definition: isn.c:73
static int sig
Definition: pg_ctl.c:84
void(* pqsigfunc)(int signo)
Definition: port.h:535
static pqsigfunc 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:455
#define SIG_DFL
Definition: win32_port.h:162
#define SIG_ERR
Definition: win32_port.h:163
#define SIG_IGN
Definition: win32_port.h:164

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

◆ 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  {
90  }
91  pg_signal_mask = 0;
92  pg_signal_queue = 0;
93 
94  /* Create the global event handle used to flag signals */
95  pgwin32_signal_event = CreateEvent(NULL, TRUE, FALSE, NULL);
96  if (pgwin32_signal_event == NULL)
97  ereport(FATAL,
98  (errmsg_internal("could not create signal event: error code %lu", GetLastError())));
99 
100  /* Create thread for handling signals */
101  signal_thread_handle = CreateThread(NULL, 0, pg_signal_thread, NULL, 0, NULL);
102  if (signal_thread_handle == NULL)
103  ereport(FATAL,
104  (errmsg_internal("could not create signal handler thread")));
105 
106  /* Create console control handle to pick up Ctrl-C etc */
107  if (!SetConsoleCtrlHandler(pg_console_handler, TRUE))
108  ereport(FATAL,
109  (errmsg_internal("could not set console control handler")));
110 }
int errmsg_internal(const char *fmt,...)
Definition: elog.c:991
#define FATAL
Definition: elog.h:35
static DWORD WINAPI pg_signal_thread(LPVOID param)
Definition: signal.c:240
static BOOL WINAPI pg_console_handler(DWORD dwCtrlType)
Definition: signal.c:343
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().

◆ pqsignal()

◆ pqsigsetmask()

int pqsigsetmask ( int  mask)

Definition at line 157 of file signal.c.

158 {
159  int prevmask;
160 
161  prevmask = pg_signal_mask;
162  pg_signal_mask = mask;
163 
164  /*
165  * Dispatch any signals queued up right away, in case we have unblocked
166  * one or more signals previously queued
167  */
169 
170  return prevmask;
171 }

References pg_signal_mask, and pgwin32_dispatch_queued_signals().

Variable Documentation

◆ pg_signal_array

pqsigfunc pg_signal_array[PG_SIGNAL_COUNT]
static

Definition at line 37 of file signal.c.

Referenced by pgwin32_dispatch_queued_signals(), pgwin32_signal_initialize(), and pqsignal().

◆ pg_signal_crit_sec

CRITICAL_SECTION pg_signal_crit_sec
static

◆ pg_signal_defaults

pqsigfunc pg_signal_defaults[PG_SIGNAL_COUNT]
static

Definition at line 38 of file signal.c.

Referenced by pgwin32_dispatch_queued_signals(), and pgwin32_signal_initialize().

◆ pg_signal_mask

int pg_signal_mask

Definition at line 25 of file signal.c.

Referenced by pgwin32_signal_initialize(), and pqsigsetmask().

◆ pg_signal_queue

volatile int pg_signal_queue

◆ pgwin32_initial_signal_pipe

HANDLE pgwin32_initial_signal_pipe = INVALID_HANDLE_VALUE

Definition at line 28 of file signal.c.

Referenced by pg_signal_thread().

◆ pgwin32_signal_event