1*946379e7Schristos /* Emergency actions in case of a fatal signal. 2*946379e7Schristos Copyright (C) 2003-2004 Free Software Foundation, Inc. 3*946379e7Schristos Written by Bruno Haible <bruno@clisp.org>, 2003. 4*946379e7Schristos 5*946379e7Schristos This program is free software; you can redistribute it and/or modify 6*946379e7Schristos it under the terms of the GNU General Public License as published by 7*946379e7Schristos the Free Software Foundation; either version 2, or (at your option) 8*946379e7Schristos any later version. 9*946379e7Schristos 10*946379e7Schristos This program is distributed in the hope that it will be useful, 11*946379e7Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of 12*946379e7Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13*946379e7Schristos GNU General Public License for more details. 14*946379e7Schristos 15*946379e7Schristos You should have received a copy of the GNU General Public License 16*946379e7Schristos along with this program; if not, write to the Free Software Foundation, 17*946379e7Schristos Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 18*946379e7Schristos 19*946379e7Schristos 20*946379e7Schristos #ifdef __cplusplus 21*946379e7Schristos extern "C" { 22*946379e7Schristos #endif 23*946379e7Schristos 24*946379e7Schristos 25*946379e7Schristos /* It is often useful to do some cleanup action when a usually fatal signal 26*946379e7Schristos terminates the process, like removing a temporary file or killing a 27*946379e7Schristos subprocess that may be stuck waiting for a device, pipe or network input. 28*946379e7Schristos Such signals are SIGHUP, SIGINT, SIGPIPE, SIGTERM, and possibly others. 29*946379e7Schristos The limitation of this facility is that it cannot work for SIGKILL. 30*946379e7Schristos 31*946379e7Schristos Signals with a SIG_IGN handler are considered to be non-fatal. The 32*946379e7Schristos functions in this file assume that when a SIG_IGN handler is installed 33*946379e7Schristos for a signal, it was installed before any functions in this file were 34*946379e7Schristos called and it stays so for the whole lifetime of the process. */ 35*946379e7Schristos 36*946379e7Schristos /* Register a cleanup function to be executed when a catchable fatal signal 37*946379e7Schristos occurs. 38*946379e7Schristos 39*946379e7Schristos Restrictions for the cleanup function: 40*946379e7Schristos - The cleanup function can do all kinds of system calls. 41*946379e7Schristos - It can also access application dependent memory locations and data 42*946379e7Schristos structures provided they are in a consistent state. One way to ensure 43*946379e7Schristos this is through block_fatal_signals()/unblock_fatal_signals(), see 44*946379e7Schristos below. Another - more tricky - way to ensure this is the careful use 45*946379e7Schristos of 'volatile'. 46*946379e7Schristos However, 47*946379e7Schristos - malloc() and similarly complex facilities are not safe to be called 48*946379e7Schristos because they are not guaranteed to be in a consistent state. 49*946379e7Schristos - Also, the cleanup function must not block the catchable fatal signals 50*946379e7Schristos and leave them blocked upon return. 51*946379e7Schristos 52*946379e7Schristos The cleanup function is executed asynchronously. It is unspecified 53*946379e7Schristos whether during its execution the catchable fatal signals are blocked 54*946379e7Schristos or not. */ 55*946379e7Schristos extern void at_fatal_signal (void (*function) (void)); 56*946379e7Schristos 57*946379e7Schristos 58*946379e7Schristos /* Sometimes it is necessary to block the usually fatal signals while the 59*946379e7Schristos data structures being accessed by the cleanup action are being built or 60*946379e7Schristos reorganized. This is the case, for example, when a temporary file or 61*946379e7Schristos directory is created through mkstemp() or mkdtemp(), because these 62*946379e7Schristos functions create the temporary file or directory _before_ returning its 63*946379e7Schristos name to the application. */ 64*946379e7Schristos 65*946379e7Schristos /* Temporarily delay the catchable fatal signals. 66*946379e7Schristos The signals will be blocked (= delayed) until the next call to 67*946379e7Schristos unblock_fatal_signals(). If the signals are already blocked, a further 68*946379e7Schristos call to block_fatal_signals() has no effect. */ 69*946379e7Schristos extern void block_fatal_signals (void); 70*946379e7Schristos 71*946379e7Schristos /* Stop delaying the catchable fatal signals. */ 72*946379e7Schristos extern void unblock_fatal_signals (void); 73*946379e7Schristos 74*946379e7Schristos 75*946379e7Schristos #ifdef __cplusplus 76*946379e7Schristos } 77*946379e7Schristos #endif 78