xref: /titanic_51/usr/src/cmd/svr4pkg/pkgrm/quit.c (revision 1e49577a7fcde812700ded04431b49d67cc57d6d)
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 intfRelocFunc_t	*intfRelocFunc = (intfRelocFunc_t *)NULL;
68 static char		*zoneTempDir = (char *)NULL;
69 static void		trap(int signo);
70 static zoneList_t	zoneList = (zoneList_t)NULL;
71 static int		trapEntered = 0;
72 
73 /*
74  * exported functions
75  */
76 
77 void		quit(int retcode);
78 void		quitSetCkreturnFunc(ckreturnFunc_t *a_ckreturnFunc);
79 void		quitSetZoneName(char *a_zoneName);
80 void		quitSetZoneTmpdir(char *z_zoneTempDir);
81 void		quitSetZonelist(zoneList_t a_zlst);
82 sighdlrFunc_t	*quitGetTrapHandler(void);
83 
84 /*
85  * *****************************************************************************
86  * global external (public) functions
87  * *****************************************************************************
88  */
89 
90 /*
91  * Name:	quitGetTrapHandler
92  * Description:	return address of this modules "signal trap" handler
93  * Arguments:	void
94  * Returns:	sighdlrFunc_t
95  *			The address of the trap handler that can be passed to
96  *			the signal() type system calls
97  */
98 
99 sighdlrFunc_t *
100 quitGetTrapHandler()
101 {
102 	return (&trap);
103 }
104 
105 /*
106  * Name:	quitSetIntfReloc
107  * Description:	set the "intf_reloc" interface to run when quit() is called
108  * Arguments:	a_intfReloc - pointer to function to call when quit() is called
109  * Returns:	void
110  * NOTE:	When quit() is called, if an "intf_reloc" function is set, quit
111  *		will call that function to perform whatever operations it needs
112  *		to perform - typically this is needed to run "intf_reloc" when
113  *		pre-SVR4 packages have been removed
114  */
115 
116 void
117 quitSetIntfReloc(intfRelocFunc_t *a_intfReloc)
118 {
119 	intfRelocFunc = a_intfReloc;
120 }
121 
122 /*
123  * Name:	quitSetCkreturnFunc
124  * Description:	set the ckreturn() interface to call when quit() is called
125  * Arguments:	a_ckreturnFunc - pointer to function to call when quit() is
126  *			called
127  * Returns:	void
128  * NOTE:	When quit() is called if a "ckreturnfunc" is set, then the first
129  *		action quit() takes is to call the "ckreturnfunc" specified with
130  *		the value passed to quit as the first argument. Quit will then
131  *		set the final return code to be used when exit() is called based
132  *		on the contents of these global variables:
133  *		 - admnflag - != 0 if any pkgop admin setting failed (4)
134  *		 - doreboot - != 0 if reboot required after installation
135  *		 - failflag - != 0 if fatal error has occurred (1)
136  *		 - intrflag - != 0 if user selected quit (3)
137  *		 - ireboot - != 0 if immediate reboot required
138  *		 - nullflag - != 0 if admin interaction required (5)
139  *		 - warnflag - != 0 if non-fatal error has occurred (2)
140  */
141 
142 void
143 quitSetCkreturnFunc(ckreturnFunc_t *a_ckreturnFunc)
144 {
145 	ckreturnFunc = a_ckreturnFunc;
146 }
147 
148 /*
149  * Name:	quitSetZonelist
150  * Description:	set the list of zones that are "locked" so that the zones can
151  *		be unlocked if quit() is called to exit
152  * Arguments:	a_zlst - list of zones that are "locked"
153  * Returns:	void
154  * NOTE:	When quit() is called, if this list is set, then z_unlock_zones
155  *		is called to unlock all of the zones in the list. If this list
156  *		is NOT set, then z_unlock_this_zone is called to unlock this
157  *		zone.
158  */
159 
160 void
161 quitSetZonelist(zoneList_t a_zlst)
162 {
163 	zoneList = a_zlst;
164 }
165 
166 /*
167  * Name:	quitSetZoneName
168  * Description:	set the zone name the program is running in
169  * Arguments:	a_zoneName - pointer to string representing the name of the zone
170  *			that the program is running in
171  * Returns:	void
172  */
173 
174 /* ARGSUSED */
175 void
176 quitSetZoneName(char *a_zoneName)
177 {
178 }
179 
180 /*
181  * Name:	quitSetZoneTmpdir
182  * Description:	set the path to the "zone temporary directory" in use
183  * Arguments:	a_zoneTempDir - pointer to string representing the full path to
184  *			the temporary directory used to hold files used during
185  *			zone operations
186  * Returns:	void
187  * NOTE:	If a zone temporary directory is set when quit() is called, the
188  *		directory is recursively removed before quit() calls exit
189  */
190 
191 void
192 quitSetZoneTmpdir(char *a_zoneTempDir)
193 {
194 	zoneTempDir = a_zoneTempDir;
195 }
196 
197 /*
198  * Name:	quit
199  * Description:	cleanup and exit
200  * Arguments:	a_retcode - the code to use to determine final exit status;
201  *			if this is NOT "99" and if a "ckreturnFunc" is
202  *			set, then that function is called with a_retcode
203  *			to set the final exit status.
204  *		Valid values are:
205  *		0 - success
206  *		1 - package operation failed (fatal error)
207  *		2 - non-fatal error (warning)
208  *		3 - user selected quit (operation interrupted)
209  *		4 - admin settings prevented operation
210  *		5 - interaction required and -n (non-interactive) specified
211  *		"10" is added to indicate "immediate reboot required"
212  *		"20" is be added to indicate "reboot after install required"
213  *		99 - do not interpret the code - just exit "99"
214  * Returns:	<<this function does not return - calls exit()>>
215  */
216 
217 void
218 quit(int retcode)
219 {
220 	/* disable interrupts */
221 
222 	(void) signal(SIGINT, SIG_IGN);
223 	(void) signal(SIGHUP, SIG_IGN);
224 
225 	if (!(restore_local_fs())) {
226 		progerr(ERR_CANNOT_RESTORE_LOCAL_FS);
227 	}
228 
229 	/* process return code if not quit(99) */
230 
231 	if (retcode != 99) {
232 		if (ckreturnFunc != (ckreturnFunc_t *)NULL) {
233 			(ckreturnFunc)(retcode);
234 		}
235 		if (failflag) {
236 			retcode = 1;
237 		} else if (warnflag) {
238 			retcode = 2;
239 		} else if (intrflag) {
240 			retcode = 3;
241 		} else if (admnflag) {
242 			retcode = 4;
243 		} else if (nullflag) {
244 			retcode = 5;
245 		} else {
246 			retcode = 0;
247 		}
248 		if (ireboot) {
249 			retcode += 20;
250 		}
251 		if (doreboot) {
252 			retcode += 10;
253 		}
254 	}
255 
256 	if (doreboot || ireboot) {
257 		ptext(stderr, gettext(MSG_REBOOT));
258 	}
259 
260 	if (pkgdev.mount) {
261 		(void) chdir("/");
262 		(void) pkgumount(&pkgdev);
263 	}
264 
265 	/* if set remove zone temporary directory */
266 
267 	if (zoneTempDir != (char *)NULL) {
268 		echoDebug(DBG_REMOVING_ZONE_TMPDIR, zoneTempDir);
269 		(void) rrmdir(zoneTempDir);
270 		zoneTempDir = (char *)NULL;
271 	}
272 
273 	/*
274 	 * issue final exit message depending on number of packages left
275 	 * to process
276 	 */
277 
278 	if (npkgs == 1) {
279 		echo(MSG_1_PKG_NOT_PROCESSED);
280 	} else if (npkgs) {
281 		echo(MSG_N_PKGS_NOT_PROCESSED, npkgs);
282 	}
283 
284 	/* call intf_reloc function if registered */
285 
286 	if (intfRelocFunc != (intfRelocFunc_t *)NULL) {
287 		(intfRelocFunc)();
288 	}
289 
290 	/* if a zone list exists, unlock all zones */
291 
292 	if (zoneList != (zoneList_t)NULL) {
293 		(void) z_unlock_zones(zoneList, ZLOCKS_ALL);
294 	} else {
295 		(void) z_unlock_this_zone(ZLOCKS_ALL);
296 	}
297 
298 	/* final exit debugging message */
299 
300 	echoDebug(DBG_EXIT_WITH_CODE, retcode);
301 
302 	exit(retcode);
303 	/* NOTREACHED */
304 }
305 
306 /*
307  * *****************************************************************************
308  * static internal (private) functions
309  * *****************************************************************************
310  */
311 
312 /*
313  * Name:	trap
314  * Description:	signal handler connected via quitGetTrapHandler()
315  * Arguments:	signo - [RO, *RO] - (int)
316  *			Integer representing the signal that caused the trap
317  *			to this function to occur
318  * Returns:	<< NONE >>
319  * NOTE:	This function exits the program after doing mandatory cleanup.
320  * NOTE:	Even though quit() should NOT return, there is a call to _exit()
321  *		put after each call to quit() just in case quit() ever returned
322  *		by mistake.
323  */
324 
325 static void
326 trap(int signo)
327 {
328 	/* prevent reentrance */
329 
330 	if (trapEntered++ != 0) {
331 		return;
332 	}
333 
334 	if ((signo == SIGINT) || (signo == SIGHUP)) {
335 		quit(3);
336 		_exit(3);
337 	}
338 	quit(1);
339 	_exit(1);
340 }
341