xref: /illumos-gate/usr/src/cmd/lp/cmd/lpshut.c (revision 7014882c)
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 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include <locale.h>
33 #include "stdio.h"
34 #include "signal.h"
35 #include "string.h"
36 #include "sys/types.h"
37 #include "errno.h"
38 #include "stdlib.h"
39 
40 #include "lp.h"
41 #include "msgs.h"
42 
43 #define	WHO_AM_I	I_AM_LPSHUT
44 #include "oam.h"
45 
46 void			startup(),
47 			cleanup(),
48 			done();
49 
50 /*
51  * There are no sections of code in this progam that have to be
52  * protected from interrupts. We do want to catch them, however,
53  * so we can clean up properly.
54  */
55 
56 /**
57  ** main()
58  **/
59 
60 int
61 main(int argc, char *argv[])
62 {
63 	char			msgbuf[MSGMAX];
64 	char *			tempo;
65 
66 	int			mtype;
67 
68 	short			status;
69 
70 
71 	(void) setlocale (LC_ALL, "");
72 
73 #if !defined(TEXT_DOMAIN)
74 #define TEXT_DOMAIN "SYS_TEST"
75 #endif
76 	(void) textdomain(TEXT_DOMAIN);
77 
78 	if (argc > 1)
79 		if (STREQU(argv[1], "-?")) {
80 			printf (gettext("usage: lpshut\n"));
81 			exit (0);
82 
83 		} else {
84 			LP_ERRMSG1 (ERROR, E_LP_OPTION, argv[1]);
85 			exit (1);
86 		}
87 
88 
89 	startup ();
90 
91 	if ((tempo = getenv("LPSHUT")) && STREQU(tempo, "slow"))
92 		(void)putmessage (msgbuf, S_SHUTDOWN, 0);
93 	else
94 		(void)putmessage (msgbuf, S_SHUTDOWN, 1);
95 
96 	if (msend(msgbuf) == -1) {
97 		LP_ERRMSG (ERROR, E_LP_MSEND);
98 		done (1);
99 	}
100 	if (mrecv(msgbuf, sizeof(msgbuf)) == -1) {
101 		LP_ERRMSG (ERROR, E_LP_MRECV);
102 		done (1);
103 	}
104 
105 	mtype = getmessage(msgbuf, R_SHUTDOWN, &status);
106 	if (mtype != R_SHUTDOWN) {
107 		LP_ERRMSG1 (ERROR, E_LP_BADREPLY, mtype);
108 		done (1);
109 	}
110 
111 	switch (status) {
112 
113 	case MOK:
114 		printf (gettext("Print services stopped.\n"));
115 		done (0);
116 
117 	case MNOPERM:
118 		LP_ERRMSG (WARNING, E_SHT_CANT);
119 		done (1);
120 
121 	default:
122 		LP_ERRMSG1 (ERROR, E_LP_BADSTATUS, status);
123 		done (1);
124 	}
125 	/*NOTREACHED*/
126 	return (0);
127 }
128 
129 /**
130  ** startup() - OPEN MESSAGE QUEUE TO SPOOLER
131  **/
132 
133 void			startup ()
134 {
135 	void			catch();
136 
137 	/*
138 	 * Open a private queue for messages to the Spooler.
139 	 * An error is deadly.
140 	 */
141 	if (mopen() == -1) {
142 
143 		switch (errno) {
144 		case ENOMEM:
145 		case ENOSPC:
146 			LP_ERRMSG (ERROR, E_LP_MLATER);
147 			exit (1);
148 			/*NOTREACHED*/
149 
150 		default:
151 			printf (gettext("Print services already stopped.\n"));
152 			exit (1);
153 			/*NOTREACHED*/
154 		}
155 	}
156 
157 	/*
158 	 * Now that the queue is open, quickly trap signals
159 	 * that we might get so we'll be able to close the
160 	 * queue again, regardless of what happens.
161 	 */
162 	if(signal(SIGHUP, SIG_IGN) != SIG_IGN)
163 		signal(SIGHUP, catch);
164 	if(signal(SIGINT, SIG_IGN) != SIG_IGN)
165 		signal(SIGINT, catch);
166 	if(signal(SIGQUIT, SIG_IGN) != SIG_IGN)
167 		signal(SIGQUIT, catch);
168 	if(signal(SIGTERM, SIG_IGN) != SIG_IGN)
169 		signal(SIGTERM, catch);
170 
171 	return;
172 }
173 
174 /**
175  ** catch() - CATCH INTERRUPT, HANGUP, ETC.
176  **/
177 
178 void			catch (sig)
179 	int			sig;
180 {
181 	signal (sig, SIG_IGN);
182 	done (1);
183 }
184 
185 /**
186  ** cleanup() - CLOSE THE MESSAGE QUEUE TO THE SPOOLER
187  **/
188 
189 void			cleanup ()
190 {
191 	mclose ();
192 	return;
193 }
194 
195 /**
196  ** done() - CLEANUP AND EXIT
197  **/
198 
199 void			done (ec)
200 	int			ec;
201 {
202 	cleanup ();
203 	exit (ec);
204 }
205