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