xref: /titanic_52/usr/src/cmd/lp/cmd/lpadmin/signals.c (revision c7158ae983f5a04c4a998f468ecefba6d23ba721)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 #ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.5	*/
27 
28 #include "signal.h"
29 
30 #include "lpadmin.h"
31 
32 static int		trapping	= -1;	/* -1 means first time */
33 
34 static
35 #ifdef	SIGPOLL
36 	void
37 #else
38 	int
39 #endif
40 			(*old_sighup)(),
41 			(*old_sigint)(),
42 			(*old_sigquit)(),
43 			(*old_sigterm)();
44 
45 /**
46  ** catch() - CLEAN UP AFTER SIGNAL
47  **/
48 
49 static void
50 catch (sig)
51 {
52 	(void)signal (SIGHUP, SIG_IGN);
53 	(void)signal (SIGINT, SIG_IGN);
54 	(void)signal (SIGQUIT, SIG_IGN);
55 	(void)signal (SIGTERM, SIG_IGN);
56 	done (1);
57 }
58 
59 /**
60  ** trap_signals() - SET SIGNALS TO BE CAUGHT FOR CLEAN EXIT
61  **/
62 
63 void			trap_signals ()
64 {
65 	switch (trapping) {
66 
67 	case -1:	/* first time */
68 
69 #define	SETSIG(SIG) \
70 		if (signal(SIG, SIG_IGN) != SIG_IGN) \
71 			signal (SIG, catch);
72 
73 		SETSIG (SIGHUP);
74 		SETSIG (SIGINT);
75 		SETSIG (SIGQUIT);
76 		SETSIG (SIGTERM);
77 		break;
78 
79 	case 0:		/* currently ignoring */
80 		signal (SIGHUP, old_sighup);
81 		signal (SIGINT, old_sigint);
82 		signal (SIGQUIT, old_sigquit);
83 		signal (SIGTERM, old_sigterm);
84 		trapping = 1;
85 		break;
86 
87 	case 1:		/* already trapping */
88 		break;
89 
90 	}
91 	return;
92 }
93 
94 /**
95  ** ignore_signals() - SET SIGNALS TO BE IGNORED FOR CRITICAL SECTIONS
96  **/
97 
98 void			ignore_signals ()
99 {
100 	switch (trapping) {
101 
102 	case -1:	/* first time */
103 		trap_signals ();
104 		/*fall through*/
105 
106 	case 1:		/* currently trapping */
107 		old_sighup = signal(SIGHUP, SIG_IGN);
108 		old_sigint = signal(SIGINT, SIG_IGN);
109 		old_sigquit = signal(SIGQUIT, SIG_IGN);
110 		old_sigterm = signal(SIGTERM, SIG_IGN);
111 		trapping = 0;
112 		break;
113 
114 	case 0:		/* already ignoring */
115 		break;
116 
117 	}
118 	return;
119 }
120