xref: /illumos-gate/usr/src/cmd/lp/cmd/lpadmin/signals.c (revision 6241c4c0)
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 #include "signal.h"
26 
27 #include "lpadmin.h"
28 
29 static int		trapping	= -1;	/* -1 means first time */
30 
31 static
32 #ifdef	SIGPOLL
33 	void
34 #else
35 	int
36 #endif
37 			(*old_sighup)(),
38 			(*old_sigint)(),
39 			(*old_sigquit)(),
40 			(*old_sigterm)();
41 
42 /**
43  ** catch() - CLEAN UP AFTER SIGNAL
44  **/
45 
46 static void
catch(int sig __unused)47 catch (int sig __unused)
48 {
49 	(void)signal (SIGHUP, SIG_IGN);
50 	(void)signal (SIGINT, SIG_IGN);
51 	(void)signal (SIGQUIT, SIG_IGN);
52 	(void)signal (SIGTERM, SIG_IGN);
53 	done (1);
54 }
55 
56 /**
57  ** trap_signals() - SET SIGNALS TO BE CAUGHT FOR CLEAN EXIT
58  **/
59 
trap_signals()60 void			trap_signals ()
61 {
62 	switch (trapping) {
63 
64 	case -1:	/* first time */
65 
66 #define	SETSIG(SIG) \
67 		if (signal(SIG, SIG_IGN) != SIG_IGN) \
68 			signal (SIG, catch);
69 
70 		SETSIG (SIGHUP);
71 		SETSIG (SIGINT);
72 		SETSIG (SIGQUIT);
73 		SETSIG (SIGTERM);
74 		break;
75 
76 	case 0:		/* currently ignoring */
77 		signal (SIGHUP, old_sighup);
78 		signal (SIGINT, old_sigint);
79 		signal (SIGQUIT, old_sigquit);
80 		signal (SIGTERM, old_sigterm);
81 		trapping = 1;
82 		break;
83 
84 	case 1:		/* already trapping */
85 		break;
86 
87 	}
88 	return;
89 }
90 
91 /**
92  ** ignore_signals() - SET SIGNALS TO BE IGNORED FOR CRITICAL SECTIONS
93  **/
94 
ignore_signals()95 void			ignore_signals ()
96 {
97 	switch (trapping) {
98 
99 	case -1:	/* first time */
100 		trap_signals ();
101 		/*fall through*/
102 
103 	case 1:		/* currently trapping */
104 		old_sighup = signal(SIGHUP, SIG_IGN);
105 		old_sigint = signal(SIGINT, SIG_IGN);
106 		old_sigquit = signal(SIGQUIT, SIG_IGN);
107 		old_sigterm = signal(SIGTERM, SIG_IGN);
108 		trapping = 0;
109 		break;
110 
111 	case 0:		/* already ignoring */
112 		break;
113 
114 	}
115 	return;
116 }
117