2006-04-14 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / common / signal.c
blob2837d7b72f15dce9feb494cd9ee2a1393b931cf8
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 2 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, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
22 #include <config.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <signal.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <assert.h>
31 #include "util.h"
34 #ifndef HAVE_DOSISH_SYSTEM
35 static volatile int caught_fatal_sig;
36 static volatile int caught_sigusr1;
37 #endif
38 static void (*cleanup_fnc)(void);
41 #ifndef HAVE_DOSISH_SYSTEM
42 static void
43 init_one_signal (int sig, RETSIGTYPE (*handler)(int), int check_ign )
45 # ifdef HAVE_SIGACTION
46 struct sigaction oact, nact;
48 if (check_ign)
50 /* we don't want to change an IGN handler */
51 sigaction (sig, NULL, &oact );
52 if (oact.sa_handler == SIG_IGN )
53 return;
56 nact.sa_handler = handler;
57 sigemptyset (&nact.sa_mask);
58 nact.sa_flags = 0;
59 sigaction ( sig, &nact, NULL);
60 # else
61 RETSIGTYPE (*ohandler)(int);
63 ohandler = signal (sig, handler);
64 if (check_ign && ohandler == SIG_IGN)
66 /* Change it back if it was already set to IGN */
67 signal (sig, SIG_IGN);
69 # endif
71 #endif /*!HAVE_DOSISH_SYSTEM*/
73 #ifndef HAVE_DOSISH_SYSTEM
74 static const char *
75 get_signal_name( int signum )
77 /* Note that we can't use strsignal(), because it is not
78 reentrant. */
79 #if HAVE_DECL_SYS_SIGLIST && defined(NSIG)
80 return (signum >= 0 && signum < NSIG) ? sys_siglist[signum] : "?";
81 #else
82 return NULL;
83 #endif
85 #endif /*!HAVE_DOSISH_SYSTEM*/
87 #ifndef HAVE_DOSISH_SYSTEM
88 static RETSIGTYPE
89 got_fatal_signal (int sig)
91 const char *s;
93 if (caught_fatal_sig)
94 raise (sig);
95 caught_fatal_sig = 1;
97 if (cleanup_fnc)
98 cleanup_fnc ();
99 /* Better don't translate these messages. */
100 write (2, "\n", 1 );
101 s = log_get_prefix (NULL);
102 if (s)
103 write(2, s, strlen (s));
104 write (2, ": signal ", 9 );
105 s = get_signal_name(sig);
106 if (s)
107 write (2, s, strlen(s) );
108 else
110 /* We are in a signal handler so we can't use any kind of printf
111 even not sprintf. USe a straightforward algorithm. */
112 if (sig < 0 || sig >= 100000)
113 write (2, "?", 1);
114 else
116 int i, any=0;
118 for (i=10000; i; i /= 10)
120 if (sig >= i || ((any || i==1) && !(sig/i)))
122 write (2, "0123456789"+(sig/i), 1);
123 if ((sig/i))
124 any = 1;
125 sig %= i;
130 write (2, " caught ... exiting\n", 20);
132 /* Reset action to default action and raise signal again */
133 init_one_signal (sig, SIG_DFL, 0);
134 /* Fixme: remove_lockfiles ();*/
135 #ifdef __riscos__
136 close_fds ();
137 #endif /* __riscos__ */
138 raise( sig );
140 #endif /*!HAVE_DOSISH_SYSTEM*/
142 #ifndef HAVE_DOSISH_SYSTEM
143 static RETSIGTYPE
144 got_usr_signal (int sig)
146 caught_sigusr1 = 1;
148 #endif /*!HAVE_DOSISH_SYSTEM*/
150 void
151 gnupg_init_signals (int mode, void (*fast_cleanup)(void))
153 assert (!mode);
155 cleanup_fnc = fast_cleanup;
156 #ifndef HAVE_DOSISH_SYSTEM
157 init_one_signal (SIGINT, got_fatal_signal, 1 );
158 init_one_signal (SIGHUP, got_fatal_signal, 1 );
159 init_one_signal (SIGTERM, got_fatal_signal, 1 );
160 init_one_signal (SIGQUIT, got_fatal_signal, 1 );
161 init_one_signal (SIGSEGV, got_fatal_signal, 1 );
162 init_one_signal (SIGUSR1, got_usr_signal, 0 );
163 init_one_signal (SIGPIPE, SIG_IGN, 0 );
164 #endif
167 void
168 gnupg_pause_on_sigusr (int which)
170 #ifndef HAVE_DOSISH_SYSTEM
171 # ifdef HAVE_SIGPROCMASK
172 sigset_t mask, oldmask;
174 assert (which == 1);
175 sigemptyset( &mask );
176 sigaddset( &mask, SIGUSR1 );
178 sigprocmask( SIG_BLOCK, &mask, &oldmask );
179 while (!caught_sigusr1)
180 sigsuspend (&oldmask);
181 caught_sigusr1 = 0;
182 sigprocmask (SIG_UNBLOCK, &mask, NULL);
183 # else
184 assert (which == 1);
185 sighold (SIGUSR1);
186 while (!caught_sigusr1)
187 sigpause(SIGUSR1);
188 caught_sigusr1 = 0;
189 sigrelease(SIGUSR1);
190 # endif /*!HAVE_SIGPROCMASK*/
191 #endif
195 static void
196 do_block( int block )
198 #ifndef HAVE_DOSISH_SYSTEM
199 static int is_blocked;
200 #ifdef HAVE_SIGPROCMASK
201 static sigset_t oldmask;
203 if (block)
205 sigset_t newmask;
207 if (is_blocked)
208 log_bug ("signals are already blocked\n");
209 sigfillset( &newmask );
210 sigprocmask( SIG_BLOCK, &newmask, &oldmask );
211 is_blocked = 1;
213 else
215 if (!is_blocked)
216 log_bug("signals are not blocked\n");
217 sigprocmask (SIG_SETMASK, &oldmask, NULL);
218 is_blocked = 0;
220 #else /*!HAVE_SIGPROCMASK*/
221 static void (*disposition[MAXSIG])();
222 int sig;
224 if (block)
226 if (is_blocked)
227 log_bug("signals are already blocked\n");
228 for (sig=1; sig < MAXSIG; sig++)
230 disposition[sig] = sigset (sig, SIG_HOLD);
232 is_blocked = 1;
234 else
236 if (!is_blocked)
237 log_bug ("signals are not blocked\n");
238 for (sig=1; sig < MAXSIG; sig++) {
239 sigset (sig, disposition[sig]);
241 is_blocked = 0;
243 #endif /*!HAVE_SIGPROCMASK*/
244 #endif /*HAVE_DOSISH_SYSTEM*/
248 void
249 gnupg_block_all_signals ()
251 do_block(1);
254 void
255 gnupg_unblock_all_signals ()
257 do_block(0);