17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 53f2f09c1Sdp * Common Development and Distribution License (the "License"). 63f2f09c1Sdp * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22*19397407SSherry Moore * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 277c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate /* 307c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 317c478bd9Sstevel@tonic-gate * The Regents of the University of California 327c478bd9Sstevel@tonic-gate * All Rights Reserved 337c478bd9Sstevel@tonic-gate * 347c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 357c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 367c478bd9Sstevel@tonic-gate * contributors. 377c478bd9Sstevel@tonic-gate */ 387c478bd9Sstevel@tonic-gate 397c478bd9Sstevel@tonic-gate 407c478bd9Sstevel@tonic-gate /* 417c478bd9Sstevel@tonic-gate * Common code for halt(1M), poweroff(1M), and reboot(1M). We use 427c478bd9Sstevel@tonic-gate * argv[0] to determine which behavior to exhibit. 437c478bd9Sstevel@tonic-gate */ 447c478bd9Sstevel@tonic-gate 45*19397407SSherry Moore #include <stdio.h> 463f2f09c1Sdp #include <procfs.h> 477c478bd9Sstevel@tonic-gate #include <sys/types.h> 48*19397407SSherry Moore #include <sys/elf.h> 49*19397407SSherry Moore #include <sys/systeminfo.h> 50*19397407SSherry Moore #include <sys/stat.h> 517c478bd9Sstevel@tonic-gate #include <sys/uadmin.h> 52*19397407SSherry Moore #include <sys/mntent.h> 53*19397407SSherry Moore #include <sys/mnttab.h> 54*19397407SSherry Moore #include <sys/mount.h> 557c478bd9Sstevel@tonic-gate #include <alloca.h> 567c478bd9Sstevel@tonic-gate #include <assert.h> 577c478bd9Sstevel@tonic-gate #include <errno.h> 587c478bd9Sstevel@tonic-gate #include <fcntl.h> 597c478bd9Sstevel@tonic-gate #include <libgen.h> 607c478bd9Sstevel@tonic-gate #include <libscf.h> 61*19397407SSherry Moore #include <limits.h> 627c478bd9Sstevel@tonic-gate #include <locale.h> 637c478bd9Sstevel@tonic-gate #include <libintl.h> 647c478bd9Sstevel@tonic-gate #include <syslog.h> 657c478bd9Sstevel@tonic-gate #include <signal.h> 667c478bd9Sstevel@tonic-gate #include <strings.h> 677c478bd9Sstevel@tonic-gate #include <unistd.h> 687c478bd9Sstevel@tonic-gate #include <stdlib.h> 697c478bd9Sstevel@tonic-gate #include <stdio.h> 707c478bd9Sstevel@tonic-gate #include <strings.h> 717c478bd9Sstevel@tonic-gate #include <time.h> 727c478bd9Sstevel@tonic-gate #include <utmpx.h> 737c478bd9Sstevel@tonic-gate #include <pwd.h> 747c478bd9Sstevel@tonic-gate #include <zone.h> 75*19397407SSherry Moore 76*19397407SSherry Moore #include <libzfs.h> 77*19397407SSherry Moore 787c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) 797c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" 807c478bd9Sstevel@tonic-gate #endif 817c478bd9Sstevel@tonic-gate 82*19397407SSherry Moore #if defined(__sparc) 83*19397407SSherry Moore #define CUR_ELFDATA ELFDATA2MSB 84*19397407SSherry Moore #elif defined(__i386) 85*19397407SSherry Moore #define CUR_ELFDATA ELFDATA2LSB 86*19397407SSherry Moore #endif 87*19397407SSherry Moore 88*19397407SSherry Moore static libzfs_handle_t *g_zfs; 89*19397407SSherry Moore 907c478bd9Sstevel@tonic-gate extern int audit_halt_setup(int, char **); 917c478bd9Sstevel@tonic-gate extern int audit_halt_success(void); 927c478bd9Sstevel@tonic-gate extern int audit_halt_fail(void); 937c478bd9Sstevel@tonic-gate 947c478bd9Sstevel@tonic-gate extern int audit_reboot_setup(void); 957c478bd9Sstevel@tonic-gate extern int audit_reboot_success(void); 967c478bd9Sstevel@tonic-gate extern int audit_reboot_fail(void); 977c478bd9Sstevel@tonic-gate 983f2f09c1Sdp static char *cmdname; /* basename(argv[0]), the name of the command */ 993f2f09c1Sdp 1007c478bd9Sstevel@tonic-gate typedef struct ctidlist_struct { 1017c478bd9Sstevel@tonic-gate ctid_t ctid; 1027c478bd9Sstevel@tonic-gate struct ctidlist_struct *next; 1037c478bd9Sstevel@tonic-gate } ctidlist_t; 1047c478bd9Sstevel@tonic-gate 1057c478bd9Sstevel@tonic-gate static ctidlist_t *ctidlist = NULL; 1067c478bd9Sstevel@tonic-gate static ctid_t startdct = -1; 1077c478bd9Sstevel@tonic-gate 1087c478bd9Sstevel@tonic-gate #define FMRI_STARTD_CONTRACT \ 1097c478bd9Sstevel@tonic-gate "svc:/system/svc/restarter:default/:properties/restarter/contract" 1107c478bd9Sstevel@tonic-gate 11126f665e8Sdstaff #define ZONEADM_PROG "/usr/sbin/zoneadm" 11226f665e8Sdstaff 113*19397407SSherry Moore /* 114*19397407SSherry Moore * The length of FASTBOOT_MOUNTPOINT must be less than MAXPATHLEN. 115*19397407SSherry Moore */ 116*19397407SSherry Moore #define FASTBOOT_MOUNTPOINT "/tmp/.fastboot.root" 117*19397407SSherry Moore 118*19397407SSherry Moore static char fastboot_mounted[MAXPATHLEN]; 119*19397407SSherry Moore 120*19397407SSherry Moore static int validate_ufs_disk(char *, char *); 121*19397407SSherry Moore static int validate_zfs_pool(char *, char *); 122*19397407SSherry Moore 1233f2f09c1Sdp static pid_t 1243f2f09c1Sdp get_initpid() 1253f2f09c1Sdp { 1263f2f09c1Sdp static int init_pid = -1; 1273f2f09c1Sdp 1283f2f09c1Sdp if (init_pid == -1) { 1293f2f09c1Sdp if (zone_getattr(getzoneid(), ZONE_ATTR_INITPID, &init_pid, 1303f2f09c1Sdp sizeof (init_pid)) != sizeof (init_pid)) { 1313f2f09c1Sdp assert(errno == ESRCH); 1323f2f09c1Sdp init_pid = -1; 1333f2f09c1Sdp } 1343f2f09c1Sdp } 1353f2f09c1Sdp return (init_pid); 1363f2f09c1Sdp } 1373f2f09c1Sdp 1383f2f09c1Sdp /* 1393f2f09c1Sdp * Quiesce or resume init using /proc. When stopping init, we can't send 1403f2f09c1Sdp * SIGTSTP (since init ignores it) or SIGSTOP (since the kernel won't permit 1413f2f09c1Sdp * it). 1423f2f09c1Sdp */ 1433f2f09c1Sdp static int 1443f2f09c1Sdp direct_init(long command) 1453f2f09c1Sdp { 1463f2f09c1Sdp char ctlfile[MAXPATHLEN]; 1473f2f09c1Sdp pid_t pid; 1483f2f09c1Sdp int ctlfd; 1493f2f09c1Sdp 1503f2f09c1Sdp assert(command == PCDSTOP || command == PCRUN); 1513f2f09c1Sdp if ((pid = get_initpid()) == -1) { 1523f2f09c1Sdp return (-1); 1533f2f09c1Sdp } 1543f2f09c1Sdp 1553f2f09c1Sdp (void) snprintf(ctlfile, sizeof (ctlfile), "/proc/%d/ctl", pid); 1563f2f09c1Sdp if ((ctlfd = open(ctlfile, O_WRONLY)) == -1) 1573f2f09c1Sdp return (-1); 1583f2f09c1Sdp 1593f2f09c1Sdp if (command == PCDSTOP) { 1603f2f09c1Sdp if (write(ctlfd, &command, sizeof (long)) == -1) { 1613f2f09c1Sdp (void) close(ctlfd); 1623f2f09c1Sdp return (-1); 1633f2f09c1Sdp } 1643f2f09c1Sdp } else { /* command == PCRUN */ 1653f2f09c1Sdp long cmds[2]; 1663f2f09c1Sdp cmds[0] = command; 1673f2f09c1Sdp cmds[1] = 0; 1683f2f09c1Sdp if (write(ctlfd, cmds, sizeof (cmds)) == -1) { 1693f2f09c1Sdp (void) close(ctlfd); 1703f2f09c1Sdp return (-1); 1713f2f09c1Sdp } 1723f2f09c1Sdp } 1733f2f09c1Sdp (void) close(ctlfd); 1743f2f09c1Sdp return (0); 1753f2f09c1Sdp } 1763f2f09c1Sdp 1777c478bd9Sstevel@tonic-gate static void 1787c478bd9Sstevel@tonic-gate stop_startd() 1797c478bd9Sstevel@tonic-gate { 1807c478bd9Sstevel@tonic-gate scf_handle_t *h; 1817c478bd9Sstevel@tonic-gate scf_property_t *prop = NULL; 1827c478bd9Sstevel@tonic-gate scf_value_t *val = NULL; 1837c478bd9Sstevel@tonic-gate uint64_t uint64; 1847c478bd9Sstevel@tonic-gate 1853f2f09c1Sdp if ((h = scf_handle_create(SCF_VERSION)) == NULL) 1867c478bd9Sstevel@tonic-gate return; 1877c478bd9Sstevel@tonic-gate 1883f2f09c1Sdp if ((scf_handle_bind(h) != 0) || 1893f2f09c1Sdp ((prop = scf_property_create(h)) == NULL) || 1903f2f09c1Sdp ((val = scf_value_create(h)) == NULL)) 1917c478bd9Sstevel@tonic-gate goto out; 1927c478bd9Sstevel@tonic-gate 1933f2f09c1Sdp if (scf_handle_decode_fmri(h, FMRI_STARTD_CONTRACT, 1943f2f09c1Sdp NULL, NULL, NULL, NULL, prop, SCF_DECODE_FMRI_EXACT) != 0) 1957c478bd9Sstevel@tonic-gate goto out; 1967c478bd9Sstevel@tonic-gate 1973f2f09c1Sdp if (scf_property_is_type(prop, SCF_TYPE_COUNT) != 0 || 1983f2f09c1Sdp scf_property_get_value(prop, val) != 0 || 1993f2f09c1Sdp scf_value_get_count(val, &uint64) != 0) 2007c478bd9Sstevel@tonic-gate goto out; 2017c478bd9Sstevel@tonic-gate 2023f2f09c1Sdp startdct = (ctid_t)uint64; 2033f2f09c1Sdp (void) sigsend(P_CTID, startdct, SIGSTOP); 2047c478bd9Sstevel@tonic-gate 2057c478bd9Sstevel@tonic-gate out: 2063f2f09c1Sdp scf_property_destroy(prop); 2073f2f09c1Sdp scf_value_destroy(val); 2087c478bd9Sstevel@tonic-gate scf_handle_destroy(h); 2097c478bd9Sstevel@tonic-gate } 2107c478bd9Sstevel@tonic-gate 2117c478bd9Sstevel@tonic-gate static void 2127c478bd9Sstevel@tonic-gate continue_startd() 2137c478bd9Sstevel@tonic-gate { 2147c478bd9Sstevel@tonic-gate if (startdct != -1) 2157c478bd9Sstevel@tonic-gate (void) sigsend(P_CTID, startdct, SIGCONT); 2167c478bd9Sstevel@tonic-gate } 2177c478bd9Sstevel@tonic-gate 2187c478bd9Sstevel@tonic-gate #define FMRI_RESTARTER_PROP "/:properties/general/restarter" 2197c478bd9Sstevel@tonic-gate #define FMRI_CONTRACT_PROP "/:properties/restarter/contract" 2207c478bd9Sstevel@tonic-gate 2217c478bd9Sstevel@tonic-gate static int 2227c478bd9Sstevel@tonic-gate save_ctid(ctid_t ctid) 2237c478bd9Sstevel@tonic-gate { 2247c478bd9Sstevel@tonic-gate ctidlist_t *next; 2257c478bd9Sstevel@tonic-gate 2267c478bd9Sstevel@tonic-gate for (next = ctidlist; next != NULL; next = next->next) 2277c478bd9Sstevel@tonic-gate if (next->ctid == ctid) 2287c478bd9Sstevel@tonic-gate return (-1); 2297c478bd9Sstevel@tonic-gate 2307c478bd9Sstevel@tonic-gate next = (ctidlist_t *)malloc(sizeof (ctidlist_t)); 2317c478bd9Sstevel@tonic-gate if (next == NULL) 2327c478bd9Sstevel@tonic-gate return (-1); 2337c478bd9Sstevel@tonic-gate 2347c478bd9Sstevel@tonic-gate next->ctid = ctid; 2357c478bd9Sstevel@tonic-gate next->next = ctidlist; 2367c478bd9Sstevel@tonic-gate ctidlist = next; 2377c478bd9Sstevel@tonic-gate return (0); 2387c478bd9Sstevel@tonic-gate } 2397c478bd9Sstevel@tonic-gate 2407c478bd9Sstevel@tonic-gate static void 2417c478bd9Sstevel@tonic-gate stop_delegates() 2427c478bd9Sstevel@tonic-gate { 2437c478bd9Sstevel@tonic-gate ctid_t ctid; 2447c478bd9Sstevel@tonic-gate scf_handle_t *h; 2457c478bd9Sstevel@tonic-gate scf_scope_t *sc = NULL; 2467c478bd9Sstevel@tonic-gate scf_service_t *svc = NULL; 2477c478bd9Sstevel@tonic-gate scf_instance_t *inst = NULL; 2487c478bd9Sstevel@tonic-gate scf_snapshot_t *snap = NULL; 2497c478bd9Sstevel@tonic-gate scf_snapshot_t *isnap = NULL; 2507c478bd9Sstevel@tonic-gate scf_propertygroup_t *pg = NULL; 2517c478bd9Sstevel@tonic-gate scf_property_t *prop = NULL; 2527c478bd9Sstevel@tonic-gate scf_value_t *val = NULL; 2537c478bd9Sstevel@tonic-gate scf_iter_t *siter = NULL; 2547c478bd9Sstevel@tonic-gate scf_iter_t *iiter = NULL; 2557c478bd9Sstevel@tonic-gate char *fmri; 2567c478bd9Sstevel@tonic-gate ssize_t length; 2577c478bd9Sstevel@tonic-gate 2587c478bd9Sstevel@tonic-gate uint64_t uint64; 2597c478bd9Sstevel@tonic-gate ssize_t bytes; 2607c478bd9Sstevel@tonic-gate 2617c478bd9Sstevel@tonic-gate length = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH); 2627c478bd9Sstevel@tonic-gate if (length <= 0) 2637c478bd9Sstevel@tonic-gate return; 2647c478bd9Sstevel@tonic-gate 2657c478bd9Sstevel@tonic-gate length++; 2667c478bd9Sstevel@tonic-gate fmri = alloca(length * sizeof (char)); 2677c478bd9Sstevel@tonic-gate 2683f2f09c1Sdp if ((h = scf_handle_create(SCF_VERSION)) == NULL) 2697c478bd9Sstevel@tonic-gate return; 2707c478bd9Sstevel@tonic-gate 2713f2f09c1Sdp if (scf_handle_bind(h) != 0) { 2727c478bd9Sstevel@tonic-gate scf_handle_destroy(h); 2737c478bd9Sstevel@tonic-gate return; 2747c478bd9Sstevel@tonic-gate } 2757c478bd9Sstevel@tonic-gate 2763f2f09c1Sdp if ((sc = scf_scope_create(h)) == NULL || 2773f2f09c1Sdp (svc = scf_service_create(h)) == NULL || 2783f2f09c1Sdp (inst = scf_instance_create(h)) == NULL || 2793f2f09c1Sdp (snap = scf_snapshot_create(h)) == NULL || 2803f2f09c1Sdp (pg = scf_pg_create(h)) == NULL || 2813f2f09c1Sdp (prop = scf_property_create(h)) == NULL || 2823f2f09c1Sdp (val = scf_value_create(h)) == NULL || 2833f2f09c1Sdp (siter = scf_iter_create(h)) == NULL || 2843f2f09c1Sdp (iiter = scf_iter_create(h)) == NULL) 2857c478bd9Sstevel@tonic-gate goto out; 2867c478bd9Sstevel@tonic-gate 2873f2f09c1Sdp if (scf_handle_get_scope(h, SCF_SCOPE_LOCAL, sc) != 0) 2887c478bd9Sstevel@tonic-gate goto out; 2897c478bd9Sstevel@tonic-gate 2903f2f09c1Sdp if (scf_iter_scope_services(siter, sc) != 0) 2917c478bd9Sstevel@tonic-gate goto out; 2927c478bd9Sstevel@tonic-gate 2937c478bd9Sstevel@tonic-gate while (scf_iter_next_service(siter, svc) == 1) { 2947c478bd9Sstevel@tonic-gate 2953f2f09c1Sdp if (scf_iter_service_instances(iiter, svc) != 0) 2967c478bd9Sstevel@tonic-gate continue; 2977c478bd9Sstevel@tonic-gate 2987c478bd9Sstevel@tonic-gate while (scf_iter_next_instance(iiter, inst) == 1) { 2997c478bd9Sstevel@tonic-gate 3003f2f09c1Sdp if ((scf_instance_get_snapshot(inst, "running", 3013f2f09c1Sdp snap)) != 0) 3023f2f09c1Sdp isnap = NULL; 3033f2f09c1Sdp else 3043f2f09c1Sdp isnap = snap; 3057c478bd9Sstevel@tonic-gate 3063f2f09c1Sdp if (scf_instance_get_pg_composed(inst, isnap, 3073f2f09c1Sdp SCF_PG_GENERAL, pg) != 0) 3087c478bd9Sstevel@tonic-gate continue; 3097c478bd9Sstevel@tonic-gate 3103f2f09c1Sdp if (scf_pg_get_property(pg, SCF_PROPERTY_RESTARTER, 3113f2f09c1Sdp prop) != 0 || 3123f2f09c1Sdp scf_property_get_value(prop, val) != 0) 3137c478bd9Sstevel@tonic-gate continue; 3147c478bd9Sstevel@tonic-gate 3157c478bd9Sstevel@tonic-gate bytes = scf_value_get_astring(val, fmri, length); 3167c478bd9Sstevel@tonic-gate if (bytes <= 0 || bytes >= length) 3177c478bd9Sstevel@tonic-gate continue; 3187c478bd9Sstevel@tonic-gate 3197c478bd9Sstevel@tonic-gate if (strlcat(fmri, FMRI_CONTRACT_PROP, length) >= 3207c478bd9Sstevel@tonic-gate length) 3217c478bd9Sstevel@tonic-gate continue; 3227c478bd9Sstevel@tonic-gate 3233f2f09c1Sdp if (scf_handle_decode_fmri(h, fmri, NULL, NULL, 3243f2f09c1Sdp NULL, NULL, prop, SCF_DECODE_FMRI_EXACT) != 0) 3257c478bd9Sstevel@tonic-gate continue; 3267c478bd9Sstevel@tonic-gate 3273f2f09c1Sdp if (scf_property_is_type(prop, SCF_TYPE_COUNT) != 0 || 3283f2f09c1Sdp scf_property_get_value(prop, val) != 0 || 3293f2f09c1Sdp scf_value_get_count(val, &uint64) != 0) 3307c478bd9Sstevel@tonic-gate continue; 3317c478bd9Sstevel@tonic-gate 3327c478bd9Sstevel@tonic-gate ctid = (ctid_t)uint64; 3337c478bd9Sstevel@tonic-gate if (save_ctid(ctid) == 0) { 3347c478bd9Sstevel@tonic-gate (void) sigsend(P_CTID, ctid, SIGSTOP); 3357c478bd9Sstevel@tonic-gate } 3367c478bd9Sstevel@tonic-gate } 3377c478bd9Sstevel@tonic-gate } 3387c478bd9Sstevel@tonic-gate out: 3393f2f09c1Sdp scf_scope_destroy(sc); 3403f2f09c1Sdp scf_service_destroy(svc); 3413f2f09c1Sdp scf_instance_destroy(inst); 3423f2f09c1Sdp scf_snapshot_destroy(snap); 3433f2f09c1Sdp scf_pg_destroy(pg); 3443f2f09c1Sdp scf_property_destroy(prop); 3453f2f09c1Sdp scf_value_destroy(val); 3463f2f09c1Sdp scf_iter_destroy(siter); 3473f2f09c1Sdp scf_iter_destroy(iiter); 3487c478bd9Sstevel@tonic-gate 3497c478bd9Sstevel@tonic-gate (void) scf_handle_unbind(h); 3507c478bd9Sstevel@tonic-gate scf_handle_destroy(h); 3517c478bd9Sstevel@tonic-gate } 3527c478bd9Sstevel@tonic-gate 3537c478bd9Sstevel@tonic-gate static void 3547c478bd9Sstevel@tonic-gate continue_delegates() 3557c478bd9Sstevel@tonic-gate { 3567c478bd9Sstevel@tonic-gate ctidlist_t *next; 3577c478bd9Sstevel@tonic-gate for (next = ctidlist; next != NULL; next = next->next) 3587c478bd9Sstevel@tonic-gate (void) sigsend(P_CTID, next->ctid, SIGCONT); 3597c478bd9Sstevel@tonic-gate } 3607c478bd9Sstevel@tonic-gate 3617c478bd9Sstevel@tonic-gate static void 3627c478bd9Sstevel@tonic-gate stop_restarters() 3637c478bd9Sstevel@tonic-gate { 3647c478bd9Sstevel@tonic-gate stop_startd(); 3657c478bd9Sstevel@tonic-gate stop_delegates(); 3667c478bd9Sstevel@tonic-gate } 3677c478bd9Sstevel@tonic-gate 3687c478bd9Sstevel@tonic-gate static void 3697c478bd9Sstevel@tonic-gate continue_restarters() 3707c478bd9Sstevel@tonic-gate { 3717c478bd9Sstevel@tonic-gate continue_startd(); 3727c478bd9Sstevel@tonic-gate continue_delegates(); 3737c478bd9Sstevel@tonic-gate } 3747c478bd9Sstevel@tonic-gate 3757c478bd9Sstevel@tonic-gate /* 3767c478bd9Sstevel@tonic-gate * Copy an array of strings into buf, separated by spaces. Returns 0 on 3777c478bd9Sstevel@tonic-gate * success. 3787c478bd9Sstevel@tonic-gate */ 3797c478bd9Sstevel@tonic-gate static int 3807c478bd9Sstevel@tonic-gate gather_args(char **args, char *buf, size_t buf_sz) 3817c478bd9Sstevel@tonic-gate { 3827c478bd9Sstevel@tonic-gate if (strlcpy(buf, *args, buf_sz) >= buf_sz) 3837c478bd9Sstevel@tonic-gate return (-1); 3847c478bd9Sstevel@tonic-gate 3857c478bd9Sstevel@tonic-gate for (++args; *args != NULL; ++args) { 3867c478bd9Sstevel@tonic-gate if (strlcat(buf, " ", buf_sz) >= buf_sz) 3877c478bd9Sstevel@tonic-gate return (-1); 3887c478bd9Sstevel@tonic-gate if (strlcat(buf, *args, buf_sz) >= buf_sz) 3897c478bd9Sstevel@tonic-gate return (-1); 3907c478bd9Sstevel@tonic-gate } 3917c478bd9Sstevel@tonic-gate 3927c478bd9Sstevel@tonic-gate return (0); 3937c478bd9Sstevel@tonic-gate } 3947c478bd9Sstevel@tonic-gate 39526f665e8Sdstaff /* 39626f665e8Sdstaff * Halt every zone on the system. We are committed to doing a shutdown 39726f665e8Sdstaff * even if something goes wrong here. If something goes wrong, we just 39826f665e8Sdstaff * continue with the shutdown. Return non-zero if we need to wait for zones to 39926f665e8Sdstaff * halt later on. 40026f665e8Sdstaff */ 40126f665e8Sdstaff static int 4023f2f09c1Sdp halt_zones() 40326f665e8Sdstaff { 40426f665e8Sdstaff pid_t pid; 40526f665e8Sdstaff zoneid_t *zones; 4063f2f09c1Sdp size_t nz = 0, old_nz; 40726f665e8Sdstaff int i; 40826f665e8Sdstaff char zname[ZONENAME_MAX]; 40926f665e8Sdstaff 41026f665e8Sdstaff /* 41126f665e8Sdstaff * Get a list of zones. If the number of zones changes in between the 41226f665e8Sdstaff * two zone_list calls, try again. 41326f665e8Sdstaff */ 41426f665e8Sdstaff 41526f665e8Sdstaff for (;;) { 41626f665e8Sdstaff (void) zone_list(NULL, &nz); 41726f665e8Sdstaff if (nz == 1) 41826f665e8Sdstaff return (0); 41926f665e8Sdstaff old_nz = nz; 42026f665e8Sdstaff zones = calloc(sizeof (zoneid_t), nz); 42126f665e8Sdstaff if (zones == NULL) { 42226f665e8Sdstaff (void) fprintf(stderr, 42326f665e8Sdstaff gettext("%s: Could not halt zones" 4243f2f09c1Sdp " (out of memory).\n"), cmdname); 42526f665e8Sdstaff return (0); 42626f665e8Sdstaff } 42726f665e8Sdstaff 42826f665e8Sdstaff (void) zone_list(zones, &nz); 42926f665e8Sdstaff if (old_nz == nz) 43026f665e8Sdstaff break; 43126f665e8Sdstaff free(zones); 43226f665e8Sdstaff } 43326f665e8Sdstaff 43426f665e8Sdstaff if (nz == 2) { 4353f2f09c1Sdp (void) fprintf(stderr, gettext("%s: Halting 1 zone.\n"), 4363f2f09c1Sdp cmdname); 43726f665e8Sdstaff } else { 4383f2f09c1Sdp (void) fprintf(stderr, gettext("%s: Halting %i zones.\n"), 4393f2f09c1Sdp cmdname, nz - 1); 44026f665e8Sdstaff } 44126f665e8Sdstaff 44226f665e8Sdstaff for (i = 0; i < nz; i++) { 44326f665e8Sdstaff if (zones[i] == GLOBAL_ZONEID) 44426f665e8Sdstaff continue; 44526f665e8Sdstaff if (getzonenamebyid(zones[i], zname, sizeof (zname)) < 0) { 44626f665e8Sdstaff /* 44726f665e8Sdstaff * getzonenamebyid should only fail if we raced with 44826f665e8Sdstaff * another process trying to shut down the zone. 44926f665e8Sdstaff * We assume this happened and ignore the error. 45026f665e8Sdstaff */ 45126f665e8Sdstaff if (errno != EINVAL) { 45226f665e8Sdstaff (void) fprintf(stderr, 45326f665e8Sdstaff gettext("%s: Unexpected error while " 45426f665e8Sdstaff "looking up zone %ul: %s.\n"), 4553f2f09c1Sdp cmdname, zones[i], strerror(errno)); 45626f665e8Sdstaff } 45726f665e8Sdstaff 45826f665e8Sdstaff continue; 45926f665e8Sdstaff } 46026f665e8Sdstaff pid = fork(); 46126f665e8Sdstaff if (pid < 0) { 46226f665e8Sdstaff (void) fprintf(stderr, 46326f665e8Sdstaff gettext("%s: Zone \"%s\" could not be" 46426f665e8Sdstaff " halted (could not fork(): %s).\n"), 4653f2f09c1Sdp cmdname, zname, strerror(errno)); 46626f665e8Sdstaff continue; 46726f665e8Sdstaff } 46826f665e8Sdstaff if (pid == 0) { 46926f665e8Sdstaff (void) execl(ZONEADM_PROG, ZONEADM_PROG, 47026f665e8Sdstaff "-z", zname, "halt", NULL); 47126f665e8Sdstaff (void) fprintf(stderr, 47226f665e8Sdstaff gettext("%s: Zone \"%s\" could not be halted" 47326f665e8Sdstaff " (cannot exec(" ZONEADM_PROG "): %s).\n"), 4743f2f09c1Sdp cmdname, zname, strerror(errno)); 47526f665e8Sdstaff exit(0); 47626f665e8Sdstaff } 47726f665e8Sdstaff } 47826f665e8Sdstaff 47926f665e8Sdstaff return (1); 48026f665e8Sdstaff } 48126f665e8Sdstaff 48226f665e8Sdstaff /* 48326f665e8Sdstaff * This function tries to wait for all non-global zones to go away. 48426f665e8Sdstaff * It will timeout if no progress is made for 5 seconds, or a total of 48526f665e8Sdstaff * 30 seconds elapses. 48626f665e8Sdstaff */ 48726f665e8Sdstaff 48826f665e8Sdstaff static void 4893f2f09c1Sdp check_zones_haltedness() 49026f665e8Sdstaff { 49126f665e8Sdstaff int t = 0, t_prog = 0; 49226f665e8Sdstaff size_t nz = 0, last_nz; 49326f665e8Sdstaff 49426f665e8Sdstaff do { 49526f665e8Sdstaff last_nz = nz; 49626f665e8Sdstaff (void) zone_list(NULL, &nz); 49726f665e8Sdstaff if (nz == 1) 49826f665e8Sdstaff return; 49926f665e8Sdstaff 50026f665e8Sdstaff (void) sleep(1); 50126f665e8Sdstaff 50226f665e8Sdstaff if (last_nz > nz) 50326f665e8Sdstaff t_prog = 0; 50426f665e8Sdstaff 50526f665e8Sdstaff t++; 50626f665e8Sdstaff t_prog++; 50726f665e8Sdstaff 50826f665e8Sdstaff if (t == 10) { 50926f665e8Sdstaff if (nz == 2) { 51026f665e8Sdstaff (void) fprintf(stderr, 51126f665e8Sdstaff gettext("%s: Still waiting for 1 zone to " 51226f665e8Sdstaff "halt. Will wait up to 20 seconds.\n"), 5133f2f09c1Sdp cmdname); 51426f665e8Sdstaff } else { 51526f665e8Sdstaff (void) fprintf(stderr, 51626f665e8Sdstaff gettext("%s: Still waiting for %i zones " 51726f665e8Sdstaff "to halt. Will wait up to 20 seconds.\n"), 5183f2f09c1Sdp cmdname, nz - 1); 51926f665e8Sdstaff } 52026f665e8Sdstaff } 52126f665e8Sdstaff 52226f665e8Sdstaff } while ((t < 30) && (t_prog < 5)); 52326f665e8Sdstaff } 52426f665e8Sdstaff 525*19397407SSherry Moore 526*19397407SSherry Moore /* 527*19397407SSherry Moore * Validate that this is a root disk or dataset 528*19397407SSherry Moore * Returns 0 if it is a root disk or dataset; 529*19397407SSherry Moore * returns 1 if it is a disk argument or dataset, but not valid or not root; 530*19397407SSherry Moore * returns -1 if it is not a valid argument or a disk argument. 531*19397407SSherry Moore */ 532*19397407SSherry Moore static int 533*19397407SSherry Moore validate_disk(char *arg, char *mountpoint) 534*19397407SSherry Moore { 535*19397407SSherry Moore static char root_dev_path[] = "/dev/dsk"; 536*19397407SSherry Moore char kernpath[MAXPATHLEN]; 537*19397407SSherry Moore struct stat buf; 538*19397407SSherry Moore struct stat64 statbuf; 539*19397407SSherry Moore int rc = 0; 540*19397407SSherry Moore 541*19397407SSherry Moore if (strlen(arg) > MAXPATHLEN) { 542*19397407SSherry Moore (void) fprintf(stderr, 543*19397407SSherry Moore gettext("%s: argument is too long\n"), cmdname); 544*19397407SSherry Moore return (-1); 545*19397407SSherry Moore } 546*19397407SSherry Moore 547*19397407SSherry Moore bcopy(FASTBOOT_MOUNTPOINT, mountpoint, sizeof (FASTBOOT_MOUNTPOINT)); 548*19397407SSherry Moore 549*19397407SSherry Moore /* 550*19397407SSherry Moore * Do a force umount just in case some other filesystem has 551*19397407SSherry Moore * been mounted there. 552*19397407SSherry Moore */ 553*19397407SSherry Moore (void) umount2(mountpoint, MS_FORCE); 554*19397407SSherry Moore 555*19397407SSherry Moore /* Create the directory if it doesn't already exist */ 556*19397407SSherry Moore if (lstat(mountpoint, &buf) != 0) { 557*19397407SSherry Moore if (mkdirp(mountpoint, 0755) != 0) { 558*19397407SSherry Moore (void) fprintf(stderr, 559*19397407SSherry Moore gettext("failed to create mountpoint %s\n"), 560*19397407SSherry Moore mountpoint); 561*19397407SSherry Moore return (-1); 562*19397407SSherry Moore } 563*19397407SSherry Moore } 564*19397407SSherry Moore 565*19397407SSherry Moore if (strncmp(arg, root_dev_path, strlen(root_dev_path)) == 0) { 566*19397407SSherry Moore /* ufs root disk argument */ 567*19397407SSherry Moore rc = validate_ufs_disk(arg, mountpoint); 568*19397407SSherry Moore } else { 569*19397407SSherry Moore /* zfs root pool argument */ 570*19397407SSherry Moore rc = validate_zfs_pool(arg, mountpoint); 571*19397407SSherry Moore } 572*19397407SSherry Moore 573*19397407SSherry Moore if (rc != 0) 574*19397407SSherry Moore return (rc); 575*19397407SSherry Moore 576*19397407SSherry Moore (void) snprintf(kernpath, MAXPATHLEN, "%s/platform/i86pc/kernel/unix", 577*19397407SSherry Moore mountpoint); 578*19397407SSherry Moore 579*19397407SSherry Moore if (stat64(kernpath, &statbuf) != 0) { 580*19397407SSherry Moore (void) fprintf(stderr, 581*19397407SSherry Moore gettext("%s: %s is not a root disk or dataset\n"), 582*19397407SSherry Moore cmdname, arg); 583*19397407SSherry Moore return (1); 584*19397407SSherry Moore } 585*19397407SSherry Moore 586*19397407SSherry Moore return (0); 587*19397407SSherry Moore } 588*19397407SSherry Moore 589*19397407SSherry Moore 590*19397407SSherry Moore static int 591*19397407SSherry Moore validate_ufs_disk(char *arg, char *mountpoint) 592*19397407SSherry Moore { 593*19397407SSherry Moore char mntopts[MNT_LINE_MAX] = { '\0' }; 594*19397407SSherry Moore 595*19397407SSherry Moore /* perform the mount */ 596*19397407SSherry Moore if (mount(arg, mountpoint, MS_DATA|MS_OPTIONSTR, 597*19397407SSherry Moore MNTTYPE_UFS, NULL, 0, mntopts, sizeof (mntopts)) != 0) { 598*19397407SSherry Moore perror(cmdname); 599*19397407SSherry Moore (void) fprintf(stderr, 600*19397407SSherry Moore gettext("%s: failed to mount %s\n"), cmdname, arg); 601*19397407SSherry Moore return (-1); 602*19397407SSherry Moore } 603*19397407SSherry Moore 604*19397407SSherry Moore return (0); 605*19397407SSherry Moore } 606*19397407SSherry Moore 607*19397407SSherry Moore static int 608*19397407SSherry Moore validate_zfs_pool(char *arg, char *mountpoint) 609*19397407SSherry Moore { 610*19397407SSherry Moore zfs_handle_t *zhp = NULL; 611*19397407SSherry Moore char mntopts[MNT_LINE_MAX] = { '\0' }; 612*19397407SSherry Moore int rc = 0; 613*19397407SSherry Moore 614*19397407SSherry Moore if ((g_zfs = libzfs_init()) == NULL) { 615*19397407SSherry Moore (void) fprintf(stderr, gettext("internal error: failed to " 616*19397407SSherry Moore "initialize ZFS library\n")); 617*19397407SSherry Moore return (-1); 618*19397407SSherry Moore } 619*19397407SSherry Moore 620*19397407SSherry Moore /* Try to open the dataset */ 621*19397407SSherry Moore if ((zhp = zfs_open(g_zfs, arg, 622*19397407SSherry Moore ZFS_TYPE_FILESYSTEM | ZFS_TYPE_DATASET)) == NULL) 623*19397407SSherry Moore return (-1); 624*19397407SSherry Moore 625*19397407SSherry Moore /* perform the mount */ 626*19397407SSherry Moore if (mount(zfs_get_name(zhp), mountpoint, MS_DATA|MS_OPTIONSTR, 627*19397407SSherry Moore MNTTYPE_ZFS, NULL, 0, mntopts, sizeof (mntopts)) != 0) { 628*19397407SSherry Moore perror(cmdname); 629*19397407SSherry Moore (void) fprintf(stderr, 630*19397407SSherry Moore gettext("%s: failed to mount %s\n"), cmdname, arg); 631*19397407SSherry Moore rc = -1; 632*19397407SSherry Moore } 633*19397407SSherry Moore 634*19397407SSherry Moore validate_zfs_err_out: 635*19397407SSherry Moore if (zhp != NULL) 636*19397407SSherry Moore zfs_close(zhp); 637*19397407SSherry Moore 638*19397407SSherry Moore libzfs_fini(g_zfs); 639*19397407SSherry Moore return (rc); 640*19397407SSherry Moore } 641*19397407SSherry Moore 642*19397407SSherry Moore /* 643*19397407SSherry Moore * Return 0 if not zfs, or is zfs and have successfully constructed the 644*19397407SSherry Moore * boot argument; returns non-zero otherwise. 645*19397407SSherry Moore * At successful completion fpth contains pointer where mount point ends. 646*19397407SSherry Moore * NOTE: arg is supposed to be the resolved path 647*19397407SSherry Moore */ 648*19397407SSherry Moore static int 649*19397407SSherry Moore get_zfs_bootfs_arg(const char *arg, const char ** fpth, int *is_zfs, 650*19397407SSherry Moore char *bootfs_arg) 651*19397407SSherry Moore { 652*19397407SSherry Moore zfs_handle_t *zhp = NULL; 653*19397407SSherry Moore zpool_handle_t *zpoolp = NULL; 654*19397407SSherry Moore FILE *mtabp = NULL; 655*19397407SSherry Moore struct mnttab mnt; 656*19397407SSherry Moore char *poolname = NULL; 657*19397407SSherry Moore char physpath[MAXNAMELEN]; 658*19397407SSherry Moore char mntsp[ZPOOL_MAXNAMELEN]; 659*19397407SSherry Moore char bootfs[ZPOOL_MAXNAMELEN]; 660*19397407SSherry Moore int rc = 0; 661*19397407SSherry Moore size_t mntlen = 0; 662*19397407SSherry Moore size_t msz; 663*19397407SSherry Moore 664*19397407SSherry Moore *fpth = arg; 665*19397407SSherry Moore *is_zfs = 0; 666*19397407SSherry Moore 667*19397407SSherry Moore bzero(physpath, sizeof (physpath)); 668*19397407SSherry Moore bzero(bootfs, sizeof (bootfs)); 669*19397407SSherry Moore 670*19397407SSherry Moore if ((mtabp = fopen(MNTTAB, "r")) == NULL) { 671*19397407SSherry Moore return (-1); 672*19397407SSherry Moore } 673*19397407SSherry Moore 674*19397407SSherry Moore while (getmntent(mtabp, &mnt) == 0) { 675*19397407SSherry Moore if (strstr(arg, mnt.mnt_mountp) == arg && 676*19397407SSherry Moore (msz = strlen(mnt.mnt_mountp)) > mntlen) { 677*19397407SSherry Moore mntlen = msz; 678*19397407SSherry Moore *is_zfs = strcmp(MNTTYPE_ZFS, mnt.mnt_fstype) == 0; 679*19397407SSherry Moore (void) strlcpy(mntsp, mnt.mnt_special, sizeof (mntsp)); 680*19397407SSherry Moore } 681*19397407SSherry Moore } 682*19397407SSherry Moore 683*19397407SSherry Moore (void) fclose(mtabp); 684*19397407SSherry Moore 685*19397407SSherry Moore if (mntlen > 1) 686*19397407SSherry Moore *fpth += mntlen; 687*19397407SSherry Moore 688*19397407SSherry Moore if (!*is_zfs) 689*19397407SSherry Moore return (0); 690*19397407SSherry Moore 691*19397407SSherry Moore if ((g_zfs = libzfs_init()) == NULL) 692*19397407SSherry Moore return (-1); 693*19397407SSherry Moore 694*19397407SSherry Moore /* Try to open the dataset */ 695*19397407SSherry Moore if ((zhp = zfs_open(g_zfs, mntsp, 696*19397407SSherry Moore ZFS_TYPE_FILESYSTEM | ZFS_TYPE_DATASET)) == NULL) { 697*19397407SSherry Moore (void) fprintf(stderr, gettext("cannot open %s\n"), mntsp); 698*19397407SSherry Moore rc = -1; 699*19397407SSherry Moore goto validate_zfs_err_out; 700*19397407SSherry Moore } 701*19397407SSherry Moore 702*19397407SSherry Moore (void) strlcpy(bootfs, mntsp, sizeof (bootfs)); 703*19397407SSherry Moore 704*19397407SSherry Moore if ((poolname = strtok(mntsp, "/")) == NULL) { 705*19397407SSherry Moore rc = -1; 706*19397407SSherry Moore goto validate_zfs_err_out; 707*19397407SSherry Moore } 708*19397407SSherry Moore 709*19397407SSherry Moore if ((zpoolp = zpool_open(g_zfs, poolname)) == NULL) { 710*19397407SSherry Moore (void) fprintf(stderr, gettext("cannot open %s\n"), poolname); 711*19397407SSherry Moore rc = -1; 712*19397407SSherry Moore goto validate_zfs_err_out; 713*19397407SSherry Moore } 714*19397407SSherry Moore 715*19397407SSherry Moore if (zpool_get_physpath(zpoolp, physpath) != 0) { 716*19397407SSherry Moore (void) fprintf(stderr, gettext("cannot find phys_path\n")); 717*19397407SSherry Moore rc = -1; 718*19397407SSherry Moore goto validate_zfs_err_out; 719*19397407SSherry Moore } 720*19397407SSherry Moore 721*19397407SSherry Moore if (zpool_set_prop(zpoolp, "bootfs", bootfs) != 0) { 722*19397407SSherry Moore (void) fprintf(stderr, gettext("cannot set bootfs to %s\n"), 723*19397407SSherry Moore bootfs); 724*19397407SSherry Moore rc = -1; 725*19397407SSherry Moore goto validate_zfs_err_out; 726*19397407SSherry Moore } 727*19397407SSherry Moore 728*19397407SSherry Moore (void) snprintf(bootfs_arg, BOOTARGS_MAX, 729*19397407SSherry Moore "-B zfs-bootfs=%s,bootpath=\"%s\"", bootfs, physpath); 730*19397407SSherry Moore 731*19397407SSherry Moore validate_zfs_err_out: 732*19397407SSherry Moore if (zhp != NULL) 733*19397407SSherry Moore zfs_close(zhp); 734*19397407SSherry Moore 735*19397407SSherry Moore if (zpoolp != NULL) 736*19397407SSherry Moore zpool_close(zpoolp); 737*19397407SSherry Moore 738*19397407SSherry Moore libzfs_fini(g_zfs); 739*19397407SSherry Moore return (rc); 740*19397407SSherry Moore } 741*19397407SSherry Moore 742*19397407SSherry Moore /* 743*19397407SSherry Moore * Validate that the file exists, and is an ELF file. 744*19397407SSherry Moore * Returns 0 on success, -1 on failure. 745*19397407SSherry Moore */ 746*19397407SSherry Moore static int 747*19397407SSherry Moore validate_unix(char *arg, int *mplen, int *is_zfs, char *bootfs_arg, 748*19397407SSherry Moore int *failsafe) 749*19397407SSherry Moore { 750*19397407SSherry Moore const char *location; 751*19397407SSherry Moore int class, format; 752*19397407SSherry Moore unsigned char ident[EI_NIDENT]; 753*19397407SSherry Moore char physpath[MAXPATHLEN]; 754*19397407SSherry Moore int elffd = -1; 755*19397407SSherry Moore size_t sz; 756*19397407SSherry Moore 757*19397407SSherry Moore if ((sz = resolvepath(arg, physpath, sizeof (physpath) - 1)) == 758*19397407SSherry Moore (size_t)-1) { 759*19397407SSherry Moore (void) fprintf(stderr, 760*19397407SSherry Moore gettext("cannot resolve path for %s: %s\n"), 761*19397407SSherry Moore arg, strerror(errno)); 762*19397407SSherry Moore return (-1); 763*19397407SSherry Moore } 764*19397407SSherry Moore (void) strlcpy(arg, physpath, sz + 1); 765*19397407SSherry Moore 766*19397407SSherry Moore if (strlen(arg) > MAXPATHLEN) { 767*19397407SSherry Moore (void) fprintf(stderr, 768*19397407SSherry Moore gettext("%s: new kernel name is too long\n"), cmdname); 769*19397407SSherry Moore return (-1); 770*19397407SSherry Moore } 771*19397407SSherry Moore 772*19397407SSherry Moore if (strncmp(basename(arg), "unix", 4) != 0) { 773*19397407SSherry Moore (void) fprintf(stderr, 774*19397407SSherry Moore gettext("%s: %s: kernel name must be unix\n"), 775*19397407SSherry Moore cmdname, arg); 776*19397407SSherry Moore return (-1); 777*19397407SSherry Moore } 778*19397407SSherry Moore 779*19397407SSherry Moore if (get_zfs_bootfs_arg(arg, &location, is_zfs, bootfs_arg) != 0) 780*19397407SSherry Moore goto err_out; 781*19397407SSherry Moore 782*19397407SSherry Moore *mplen = location - arg; 783*19397407SSherry Moore 784*19397407SSherry Moore if ((strstr(location, "/boot/platform")) == location) 785*19397407SSherry Moore *failsafe = 1; 786*19397407SSherry Moore else if ((strstr(location, "/platform")) == location) 787*19397407SSherry Moore *failsafe = 0; 788*19397407SSherry Moore else { 789*19397407SSherry Moore (void) fprintf(stderr, 790*19397407SSherry Moore gettext("%s: %s: no /boot/platform or /platform in" 791*19397407SSherry Moore " file name\n"), cmdname, arg); 792*19397407SSherry Moore goto err_out; 793*19397407SSherry Moore } 794*19397407SSherry Moore 795*19397407SSherry Moore if ((elffd = open64(arg, O_RDONLY)) < 0 || 796*19397407SSherry Moore (pread64(elffd, ident, EI_NIDENT, 0) != EI_NIDENT)) { 797*19397407SSherry Moore (void) fprintf(stderr, "%s: %s: %s\n", 798*19397407SSherry Moore cmdname, arg, strerror(errno)); 799*19397407SSherry Moore goto err_out; 800*19397407SSherry Moore } 801*19397407SSherry Moore 802*19397407SSherry Moore class = ident[EI_CLASS]; 803*19397407SSherry Moore 804*19397407SSherry Moore if ((class != ELFCLASS32 && class != ELFCLASS64) || 805*19397407SSherry Moore ident[EI_MAG0] != ELFMAG0 || ident[EI_MAG1] != ELFMAG1 || 806*19397407SSherry Moore ident[EI_MAG2] != ELFMAG2 || ident[EI_MAG3] != ELFMAG3) { 807*19397407SSherry Moore (void) fprintf(stderr, 808*19397407SSherry Moore gettext("%s: %s: not a valid ELF file\n"), 809*19397407SSherry Moore cmdname, arg); 810*19397407SSherry Moore goto err_out; 811*19397407SSherry Moore } 812*19397407SSherry Moore 813*19397407SSherry Moore format = ident[EI_DATA]; 814*19397407SSherry Moore 815*19397407SSherry Moore if (format != CUR_ELFDATA) { 816*19397407SSherry Moore (void) fprintf(stderr, gettext("%s: %s: invalid data format\n"), 817*19397407SSherry Moore cmdname, arg); 818*19397407SSherry Moore goto err_out; 819*19397407SSherry Moore } 820*19397407SSherry Moore 821*19397407SSherry Moore return (0); 822*19397407SSherry Moore 823*19397407SSherry Moore err_out: 824*19397407SSherry Moore if (elffd >= 0) { 825*19397407SSherry Moore (void) close(elffd); 826*19397407SSherry Moore elffd = -1; 827*19397407SSherry Moore } 828*19397407SSherry Moore return (-1); 829*19397407SSherry Moore } 830*19397407SSherry Moore 831*19397407SSherry Moore #ifndef __i386 832*19397407SSherry Moore /* ARGSUSED */ 833*19397407SSherry Moore #endif /* __i386 */ 834*19397407SSherry Moore static int 835*19397407SSherry Moore is_fastboot_default(uid_t uid) 836*19397407SSherry Moore { 837*19397407SSherry Moore #if defined(__i386) 838*19397407SSherry Moore int ret; 839*19397407SSherry Moore struct stat st; 840*19397407SSherry Moore static const char fastboot_default[] = "/etc/fastreboot"; 841*19397407SSherry Moore 842*19397407SSherry Moore ret = (lstat(fastboot_default, &st) == 0 && 843*19397407SSherry Moore S_ISREG(st.st_mode) && 844*19397407SSherry Moore (st.st_mode & S_IRUSR) != 0 && 845*19397407SSherry Moore uid == st.st_uid); 846*19397407SSherry Moore 847*19397407SSherry Moore return (ret); 848*19397407SSherry Moore #else 849*19397407SSherry Moore return (0); 850*19397407SSherry Moore #endif /* __i386 */ 851*19397407SSherry Moore } 852*19397407SSherry Moore 853*19397407SSherry Moore static int 854*19397407SSherry Moore fastboot_bename(const char *bename, char *mountpoint, size_t mpsz) 855*19397407SSherry Moore { 856*19397407SSherry Moore int rc; 857*19397407SSherry Moore char cmdbuf[MAXPATHLEN]; 858*19397407SSherry Moore 859*19397407SSherry Moore (void) snprintf(cmdbuf, sizeof (cmdbuf), 860*19397407SSherry Moore "/usr/sbin/luumount %s > /dev/null 2>&1", bename); 861*19397407SSherry Moore (void) system(cmdbuf); 862*19397407SSherry Moore 863*19397407SSherry Moore (void) snprintf(cmdbuf, sizeof (cmdbuf), 864*19397407SSherry Moore "/usr/sbin/lumount %s %s > /dev/null 2>&1", 865*19397407SSherry Moore bename, FASTBOOT_MOUNTPOINT); 866*19397407SSherry Moore if ((rc = system(cmdbuf)) != 0) 867*19397407SSherry Moore (void) fprintf(stderr, gettext("%s: cannot mount BE %s\n"), 868*19397407SSherry Moore cmdname, bename); 869*19397407SSherry Moore else 870*19397407SSherry Moore (void) strlcpy(mountpoint, FASTBOOT_MOUNTPOINT, mpsz); 871*19397407SSherry Moore 872*19397407SSherry Moore return (rc); 873*19397407SSherry Moore } 874*19397407SSherry Moore 875*19397407SSherry Moore /* 876*19397407SSherry Moore * Returns 0 on successful parsing of the arguments; 877*19397407SSherry Moore * retuens non-zero on failure. 878*19397407SSherry Moore */ 879*19397407SSherry Moore static int 880*19397407SSherry Moore parse_fastboot_args(char *bootargs_buf, int *is_dryrun, const char *bename, 881*19397407SSherry Moore int *failsafe) 882*19397407SSherry Moore { 883*19397407SSherry Moore char mountpoint[MAXPATHLEN]; 884*19397407SSherry Moore char bootargs_saved[BOOTARGS_MAX]; 885*19397407SSherry Moore char bootargs_scratch[BOOTARGS_MAX]; 886*19397407SSherry Moore char bootfs_arg[BOOTARGS_MAX]; 887*19397407SSherry Moore char unixfile[BOOTARGS_MAX]; 888*19397407SSherry Moore char *head, *newarg; 889*19397407SSherry Moore int buflen; /* length of the bootargs_buf */ 890*19397407SSherry Moore int mplen; /* length of the mount point */ 891*19397407SSherry Moore int rootlen = 0; /* length of the root argument */ 892*19397407SSherry Moore int unixlen = 0; /* length of the unix argument */ 893*19397407SSherry Moore int off = 0; /* offset into the new boot argument */ 894*19397407SSherry Moore int is_zfs = 0; 895*19397407SSherry Moore int rc = 0; 896*19397407SSherry Moore 897*19397407SSherry Moore bzero(mountpoint, sizeof (mountpoint)); 898*19397407SSherry Moore 899*19397407SSherry Moore /* 900*19397407SSherry Moore * If argc is not 0, buflen is length of the argument being passed in; 901*19397407SSherry Moore * else it is 0 as bootargs_buf has been initialized to all 0's. 902*19397407SSherry Moore */ 903*19397407SSherry Moore buflen = strlen(bootargs_buf); 904*19397407SSherry Moore 905*19397407SSherry Moore /* Save a copy of the original argument */ 906*19397407SSherry Moore bcopy(bootargs_buf, bootargs_saved, buflen); 907*19397407SSherry Moore bzero(&bootargs_saved[buflen], sizeof (bootargs_saved) - buflen); 908*19397407SSherry Moore 909*19397407SSherry Moore /* Save another copy to be used by strtok */ 910*19397407SSherry Moore bcopy(bootargs_buf, bootargs_scratch, buflen); 911*19397407SSherry Moore bzero(&bootargs_scratch[buflen], sizeof (bootargs_scratch) - buflen); 912*19397407SSherry Moore head = &bootargs_scratch[0]; 913*19397407SSherry Moore 914*19397407SSherry Moore /* Zero out the boot argument buffer as we will reconstruct it */ 915*19397407SSherry Moore bzero(bootargs_buf, BOOTARGS_MAX); 916*19397407SSherry Moore bzero(bootfs_arg, BOOTARGS_MAX); 917*19397407SSherry Moore bzero(unixfile, sizeof (unixfile)); 918*19397407SSherry Moore 919*19397407SSherry Moore /* Get the first argument */ 920*19397407SSherry Moore newarg = strtok(bootargs_scratch, " "); 921*19397407SSherry Moore 922*19397407SSherry Moore /* 923*19397407SSherry Moore * If this is a dry run request, verify that the drivers can handle 924*19397407SSherry Moore * fast reboot. 925*19397407SSherry Moore */ 926*19397407SSherry Moore if (newarg && strncasecmp(newarg, "dryrun", strlen("dryrun")) == 0) { 927*19397407SSherry Moore *is_dryrun = 1; 928*19397407SSherry Moore (void) system("/usr/sbin/devfsadm"); 929*19397407SSherry Moore } 930*19397407SSherry Moore 931*19397407SSherry Moore /* 932*19397407SSherry Moore * Always perform a dry run to identify all the drivers that 933*19397407SSherry Moore * need to implement devo_reset(). 934*19397407SSherry Moore */ 935*19397407SSherry Moore if (uadmin(A_SHUTDOWN, AD_FASTREBOOT_DRYRUN, 936*19397407SSherry Moore (uintptr_t)bootargs_saved) != 0) { 937*19397407SSherry Moore (void) fprintf(stderr, gettext("%s: not all drivers " 938*19397407SSherry Moore "have implemented quiesce(9E)\n"), cmdname); 939*19397407SSherry Moore } else if (*is_dryrun) { 940*19397407SSherry Moore (void) fprintf(stderr, gettext("%s: all drivers have " 941*19397407SSherry Moore "implemented quiesce(9E)\n"), cmdname); 942*19397407SSherry Moore } 943*19397407SSherry Moore 944*19397407SSherry Moore /* 945*19397407SSherry Moore * Return if it is a true dry run. 946*19397407SSherry Moore */ 947*19397407SSherry Moore if (*is_dryrun) 948*19397407SSherry Moore return (rc); 949*19397407SSherry Moore 950*19397407SSherry Moore if (bename && (rc = fastboot_bename(bename, mountpoint, 951*19397407SSherry Moore sizeof (mountpoint))) != 0) 952*19397407SSherry Moore return (rc); 953*19397407SSherry Moore 954*19397407SSherry Moore /* 955*19397407SSherry Moore * If BE is not specified, look for disk argument to construct 956*19397407SSherry Moore * mountpoint; if BE has been specified, mountpoint has already been 957*19397407SSherry Moore * constructed. 958*19397407SSherry Moore */ 959*19397407SSherry Moore if (newarg && newarg[0] != '-' && !bename) { 960*19397407SSherry Moore int tmprc; 961*19397407SSherry Moore 962*19397407SSherry Moore if ((tmprc = validate_disk(newarg, mountpoint)) == 0) { 963*19397407SSherry Moore /* 964*19397407SSherry Moore * The first argument is a valid root argument. 965*19397407SSherry Moore * Get the next argument. 966*19397407SSherry Moore */ 967*19397407SSherry Moore newarg = strtok(NULL, " "); 968*19397407SSherry Moore rootlen = (newarg) ? (newarg - head) : buflen; 969*19397407SSherry Moore (void) strlcpy(fastboot_mounted, mountpoint, 970*19397407SSherry Moore sizeof (fastboot_mounted)); 971*19397407SSherry Moore 972*19397407SSherry Moore } else if (tmprc == -1) { 973*19397407SSherry Moore /* 974*19397407SSherry Moore * Not a disk argument. Use / as default root. 975*19397407SSherry Moore */ 976*19397407SSherry Moore bcopy("/", mountpoint, 1); 977*19397407SSherry Moore bzero(&mountpoint[1], sizeof (mountpoint) - 1); 978*19397407SSherry Moore } else { 979*19397407SSherry Moore /* 980*19397407SSherry Moore * Disk argument, but not valid or not root. 981*19397407SSherry Moore * Return failure. 982*19397407SSherry Moore */ 983*19397407SSherry Moore return (EINVAL); 984*19397407SSherry Moore } 985*19397407SSherry Moore } 986*19397407SSherry Moore 987*19397407SSherry Moore /* 988*19397407SSherry Moore * Make mountpoint the first part of unixfile. 989*19397407SSherry Moore * If there is not disk argument, and BE has not been specified, 990*19397407SSherry Moore * mountpoint could be empty. 991*19397407SSherry Moore */ 992*19397407SSherry Moore mplen = strlen(mountpoint); 993*19397407SSherry Moore bcopy(mountpoint, unixfile, mplen); 994*19397407SSherry Moore 995*19397407SSherry Moore /* 996*19397407SSherry Moore * Look for unix argument 997*19397407SSherry Moore */ 998*19397407SSherry Moore if (newarg && newarg[0] != '-') { 999*19397407SSherry Moore bcopy(newarg, &unixfile[mplen], strlen(newarg)); 1000*19397407SSherry Moore newarg = strtok(NULL, " "); 1001*19397407SSherry Moore rootlen = (newarg) ? (newarg - head) : buflen; 1002*19397407SSherry Moore } else if (mplen != 0) { 1003*19397407SSherry Moore /* 1004*19397407SSherry Moore * No unix argument, but mountpoint is not empty, use 1005*19397407SSherry Moore * /platform/i86pc/$ISADIR/kernel/unix as default. 1006*19397407SSherry Moore */ 1007*19397407SSherry Moore char isa[20]; 1008*19397407SSherry Moore 1009*19397407SSherry Moore if (sysinfo(SI_ARCHITECTURE_64, isa, sizeof (isa)) != -1) 1010*19397407SSherry Moore (void) snprintf(&unixfile[mplen], 1011*19397407SSherry Moore sizeof (unixfile) - mplen, 1012*19397407SSherry Moore "/platform/i86pc/kernel/%s/unix", isa); 1013*19397407SSherry Moore else if (sysinfo(SI_ARCHITECTURE_32, isa, sizeof (isa)) != -1) { 1014*19397407SSherry Moore (void) snprintf(&unixfile[mplen], 1015*19397407SSherry Moore sizeof (unixfile) - mplen, 1016*19397407SSherry Moore "/platform/i86pc/kernel/unix"); 1017*19397407SSherry Moore } else { 1018*19397407SSherry Moore (void) fprintf(stderr, 1019*19397407SSherry Moore gettext("%s: unknown architecture"), cmdname); 1020*19397407SSherry Moore return (EINVAL); 1021*19397407SSherry Moore } 1022*19397407SSherry Moore } 1023*19397407SSherry Moore 1024*19397407SSherry Moore /* 1025*19397407SSherry Moore * We now have the complete unix argument. Verify that it exists and 1026*19397407SSherry Moore * is an ELF file. Split the argument up into mountpoint and unix 1027*19397407SSherry Moore * portions again. This is necessary to handle cases where mountpoint 1028*19397407SSherry Moore * is specified on the command line as part of the unix argument, 1029*19397407SSherry Moore * such as this: 1030*19397407SSherry Moore * # reboot -f /.alt/platform/i86pc/kernel/amd64/unix 1031*19397407SSherry Moore */ 1032*19397407SSherry Moore unixlen = strlen(unixfile); 1033*19397407SSherry Moore if (unixlen > 0) { 1034*19397407SSherry Moore if (validate_unix(unixfile, &mplen, &is_zfs, 1035*19397407SSherry Moore bootfs_arg, failsafe) != 0) { 1036*19397407SSherry Moore /* Not a valid unix file */ 1037*19397407SSherry Moore return (EINVAL); 1038*19397407SSherry Moore } else { 1039*19397407SSherry Moore /* 1040*19397407SSherry Moore * Construct boot argument. 1041*19397407SSherry Moore */ 1042*19397407SSherry Moore unixlen = strlen(unixfile); 1043*19397407SSherry Moore bcopy(unixfile, bootargs_buf, mplen); 1044*19397407SSherry Moore (void) strcat(bootargs_buf, " "); 1045*19397407SSherry Moore bcopy(&unixfile[mplen], &bootargs_buf[mplen + 1], 1046*19397407SSherry Moore unixlen - mplen); 1047*19397407SSherry Moore (void) strcat(bootargs_buf, " "); 1048*19397407SSherry Moore off += unixlen + 2; 1049*19397407SSherry Moore } 1050*19397407SSherry Moore } else { 1051*19397407SSherry Moore /* Check to see if root is zfs */ 1052*19397407SSherry Moore const char *dp; 1053*19397407SSherry Moore (void) get_zfs_bootfs_arg("/", &dp, &is_zfs, bootfs_arg); 1054*19397407SSherry Moore } 1055*19397407SSherry Moore 1056*19397407SSherry Moore if (is_zfs && (buflen != 0 || bename != NULL)) { 1057*19397407SSherry Moore /* LINTED E_SEC_SPRINTF_UNBOUNDED_COPY */ 1058*19397407SSherry Moore off += sprintf(bootargs_buf + off, "%s ", bootfs_arg); 1059*19397407SSherry Moore } 1060*19397407SSherry Moore 1061*19397407SSherry Moore /* 1062*19397407SSherry Moore * Copy the rest of the arguments 1063*19397407SSherry Moore */ 1064*19397407SSherry Moore bcopy(&bootargs_saved[rootlen], &bootargs_buf[off], buflen - rootlen); 1065*19397407SSherry Moore 1066*19397407SSherry Moore return (rc); 1067*19397407SSherry Moore } 1068*19397407SSherry Moore 10697c478bd9Sstevel@tonic-gate int 10707c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 10717c478bd9Sstevel@tonic-gate { 10727c478bd9Sstevel@tonic-gate char *ttyn = ttyname(STDERR_FILENO); 10737c478bd9Sstevel@tonic-gate 1074*19397407SSherry Moore uid_t euid; 10757c478bd9Sstevel@tonic-gate int qflag = 0, needlog = 1, nosync = 0; 1076*19397407SSherry Moore int fast_reboot = 0; 10777c478bd9Sstevel@tonic-gate uintptr_t mdep = NULL; 10787c478bd9Sstevel@tonic-gate int cmd, fcn, c, aval, r; 10797c478bd9Sstevel@tonic-gate const char *usage; 10807c478bd9Sstevel@tonic-gate zoneid_t zoneid = getzoneid(); 10813f2f09c1Sdp int need_check_zones = 0; 10823f2f09c1Sdp char bootargs_buf[BOOTARGS_MAX]; 1083*19397407SSherry Moore int failsafe = 0; 1084*19397407SSherry Moore char *bename = NULL; 10857c478bd9Sstevel@tonic-gate 10867c478bd9Sstevel@tonic-gate const char * const resetting = "/etc/svc/volatile/resetting"; 10877c478bd9Sstevel@tonic-gate 10887c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 10897c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 10907c478bd9Sstevel@tonic-gate 10913f2f09c1Sdp cmdname = basename(argv[0]); 10923f2f09c1Sdp 10937c478bd9Sstevel@tonic-gate if (strcmp(cmdname, "halt") == 0) { 10947c478bd9Sstevel@tonic-gate (void) audit_halt_setup(argc, argv); 10957c478bd9Sstevel@tonic-gate usage = gettext("usage: %s [ -dlnqy ]\n"); 10967c478bd9Sstevel@tonic-gate cmd = A_SHUTDOWN; 10977c478bd9Sstevel@tonic-gate fcn = AD_HALT; 10987c478bd9Sstevel@tonic-gate } else if (strcmp(cmdname, "poweroff") == 0) { 10997c478bd9Sstevel@tonic-gate (void) audit_halt_setup(argc, argv); 11007c478bd9Sstevel@tonic-gate usage = gettext("usage: %s [ -dlnqy ]\n"); 11017c478bd9Sstevel@tonic-gate cmd = A_SHUTDOWN; 11027c478bd9Sstevel@tonic-gate fcn = AD_POWEROFF; 11037c478bd9Sstevel@tonic-gate } else if (strcmp(cmdname, "reboot") == 0) { 11047c478bd9Sstevel@tonic-gate (void) audit_reboot_setup(); 1105*19397407SSherry Moore #if defined(__i386) 1106*19397407SSherry Moore usage = gettext("usage: %s [ -dlnqfe: ] [ boot args ]\n"); 1107*19397407SSherry Moore #else 11087c478bd9Sstevel@tonic-gate usage = gettext("usage: %s [ -dlnq ] [ boot args ]\n"); 1109*19397407SSherry Moore #endif 11107c478bd9Sstevel@tonic-gate cmd = A_SHUTDOWN; 11117c478bd9Sstevel@tonic-gate fcn = AD_BOOT; 11127c478bd9Sstevel@tonic-gate } else { 11137c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 11147c478bd9Sstevel@tonic-gate gettext("%s: not installed properly\n"), cmdname); 11157c478bd9Sstevel@tonic-gate return (1); 11167c478bd9Sstevel@tonic-gate } 11177c478bd9Sstevel@tonic-gate 1118*19397407SSherry Moore while ((c = getopt(argc, argv, "dlnqyfe:")) != EOF) { 11197c478bd9Sstevel@tonic-gate switch (c) { 11207c478bd9Sstevel@tonic-gate case 'd': 11217c478bd9Sstevel@tonic-gate if (zoneid == GLOBAL_ZONEID) 11227c478bd9Sstevel@tonic-gate cmd = A_DUMP; 11237c478bd9Sstevel@tonic-gate else { 11247c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 11257c478bd9Sstevel@tonic-gate gettext("%s: -d only valid from global" 11267c478bd9Sstevel@tonic-gate " zone\n"), cmdname); 11277c478bd9Sstevel@tonic-gate return (1); 11287c478bd9Sstevel@tonic-gate } 11297c478bd9Sstevel@tonic-gate break; 11307c478bd9Sstevel@tonic-gate case 'l': 11317c478bd9Sstevel@tonic-gate needlog = 0; 11327c478bd9Sstevel@tonic-gate break; 11337c478bd9Sstevel@tonic-gate case 'n': 11347c478bd9Sstevel@tonic-gate nosync = 1; 11357c478bd9Sstevel@tonic-gate break; 11367c478bd9Sstevel@tonic-gate case 'q': 11377c478bd9Sstevel@tonic-gate qflag = 1; 11387c478bd9Sstevel@tonic-gate break; 11397c478bd9Sstevel@tonic-gate case 'y': 11407c478bd9Sstevel@tonic-gate ttyn = NULL; 11417c478bd9Sstevel@tonic-gate break; 1142*19397407SSherry Moore #if defined(__i386) 1143*19397407SSherry Moore case 'f': 1144*19397407SSherry Moore fast_reboot = 1; 1145*19397407SSherry Moore break; 1146*19397407SSherry Moore case 'e': 1147*19397407SSherry Moore bename = optarg; 1148*19397407SSherry Moore break; 1149*19397407SSherry Moore #endif 11507c478bd9Sstevel@tonic-gate default: 11517c478bd9Sstevel@tonic-gate /* 11527c478bd9Sstevel@tonic-gate * TRANSLATION_NOTE 11537c478bd9Sstevel@tonic-gate * Don't translate the words "halt" or "reboot" 11547c478bd9Sstevel@tonic-gate */ 11557c478bd9Sstevel@tonic-gate (void) fprintf(stderr, usage, cmdname); 11567c478bd9Sstevel@tonic-gate return (1); 11577c478bd9Sstevel@tonic-gate } 11587c478bd9Sstevel@tonic-gate } 11597c478bd9Sstevel@tonic-gate 11607c478bd9Sstevel@tonic-gate argc -= optind; 11617c478bd9Sstevel@tonic-gate argv += optind; 11627c478bd9Sstevel@tonic-gate 11637c478bd9Sstevel@tonic-gate if (argc != 0) { 11647c478bd9Sstevel@tonic-gate if (fcn != AD_BOOT) { 11657c478bd9Sstevel@tonic-gate (void) fprintf(stderr, usage, cmdname); 11667c478bd9Sstevel@tonic-gate return (1); 11677c478bd9Sstevel@tonic-gate } 11687c478bd9Sstevel@tonic-gate 11697c478bd9Sstevel@tonic-gate /* Gather the arguments into bootargs_buf. */ 11707c478bd9Sstevel@tonic-gate if (gather_args(argv, bootargs_buf, sizeof (bootargs_buf)) != 11717c478bd9Sstevel@tonic-gate 0) { 11727c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 11737c478bd9Sstevel@tonic-gate gettext("%s: Boot arguments too long.\n"), cmdname); 11747c478bd9Sstevel@tonic-gate return (1); 11757c478bd9Sstevel@tonic-gate } 1176*19397407SSherry Moore 11777c478bd9Sstevel@tonic-gate mdep = (uintptr_t)bootargs_buf; 1178*19397407SSherry Moore } else { 1179*19397407SSherry Moore /* 1180*19397407SSherry Moore * Initialize it to 0 in case of fastboot, the buffer 1181*19397407SSherry Moore * will be used. 1182*19397407SSherry Moore */ 1183*19397407SSherry Moore bzero(bootargs_buf, sizeof (bootargs_buf)); 11847c478bd9Sstevel@tonic-gate } 11857c478bd9Sstevel@tonic-gate 1186*19397407SSherry Moore if ((euid = geteuid()) != 0) { 11877c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 11887c478bd9Sstevel@tonic-gate gettext("%s: permission denied\n"), cmdname); 11897c478bd9Sstevel@tonic-gate goto fail; 11907c478bd9Sstevel@tonic-gate } 11917c478bd9Sstevel@tonic-gate 1192*19397407SSherry Moore /* 1193*19397407SSherry Moore * Check whether fast reboot is the default operating mode 1194*19397407SSherry Moore */ 1195*19397407SSherry Moore if (!fast_reboot) 1196*19397407SSherry Moore fast_reboot = is_fastboot_default(euid); 1197*19397407SSherry Moore 1198*19397407SSherry Moore if (bename && !fast_reboot) { 1199*19397407SSherry Moore (void) fprintf(stderr, gettext("%s: -e only valid with -f\n"), 1200*19397407SSherry Moore cmdname); 1201*19397407SSherry Moore return (EINVAL); 1202*19397407SSherry Moore } 1203*19397407SSherry Moore 1204*19397407SSherry Moore 1205*19397407SSherry Moore /* 1206*19397407SSherry Moore * If fast reboot, do some sanity check on the argument 1207*19397407SSherry Moore */ 1208*19397407SSherry Moore if (fast_reboot) { 1209*19397407SSherry Moore int rc; 1210*19397407SSherry Moore int is_dryrun = 0; 1211*19397407SSherry Moore 1212*19397407SSherry Moore if (zoneid != GLOBAL_ZONEID) { 1213*19397407SSherry Moore (void) fprintf(stderr, 1214*19397407SSherry Moore gettext("%s: fast reboot only valid from global" 1215*19397407SSherry Moore " zone\n"), cmdname); 1216*19397407SSherry Moore return (EINVAL); 1217*19397407SSherry Moore } 1218*19397407SSherry Moore 1219*19397407SSherry Moore rc = parse_fastboot_args(bootargs_buf, &is_dryrun, 1220*19397407SSherry Moore bename, &failsafe); 1221*19397407SSherry Moore 1222*19397407SSherry Moore /* 1223*19397407SSherry Moore * If dry run, or if arguments are invalid, return. 1224*19397407SSherry Moore */ 1225*19397407SSherry Moore if (is_dryrun) 1226*19397407SSherry Moore return (rc); 1227*19397407SSherry Moore else if (rc != 0) 1228*19397407SSherry Moore goto fail; 1229*19397407SSherry Moore 1230*19397407SSherry Moore /* 1231*19397407SSherry Moore * For all the other errors, we continue on in case user 1232*19397407SSherry Moore * user want to force fast reboot. 1233*19397407SSherry Moore */ 1234*19397407SSherry Moore if (strlen(bootargs_buf) != 0) 1235*19397407SSherry Moore mdep = (uintptr_t)bootargs_buf; 1236*19397407SSherry Moore } 1237*19397407SSherry Moore 1238*19397407SSherry Moore #if 0 /* For debugging */ 1239*19397407SSherry Moore if (mdep != NULL) 1240*19397407SSherry Moore (void) fprintf(stderr, "mdep = %s\n", (char *)mdep); 1241*19397407SSherry Moore #endif 1242*19397407SSherry Moore 12437c478bd9Sstevel@tonic-gate if (fcn != AD_BOOT && ttyn != NULL && 12447c478bd9Sstevel@tonic-gate strncmp(ttyn, "/dev/term/", strlen("/dev/term/")) == 0) { 12457c478bd9Sstevel@tonic-gate /* 12467c478bd9Sstevel@tonic-gate * TRANSLATION_NOTE 12477c478bd9Sstevel@tonic-gate * Don't translate ``halt -y'' 12487c478bd9Sstevel@tonic-gate */ 12497c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 12507c478bd9Sstevel@tonic-gate gettext("%s: dangerous on a dialup;"), cmdname); 12517c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 12527c478bd9Sstevel@tonic-gate gettext("use ``%s -y'' if you are really sure\n"), cmdname); 12537c478bd9Sstevel@tonic-gate goto fail; 12547c478bd9Sstevel@tonic-gate } 12557c478bd9Sstevel@tonic-gate 12567c478bd9Sstevel@tonic-gate if (needlog) { 12577c478bd9Sstevel@tonic-gate char *user = getlogin(); 12587c478bd9Sstevel@tonic-gate struct passwd *pw; 1259f040a7a6Ssetje char *tty; 12607c478bd9Sstevel@tonic-gate 12617c478bd9Sstevel@tonic-gate openlog(cmdname, 0, LOG_AUTH); 12627c478bd9Sstevel@tonic-gate if (user == NULL && (pw = getpwuid(getuid())) != NULL) 12637c478bd9Sstevel@tonic-gate user = pw->pw_name; 12647c478bd9Sstevel@tonic-gate if (user == NULL) 12657c478bd9Sstevel@tonic-gate user = "root"; 1266f040a7a6Ssetje 1267f040a7a6Ssetje tty = ttyname(1); 1268f040a7a6Ssetje 1269f040a7a6Ssetje if (tty == NULL) 1270f040a7a6Ssetje syslog(LOG_CRIT, "initiated by %s", user); 1271f040a7a6Ssetje else 1272f040a7a6Ssetje syslog(LOG_CRIT, "initiated by %s on %s", user, tty); 12737c478bd9Sstevel@tonic-gate } 12747c478bd9Sstevel@tonic-gate 12757c478bd9Sstevel@tonic-gate /* 12767c478bd9Sstevel@tonic-gate * We must assume success and log it before auditd is terminated. 12777c478bd9Sstevel@tonic-gate */ 12787c478bd9Sstevel@tonic-gate if (fcn == AD_BOOT) 12797c478bd9Sstevel@tonic-gate aval = audit_reboot_success(); 12807c478bd9Sstevel@tonic-gate else 12817c478bd9Sstevel@tonic-gate aval = audit_halt_success(); 12827c478bd9Sstevel@tonic-gate 12837c478bd9Sstevel@tonic-gate if (aval == -1) { 12847c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 12857c478bd9Sstevel@tonic-gate gettext("%s: can't turn off auditd\n"), cmdname); 12867c478bd9Sstevel@tonic-gate if (needlog) 12877c478bd9Sstevel@tonic-gate (void) sleep(5); /* Give syslogd time to record this */ 12887c478bd9Sstevel@tonic-gate } 12897c478bd9Sstevel@tonic-gate 12907c478bd9Sstevel@tonic-gate (void) signal(SIGHUP, SIG_IGN); /* for remote connections */ 12917c478bd9Sstevel@tonic-gate 129226f665e8Sdstaff /* 129326f665e8Sdstaff * We start to fork a bunch of zoneadms to halt any active zones. 129426f665e8Sdstaff * This will proceed with halt in parallel until we call 129526f665e8Sdstaff * check_zone_haltedness later on. 129626f665e8Sdstaff */ 129726f665e8Sdstaff if (zoneid == GLOBAL_ZONEID && cmd != A_DUMP) { 12983f2f09c1Sdp need_check_zones = halt_zones(); 129926f665e8Sdstaff } 130026f665e8Sdstaff 130126f665e8Sdstaff 13027c478bd9Sstevel@tonic-gate /* sync boot archive in the global zone */ 13033f2f09c1Sdp if (zoneid == GLOBAL_ZONEID && !nosync) { 1304*19397407SSherry Moore if (fast_reboot) 1305*19397407SSherry Moore (void) system("/sbin/bootadm -a update_all fastboot"); 1306*19397407SSherry Moore else 1307*19397407SSherry Moore (void) system("/sbin/bootadm -a update_all"); 13087c478bd9Sstevel@tonic-gate } 13097c478bd9Sstevel@tonic-gate 13107c478bd9Sstevel@tonic-gate /* 13117c478bd9Sstevel@tonic-gate * If we're not forcing a crash dump, mark the system as quiescing for 13127c478bd9Sstevel@tonic-gate * smf(5)'s benefit, and idle the init process. 13137c478bd9Sstevel@tonic-gate */ 13147c478bd9Sstevel@tonic-gate if (cmd != A_DUMP) { 13153f2f09c1Sdp if (direct_init(PCDSTOP) == -1) { 13167c478bd9Sstevel@tonic-gate /* 13177c478bd9Sstevel@tonic-gate * TRANSLATION_NOTE 13187c478bd9Sstevel@tonic-gate * Don't translate the word "init" 13197c478bd9Sstevel@tonic-gate */ 13207c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 13217c478bd9Sstevel@tonic-gate gettext("%s: can't idle init\n"), cmdname); 13227c478bd9Sstevel@tonic-gate goto fail; 13237c478bd9Sstevel@tonic-gate } 13247c478bd9Sstevel@tonic-gate 13257c478bd9Sstevel@tonic-gate if (creat(resetting, 0755) == -1) 13267c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 13277c478bd9Sstevel@tonic-gate gettext("%s: could not create %s.\n"), 13287c478bd9Sstevel@tonic-gate cmdname, resetting); 13297c478bd9Sstevel@tonic-gate 13307c478bd9Sstevel@tonic-gate /* 13317c478bd9Sstevel@tonic-gate * Stop all restarters so they do not try to restart services 13327c478bd9Sstevel@tonic-gate * that are terminated. 13337c478bd9Sstevel@tonic-gate */ 13347c478bd9Sstevel@tonic-gate stop_restarters(); 133526f665e8Sdstaff 133626f665e8Sdstaff /* 133726f665e8Sdstaff * Wait a little while for zones to shutdown. 133826f665e8Sdstaff */ 133926f665e8Sdstaff if (need_check_zones) { 13403f2f09c1Sdp check_zones_haltedness(); 134126f665e8Sdstaff 134226f665e8Sdstaff (void) fprintf(stderr, 134326f665e8Sdstaff gettext("%s: Completing system halt.\n"), 134426f665e8Sdstaff cmdname); 134526f665e8Sdstaff } 13467c478bd9Sstevel@tonic-gate } 13477c478bd9Sstevel@tonic-gate 13487c478bd9Sstevel@tonic-gate /* 13497c478bd9Sstevel@tonic-gate * Make sure we don't get stopped by a jobcontrol shell 13507c478bd9Sstevel@tonic-gate * once we start killing everybody. 13517c478bd9Sstevel@tonic-gate */ 13527c478bd9Sstevel@tonic-gate (void) signal(SIGTSTP, SIG_IGN); 13537c478bd9Sstevel@tonic-gate (void) signal(SIGTTIN, SIG_IGN); 13547c478bd9Sstevel@tonic-gate (void) signal(SIGTTOU, SIG_IGN); 13557c478bd9Sstevel@tonic-gate (void) signal(SIGTERM, SIG_IGN); 13567c478bd9Sstevel@tonic-gate 13577c478bd9Sstevel@tonic-gate /* 13587c478bd9Sstevel@tonic-gate * If we're not forcing a crash dump, give everyone 5 seconds to 13597c478bd9Sstevel@tonic-gate * handle a SIGTERM and clean up properly. 13607c478bd9Sstevel@tonic-gate */ 13617c478bd9Sstevel@tonic-gate if (cmd != A_DUMP) { 13627c478bd9Sstevel@tonic-gate (void) kill(-1, SIGTERM); 13637c478bd9Sstevel@tonic-gate (void) sleep(5); 13647c478bd9Sstevel@tonic-gate } 13657c478bd9Sstevel@tonic-gate 13667c478bd9Sstevel@tonic-gate if (!qflag && !nosync) { 13677c478bd9Sstevel@tonic-gate struct utmpx wtmpx; 13687c478bd9Sstevel@tonic-gate 13697c478bd9Sstevel@tonic-gate bzero(&wtmpx, sizeof (struct utmpx)); 13707c478bd9Sstevel@tonic-gate (void) strcpy(wtmpx.ut_line, "~"); 13717c478bd9Sstevel@tonic-gate (void) time(&wtmpx.ut_tv.tv_sec); 13727c478bd9Sstevel@tonic-gate 13737c478bd9Sstevel@tonic-gate if (cmd == A_DUMP) 13747c478bd9Sstevel@tonic-gate (void) strcpy(wtmpx.ut_name, "crash dump"); 13757c478bd9Sstevel@tonic-gate else 13767c478bd9Sstevel@tonic-gate (void) strcpy(wtmpx.ut_name, "shutdown"); 13777c478bd9Sstevel@tonic-gate 13787c478bd9Sstevel@tonic-gate (void) updwtmpx(WTMPX_FILE, &wtmpx); 13797c478bd9Sstevel@tonic-gate sync(); 13807c478bd9Sstevel@tonic-gate } 13817c478bd9Sstevel@tonic-gate 13827c478bd9Sstevel@tonic-gate if (cmd == A_DUMP && nosync != 0) 13837c478bd9Sstevel@tonic-gate (void) uadmin(A_DUMP, AD_NOSYNC, NULL); 13847c478bd9Sstevel@tonic-gate 1385*19397407SSherry Moore if (fast_reboot) { 1386*19397407SSherry Moore if (failsafe) 1387*19397407SSherry Moore (void) fprintf(stderr, "Fast reboot - failsafe.\n"); 1388*19397407SSherry Moore else 1389*19397407SSherry Moore (void) fprintf(stderr, "Fast reboot.\n"); 1390*19397407SSherry Moore 1391*19397407SSherry Moore fcn = AD_FASTREBOOT; 1392*19397407SSherry Moore } 1393*19397407SSherry Moore 13943f2f09c1Sdp if (uadmin(cmd, fcn, mdep) == -1) 13953f2f09c1Sdp (void) fprintf(stderr, "%s: uadmin failed: %s\n", 13963f2f09c1Sdp cmdname, strerror(errno)); 13973f2f09c1Sdp else 13983f2f09c1Sdp (void) fprintf(stderr, "%s: uadmin unexpectedly returned 0\n", 13993f2f09c1Sdp cmdname); 14003f2f09c1Sdp 14013f2f09c1Sdp do { 14027c478bd9Sstevel@tonic-gate r = remove(resetting); 14033f2f09c1Sdp } while (r != 0 && errno == EINTR); 14043f2f09c1Sdp 14057c478bd9Sstevel@tonic-gate if (r != 0 && errno != ENOENT) 14067c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s: could not remove %s.\n"), 14077c478bd9Sstevel@tonic-gate cmdname, resetting); 14087c478bd9Sstevel@tonic-gate 14093f2f09c1Sdp if (direct_init(PCRUN) == -1) { 14103f2f09c1Sdp /* 14113f2f09c1Sdp * TRANSLATION_NOTE 14123f2f09c1Sdp * Don't translate the word "init" 14133f2f09c1Sdp */ 14143f2f09c1Sdp (void) fprintf(stderr, 14153f2f09c1Sdp gettext("%s: can't resume init\n"), cmdname); 14163f2f09c1Sdp } 14173f2f09c1Sdp 14187c478bd9Sstevel@tonic-gate continue_restarters(); 14197c478bd9Sstevel@tonic-gate 14203f2f09c1Sdp if (get_initpid() != -1) 14217c478bd9Sstevel@tonic-gate /* tell init to restate current level */ 14223f2f09c1Sdp (void) kill(get_initpid(), SIGHUP); 14237c478bd9Sstevel@tonic-gate 14247c478bd9Sstevel@tonic-gate fail: 14257c478bd9Sstevel@tonic-gate if (fcn == AD_BOOT) 14267c478bd9Sstevel@tonic-gate (void) audit_reboot_fail(); 14277c478bd9Sstevel@tonic-gate else 14287c478bd9Sstevel@tonic-gate (void) audit_halt_fail(); 14297c478bd9Sstevel@tonic-gate 1430*19397407SSherry Moore if (fast_reboot) { 1431*19397407SSherry Moore if (bename) { 1432*19397407SSherry Moore char cmdbuf[MAXPATHLEN]; 1433*19397407SSherry Moore 1434*19397407SSherry Moore (void) snprintf(cmdbuf, sizeof (cmdbuf), 1435*19397407SSherry Moore "/usr/sbin/luumount %s > /dev/null 2>&1", bename); 1436*19397407SSherry Moore (void) system(cmdbuf); 1437*19397407SSherry Moore 1438*19397407SSherry Moore } else if (strlen(fastboot_mounted) != 0) { 1439*19397407SSherry Moore (void) umount(fastboot_mounted); 1440*19397407SSherry Moore } 1441*19397407SSherry Moore } 1442*19397407SSherry Moore 14437c478bd9Sstevel@tonic-gate return (1); 14447c478bd9Sstevel@tonic-gate } 1445