xref: /dflybsd-src/contrib/cvs-1.12/src/exithandle.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
1*86d7f5d3SJohn Marino /*
2*86d7f5d3SJohn Marino  *    Copyright (c) 2003, Derek Price and Ximbiot <http://ximbiot.com>
3*86d7f5d3SJohn Marino  *
4*86d7f5d3SJohn Marino  *    You may distribute under the terms of the GNU General Public License
5*86d7f5d3SJohn Marino  *    as specified in the README file that comes with the CVS source
6*86d7f5d3SJohn Marino  *    distribution.
7*86d7f5d3SJohn Marino  *
8*86d7f5d3SJohn Marino  * This is a convenience wrapper for some of the functions in lib/sighandle.c.
9*86d7f5d3SJohn Marino  */
10*86d7f5d3SJohn Marino 
11*86d7f5d3SJohn Marino #include "cvs.h"
12*86d7f5d3SJohn Marino 
13*86d7f5d3SJohn Marino /*
14*86d7f5d3SJohn Marino  * Register a handler for all signals.
15*86d7f5d3SJohn Marino  */
16*86d7f5d3SJohn Marino void
signals_register(RETSIGTYPE (* handler)(int))17*86d7f5d3SJohn Marino signals_register (RETSIGTYPE (*handler)(int))
18*86d7f5d3SJohn Marino {
19*86d7f5d3SJohn Marino #ifndef DONT_USE_SIGNALS
20*86d7f5d3SJohn Marino #ifdef SIGABRT
21*86d7f5d3SJohn Marino 	(void) SIG_register (SIGABRT, handler);
22*86d7f5d3SJohn Marino #endif
23*86d7f5d3SJohn Marino #ifdef SIGHUP
24*86d7f5d3SJohn Marino 	(void) SIG_register (SIGHUP, handler);
25*86d7f5d3SJohn Marino #endif
26*86d7f5d3SJohn Marino #ifdef SIGINT
27*86d7f5d3SJohn Marino 	(void) SIG_register (SIGINT, handler);
28*86d7f5d3SJohn Marino #endif
29*86d7f5d3SJohn Marino #ifdef SIGQUIT
30*86d7f5d3SJohn Marino 	(void) SIG_register (SIGQUIT, handler);
31*86d7f5d3SJohn Marino #endif
32*86d7f5d3SJohn Marino #ifdef SIGPIPE
33*86d7f5d3SJohn Marino 	(void) SIG_register (SIGPIPE, handler);
34*86d7f5d3SJohn Marino #endif
35*86d7f5d3SJohn Marino #ifdef SIGTERM
36*86d7f5d3SJohn Marino 	(void) SIG_register (SIGTERM, handler);
37*86d7f5d3SJohn Marino #endif
38*86d7f5d3SJohn Marino #endif /* !DONT_USE_SIGNALS */
39*86d7f5d3SJohn Marino }
40*86d7f5d3SJohn Marino 
41*86d7f5d3SJohn Marino 
42*86d7f5d3SJohn Marino 
43*86d7f5d3SJohn Marino /*
44*86d7f5d3SJohn Marino  * Register a handler for all signals and exit.
45*86d7f5d3SJohn Marino  */
46*86d7f5d3SJohn Marino void
cleanup_register(void (* handler)(void))47*86d7f5d3SJohn Marino cleanup_register (void (*handler) (void))
48*86d7f5d3SJohn Marino {
49*86d7f5d3SJohn Marino     atexit (handler);
50*86d7f5d3SJohn Marino 
51*86d7f5d3SJohn Marino     /* Always calling this function before any other exit handlers guarantees
52*86d7f5d3SJohn Marino      * that signals will be blocked by the time the other exit handlers are
53*86d7f5d3SJohn Marino      * called.
54*86d7f5d3SJohn Marino      *
55*86d7f5d3SJohn Marino      * SIG_beginCrSect will be called once for each handler registered via
56*86d7f5d3SJohn Marino      * cleanup_register, but there is no unregister routine for atexit() and
57*86d7f5d3SJohn Marino      * this seems like minimal overhead.
58*86d7f5d3SJohn Marino      *
59*86d7f5d3SJohn Marino      * There is no reason to unblock signals again when the exit handlers are
60*86d7f5d3SJohn Marino      * done since the program will be exiting anyhow.
61*86d7f5d3SJohn Marino      */
62*86d7f5d3SJohn Marino     atexit (SIG_beginCrSect);
63*86d7f5d3SJohn Marino }
64