]> git.8kb.co.uk Git - pgpool-ii/pgpool-ii_2.2.5/blob - pool_signal.c
Attempt to send a proper failure message to frontend when authentication
[pgpool-ii/pgpool-ii_2.2.5] / pool_signal.c
1 /* -*-pgsql-c-*- */
2 /*
3  * $Header: /cvsroot/pgpool/pgpool-II/pool_signal.c,v 1.3.2.1 2009/08/22 04:19:49 t-ishii Exp $
4  *
5  * pgpool: a language independent connection pool server for PostgreSQL
6  * written by Tatsuo Ishii
7  *
8  * Portions Copyright (c) 2003-2008, PgPool Global Development Group
9  * Portions Copyright (c) 2003-2004, PostgreSQL Global Development Group
10  *
11  * Permission to use, copy, modify, and distribute this software and
12  * its documentation for any purpose and without fee is hereby
13  * granted, provided that the above copyright notice appear in all
14  * copies and that both that copyright notice and this permission
15  * notice appear in supporting documentation, and that the name of the
16  * author not be used in advertising or publicity pertaining to
17  * distribution of the software without specific, written prior
18  * permission. The author makes no representations about the
19  * suitability of this software for any purpose.  It is provided "as
20  * is" without express or implied warranty.
21  */
22
23 /*
24  * Signal stuff. Stolen from PostgreSQL source code.
25  */
26
27 #include "config.h"
28 #include "pool_signal.h"
29 #include <signal.h>
30
31 #ifdef HAVE_SIGPROCMASK
32 sigset_t        UnBlockSig,
33                         BlockSig,
34                         AuthBlockSig;
35
36 #else
37 int                     UnBlockSig,
38                         BlockSig,
39                         AuthBlockSig;
40 #endif
41
42 /*
43  * Initialize BlockSig, UnBlockSig, and AuthBlockSig.
44  *
45  * BlockSig is the set of signals to block when we are trying to block
46  * signals.  This includes all signals we normally expect to get, but NOT
47  * signals that should never be turned off.
48  *
49  * AuthBlockSig is the set of signals to block during authentication;
50  * it's essentially BlockSig minus SIGTERM, SIGQUIT, SIGALRM.
51  *
52  * UnBlockSig is the set of signals to block when we don't want to block
53  * signals (is this ever nonzero??)
54  */
55 void
56 poolinitmask(void)
57 {
58 #ifdef HAVE_SIGPROCMASK
59         sigemptyset(&UnBlockSig);
60         sigfillset(&BlockSig);
61         sigfillset(&AuthBlockSig);
62
63         /*
64          * Unmark those signals that should never be blocked. Some of these
65          * signal names don't exist on all platforms.  Most do, but might as
66          * well ifdef them all for consistency...
67          */
68 #ifdef SIGTRAP
69         sigdelset(&BlockSig, SIGTRAP);
70         sigdelset(&AuthBlockSig, SIGTRAP);
71 #endif
72 #ifdef SIGABRT
73         sigdelset(&BlockSig, SIGABRT);
74         sigdelset(&AuthBlockSig, SIGABRT);
75 #endif
76 #ifdef SIGILL
77         sigdelset(&BlockSig, SIGILL);
78         sigdelset(&AuthBlockSig, SIGILL);
79 #endif
80 #ifdef SIGFPE
81         sigdelset(&BlockSig, SIGFPE);
82         sigdelset(&AuthBlockSig, SIGFPE);
83 #endif
84 #ifdef SIGSEGV
85         sigdelset(&BlockSig, SIGSEGV);
86         sigdelset(&AuthBlockSig, SIGSEGV);
87 #endif
88 #ifdef SIGBUS
89         sigdelset(&BlockSig, SIGBUS);
90         sigdelset(&AuthBlockSig, SIGBUS);
91 #endif
92 #ifdef SIGSYS
93         sigdelset(&BlockSig, SIGSYS);
94         sigdelset(&AuthBlockSig, SIGSYS);
95 #endif
96 #ifdef SIGCONT
97         sigdelset(&BlockSig, SIGCONT);
98         sigdelset(&AuthBlockSig, SIGCONT);
99 #endif
100 #ifdef SIGTERM
101         sigdelset(&AuthBlockSig, SIGTERM);
102 #endif
103 #ifdef SIGQUIT
104         sigdelset(&AuthBlockSig, SIGQUIT);
105 #endif
106 #ifdef SIGALRM
107         sigdelset(&AuthBlockSig, SIGALRM);
108 #endif
109 #else
110         UnBlockSig = 0;
111         BlockSig = sigmask(SIGHUP) | sigmask(SIGQUIT) |
112                 sigmask(SIGTERM) | sigmask(SIGALRM) |
113                 sigmask(SIGINT) | sigmask(SIGUSR1) |
114                 sigmask(SIGUSR2) | sigmask(SIGCHLD) |
115                 sigmask(SIGWINCH) | sigmask(SIGFPE);
116         AuthBlockSig = sigmask(SIGHUP) |
117                 sigmask(SIGINT) | sigmask(SIGUSR1) |
118                 sigmask(SIGUSR2) | sigmask(SIGCHLD) |
119                 sigmask(SIGWINCH) | sigmask(SIGFPE);
120 #endif
121 }
122
123
124 /* Win32 signal handling is in backend/port/win32/signal.c */
125 #ifndef WIN32
126
127 /*
128  * We need to check actually the system has the posix signals or not, but...
129  */
130 #define HAVE_POSIX_SIGNALS
131 /*
132  * Set up a signal handler
133  */
134 pool_sighandler_t
135 pool_signal(int signo, pool_sighandler_t func)
136 {
137 #if !defined(HAVE_POSIX_SIGNALS)
138         return signal(signo, func);
139 #else
140         struct sigaction act,
141                                 oact;
142
143         act.sa_handler = func;
144         sigemptyset(&act.sa_mask);
145         act.sa_flags = 0;
146         if (signo != SIGALRM)
147                 act.sa_flags |= SA_RESTART;
148 #ifdef SA_NOCLDSTOP
149         if (signo == SIGCHLD)
150                 act.sa_flags |= SA_NOCLDSTOP;
151 #endif
152         if (sigaction(signo, &act, &oact) < 0)
153                 return SIG_ERR;
154         return oact.sa_handler;
155 #endif   /* !HAVE_POSIX_SIGNALS */
156 }
157
158 #endif   /* WIN32 */