xref: /netbsd-src/external/gpl2/groff/dist/src/utils/indxbib/signal.c (revision 89a07cf815a29524268025a1139fac4c5190f765)
1*89a07cf8Schristos /*	$NetBSD: signal.c,v 1.1.1.1 2016/01/13 18:41:49 christos Exp $	*/
2*89a07cf8Schristos 
3*89a07cf8Schristos /* Copyright (C) 1992, 2001, 2003, 2004 Free Software Foundation, Inc.
4*89a07cf8Schristos      Written by James Clark (jjc@jclark.com)
5*89a07cf8Schristos 
6*89a07cf8Schristos This file is part of groff.
7*89a07cf8Schristos 
8*89a07cf8Schristos groff is free software; you can redistribute it and/or modify it under
9*89a07cf8Schristos the terms of the GNU General Public License as published by the Free
10*89a07cf8Schristos Software Foundation; either version 2, or (at your option) any later
11*89a07cf8Schristos version.
12*89a07cf8Schristos 
13*89a07cf8Schristos groff is distributed in the hope that it will be useful, but WITHOUT ANY
14*89a07cf8Schristos WARRANTY; without even the implied warranty of MERCHANTABILITY or
15*89a07cf8Schristos FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16*89a07cf8Schristos for more details.
17*89a07cf8Schristos 
18*89a07cf8Schristos You should have received a copy of the GNU General Public License along
19*89a07cf8Schristos with groff; see the file COPYING.  If not, write to the Free Software
20*89a07cf8Schristos Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */
21*89a07cf8Schristos 
22*89a07cf8Schristos /* Unfortunately vendors seem to have problems writing a <signal.h>
23*89a07cf8Schristos that is correct for C++, so we implement all signal handling in C. */
24*89a07cf8Schristos 
25*89a07cf8Schristos #include <stdlib.h>
26*89a07cf8Schristos 
27*89a07cf8Schristos #ifdef HAVE_CONFIG_H
28*89a07cf8Schristos #include <config.h>
29*89a07cf8Schristos #endif
30*89a07cf8Schristos 
31*89a07cf8Schristos #include <sys/types.h>
32*89a07cf8Schristos #include <signal.h>
33*89a07cf8Schristos #ifdef HAVE_UNISTD_H
34*89a07cf8Schristos #include <unistd.h>
35*89a07cf8Schristos #endif
36*89a07cf8Schristos 
37*89a07cf8Schristos #ifdef __cplusplus
38*89a07cf8Schristos extern "C" {
39*89a07cf8Schristos #endif
40*89a07cf8Schristos 
41*89a07cf8Schristos extern void cleanup(void);
42*89a07cf8Schristos 
handle_fatal_signal(int signum)43*89a07cf8Schristos static RETSIGTYPE handle_fatal_signal(int signum)
44*89a07cf8Schristos {
45*89a07cf8Schristos   signal(signum, SIG_DFL);
46*89a07cf8Schristos   cleanup();
47*89a07cf8Schristos #ifdef HAVE_KILL
48*89a07cf8Schristos   kill(getpid(), signum);
49*89a07cf8Schristos #else
50*89a07cf8Schristos   /* MS-DOS and Win32 don't have kill(); the best compromise is
51*89a07cf8Schristos      probably to use exit() instead. */
52*89a07cf8Schristos   exit(signum);
53*89a07cf8Schristos #endif
54*89a07cf8Schristos }
55*89a07cf8Schristos 
catch_fatal_signals(void)56*89a07cf8Schristos void catch_fatal_signals(void)
57*89a07cf8Schristos {
58*89a07cf8Schristos #ifdef SIGHUP
59*89a07cf8Schristos   signal(SIGHUP, handle_fatal_signal);
60*89a07cf8Schristos #endif
61*89a07cf8Schristos   signal(SIGINT, handle_fatal_signal);
62*89a07cf8Schristos   signal(SIGTERM, handle_fatal_signal);
63*89a07cf8Schristos }
64*89a07cf8Schristos 
65*89a07cf8Schristos #ifdef __cplusplus
66*89a07cf8Schristos }
67*89a07cf8Schristos #endif
68*89a07cf8Schristos 
69*89a07cf8Schristos #ifndef HAVE_RENAME
70*89a07cf8Schristos 
ignore_fatal_signals()71*89a07cf8Schristos void ignore_fatal_signals()
72*89a07cf8Schristos {
73*89a07cf8Schristos #ifdef SIGHUP
74*89a07cf8Schristos   signal(SIGHUP, SIG_IGN);
75*89a07cf8Schristos #endif
76*89a07cf8Schristos   signal(SIGINT, SIG_IGN);
77*89a07cf8Schristos   signal(SIGTERM, SIG_IGN);
78*89a07cf8Schristos }
79*89a07cf8Schristos 
80*89a07cf8Schristos #endif /* not HAVE_RENAME */
81