xref: /titanic_44/usr/src/cmd/svr4pkg/pkgadd/quit.c (revision af4c679f647cf088543c762e33d41a3ac52cfa14)
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 #include <stdio.h>
31 #include <signal.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <pkgdev.h>
35 #include <locale.h>
36 #include <libintl.h>
37 
38 #include <pkglib.h>
39 #include <pkgweb.h>
40 #include <messages.h>
41 
42 #include <libadm.h>
43 #include <libinst.h>
44 
45 #include "quit.h"
46 
47 /*
48  * imported global variables
49  */
50 
51 /* imported from main.c */
52 
53 extern struct pkgdev pkgdev;	/* holds info about the installation device */
54 
55 extern int	npkgs;		/* the number of packages yet to be installed */
56 extern int	admnflag;	/* != 0 if any pkgop admin setting failed (4) */
57 extern int	doreboot;	/* != 0 if reboot required after installation */
58 extern int	failflag;	/* != 0 if fatal error has occurred (1) */
59 extern int	intrflag;	/* != 0 if user selected quit (3) */
60 extern int	ireboot;	/* != 0 if immediate reboot required */
61 extern int	nullflag;	/* != 0 if admin interaction required (5) */
62 extern int	warnflag;	/* != 0 if non-fatal error has occurred (2) */
63 
64 /*
65  * forward declarations
66  */
67 
68 static char		*dwnldTempDir = (char *)NULL;
69 static char		*idsName = (char *)NULL;
70 static char		*zoneTempDir = (char *)NULL;
71 static ckreturnFunc_t	*ckreturnFunc = (ckreturnFunc_t *)NULL;
72 static int		trapEntered = 0;
73 static intfRelocFunc_t	*intfRelocFunc = (intfRelocFunc_t *)NULL;
74 static void		trap(int signo);
75 static zoneList_t	zoneList = (zoneList_t)NULL;
76 
77 /*
78  * exported functions
79  */
80 
81 void		quit(int retcode);
82 void		quitSetCkreturnFunc(ckreturnFunc_t *a_ckreturnFunc);
83 void		quitSetDwnldTmpdir(char *a_dwnldTempDir);
84 void		quitSetIdsName(char *a_idsName);
85 void		quitSetZoneName(char *a_zoneName);
86 void		quitSetZoneTmpdir(char *z_zoneTempDir);
87 void		quitSetZonelist(zoneList_t a_zlst);
88 sighdlrFunc_t	*quitGetTrapHandler(void);
89 
90 /*
91  * *****************************************************************************
92  * global external (public) functions
93  * *****************************************************************************
94  */
95 
96 /*
97  * Name:	quitGetTrapHandler
98  * Description:	return address of this modules "signal trap" handler
99  * Arguments:	void
100  * Returns:	sighdlrFunc_t
101  *			The address of the trap handler that can be passed to
102  *			the signal() type system calls
103  */
104 
105 sighdlrFunc_t *
106 quitGetTrapHandler()
107 {
108 	return (&trap);
109 }
110 
111 /*
112  * Name:	quitSetIdsName
113  * Description:	set the input data stream name to use when quit() is called
114  * Arguments:	a_idsName - pointer to string representing the input data
115  *			stream object currently open
116  *			== NULL - there is no input datastream object to use
117  * Returns:	void
118  * NOTE:	When quit() is called, if an input datastream object is set,
119  *		quit will close the datastream and cleanup certain objects
120  *		associated with the datastream
121  */
122 
123 void
124 quitSetIdsName(char *a_idsName)
125 {
126 	idsName = a_idsName;
127 }
128 
129 /*
130  * Name:	quitSetIntfReloc
131  * Description:	set the "intf_reloc" interface to run when quit() is called
132  * Arguments:	a_intfReloc - pointer to function to call when quit() is called
133  * Returns:	void
134  * NOTE:	When quit() is called, if an "intf_reloc" function is set, quit
135  *		will call that function to perform whatever operations it needs
136  *		to perform - typically this is needed to run "intf_reloc" when
137  *		pre-SVR4 packages have been installed
138  */
139 
140 void
141 quitSetIntfReloc(intfRelocFunc_t *a_intfReloc)
142 {
143 	intfRelocFunc = a_intfReloc;
144 }
145 
146 /*
147  * Name:	quitSetCkreturnFunc
148  * Description:	set the ckreturn() interface to call when quit() is called
149  * Arguments:	a_ckreturnFunc - pointer to function to call when quit() is
150  *			called
151  * Returns:	void
152  * NOTE:	When quit() is called if a "ckreturnfunc" is set, then the first
153  *		action quit() takes is to call the "ckreturnfunc" specified with
154  *		the value passed to quit as the first argument. Quit will then
155  *		set the final return code to be used when exit() is called based
156  *		on the contents of these global variables:
157  *		 - admnflag - != 0 if any pkgop admin setting failed (4)
158  *		 - doreboot - != 0 if reboot required after installation
159  *		 - failflag - != 0 if fatal error has occurred (1)
160  *		 - intrflag - != 0 if user selected quit (3)
161  *		 - ireboot - != 0 if immediate reboot required
162  *		 - nullflag - != 0 if admin interaction required (5)
163  *		 - warnflag - != 0 if non-fatal error has occurred (2)
164  */
165 
166 void
167 quitSetCkreturnFunc(ckreturnFunc_t *a_ckreturnFunc)
168 {
169 	ckreturnFunc = a_ckreturnFunc;
170 }
171 
172 /*
173  * Name:	quitSetZonelist
174  * Description:	set the list of zones that are "locked" so that the zones can
175  *		be unlocked if quit() is called to exit
176  * Arguments:	a_zlst - list of zones that are "locked"
177  * Returns:	void
178  * NOTE:	When quit() is called, if this list is set, then z_unlock_zones
179  *		is called to unlock all of the zones in the list. If this list
180  *		is NOT set, then z_unlock_this_zone is called to unlock this
181  *		zone.
182  */
183 
184 void
185 quitSetZonelist(zoneList_t a_zlst)
186 {
187 	zoneList = a_zlst;
188 }
189 
190 /*
191  * Name:	quitSetZoneName
192  * Description:	set the zone name the program is running in
193  * Arguments:	a_zoneName - pointer to string representing the name of the zone
194  *			that the program is running in
195  * Returns:	void
196  */
197 
198 /* ARGSUSED */
199 void
200 quitSetZoneName(char *a_zoneName)
201 {
202 }
203 
204 /*
205  * Name:	quitSetZoneTmpdir
206  * Description:	set the path to the "zone temporary directory" in use
207  * Arguments:	a_zoneTempDir - pointer to string representing the full path to
208  *			the temporary directory used to hold files used during
209  *			zone operations
210  * Returns:	void
211  * NOTE:	If a zone temporary directory is set when quit() is called, the
212  *		directory is recursively removed before quit() calls exit
213  */
214 
215 void
216 quitSetZoneTmpdir(char *a_zoneTempDir)
217 {
218 	zoneTempDir = a_zoneTempDir;
219 }
220 
221 /*
222  * Name:	quitSetDwnldTmpdir
223  * Description:	set the path to the "download temporary directory" in use
224  * Arguments:	a_dwnldTempDir - pointer to string representing the full path to
225  *			the temporary directory used to hold files used during
226  *			download operations
227  * Returns:	void
228  * NOTE:	If a download temporary directory is set when quit() is called,
229  *		the directory is recursively removed before quit() calls exit
230  */
231 
232 void
233 quitSetDwnldTmpdir(char *a_dwnldTempDir)
234 {
235 	dwnldTempDir = a_dwnldTempDir;
236 }
237 
238 /*
239  * Name:	quit
240  * Description:	cleanup and exit
241  * Arguments:	a_retcode - the code to use to determine final exit status;
242  *			if this is NOT "99" and if a "ckreturnFunc" is
243  *			set, then that function is called with a_retcode
244  *			to set the final exit status.
245  *		Valid values are:
246  *		0 - success
247  *		1 - package operation failed (fatal error)
248  *		2 - non-fatal error (warning)
249  *		3 - user selected quit (operation interrupted)
250  *		4 - admin settings prevented operation
251  *		5 - interaction required and -n (non-interactive) specified
252  *		"10" is added to indicate "immediate reboot required"
253  *		"20" is be added to indicate "reboot after install required"
254  *		99 - do not interpret the code - just exit "99"
255  * Returns:	<<this function does not return - calls exit()>>
256  */
257 
258 void
259 quit(int a_retcode)
260 {
261 	/* disable interrupts */
262 
263 	(void) signal(SIGINT, SIG_IGN);
264 	(void) signal(SIGHUP, SIG_IGN);
265 
266 	if (!restore_local_fs()) {
267 		progerr(ERR_CANNOT_RESTORE_LOCAL_FS);
268 	}
269 
270 	/* process return code if not quit(99) */
271 
272 	if (a_retcode != 99) {
273 		if (ckreturnFunc != (ckreturnFunc_t *)NULL) {
274 			(ckreturnFunc)(a_retcode);
275 		}
276 		if (failflag) {
277 			a_retcode = 1;
278 		} else if (warnflag) {
279 			a_retcode = 2;
280 		} else if (intrflag) {
281 			a_retcode = 3;
282 		} else if (admnflag) {
283 			a_retcode = 4;
284 		} else if (nullflag) {
285 			a_retcode = 5;
286 		} else {
287 			a_retcode = 0;
288 		}
289 		if (ireboot) {
290 			a_retcode = (a_retcode % 10) + 20;
291 		}
292 		if (doreboot) {
293 			a_retcode = (a_retcode % 10) + 10;
294 		}
295 	}
296 
297 	if (doreboot || ireboot) {
298 		ptext(stderr, MSG_REBOOT);
299 	}
300 
301 	(void) chdir("/");
302 
303 	/* if set remove download temporary directory */
304 
305 	if (dwnldTempDir != (char *)NULL) {
306 		echoDebug(DBG_REMOVING_DWNLD_TMPDIR, dwnldTempDir);
307 		(void) rrmdir(dwnldTempDir);
308 		dwnldTempDir = (char *)NULL;
309 	}
310 
311 	/* if set remove zone temporary directory */
312 
313 	if (zoneTempDir != (char *)NULL) {
314 		echoDebug(DBG_REMOVING_ZONE_TMPDIR, zoneTempDir);
315 		(void) rrmdir(zoneTempDir);
316 		zoneTempDir = (char *)NULL;
317 	}
318 
319 	/* close and cleanup if input datastream is set */
320 
321 	if (idsName != (char *)NULL) { /* datastream */
322 		if (pkgdev.dirname != NULL) {
323 			echoDebug(DBG_REMOVING_DSTREAM_TMPDIR, pkgdev.dirname);
324 			(void) rrmdir(pkgdev.dirname);  /* from tempnam */
325 		}
326 		/*
327 		 * cleanup after a web-based install.
328 		 * web-based install failures
329 		 * are indicated by exit codes 10-98
330 		 * exit code 99 is fatal error exit.
331 		 */
332 		if (pkgdev.pathname != NULL && is_web_install() &&
333 			(a_retcode == 0 ||
334 				(a_retcode >= 10 && a_retcode < 99))) {
335 			(void) web_cleanup();
336 		}
337 		(void) ds_close(1);
338 	} else if (pkgdev.mount) {
339 		(void) pkgumount(&pkgdev);
340 	}
341 
342 	/*
343 	 * issue final exit message depending on number of packages left
344 	 * to process
345 	 */
346 
347 	if (npkgs == 1) {
348 		echo(MSG_1_PKG_NOT_PROCESSED);
349 	} else if (npkgs) {
350 		echo(MSG_N_PKGS_NOT_PROCESSED, npkgs);
351 	}
352 
353 	/* call intf_reloc function if registered */
354 
355 	if (intfRelocFunc != (intfRelocFunc_t *)NULL) {
356 		(intfRelocFunc)();
357 	}
358 
359 	/* if a zone list exists, unlock all zones */
360 
361 	if (zoneList != (zoneList_t)NULL) {
362 		(void) z_unlock_zones(zoneList, ZLOCKS_ALL);
363 	} else {
364 		(void) z_unlock_this_zone(ZLOCKS_ALL);
365 	}
366 
367 	/* final exit debugging message */
368 
369 	echoDebug(DBG_EXIT_WITH_CODE, a_retcode);
370 
371 	exit(a_retcode);
372 	/* NOTREACHED */
373 }
374 
375 /*
376  * *****************************************************************************
377  * static internal (private) functions
378  * *****************************************************************************
379  */
380 
381 /*
382  * Name:	trap
383  * Description:	signal handler connected via quitGetTrapHandler()
384  * Arguments:	signo - [RO, *RO] - (int)
385  *			Integer representing the signal that caused the trap
386  *			to this function to occur
387  * Returns:	<< NONE >>
388  * NOTE:	This function exits the program after doing mandatory cleanup.
389  * NOTE:	Even though quit() should NOT return, there is a call to _exit()
390  *		put after each call to quit() just in case quit() ever returned
391  *		by mistake.
392  */
393 
394 static void
395 trap(int signo)
396 {
397 	/* prevent reentrance */
398 
399 	if (trapEntered++ != 0) {
400 		return;
401 	}
402 
403 	if ((signo == SIGINT) || (signo == SIGHUP)) {
404 		quit(3);
405 		_exit(3);
406 	}
407 	quit(1);
408 	_exit(1);
409 }
410