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 pqsigprocmask (int how, const sigset_t *set, sigset_t *oset)
 
int pqsigaction (int signum, const struct sigaction *act, struct sigaction *oldact)
 
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 struct sigaction 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 377 of file signal.c.

378 {
379  if (dwCtrlType == CTRL_C_EVENT ||
380  dwCtrlType == CTRL_BREAK_EVENT ||
381  dwCtrlType == CTRL_CLOSE_EVENT ||
382  dwCtrlType == CTRL_SHUTDOWN_EVENT)
383  {
384  pg_queue_signal(SIGINT);
385  return TRUE;
386  }
387  return FALSE;
388 }
void pg_queue_signal(int signum)
Definition: signal.c:259

References pg_queue_signal().

Referenced by pgwin32_signal_initialize().

◆ 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 }
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: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().

◆ pg_signal_thread()

static DWORD WINAPI pg_signal_thread ( LPVOID  param)
static

Definition at line 274 of file signal.c.

275 {
276  char pipename[128];
277  HANDLE pipe = pgwin32_initial_signal_pipe;
278 
279  /* Set up pipe name, in case we have to re-create the pipe. */
280  snprintf(pipename, sizeof(pipename), "\\\\.\\pipe\\pgsignal_%lu", GetCurrentProcessId());
281 
282  for (;;)
283  {
284  BOOL fConnected;
285 
286  /* Create a new pipe instance if we don't have one. */
287  if (pipe == INVALID_HANDLE_VALUE)
288  {
289  pipe = CreateNamedPipe(pipename, PIPE_ACCESS_DUPLEX,
290  PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
291  PIPE_UNLIMITED_INSTANCES, 16, 16, 1000, NULL);
292 
293  if (pipe == INVALID_HANDLE_VALUE)
294  {
295  write_stderr("could not create signal listener pipe: error code %lu; retrying\n", GetLastError());
296  SleepEx(500, FALSE);
297  continue;
298  }
299  }
300 
301  /*
302  * Wait for a client to connect. If something connects before we
303  * reach here, we'll get back a "failure" with ERROR_PIPE_CONNECTED,
304  * which is actually a success (way to go, Microsoft).
305  */
306  fConnected = ConnectNamedPipe(pipe, NULL) ? TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);
307  if (fConnected)
308  {
309  /*
310  * We have a connection from a would-be signal sender. Process it.
311  */
312  BYTE sigNum;
313  DWORD bytes;
314 
315  if (ReadFile(pipe, &sigNum, 1, &bytes, NULL) &&
316  bytes == 1)
317  {
318  /*
319  * Queue the signal before responding to the client. In this
320  * way, it's guaranteed that once kill() has returned in the
321  * signal sender, the next CHECK_FOR_INTERRUPTS() in the
322  * signal recipient will see the signal. (This is a stronger
323  * guarantee than POSIX makes; maybe we don't need it? But
324  * without it, we've seen timing bugs on Windows that do not
325  * manifest on any known Unix.)
326  */
327  pg_queue_signal(sigNum);
328 
329  /*
330  * Write something back to the client, allowing its
331  * CallNamedPipe() call to terminate.
332  */
333  WriteFile(pipe, &sigNum, 1, &bytes, NULL); /* Don't care if it
334  * works or not */
335 
336  /*
337  * We must wait for the client to read the data before we can
338  * disconnect, else the data will be lost. (If the WriteFile
339  * call failed, there'll be nothing in the buffer, so this
340  * shouldn't block.)
341  */
342  FlushFileBuffers(pipe);
343  }
344  else
345  {
346  /*
347  * If we fail to read a byte from the client, assume it's the
348  * client's problem and do nothing. Perhaps it'd be better to
349  * force a pipe close and reopen?
350  */
351  }
352 
353  /* Disconnect from client so that we can re-use the pipe. */
354  DisconnectNamedPipe(pipe);
355  }
356  else
357  {
358  /*
359  * Connection failed. Cleanup and try again.
360  *
361  * This should never happen. If it does, there's a window where
362  * we'll miss signals until we manage to re-create the pipe.
363  * However, just trying to use the same pipe again is probably not
364  * going to work, so we have little choice.
365  */
366  CloseHandle(pipe);
367  pipe = INVALID_HANDLE_VALUE;
368  }
369  }
370  return 0;
371 }
#define write_stderr(str)
Definition: parallel.c:184
#define snprintf
Definition: port.h:238
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:298
void pgwin32_dispatch_queued_signals(void)
Definition: signal.c:120
#define EINTR
Definition: win32_port.h:374

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

Referenced by _bt_pendingfsm_finalize(), AcceptConnection(), auth_delay_checks(), AutoVacWorkerMain(), BackendInitialize(), BackgroundWorkerMain(), BackgroundWriterMain(), CheckpointerMain(), ConditionalXactLockTableWait(), CountOtherDBBackends(), CreateCheckPoint(), do_watch(), exec_prog(), FileReadV(), FileWriteV(), get_controlfile_by_exact_path(), GetMultiXactIdMembers(), InitPostgres(), main(), perform_spin_delay(), pgarch_ArchiverCopyLoop(), pgwin32_recv(), read_local_xlog_page_guts(), regression_main(), RequestCheckpoint(), ResolveRecoveryConflictWithDatabase(), ResolveRecoveryConflictWithVirtualXIDs(), StartupXLOG(), threadRun(), vacuum_delay_point(), wait_for_connection_state(), 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 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 ereport(elevel,...)
Definition: elog.h:149

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 }
int i
Definition: isn.c:73
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_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 }
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1159
#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().

◆ pqsigaction()

int pqsigaction ( int  signum,
const struct sigaction *  act,
struct sigaction *  oldact 
)

Definition at line 210 of file signal.c.

212 {
213  if (signum >= PG_SIGNAL_COUNT || signum < 0)
214  {
215  errno = EINVAL;
216  return -1;
217  }
218  if (oldact)
219  *oldact = pg_signal_array[signum];
220  if (act)
221  pg_signal_array[signum] = *act;
222  return 0;
223 }

References pg_signal_array, and PG_SIGNAL_COUNT.

◆ pqsigprocmask()

int pqsigprocmask ( int  how,
const sigset_t *  set,
sigset_t *  oset 
)

Definition at line 171 of file signal.c.

172 {
173  if (oset)
174  *oset = pg_signal_mask;
175 
176  if (!set)
177  return 0;
178 
179  switch (how)
180  {
181  case SIG_BLOCK:
182  pg_signal_mask |= *set;
183  break;
184  case SIG_UNBLOCK:
185  pg_signal_mask &= ~*set;
186  break;
187  case SIG_SETMASK:
188  pg_signal_mask = *set;
189  break;
190  default:
191  errno = EINVAL;
192  return -1;
193  }
194 
195  /*
196  * Dispatch any signals queued up right away, in case we have unblocked
197  * one or more signals previously queued
198  */
200 
201  return 0;
202 }

References pg_signal_mask, and pgwin32_dispatch_queued_signals().

Variable Documentation

◆ pg_signal_array

struct sigaction pg_signal_array[PG_SIGNAL_COUNT]
static

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

◆ 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