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 (c) 2017 Peter Tribble. 24 */ 25 26 /* 27 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 28 * Use is subject to license terms. 29 */ 30 31 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 32 /* All Rights Reserved */ 33 34 #include <stdio.h> 35 #include <signal.h> 36 #include <stdlib.h> 37 #include <unistd.h> 38 #include <pkgdev.h> 39 #include <locale.h> 40 #include <libintl.h> 41 42 #include <pkglib.h> 43 #include <messages.h> 44 45 #include <libadm.h> 46 #include <libinst.h> 47 48 #include "quit.h" 49 50 /* 51 * imported global variables 52 */ 53 54 /* imported from main.c */ 55 56 extern struct pkgdev pkgdev; /* holds info about the installation device */ 57 58 extern int npkgs; /* the number of packages yet to be installed */ 59 extern int admnflag; /* != 0 if any pkgop admin setting failed (4) */ 60 extern int doreboot; /* != 0 if reboot required after installation */ 61 extern int failflag; /* != 0 if fatal error has occurred (1) */ 62 extern int intrflag; /* != 0 if user selected quit (3) */ 63 extern int ireboot; /* != 0 if immediate reboot required */ 64 extern int nullflag; /* != 0 if admin interaction required (5) */ 65 extern int warnflag; /* != 0 if non-fatal error has occurred (2) */ 66 67 /* 68 * forward declarations 69 */ 70 71 static char *idsName = (char *)NULL; 72 static char *zoneTempDir = (char *)NULL; 73 static ckreturnFunc_t *ckreturnFunc = (ckreturnFunc_t *)NULL; 74 static int trapEntered = 0; 75 static void trap(int signo); 76 static zoneList_t zoneList = (zoneList_t)NULL; 77 78 /* 79 * exported functions 80 */ 81 82 void quit(int retcode); 83 void quitSetCkreturnFunc(ckreturnFunc_t *a_ckreturnFunc); 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: quitSetCkreturnFunc 131 * Description: set the ckreturn() interface to call when quit() is called 132 * Arguments: a_ckreturnFunc - pointer to function to call when quit() is 133 * called 134 * Returns: void 135 * NOTE: When quit() is called if a "ckreturnfunc" is set, then the first 136 * action quit() takes is to call the "ckreturnfunc" specified with 137 * the value passed to quit as the first argument. Quit will then 138 * set the final return code to be used when exit() is called based 139 * on the contents of these global variables: 140 * - admnflag - != 0 if any pkgop admin setting failed (4) 141 * - doreboot - != 0 if reboot required after installation 142 * - failflag - != 0 if fatal error has occurred (1) 143 * - intrflag - != 0 if user selected quit (3) 144 * - ireboot - != 0 if immediate reboot required 145 * - nullflag - != 0 if admin interaction required (5) 146 * - warnflag - != 0 if non-fatal error has occurred (2) 147 */ 148 149 void 150 quitSetCkreturnFunc(ckreturnFunc_t *a_ckreturnFunc) 151 { 152 ckreturnFunc = a_ckreturnFunc; 153 } 154 155 /* 156 * Name: quitSetZonelist 157 * Description: set the list of zones that are "locked" so that the zones can 158 * be unlocked if quit() is called to exit 159 * Arguments: a_zlst - list of zones that are "locked" 160 * Returns: void 161 * NOTE: When quit() is called, if this list is set, then z_unlock_zones 162 * is called to unlock all of the zones in the list. If this list 163 * is NOT set, then z_unlock_this_zone is called to unlock this 164 * zone. 165 */ 166 167 void 168 quitSetZonelist(zoneList_t a_zlst) 169 { 170 zoneList = a_zlst; 171 } 172 173 /* 174 * Name: quitSetZoneName 175 * Description: set the zone name the program is running in 176 * Arguments: a_zoneName - pointer to string representing the name of the zone 177 * that the program is running in 178 * Returns: void 179 */ 180 181 /* ARGSUSED */ 182 void 183 quitSetZoneName(char *a_zoneName) 184 { 185 } 186 187 /* 188 * Name: quitSetZoneTmpdir 189 * Description: set the path to the "zone temporary directory" in use 190 * Arguments: a_zoneTempDir - pointer to string representing the full path to 191 * the temporary directory used to hold files used during 192 * zone operations 193 * Returns: void 194 * NOTE: If a zone temporary directory is set when quit() is called, the 195 * directory is recursively removed before quit() calls exit 196 */ 197 198 void 199 quitSetZoneTmpdir(char *a_zoneTempDir) 200 { 201 zoneTempDir = a_zoneTempDir; 202 } 203 204 /* 205 * Name: quit 206 * Description: cleanup and exit 207 * Arguments: a_retcode - the code to use to determine final exit status; 208 * if this is NOT "99" and if a "ckreturnFunc" is 209 * set, then that function is called with a_retcode 210 * to set the final exit status. 211 * Valid values are: 212 * 0 - success 213 * 1 - package operation failed (fatal error) 214 * 2 - non-fatal error (warning) 215 * 3 - user selected quit (operation interrupted) 216 * 4 - admin settings prevented operation 217 * 5 - interaction required and -n (non-interactive) specified 218 * "10" is added to indicate "immediate reboot required" 219 * "20" is be added to indicate "reboot after install required" 220 * 99 - do not interpret the code - just exit "99" 221 * Returns: <<this function does not return - calls exit()>> 222 */ 223 224 void 225 quit(int a_retcode) 226 { 227 /* disable interrupts */ 228 229 (void) signal(SIGINT, SIG_IGN); 230 (void) signal(SIGHUP, SIG_IGN); 231 232 if (!restore_local_fs()) { 233 progerr(ERR_CANNOT_RESTORE_LOCAL_FS); 234 } 235 236 /* process return code if not quit(99) */ 237 238 if (a_retcode != 99) { 239 if (ckreturnFunc != (ckreturnFunc_t *)NULL) { 240 (ckreturnFunc)(a_retcode); 241 } 242 if (failflag) { 243 a_retcode = 1; 244 } else if (warnflag) { 245 a_retcode = 2; 246 } else if (intrflag) { 247 a_retcode = 3; 248 } else if (admnflag) { 249 a_retcode = 4; 250 } else if (nullflag) { 251 a_retcode = 5; 252 } else { 253 a_retcode = 0; 254 } 255 if (ireboot) { 256 a_retcode = (a_retcode % 10) + 20; 257 } 258 if (doreboot) { 259 a_retcode = (a_retcode % 10) + 10; 260 } 261 } 262 263 if (doreboot || ireboot) { 264 ptext(stderr, MSG_REBOOT); 265 } 266 267 (void) chdir("/"); 268 269 /* if set remove zone temporary directory */ 270 271 if (zoneTempDir != (char *)NULL) { 272 echoDebug(DBG_REMOVING_ZONE_TMPDIR, zoneTempDir); 273 (void) rrmdir(zoneTempDir); 274 zoneTempDir = (char *)NULL; 275 } 276 277 /* close and cleanup if input datastream is set */ 278 279 if (idsName != (char *)NULL) { /* datastream */ 280 if (pkgdev.dirname != NULL) { 281 echoDebug(DBG_REMOVING_DSTREAM_TMPDIR, pkgdev.dirname); 282 (void) rrmdir(pkgdev.dirname); /* from tempnam */ 283 } 284 (void) ds_close(1); 285 } else if (pkgdev.mount) { 286 (void) pkgumount(&pkgdev); 287 } 288 289 /* 290 * issue final exit message depending on number of packages left 291 * to process 292 */ 293 294 if (npkgs == 1) { 295 echo(MSG_1_PKG_NOT_PROCESSED); 296 } else if (npkgs) { 297 echo(MSG_N_PKGS_NOT_PROCESSED, npkgs); 298 } 299 300 /* if a zone list exists, unlock all zones */ 301 302 if (zoneList != (zoneList_t)NULL) { 303 (void) z_unlock_zones(zoneList, ZLOCKS_ALL); 304 } else { 305 (void) z_unlock_this_zone(ZLOCKS_ALL); 306 } 307 308 /* final exit debugging message */ 309 310 echoDebug(DBG_EXIT_WITH_CODE, a_retcode); 311 312 exit(a_retcode); 313 /* NOTREACHED */ 314 } 315 316 /* 317 * ***************************************************************************** 318 * static internal (private) functions 319 * ***************************************************************************** 320 */ 321 322 /* 323 * Name: trap 324 * Description: signal handler connected via quitGetTrapHandler() 325 * Arguments: signo - [RO, *RO] - (int) 326 * Integer representing the signal that caused the trap 327 * to this function to occur 328 * Returns: << NONE >> 329 * NOTE: This function exits the program after doing mandatory cleanup. 330 * NOTE: Even though quit() should NOT return, there is a call to _exit() 331 * put after each call to quit() just in case quit() ever returned 332 * by mistake. 333 */ 334 335 static void 336 trap(int signo) 337 { 338 /* prevent reentrance */ 339 340 if (trapEntered++ != 0) { 341 return; 342 } 343 344 if ((signo == SIGINT) || (signo == SIGHUP)) { 345 quit(3); 346 _exit(3); 347 } 348 quit(1); 349 _exit(1); 350 } 351