Imported Upstream version 2.1.0
[platform/upstream/gpg2.git] / g10 / signal.c
1 /* signal.c - signal handling
2  * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3  *               2005 Free Software Foundation, Inc.
4  *
5  * This file is part of GnuPG.
6  *
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.
11  *
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.
16  *
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/>.
19  */
20
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>
29
30 #include "gpg.h"
31 #include "options.h"
32 #include "status.h"
33 #include "util.h"
34 #include "main.h"
35 #include "ttyio.h"
36
37 #ifdef HAVE_DOSISH_SYSTEM
38 void init_signals(void) {}
39 #else
40 static volatile int caught_fatal_sig = 0;
41 static volatile int caught_sigusr1 = 0;
42
43 static void
44 init_one_signal (int sig, RETSIGTYPE (*handler)(int), int check_ign )
45 {
46 #if defined(HAVE_SIGACTION) && defined(HAVE_STRUCT_SIGACTION)
47     struct sigaction oact, nact;
48
49     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;
54     }
55
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);
62
63     ohandler = signal (sig, handler);
64     if (check_ign && ohandler == SIG_IGN) {
65         /* Change it back if it was already set to IGN */
66         signal (sig, SIG_IGN);
67     }
68 #endif
69 }
70
71 static RETSIGTYPE
72 got_fatal_signal( int sig )
73 {
74     const char *s;
75
76     if( caught_fatal_sig )
77         raise( sig );
78     caught_fatal_sig = 1;
79
80     gcry_control (GCRYCTL_TERM_SECMEM );
81
82     tty_cleanup_rl_after_signal ();
83     tty_cleanup_after_signal ();
84
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 );
89
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 );
101     }
102 #endif
103     write(2, " caught ... exiting\n", 20 );
104
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 );
112 }
113
114
115 static RETSIGTYPE
116 got_usr_signal( int sig )
117 {
118     caught_sigusr1 = 1;
119 }
120
121
122 void
123 init_signals()
124 {
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 );
132 }
133
134
135 /* Disabled - see comment in tdbio.c:tdbio_begin_transaction() */
136 #if 0
137 static void
138 do_block( int block )
139 {
140     static int is_blocked;
141 #if defined(HAVE_SIGPROCMASK) && defined(HAVE_SIGSET_T)
142     static sigset_t oldmask;
143
144     if( block ) {
145         sigset_t newmask;
146
147         if( is_blocked )
148             log_bug("signals are already blocked\n");
149         sigfillset( &newmask );
150         sigprocmask( SIG_BLOCK, &newmask, &oldmask );
151         is_blocked = 1;
152     }
153     else {
154         if( !is_blocked )
155             log_bug("signals are not blocked\n");
156         sigprocmask( SIG_SETMASK, &oldmask, NULL );
157         is_blocked = 0;
158     }
159 #else /*! HAVE_SIGPROCMASK && HAVE_SIGSET_T */
160
161 #if defined(NSIG)
162 #define SIGSMAX (NSIG)
163 #elif defined(MAXSIG)
164 #define SIGSMAX (MAXSIG+1)
165 #else
166 #error "define SIGSMAX to the number of signals on your platform plus one"
167 #endif
168
169     static void (*disposition[SIGSMAX])(int);
170     int sig;
171
172     if( block ) {
173         if( is_blocked )
174             log_bug("signals are already blocked\n");
175         for (sig=1; sig < SIGSMAX; sig++) {
176             disposition[sig] = sigset (sig, SIG_HOLD);
177         }
178         is_blocked = 1;
179     }
180     else {
181         if( !is_blocked )
182             log_bug("signals are not blocked\n");
183         for (sig=1; sig < SIGSMAX; sig++) {
184             sigset (sig, disposition[sig]);
185         }
186         is_blocked = 0;
187     }
188 #endif /*! HAVE_SIGPROCMASK && HAVE_SIGSET_T */
189 }
190
191 void
192 block_all_signals()
193 {
194     do_block(1);
195 }
196
197 void
198 unblock_all_signals()
199 {
200     do_block(0);
201 }
202 #endif
203
204 #endif /* !HAVE_DOSISH_SYSTEM */