]> granicus.if.org Git - postgresql/blob - src/port/pqsignal.c
Use SA_RESTART for all signals, including SIGALRM.
[postgresql] / src / port / pqsignal.c
1 /*-------------------------------------------------------------------------
2  *
3  * pqsignal.c
4  *        reliable BSD-style signal(2) routine stolen from RWW who stole it
5  *        from Stevens...
6  *
7  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  *
11  * IDENTIFICATION
12  *        src/port/pqsignal.c
13  *
14  *      A NOTE ABOUT SIGNAL HANDLING ACROSS THE VARIOUS PLATFORMS.
15  *
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.
18  *
19  *      Ultrix and SunOS provide BSD signal(2) semantics by default.
20  *
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).
24  *
25  *      Some systems don't allow restartable signals at all unless we
26  *      link to a special BSD library.
27  *
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.
31  *
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  * ------------------------------------------------------------------------
39  */
40
41 #include "c.h"
42
43 #include <signal.h>
44
45 #if !defined(WIN32) || defined(FRONTEND)
46
47 /*
48  * Set up a signal handler for signal "signo"
49  *
50  * Returns the previous handler.
51  */
52 pqsigfunc
53 pqsignal(int signo, pqsigfunc func)
54 {
55 #if !defined(HAVE_POSIX_SIGNALS)
56         return signal(signo, func);
57 #else
58         struct sigaction act,
59                                 oact;
60
61         act.sa_handler = func;
62         sigemptyset(&act.sa_mask);
63         act.sa_flags = SA_RESTART;
64 #ifdef SA_NOCLDSTOP
65         if (signo == SIGCHLD)
66                 act.sa_flags |= SA_NOCLDSTOP;
67 #endif
68         if (sigaction(signo, &act, &oact) < 0)
69                 return SIG_ERR;
70         return oact.sa_handler;
71 #endif   /* !HAVE_POSIX_SIGNALS */
72 }
73
74 #endif   /* !defined(WIN32) || defined(FRONTEND) */