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 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/>.
33 #ifndef HAVE_DOSISH_SYSTEM
34 static volatile int caught_fatal_sig
;
35 static volatile int caught_sigusr1
;
37 static void (*cleanup_fnc
)(void);
40 #ifndef HAVE_DOSISH_SYSTEM
42 init_one_signal (int sig
, RETSIGTYPE (*handler
)(int), int check_ign
)
44 # ifdef HAVE_SIGACTION
45 struct sigaction oact
, nact
;
49 /* we don't want to change an IGN handler */
50 sigaction (sig
, NULL
, &oact
);
51 if (oact
.sa_handler
== SIG_IGN
)
55 nact
.sa_handler
= handler
;
56 sigemptyset (&nact
.sa_mask
);
58 sigaction ( sig
, &nact
, NULL
);
60 RETSIGTYPE (*ohandler
)(int);
62 ohandler
= signal (sig
, handler
);
63 if (check_ign
&& ohandler
== SIG_IGN
)
65 /* Change it back if it was already set to IGN */
66 signal (sig
, SIG_IGN
);
70 #endif /*!HAVE_DOSISH_SYSTEM*/
72 #ifndef HAVE_DOSISH_SYSTEM
74 get_signal_name( int signum
)
76 /* Note that we can't use strsignal(), because it is not
78 #if HAVE_DECL_SYS_SIGLIST && defined(NSIG)
79 return (signum
>= 0 && signum
< NSIG
) ? sys_siglist
[signum
] : "?";
84 #endif /*!HAVE_DOSISH_SYSTEM*/
86 #ifndef HAVE_DOSISH_SYSTEM
88 got_fatal_signal (int sig
)
98 /* Better don't translate these messages. */
100 s
= log_get_prefix (NULL
);
102 write(2, s
, strlen (s
));
103 write (2, ": signal ", 9 );
104 s
= get_signal_name(sig
);
106 write (2, s
, strlen(s
) );
109 /* We are in a signal handler so we can't use any kind of printf
110 even not sprintf. So we use a straightforward algorithm. We
111 got a report that on one particular system, raising a signal
112 while in this handler, the parameter SIG get sclobbered and
113 things are messed up because we modify its value. Although
114 this is a bug in that system, we will protect against it. */
115 if (sig
< 0 || sig
>= 100000)
121 for (value
=sig
,i
=10000; i
; i
/= 10)
123 if (value
>= i
|| ((any
|| i
==1) && !(value
/i
)))
125 write (2, "0123456789"+(value
/i
), 1);
133 write (2, " caught ... exiting\n", 20);
135 /* Reset action to default action and raise signal again */
136 init_one_signal (sig
, SIG_DFL
, 0);
137 /* Fixme: remove_lockfiles ();*/
140 #endif /* __riscos__ */
143 #endif /*!HAVE_DOSISH_SYSTEM*/
145 #ifndef HAVE_DOSISH_SYSTEM
147 got_usr_signal (int sig
)
152 #endif /*!HAVE_DOSISH_SYSTEM*/
155 gnupg_init_signals (int mode
, void (*fast_cleanup
)(void))
159 cleanup_fnc
= fast_cleanup
;
160 #ifndef HAVE_DOSISH_SYSTEM
161 init_one_signal (SIGINT
, got_fatal_signal
, 1 );
162 init_one_signal (SIGHUP
, got_fatal_signal
, 1 );
163 init_one_signal (SIGTERM
, got_fatal_signal
, 1 );
164 init_one_signal (SIGQUIT
, got_fatal_signal
, 1 );
165 init_one_signal (SIGSEGV
, got_fatal_signal
, 1 );
166 init_one_signal (SIGUSR1
, got_usr_signal
, 0 );
167 init_one_signal (SIGPIPE
, SIG_IGN
, 0 );
172 gnupg_pause_on_sigusr (int which
)
174 #ifndef HAVE_DOSISH_SYSTEM
175 # ifdef HAVE_SIGPROCMASK
176 sigset_t mask
, oldmask
;
179 sigemptyset( &mask
);
180 sigaddset( &mask
, SIGUSR1
);
182 sigprocmask( SIG_BLOCK
, &mask
, &oldmask
);
183 while (!caught_sigusr1
)
184 sigsuspend (&oldmask
);
186 sigprocmask (SIG_UNBLOCK
, &mask
, NULL
);
190 while (!caught_sigusr1
)
194 # endif /*!HAVE_SIGPROCMASK*/
200 do_block( int block
)
202 #ifndef HAVE_DOSISH_SYSTEM
203 static int is_blocked
;
204 #ifdef HAVE_SIGPROCMASK
205 static sigset_t oldmask
;
212 log_bug ("signals are already blocked\n");
213 sigfillset( &newmask
);
214 sigprocmask( SIG_BLOCK
, &newmask
, &oldmask
);
220 log_bug("signals are not blocked\n");
221 sigprocmask (SIG_SETMASK
, &oldmask
, NULL
);
224 #else /*!HAVE_SIGPROCMASK*/
225 static void (*disposition
[MAXSIG
])();
231 log_bug("signals are already blocked\n");
232 for (sig
=1; sig
< MAXSIG
; sig
++)
234 disposition
[sig
] = sigset (sig
, SIG_HOLD
);
241 log_bug ("signals are not blocked\n");
242 for (sig
=1; sig
< MAXSIG
; sig
++) {
243 sigset (sig
, disposition
[sig
]);
247 #endif /*!HAVE_SIGPROCMASK*/
248 #endif /*HAVE_DOSISH_SYSTEM*/
253 gnupg_block_all_signals ()
259 gnupg_unblock_all_signals ()