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