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 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
34 #ifndef HAVE_DOSISH_SYSTEM
35 static volatile int caught_fatal_sig
;
36 static volatile int caught_sigusr1
;
38 static void (*cleanup_fnc
)(void);
41 #ifndef HAVE_DOSISH_SYSTEM
43 init_one_signal (int sig
, RETSIGTYPE (*handler
)(int), int check_ign
)
45 # ifdef HAVE_SIGACTION
46 struct sigaction oact
, nact
;
50 /* we don't want to change an IGN handler */
51 sigaction (sig
, NULL
, &oact
);
52 if (oact
.sa_handler
== SIG_IGN
)
56 nact
.sa_handler
= handler
;
57 sigemptyset (&nact
.sa_mask
);
59 sigaction ( sig
, &nact
, NULL
);
61 RETSIGTYPE (*ohandler
)(int);
63 ohandler
= signal (sig
, handler
);
64 if (check_ign
&& ohandler
== SIG_IGN
)
66 /* Change it back if it was already set to IGN */
67 signal (sig
, SIG_IGN
);
71 #endif /*!HAVE_DOSISH_SYSTEM*/
73 #ifndef HAVE_DOSISH_SYSTEM
75 get_signal_name( int signum
)
77 /* Note that we can't use strsignal(), because it is not
79 #if HAVE_DECL_SYS_SIGLIST && defined(NSIG)
80 return (signum
>= 0 && signum
< NSIG
) ? sys_siglist
[signum
] : "?";
85 #endif /*!HAVE_DOSISH_SYSTEM*/
87 #ifndef HAVE_DOSISH_SYSTEM
89 got_fatal_signal (int sig
)
99 /* Better don't translate these messages. */
101 s
= log_get_prefix (NULL
);
103 write(2, s
, strlen (s
));
104 write (2, ": signal ", 9 );
105 s
= get_signal_name(sig
);
107 write (2, s
, strlen(s
) );
110 /* We are in a signal handler so we can't use any kind of printf
111 even not sprintf. USe a straightforward algorithm. */
112 if (sig
< 0 || sig
>= 100000)
118 for (i
=10000; i
; i
/= 10)
120 if (sig
>= i
|| ((any
|| i
==1) && !(sig
/i
)))
122 write (2, "0123456789"+(sig
/i
), 1);
130 write (2, " caught ... exiting\n", 20);
132 /* Reset action to default action and raise signal again */
133 init_one_signal (sig
, SIG_DFL
, 0);
134 /* Fixme: remove_lockfiles ();*/
137 #endif /* __riscos__ */
140 #endif /*!HAVE_DOSISH_SYSTEM*/
142 #ifndef HAVE_DOSISH_SYSTEM
144 got_usr_signal (int sig
)
148 #endif /*!HAVE_DOSISH_SYSTEM*/
151 gnupg_init_signals (int mode
, void (*fast_cleanup
)(void))
155 cleanup_fnc
= fast_cleanup
;
156 #ifndef HAVE_DOSISH_SYSTEM
157 init_one_signal (SIGINT
, got_fatal_signal
, 1 );
158 init_one_signal (SIGHUP
, got_fatal_signal
, 1 );
159 init_one_signal (SIGTERM
, got_fatal_signal
, 1 );
160 init_one_signal (SIGQUIT
, got_fatal_signal
, 1 );
161 init_one_signal (SIGSEGV
, got_fatal_signal
, 1 );
162 init_one_signal (SIGUSR1
, got_usr_signal
, 0 );
163 init_one_signal (SIGPIPE
, SIG_IGN
, 0 );
168 gnupg_pause_on_sigusr (int which
)
170 #ifndef HAVE_DOSISH_SYSTEM
171 # ifdef HAVE_SIGPROCMASK
172 sigset_t mask
, oldmask
;
175 sigemptyset( &mask
);
176 sigaddset( &mask
, SIGUSR1
);
178 sigprocmask( SIG_BLOCK
, &mask
, &oldmask
);
179 while (!caught_sigusr1
)
180 sigsuspend (&oldmask
);
182 sigprocmask (SIG_UNBLOCK
, &mask
, NULL
);
186 while (!caught_sigusr1
)
190 # endif /*!HAVE_SIGPROCMASK*/
196 do_block( int block
)
198 #ifndef HAVE_DOSISH_SYSTEM
199 static int is_blocked
;
200 #ifdef HAVE_SIGPROCMASK
201 static sigset_t oldmask
;
208 log_bug ("signals are already blocked\n");
209 sigfillset( &newmask
);
210 sigprocmask( SIG_BLOCK
, &newmask
, &oldmask
);
216 log_bug("signals are not blocked\n");
217 sigprocmask (SIG_SETMASK
, &oldmask
, NULL
);
220 #else /*!HAVE_SIGPROCMASK*/
221 static void (*disposition
[MAXSIG
])();
227 log_bug("signals are already blocked\n");
228 for (sig
=1; sig
< MAXSIG
; sig
++)
230 disposition
[sig
] = sigset (sig
, SIG_HOLD
);
237 log_bug ("signals are not blocked\n");
238 for (sig
=1; sig
< MAXSIG
; sig
++) {
239 sigset (sig
, disposition
[sig
]);
243 #endif /*!HAVE_SIGPROCMASK*/
244 #endif /*HAVE_DOSISH_SYSTEM*/
249 gnupg_block_all_signals ()
255 gnupg_unblock_all_signals ()