Imported Upstream version 2.0.27
[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 void pause_on_sigusr(int which) {}
40 #else
41 static volatile int caught_fatal_sig = 0;
42 static volatile int caught_sigusr1 = 0;
43
44 static void
45 init_one_signal (int sig, RETSIGTYPE (*handler)(int), int check_ign )
46 {
47 #if defined(HAVE_SIGACTION) && defined(HAVE_STRUCT_SIGACTION)
48     struct sigaction oact, nact;
49
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;
55     }
56
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);
63
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);
68     }
69 #endif
70 }
71
72 static RETSIGTYPE
73 got_fatal_signal( int sig )
74 {
75     const char *s;
76
77     if( caught_fatal_sig )
78         raise( sig );
79     caught_fatal_sig = 1;
80
81     gcry_control (GCRYCTL_TERM_SECMEM );
82
83     tty_cleanup_rl_after_signal ();
84     tty_cleanup_after_signal ();
85
86     /* Better don't translate these messages. */
87     write(2, "\n", 1 );
88     s = log_get_name(); if( s ) write(2, s, strlen(s) );
89     write(2, ": ", 2 );
90
91 #if HAVE_DECL_SYS_SIGLIST && defined(NSIG)
92     s = (sig >= 0 && sig < NSIG) ? sys_siglist[sig] : "?";
93     write (2, s, strlen(s) );
94 #else
95     write (2, "signal ", 7 );
96     if (sig < 0 || sig >=100)
97         write (2, "?", 1);
98     else {
99         if (sig >= 10)
100             write (2, "0123456789"+(sig/10), 1 );
101         write (2, "0123456789"+(sig%10), 1 );
102     }
103 #endif
104     write(2, " caught ... exiting\n", 20 );
105
106     /* Reset action to default action and raise signal again. */
107     init_one_signal (sig, SIG_DFL, 0);
108     dotlock_remove_lockfiles ();
109 #ifdef __riscos__
110     riscos_close_fds ();
111 #endif /* __riscos__ */
112     raise( sig );
113 }
114
115
116 static RETSIGTYPE
117 got_usr_signal( int sig )
118 {
119     caught_sigusr1 = 1;
120 }
121
122
123 void
124 init_signals()
125 {
126     init_one_signal (SIGINT, got_fatal_signal, 1 );
127     init_one_signal (SIGHUP, got_fatal_signal, 1 );
128     init_one_signal (SIGTERM, got_fatal_signal, 1 );
129     init_one_signal (SIGQUIT, got_fatal_signal, 1 );
130     init_one_signal (SIGSEGV, got_fatal_signal, 1 );
131     init_one_signal (SIGUSR1, got_usr_signal, 0 );
132     init_one_signal (SIGPIPE, SIG_IGN, 0 );
133 }
134
135
136 void
137 pause_on_sigusr( int which )
138 {
139 #if defined(HAVE_SIGPROCMASK) && defined(HAVE_SIGSET_T)
140     sigset_t mask, oldmask;
141
142     assert( which == 1 );
143     sigemptyset( &mask );
144     sigaddset( &mask, SIGUSR1 );
145
146     sigprocmask( SIG_BLOCK, &mask, &oldmask );
147     while( !caught_sigusr1 )
148         sigsuspend( &oldmask );
149     caught_sigusr1 = 0;
150     sigprocmask( SIG_UNBLOCK, &mask, NULL );
151 #else 
152      assert (which == 1);
153      sighold (SIGUSR1);
154      while (!caught_sigusr1)
155          sigpause(SIGUSR1);
156      caught_sigusr1 = 0;
157      sigrelse(SIGUSR1);
158 #endif /*! HAVE_SIGPROCMASK && HAVE_SIGSET_T */
159 }
160
161 /* Disabled - see comment in tdbio.c:tdbio_begin_transaction() */
162 #if 0
163 static void
164 do_block( int block )
165 {
166     static int is_blocked;
167 #if defined(HAVE_SIGPROCMASK) && defined(HAVE_SIGSET_T)
168     static sigset_t oldmask;
169
170     if( block ) {
171         sigset_t newmask;
172
173         if( is_blocked )
174             log_bug("signals are already blocked\n");
175         sigfillset( &newmask );
176         sigprocmask( SIG_BLOCK, &newmask, &oldmask );
177         is_blocked = 1;
178     }
179     else {
180         if( !is_blocked )
181             log_bug("signals are not blocked\n");
182         sigprocmask( SIG_SETMASK, &oldmask, NULL );
183         is_blocked = 0;
184     }
185 #else /*! HAVE_SIGPROCMASK && HAVE_SIGSET_T */
186
187 #if defined(NSIG)
188 #define SIGSMAX (NSIG)
189 #elif defined(MAXSIG)
190 #define SIGSMAX (MAXSIG+1)
191 #else
192 #error "define SIGSMAX to the number of signals on your platform plus one"
193 #endif
194
195     static void (*disposition[SIGSMAX])(int);
196     int sig;
197
198     if( block ) {
199         if( is_blocked )
200             log_bug("signals are already blocked\n");
201         for (sig=1; sig < SIGSMAX; sig++) {
202             disposition[sig] = sigset (sig, SIG_HOLD);
203         }
204         is_blocked = 1;
205     }
206     else {
207         if( !is_blocked )
208             log_bug("signals are not blocked\n");
209         for (sig=1; sig < SIGSMAX; sig++) {
210             sigset (sig, disposition[sig]);
211         }
212         is_blocked = 0;
213     }
214 #endif /*! HAVE_SIGPROCMASK && HAVE_SIGSET_T */
215 }
216
217 void
218 block_all_signals()
219 {
220     do_block(1);
221 }
222
223 void
224 unblock_all_signals()
225 {
226     do_block(0);
227 }
228 #endif
229
230 #endif /* !HAVE_DOSISH_SYSTEM */