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 333 of file signal.c.

References pg_queue_signal().

Referenced by pgwin32_signal_initialize().

334 {
335  if (dwCtrlType == CTRL_C_EVENT ||
336  dwCtrlType == CTRL_BREAK_EVENT ||
337  dwCtrlType == CTRL_CLOSE_EVENT ||
338  dwCtrlType == CTRL_SHUTDOWN_EVENT)
339  {
340  pg_queue_signal(SIGINT);
341  return TRUE;
342  }
343  return FALSE;
344 }
void pg_queue_signal(int signum)
Definition: signal.c:215

◆ pg_queue_signal()

void pg_queue_signal ( int  signum)

Definition at line 215 of file signal.c.

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(), pg_timer_thread(), and PostmasterMarkPIDForWorkerNotify().

216 {
217  Assert(pgwin32_signal_event != NULL);
218  if (signum >= PG_SIGNAL_COUNT || signum <= 0)
219  return; /* ignore any bad signal number */
220 
221  EnterCriticalSection(&pg_signal_crit_sec);
222  pg_signal_queue |= sigmask(signum);
223  LeaveCriticalSection(&pg_signal_crit_sec);
224 
225  SetEvent(pgwin32_signal_event);
226 }
volatile int pg_signal_queue
Definition: signal.c:24
#define sigmask(sig)
Definition: win32_port.h:143
HANDLE pgwin32_signal_event
Definition: signal.c:27
static CRITICAL_SECTION pg_signal_crit_sec
Definition: signal.c:34
#define PG_SIGNAL_COUNT
Definition: win32_port.h:418
#define Assert(condition)
Definition: c.h:739

◆ pg_signal_thread()

static DWORD WINAPI pg_signal_thread ( LPVOID  param)
static

Definition at line 230 of file signal.c.

References generate_unaccent_rules::bytes(), pg_queue_signal(), pgwin32_initial_signal_pipe, snprintf, and write_stderr.

Referenced by pgwin32_signal_initialize().

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

◆ pg_usleep()

void pg_usleep ( long  microsec)

Definition at line 53 of file signal.c.

Referenced by auth_delay_checks(), AutoVacLauncherMain(), AutoVacWorkerMain(), backend_read_statsfile(), BackendInitialize(), BackgroundWriterMain(), CheckpointerMain(), CheckpointWriteDelay(), ConditionalXactLockTableWait(), CountOtherDBBackends(), CreateCheckPoint(), CustomizableNextWALFileReady(), do_pg_stop_backup(), do_promote(), do_restart(), do_stop(), do_watch(), exec_prog(), FileRead(), FileWrite(), GetMultiXactIdMembers(), InitPostgres(), lazy_truncate_heap(), main(), perform_spin_delay(), pgarch_ArchiverCopyLoop(), pgwin32_recv(), read_local_xlog_page(), recoveryPausesHere(), RegisterSyncRequest(), regression_main(), RequestCheckpoint(), ResolveRecoveryConflictWithDatabase(), ResolveRecoveryConflictWithVirtualXIDs(), RestoreWALFileForRecovery(), ServerLoop(), ShutdownWalRcv(), StartBackgroundWorker(), StartupXLOG(), StreamConnection(), threadRun(), vacuum_delay_point(), wait_for_postmaster(), wait_pid(), WaitExceedsMaxStandbyDelay(), WALDumpOpenSegment(), WalSndWaitStopping(), WalWriterMain(), XactLockTableWait(), and XLogFlush().

54 {
56  if (WaitForSingleObject(pgwin32_signal_event,
57  (microsec < 500 ? 1 : (microsec + 500) / 1000))
58  == WAIT_OBJECT_0)
59  {
61  errno = EINTR;
62  return;
63  }
64 }
HANDLE pgwin32_signal_event
Definition: signal.c:27
void pgwin32_dispatch_queued_signals(void)
Definition: signal.c:108
#define Assert(condition)
Definition: c.h:739
#define EINTR
Definition: win32_port.h:323

◆ pgwin32_create_signal_listener()

HANDLE pgwin32_create_signal_listener ( pid_t  pid)

Definition at line 183 of file signal.c.

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

Referenced by PostmasterMarkPIDForWorkerNotify().

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

◆ pgwin32_dispatch_queued_signals()

void pgwin32_dispatch_queued_signals ( void  )

Definition at line 108 of file signal.c.

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

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

◆ pgwin32_signal_initialize()

void pgwin32_signal_initialize ( void  )

Definition at line 69 of file signal.c.

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 BackendRun(), and main().

70 {
71  int i;
72  HANDLE signal_thread_handle;
73 
74  InitializeCriticalSection(&pg_signal_crit_sec);
75 
76  for (i = 0; i < PG_SIGNAL_COUNT; i++)
77  {
80  }
81  pg_signal_mask = 0;
82  pg_signal_queue = 0;
83 
84  /* Create the global event handle used to flag signals */
85  pgwin32_signal_event = CreateEvent(NULL, TRUE, FALSE, NULL);
86  if (pgwin32_signal_event == NULL)
87  ereport(FATAL,
88  (errmsg_internal("could not create signal event: error code %lu", GetLastError())));
89 
90  /* Create thread for handling signals */
91  signal_thread_handle = CreateThread(NULL, 0, pg_signal_thread, NULL, 0, NULL);
92  if (signal_thread_handle == NULL)
93  ereport(FATAL,
94  (errmsg_internal("could not create signal handler thread")));
95 
96  /* Create console control handle to pick up Ctrl-C etc */
97  if (!SetConsoleCtrlHandler(pg_console_handler, TRUE))
98  ereport(FATAL,
99  (errmsg_internal("could not set console control handler")));
100 }
int pg_signal_mask
Definition: signal.c:25
static BOOL WINAPI pg_console_handler(DWORD dwCtrlType)
Definition: signal.c:333
volatile int pg_signal_queue
Definition: signal.c:24
HANDLE pgwin32_signal_event
Definition: signal.c:27
#define FATAL
Definition: elog.h:52
static CRITICAL_SECTION pg_signal_crit_sec
Definition: signal.c:34
#define ereport(elevel, rest)
Definition: elog.h:141
#define PG_SIGNAL_COUNT
Definition: win32_port.h:418
#define SIG_IGN
Definition: win32_port.h:151
static pqsigfunc pg_signal_defaults[PG_SIGNAL_COUNT]
Definition: signal.c:38
int errmsg_internal(const char *fmt,...)
Definition: elog.c:909
#define SIG_DFL
Definition: win32_port.h:149
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:230

◆ pqsignal()

◆ pqsigsetmask()

int pqsigsetmask ( int  mask)

Definition at line 147 of file signal.c.

References pg_signal_mask, and pgwin32_dispatch_queued_signals().

148 {
149  int prevmask;
150 
151  prevmask = pg_signal_mask;
152  pg_signal_mask = mask;
153 
154  /*
155  * Dispatch any signals queued up right away, in case we have unblocked
156  * one or more signals previously queued
157  */
159 
160  return prevmask;
161 }
int pg_signal_mask
Definition: signal.c:25
void pgwin32_dispatch_queued_signals(void)
Definition: signal.c:108

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

◆ pgwin32_signal_event