2009-05-15 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / common / signal.c
blob98859a43dd2fcbe2909910c3bf46cdc5770839a7
1 /* signal.c - signal handling
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002,
3 * 2005 Free Software Foundation, Inc.
5 * This file is part of GnuPG.
7 * GnuPG is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * GnuPG is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include <config.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <signal.h>
25 #include <unistd.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <assert.h>
30 #include "util.h"
33 #ifndef HAVE_DOSISH_SYSTEM
34 static volatile int caught_fatal_sig;
35 static volatile int caught_sigusr1;
36 #endif
37 static void (*cleanup_fnc)(void);
40 #ifndef HAVE_DOSISH_SYSTEM
41 static void
42 init_one_signal (int sig, RETSIGTYPE (*handler)(int), int check_ign )
44 # ifdef HAVE_SIGACTION
45 struct sigaction oact, nact;
47 if (check_ign)
49 /* we don't want to change an IGN handler */
50 sigaction (sig, NULL, &oact );
51 if (oact.sa_handler == SIG_IGN )
52 return;
55 nact.sa_handler = handler;
56 sigemptyset (&nact.sa_mask);
57 nact.sa_flags = 0;
58 sigaction ( sig, &nact, NULL);
59 # else
60 RETSIGTYPE (*ohandler)(int);
62 ohandler = signal (sig, handler);
63 if (check_ign && ohandler == SIG_IGN)
65 /* Change it back if it was already set to IGN */
66 signal (sig, SIG_IGN);
68 # endif
70 #endif /*!HAVE_DOSISH_SYSTEM*/
72 #ifndef HAVE_DOSISH_SYSTEM
73 static const char *
74 get_signal_name( int signum )
76 /* Note that we can't use strsignal(), because it is not
77 reentrant. */
78 #if HAVE_DECL_SYS_SIGLIST && defined(NSIG)
79 return (signum >= 0 && signum < NSIG) ? sys_siglist[signum] : "?";
80 #else
81 return NULL;
82 #endif
84 #endif /*!HAVE_DOSISH_SYSTEM*/
86 #ifndef HAVE_DOSISH_SYSTEM
87 static RETSIGTYPE
88 got_fatal_signal (int sig)
90 const char *s;
92 if (caught_fatal_sig)
93 raise (sig);
94 caught_fatal_sig = 1;
96 if (cleanup_fnc)
97 cleanup_fnc ();
98 /* Better don't translate these messages. */
99 write (2, "\n", 1 );
100 s = log_get_prefix (NULL);
101 if (s)
102 write(2, s, strlen (s));
103 write (2, ": signal ", 9 );
104 s = get_signal_name(sig);
105 if (s)
106 write (2, s, strlen(s) );
107 else
109 /* We are in a signal handler so we can't use any kind of printf
110 even not sprintf. So we use a straightforward algorithm. We
111 got a report that on one particular system, raising a signal
112 while in this handler, the parameter SIG get sclobbered and
113 things are messed up because we modify its value. Although
114 this is a bug in that system, we will protect against it. */
115 if (sig < 0 || sig >= 100000)
116 write (2, "?", 1);
117 else
119 int i, value, any=0;
121 for (value=sig,i=10000; i; i /= 10)
123 if (value >= i || ((any || i==1) && !(value/i)))
125 write (2, "0123456789"+(value/i), 1);
126 if ((value/i))
127 any = 1;
128 value %= i;
133 write (2, " caught ... exiting\n", 20);
135 /* Reset action to default action and raise signal again */
136 init_one_signal (sig, SIG_DFL, 0);
137 /* Fixme: remove_lockfiles ();*/
138 #ifdef __riscos__
139 close_fds ();
140 #endif /* __riscos__ */
141 raise( sig );
143 #endif /*!HAVE_DOSISH_SYSTEM*/
145 #ifndef HAVE_DOSISH_SYSTEM
146 static RETSIGTYPE
147 got_usr_signal (int sig)
149 (void)sig;
150 caught_sigusr1 = 1;
152 #endif /*!HAVE_DOSISH_SYSTEM*/
154 void
155 gnupg_init_signals (int mode, void (*fast_cleanup)(void))
157 assert (!mode);
159 cleanup_fnc = fast_cleanup;
160 #ifndef HAVE_DOSISH_SYSTEM
161 init_one_signal (SIGINT, got_fatal_signal, 1 );
162 init_one_signal (SIGHUP, got_fatal_signal, 1 );
163 init_one_signal (SIGTERM, got_fatal_signal, 1 );
164 init_one_signal (SIGQUIT, got_fatal_signal, 1 );
165 init_one_signal (SIGSEGV, got_fatal_signal, 1 );
166 init_one_signal (SIGUSR1, got_usr_signal, 0 );
167 init_one_signal (SIGPIPE, SIG_IGN, 0 );
168 #endif
171 void
172 gnupg_pause_on_sigusr (int which)
174 #ifndef HAVE_DOSISH_SYSTEM
175 # ifdef HAVE_SIGPROCMASK
176 sigset_t mask, oldmask;
178 assert (which == 1);
179 sigemptyset( &mask );
180 sigaddset( &mask, SIGUSR1 );
182 sigprocmask( SIG_BLOCK, &mask, &oldmask );
183 while (!caught_sigusr1)
184 sigsuspend (&oldmask);
185 caught_sigusr1 = 0;
186 sigprocmask (SIG_UNBLOCK, &mask, NULL);
187 # else
188 assert (which == 1);
189 sighold (SIGUSR1);
190 while (!caught_sigusr1)
191 sigpause(SIGUSR1);
192 caught_sigusr1 = 0;
193 sigrelease(SIGUSR1);
194 # endif /*!HAVE_SIGPROCMASK*/
195 #endif
199 static void
200 do_block( int block )
202 #ifndef HAVE_DOSISH_SYSTEM
203 static int is_blocked;
204 #ifdef HAVE_SIGPROCMASK
205 static sigset_t oldmask;
207 if (block)
209 sigset_t newmask;
211 if (is_blocked)
212 log_bug ("signals are already blocked\n");
213 sigfillset( &newmask );
214 sigprocmask( SIG_BLOCK, &newmask, &oldmask );
215 is_blocked = 1;
217 else
219 if (!is_blocked)
220 log_bug("signals are not blocked\n");
221 sigprocmask (SIG_SETMASK, &oldmask, NULL);
222 is_blocked = 0;
224 #else /*!HAVE_SIGPROCMASK*/
225 static void (*disposition[MAXSIG])();
226 int sig;
228 if (block)
230 if (is_blocked)
231 log_bug("signals are already blocked\n");
232 for (sig=1; sig < MAXSIG; sig++)
234 disposition[sig] = sigset (sig, SIG_HOLD);
236 is_blocked = 1;
238 else
240 if (!is_blocked)
241 log_bug ("signals are not blocked\n");
242 for (sig=1; sig < MAXSIG; sig++) {
243 sigset (sig, disposition[sig]);
245 is_blocked = 0;
247 #endif /*!HAVE_SIGPROCMASK*/
248 #endif /*HAVE_DOSISH_SYSTEM*/
252 void
253 gnupg_block_all_signals ()
255 do_block(1);
258 void
259 gnupg_unblock_all_signals ()
261 do_block(0);