xref: /titanic_52/usr/src/cmd/svr4pkg/pkgrm/quit.c (revision e5803b76927480e8f9b67b22201c484ccf4c2bcf)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 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 
31 #include <stdio.h>
32 #include <signal.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <locale.h>
36 #include <libintl.h>
37 #include <pkgdev.h>
38 #include <pkglib.h>
39 #include <instzones_api.h>
40 #include <libadm.h>
41 #include <libinst.h>
42 #include <messages.h>
43 #include "quit.h"
44 
45 /*
46  * imported global variables
47  */
48 
49 /* imported from main.c */
50 
51 extern struct pkgdev pkgdev;	/* holds info about the installation device */
52 
53 extern int	npkgs;		/* the number of packages yet to be installed */
54 extern int	admnflag;	/* != 0 if any pkgop admin setting failed (4) */
55 extern int	doreboot;	/* != 0 if reboot required after installation */
56 extern int	failflag;	/* != 0 if fatal error has occurred (1) */
57 extern int	intrflag;	/* != 0 if user selected quit (3) */
58 extern int	ireboot;	/* != 0 if immediate reboot required */
59 extern int	nullflag;	/* != 0 if admin interaction required (5) */
60 extern int	warnflag;	/* != 0 if non-fatal error has occurred (2) */
61 
62 /*
63  * forward declarations
64  */
65 
66 static ckreturnFunc_t	*ckreturnFunc = (ckreturnFunc_t *)NULL;
67 static char		*zoneTempDir = (char *)NULL;
68 static void		trap(int signo);
69 static zoneList_t	zoneList = (zoneList_t)NULL;
70 static int		trapEntered = 0;
71 
72 /*
73  * exported functions
74  */
75 
76 void		quit(int retcode);
77 void		quitSetCkreturnFunc(ckreturnFunc_t *a_ckreturnFunc);
78 void		quitSetZoneName(char *a_zoneName);
79 void		quitSetZoneTmpdir(char *z_zoneTempDir);
80 void		quitSetZonelist(zoneList_t a_zlst);
81 sighdlrFunc_t	*quitGetTrapHandler(void);
82 
83 /*
84  * *****************************************************************************
85  * global external (public) functions
86  * *****************************************************************************
87  */
88 
89 /*
90  * Name:	quitGetTrapHandler
91  * Description:	return address of this modules "signal trap" handler
92  * Arguments:	void
93  * Returns:	sighdlrFunc_t
94  *			The address of the trap handler that can be passed to
95  *			the signal() type system calls
96  */
97 
98 sighdlrFunc_t *
99 quitGetTrapHandler()
100 {
101 	return (&trap);
102 }
103 
104 /*
105  * Name:	quitSetCkreturnFunc
106  * Description:	set the ckreturn() interface to call when quit() is called
107  * Arguments:	a_ckreturnFunc - pointer to function to call when quit() is
108  *			called
109  * Returns:	void
110  * NOTE:	When quit() is called if a "ckreturnfunc" is set, then the first
111  *		action quit() takes is to call the "ckreturnfunc" specified with
112  *		the value passed to quit as the first argument. Quit will then
113  *		set the final return code to be used when exit() is called based
114  *		on the contents of these global variables:
115  *		 - admnflag - != 0 if any pkgop admin setting failed (4)
116  *		 - doreboot - != 0 if reboot required after installation
117  *		 - failflag - != 0 if fatal error has occurred (1)
118  *		 - intrflag - != 0 if user selected quit (3)
119  *		 - ireboot - != 0 if immediate reboot required
120  *		 - nullflag - != 0 if admin interaction required (5)
121  *		 - warnflag - != 0 if non-fatal error has occurred (2)
122  */
123 
124 void
125 quitSetCkreturnFunc(ckreturnFunc_t *a_ckreturnFunc)
126 {
127 	ckreturnFunc = a_ckreturnFunc;
128 }
129 
130 /*
131  * Name:	quitSetZonelist
132  * Description:	set the list of zones that are "locked" so that the zones can
133  *		be unlocked if quit() is called to exit
134  * Arguments:	a_zlst - list of zones that are "locked"
135  * Returns:	void
136  * NOTE:	When quit() is called, if this list is set, then z_unlock_zones
137  *		is called to unlock all of the zones in the list. If this list
138  *		is NOT set, then z_unlock_this_zone is called to unlock this
139  *		zone.
140  */
141 
142 void
143 quitSetZonelist(zoneList_t a_zlst)
144 {
145 	zoneList = a_zlst;
146 }
147 
148 /*
149  * Name:	quitSetZoneName
150  * Description:	set the zone name the program is running in
151  * Arguments:	a_zoneName - pointer to string representing the name of the zone
152  *			that the program is running in
153  * Returns:	void
154  */
155 
156 /* ARGSUSED */
157 void
158 quitSetZoneName(char *a_zoneName)
159 {
160 }
161 
162 /*
163  * Name:	quitSetZoneTmpdir
164  * Description:	set the path to the "zone temporary directory" in use
165  * Arguments:	a_zoneTempDir - pointer to string representing the full path to
166  *			the temporary directory used to hold files used during
167  *			zone operations
168  * Returns:	void
169  * NOTE:	If a zone temporary directory is set when quit() is called, the
170  *		directory is recursively removed before quit() calls exit
171  */
172 
173 void
174 quitSetZoneTmpdir(char *a_zoneTempDir)
175 {
176 	zoneTempDir = a_zoneTempDir;
177 }
178 
179 /*
180  * Name:	quit
181  * Description:	cleanup and exit
182  * Arguments:	a_retcode - the code to use to determine final exit status;
183  *			if this is NOT "99" and if a "ckreturnFunc" is
184  *			set, then that function is called with a_retcode
185  *			to set the final exit status.
186  *		Valid values are:
187  *		0 - success
188  *		1 - package operation failed (fatal error)
189  *		2 - non-fatal error (warning)
190  *		3 - user selected quit (operation interrupted)
191  *		4 - admin settings prevented operation
192  *		5 - interaction required and -n (non-interactive) specified
193  *		"10" is added to indicate "immediate reboot required"
194  *		"20" is be added to indicate "reboot after install required"
195  *		99 - do not interpret the code - just exit "99"
196  * Returns:	<<this function does not return - calls exit()>>
197  */
198 
199 void
200 quit(int retcode)
201 {
202 	/* disable interrupts */
203 
204 	(void) signal(SIGINT, SIG_IGN);
205 	(void) signal(SIGHUP, SIG_IGN);
206 
207 	if (!(restore_local_fs())) {
208 		progerr(ERR_CANNOT_RESTORE_LOCAL_FS);
209 	}
210 
211 	/* process return code if not quit(99) */
212 
213 	if (retcode != 99) {
214 		if (ckreturnFunc != (ckreturnFunc_t *)NULL) {
215 			(ckreturnFunc)(retcode);
216 		}
217 		if (failflag) {
218 			retcode = 1;
219 		} else if (warnflag) {
220 			retcode = 2;
221 		} else if (intrflag) {
222 			retcode = 3;
223 		} else if (admnflag) {
224 			retcode = 4;
225 		} else if (nullflag) {
226 			retcode = 5;
227 		} else {
228 			retcode = 0;
229 		}
230 		if (ireboot) {
231 			retcode += 20;
232 		}
233 		if (doreboot) {
234 			retcode += 10;
235 		}
236 	}
237 
238 	if (doreboot || ireboot) {
239 		ptext(stderr, gettext(MSG_REBOOT));
240 	}
241 
242 	if (pkgdev.mount) {
243 		(void) chdir("/");
244 		(void) pkgumount(&pkgdev);
245 	}
246 
247 	/* if set remove zone temporary directory */
248 
249 	if (zoneTempDir != (char *)NULL) {
250 		echoDebug(DBG_REMOVING_ZONE_TMPDIR, zoneTempDir);
251 		(void) rrmdir(zoneTempDir);
252 		zoneTempDir = (char *)NULL;
253 	}
254 
255 	/*
256 	 * issue final exit message depending on number of packages left
257 	 * to process
258 	 */
259 
260 	if (npkgs == 1) {
261 		echo(MSG_1_PKG_NOT_PROCESSED);
262 	} else if (npkgs) {
263 		echo(MSG_N_PKGS_NOT_PROCESSED, npkgs);
264 	}
265 
266 	/* if a zone list exists, unlock all zones */
267 
268 	if (zoneList != (zoneList_t)NULL) {
269 		(void) z_unlock_zones(zoneList, ZLOCKS_ALL);
270 	} else {
271 		(void) z_unlock_this_zone(ZLOCKS_ALL);
272 	}
273 
274 	/* final exit debugging message */
275 
276 	echoDebug(DBG_EXIT_WITH_CODE, retcode);
277 
278 	exit(retcode);
279 	/* NOTREACHED */
280 }
281 
282 /*
283  * *****************************************************************************
284  * static internal (private) functions
285  * *****************************************************************************
286  */
287 
288 /*
289  * Name:	trap
290  * Description:	signal handler connected via quitGetTrapHandler()
291  * Arguments:	signo - [RO, *RO] - (int)
292  *			Integer representing the signal that caused the trap
293  *			to this function to occur
294  * Returns:	<< NONE >>
295  * NOTE:	This function exits the program after doing mandatory cleanup.
296  * NOTE:	Even though quit() should NOT return, there is a call to _exit()
297  *		put after each call to quit() just in case quit() ever returned
298  *		by mistake.
299  */
300 
301 static void
302 trap(int signo)
303 {
304 	/* prevent reentrance */
305 
306 	if (trapEntered++ != 0) {
307 		return;
308 	}
309 
310 	if ((signo == SIGINT) || (signo == SIGHUP)) {
311 		quit(3);
312 		_exit(3);
313 	}
314 	quit(1);
315 	_exit(1);
316 }
317