1 /*-------------------------------------------------------------------------
4 * reliable BSD-style signal(2) routine stolen from RWW who stole it
7 * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
14 * A NOTE ABOUT SIGNAL HANDLING ACROSS THE VARIOUS PLATFORMS.
16 * pg_config.h defines the macro HAVE_POSIX_SIGNALS for some platforms and
17 * not for others. We use that here to decide how to handle signalling.
19 * Ultrix and SunOS provide BSD signal(2) semantics by default.
21 * SVID2 and POSIX signal(2) semantics differ from BSD signal(2)
22 * semantics. We can use the POSIX sigaction(2) on systems that
23 * allow us to request restartable signals (SA_RESTART).
25 * Some systems don't allow restartable signals at all unless we
26 * link to a special BSD library.
28 * We devoutly hope that there aren't any Unix-oid systems that provide
29 * neither POSIX signals nor BSD signals. The alternative is to do
30 * signal-handler reinstallation, which doesn't work well at all.
32 * Windows, of course, is resolutely in a class by itself. In the backend,
33 * we don't use this file at all; src/backend/port/win32/signal.c provides
34 * pqsignal() for the backend environment. Frontend programs can use
35 * this version of pqsignal() if they wish, but beware that Windows
36 * requires signal-handler reinstallation, because indeed it provides
37 * neither POSIX signals nor BSD signals :-(
38 * ------------------------------------------------------------------------
45 #if !defined(WIN32) || defined(FRONTEND)
48 * Set up a signal handler for signal "signo"
50 * Returns the previous handler.
53 pqsignal(int signo, pqsigfunc func)
55 #if !defined(HAVE_POSIX_SIGNALS)
56 return signal(signo, func);
61 act.sa_handler = func;
62 sigemptyset(&act.sa_mask);
63 act.sa_flags = SA_RESTART;
66 act.sa_flags |= SA_NOCLDSTOP;
68 if (sigaction(signo, &act, &oact) < 0)
70 return oact.sa_handler;
71 #endif /* !HAVE_POSIX_SIGNALS */
74 #endif /* !defined(WIN32) || defined(FRONTEND) */