typo fixes.
[gnupg.git] / g10 / signal.c
blob2cabd1990942688b2907ddebaa3c108769dc8445
1 /* signal.c - signal handling
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
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 "gpg.h"
31 #include "options.h"
32 #include "status.h"
33 #include "util.h"
34 #include "main.h"
35 #include "ttyio.h"
37 #ifdef HAVE_DOSISH_SYSTEM
38 void init_signals(void) {}
39 void pause_on_sigusr(int which) {}
40 #else
41 static volatile int caught_fatal_sig = 0;
42 static volatile int caught_sigusr1 = 0;
44 static void
45 init_one_signal (int sig, RETSIGTYPE (*handler)(int), int check_ign )
47 #if defined(HAVE_SIGACTION) && defined(HAVE_STRUCT_SIGACTION)
48 struct sigaction oact, nact;
50 if (check_ign) {
51 /* we don't want to change an IGN handler */
52 sigaction (sig, NULL, &oact );
53 if (oact.sa_handler == SIG_IGN )
54 return;
57 nact.sa_handler = handler;
58 sigemptyset (&nact.sa_mask);
59 nact.sa_flags = 0;
60 sigaction ( sig, &nact, NULL);
61 #else
62 RETSIGTYPE (*ohandler)(int);
64 ohandler = signal (sig, handler);
65 if (check_ign && ohandler == SIG_IGN) {
66 /* Change it back if it was already set to IGN */
67 signal (sig, SIG_IGN);
69 #endif
72 static RETSIGTYPE
73 got_fatal_signal( int sig )
75 const char *s;
77 if( caught_fatal_sig )
78 raise( sig );
79 caught_fatal_sig = 1;
81 gcry_control (GCRYCTL_TERM_SECMEM );
83 tty_cleanup_rl_after_signal ();
85 /* Better don't translate these messages. */
86 write(2, "\n", 1 );
87 s = log_get_name(); if( s ) write(2, s, strlen(s) );
88 write(2, ": ", 2 );
90 #if HAVE_DECL_SYS_SIGLIST && defined(NSIG)
91 s = (sig >= 0 && sig < NSIG) ? sys_siglist[sig] : "?";
92 write (2, s, strlen(s) );
93 #else
94 write (2, "signal ", 7 );
95 if (sig < 0 || sig >=100)
96 write (2, "?", 1);
97 else {
98 if (sig >= 10)
99 write (2, "0123456789"+(sig/10), 1 );
100 write (2, "0123456789"+(sig%10), 1 );
102 #endif
103 write(2, " caught ... exiting\n", 20 );
105 /* Reset action to default action and raise signal again. */
106 init_one_signal (sig, SIG_DFL, 0);
107 dotlock_remove_lockfiles ();
108 #ifdef __riscos__
109 riscos_close_fds ();
110 #endif /* __riscos__ */
111 raise( sig );
115 static RETSIGTYPE
116 got_usr_signal( int sig )
118 caught_sigusr1 = 1;
122 void
123 init_signals()
125 init_one_signal (SIGINT, got_fatal_signal, 1 );
126 init_one_signal (SIGHUP, got_fatal_signal, 1 );
127 init_one_signal (SIGTERM, got_fatal_signal, 1 );
128 init_one_signal (SIGQUIT, got_fatal_signal, 1 );
129 init_one_signal (SIGSEGV, got_fatal_signal, 1 );
130 init_one_signal (SIGUSR1, got_usr_signal, 0 );
131 init_one_signal (SIGPIPE, SIG_IGN, 0 );
135 void
136 pause_on_sigusr( int which )
138 #if defined(HAVE_SIGPROCMASK) && defined(HAVE_SIGSET_T)
139 sigset_t mask, oldmask;
141 assert( which == 1 );
142 sigemptyset( &mask );
143 sigaddset( &mask, SIGUSR1 );
145 sigprocmask( SIG_BLOCK, &mask, &oldmask );
146 while( !caught_sigusr1 )
147 sigsuspend( &oldmask );
148 caught_sigusr1 = 0;
149 sigprocmask( SIG_UNBLOCK, &mask, NULL );
150 #else
151 assert (which == 1);
152 sighold (SIGUSR1);
153 while (!caught_sigusr1)
154 sigpause(SIGUSR1);
155 caught_sigusr1 = 0;
156 sigrelse(SIGUSR1);
157 #endif /*! HAVE_SIGPROCMASK && HAVE_SIGSET_T */
160 /* Disabled - see comment in tdbio.c:tdbio_begin_transaction() */
161 #if 0
162 static void
163 do_block( int block )
165 static int is_blocked;
166 #if defined(HAVE_SIGPROCMASK) && defined(HAVE_SIGSET_T)
167 static sigset_t oldmask;
169 if( block ) {
170 sigset_t newmask;
172 if( is_blocked )
173 log_bug("signals are already blocked\n");
174 sigfillset( &newmask );
175 sigprocmask( SIG_BLOCK, &newmask, &oldmask );
176 is_blocked = 1;
178 else {
179 if( !is_blocked )
180 log_bug("signals are not blocked\n");
181 sigprocmask( SIG_SETMASK, &oldmask, NULL );
182 is_blocked = 0;
184 #else /*! HAVE_SIGPROCMASK && HAVE_SIGSET_T */
186 #if defined(NSIG)
187 #define SIGSMAX (NSIG)
188 #elif defined(MAXSIG)
189 #define SIGSMAX (MAXSIG+1)
190 #else
191 #error "define SIGSMAX to the number of signals on your platform plus one"
192 #endif
194 static void (*disposition[SIGSMAX])(int);
195 int sig;
197 if( block ) {
198 if( is_blocked )
199 log_bug("signals are already blocked\n");
200 for (sig=1; sig < SIGSMAX; sig++) {
201 disposition[sig] = sigset (sig, SIG_HOLD);
203 is_blocked = 1;
205 else {
206 if( !is_blocked )
207 log_bug("signals are not blocked\n");
208 for (sig=1; sig < SIGSMAX; sig++) {
209 sigset (sig, disposition[sig]);
211 is_blocked = 0;
213 #endif /*! HAVE_SIGPROCMASK && HAVE_SIGSET_T */
216 void
217 block_all_signals()
219 do_block(1);
222 void
223 unblock_all_signals()
225 do_block(0);
227 #endif
229 #endif /* !HAVE_DOSISH_SYSTEM */