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

Go to the source code of this file.

Macros

#define PG_NSIG   (64) /* XXX: wild guess */
 

Functions

 StaticAssertDecl (SIGUSR2< PG_NSIG, "SIGUSR2 >= PG_NSIG")
 
static void wrapper_handler (SIGNAL_ARGS)
 
pqsigfunc pqsignal (int signo, pqsigfunc func)
 

Variables

static volatile pqsigfunc pqsignal_handlers [PG_NSIG]
 

Macro Definition Documentation

◆ PG_NSIG

#define PG_NSIG   (64) /* XXX: wild guess */

Definition at line 63 of file pqsignal.c.

Function Documentation

◆ pqsignal()

pqsigfunc pqsignal ( int  signo,
pqsigfunc  func 
)

Definition at line 129 of file pqsignal.c.

130 {
131  pqsigfunc orig_func = pqsignal_handlers[signo]; /* assumed atomic */
132 #if !(defined(WIN32) && defined(FRONTEND))
133  struct sigaction act,
134  oact;
135 #else
136  pqsigfunc ret;
137 #endif
138 
139  Assert(signo < PG_NSIG);
140 
141  if (func != SIG_IGN && func != SIG_DFL)
142  {
143  pqsignal_handlers[signo] = func; /* assumed atomic */
144  func = wrapper_handler;
145  }
146 
147 #if !(defined(WIN32) && defined(FRONTEND))
148  act.sa_handler = func;
149  sigemptyset(&act.sa_mask);
150  act.sa_flags = SA_RESTART;
151 #ifdef SA_NOCLDSTOP
152  if (signo == SIGCHLD)
153  act.sa_flags |= SA_NOCLDSTOP;
154 #endif
155  if (sigaction(signo, &act, &oact) < 0)
156  return SIG_ERR;
157  else if (oact.sa_handler == wrapper_handler)
158  return orig_func;
159  else
160  return oact.sa_handler;
161 #else
162  /* Forward to Windows native signal system. */
163  if ((ret = signal(signo, func)) == wrapper_handler)
164  return orig_func;
165  else
166  return ret;
167 #endif
168 }
#define Assert(condition)
Definition: c.h:812
static void wrapper_handler(SIGNAL_ARGS)
Definition: pqsignal.c:87
static volatile pqsigfunc pqsignal_handlers[PG_NSIG]
Definition: pqsignal.c:72
#define PG_NSIG
Definition: pqsignal.c:63
void(* pqsigfunc)(SIGNAL_ARGS)
Definition: port.h:490
#define SIGCHLD
Definition: win32_port.h:178
#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 SIG_ERR, SIGALRM, and SIGCHLD.

Referenced by ApplyLauncherMain(), autoprewarm_database_main(), autoprewarm_main(), AutoVacWorkerMain(), BackendInitialize(), BackgroundWorkerMain(), BackgroundWriterMain(), bootstrap_signals(), CheckpointerMain(), disable_sigpipe_trap(), do_start(), InitializeLatchSupport(), InitializeTimeouts(), InitPostmasterChild(), main(), make_temp_sockdir(), OpenPipeStream(), ParallelApplyWorkerMain(), ParallelBackupStart(), ParallelWorkerMain(), PgArchiverMain(), plperl_init_interp(), PostgresMain(), PostmasterDeathSignalInit(), PostmasterMain(), ReplSlotSyncWorkerMain(), restore_sigpipe_trap(), set_cancel_handler(), setalarm(), setup_cancel_handler(), setup_signals(), SetupApplyOrSyncWorker(), ShutdownLatchSupport(), signal_remove_temp(), sigTermHandler(), StartupProcessMain(), SysLoggerMain(), test_shm_mq_main(), trap_sigint_during_startup(), trapsig(), WalReceiverMain(), WalSndSignals(), WalSummarizerMain(), WalWriterMain(), and worker_spi_main().

◆ StaticAssertDecl()

StaticAssertDecl ( )

◆ wrapper_handler()

static void wrapper_handler ( SIGNAL_ARGS  )
static

Definition at line 87 of file pqsignal.c.

88 {
89  int save_errno = errno;
90 
91 #ifndef FRONTEND
92 
93  /*
94  * We expect processes to set MyProcPid before calling pqsignal() or
95  * before accepting signals.
96  */
99 
100  if (unlikely(MyProcPid != (int) getpid()))
101  {
102  pqsignal(postgres_signal_arg, SIG_DFL);
103  raise(postgres_signal_arg);
104  return;
105  }
106 #endif
107 
108  (*pqsignal_handlers[postgres_signal_arg]) (postgres_signal_arg);
109 
110  errno = save_errno;
111 }
#define unlikely(x)
Definition: c.h:330
pid_t PostmasterPid
Definition: globals.c:105
int MyProcPid
Definition: globals.c:46
bool IsUnderPostmaster
Definition: globals.c:119
pqsigfunc pqsignal(int signo, pqsigfunc func)
Definition: pqsignal.c:129

Variable Documentation

◆ pqsignal_handlers

volatile pqsigfunc pqsignal_handlers[PG_NSIG]
static

Definition at line 72 of file pqsignal.c.