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