58 #ifdef PG_SIGNAL_COUNT
59 #define PG_NSIG (PG_SIGNAL_COUNT)
61 #define PG_NSIG (NSIG)
69 StaticAssertDecl(SIGTERM < PG_NSIG, "SIGTERM >=
PG_NSIG");
72 static volatile pqsigfunc pqsignal_handlers[PG_NSIG];
75 * Except when called with SIG_IGN or SIG_DFL, pqsignal() sets up this function
76 * as the handler for all signals. This wrapper handler function checks that
77 * it is called within a process that knew to maintain MyProcPid, and not a
78 * child process forked by system(3), etc. This check ensures that such child
79 * processes do not modify shared memory, which is often detrimental. If the
80 * check succeeds, the function originally provided to pqsignal() is called.
81 * Otherwise, the default signal handler is installed and then called.
83 * This wrapper also handles restoring the value of errno.
86 wrapper_handler(SIGNAL_ARGS)
88 int save_errno = errno;
93 * We expect processes to set MyProcPid before calling pqsignal() or
94 * before accepting signals.
97 Assert(MyProcPid != PostmasterPid || !IsUnderPostmaster);
99 if (unlikely(MyProcPid != (int) getpid()))
101 pqsignal(postgres_signal_arg, SIG_DFL);
102 raise(postgres_signal_arg);
107 (*pqsignal_handlers[postgres_signal_arg]) (postgres_signal_arg);
113 * Set up a signal handler, with SA_RESTART, for signal "signo
"
115 * Returns the previous handler.
117 * NB: If called within a signal handler, race conditions may lead to bogus
118 * return values. You should either avoid calling this within signal handlers
119 * or ignore the return value.
121 * XXX: Since no in-tree callers use the return value, and there is little
122 * reason to do so, it would be nice if we could convert this to a void
123 * function instead of providing potentially-bogus return values.
124 * Unfortunately, that requires modifying the pqsignal() in legacy-pqsignal.c,
125 * which in turn requires an SONAME bump, which is probably not worth it.
128 pqsignal(int signo, pqsigfunc func)
130 pqsigfunc orig_func = pqsignal_handlers[signo]; /* assumed atomic */
131 #if !(defined(WIN32) && defined(FRONTEND))
132 struct sigaction act,
138 Assert(signo < PG_NSIG);
140 if (func != SIG_IGN && func != SIG_DFL)
142 pqsignal_handlers[signo] = func; /* assumed atomic */
143 func = wrapper_handler;
146 #if !(defined(WIN32) && defined(FRONTEND))
147 act.sa_handler = func;
148 sigemptyset(&act.sa_mask);
149 act.sa_flags = SA_RESTART;
151 if (signo == SIGCHLD)
152 act.sa_flags |= SA_NOCLDSTOP;
154 if (sigaction(signo, &act, &oact) < 0)
156 else if (oact.sa_handler == wrapper_handler)
159 return oact.sa_handler;
161 /* Forward to Windows native signal system. */
162 if ((ret = signal(signo, func)) == wrapper_handler)
StaticAssertDecl(SIGUSR2< PG_NSIG, "SIGUSR2 >= PG_NSIG")