* keyserver.c (path_makes_direct): New. (keyserver_spawn): Used here
[gnupg.git] / g10 / signal.c
blob2e40af13a57f999f46db642c548187194d5e73fa
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 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 * USA.
23 #include <config.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <signal.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <assert.h>
31 #ifdef HAVE_LIBREADLINE
32 #include <readline/readline.h>
33 #include <readline/history.h>
34 #endif
36 #include "options.h"
37 #include "errors.h"
38 #include "memory.h"
39 #include "util.h"
40 #include "main.h"
41 #include "ttyio.h"
43 #ifdef HAVE_DOSISH_SYSTEM
44 void init_signals(void) {}
45 void pause_on_sigusr(int which) {}
46 #else
47 static volatile int caught_fatal_sig = 0;
48 static volatile int caught_sigusr1 = 0;
50 static void
51 init_one_signal (int sig, RETSIGTYPE (*handler)(int), int check_ign )
53 #if defined(HAVE_SIGACTION) && defined(HAVE_STRUCT_SIGACTION)
54 struct sigaction oact, nact;
56 if (check_ign) {
57 /* we don't want to change an IGN handler */
58 sigaction (sig, NULL, &oact );
59 if (oact.sa_handler == SIG_IGN )
60 return;
63 nact.sa_handler = handler;
64 sigemptyset (&nact.sa_mask);
65 nact.sa_flags = 0;
66 sigaction ( sig, &nact, NULL);
67 #else
68 RETSIGTYPE (*ohandler)(int);
70 ohandler = signal (sig, handler);
71 if (check_ign && ohandler == SIG_IGN) {
72 /* Change it back if it was already set to IGN */
73 signal (sig, SIG_IGN);
75 #endif
78 static RETSIGTYPE
79 got_fatal_signal( int sig )
81 const char *s;
83 if( caught_fatal_sig )
84 raise( sig );
85 caught_fatal_sig = 1;
87 secmem_term();
89 #ifdef HAVE_LIBREADLINE
90 rl_free_line_state ();
91 rl_cleanup_after_signal ();
92 #endif
94 /* Better don't translate these messages. */
95 write(2, "\n", 1 );
96 s = log_get_name(); if( s ) write(2, s, strlen(s) );
97 write(2, ": ", 2 );
99 #if HAVE_DECL_SYS_SIGLIST && defined(NSIG)
100 s = (sig >= 0 && sig < NSIG) ? sys_siglist[sig] : "?";
101 write (2, s, strlen(s) );
102 #else
103 write (2, "signal ", 7 );
104 if (sig < 0 || sig >=100)
105 write (2, "?", 1);
106 else {
107 if (sig >= 10)
108 write (2, "0123456789"+(sig/10), 1 );
109 write (2, "0123456789"+(sig%10), 1 );
111 #endif
112 write(2, " caught ... exiting\n", 20 );
114 /* Reset action to default action and raise signal again. */
115 init_one_signal (sig, SIG_DFL, 0);
116 remove_lockfiles ();
117 #ifdef __riscos__
118 riscos_close_fds ();
119 #endif /* __riscos__ */
120 raise( sig );
124 static RETSIGTYPE
125 got_usr_signal( int sig )
127 caught_sigusr1 = 1;
131 void
132 init_signals()
134 init_one_signal (SIGINT, got_fatal_signal, 1 );
135 init_one_signal (SIGHUP, got_fatal_signal, 1 );
136 init_one_signal (SIGTERM, got_fatal_signal, 1 );
137 init_one_signal (SIGQUIT, got_fatal_signal, 1 );
138 init_one_signal (SIGSEGV, got_fatal_signal, 1 );
139 init_one_signal (SIGUSR1, got_usr_signal, 0 );
140 init_one_signal (SIGPIPE, SIG_IGN, 0 );
144 void
145 pause_on_sigusr( int which )
147 #if defined(HAVE_SIGPROCMASK) && defined(HAVE_SIGSET_T)
148 sigset_t mask, oldmask;
150 assert( which == 1 );
151 sigemptyset( &mask );
152 sigaddset( &mask, SIGUSR1 );
154 sigprocmask( SIG_BLOCK, &mask, &oldmask );
155 while( !caught_sigusr1 )
156 sigsuspend( &oldmask );
157 caught_sigusr1 = 0;
158 sigprocmask( SIG_UNBLOCK, &mask, NULL );
159 #else
160 assert (which == 1);
161 sighold (SIGUSR1);
162 while (!caught_sigusr1)
163 sigpause(SIGUSR1);
164 caught_sigusr1 = 0;
165 sigrelse(SIGUSR1);
166 #endif /*! HAVE_SIGPROCMASK && HAVE_SIGSET_T */
169 /* Disabled - see comment in tdbio.c:tdbio_begin_transaction() */
170 #if 0
171 static void
172 do_block( int block )
174 static int is_blocked;
175 #if defined(HAVE_SIGPROCMASK) && defined(HAVE_SIGSET_T)
176 static sigset_t oldmask;
178 if( block ) {
179 sigset_t newmask;
181 if( is_blocked )
182 log_bug("signals are already blocked\n");
183 sigfillset( &newmask );
184 sigprocmask( SIG_BLOCK, &newmask, &oldmask );
185 is_blocked = 1;
187 else {
188 if( !is_blocked )
189 log_bug("signals are not blocked\n");
190 sigprocmask( SIG_SETMASK, &oldmask, NULL );
191 is_blocked = 0;
193 #else /*! HAVE_SIGPROCMASK && HAVE_SIGSET_T */
195 #if defined(NSIG)
196 #define SIGSMAX (NSIG)
197 #elif defined(MAXSIG)
198 #define SIGSMAX (MAXSIG+1)
199 #else
200 #error "define SIGSMAX to the number of signals on your platform plus one"
201 #endif
203 static void (*disposition[SIGSMAX])(int);
204 int sig;
206 if( block ) {
207 if( is_blocked )
208 log_bug("signals are already blocked\n");
209 for (sig=1; sig < SIGSMAX; sig++) {
210 disposition[sig] = sigset (sig, SIG_HOLD);
212 is_blocked = 1;
214 else {
215 if( !is_blocked )
216 log_bug("signals are not blocked\n");
217 for (sig=1; sig < SIGSMAX; sig++) {
218 sigset (sig, disposition[sig]);
220 is_blocked = 0;
222 #endif /*! HAVE_SIGPROCMASK && HAVE_SIGSET_T */
225 void
226 block_all_signals()
228 do_block(1);
231 void
232 unblock_all_signals()
234 do_block(0);
236 #endif
238 #endif /* !HAVE_DOSISH_SYSTEM */