PostgreSQL Source Code  git master
pqsignal.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * pqsignal.c
4  * Backend signal(2) support (see also src/port/pqsignal.c)
5  *
6  * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/backend/libpq/pqsignal.c
12  *
13  * ------------------------------------------------------------------------
14  */
15 
16 #include "postgres.h"
17 
18 #include "libpq/pqsignal.h"
19 
20 
21 /* Global variables */
22 sigset_t UnBlockSig,
23  BlockSig,
25 
26 
27 /*
28  * Initialize BlockSig, UnBlockSig, and StartupBlockSig.
29  *
30  * BlockSig is the set of signals to block when we are trying to block
31  * signals. This includes all signals we normally expect to get, but NOT
32  * signals that should never be turned off.
33  *
34  * StartupBlockSig is the set of signals to block during startup packet
35  * collection; it's essentially BlockSig minus SIGTERM, SIGQUIT, SIGALRM.
36  *
37  * UnBlockSig is the set of signals to block when we don't want to block
38  * signals.
39  */
40 void
42 {
43  sigemptyset(&UnBlockSig);
44 
45  /* Note: InitializeLatchSupport() modifies UnBlockSig. */
46 
47  /* First set all signals, then clear some. */
48  sigfillset(&BlockSig);
49  sigfillset(&StartupBlockSig);
50 
51  /*
52  * Unmark those signals that should never be blocked. Some of these signal
53  * names don't exist on all platforms. Most do, but might as well ifdef
54  * them all for consistency...
55  */
56 #ifdef SIGTRAP
57  sigdelset(&BlockSig, SIGTRAP);
58  sigdelset(&StartupBlockSig, SIGTRAP);
59 #endif
60 #ifdef SIGABRT
61  sigdelset(&BlockSig, SIGABRT);
62  sigdelset(&StartupBlockSig, SIGABRT);
63 #endif
64 #ifdef SIGILL
65  sigdelset(&BlockSig, SIGILL);
66  sigdelset(&StartupBlockSig, SIGILL);
67 #endif
68 #ifdef SIGFPE
69  sigdelset(&BlockSig, SIGFPE);
70  sigdelset(&StartupBlockSig, SIGFPE);
71 #endif
72 #ifdef SIGSEGV
73  sigdelset(&BlockSig, SIGSEGV);
74  sigdelset(&StartupBlockSig, SIGSEGV);
75 #endif
76 #ifdef SIGBUS
77  sigdelset(&BlockSig, SIGBUS);
78  sigdelset(&StartupBlockSig, SIGBUS);
79 #endif
80 #ifdef SIGSYS
81  sigdelset(&BlockSig, SIGSYS);
82  sigdelset(&StartupBlockSig, SIGSYS);
83 #endif
84 #ifdef SIGCONT
85  sigdelset(&BlockSig, SIGCONT);
86  sigdelset(&StartupBlockSig, SIGCONT);
87 #endif
88 
89 /* Signals unique to startup */
90 #ifdef SIGQUIT
91  sigdelset(&StartupBlockSig, SIGQUIT);
92 #endif
93 #ifdef SIGTERM
94  sigdelset(&StartupBlockSig, SIGTERM);
95 #endif
96 #ifdef SIGALRM
97  sigdelset(&StartupBlockSig, SIGALRM);
98 #endif
99 }
100 
101 /*
102  * Set up a postmaster signal handler for signal "signo"
103  *
104  * Returns the previous handler.
105  *
106  * This is used only in the postmaster, which has its own odd approach to
107  * signal handling. For signals with handlers, we block all signals for the
108  * duration of signal handler execution. We also do not set the SA_RESTART
109  * flag; this should be safe given the tiny range of code in which the
110  * postmaster ever unblocks signals.
111  *
112  * pqinitmask() must have been invoked previously.
113  *
114  * On Windows, this function is just an alias for pqsignal()
115  * (and note that it's calling the code in src/backend/port/win32/signal.c,
116  * not src/port/pqsignal.c). On that platform, the postmaster's signal
117  * handlers still have to block signals for themselves.
118  */
119 pqsigfunc
120 pqsignal_pm(int signo, pqsigfunc func)
121 {
122 #ifndef WIN32
123  struct sigaction act,
124  oact;
125 
126  act.sa_handler = func;
127  if (func == SIG_IGN || func == SIG_DFL)
128  {
129  /* in these cases, act the same as pqsignal() */
130  sigemptyset(&act.sa_mask);
131  act.sa_flags = SA_RESTART;
132  }
133  else
134  {
135  act.sa_mask = BlockSig;
136  act.sa_flags = 0;
137  }
138 #ifdef SA_NOCLDSTOP
139  if (signo == SIGCHLD)
140  act.sa_flags |= SA_NOCLDSTOP;
141 #endif
142  if (sigaction(signo, &act, &oact) < 0)
143  return SIG_ERR;
144  return oact.sa_handler;
145 #else /* WIN32 */
146  return pqsignal(signo, func);
147 #endif
148 }
pqsigfunc pqsignal(int signo, pqsigfunc func)
Definition: pqsignal.c:40
#define SIGQUIT
Definition: win32_port.h:160
#define SIGCHLD
Definition: win32_port.h:169
void(* pqsigfunc)(int signo)
Definition: port.h:544
#define SIGCONT
Definition: win32_port.h:168
#define SIGTRAP
Definition: win32_port.h:161
#define SIG_ERR
Definition: win32_port.h:155
#define SIGABRT
Definition: win32_port.h:162
void pqinitmask(void)
Definition: pqsignal.c:41
sigset_t UnBlockSig
Definition: pqsignal.c:22
#define SIG_IGN
Definition: win32_port.h:156
pqsigfunc pqsignal_pm(int signo, pqsigfunc func)
Definition: pqsignal.c:120
sigset_t BlockSig
Definition: pqsignal.c:22
#define SIG_DFL
Definition: win32_port.h:154
#define SIGALRM
Definition: win32_port.h:165
sigset_t StartupBlockSig
Definition: pqsignal.c:22