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,
31 #ifdef HAVE_LIBREADLINE
32 #include <readline/readline.h>
33 #include <readline/history.h>
43 #ifdef HAVE_DOSISH_SYSTEM
44 void init_signals(void) {}
45 void pause_on_sigusr(int which
) {}
47 static volatile int caught_fatal_sig
= 0;
48 static volatile int caught_sigusr1
= 0;
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
;
57 /* we don't want to change an IGN handler */
58 sigaction (sig
, NULL
, &oact
);
59 if (oact
.sa_handler
== SIG_IGN
)
63 nact
.sa_handler
= handler
;
64 sigemptyset (&nact
.sa_mask
);
66 sigaction ( sig
, &nact
, NULL
);
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
);
79 got_fatal_signal( int sig
)
83 if( caught_fatal_sig
)
89 #ifdef HAVE_LIBREADLINE
90 rl_free_line_state ();
91 rl_cleanup_after_signal ();
94 /* Better don't translate these messages. */
96 s
= log_get_name(); if( s
) write(2, s
, strlen(s
) );
99 #if HAVE_DECL_SYS_SIGLIST && defined(NSIG)
100 s
= (sig
>= 0 && sig
< NSIG
) ? sys_siglist
[sig
] : "?";
101 write (2, s
, strlen(s
) );
103 write (2, "signal ", 7 );
104 if (sig
< 0 || sig
>=100)
108 write (2, "0123456789"+(sig
/10), 1 );
109 write (2, "0123456789"+(sig
%10), 1 );
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);
119 #endif /* __riscos__ */
125 got_usr_signal( int sig
)
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 );
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
);
158 sigprocmask( SIG_UNBLOCK
, &mask
, NULL
);
162 while (!caught_sigusr1
)
166 #endif /*! HAVE_SIGPROCMASK && HAVE_SIGSET_T */
169 /* Disabled - see comment in tdbio.c:tdbio_begin_transaction() */
172 do_block( int block
)
174 static int is_blocked
;
175 #if defined(HAVE_SIGPROCMASK) && defined(HAVE_SIGSET_T)
176 static sigset_t oldmask
;
182 log_bug("signals are already blocked\n");
183 sigfillset( &newmask
);
184 sigprocmask( SIG_BLOCK
, &newmask
, &oldmask
);
189 log_bug("signals are not blocked\n");
190 sigprocmask( SIG_SETMASK
, &oldmask
, NULL
);
193 #else /*! HAVE_SIGPROCMASK && HAVE_SIGSET_T */
196 #define SIGSMAX (NSIG)
197 #elif defined(MAXSIG)
198 #define SIGSMAX (MAXSIG+1)
200 #error "define SIGSMAX to the number of signals on your platform plus one"
203 static void (*disposition
[SIGSMAX
])(int);
208 log_bug("signals are already blocked\n");
209 for (sig
=1; sig
< SIGSMAX
; sig
++) {
210 disposition
[sig
] = sigset (sig
, SIG_HOLD
);
216 log_bug("signals are not blocked\n");
217 for (sig
=1; sig
< SIGSMAX
; sig
++) {
218 sigset (sig
, disposition
[sig
]);
222 #endif /*! HAVE_SIGPROCMASK && HAVE_SIGSET_T */
232 unblock_all_signals()
238 #endif /* !HAVE_DOSISH_SYSTEM */