105436638acc7c010349a69c3395f1a57c642dc62Ying Wang/* Emergency actions in case of a fatal signal.
205436638acc7c010349a69c3395f1a57c642dc62Ying Wang   Copyright (C) 2003-2004, 2009-2012 Free Software Foundation, Inc.
305436638acc7c010349a69c3395f1a57c642dc62Ying Wang   Written by Bruno Haible <bruno@clisp.org>, 2003.
405436638acc7c010349a69c3395f1a57c642dc62Ying Wang
505436638acc7c010349a69c3395f1a57c642dc62Ying Wang   This program is free software: you can redistribute it and/or modify
605436638acc7c010349a69c3395f1a57c642dc62Ying Wang   it under the terms of the GNU General Public License as published by
705436638acc7c010349a69c3395f1a57c642dc62Ying Wang   the Free Software Foundation; either version 3 of the License, or
805436638acc7c010349a69c3395f1a57c642dc62Ying Wang   (at your option) any later version.
905436638acc7c010349a69c3395f1a57c642dc62Ying Wang
1005436638acc7c010349a69c3395f1a57c642dc62Ying Wang   This program is distributed in the hope that it will be useful,
1105436638acc7c010349a69c3395f1a57c642dc62Ying Wang   but WITHOUT ANY WARRANTY; without even the implied warranty of
1205436638acc7c010349a69c3395f1a57c642dc62Ying Wang   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1305436638acc7c010349a69c3395f1a57c642dc62Ying Wang   GNU General Public License for more details.
1405436638acc7c010349a69c3395f1a57c642dc62Ying Wang
1505436638acc7c010349a69c3395f1a57c642dc62Ying Wang   You should have received a copy of the GNU General Public License
1605436638acc7c010349a69c3395f1a57c642dc62Ying Wang   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
1705436638acc7c010349a69c3395f1a57c642dc62Ying Wang
1805436638acc7c010349a69c3395f1a57c642dc62Ying Wang
1905436638acc7c010349a69c3395f1a57c642dc62Ying Wang#ifdef __cplusplus
2005436638acc7c010349a69c3395f1a57c642dc62Ying Wangextern "C" {
2105436638acc7c010349a69c3395f1a57c642dc62Ying Wang#endif
2205436638acc7c010349a69c3395f1a57c642dc62Ying Wang
2305436638acc7c010349a69c3395f1a57c642dc62Ying Wang
2405436638acc7c010349a69c3395f1a57c642dc62Ying Wang/* It is often useful to do some cleanup action when a usually fatal signal
2505436638acc7c010349a69c3395f1a57c642dc62Ying Wang   terminates the process, like removing a temporary file or killing a
2605436638acc7c010349a69c3395f1a57c642dc62Ying Wang   subprocess that may be stuck waiting for a device, pipe or network input.
2705436638acc7c010349a69c3395f1a57c642dc62Ying Wang   Such signals are SIGHUP, SIGINT, SIGPIPE, SIGTERM, and possibly others.
2805436638acc7c010349a69c3395f1a57c642dc62Ying Wang   The limitation of this facility is that it cannot work for SIGKILL.
2905436638acc7c010349a69c3395f1a57c642dc62Ying Wang
3005436638acc7c010349a69c3395f1a57c642dc62Ying Wang   Signals with a SIG_IGN handler are considered to be non-fatal.  The
3105436638acc7c010349a69c3395f1a57c642dc62Ying Wang   functions in this file assume that when a SIG_IGN handler is installed
3205436638acc7c010349a69c3395f1a57c642dc62Ying Wang   for a signal, it was installed before any functions in this file were
3305436638acc7c010349a69c3395f1a57c642dc62Ying Wang   called and it stays so for the whole lifetime of the process.  */
3405436638acc7c010349a69c3395f1a57c642dc62Ying Wang
3505436638acc7c010349a69c3395f1a57c642dc62Ying Wang/* Register a cleanup function to be executed when a catchable fatal signal
3605436638acc7c010349a69c3395f1a57c642dc62Ying Wang   occurs.
3705436638acc7c010349a69c3395f1a57c642dc62Ying Wang
3805436638acc7c010349a69c3395f1a57c642dc62Ying Wang   Restrictions for the cleanup function:
3905436638acc7c010349a69c3395f1a57c642dc62Ying Wang     - The cleanup function can do all kinds of system calls.
4005436638acc7c010349a69c3395f1a57c642dc62Ying Wang     - It can also access application dependent memory locations and data
4105436638acc7c010349a69c3395f1a57c642dc62Ying Wang       structures provided they are in a consistent state. One way to ensure
4205436638acc7c010349a69c3395f1a57c642dc62Ying Wang       this is through block_fatal_signals()/unblock_fatal_signals(), see
4305436638acc7c010349a69c3395f1a57c642dc62Ying Wang       below.  Another - more tricky - way to ensure this is the careful use
4405436638acc7c010349a69c3395f1a57c642dc62Ying Wang       of 'volatile'.
4505436638acc7c010349a69c3395f1a57c642dc62Ying Wang   However,
4605436638acc7c010349a69c3395f1a57c642dc62Ying Wang     - malloc() and similarly complex facilities are not safe to be called
4705436638acc7c010349a69c3395f1a57c642dc62Ying Wang       because they are not guaranteed to be in a consistent state.
4805436638acc7c010349a69c3395f1a57c642dc62Ying Wang     - Also, the cleanup function must not block the catchable fatal signals
4905436638acc7c010349a69c3395f1a57c642dc62Ying Wang       and leave them blocked upon return.
5005436638acc7c010349a69c3395f1a57c642dc62Ying Wang
5105436638acc7c010349a69c3395f1a57c642dc62Ying Wang   The cleanup function is executed asynchronously.  It is unspecified
5205436638acc7c010349a69c3395f1a57c642dc62Ying Wang   whether during its execution the catchable fatal signals are blocked
5305436638acc7c010349a69c3395f1a57c642dc62Ying Wang   or not.  */
5405436638acc7c010349a69c3395f1a57c642dc62Ying Wangextern void at_fatal_signal (void (*function) (void));
5505436638acc7c010349a69c3395f1a57c642dc62Ying Wang
5605436638acc7c010349a69c3395f1a57c642dc62Ying Wang
5705436638acc7c010349a69c3395f1a57c642dc62Ying Wang/* Sometimes it is necessary to block the usually fatal signals while the
5805436638acc7c010349a69c3395f1a57c642dc62Ying Wang   data structures being accessed by the cleanup action are being built or
5905436638acc7c010349a69c3395f1a57c642dc62Ying Wang   reorganized.  This is the case, for example, when a temporary file or
6005436638acc7c010349a69c3395f1a57c642dc62Ying Wang   directory is created through mkstemp() or mkdtemp(), because these
6105436638acc7c010349a69c3395f1a57c642dc62Ying Wang   functions create the temporary file or directory _before_ returning its
6205436638acc7c010349a69c3395f1a57c642dc62Ying Wang   name to the application.  */
6305436638acc7c010349a69c3395f1a57c642dc62Ying Wang
6405436638acc7c010349a69c3395f1a57c642dc62Ying Wang/* Temporarily delay the catchable fatal signals.
6505436638acc7c010349a69c3395f1a57c642dc62Ying Wang   The signals will be blocked (= delayed) until the next call to
6605436638acc7c010349a69c3395f1a57c642dc62Ying Wang   unblock_fatal_signals().  If the signals are already blocked, a further
6705436638acc7c010349a69c3395f1a57c642dc62Ying Wang   call to block_fatal_signals() has no effect.  */
6805436638acc7c010349a69c3395f1a57c642dc62Ying Wangextern void block_fatal_signals (void);
6905436638acc7c010349a69c3395f1a57c642dc62Ying Wang
7005436638acc7c010349a69c3395f1a57c642dc62Ying Wang/* Stop delaying the catchable fatal signals.  */
7105436638acc7c010349a69c3395f1a57c642dc62Ying Wangextern void unblock_fatal_signals (void);
7205436638acc7c010349a69c3395f1a57c642dc62Ying Wang
7305436638acc7c010349a69c3395f1a57c642dc62Ying Wang
7405436638acc7c010349a69c3395f1a57c642dc62Ying Wang#ifdef __cplusplus
7505436638acc7c010349a69c3395f1a57c642dc62Ying Wang}
7605436638acc7c010349a69c3395f1a57c642dc62Ying Wang#endif
77