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 /*
22e557d412SChristopher Kiick * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
25bc2cf0f5SJoshua M. Clulow /*
26bc2cf0f5SJoshua M. Clulow * Copyright (c) 2013, Joyent, Inc. All rights reserved.
279adfa60dSMatthew Ahrens * Copyright (c) 2015 by Delphix. All rights reserved.
2875383e32SAndy Fiddaman * Copyright 2016 Toomas Soome <tsoome@me.com>
2975383e32SAndy Fiddaman * Copyright 2018 OmniOS Community Edition (OmniOSce) Association.
30bc2cf0f5SJoshua M. Clulow */
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
33753a6d45SSherry Moore /* All Rights Reserved */
347c478bd9Sstevel@tonic-gate
357c478bd9Sstevel@tonic-gate /*
367c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
377c478bd9Sstevel@tonic-gate * The Regents of the University of California
387c478bd9Sstevel@tonic-gate * All Rights Reserved
397c478bd9Sstevel@tonic-gate *
407c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
417c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its
427c478bd9Sstevel@tonic-gate * contributors.
438e07a41fSKonstantin Ananyev * Portions contributed by Juergen Keil, <jk@tools.de>.
447c478bd9Sstevel@tonic-gate */
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate /*
48*bbf21555SRichard Lowe * Common code for halt(8), poweroff(8), and reboot(8). We use
497c478bd9Sstevel@tonic-gate * argv[0] to determine which behavior to exhibit.
507c478bd9Sstevel@tonic-gate */
517c478bd9Sstevel@tonic-gate
5219397407SSherry Moore #include <stdio.h>
533f2f09c1Sdp #include <procfs.h>
547c478bd9Sstevel@tonic-gate #include <sys/types.h>
5519397407SSherry Moore #include <sys/elf.h>
5619397407SSherry Moore #include <sys/systeminfo.h>
5719397407SSherry Moore #include <sys/stat.h>
587c478bd9Sstevel@tonic-gate #include <sys/uadmin.h>
5919397407SSherry Moore #include <sys/mntent.h>
6019397407SSherry Moore #include <sys/mnttab.h>
6119397407SSherry Moore #include <sys/mount.h>
628e07a41fSKonstantin Ananyev #include <sys/fs/ufs_mount.h>
637c478bd9Sstevel@tonic-gate #include <alloca.h>
647c478bd9Sstevel@tonic-gate #include <assert.h>
657c478bd9Sstevel@tonic-gate #include <errno.h>
667c478bd9Sstevel@tonic-gate #include <fcntl.h>
677c478bd9Sstevel@tonic-gate #include <libgen.h>
687c478bd9Sstevel@tonic-gate #include <libscf.h>
69753a6d45SSherry Moore #include <libscf_priv.h>
7019397407SSherry Moore #include <limits.h>
717c478bd9Sstevel@tonic-gate #include <locale.h>
727c478bd9Sstevel@tonic-gate #include <libintl.h>
737c478bd9Sstevel@tonic-gate #include <syslog.h>
747c478bd9Sstevel@tonic-gate #include <signal.h>
757c478bd9Sstevel@tonic-gate #include <strings.h>
767c478bd9Sstevel@tonic-gate #include <unistd.h>
777c478bd9Sstevel@tonic-gate #include <stdlib.h>
787c478bd9Sstevel@tonic-gate #include <stdio.h>
797c478bd9Sstevel@tonic-gate #include <strings.h>
807c478bd9Sstevel@tonic-gate #include <time.h>
814e1f1c13SKonstantin Ananyev #include <wait.h>
824e1f1c13SKonstantin Ananyev #include <ctype.h>
837c478bd9Sstevel@tonic-gate #include <utmpx.h>
847c478bd9Sstevel@tonic-gate #include <pwd.h>
857c478bd9Sstevel@tonic-gate #include <zone.h>
863a0fa635SEnrico Perla - Sun Microsystems #include <spawn.h>
8719397407SSherry Moore
8819397407SSherry Moore #include <libzfs.h>
899abc7a57SToomas Soome #if defined(__x86)
909abc7a57SToomas Soome #include <libbe.h>
91753a6d45SSherry Moore #endif
9219397407SSherry Moore
937c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
947c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
957c478bd9Sstevel@tonic-gate #endif
967c478bd9Sstevel@tonic-gate
9719397407SSherry Moore #if defined(__sparc)
9819397407SSherry Moore #define CUR_ELFDATA ELFDATA2MSB
999abc7a57SToomas Soome #elif defined(__x86)
10019397407SSherry Moore #define CUR_ELFDATA ELFDATA2LSB
10119397407SSherry Moore #endif
10219397407SSherry Moore
10319397407SSherry Moore static libzfs_handle_t *g_zfs;
10419397407SSherry Moore
1057c478bd9Sstevel@tonic-gate extern int audit_halt_setup(int, char **);
1067c478bd9Sstevel@tonic-gate extern int audit_halt_success(void);
1077c478bd9Sstevel@tonic-gate extern int audit_halt_fail(void);
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate extern int audit_reboot_setup(void);
1107c478bd9Sstevel@tonic-gate extern int audit_reboot_success(void);
1117c478bd9Sstevel@tonic-gate extern int audit_reboot_fail(void);
1127c478bd9Sstevel@tonic-gate
1133f2f09c1Sdp static char *cmdname; /* basename(argv[0]), the name of the command */
1143f2f09c1Sdp
1157c478bd9Sstevel@tonic-gate typedef struct ctidlist_struct {
1167c478bd9Sstevel@tonic-gate ctid_t ctid;
1177c478bd9Sstevel@tonic-gate struct ctidlist_struct *next;
1187c478bd9Sstevel@tonic-gate } ctidlist_t;
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate static ctidlist_t *ctidlist = NULL;
1217c478bd9Sstevel@tonic-gate static ctid_t startdct = -1;
1227c478bd9Sstevel@tonic-gate
1237c478bd9Sstevel@tonic-gate #define FMRI_STARTD_CONTRACT \
1247c478bd9Sstevel@tonic-gate "svc:/system/svc/restarter:default/:properties/restarter/contract"
1257c478bd9Sstevel@tonic-gate
12680cb75f4SWilliam Kucharski #define BEADM_PROG "/usr/sbin/beadm"
127753a6d45SSherry Moore #define BOOTADM_PROG "/sbin/bootadm"
12880cb75f4SWilliam Kucharski #define ZONEADM_PROG "/usr/sbin/zoneadm"
12980cb75f4SWilliam Kucharski
13019397407SSherry Moore /*
13119397407SSherry Moore * The length of FASTBOOT_MOUNTPOINT must be less than MAXPATHLEN.
13219397407SSherry Moore */
13319397407SSherry Moore #define FASTBOOT_MOUNTPOINT "/tmp/.fastboot.root"
13419397407SSherry Moore
135753a6d45SSherry Moore /*
136753a6d45SSherry Moore * Fast Reboot related variables
137753a6d45SSherry Moore */
13819397407SSherry Moore static char fastboot_mounted[MAXPATHLEN];
139e557d412SChristopher Kiick
1409abc7a57SToomas Soome #if defined(__x86)
1419abc7a57SToomas Soome static char *fbarg;
1429abc7a57SToomas Soome static char *fbarg_used;
1439abc7a57SToomas Soome static int fbarg_entnum = BE_ENTRY_DEFAULT;
1449abc7a57SToomas Soome #endif /* __x86 */
14519397407SSherry Moore
14619397407SSherry Moore static int validate_ufs_disk(char *, char *);
14719397407SSherry Moore static int validate_zfs_pool(char *, char *);
14819397407SSherry Moore
1493f2f09c1Sdp static pid_t
get_initpid()1503f2f09c1Sdp get_initpid()
1513f2f09c1Sdp {
1523f2f09c1Sdp static int init_pid = -1;
1533f2f09c1Sdp
1543f2f09c1Sdp if (init_pid == -1) {
1553f2f09c1Sdp if (zone_getattr(getzoneid(), ZONE_ATTR_INITPID, &init_pid,
1563f2f09c1Sdp sizeof (init_pid)) != sizeof (init_pid)) {
1573f2f09c1Sdp assert(errno == ESRCH);
1583f2f09c1Sdp init_pid = -1;
1593f2f09c1Sdp }
1603f2f09c1Sdp }
1613f2f09c1Sdp return (init_pid);
1623f2f09c1Sdp }
1633f2f09c1Sdp
1643f2f09c1Sdp /*
1653f2f09c1Sdp * Quiesce or resume init using /proc. When stopping init, we can't send
1663f2f09c1Sdp * SIGTSTP (since init ignores it) or SIGSTOP (since the kernel won't permit
1673f2f09c1Sdp * it).
1683f2f09c1Sdp */
1693f2f09c1Sdp static int
direct_init(long command)1703f2f09c1Sdp direct_init(long command)
1713f2f09c1Sdp {
1723f2f09c1Sdp char ctlfile[MAXPATHLEN];
1733f2f09c1Sdp pid_t pid;
1743f2f09c1Sdp int ctlfd;
1753f2f09c1Sdp
1763f2f09c1Sdp assert(command == PCDSTOP || command == PCRUN);
1773f2f09c1Sdp if ((pid = get_initpid()) == -1) {
1783f2f09c1Sdp return (-1);
1793f2f09c1Sdp }
1803f2f09c1Sdp
1813f2f09c1Sdp (void) snprintf(ctlfile, sizeof (ctlfile), "/proc/%d/ctl", pid);
1823f2f09c1Sdp if ((ctlfd = open(ctlfile, O_WRONLY)) == -1)
1833f2f09c1Sdp return (-1);
1843f2f09c1Sdp
1853f2f09c1Sdp if (command == PCDSTOP) {
1863f2f09c1Sdp if (write(ctlfd, &command, sizeof (long)) == -1) {
1873f2f09c1Sdp (void) close(ctlfd);
1883f2f09c1Sdp return (-1);
1893f2f09c1Sdp }
1903f2f09c1Sdp } else { /* command == PCRUN */
1913f2f09c1Sdp long cmds[2];
1923f2f09c1Sdp cmds[0] = command;
1933f2f09c1Sdp cmds[1] = 0;
1943f2f09c1Sdp if (write(ctlfd, cmds, sizeof (cmds)) == -1) {
1953f2f09c1Sdp (void) close(ctlfd);
1963f2f09c1Sdp return (-1);
1973f2f09c1Sdp }
1983f2f09c1Sdp }
1993f2f09c1Sdp (void) close(ctlfd);
2003f2f09c1Sdp return (0);
2013f2f09c1Sdp }
2023f2f09c1Sdp
2037c478bd9Sstevel@tonic-gate static void
stop_startd()2047c478bd9Sstevel@tonic-gate stop_startd()
2057c478bd9Sstevel@tonic-gate {
2067c478bd9Sstevel@tonic-gate scf_handle_t *h;
2077c478bd9Sstevel@tonic-gate scf_property_t *prop = NULL;
2087c478bd9Sstevel@tonic-gate scf_value_t *val = NULL;
2097c478bd9Sstevel@tonic-gate uint64_t uint64;
2107c478bd9Sstevel@tonic-gate
2113f2f09c1Sdp if ((h = scf_handle_create(SCF_VERSION)) == NULL)
2127c478bd9Sstevel@tonic-gate return;
2137c478bd9Sstevel@tonic-gate
2143f2f09c1Sdp if ((scf_handle_bind(h) != 0) ||
2153f2f09c1Sdp ((prop = scf_property_create(h)) == NULL) ||
2163f2f09c1Sdp ((val = scf_value_create(h)) == NULL))
2177c478bd9Sstevel@tonic-gate goto out;
2187c478bd9Sstevel@tonic-gate
2193f2f09c1Sdp if (scf_handle_decode_fmri(h, FMRI_STARTD_CONTRACT,
2203f2f09c1Sdp NULL, NULL, NULL, NULL, prop, SCF_DECODE_FMRI_EXACT) != 0)
2217c478bd9Sstevel@tonic-gate goto out;
2227c478bd9Sstevel@tonic-gate
2233f2f09c1Sdp if (scf_property_is_type(prop, SCF_TYPE_COUNT) != 0 ||
2243f2f09c1Sdp scf_property_get_value(prop, val) != 0 ||
2253f2f09c1Sdp scf_value_get_count(val, &uint64) != 0)
2267c478bd9Sstevel@tonic-gate goto out;
2277c478bd9Sstevel@tonic-gate
2283f2f09c1Sdp startdct = (ctid_t)uint64;
2293f2f09c1Sdp (void) sigsend(P_CTID, startdct, SIGSTOP);
2307c478bd9Sstevel@tonic-gate
2317c478bd9Sstevel@tonic-gate out:
2323f2f09c1Sdp scf_property_destroy(prop);
2333f2f09c1Sdp scf_value_destroy(val);
2347c478bd9Sstevel@tonic-gate scf_handle_destroy(h);
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate
2377c478bd9Sstevel@tonic-gate static void
continue_startd()2387c478bd9Sstevel@tonic-gate continue_startd()
2397c478bd9Sstevel@tonic-gate {
2407c478bd9Sstevel@tonic-gate if (startdct != -1)
2417c478bd9Sstevel@tonic-gate (void) sigsend(P_CTID, startdct, SIGCONT);
2427c478bd9Sstevel@tonic-gate }
2437c478bd9Sstevel@tonic-gate
2447c478bd9Sstevel@tonic-gate #define FMRI_RESTARTER_PROP "/:properties/general/restarter"
2457c478bd9Sstevel@tonic-gate #define FMRI_CONTRACT_PROP "/:properties/restarter/contract"
2467c478bd9Sstevel@tonic-gate
2477c478bd9Sstevel@tonic-gate static int
save_ctid(ctid_t ctid)2487c478bd9Sstevel@tonic-gate save_ctid(ctid_t ctid)
2497c478bd9Sstevel@tonic-gate {
2507c478bd9Sstevel@tonic-gate ctidlist_t *next;
2517c478bd9Sstevel@tonic-gate
2527c478bd9Sstevel@tonic-gate for (next = ctidlist; next != NULL; next = next->next)
2537c478bd9Sstevel@tonic-gate if (next->ctid == ctid)
2547c478bd9Sstevel@tonic-gate return (-1);
2557c478bd9Sstevel@tonic-gate
2567c478bd9Sstevel@tonic-gate next = (ctidlist_t *)malloc(sizeof (ctidlist_t));
2577c478bd9Sstevel@tonic-gate if (next == NULL)
2587c478bd9Sstevel@tonic-gate return (-1);
2597c478bd9Sstevel@tonic-gate
2607c478bd9Sstevel@tonic-gate next->ctid = ctid;
2617c478bd9Sstevel@tonic-gate next->next = ctidlist;
2627c478bd9Sstevel@tonic-gate ctidlist = next;
2637c478bd9Sstevel@tonic-gate return (0);
2647c478bd9Sstevel@tonic-gate }
2657c478bd9Sstevel@tonic-gate
2667c478bd9Sstevel@tonic-gate static void
stop_delegates()2677c478bd9Sstevel@tonic-gate stop_delegates()
2687c478bd9Sstevel@tonic-gate {
2697c478bd9Sstevel@tonic-gate ctid_t ctid;
2707c478bd9Sstevel@tonic-gate scf_handle_t *h;
2717c478bd9Sstevel@tonic-gate scf_scope_t *sc = NULL;
2727c478bd9Sstevel@tonic-gate scf_service_t *svc = NULL;
2737c478bd9Sstevel@tonic-gate scf_instance_t *inst = NULL;
2747c478bd9Sstevel@tonic-gate scf_snapshot_t *snap = NULL;
2757c478bd9Sstevel@tonic-gate scf_snapshot_t *isnap = NULL;
2767c478bd9Sstevel@tonic-gate scf_propertygroup_t *pg = NULL;
2777c478bd9Sstevel@tonic-gate scf_property_t *prop = NULL;
2787c478bd9Sstevel@tonic-gate scf_value_t *val = NULL;
2797c478bd9Sstevel@tonic-gate scf_iter_t *siter = NULL;
2807c478bd9Sstevel@tonic-gate scf_iter_t *iiter = NULL;
2817c478bd9Sstevel@tonic-gate char *fmri;
2827c478bd9Sstevel@tonic-gate ssize_t length;
2837c478bd9Sstevel@tonic-gate
2847c478bd9Sstevel@tonic-gate uint64_t uint64;
2857c478bd9Sstevel@tonic-gate ssize_t bytes;
2867c478bd9Sstevel@tonic-gate
2877c478bd9Sstevel@tonic-gate length = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH);
2887c478bd9Sstevel@tonic-gate if (length <= 0)
2897c478bd9Sstevel@tonic-gate return;
2907c478bd9Sstevel@tonic-gate
2917c478bd9Sstevel@tonic-gate length++;
2927c478bd9Sstevel@tonic-gate fmri = alloca(length * sizeof (char));
2937c478bd9Sstevel@tonic-gate
2943f2f09c1Sdp if ((h = scf_handle_create(SCF_VERSION)) == NULL)
2957c478bd9Sstevel@tonic-gate return;
2967c478bd9Sstevel@tonic-gate
2973f2f09c1Sdp if (scf_handle_bind(h) != 0) {
2987c478bd9Sstevel@tonic-gate scf_handle_destroy(h);
2997c478bd9Sstevel@tonic-gate return;
3007c478bd9Sstevel@tonic-gate }
3017c478bd9Sstevel@tonic-gate
3023f2f09c1Sdp if ((sc = scf_scope_create(h)) == NULL ||
3033f2f09c1Sdp (svc = scf_service_create(h)) == NULL ||
3043f2f09c1Sdp (inst = scf_instance_create(h)) == NULL ||
3053f2f09c1Sdp (snap = scf_snapshot_create(h)) == NULL ||
3063f2f09c1Sdp (pg = scf_pg_create(h)) == NULL ||
3073f2f09c1Sdp (prop = scf_property_create(h)) == NULL ||
3083f2f09c1Sdp (val = scf_value_create(h)) == NULL ||
3093f2f09c1Sdp (siter = scf_iter_create(h)) == NULL ||
3103f2f09c1Sdp (iiter = scf_iter_create(h)) == NULL)
3117c478bd9Sstevel@tonic-gate goto out;
3127c478bd9Sstevel@tonic-gate
3133f2f09c1Sdp if (scf_handle_get_scope(h, SCF_SCOPE_LOCAL, sc) != 0)
3147c478bd9Sstevel@tonic-gate goto out;
3157c478bd9Sstevel@tonic-gate
3163f2f09c1Sdp if (scf_iter_scope_services(siter, sc) != 0)
3177c478bd9Sstevel@tonic-gate goto out;
3187c478bd9Sstevel@tonic-gate
3197c478bd9Sstevel@tonic-gate while (scf_iter_next_service(siter, svc) == 1) {
3207c478bd9Sstevel@tonic-gate
3213f2f09c1Sdp if (scf_iter_service_instances(iiter, svc) != 0)
3227c478bd9Sstevel@tonic-gate continue;
3237c478bd9Sstevel@tonic-gate
3247c478bd9Sstevel@tonic-gate while (scf_iter_next_instance(iiter, inst) == 1) {
3257c478bd9Sstevel@tonic-gate
3263f2f09c1Sdp if ((scf_instance_get_snapshot(inst, "running",
3273f2f09c1Sdp snap)) != 0)
3283f2f09c1Sdp isnap = NULL;
3293f2f09c1Sdp else
3303f2f09c1Sdp isnap = snap;
3317c478bd9Sstevel@tonic-gate
3323f2f09c1Sdp if (scf_instance_get_pg_composed(inst, isnap,
3333f2f09c1Sdp SCF_PG_GENERAL, pg) != 0)
3347c478bd9Sstevel@tonic-gate continue;
3357c478bd9Sstevel@tonic-gate
3363f2f09c1Sdp if (scf_pg_get_property(pg, SCF_PROPERTY_RESTARTER,
3373f2f09c1Sdp prop) != 0 ||
3383f2f09c1Sdp scf_property_get_value(prop, val) != 0)
3397c478bd9Sstevel@tonic-gate continue;
3407c478bd9Sstevel@tonic-gate
3417c478bd9Sstevel@tonic-gate bytes = scf_value_get_astring(val, fmri, length);
3427c478bd9Sstevel@tonic-gate if (bytes <= 0 || bytes >= length)
3437c478bd9Sstevel@tonic-gate continue;
3447c478bd9Sstevel@tonic-gate
3457c478bd9Sstevel@tonic-gate if (strlcat(fmri, FMRI_CONTRACT_PROP, length) >=
3467c478bd9Sstevel@tonic-gate length)
3477c478bd9Sstevel@tonic-gate continue;
3487c478bd9Sstevel@tonic-gate
3493f2f09c1Sdp if (scf_handle_decode_fmri(h, fmri, NULL, NULL,
3503f2f09c1Sdp NULL, NULL, prop, SCF_DECODE_FMRI_EXACT) != 0)
3517c478bd9Sstevel@tonic-gate continue;
3527c478bd9Sstevel@tonic-gate
3533f2f09c1Sdp if (scf_property_is_type(prop, SCF_TYPE_COUNT) != 0 ||
3543f2f09c1Sdp scf_property_get_value(prop, val) != 0 ||
3553f2f09c1Sdp scf_value_get_count(val, &uint64) != 0)
3567c478bd9Sstevel@tonic-gate continue;
3577c478bd9Sstevel@tonic-gate
3587c478bd9Sstevel@tonic-gate ctid = (ctid_t)uint64;
3597c478bd9Sstevel@tonic-gate if (save_ctid(ctid) == 0) {
3607c478bd9Sstevel@tonic-gate (void) sigsend(P_CTID, ctid, SIGSTOP);
3617c478bd9Sstevel@tonic-gate }
3627c478bd9Sstevel@tonic-gate }
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate out:
3653f2f09c1Sdp scf_scope_destroy(sc);
3663f2f09c1Sdp scf_service_destroy(svc);
3673f2f09c1Sdp scf_instance_destroy(inst);
3683f2f09c1Sdp scf_snapshot_destroy(snap);
3693f2f09c1Sdp scf_pg_destroy(pg);
3703f2f09c1Sdp scf_property_destroy(prop);
3713f2f09c1Sdp scf_value_destroy(val);
3723f2f09c1Sdp scf_iter_destroy(siter);
3733f2f09c1Sdp scf_iter_destroy(iiter);
3747c478bd9Sstevel@tonic-gate
3757c478bd9Sstevel@tonic-gate (void) scf_handle_unbind(h);
3767c478bd9Sstevel@tonic-gate scf_handle_destroy(h);
3777c478bd9Sstevel@tonic-gate }
3787c478bd9Sstevel@tonic-gate
3797c478bd9Sstevel@tonic-gate static void
continue_delegates()3807c478bd9Sstevel@tonic-gate continue_delegates()
3817c478bd9Sstevel@tonic-gate {
3827c478bd9Sstevel@tonic-gate ctidlist_t *next;
3837c478bd9Sstevel@tonic-gate for (next = ctidlist; next != NULL; next = next->next)
3847c478bd9Sstevel@tonic-gate (void) sigsend(P_CTID, next->ctid, SIGCONT);
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate
3879db7147eSSherry Moore #define FMRI_GDM "svc:/application/graphical-login/gdm:default"
3883b860eeeSSherry Moore #define GDM_STOP_TIMEOUT 10 /* Give gdm 10 seconds to shut down */
3899db7147eSSherry Moore
3909db7147eSSherry Moore /*
3919db7147eSSherry Moore * If gdm is running, try to stop gdm.
3929db7147eSSherry Moore * Returns 0 on success, -1 on failure.
3939db7147eSSherry Moore */
3949db7147eSSherry Moore static int
stop_gdm()3959db7147eSSherry Moore stop_gdm()
3969db7147eSSherry Moore {
3979db7147eSSherry Moore char *gdm_state = NULL;
3989db7147eSSherry Moore int retry = 0;
3999db7147eSSherry Moore
4009db7147eSSherry Moore /*
4019db7147eSSherry Moore * If gdm is running, try to stop gdm.
4029db7147eSSherry Moore */
4039db7147eSSherry Moore while ((gdm_state = smf_get_state(FMRI_GDM)) != NULL &&
4043b860eeeSSherry Moore strcmp(gdm_state, SCF_STATE_STRING_ONLINE) == 0 &&
4053b860eeeSSherry Moore retry++ < GDM_STOP_TIMEOUT) {
4063b860eeeSSherry Moore
4073b860eeeSSherry Moore free(gdm_state);
4083b860eeeSSherry Moore
4093b860eeeSSherry Moore /*
4103b860eeeSSherry Moore * Only need to disable once.
4113b860eeeSSherry Moore */
4123b860eeeSSherry Moore if (retry == 1 &&
4133b860eeeSSherry Moore smf_disable_instance(FMRI_GDM, SMF_TEMPORARY) != 0) {
4149db7147eSSherry Moore (void) fprintf(stderr,
4159db7147eSSherry Moore gettext("%s: Failed to stop %s: %s.\n"),
4169db7147eSSherry Moore cmdname, FMRI_GDM, scf_strerror(scf_error()));
4179db7147eSSherry Moore return (-1);
4189db7147eSSherry Moore }
4199db7147eSSherry Moore (void) sleep(1);
4209db7147eSSherry Moore }
4219db7147eSSherry Moore
4223b860eeeSSherry Moore if (retry >= GDM_STOP_TIMEOUT) {
4239db7147eSSherry Moore (void) fprintf(stderr, gettext("%s: Failed to stop %s.\n"),
4249db7147eSSherry Moore cmdname, FMRI_GDM);
4259db7147eSSherry Moore return (-1);
4269db7147eSSherry Moore }
4279db7147eSSherry Moore
4289db7147eSSherry Moore return (0);
4299db7147eSSherry Moore }
4309db7147eSSherry Moore
4319db7147eSSherry Moore
4327c478bd9Sstevel@tonic-gate static void
stop_restarters()4337c478bd9Sstevel@tonic-gate stop_restarters()
4347c478bd9Sstevel@tonic-gate {
4357c478bd9Sstevel@tonic-gate stop_startd();
4367c478bd9Sstevel@tonic-gate stop_delegates();
4377c478bd9Sstevel@tonic-gate }
4387c478bd9Sstevel@tonic-gate
4397c478bd9Sstevel@tonic-gate static void
continue_restarters()4407c478bd9Sstevel@tonic-gate continue_restarters()
4417c478bd9Sstevel@tonic-gate {
4427c478bd9Sstevel@tonic-gate continue_startd();
4437c478bd9Sstevel@tonic-gate continue_delegates();
4447c478bd9Sstevel@tonic-gate }
4457c478bd9Sstevel@tonic-gate
4467c478bd9Sstevel@tonic-gate /*
4477c478bd9Sstevel@tonic-gate * Copy an array of strings into buf, separated by spaces. Returns 0 on
4487c478bd9Sstevel@tonic-gate * success.
4497c478bd9Sstevel@tonic-gate */
4507c478bd9Sstevel@tonic-gate static int
gather_args(char ** args,char * buf,size_t buf_sz)4517c478bd9Sstevel@tonic-gate gather_args(char **args, char *buf, size_t buf_sz)
4527c478bd9Sstevel@tonic-gate {
4537c478bd9Sstevel@tonic-gate if (strlcpy(buf, *args, buf_sz) >= buf_sz)
4547c478bd9Sstevel@tonic-gate return (-1);
4557c478bd9Sstevel@tonic-gate
4567c478bd9Sstevel@tonic-gate for (++args; *args != NULL; ++args) {
4577c478bd9Sstevel@tonic-gate if (strlcat(buf, " ", buf_sz) >= buf_sz)
4587c478bd9Sstevel@tonic-gate return (-1);
4597c478bd9Sstevel@tonic-gate if (strlcat(buf, *args, buf_sz) >= buf_sz)
4607c478bd9Sstevel@tonic-gate return (-1);
4617c478bd9Sstevel@tonic-gate }
4627c478bd9Sstevel@tonic-gate
4637c478bd9Sstevel@tonic-gate return (0);
4647c478bd9Sstevel@tonic-gate }
4657c478bd9Sstevel@tonic-gate
46626f665e8Sdstaff /*
46726f665e8Sdstaff * Halt every zone on the system. We are committed to doing a shutdown
46826f665e8Sdstaff * even if something goes wrong here. If something goes wrong, we just
46926f665e8Sdstaff * continue with the shutdown. Return non-zero if we need to wait for zones to
47026f665e8Sdstaff * halt later on.
47126f665e8Sdstaff */
47226f665e8Sdstaff static int
halt_zones()4733f2f09c1Sdp halt_zones()
47426f665e8Sdstaff {
47526f665e8Sdstaff pid_t pid;
47626f665e8Sdstaff zoneid_t *zones;
4773f2f09c1Sdp size_t nz = 0, old_nz;
47826f665e8Sdstaff int i;
47926f665e8Sdstaff char zname[ZONENAME_MAX];
48026f665e8Sdstaff
48126f665e8Sdstaff /*
48226f665e8Sdstaff * Get a list of zones. If the number of zones changes in between the
48326f665e8Sdstaff * two zone_list calls, try again.
48426f665e8Sdstaff */
48526f665e8Sdstaff
48626f665e8Sdstaff for (;;) {
48726f665e8Sdstaff (void) zone_list(NULL, &nz);
48826f665e8Sdstaff if (nz == 1)
48926f665e8Sdstaff return (0);
49026f665e8Sdstaff old_nz = nz;
49126f665e8Sdstaff zones = calloc(sizeof (zoneid_t), nz);
49226f665e8Sdstaff if (zones == NULL) {
49326f665e8Sdstaff (void) fprintf(stderr,
49426f665e8Sdstaff gettext("%s: Could not halt zones"
4953f2f09c1Sdp " (out of memory).\n"), cmdname);
49626f665e8Sdstaff return (0);
49726f665e8Sdstaff }
49826f665e8Sdstaff
49926f665e8Sdstaff (void) zone_list(zones, &nz);
50026f665e8Sdstaff if (old_nz == nz)
50126f665e8Sdstaff break;
50226f665e8Sdstaff free(zones);
50326f665e8Sdstaff }
50426f665e8Sdstaff
50526f665e8Sdstaff if (nz == 2) {
5063f2f09c1Sdp (void) fprintf(stderr, gettext("%s: Halting 1 zone.\n"),
5073f2f09c1Sdp cmdname);
50826f665e8Sdstaff } else {
5093f2f09c1Sdp (void) fprintf(stderr, gettext("%s: Halting %i zones.\n"),
5103f2f09c1Sdp cmdname, nz - 1);
51126f665e8Sdstaff }
51226f665e8Sdstaff
51326f665e8Sdstaff for (i = 0; i < nz; i++) {
51426f665e8Sdstaff if (zones[i] == GLOBAL_ZONEID)
51526f665e8Sdstaff continue;
51626f665e8Sdstaff if (getzonenamebyid(zones[i], zname, sizeof (zname)) < 0) {
51726f665e8Sdstaff /*
51826f665e8Sdstaff * getzonenamebyid should only fail if we raced with
51926f665e8Sdstaff * another process trying to shut down the zone.
52026f665e8Sdstaff * We assume this happened and ignore the error.
52126f665e8Sdstaff */
52226f665e8Sdstaff if (errno != EINVAL) {
52326f665e8Sdstaff (void) fprintf(stderr,
52426f665e8Sdstaff gettext("%s: Unexpected error while "
52526f665e8Sdstaff "looking up zone %ul: %s.\n"),
5263f2f09c1Sdp cmdname, zones[i], strerror(errno));
52726f665e8Sdstaff }
52826f665e8Sdstaff
52926f665e8Sdstaff continue;
53026f665e8Sdstaff }
53126f665e8Sdstaff pid = fork();
53226f665e8Sdstaff if (pid < 0) {
53326f665e8Sdstaff (void) fprintf(stderr,
53426f665e8Sdstaff gettext("%s: Zone \"%s\" could not be"
53526f665e8Sdstaff " halted (could not fork(): %s).\n"),
5363f2f09c1Sdp cmdname, zname, strerror(errno));
53726f665e8Sdstaff continue;
53826f665e8Sdstaff }
53926f665e8Sdstaff if (pid == 0) {
54026f665e8Sdstaff (void) execl(ZONEADM_PROG, ZONEADM_PROG,
54126f665e8Sdstaff "-z", zname, "halt", NULL);
54226f665e8Sdstaff (void) fprintf(stderr,
54326f665e8Sdstaff gettext("%s: Zone \"%s\" could not be halted"
54426f665e8Sdstaff " (cannot exec(" ZONEADM_PROG "): %s).\n"),
5453f2f09c1Sdp cmdname, zname, strerror(errno));
54626f665e8Sdstaff exit(0);
54726f665e8Sdstaff }
54826f665e8Sdstaff }
54926f665e8Sdstaff
55026f665e8Sdstaff return (1);
55126f665e8Sdstaff }
55226f665e8Sdstaff
55326f665e8Sdstaff /*
55426f665e8Sdstaff * This function tries to wait for all non-global zones to go away.
55526f665e8Sdstaff * It will timeout if no progress is made for 5 seconds, or a total of
55626f665e8Sdstaff * 30 seconds elapses.
55726f665e8Sdstaff */
55826f665e8Sdstaff
55926f665e8Sdstaff static void
check_zones_haltedness()5603f2f09c1Sdp check_zones_haltedness()
56126f665e8Sdstaff {
56226f665e8Sdstaff int t = 0, t_prog = 0;
56326f665e8Sdstaff size_t nz = 0, last_nz;
56426f665e8Sdstaff
56526f665e8Sdstaff do {
56626f665e8Sdstaff last_nz = nz;
56726f665e8Sdstaff (void) zone_list(NULL, &nz);
56826f665e8Sdstaff if (nz == 1)
56926f665e8Sdstaff return;
57026f665e8Sdstaff
57126f665e8Sdstaff (void) sleep(1);
57226f665e8Sdstaff
57326f665e8Sdstaff if (last_nz > nz)
57426f665e8Sdstaff t_prog = 0;
57526f665e8Sdstaff
57626f665e8Sdstaff t++;
57726f665e8Sdstaff t_prog++;
57826f665e8Sdstaff
57926f665e8Sdstaff if (t == 10) {
58026f665e8Sdstaff if (nz == 2) {
58126f665e8Sdstaff (void) fprintf(stderr,
58226f665e8Sdstaff gettext("%s: Still waiting for 1 zone to "
58326f665e8Sdstaff "halt. Will wait up to 20 seconds.\n"),
5843f2f09c1Sdp cmdname);
58526f665e8Sdstaff } else {
58626f665e8Sdstaff (void) fprintf(stderr,
58726f665e8Sdstaff gettext("%s: Still waiting for %i zones "
58826f665e8Sdstaff "to halt. Will wait up to 20 seconds.\n"),
5893f2f09c1Sdp cmdname, nz - 1);
59026f665e8Sdstaff }
59126f665e8Sdstaff }
59226f665e8Sdstaff
59326f665e8Sdstaff } while ((t < 30) && (t_prog < 5));
59426f665e8Sdstaff }
59526f665e8Sdstaff
59619397407SSherry Moore
59719397407SSherry Moore /*
59819397407SSherry Moore * Validate that this is a root disk or dataset
59919397407SSherry Moore * Returns 0 if it is a root disk or dataset;
60019397407SSherry Moore * returns 1 if it is a disk argument or dataset, but not valid or not root;
60119397407SSherry Moore * returns -1 if it is not a valid argument or a disk argument.
60219397407SSherry Moore */
60319397407SSherry Moore static int
validate_disk(char * arg,char * mountpoint)60419397407SSherry Moore validate_disk(char *arg, char *mountpoint)
60519397407SSherry Moore {
60619397407SSherry Moore static char root_dev_path[] = "/dev/dsk";
60719397407SSherry Moore char kernpath[MAXPATHLEN];
60819397407SSherry Moore struct stat64 statbuf;
60919397407SSherry Moore int rc = 0;
61019397407SSherry Moore
61119397407SSherry Moore if (strlen(arg) > MAXPATHLEN) {
61219397407SSherry Moore (void) fprintf(stderr,
613753a6d45SSherry Moore gettext("%s: Argument is too long\n"), cmdname);
61419397407SSherry Moore return (-1);
61519397407SSherry Moore }
61619397407SSherry Moore
61719397407SSherry Moore bcopy(FASTBOOT_MOUNTPOINT, mountpoint, sizeof (FASTBOOT_MOUNTPOINT));
61819397407SSherry Moore
619753a6d45SSherry Moore if (strstr(arg, mountpoint) == NULL) {
620753a6d45SSherry Moore /*
621753a6d45SSherry Moore * Do a force umount just in case some other filesystem has
622753a6d45SSherry Moore * been mounted there.
623753a6d45SSherry Moore */
624753a6d45SSherry Moore (void) umount2(mountpoint, MS_FORCE);
625753a6d45SSherry Moore }
62619397407SSherry Moore
62719397407SSherry Moore /* Create the directory if it doesn't already exist */
628753a6d45SSherry Moore if (lstat64(mountpoint, &statbuf) != 0) {
62919397407SSherry Moore if (mkdirp(mountpoint, 0755) != 0) {
63019397407SSherry Moore (void) fprintf(stderr,
631753a6d45SSherry Moore gettext("Failed to create mountpoint %s\n"),
63219397407SSherry Moore mountpoint);
63319397407SSherry Moore return (-1);
63419397407SSherry Moore }
63519397407SSherry Moore }
63619397407SSherry Moore
63719397407SSherry Moore if (strncmp(arg, root_dev_path, strlen(root_dev_path)) == 0) {
63819397407SSherry Moore /* ufs root disk argument */
63919397407SSherry Moore rc = validate_ufs_disk(arg, mountpoint);
64019397407SSherry Moore } else {
64119397407SSherry Moore /* zfs root pool argument */
64219397407SSherry Moore rc = validate_zfs_pool(arg, mountpoint);
64319397407SSherry Moore }
64419397407SSherry Moore
64519397407SSherry Moore if (rc != 0)
64619397407SSherry Moore return (rc);
64719397407SSherry Moore
6484e995f2aSMarcel Telka /*
6494e995f2aSMarcel Telka * Check for the usual case: 64-bit kernel
6504e995f2aSMarcel Telka */
6514e995f2aSMarcel Telka (void) snprintf(kernpath, MAXPATHLEN,
6524e995f2aSMarcel Telka "%s/platform/i86pc/kernel/amd64/unix", mountpoint);
6534e995f2aSMarcel Telka if (stat64(kernpath, &statbuf) == 0)
6544e995f2aSMarcel Telka return (0);
6554e995f2aSMarcel Telka
6564e995f2aSMarcel Telka /*
6574e995f2aSMarcel Telka * We no longer build 32-bit kernel but in a case we are trying to boot
6584e995f2aSMarcel Telka * some ancient filesystem with 32-bit only kernel we should be able to
6594e995f2aSMarcel Telka * proceed too
6604e995f2aSMarcel Telka */
66119397407SSherry Moore (void) snprintf(kernpath, MAXPATHLEN, "%s/platform/i86pc/kernel/unix",
66219397407SSherry Moore mountpoint);
66319397407SSherry Moore
66419397407SSherry Moore if (stat64(kernpath, &statbuf) != 0) {
66519397407SSherry Moore (void) fprintf(stderr,
66619397407SSherry Moore gettext("%s: %s is not a root disk or dataset\n"),
66719397407SSherry Moore cmdname, arg);
66819397407SSherry Moore return (1);
66919397407SSherry Moore }
67019397407SSherry Moore
67119397407SSherry Moore return (0);
67219397407SSherry Moore }
67319397407SSherry Moore
67419397407SSherry Moore
67519397407SSherry Moore static int
validate_ufs_disk(char * arg,char * mountpoint)67619397407SSherry Moore validate_ufs_disk(char *arg, char *mountpoint)
67719397407SSherry Moore {
6788e07a41fSKonstantin Ananyev struct ufs_args ufs_args = { 0 };
6798e07a41fSKonstantin Ananyev char mntopts[MNT_LINE_MAX] = MNTOPT_LARGEFILES;
68019397407SSherry Moore
68119397407SSherry Moore /* perform the mount */
6828e07a41fSKonstantin Ananyev ufs_args.flags = UFSMNT_LARGEFILES;
68319397407SSherry Moore if (mount(arg, mountpoint, MS_DATA|MS_OPTIONSTR,
6848e07a41fSKonstantin Ananyev MNTTYPE_UFS, &ufs_args, sizeof (ufs_args),
6858e07a41fSKonstantin Ananyev mntopts, sizeof (mntopts)) != 0) {
68619397407SSherry Moore perror(cmdname);
68719397407SSherry Moore (void) fprintf(stderr,
688753a6d45SSherry Moore gettext("%s: Failed to mount %s\n"), cmdname, arg);
68919397407SSherry Moore return (-1);
69019397407SSherry Moore }
69119397407SSherry Moore
69219397407SSherry Moore return (0);
69319397407SSherry Moore }
69419397407SSherry Moore
69519397407SSherry Moore static int
validate_zfs_pool(char * arg,char * mountpoint)69619397407SSherry Moore validate_zfs_pool(char *arg, char *mountpoint)
69719397407SSherry Moore {
69819397407SSherry Moore zfs_handle_t *zhp = NULL;
69919397407SSherry Moore char mntopts[MNT_LINE_MAX] = { '\0' };
70019397407SSherry Moore int rc = 0;
70119397407SSherry Moore
70219397407SSherry Moore if ((g_zfs = libzfs_init()) == NULL) {
703753a6d45SSherry Moore (void) fprintf(stderr, gettext("Internal error: failed to "
70419397407SSherry Moore "initialize ZFS library\n"));
70519397407SSherry Moore return (-1);
70619397407SSherry Moore }
70719397407SSherry Moore
70819397407SSherry Moore /* Try to open the dataset */
70919397407SSherry Moore if ((zhp = zfs_open(g_zfs, arg,
71019397407SSherry Moore ZFS_TYPE_FILESYSTEM | ZFS_TYPE_DATASET)) == NULL)
71119397407SSherry Moore return (-1);
71219397407SSherry Moore
71319397407SSherry Moore /* perform the mount */
714753a6d45SSherry Moore if (mount(zfs_get_name(zhp), mountpoint, MS_DATA|MS_OPTIONSTR|MS_RDONLY,
71519397407SSherry Moore MNTTYPE_ZFS, NULL, 0, mntopts, sizeof (mntopts)) != 0) {
71619397407SSherry Moore perror(cmdname);
71719397407SSherry Moore (void) fprintf(stderr,
718753a6d45SSherry Moore gettext("%s: Failed to mount %s\n"), cmdname, arg);
71919397407SSherry Moore rc = -1;
72019397407SSherry Moore }
72119397407SSherry Moore
72219397407SSherry Moore if (zhp != NULL)
72319397407SSherry Moore zfs_close(zhp);
72419397407SSherry Moore
72519397407SSherry Moore libzfs_fini(g_zfs);
72619397407SSherry Moore return (rc);
72719397407SSherry Moore }
72819397407SSherry Moore
72919397407SSherry Moore /*
73019397407SSherry Moore * Return 0 if not zfs, or is zfs and have successfully constructed the
73119397407SSherry Moore * boot argument; returns non-zero otherwise.
73219397407SSherry Moore * At successful completion fpth contains pointer where mount point ends.
73319397407SSherry Moore * NOTE: arg is supposed to be the resolved path
73419397407SSherry Moore */
73519397407SSherry Moore static int
get_zfs_bootfs_arg(const char * arg,const char ** fpth,int * is_zfs,char * bootfs_arg)73619397407SSherry Moore get_zfs_bootfs_arg(const char *arg, const char ** fpth, int *is_zfs,
7379adfa60dSMatthew Ahrens char *bootfs_arg)
73819397407SSherry Moore {
73919397407SSherry Moore zfs_handle_t *zhp = NULL;
74019397407SSherry Moore zpool_handle_t *zpoolp = NULL;
74119397407SSherry Moore FILE *mtabp = NULL;
74219397407SSherry Moore struct mnttab mnt;
74319397407SSherry Moore char *poolname = NULL;
744753a6d45SSherry Moore char physpath[MAXPATHLEN];
7459adfa60dSMatthew Ahrens char mntsp[ZFS_MAX_DATASET_NAME_LEN];
7469adfa60dSMatthew Ahrens char bootfs[ZFS_MAX_DATASET_NAME_LEN];
74719397407SSherry Moore int rc = 0;
74819397407SSherry Moore size_t mntlen = 0;
74919397407SSherry Moore size_t msz;
750753a6d45SSherry Moore static char fmt[] = "-B zfs-bootfs=%s,bootpath=\"%s\"";
75119397407SSherry Moore
75219397407SSherry Moore *fpth = arg;
75319397407SSherry Moore *is_zfs = 0;
75419397407SSherry Moore
75519397407SSherry Moore bzero(physpath, sizeof (physpath));
75619397407SSherry Moore bzero(bootfs, sizeof (bootfs));
75719397407SSherry Moore
75819397407SSherry Moore if ((mtabp = fopen(MNTTAB, "r")) == NULL) {
75919397407SSherry Moore return (-1);
76019397407SSherry Moore }
76119397407SSherry Moore
76219397407SSherry Moore while (getmntent(mtabp, &mnt) == 0) {
76319397407SSherry Moore if (strstr(arg, mnt.mnt_mountp) == arg &&
76419397407SSherry Moore (msz = strlen(mnt.mnt_mountp)) > mntlen) {
76519397407SSherry Moore mntlen = msz;
76619397407SSherry Moore *is_zfs = strcmp(MNTTYPE_ZFS, mnt.mnt_fstype) == 0;
76719397407SSherry Moore (void) strlcpy(mntsp, mnt.mnt_special, sizeof (mntsp));
76819397407SSherry Moore }
76919397407SSherry Moore }
77019397407SSherry Moore
77119397407SSherry Moore (void) fclose(mtabp);
77219397407SSherry Moore
77319397407SSherry Moore if (mntlen > 1)
77419397407SSherry Moore *fpth += mntlen;
77519397407SSherry Moore
77619397407SSherry Moore if (!*is_zfs)
77719397407SSherry Moore return (0);
77819397407SSherry Moore
77919397407SSherry Moore if ((g_zfs = libzfs_init()) == NULL)
78019397407SSherry Moore return (-1);
78119397407SSherry Moore
78219397407SSherry Moore /* Try to open the dataset */
78319397407SSherry Moore if ((zhp = zfs_open(g_zfs, mntsp,
78419397407SSherry Moore ZFS_TYPE_FILESYSTEM | ZFS_TYPE_DATASET)) == NULL) {
785753a6d45SSherry Moore (void) fprintf(stderr, gettext("Cannot open %s\n"), mntsp);
78619397407SSherry Moore rc = -1;
78719397407SSherry Moore goto validate_zfs_err_out;
78819397407SSherry Moore }
78919397407SSherry Moore
79019397407SSherry Moore (void) strlcpy(bootfs, mntsp, sizeof (bootfs));
79119397407SSherry Moore
79219397407SSherry Moore if ((poolname = strtok(mntsp, "/")) == NULL) {
79319397407SSherry Moore rc = -1;
79419397407SSherry Moore goto validate_zfs_err_out;
79519397407SSherry Moore }
79619397407SSherry Moore
79719397407SSherry Moore if ((zpoolp = zpool_open(g_zfs, poolname)) == NULL) {
798753a6d45SSherry Moore (void) fprintf(stderr, gettext("Cannot open %s\n"), poolname);
79919397407SSherry Moore rc = -1;
80019397407SSherry Moore goto validate_zfs_err_out;
80119397407SSherry Moore }
80219397407SSherry Moore
803753a6d45SSherry Moore if (zpool_get_physpath(zpoolp, physpath, sizeof (physpath)) != 0) {
804753a6d45SSherry Moore (void) fprintf(stderr, gettext("Cannot find phys_path\n"));
80519397407SSherry Moore rc = -1;
80619397407SSherry Moore goto validate_zfs_err_out;
80719397407SSherry Moore }
80819397407SSherry Moore
809753a6d45SSherry Moore /*
810753a6d45SSherry Moore * For the mirror physpath would contain the list of all
811753a6d45SSherry Moore * bootable devices, pick up the first one.
812753a6d45SSherry Moore */
813753a6d45SSherry Moore (void) strtok(physpath, " ");
814753a6d45SSherry Moore if (snprintf(bootfs_arg, BOOTARGS_MAX, fmt, bootfs, physpath) >=
815753a6d45SSherry Moore BOOTARGS_MAX) {
816753a6d45SSherry Moore rc = E2BIG;
817753a6d45SSherry Moore (void) fprintf(stderr,
818753a6d45SSherry Moore gettext("Boot arguments are too long\n"));
81919397407SSherry Moore }
82019397407SSherry Moore
82119397407SSherry Moore validate_zfs_err_out:
82219397407SSherry Moore if (zhp != NULL)
82319397407SSherry Moore zfs_close(zhp);
82419397407SSherry Moore
82519397407SSherry Moore if (zpoolp != NULL)
82619397407SSherry Moore zpool_close(zpoolp);
82719397407SSherry Moore
82819397407SSherry Moore libzfs_fini(g_zfs);
82919397407SSherry Moore return (rc);
83019397407SSherry Moore }
83119397407SSherry Moore
83219397407SSherry Moore /*
83319397407SSherry Moore * Validate that the file exists, and is an ELF file.
83419397407SSherry Moore * Returns 0 on success, -1 on failure.
83519397407SSherry Moore */
83619397407SSherry Moore static int
validate_unix(char * arg,int * mplen,int * is_zfs,char * bootfs_arg)8375b89e3f7SSherry Moore validate_unix(char *arg, int *mplen, int *is_zfs, char *bootfs_arg)
83819397407SSherry Moore {
83919397407SSherry Moore const char *location;
84019397407SSherry Moore int class, format;
84119397407SSherry Moore unsigned char ident[EI_NIDENT];
84219397407SSherry Moore char physpath[MAXPATHLEN];
84319397407SSherry Moore int elffd = -1;
84419397407SSherry Moore size_t sz;
84519397407SSherry Moore
84619397407SSherry Moore if ((sz = resolvepath(arg, physpath, sizeof (physpath) - 1)) ==
84719397407SSherry Moore (size_t)-1) {
84819397407SSherry Moore (void) fprintf(stderr,
849753a6d45SSherry Moore gettext("Cannot resolve path for %s: %s\n"),
85019397407SSherry Moore arg, strerror(errno));
85119397407SSherry Moore return (-1);
85219397407SSherry Moore }
85319397407SSherry Moore (void) strlcpy(arg, physpath, sz + 1);
85419397407SSherry Moore
85519397407SSherry Moore if (strlen(arg) > MAXPATHLEN) {
85619397407SSherry Moore (void) fprintf(stderr,
857753a6d45SSherry Moore gettext("%s: New kernel name is too long\n"), cmdname);
85819397407SSherry Moore return (-1);
85919397407SSherry Moore }
86019397407SSherry Moore
86119397407SSherry Moore if (strncmp(basename(arg), "unix", 4) != 0) {
86219397407SSherry Moore (void) fprintf(stderr,
863753a6d45SSherry Moore gettext("%s: %s: Kernel name must be unix\n"),
86419397407SSherry Moore cmdname, arg);
86519397407SSherry Moore return (-1);
86619397407SSherry Moore }
86719397407SSherry Moore
86819397407SSherry Moore if (get_zfs_bootfs_arg(arg, &location, is_zfs, bootfs_arg) != 0)
86919397407SSherry Moore goto err_out;
87019397407SSherry Moore
87119397407SSherry Moore *mplen = location - arg;
87219397407SSherry Moore
8735b89e3f7SSherry Moore if (strstr(location, "/boot/platform") == location) {
8745b89e3f7SSherry Moore /*
8755b89e3f7SSherry Moore * Rebooting to failsafe.
8765b89e3f7SSherry Moore * Clear bootfs_arg and is_zfs flag.
8775b89e3f7SSherry Moore */
8785b89e3f7SSherry Moore bootfs_arg[0] = 0;
8795b89e3f7SSherry Moore *is_zfs = 0;
8805b89e3f7SSherry Moore } else if (strstr(location, "/platform") != location) {
88119397407SSherry Moore (void) fprintf(stderr,
8825b89e3f7SSherry Moore gettext("%s: %s: No /platform in file name\n"),
8835b89e3f7SSherry Moore cmdname, arg);
8845b89e3f7SSherry Moore goto err_out;
88519397407SSherry Moore }
88619397407SSherry Moore
88719397407SSherry Moore if ((elffd = open64(arg, O_RDONLY)) < 0 ||
88819397407SSherry Moore (pread64(elffd, ident, EI_NIDENT, 0) != EI_NIDENT)) {
88919397407SSherry Moore (void) fprintf(stderr, "%s: %s: %s\n",
89019397407SSherry Moore cmdname, arg, strerror(errno));
89119397407SSherry Moore goto err_out;
89219397407SSherry Moore }
89319397407SSherry Moore
89419397407SSherry Moore class = ident[EI_CLASS];
89519397407SSherry Moore
89619397407SSherry Moore if ((class != ELFCLASS32 && class != ELFCLASS64) ||
897753a6d45SSherry Moore memcmp(&ident[EI_MAG0], ELFMAG, 4) != 0) {
89819397407SSherry Moore (void) fprintf(stderr,
899753a6d45SSherry Moore gettext("%s: %s: Not a valid ELF file\n"), cmdname, arg);
90019397407SSherry Moore goto err_out;
90119397407SSherry Moore }
90219397407SSherry Moore
90319397407SSherry Moore format = ident[EI_DATA];
90419397407SSherry Moore
90519397407SSherry Moore if (format != CUR_ELFDATA) {
906753a6d45SSherry Moore (void) fprintf(stderr, gettext("%s: %s: Invalid data format\n"),
90719397407SSherry Moore cmdname, arg);
90819397407SSherry Moore goto err_out;
90919397407SSherry Moore }
91019397407SSherry Moore
91119397407SSherry Moore return (0);
91219397407SSherry Moore
91319397407SSherry Moore err_out:
91419397407SSherry Moore if (elffd >= 0) {
91519397407SSherry Moore (void) close(elffd);
91619397407SSherry Moore elffd = -1;
91719397407SSherry Moore }
91819397407SSherry Moore return (-1);
91919397407SSherry Moore }
92019397407SSherry Moore
9214e1f1c13SKonstantin Ananyev static int
halt_exec(const char * path,...)9224e1f1c13SKonstantin Ananyev halt_exec(const char *path, ...)
9234e1f1c13SKonstantin Ananyev {
9244e1f1c13SKonstantin Ananyev pid_t pid;
9254e1f1c13SKonstantin Ananyev int i;
9264e1f1c13SKonstantin Ananyev int st;
9274e1f1c13SKonstantin Ananyev const char *arg;
9284e1f1c13SKonstantin Ananyev va_list vp;
9294e1f1c13SKonstantin Ananyev const char *argv[256];
9304e1f1c13SKonstantin Ananyev
9314e1f1c13SKonstantin Ananyev if ((pid = fork()) == -1) {
9324e1f1c13SKonstantin Ananyev return (errno);
9334e1f1c13SKonstantin Ananyev } else if (pid == 0) {
9344e1f1c13SKonstantin Ananyev (void) fclose(stdout);
9354e1f1c13SKonstantin Ananyev (void) fclose(stderr);
9364e1f1c13SKonstantin Ananyev
9374e1f1c13SKonstantin Ananyev argv[0] = path;
9384e1f1c13SKonstantin Ananyev i = 1;
9394e1f1c13SKonstantin Ananyev
9404e1f1c13SKonstantin Ananyev va_start(vp, path);
9414e1f1c13SKonstantin Ananyev
9424e1f1c13SKonstantin Ananyev do {
9434e1f1c13SKonstantin Ananyev arg = va_arg(vp, const char *);
9444e1f1c13SKonstantin Ananyev argv[i] = arg;
9454e1f1c13SKonstantin Ananyev } while (arg != NULL &&
9464e1f1c13SKonstantin Ananyev ++i != sizeof (argv) / sizeof (argv[0]));
9474e1f1c13SKonstantin Ananyev
9484e1f1c13SKonstantin Ananyev va_end(vp);
9494e1f1c13SKonstantin Ananyev
9504e1f1c13SKonstantin Ananyev (void) execve(path, (char * const *)argv, NULL);
951753a6d45SSherry Moore (void) fprintf(stderr, gettext("Cannot execute %s: %s\n"),
9524e1f1c13SKonstantin Ananyev path, strerror(errno));
9534e1f1c13SKonstantin Ananyev exit(-1);
9544e1f1c13SKonstantin Ananyev } else {
9554e1f1c13SKonstantin Ananyev if (waitpid(pid, &st, 0) == pid &&
9564e1f1c13SKonstantin Ananyev !WIFSIGNALED(st) && WIFEXITED(st))
9574e1f1c13SKonstantin Ananyev st = WEXITSTATUS(st);
9584e1f1c13SKonstantin Ananyev else
9594e1f1c13SKonstantin Ananyev st = -1;
9604e1f1c13SKonstantin Ananyev }
9614e1f1c13SKonstantin Ananyev return (st);
9624e1f1c13SKonstantin Ananyev }
9634e1f1c13SKonstantin Ananyev
9644e1f1c13SKonstantin Ananyev /*
96580cb75f4SWilliam Kucharski * Mount the specified BE.
96680cb75f4SWilliam Kucharski *
96780cb75f4SWilliam Kucharski * Upon success returns zero and copies bename string to mountpoint[]
9684e1f1c13SKonstantin Ananyev */
96919397407SSherry Moore static int
fastboot_bename(const char * bename,char * mountpoint,size_t mpsz)97019397407SSherry Moore fastboot_bename(const char *bename, char *mountpoint, size_t mpsz)
97119397407SSherry Moore {
97219397407SSherry Moore int rc;
97319397407SSherry Moore
97480cb75f4SWilliam Kucharski /*
97580cb75f4SWilliam Kucharski * Attempt to unmount the BE first in case it's already mounted
97680cb75f4SWilliam Kucharski * elsewhere.
97780cb75f4SWilliam Kucharski */
97880cb75f4SWilliam Kucharski (void) halt_exec(BEADM_PROG, "umount", bename, NULL);
97919397407SSherry Moore
98080cb75f4SWilliam Kucharski if ((rc = halt_exec(BEADM_PROG, "mount", bename, FASTBOOT_MOUNTPOINT,
9814e1f1c13SKonstantin Ananyev NULL)) != 0)
98280cb75f4SWilliam Kucharski (void) fprintf(stderr,
98380cb75f4SWilliam Kucharski gettext("%s: Unable to mount BE \"%s\" at %s\n"),
98480cb75f4SWilliam Kucharski cmdname, bename, FASTBOOT_MOUNTPOINT);
98519397407SSherry Moore else
98619397407SSherry Moore (void) strlcpy(mountpoint, FASTBOOT_MOUNTPOINT, mpsz);
98719397407SSherry Moore
98819397407SSherry Moore return (rc);
98919397407SSherry Moore }
99019397407SSherry Moore
99119397407SSherry Moore /*
99219397407SSherry Moore * Returns 0 on successful parsing of the arguments;
993753a6d45SSherry Moore * returns EINVAL on parsing failures that should abort the reboot attempt;
994753a6d45SSherry Moore * returns other error code to fall back to regular reboot.
99519397407SSherry Moore */
99619397407SSherry Moore static int
parse_fastboot_args(char * bootargs_buf,size_t buf_size,int * is_dryrun,const char * bename)997753a6d45SSherry Moore parse_fastboot_args(char *bootargs_buf, size_t buf_size,
9985b89e3f7SSherry Moore int *is_dryrun, const char *bename)
99919397407SSherry Moore {
100019397407SSherry Moore char mountpoint[MAXPATHLEN];
100119397407SSherry Moore char bootargs_saved[BOOTARGS_MAX];
100219397407SSherry Moore char bootargs_scratch[BOOTARGS_MAX];
100319397407SSherry Moore char bootfs_arg[BOOTARGS_MAX];
100419397407SSherry Moore char unixfile[BOOTARGS_MAX];
100519397407SSherry Moore char *head, *newarg;
100619397407SSherry Moore int buflen; /* length of the bootargs_buf */
100719397407SSherry Moore int mplen; /* length of the mount point */
100819397407SSherry Moore int rootlen = 0; /* length of the root argument */
100919397407SSherry Moore int unixlen = 0; /* length of the unix argument */
101019397407SSherry Moore int off = 0; /* offset into the new boot argument */
101119397407SSherry Moore int is_zfs = 0;
101219397407SSherry Moore int rc = 0;
101319397407SSherry Moore
101419397407SSherry Moore bzero(mountpoint, sizeof (mountpoint));
101519397407SSherry Moore
101619397407SSherry Moore /*
101719397407SSherry Moore * If argc is not 0, buflen is length of the argument being passed in;
101819397407SSherry Moore * else it is 0 as bootargs_buf has been initialized to all 0's.
101919397407SSherry Moore */
102019397407SSherry Moore buflen = strlen(bootargs_buf);
102119397407SSherry Moore
102219397407SSherry Moore /* Save a copy of the original argument */
102319397407SSherry Moore bcopy(bootargs_buf, bootargs_saved, buflen);
102419397407SSherry Moore bzero(&bootargs_saved[buflen], sizeof (bootargs_saved) - buflen);
102519397407SSherry Moore
102619397407SSherry Moore /* Save another copy to be used by strtok */
102719397407SSherry Moore bcopy(bootargs_buf, bootargs_scratch, buflen);
102819397407SSherry Moore bzero(&bootargs_scratch[buflen], sizeof (bootargs_scratch) - buflen);
102919397407SSherry Moore head = &bootargs_scratch[0];
103019397407SSherry Moore
103119397407SSherry Moore /* Get the first argument */
103219397407SSherry Moore newarg = strtok(bootargs_scratch, " ");
103319397407SSherry Moore
103419397407SSherry Moore /*
103519397407SSherry Moore * If this is a dry run request, verify that the drivers can handle
103619397407SSherry Moore * fast reboot.
103719397407SSherry Moore */
103819397407SSherry Moore if (newarg && strncasecmp(newarg, "dryrun", strlen("dryrun")) == 0) {
103919397407SSherry Moore *is_dryrun = 1;
104019397407SSherry Moore (void) system("/usr/sbin/devfsadm");
104119397407SSherry Moore }
104219397407SSherry Moore
104319397407SSherry Moore /*
104419397407SSherry Moore * Always perform a dry run to identify all the drivers that
104519397407SSherry Moore * need to implement devo_reset().
104619397407SSherry Moore */
104719397407SSherry Moore if (uadmin(A_SHUTDOWN, AD_FASTREBOOT_DRYRUN,
104819397407SSherry Moore (uintptr_t)bootargs_saved) != 0) {
1049753a6d45SSherry Moore (void) fprintf(stderr, gettext("%s: Not all drivers "
1050753a6d45SSherry Moore "have implemented quiesce(9E)\n"
1051753a6d45SSherry Moore "\tPlease see /var/adm/messages for drivers that haven't\n"
1052753a6d45SSherry Moore "\timplemented quiesce(9E).\n"), cmdname);
105319397407SSherry Moore } else if (*is_dryrun) {
1054753a6d45SSherry Moore (void) fprintf(stderr, gettext("%s: All drivers have "
105519397407SSherry Moore "implemented quiesce(9E)\n"), cmdname);
105619397407SSherry Moore }
105719397407SSherry Moore
1058753a6d45SSherry Moore /* Return if it is a true dry run. */
105919397407SSherry Moore if (*is_dryrun)
106019397407SSherry Moore return (rc);
106119397407SSherry Moore
10629abc7a57SToomas Soome #if defined(__x86)
10639abc7a57SToomas Soome /* Read boot args from Boot Environment */
1064753a6d45SSherry Moore if ((bootargs_buf[0] == 0 || isdigit(bootargs_buf[0])) &&
1065753a6d45SSherry Moore bename == NULL) {
1066753a6d45SSherry Moore /*
10679abc7a57SToomas Soome * If no boot arguments are given, or a BE entry
10689abc7a57SToomas Soome * number is provided, process the boot arguments from BE.
1069753a6d45SSherry Moore */
1070753a6d45SSherry Moore int entnum;
1071753a6d45SSherry Moore if (bootargs_buf[0] == 0)
10729abc7a57SToomas Soome entnum = BE_ENTRY_DEFAULT;
1073753a6d45SSherry Moore else {
1074753a6d45SSherry Moore errno = 0;
1075753a6d45SSherry Moore entnum = strtoul(bootargs_buf, NULL, 10);
1076753a6d45SSherry Moore rc = errno;
1077753a6d45SSherry Moore }
1078753a6d45SSherry Moore
10799abc7a57SToomas Soome if (rc == 0 && (rc = be_get_boot_args(&fbarg, entnum)) == 0) {
10809abc7a57SToomas Soome if (strlcpy(bootargs_buf, fbarg,
1081753a6d45SSherry Moore buf_size) >= buf_size) {
10829abc7a57SToomas Soome free(fbarg);
1083753a6d45SSherry Moore bcopy(bootargs_saved, bootargs_buf, buf_size);
1084753a6d45SSherry Moore rc = E2BIG;
1085753a6d45SSherry Moore }
1086753a6d45SSherry Moore }
10879abc7a57SToomas Soome /* Failed to read FB args, fall back to normal reboot */
1088753a6d45SSherry Moore if (rc != 0) {
1089753a6d45SSherry Moore (void) fprintf(stderr,
10909abc7a57SToomas Soome gettext("%s: Failed to process boot "
10919abc7a57SToomas Soome "arguments from Boot Environment.\n"), cmdname);
1092753a6d45SSherry Moore (void) fprintf(stderr,
1093753a6d45SSherry Moore gettext("%s: Falling back to regular reboot.\n"),
1094753a6d45SSherry Moore cmdname);
1095753a6d45SSherry Moore return (-1);
1096753a6d45SSherry Moore }
1097753a6d45SSherry Moore /* No need to process further */
10989abc7a57SToomas Soome fbarg_used = fbarg;
1099753a6d45SSherry Moore fbarg_entnum = entnum;
1100753a6d45SSherry Moore return (0);
1101753a6d45SSherry Moore }
11029abc7a57SToomas Soome #endif /* __x86 */
1103753a6d45SSherry Moore
1104753a6d45SSherry Moore /* Zero out the boot argument buffer as we will reconstruct it */
1105753a6d45SSherry Moore bzero(bootargs_buf, buf_size);
1106753a6d45SSherry Moore bzero(bootfs_arg, sizeof (bootfs_arg));
1107753a6d45SSherry Moore bzero(unixfile, sizeof (unixfile));
1108753a6d45SSherry Moore
110919397407SSherry Moore if (bename && (rc = fastboot_bename(bename, mountpoint,
111019397407SSherry Moore sizeof (mountpoint))) != 0)
1111753a6d45SSherry Moore return (EINVAL);
1112753a6d45SSherry Moore
111319397407SSherry Moore
111419397407SSherry Moore /*
111519397407SSherry Moore * If BE is not specified, look for disk argument to construct
111619397407SSherry Moore * mountpoint; if BE has been specified, mountpoint has already been
111719397407SSherry Moore * constructed.
111819397407SSherry Moore */
111919397407SSherry Moore if (newarg && newarg[0] != '-' && !bename) {
112019397407SSherry Moore int tmprc;
112119397407SSherry Moore
112219397407SSherry Moore if ((tmprc = validate_disk(newarg, mountpoint)) == 0) {
112319397407SSherry Moore /*
112419397407SSherry Moore * The first argument is a valid root argument.
112519397407SSherry Moore * Get the next argument.
112619397407SSherry Moore */
112719397407SSherry Moore newarg = strtok(NULL, " ");
112819397407SSherry Moore rootlen = (newarg) ? (newarg - head) : buflen;
112919397407SSherry Moore (void) strlcpy(fastboot_mounted, mountpoint,
113019397407SSherry Moore sizeof (fastboot_mounted));
113119397407SSherry Moore
113219397407SSherry Moore } else if (tmprc == -1) {
113319397407SSherry Moore /*
113419397407SSherry Moore * Not a disk argument. Use / as default root.
113519397407SSherry Moore */
113619397407SSherry Moore bcopy("/", mountpoint, 1);
113719397407SSherry Moore bzero(&mountpoint[1], sizeof (mountpoint) - 1);
113819397407SSherry Moore } else {
113919397407SSherry Moore /*
114019397407SSherry Moore * Disk argument, but not valid or not root.
114119397407SSherry Moore * Return failure.
114219397407SSherry Moore */
114319397407SSherry Moore return (EINVAL);
114419397407SSherry Moore }
114519397407SSherry Moore }
114619397407SSherry Moore
114719397407SSherry Moore /*
114819397407SSherry Moore * Make mountpoint the first part of unixfile.
114919397407SSherry Moore * If there is not disk argument, and BE has not been specified,
115019397407SSherry Moore * mountpoint could be empty.
115119397407SSherry Moore */
115219397407SSherry Moore mplen = strlen(mountpoint);
115319397407SSherry Moore bcopy(mountpoint, unixfile, mplen);
115419397407SSherry Moore
115519397407SSherry Moore /*
115619397407SSherry Moore * Look for unix argument
115719397407SSherry Moore */
115819397407SSherry Moore if (newarg && newarg[0] != '-') {
115919397407SSherry Moore bcopy(newarg, &unixfile[mplen], strlen(newarg));
116019397407SSherry Moore newarg = strtok(NULL, " ");
116119397407SSherry Moore rootlen = (newarg) ? (newarg - head) : buflen;
116219397407SSherry Moore } else if (mplen != 0) {
116319397407SSherry Moore /*
116419397407SSherry Moore * No unix argument, but mountpoint is not empty, use
11654e995f2aSMarcel Telka * /platform/i86pc/kernel/$ISADIR/unix as default.
116619397407SSherry Moore */
116719397407SSherry Moore char isa[20];
116819397407SSherry Moore
116919397407SSherry Moore if (sysinfo(SI_ARCHITECTURE_64, isa, sizeof (isa)) != -1)
117019397407SSherry Moore (void) snprintf(&unixfile[mplen],
117119397407SSherry Moore sizeof (unixfile) - mplen,
117219397407SSherry Moore "/platform/i86pc/kernel/%s/unix", isa);
117319397407SSherry Moore else if (sysinfo(SI_ARCHITECTURE_32, isa, sizeof (isa)) != -1) {
117419397407SSherry Moore (void) snprintf(&unixfile[mplen],
117519397407SSherry Moore sizeof (unixfile) - mplen,
117619397407SSherry Moore "/platform/i86pc/kernel/unix");
117719397407SSherry Moore } else {
117819397407SSherry Moore (void) fprintf(stderr,
1179753a6d45SSherry Moore gettext("%s: Unknown architecture"), cmdname);
118019397407SSherry Moore return (EINVAL);
118119397407SSherry Moore }
118219397407SSherry Moore }
118319397407SSherry Moore
118419397407SSherry Moore /*
118519397407SSherry Moore * We now have the complete unix argument. Verify that it exists and
118619397407SSherry Moore * is an ELF file. Split the argument up into mountpoint and unix
118719397407SSherry Moore * portions again. This is necessary to handle cases where mountpoint
118819397407SSherry Moore * is specified on the command line as part of the unix argument,
118919397407SSherry Moore * such as this:
119019397407SSherry Moore * # reboot -f /.alt/platform/i86pc/kernel/amd64/unix
119119397407SSherry Moore */
119219397407SSherry Moore unixlen = strlen(unixfile);
119319397407SSherry Moore if (unixlen > 0) {
119419397407SSherry Moore if (validate_unix(unixfile, &mplen, &is_zfs,
11955b89e3f7SSherry Moore bootfs_arg) != 0) {
119619397407SSherry Moore /* Not a valid unix file */
119719397407SSherry Moore return (EINVAL);
119819397407SSherry Moore } else {
11996bc8bc6aSSherry Moore int space = 0;
120019397407SSherry Moore /*
120119397407SSherry Moore * Construct boot argument.
120219397407SSherry Moore */
120319397407SSherry Moore unixlen = strlen(unixfile);
12046bc8bc6aSSherry Moore
12056bc8bc6aSSherry Moore /*
12066bc8bc6aSSherry Moore * mdep cannot start with space because bootadm
12076bc8bc6aSSherry Moore * creates bogus menu entries if it does.
12086bc8bc6aSSherry Moore */
12096bc8bc6aSSherry Moore if (mplen > 0) {
12106bc8bc6aSSherry Moore bcopy(unixfile, bootargs_buf, mplen);
12116bc8bc6aSSherry Moore (void) strcat(bootargs_buf, " ");
12126bc8bc6aSSherry Moore space = 1;
12136bc8bc6aSSherry Moore }
12146bc8bc6aSSherry Moore bcopy(&unixfile[mplen], &bootargs_buf[mplen + space],
121519397407SSherry Moore unixlen - mplen);
121619397407SSherry Moore (void) strcat(bootargs_buf, " ");
12176bc8bc6aSSherry Moore off += unixlen + space + 1;
121819397407SSherry Moore }
121919397407SSherry Moore } else {
122019397407SSherry Moore /* Check to see if root is zfs */
122119397407SSherry Moore const char *dp;
122219397407SSherry Moore (void) get_zfs_bootfs_arg("/", &dp, &is_zfs, bootfs_arg);
122319397407SSherry Moore }
122419397407SSherry Moore
122519397407SSherry Moore if (is_zfs && (buflen != 0 || bename != NULL)) {
12269e573dccSToomas Soome /* do not copy existing zfs boot args */
12279e573dccSToomas Soome if (strstr(&bootargs_saved[rootlen], "-B") == NULL ||
12289e573dccSToomas Soome strstr(&bootargs_saved[rootlen], "zfs-bootfs=") == NULL ||
12299e573dccSToomas Soome (strstr(&bootargs_saved[rootlen], "bootpath=") == NULL &&
12309e573dccSToomas Soome strstr(&bootargs_saved[rootlen], "diskdevid=") == NULL))
12319e573dccSToomas Soome /* LINTED E_SEC_SPRINTF_UNBOUNDED_COPY */
12329e573dccSToomas Soome off += sprintf(bootargs_buf + off, "%s ", bootfs_arg);
123319397407SSherry Moore }
123419397407SSherry Moore
123519397407SSherry Moore /*
123619397407SSherry Moore * Copy the rest of the arguments
123719397407SSherry Moore */
123819397407SSherry Moore bcopy(&bootargs_saved[rootlen], &bootargs_buf[off], buflen - rootlen);
123919397407SSherry Moore
124019397407SSherry Moore return (rc);
124119397407SSherry Moore }
124219397407SSherry Moore
1243de0cf919SEnrico Perla - Sun Microsystems #define MAXARGS 5
1244de0cf919SEnrico Perla - Sun Microsystems
1245de0cf919SEnrico Perla - Sun Microsystems static void
do_archives_update(int do_fast_reboot)1246de0cf919SEnrico Perla - Sun Microsystems do_archives_update(int do_fast_reboot)
1247de0cf919SEnrico Perla - Sun Microsystems {
1248de0cf919SEnrico Perla - Sun Microsystems int r, i = 0;
1249de0cf919SEnrico Perla - Sun Microsystems pid_t pid;
1250de0cf919SEnrico Perla - Sun Microsystems char *cmd_argv[MAXARGS];
1251de0cf919SEnrico Perla - Sun Microsystems
1252de0cf919SEnrico Perla - Sun Microsystems
1253de0cf919SEnrico Perla - Sun Microsystems cmd_argv[i++] = "/sbin/bootadm";
1254de0cf919SEnrico Perla - Sun Microsystems cmd_argv[i++] = "-ea";
1255de0cf919SEnrico Perla - Sun Microsystems cmd_argv[i++] = "update_all";
1256de0cf919SEnrico Perla - Sun Microsystems if (do_fast_reboot)
1257de0cf919SEnrico Perla - Sun Microsystems cmd_argv[i++] = "fastboot";
1258de0cf919SEnrico Perla - Sun Microsystems cmd_argv[i] = NULL;
1259de0cf919SEnrico Perla - Sun Microsystems
1260de0cf919SEnrico Perla - Sun Microsystems r = posix_spawn(&pid, cmd_argv[0], NULL, NULL, cmd_argv, NULL);
1261de0cf919SEnrico Perla - Sun Microsystems
1262de0cf919SEnrico Perla - Sun Microsystems /* if posix_spawn fails we emit a warning and continue */
1263de0cf919SEnrico Perla - Sun Microsystems
1264de0cf919SEnrico Perla - Sun Microsystems if (r != 0)
1265de0cf919SEnrico Perla - Sun Microsystems (void) fprintf(stderr, gettext("%s: WARNING, unable to start "
1266de0cf919SEnrico Perla - Sun Microsystems "boot archive update\n"), cmdname);
1267de0cf919SEnrico Perla - Sun Microsystems else
1268de0cf919SEnrico Perla - Sun Microsystems while (waitpid(pid, NULL, 0) == -1 && errno == EINTR)
1269de0cf919SEnrico Perla - Sun Microsystems ;
1270de0cf919SEnrico Perla - Sun Microsystems }
1271de0cf919SEnrico Perla - Sun Microsystems
12727c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])12737c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
12747c478bd9Sstevel@tonic-gate {
12757c478bd9Sstevel@tonic-gate int qflag = 0, needlog = 1, nosync = 0;
127619397407SSherry Moore int fast_reboot = 0;
1277753a6d45SSherry Moore int prom_reboot = 0;
12781940b2a8SToomas Soome uintptr_t mdep = 0;
12797c478bd9Sstevel@tonic-gate int cmd, fcn, c, aval, r;
12807c478bd9Sstevel@tonic-gate const char *usage;
12818e07a41fSKonstantin Ananyev const char *optstring;
12827c478bd9Sstevel@tonic-gate zoneid_t zoneid = getzoneid();
12833f2f09c1Sdp int need_check_zones = 0;
12843f2f09c1Sdp char bootargs_buf[BOOTARGS_MAX];
12853b860eeeSSherry Moore char *bootargs_orig = NULL;
128619397407SSherry Moore char *bename = NULL;
12877c478bd9Sstevel@tonic-gate
12887c478bd9Sstevel@tonic-gate const char * const resetting = "/etc/svc/volatile/resetting";
12897c478bd9Sstevel@tonic-gate
12907c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
12917c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
12927c478bd9Sstevel@tonic-gate
12933f2f09c1Sdp cmdname = basename(argv[0]);
12943f2f09c1Sdp
12957c478bd9Sstevel@tonic-gate if (strcmp(cmdname, "halt") == 0) {
12967c478bd9Sstevel@tonic-gate (void) audit_halt_setup(argc, argv);
12978e07a41fSKonstantin Ananyev optstring = "dlnqy";
12987c478bd9Sstevel@tonic-gate usage = gettext("usage: %s [ -dlnqy ]\n");
12997c478bd9Sstevel@tonic-gate cmd = A_SHUTDOWN;
13007c478bd9Sstevel@tonic-gate fcn = AD_HALT;
13017c478bd9Sstevel@tonic-gate } else if (strcmp(cmdname, "poweroff") == 0) {
13027c478bd9Sstevel@tonic-gate (void) audit_halt_setup(argc, argv);
13038e07a41fSKonstantin Ananyev optstring = "dlnqy";
13047c478bd9Sstevel@tonic-gate usage = gettext("usage: %s [ -dlnqy ]\n");
13057c478bd9Sstevel@tonic-gate cmd = A_SHUTDOWN;
13067c478bd9Sstevel@tonic-gate fcn = AD_POWEROFF;
13077c478bd9Sstevel@tonic-gate } else if (strcmp(cmdname, "reboot") == 0) {
13087c478bd9Sstevel@tonic-gate (void) audit_reboot_setup();
13099abc7a57SToomas Soome #if defined(__x86)
1310753a6d45SSherry Moore optstring = "dlnqpfe:";
1311753a6d45SSherry Moore usage = gettext("usage: %s [ -dlnq(p|fe:) ] [ boot args ]\n");
131219397407SSherry Moore #else
1313e557d412SChristopher Kiick optstring = "dlnqfp";
1314e557d412SChristopher Kiick usage = gettext("usage: %s [ -dlnq(p|f) ] [ boot args ]\n");
131519397407SSherry Moore #endif
13167c478bd9Sstevel@tonic-gate cmd = A_SHUTDOWN;
13177c478bd9Sstevel@tonic-gate fcn = AD_BOOT;
13187c478bd9Sstevel@tonic-gate } else {
13197c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
13207c478bd9Sstevel@tonic-gate gettext("%s: not installed properly\n"), cmdname);
13217c478bd9Sstevel@tonic-gate return (1);
13227c478bd9Sstevel@tonic-gate }
13237c478bd9Sstevel@tonic-gate
13248e07a41fSKonstantin Ananyev while ((c = getopt(argc, argv, optstring)) != EOF) {
13257c478bd9Sstevel@tonic-gate switch (c) {
13267c478bd9Sstevel@tonic-gate case 'd':
13277c478bd9Sstevel@tonic-gate if (zoneid == GLOBAL_ZONEID)
13287c478bd9Sstevel@tonic-gate cmd = A_DUMP;
13297c478bd9Sstevel@tonic-gate else {
13307c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
13317c478bd9Sstevel@tonic-gate gettext("%s: -d only valid from global"
13327c478bd9Sstevel@tonic-gate " zone\n"), cmdname);
13337c478bd9Sstevel@tonic-gate return (1);
13347c478bd9Sstevel@tonic-gate }
13357c478bd9Sstevel@tonic-gate break;
13367c478bd9Sstevel@tonic-gate case 'l':
13377c478bd9Sstevel@tonic-gate needlog = 0;
13387c478bd9Sstevel@tonic-gate break;
13397c478bd9Sstevel@tonic-gate case 'n':
13407c478bd9Sstevel@tonic-gate nosync = 1;
13417c478bd9Sstevel@tonic-gate break;
13427c478bd9Sstevel@tonic-gate case 'q':
13437c478bd9Sstevel@tonic-gate qflag = 1;
13447c478bd9Sstevel@tonic-gate break;
13457c478bd9Sstevel@tonic-gate case 'y':
1346bc2cf0f5SJoshua M. Clulow /*
1347bc2cf0f5SJoshua M. Clulow * Option ignored for backwards compatibility.
1348bc2cf0f5SJoshua M. Clulow */
13497c478bd9Sstevel@tonic-gate break;
135019397407SSherry Moore case 'f':
135119397407SSherry Moore fast_reboot = 1;
135219397407SSherry Moore break;
1353e557d412SChristopher Kiick case 'p':
1354e557d412SChristopher Kiick prom_reboot = 1;
1355e557d412SChristopher Kiick break;
13569abc7a57SToomas Soome #if defined(__x86)
135719397407SSherry Moore case 'e':
135819397407SSherry Moore bename = optarg;
135919397407SSherry Moore break;
136019397407SSherry Moore #endif
13617c478bd9Sstevel@tonic-gate default:
13627c478bd9Sstevel@tonic-gate /*
13637c478bd9Sstevel@tonic-gate * TRANSLATION_NOTE
13647c478bd9Sstevel@tonic-gate * Don't translate the words "halt" or "reboot"
13657c478bd9Sstevel@tonic-gate */
13667c478bd9Sstevel@tonic-gate (void) fprintf(stderr, usage, cmdname);
13677c478bd9Sstevel@tonic-gate return (1);
13687c478bd9Sstevel@tonic-gate }
13697c478bd9Sstevel@tonic-gate }
13707c478bd9Sstevel@tonic-gate
13717c478bd9Sstevel@tonic-gate argc -= optind;
13727c478bd9Sstevel@tonic-gate argv += optind;
13737c478bd9Sstevel@tonic-gate
13747c478bd9Sstevel@tonic-gate if (argc != 0) {
13757c478bd9Sstevel@tonic-gate if (fcn != AD_BOOT) {
13767c478bd9Sstevel@tonic-gate (void) fprintf(stderr, usage, cmdname);
13777c478bd9Sstevel@tonic-gate return (1);
13787c478bd9Sstevel@tonic-gate }
13797c478bd9Sstevel@tonic-gate
13807c478bd9Sstevel@tonic-gate /* Gather the arguments into bootargs_buf. */
13817c478bd9Sstevel@tonic-gate if (gather_args(argv, bootargs_buf, sizeof (bootargs_buf)) !=
13827c478bd9Sstevel@tonic-gate 0) {
13837c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
13847c478bd9Sstevel@tonic-gate gettext("%s: Boot arguments too long.\n"), cmdname);
13857c478bd9Sstevel@tonic-gate return (1);
13867c478bd9Sstevel@tonic-gate }
138719397407SSherry Moore
13883b860eeeSSherry Moore bootargs_orig = strdup(bootargs_buf);
13897c478bd9Sstevel@tonic-gate mdep = (uintptr_t)bootargs_buf;
139019397407SSherry Moore } else {
139119397407SSherry Moore /*
139219397407SSherry Moore * Initialize it to 0 in case of fastboot, the buffer
139319397407SSherry Moore * will be used.
139419397407SSherry Moore */
139519397407SSherry Moore bzero(bootargs_buf, sizeof (bootargs_buf));
13967c478bd9Sstevel@tonic-gate }
13977c478bd9Sstevel@tonic-gate
1398753a6d45SSherry Moore if (geteuid() != 0) {
13997c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
14007c478bd9Sstevel@tonic-gate gettext("%s: permission denied\n"), cmdname);
14017c478bd9Sstevel@tonic-gate goto fail;
14027c478bd9Sstevel@tonic-gate }
14037c478bd9Sstevel@tonic-gate
1404753a6d45SSherry Moore if (fast_reboot && prom_reboot) {
1405753a6d45SSherry Moore (void) fprintf(stderr,
1406753a6d45SSherry Moore gettext("%s: -p and -f are mutually exclusive\n"),
1407753a6d45SSherry Moore cmdname);
1408753a6d45SSherry Moore return (EINVAL);
1409753a6d45SSherry Moore }
141019397407SSherry Moore /*
1411753a6d45SSherry Moore * Check whether fast reboot is the default operating mode
141219397407SSherry Moore */
1413753a6d45SSherry Moore if (fcn == AD_BOOT && !fast_reboot && !prom_reboot &&
1414e557d412SChristopher Kiick zoneid == GLOBAL_ZONEID) {
1415753a6d45SSherry Moore fast_reboot = scf_is_fastboot_default();
141619397407SSherry Moore
1417e557d412SChristopher Kiick }
1418e557d412SChristopher Kiick
141919397407SSherry Moore if (bename && !fast_reboot) {
142019397407SSherry Moore (void) fprintf(stderr, gettext("%s: -e only valid with -f\n"),
142119397407SSherry Moore cmdname);
142219397407SSherry Moore return (EINVAL);
142319397407SSherry Moore }
142419397407SSherry Moore
1425e557d412SChristopher Kiick #if defined(__sparc)
1426e557d412SChristopher Kiick if (fast_reboot) {
1427e557d412SChristopher Kiick fast_reboot = 2; /* need to distinguish each case */
1428e557d412SChristopher Kiick }
1429e557d412SChristopher Kiick #endif
1430e557d412SChristopher Kiick
143119397407SSherry Moore /*
143219397407SSherry Moore * If fast reboot, do some sanity check on the argument
143319397407SSherry Moore */
1434e557d412SChristopher Kiick if (fast_reboot == 1) {
143519397407SSherry Moore int rc;
143619397407SSherry Moore int is_dryrun = 0;
143719397407SSherry Moore
143819397407SSherry Moore if (zoneid != GLOBAL_ZONEID) {
143919397407SSherry Moore (void) fprintf(stderr,
1440753a6d45SSherry Moore gettext("%s: Fast reboot only valid from global"
144119397407SSherry Moore " zone\n"), cmdname);
144219397407SSherry Moore return (EINVAL);
144319397407SSherry Moore }
144419397407SSherry Moore
1445753a6d45SSherry Moore rc = parse_fastboot_args(bootargs_buf, sizeof (bootargs_buf),
14465b89e3f7SSherry Moore &is_dryrun, bename);
144719397407SSherry Moore
144819397407SSherry Moore /*
144919397407SSherry Moore * If dry run, or if arguments are invalid, return.
145019397407SSherry Moore */
145119397407SSherry Moore if (is_dryrun)
145219397407SSherry Moore return (rc);
1453753a6d45SSherry Moore else if (rc == EINVAL)
145419397407SSherry Moore goto fail;
1455753a6d45SSherry Moore else if (rc != 0)
1456753a6d45SSherry Moore fast_reboot = 0;
145719397407SSherry Moore
145819397407SSherry Moore /*
145919397407SSherry Moore * For all the other errors, we continue on in case user
1460753a6d45SSherry Moore * user want to force fast reboot, or fall back to regular
1461753a6d45SSherry Moore * reboot.
146219397407SSherry Moore */
146319397407SSherry Moore if (strlen(bootargs_buf) != 0)
146419397407SSherry Moore mdep = (uintptr_t)bootargs_buf;
146519397407SSherry Moore }
146619397407SSherry Moore
146719397407SSherry Moore #if 0 /* For debugging */
146819397407SSherry Moore if (mdep != NULL)
146919397407SSherry Moore (void) fprintf(stderr, "mdep = %s\n", (char *)mdep);
147019397407SSherry Moore #endif
147119397407SSherry Moore
14727c478bd9Sstevel@tonic-gate if (needlog) {
14737c478bd9Sstevel@tonic-gate char *user = getlogin();
14747c478bd9Sstevel@tonic-gate struct passwd *pw;
1475f040a7a6Ssetje char *tty;
14767c478bd9Sstevel@tonic-gate
14777c478bd9Sstevel@tonic-gate openlog(cmdname, 0, LOG_AUTH);
14787c478bd9Sstevel@tonic-gate if (user == NULL && (pw = getpwuid(getuid())) != NULL)
14797c478bd9Sstevel@tonic-gate user = pw->pw_name;
14807c478bd9Sstevel@tonic-gate if (user == NULL)
14817c478bd9Sstevel@tonic-gate user = "root";
1482f040a7a6Ssetje
1483f040a7a6Ssetje tty = ttyname(1);
1484f040a7a6Ssetje
1485f040a7a6Ssetje if (tty == NULL)
1486f040a7a6Ssetje syslog(LOG_CRIT, "initiated by %s", user);
1487f040a7a6Ssetje else
1488f040a7a6Ssetje syslog(LOG_CRIT, "initiated by %s on %s", user, tty);
14897c478bd9Sstevel@tonic-gate }
14907c478bd9Sstevel@tonic-gate
14917c478bd9Sstevel@tonic-gate /*
14927c478bd9Sstevel@tonic-gate * We must assume success and log it before auditd is terminated.
14937c478bd9Sstevel@tonic-gate */
14947c478bd9Sstevel@tonic-gate if (fcn == AD_BOOT)
14957c478bd9Sstevel@tonic-gate aval = audit_reboot_success();
14967c478bd9Sstevel@tonic-gate else
14977c478bd9Sstevel@tonic-gate aval = audit_halt_success();
14987c478bd9Sstevel@tonic-gate
14997c478bd9Sstevel@tonic-gate if (aval == -1) {
15007c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
15017c478bd9Sstevel@tonic-gate gettext("%s: can't turn off auditd\n"), cmdname);
15027c478bd9Sstevel@tonic-gate if (needlog)
15037c478bd9Sstevel@tonic-gate (void) sleep(5); /* Give syslogd time to record this */
15047c478bd9Sstevel@tonic-gate }
15057c478bd9Sstevel@tonic-gate
15067c478bd9Sstevel@tonic-gate (void) signal(SIGHUP, SIG_IGN); /* for remote connections */
15077c478bd9Sstevel@tonic-gate
150826f665e8Sdstaff /*
150926f665e8Sdstaff * We start to fork a bunch of zoneadms to halt any active zones.
151026f665e8Sdstaff * This will proceed with halt in parallel until we call
151126f665e8Sdstaff * check_zone_haltedness later on.
151226f665e8Sdstaff */
151326f665e8Sdstaff if (zoneid == GLOBAL_ZONEID && cmd != A_DUMP) {
15143f2f09c1Sdp need_check_zones = halt_zones();
151526f665e8Sdstaff }
151626f665e8Sdstaff
15179abc7a57SToomas Soome #if defined(__x86)
1518753a6d45SSherry Moore /* set new default entry in the GRUB entry */
15199abc7a57SToomas Soome if (fbarg_entnum != BE_ENTRY_DEFAULT) {
1520753a6d45SSherry Moore char buf[32];
1521753a6d45SSherry Moore (void) snprintf(buf, sizeof (buf), "default=%u", fbarg_entnum);
1522753a6d45SSherry Moore (void) halt_exec(BOOTADM_PROG, "set-menu", buf, NULL);
1523753a6d45SSherry Moore }
15249abc7a57SToomas Soome #endif /* __x86 */
152548847494SEnrico Perla - Sun Microsystems
1526753a6d45SSherry Moore /* if we're dumping, do the archive update here and don't defer it */
1527de0cf919SEnrico Perla - Sun Microsystems if (cmd == A_DUMP && zoneid == GLOBAL_ZONEID && !nosync)
1528de0cf919SEnrico Perla - Sun Microsystems do_archives_update(fast_reboot);
15297c478bd9Sstevel@tonic-gate
15307c478bd9Sstevel@tonic-gate /*
15317c478bd9Sstevel@tonic-gate * If we're not forcing a crash dump, mark the system as quiescing for
1532*bbf21555SRichard Lowe * smf(7)'s benefit, and idle the init process.
15337c478bd9Sstevel@tonic-gate */
15347c478bd9Sstevel@tonic-gate if (cmd != A_DUMP) {
15353f2f09c1Sdp if (direct_init(PCDSTOP) == -1) {
15367c478bd9Sstevel@tonic-gate /*
15377c478bd9Sstevel@tonic-gate * TRANSLATION_NOTE
15387c478bd9Sstevel@tonic-gate * Don't translate the word "init"
15397c478bd9Sstevel@tonic-gate */
15407c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
15417c478bd9Sstevel@tonic-gate gettext("%s: can't idle init\n"), cmdname);
15427c478bd9Sstevel@tonic-gate goto fail;
15437c478bd9Sstevel@tonic-gate }
15447c478bd9Sstevel@tonic-gate
15457c478bd9Sstevel@tonic-gate if (creat(resetting, 0755) == -1)
15467c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
15477c478bd9Sstevel@tonic-gate gettext("%s: could not create %s.\n"),
15487c478bd9Sstevel@tonic-gate cmdname, resetting);
15499db7147eSSherry Moore }
15509db7147eSSherry Moore
15519db7147eSSherry Moore /*
15529db7147eSSherry Moore * Make sure we don't get stopped by a jobcontrol shell
15539db7147eSSherry Moore * once we start killing everybody.
15549db7147eSSherry Moore */
15559db7147eSSherry Moore (void) signal(SIGTSTP, SIG_IGN);
15569db7147eSSherry Moore (void) signal(SIGTTIN, SIG_IGN);
15579db7147eSSherry Moore (void) signal(SIGTTOU, SIG_IGN);
15589db7147eSSherry Moore (void) signal(SIGPIPE, SIG_IGN);
15599db7147eSSherry Moore (void) signal(SIGTERM, SIG_IGN);
15609db7147eSSherry Moore
15619db7147eSSherry Moore /*
15629db7147eSSherry Moore * Try to stop gdm so X has a chance to return the screen and
15639db7147eSSherry Moore * keyboard to a sane state.
15649db7147eSSherry Moore */
1565e557d412SChristopher Kiick if (fast_reboot == 1 && stop_gdm() != 0) {
15669db7147eSSherry Moore (void) fprintf(stderr,
15679db7147eSSherry Moore gettext("%s: Falling back to regular reboot.\n"), cmdname);
15689db7147eSSherry Moore fast_reboot = 0;
15693b860eeeSSherry Moore mdep = (uintptr_t)bootargs_orig;
15703b860eeeSSherry Moore } else if (bootargs_orig) {
15713b860eeeSSherry Moore free(bootargs_orig);
15729db7147eSSherry Moore }
15737c478bd9Sstevel@tonic-gate
15749db7147eSSherry Moore if (cmd != A_DUMP) {
15757c478bd9Sstevel@tonic-gate /*
15767c478bd9Sstevel@tonic-gate * Stop all restarters so they do not try to restart services
15777c478bd9Sstevel@tonic-gate * that are terminated.
15787c478bd9Sstevel@tonic-gate */
15797c478bd9Sstevel@tonic-gate stop_restarters();
158026f665e8Sdstaff
158126f665e8Sdstaff /*
158226f665e8Sdstaff * Wait a little while for zones to shutdown.
158326f665e8Sdstaff */
158426f665e8Sdstaff if (need_check_zones) {
15853f2f09c1Sdp check_zones_haltedness();
158626f665e8Sdstaff
158726f665e8Sdstaff (void) fprintf(stderr,
158826f665e8Sdstaff gettext("%s: Completing system halt.\n"),
158926f665e8Sdstaff cmdname);
159026f665e8Sdstaff }
15917c478bd9Sstevel@tonic-gate }
15927c478bd9Sstevel@tonic-gate
15937c478bd9Sstevel@tonic-gate /*
15947c478bd9Sstevel@tonic-gate * If we're not forcing a crash dump, give everyone 5 seconds to
15957c478bd9Sstevel@tonic-gate * handle a SIGTERM and clean up properly.
15967c478bd9Sstevel@tonic-gate */
15977c478bd9Sstevel@tonic-gate if (cmd != A_DUMP) {
1598de0cf919SEnrico Perla - Sun Microsystems if (zoneid == GLOBAL_ZONEID && !nosync)
1599de0cf919SEnrico Perla - Sun Microsystems do_archives_update(fast_reboot);
160075383e32SAndy Fiddaman (void) kill(-1, SIGTERM);
160175383e32SAndy Fiddaman (void) sleep(5);
16027c478bd9Sstevel@tonic-gate }
16037c478bd9Sstevel@tonic-gate
16043a0fa635SEnrico Perla - Sun Microsystems (void) signal(SIGINT, SIG_IGN);
16053a0fa635SEnrico Perla - Sun Microsystems
16067c478bd9Sstevel@tonic-gate if (!qflag && !nosync) {
16077c478bd9Sstevel@tonic-gate struct utmpx wtmpx;
16087c478bd9Sstevel@tonic-gate
16097c478bd9Sstevel@tonic-gate bzero(&wtmpx, sizeof (struct utmpx));
16107c478bd9Sstevel@tonic-gate (void) strcpy(wtmpx.ut_line, "~");
16117c478bd9Sstevel@tonic-gate (void) time(&wtmpx.ut_tv.tv_sec);
16127c478bd9Sstevel@tonic-gate
16137c478bd9Sstevel@tonic-gate if (cmd == A_DUMP)
16147c478bd9Sstevel@tonic-gate (void) strcpy(wtmpx.ut_name, "crash dump");
16157c478bd9Sstevel@tonic-gate else
16167c478bd9Sstevel@tonic-gate (void) strcpy(wtmpx.ut_name, "shutdown");
16177c478bd9Sstevel@tonic-gate
16187c478bd9Sstevel@tonic-gate (void) updwtmpx(WTMPX_FILE, &wtmpx);
16197c478bd9Sstevel@tonic-gate sync();
16207c478bd9Sstevel@tonic-gate }
16217c478bd9Sstevel@tonic-gate
16227c478bd9Sstevel@tonic-gate if (cmd == A_DUMP && nosync != 0)
16231940b2a8SToomas Soome (void) uadmin(A_DUMP, AD_NOSYNC, 0);
16247c478bd9Sstevel@tonic-gate
16255b89e3f7SSherry Moore if (fast_reboot)
162619397407SSherry Moore fcn = AD_FASTREBOOT;
162719397407SSherry Moore
16283f2f09c1Sdp if (uadmin(cmd, fcn, mdep) == -1)
16293f2f09c1Sdp (void) fprintf(stderr, "%s: uadmin failed: %s\n",
16303f2f09c1Sdp cmdname, strerror(errno));
16313f2f09c1Sdp else
16323f2f09c1Sdp (void) fprintf(stderr, "%s: uadmin unexpectedly returned 0\n",
16333f2f09c1Sdp cmdname);
16343f2f09c1Sdp
16353f2f09c1Sdp do {
16367c478bd9Sstevel@tonic-gate r = remove(resetting);
16373f2f09c1Sdp } while (r != 0 && errno == EINTR);
16383f2f09c1Sdp
16397c478bd9Sstevel@tonic-gate if (r != 0 && errno != ENOENT)
16407c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s: could not remove %s.\n"),
16417c478bd9Sstevel@tonic-gate cmdname, resetting);
16427c478bd9Sstevel@tonic-gate
16433f2f09c1Sdp if (direct_init(PCRUN) == -1) {
16443f2f09c1Sdp /*
16453f2f09c1Sdp * TRANSLATION_NOTE
16463f2f09c1Sdp * Don't translate the word "init"
16473f2f09c1Sdp */
16483f2f09c1Sdp (void) fprintf(stderr,
16493f2f09c1Sdp gettext("%s: can't resume init\n"), cmdname);
16503f2f09c1Sdp }
16513f2f09c1Sdp
16527c478bd9Sstevel@tonic-gate continue_restarters();
16537c478bd9Sstevel@tonic-gate
16543f2f09c1Sdp if (get_initpid() != -1)
16557c478bd9Sstevel@tonic-gate /* tell init to restate current level */
16563f2f09c1Sdp (void) kill(get_initpid(), SIGHUP);
16577c478bd9Sstevel@tonic-gate
16587c478bd9Sstevel@tonic-gate fail:
16597c478bd9Sstevel@tonic-gate if (fcn == AD_BOOT)
16607c478bd9Sstevel@tonic-gate (void) audit_reboot_fail();
16617c478bd9Sstevel@tonic-gate else
16627c478bd9Sstevel@tonic-gate (void) audit_halt_fail();
16637c478bd9Sstevel@tonic-gate
1664e557d412SChristopher Kiick if (fast_reboot == 1) {
166519397407SSherry Moore if (bename) {
166680cb75f4SWilliam Kucharski (void) halt_exec(BEADM_PROG, "umount", bename, NULL);
166719397407SSherry Moore
166819397407SSherry Moore } else if (strlen(fastboot_mounted) != 0) {
166919397407SSherry Moore (void) umount(fastboot_mounted);
16709abc7a57SToomas Soome #if defined(__x86)
16719abc7a57SToomas Soome } else {
16729abc7a57SToomas Soome free(fbarg_used);
16739abc7a57SToomas Soome #endif /* __x86 */
167419397407SSherry Moore }
167519397407SSherry Moore }
167619397407SSherry Moore
16777c478bd9Sstevel@tonic-gate return (1);
16787c478bd9Sstevel@tonic-gate }
1679