xref: /illumos-gate/usr/src/cmd/halt/halt.c (revision 3a0fa635e8519965a7acdaf4d6645dfb14835a53)
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 /*
2219397407SSherry 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.
378e07a41fSKonstantin Ananyev  * Portions contributed by Juergen Keil, <jk@tools.de>.
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate  * Common code for halt(1M), poweroff(1M), and reboot(1M).  We use
437c478bd9Sstevel@tonic-gate  * argv[0] to determine which behavior to exhibit.
447c478bd9Sstevel@tonic-gate  */
457c478bd9Sstevel@tonic-gate 
4619397407SSherry Moore #include <stdio.h>
473f2f09c1Sdp #include <procfs.h>
487c478bd9Sstevel@tonic-gate #include <sys/types.h>
4919397407SSherry Moore #include <sys/elf.h>
5019397407SSherry Moore #include <sys/systeminfo.h>
5119397407SSherry Moore #include <sys/stat.h>
527c478bd9Sstevel@tonic-gate #include <sys/uadmin.h>
5319397407SSherry Moore #include <sys/mntent.h>
5419397407SSherry Moore #include <sys/mnttab.h>
5519397407SSherry Moore #include <sys/mount.h>
568e07a41fSKonstantin Ananyev #include <sys/fs/ufs_mount.h>
577c478bd9Sstevel@tonic-gate #include <alloca.h>
587c478bd9Sstevel@tonic-gate #include <assert.h>
597c478bd9Sstevel@tonic-gate #include <errno.h>
607c478bd9Sstevel@tonic-gate #include <fcntl.h>
617c478bd9Sstevel@tonic-gate #include <libgen.h>
627c478bd9Sstevel@tonic-gate #include <libscf.h>
6319397407SSherry Moore #include <limits.h>
647c478bd9Sstevel@tonic-gate #include <locale.h>
657c478bd9Sstevel@tonic-gate #include <libintl.h>
667c478bd9Sstevel@tonic-gate #include <syslog.h>
677c478bd9Sstevel@tonic-gate #include <signal.h>
687c478bd9Sstevel@tonic-gate #include <strings.h>
697c478bd9Sstevel@tonic-gate #include <unistd.h>
707c478bd9Sstevel@tonic-gate #include <stdlib.h>
717c478bd9Sstevel@tonic-gate #include <stdio.h>
727c478bd9Sstevel@tonic-gate #include <strings.h>
737c478bd9Sstevel@tonic-gate #include <time.h>
744e1f1c13SKonstantin Ananyev #include <wait.h>
754e1f1c13SKonstantin Ananyev #include <ctype.h>
767c478bd9Sstevel@tonic-gate #include <utmpx.h>
777c478bd9Sstevel@tonic-gate #include <pwd.h>
787c478bd9Sstevel@tonic-gate #include <zone.h>
79*3a0fa635SEnrico Perla - Sun Microsystems #include <spawn.h>
8019397407SSherry Moore 
8119397407SSherry Moore #include <libzfs.h>
8219397407SSherry Moore 
837c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
847c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"
857c478bd9Sstevel@tonic-gate #endif
867c478bd9Sstevel@tonic-gate 
8719397407SSherry Moore #if defined(__sparc)
8819397407SSherry Moore #define	CUR_ELFDATA	ELFDATA2MSB
8919397407SSherry Moore #elif defined(__i386)
9019397407SSherry Moore #define	CUR_ELFDATA	ELFDATA2LSB
9119397407SSherry Moore #endif
9219397407SSherry Moore 
9319397407SSherry Moore static libzfs_handle_t *g_zfs;
9419397407SSherry Moore 
957c478bd9Sstevel@tonic-gate extern int audit_halt_setup(int, char **);
967c478bd9Sstevel@tonic-gate extern int audit_halt_success(void);
977c478bd9Sstevel@tonic-gate extern int audit_halt_fail(void);
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate extern int audit_reboot_setup(void);
1007c478bd9Sstevel@tonic-gate extern int audit_reboot_success(void);
1017c478bd9Sstevel@tonic-gate extern int audit_reboot_fail(void);
1027c478bd9Sstevel@tonic-gate 
1033f2f09c1Sdp static char *cmdname;	/* basename(argv[0]), the name of the command */
1043f2f09c1Sdp 
1057c478bd9Sstevel@tonic-gate typedef struct ctidlist_struct {
1067c478bd9Sstevel@tonic-gate 	ctid_t ctid;
1077c478bd9Sstevel@tonic-gate 	struct ctidlist_struct *next;
1087c478bd9Sstevel@tonic-gate } ctidlist_t;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate static ctidlist_t *ctidlist = NULL;
1117c478bd9Sstevel@tonic-gate static ctid_t startdct = -1;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate #define	FMRI_STARTD_CONTRACT \
1147c478bd9Sstevel@tonic-gate 	"svc:/system/svc/restarter:default/:properties/restarter/contract"
1157c478bd9Sstevel@tonic-gate 
11626f665e8Sdstaff #define	ZONEADM_PROG "/usr/sbin/zoneadm"
11726f665e8Sdstaff 
1184e1f1c13SKonstantin Ananyev #define	LUUMOUNT_PROG	"/usr/sbin/luumount"
1194e1f1c13SKonstantin Ananyev #define	LUMOUNT_PROG	"/usr/sbin/lumount"
1204e1f1c13SKonstantin Ananyev 
12119397407SSherry Moore /*
12219397407SSherry Moore  * The length of FASTBOOT_MOUNTPOINT must be less than MAXPATHLEN.
12319397407SSherry Moore  */
12419397407SSherry Moore #define	FASTBOOT_MOUNTPOINT	"/tmp/.fastboot.root"
12519397407SSherry Moore 
12619397407SSherry Moore static char	fastboot_mounted[MAXPATHLEN];
12719397407SSherry Moore 
12819397407SSherry Moore static int validate_ufs_disk(char *, char *);
12919397407SSherry Moore static int validate_zfs_pool(char *, char *);
13019397407SSherry Moore 
1313f2f09c1Sdp static pid_t
1323f2f09c1Sdp get_initpid()
1333f2f09c1Sdp {
1343f2f09c1Sdp 	static int init_pid = -1;
1353f2f09c1Sdp 
1363f2f09c1Sdp 	if (init_pid == -1) {
1373f2f09c1Sdp 		if (zone_getattr(getzoneid(), ZONE_ATTR_INITPID, &init_pid,
1383f2f09c1Sdp 		    sizeof (init_pid)) != sizeof (init_pid)) {
1393f2f09c1Sdp 			assert(errno == ESRCH);
1403f2f09c1Sdp 			init_pid = -1;
1413f2f09c1Sdp 		}
1423f2f09c1Sdp 	}
1433f2f09c1Sdp 	return (init_pid);
1443f2f09c1Sdp }
1453f2f09c1Sdp 
1463f2f09c1Sdp /*
1473f2f09c1Sdp  * Quiesce or resume init using /proc.  When stopping init, we can't send
1483f2f09c1Sdp  * SIGTSTP (since init ignores it) or SIGSTOP (since the kernel won't permit
1493f2f09c1Sdp  * it).
1503f2f09c1Sdp  */
1513f2f09c1Sdp static int
1523f2f09c1Sdp direct_init(long command)
1533f2f09c1Sdp {
1543f2f09c1Sdp 	char ctlfile[MAXPATHLEN];
1553f2f09c1Sdp 	pid_t pid;
1563f2f09c1Sdp 	int ctlfd;
1573f2f09c1Sdp 
1583f2f09c1Sdp 	assert(command == PCDSTOP || command == PCRUN);
1593f2f09c1Sdp 	if ((pid = get_initpid()) == -1) {
1603f2f09c1Sdp 		return (-1);
1613f2f09c1Sdp 	}
1623f2f09c1Sdp 
1633f2f09c1Sdp 	(void) snprintf(ctlfile, sizeof (ctlfile), "/proc/%d/ctl", pid);
1643f2f09c1Sdp 	if ((ctlfd = open(ctlfile, O_WRONLY)) == -1)
1653f2f09c1Sdp 		return (-1);
1663f2f09c1Sdp 
1673f2f09c1Sdp 	if (command == PCDSTOP) {
1683f2f09c1Sdp 		if (write(ctlfd, &command, sizeof (long)) == -1) {
1693f2f09c1Sdp 			(void) close(ctlfd);
1703f2f09c1Sdp 			return (-1);
1713f2f09c1Sdp 		}
1723f2f09c1Sdp 	} else {	/* command == PCRUN */
1733f2f09c1Sdp 		long cmds[2];
1743f2f09c1Sdp 		cmds[0] = command;
1753f2f09c1Sdp 		cmds[1] = 0;
1763f2f09c1Sdp 		if (write(ctlfd, cmds, sizeof (cmds)) == -1) {
1773f2f09c1Sdp 			(void) close(ctlfd);
1783f2f09c1Sdp 			return (-1);
1793f2f09c1Sdp 		}
1803f2f09c1Sdp 	}
1813f2f09c1Sdp 	(void) close(ctlfd);
1823f2f09c1Sdp 	return (0);
1833f2f09c1Sdp }
1843f2f09c1Sdp 
1857c478bd9Sstevel@tonic-gate static void
1867c478bd9Sstevel@tonic-gate stop_startd()
1877c478bd9Sstevel@tonic-gate {
1887c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
1897c478bd9Sstevel@tonic-gate 	scf_property_t *prop = NULL;
1907c478bd9Sstevel@tonic-gate 	scf_value_t *val = NULL;
1917c478bd9Sstevel@tonic-gate 	uint64_t uint64;
1927c478bd9Sstevel@tonic-gate 
1933f2f09c1Sdp 	if ((h = scf_handle_create(SCF_VERSION)) == NULL)
1947c478bd9Sstevel@tonic-gate 		return;
1957c478bd9Sstevel@tonic-gate 
1963f2f09c1Sdp 	if ((scf_handle_bind(h) != 0) ||
1973f2f09c1Sdp 	    ((prop = scf_property_create(h)) == NULL) ||
1983f2f09c1Sdp 	    ((val = scf_value_create(h)) == NULL))
1997c478bd9Sstevel@tonic-gate 		goto out;
2007c478bd9Sstevel@tonic-gate 
2013f2f09c1Sdp 	if (scf_handle_decode_fmri(h, FMRI_STARTD_CONTRACT,
2023f2f09c1Sdp 	    NULL, NULL, NULL, NULL, prop, SCF_DECODE_FMRI_EXACT) != 0)
2037c478bd9Sstevel@tonic-gate 		goto out;
2047c478bd9Sstevel@tonic-gate 
2053f2f09c1Sdp 	if (scf_property_is_type(prop, SCF_TYPE_COUNT) != 0 ||
2063f2f09c1Sdp 	    scf_property_get_value(prop, val) != 0 ||
2073f2f09c1Sdp 	    scf_value_get_count(val, &uint64) != 0)
2087c478bd9Sstevel@tonic-gate 		goto out;
2097c478bd9Sstevel@tonic-gate 
2103f2f09c1Sdp 	startdct = (ctid_t)uint64;
2113f2f09c1Sdp 	(void) sigsend(P_CTID, startdct, SIGSTOP);
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate out:
2143f2f09c1Sdp 	scf_property_destroy(prop);
2153f2f09c1Sdp 	scf_value_destroy(val);
2167c478bd9Sstevel@tonic-gate 	scf_handle_destroy(h);
2177c478bd9Sstevel@tonic-gate }
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate static void
2207c478bd9Sstevel@tonic-gate continue_startd()
2217c478bd9Sstevel@tonic-gate {
2227c478bd9Sstevel@tonic-gate 	if (startdct != -1)
2237c478bd9Sstevel@tonic-gate 		(void) sigsend(P_CTID, startdct, SIGCONT);
2247c478bd9Sstevel@tonic-gate }
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate #define	FMRI_RESTARTER_PROP "/:properties/general/restarter"
2277c478bd9Sstevel@tonic-gate #define	FMRI_CONTRACT_PROP "/:properties/restarter/contract"
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate static int
2307c478bd9Sstevel@tonic-gate save_ctid(ctid_t ctid)
2317c478bd9Sstevel@tonic-gate {
2327c478bd9Sstevel@tonic-gate 	ctidlist_t *next;
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	for (next = ctidlist; next != NULL; next = next->next)
2357c478bd9Sstevel@tonic-gate 		if (next->ctid == ctid)
2367c478bd9Sstevel@tonic-gate 			return (-1);
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	next = (ctidlist_t *)malloc(sizeof (ctidlist_t));
2397c478bd9Sstevel@tonic-gate 	if (next == NULL)
2407c478bd9Sstevel@tonic-gate 		return (-1);
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	next->ctid = ctid;
2437c478bd9Sstevel@tonic-gate 	next->next = ctidlist;
2447c478bd9Sstevel@tonic-gate 	ctidlist = next;
2457c478bd9Sstevel@tonic-gate 	return (0);
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate static void
2497c478bd9Sstevel@tonic-gate stop_delegates()
2507c478bd9Sstevel@tonic-gate {
2517c478bd9Sstevel@tonic-gate 	ctid_t ctid;
2527c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
2537c478bd9Sstevel@tonic-gate 	scf_scope_t *sc = NULL;
2547c478bd9Sstevel@tonic-gate 	scf_service_t *svc = NULL;
2557c478bd9Sstevel@tonic-gate 	scf_instance_t *inst = NULL;
2567c478bd9Sstevel@tonic-gate 	scf_snapshot_t *snap = NULL;
2577c478bd9Sstevel@tonic-gate 	scf_snapshot_t *isnap = NULL;
2587c478bd9Sstevel@tonic-gate 	scf_propertygroup_t *pg = NULL;
2597c478bd9Sstevel@tonic-gate 	scf_property_t *prop = NULL;
2607c478bd9Sstevel@tonic-gate 	scf_value_t *val = NULL;
2617c478bd9Sstevel@tonic-gate 	scf_iter_t *siter = NULL;
2627c478bd9Sstevel@tonic-gate 	scf_iter_t *iiter = NULL;
2637c478bd9Sstevel@tonic-gate 	char *fmri;
2647c478bd9Sstevel@tonic-gate 	ssize_t length;
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	uint64_t uint64;
2677c478bd9Sstevel@tonic-gate 	ssize_t bytes;
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	length = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH);
2707c478bd9Sstevel@tonic-gate 	if (length <= 0)
2717c478bd9Sstevel@tonic-gate 		return;
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	length++;
2747c478bd9Sstevel@tonic-gate 	fmri = alloca(length * sizeof (char));
2757c478bd9Sstevel@tonic-gate 
2763f2f09c1Sdp 	if ((h = scf_handle_create(SCF_VERSION)) == NULL)
2777c478bd9Sstevel@tonic-gate 		return;
2787c478bd9Sstevel@tonic-gate 
2793f2f09c1Sdp 	if (scf_handle_bind(h) != 0) {
2807c478bd9Sstevel@tonic-gate 		scf_handle_destroy(h);
2817c478bd9Sstevel@tonic-gate 		return;
2827c478bd9Sstevel@tonic-gate 	}
2837c478bd9Sstevel@tonic-gate 
2843f2f09c1Sdp 	if ((sc = scf_scope_create(h)) == NULL ||
2853f2f09c1Sdp 	    (svc = scf_service_create(h)) == NULL ||
2863f2f09c1Sdp 	    (inst = scf_instance_create(h)) == NULL ||
2873f2f09c1Sdp 	    (snap = scf_snapshot_create(h)) == NULL ||
2883f2f09c1Sdp 	    (pg = scf_pg_create(h)) == NULL ||
2893f2f09c1Sdp 	    (prop = scf_property_create(h)) == NULL ||
2903f2f09c1Sdp 	    (val = scf_value_create(h)) == NULL ||
2913f2f09c1Sdp 	    (siter = scf_iter_create(h)) == NULL ||
2923f2f09c1Sdp 	    (iiter = scf_iter_create(h)) == NULL)
2937c478bd9Sstevel@tonic-gate 		goto out;
2947c478bd9Sstevel@tonic-gate 
2953f2f09c1Sdp 	if (scf_handle_get_scope(h, SCF_SCOPE_LOCAL, sc) != 0)
2967c478bd9Sstevel@tonic-gate 		goto out;
2977c478bd9Sstevel@tonic-gate 
2983f2f09c1Sdp 	if (scf_iter_scope_services(siter, sc) != 0)
2997c478bd9Sstevel@tonic-gate 		goto out;
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	while (scf_iter_next_service(siter, svc) == 1) {
3027c478bd9Sstevel@tonic-gate 
3033f2f09c1Sdp 		if (scf_iter_service_instances(iiter, svc) != 0)
3047c478bd9Sstevel@tonic-gate 			continue;
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 		while (scf_iter_next_instance(iiter, inst) == 1) {
3077c478bd9Sstevel@tonic-gate 
3083f2f09c1Sdp 			if ((scf_instance_get_snapshot(inst, "running",
3093f2f09c1Sdp 			    snap)) != 0)
3103f2f09c1Sdp 				isnap = NULL;
3113f2f09c1Sdp 			else
3123f2f09c1Sdp 				isnap = snap;
3137c478bd9Sstevel@tonic-gate 
3143f2f09c1Sdp 			if (scf_instance_get_pg_composed(inst, isnap,
3153f2f09c1Sdp 			    SCF_PG_GENERAL, pg) != 0)
3167c478bd9Sstevel@tonic-gate 				continue;
3177c478bd9Sstevel@tonic-gate 
3183f2f09c1Sdp 			if (scf_pg_get_property(pg, SCF_PROPERTY_RESTARTER,
3193f2f09c1Sdp 			    prop) != 0 ||
3203f2f09c1Sdp 			    scf_property_get_value(prop, val) != 0)
3217c478bd9Sstevel@tonic-gate 				continue;
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 			bytes = scf_value_get_astring(val, fmri, length);
3247c478bd9Sstevel@tonic-gate 			if (bytes <= 0 || bytes >= length)
3257c478bd9Sstevel@tonic-gate 				continue;
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 			if (strlcat(fmri, FMRI_CONTRACT_PROP, length) >=
3287c478bd9Sstevel@tonic-gate 			    length)
3297c478bd9Sstevel@tonic-gate 				continue;
3307c478bd9Sstevel@tonic-gate 
3313f2f09c1Sdp 			if (scf_handle_decode_fmri(h, fmri, NULL, NULL,
3323f2f09c1Sdp 			    NULL, NULL, prop, SCF_DECODE_FMRI_EXACT) != 0)
3337c478bd9Sstevel@tonic-gate 				continue;
3347c478bd9Sstevel@tonic-gate 
3353f2f09c1Sdp 			if (scf_property_is_type(prop, SCF_TYPE_COUNT) != 0 ||
3363f2f09c1Sdp 			    scf_property_get_value(prop, val) != 0 ||
3373f2f09c1Sdp 			    scf_value_get_count(val, &uint64) != 0)
3387c478bd9Sstevel@tonic-gate 				continue;
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 			ctid = (ctid_t)uint64;
3417c478bd9Sstevel@tonic-gate 			if (save_ctid(ctid) == 0) {
3427c478bd9Sstevel@tonic-gate 				(void) sigsend(P_CTID, ctid, SIGSTOP);
3437c478bd9Sstevel@tonic-gate 			}
3447c478bd9Sstevel@tonic-gate 		}
3457c478bd9Sstevel@tonic-gate 	}
3467c478bd9Sstevel@tonic-gate out:
3473f2f09c1Sdp 	scf_scope_destroy(sc);
3483f2f09c1Sdp 	scf_service_destroy(svc);
3493f2f09c1Sdp 	scf_instance_destroy(inst);
3503f2f09c1Sdp 	scf_snapshot_destroy(snap);
3513f2f09c1Sdp 	scf_pg_destroy(pg);
3523f2f09c1Sdp 	scf_property_destroy(prop);
3533f2f09c1Sdp 	scf_value_destroy(val);
3543f2f09c1Sdp 	scf_iter_destroy(siter);
3553f2f09c1Sdp 	scf_iter_destroy(iiter);
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	(void) scf_handle_unbind(h);
3587c478bd9Sstevel@tonic-gate 	scf_handle_destroy(h);
3597c478bd9Sstevel@tonic-gate }
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate static void
3627c478bd9Sstevel@tonic-gate continue_delegates()
3637c478bd9Sstevel@tonic-gate {
3647c478bd9Sstevel@tonic-gate 	ctidlist_t *next;
3657c478bd9Sstevel@tonic-gate 	for (next = ctidlist; next != NULL; next = next->next)
3667c478bd9Sstevel@tonic-gate 		(void) sigsend(P_CTID, next->ctid, SIGCONT);
3677c478bd9Sstevel@tonic-gate }
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate static void
3707c478bd9Sstevel@tonic-gate stop_restarters()
3717c478bd9Sstevel@tonic-gate {
3727c478bd9Sstevel@tonic-gate 	stop_startd();
3737c478bd9Sstevel@tonic-gate 	stop_delegates();
3747c478bd9Sstevel@tonic-gate }
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate static void
3777c478bd9Sstevel@tonic-gate continue_restarters()
3787c478bd9Sstevel@tonic-gate {
3797c478bd9Sstevel@tonic-gate 	continue_startd();
3807c478bd9Sstevel@tonic-gate 	continue_delegates();
3817c478bd9Sstevel@tonic-gate }
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate /*
3847c478bd9Sstevel@tonic-gate  * Copy an array of strings into buf, separated by spaces.  Returns 0 on
3857c478bd9Sstevel@tonic-gate  * success.
3867c478bd9Sstevel@tonic-gate  */
3877c478bd9Sstevel@tonic-gate static int
3887c478bd9Sstevel@tonic-gate gather_args(char **args, char *buf, size_t buf_sz)
3897c478bd9Sstevel@tonic-gate {
3907c478bd9Sstevel@tonic-gate 	if (strlcpy(buf, *args, buf_sz) >= buf_sz)
3917c478bd9Sstevel@tonic-gate 		return (-1);
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	for (++args; *args != NULL; ++args) {
3947c478bd9Sstevel@tonic-gate 		if (strlcat(buf, " ", buf_sz) >= buf_sz)
3957c478bd9Sstevel@tonic-gate 			return (-1);
3967c478bd9Sstevel@tonic-gate 		if (strlcat(buf, *args, buf_sz) >= buf_sz)
3977c478bd9Sstevel@tonic-gate 			return (-1);
3987c478bd9Sstevel@tonic-gate 	}
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	return (0);
4017c478bd9Sstevel@tonic-gate }
4027c478bd9Sstevel@tonic-gate 
40326f665e8Sdstaff /*
40426f665e8Sdstaff  * Halt every zone on the system.  We are committed to doing a shutdown
40526f665e8Sdstaff  * even if something goes wrong here. If something goes wrong, we just
40626f665e8Sdstaff  * continue with the shutdown.  Return non-zero if we need to wait for zones to
40726f665e8Sdstaff  * halt later on.
40826f665e8Sdstaff  */
40926f665e8Sdstaff static int
4103f2f09c1Sdp halt_zones()
41126f665e8Sdstaff {
41226f665e8Sdstaff 	pid_t pid;
41326f665e8Sdstaff 	zoneid_t *zones;
4143f2f09c1Sdp 	size_t nz = 0, old_nz;
41526f665e8Sdstaff 	int i;
41626f665e8Sdstaff 	char zname[ZONENAME_MAX];
41726f665e8Sdstaff 
41826f665e8Sdstaff 	/*
41926f665e8Sdstaff 	 * Get a list of zones. If the number of zones changes in between the
42026f665e8Sdstaff 	 * two zone_list calls, try again.
42126f665e8Sdstaff 	 */
42226f665e8Sdstaff 
42326f665e8Sdstaff 	for (;;) {
42426f665e8Sdstaff 		(void) zone_list(NULL, &nz);
42526f665e8Sdstaff 		if (nz == 1)
42626f665e8Sdstaff 			return (0);
42726f665e8Sdstaff 		old_nz = nz;
42826f665e8Sdstaff 		zones = calloc(sizeof (zoneid_t), nz);
42926f665e8Sdstaff 		if (zones == NULL) {
43026f665e8Sdstaff 			(void) fprintf(stderr,
43126f665e8Sdstaff 			    gettext("%s: Could not halt zones"
4323f2f09c1Sdp 			    " (out of memory).\n"), cmdname);
43326f665e8Sdstaff 			return (0);
43426f665e8Sdstaff 		}
43526f665e8Sdstaff 
43626f665e8Sdstaff 		(void) zone_list(zones, &nz);
43726f665e8Sdstaff 		if (old_nz == nz)
43826f665e8Sdstaff 			break;
43926f665e8Sdstaff 		free(zones);
44026f665e8Sdstaff 	}
44126f665e8Sdstaff 
44226f665e8Sdstaff 	if (nz == 2) {
4433f2f09c1Sdp 		(void) fprintf(stderr, gettext("%s: Halting 1 zone.\n"),
4443f2f09c1Sdp 		    cmdname);
44526f665e8Sdstaff 	} else {
4463f2f09c1Sdp 		(void) fprintf(stderr, gettext("%s: Halting %i zones.\n"),
4473f2f09c1Sdp 		    cmdname, nz - 1);
44826f665e8Sdstaff 	}
44926f665e8Sdstaff 
45026f665e8Sdstaff 	for (i = 0; i < nz; i++) {
45126f665e8Sdstaff 		if (zones[i] == GLOBAL_ZONEID)
45226f665e8Sdstaff 			continue;
45326f665e8Sdstaff 		if (getzonenamebyid(zones[i], zname, sizeof (zname)) < 0) {
45426f665e8Sdstaff 			/*
45526f665e8Sdstaff 			 * getzonenamebyid should only fail if we raced with
45626f665e8Sdstaff 			 * another process trying to shut down the zone.
45726f665e8Sdstaff 			 * We assume this happened and ignore the error.
45826f665e8Sdstaff 			 */
45926f665e8Sdstaff 			if (errno != EINVAL) {
46026f665e8Sdstaff 				(void) fprintf(stderr,
46126f665e8Sdstaff 				    gettext("%s: Unexpected error while "
46226f665e8Sdstaff 				    "looking up zone %ul: %s.\n"),
4633f2f09c1Sdp 				    cmdname, zones[i], strerror(errno));
46426f665e8Sdstaff 			}
46526f665e8Sdstaff 
46626f665e8Sdstaff 			continue;
46726f665e8Sdstaff 		}
46826f665e8Sdstaff 		pid = fork();
46926f665e8Sdstaff 		if (pid < 0) {
47026f665e8Sdstaff 			(void) fprintf(stderr,
47126f665e8Sdstaff 			    gettext("%s: Zone \"%s\" could not be"
47226f665e8Sdstaff 			    " halted (could not fork(): %s).\n"),
4733f2f09c1Sdp 			    cmdname, zname, strerror(errno));
47426f665e8Sdstaff 			continue;
47526f665e8Sdstaff 		}
47626f665e8Sdstaff 		if (pid == 0) {
47726f665e8Sdstaff 			(void) execl(ZONEADM_PROG, ZONEADM_PROG,
47826f665e8Sdstaff 			    "-z", zname, "halt", NULL);
47926f665e8Sdstaff 			(void) fprintf(stderr,
48026f665e8Sdstaff 			    gettext("%s: Zone \"%s\" could not be halted"
48126f665e8Sdstaff 			    " (cannot exec(" ZONEADM_PROG "): %s).\n"),
4823f2f09c1Sdp 			    cmdname, zname, strerror(errno));
48326f665e8Sdstaff 			exit(0);
48426f665e8Sdstaff 		}
48526f665e8Sdstaff 	}
48626f665e8Sdstaff 
48726f665e8Sdstaff 	return (1);
48826f665e8Sdstaff }
48926f665e8Sdstaff 
49026f665e8Sdstaff /*
49126f665e8Sdstaff  * This function tries to wait for all non-global zones to go away.
49226f665e8Sdstaff  * It will timeout if no progress is made for 5 seconds, or a total of
49326f665e8Sdstaff  * 30 seconds elapses.
49426f665e8Sdstaff  */
49526f665e8Sdstaff 
49626f665e8Sdstaff static void
4973f2f09c1Sdp check_zones_haltedness()
49826f665e8Sdstaff {
49926f665e8Sdstaff 	int t = 0, t_prog = 0;
50026f665e8Sdstaff 	size_t nz = 0, last_nz;
50126f665e8Sdstaff 
50226f665e8Sdstaff 	do {
50326f665e8Sdstaff 		last_nz = nz;
50426f665e8Sdstaff 		(void) zone_list(NULL, &nz);
50526f665e8Sdstaff 		if (nz == 1)
50626f665e8Sdstaff 			return;
50726f665e8Sdstaff 
50826f665e8Sdstaff 		(void) sleep(1);
50926f665e8Sdstaff 
51026f665e8Sdstaff 		if (last_nz > nz)
51126f665e8Sdstaff 			t_prog = 0;
51226f665e8Sdstaff 
51326f665e8Sdstaff 		t++;
51426f665e8Sdstaff 		t_prog++;
51526f665e8Sdstaff 
51626f665e8Sdstaff 		if (t == 10) {
51726f665e8Sdstaff 			if (nz == 2) {
51826f665e8Sdstaff 				(void) fprintf(stderr,
51926f665e8Sdstaff 				    gettext("%s: Still waiting for 1 zone to "
52026f665e8Sdstaff 				    "halt. Will wait up to 20 seconds.\n"),
5213f2f09c1Sdp 				    cmdname);
52226f665e8Sdstaff 			} else {
52326f665e8Sdstaff 				(void) fprintf(stderr,
52426f665e8Sdstaff 				    gettext("%s: Still waiting for %i zones "
52526f665e8Sdstaff 				    "to halt. Will wait up to 20 seconds.\n"),
5263f2f09c1Sdp 				    cmdname, nz - 1);
52726f665e8Sdstaff 			}
52826f665e8Sdstaff 		}
52926f665e8Sdstaff 
53026f665e8Sdstaff 	} while ((t < 30) && (t_prog < 5));
53126f665e8Sdstaff }
53226f665e8Sdstaff 
53319397407SSherry Moore 
53419397407SSherry Moore /*
53519397407SSherry Moore  * Validate that this is a root disk or dataset
53619397407SSherry Moore  * Returns 0 if it is a root disk or dataset;
53719397407SSherry Moore  * returns 1 if it is a disk argument or dataset, but not valid or not root;
53819397407SSherry Moore  * returns -1 if it is not a valid argument or a disk argument.
53919397407SSherry Moore  */
54019397407SSherry Moore static int
54119397407SSherry Moore validate_disk(char *arg, char *mountpoint)
54219397407SSherry Moore {
54319397407SSherry Moore 	static char root_dev_path[] = "/dev/dsk";
54419397407SSherry Moore 	char kernpath[MAXPATHLEN];
54519397407SSherry Moore 	struct stat buf;
54619397407SSherry Moore 	struct stat64 statbuf;
54719397407SSherry Moore 	int rc = 0;
54819397407SSherry Moore 
54919397407SSherry Moore 	if (strlen(arg) > MAXPATHLEN) {
55019397407SSherry Moore 		(void) fprintf(stderr,
55119397407SSherry Moore 		    gettext("%s: argument is too long\n"), cmdname);
55219397407SSherry Moore 		return (-1);
55319397407SSherry Moore 	}
55419397407SSherry Moore 
55519397407SSherry Moore 	bcopy(FASTBOOT_MOUNTPOINT, mountpoint, sizeof (FASTBOOT_MOUNTPOINT));
55619397407SSherry Moore 
55719397407SSherry Moore 	/*
55819397407SSherry Moore 	 * Do a force umount just in case some other filesystem has
55919397407SSherry Moore 	 * been mounted there.
56019397407SSherry Moore 	 */
56119397407SSherry Moore 	(void) umount2(mountpoint, MS_FORCE);
56219397407SSherry Moore 
56319397407SSherry Moore 	/* Create the directory if it doesn't already exist */
56419397407SSherry Moore 	if (lstat(mountpoint, &buf) != 0) {
56519397407SSherry Moore 		if (mkdirp(mountpoint, 0755) != 0) {
56619397407SSherry Moore 			(void) fprintf(stderr,
56719397407SSherry Moore 			    gettext("failed to create mountpoint %s\n"),
56819397407SSherry Moore 			    mountpoint);
56919397407SSherry Moore 			return (-1);
57019397407SSherry Moore 		}
57119397407SSherry Moore 	}
57219397407SSherry Moore 
57319397407SSherry Moore 	if (strncmp(arg, root_dev_path, strlen(root_dev_path)) == 0) {
57419397407SSherry Moore 		/* ufs root disk argument */
57519397407SSherry Moore 		rc = validate_ufs_disk(arg, mountpoint);
57619397407SSherry Moore 	} else {
57719397407SSherry Moore 		/* zfs root pool argument */
57819397407SSherry Moore 		rc = validate_zfs_pool(arg, mountpoint);
57919397407SSherry Moore 	}
58019397407SSherry Moore 
58119397407SSherry Moore 	if (rc != 0)
58219397407SSherry Moore 		return (rc);
58319397407SSherry Moore 
58419397407SSherry Moore 	(void) snprintf(kernpath, MAXPATHLEN, "%s/platform/i86pc/kernel/unix",
58519397407SSherry Moore 	    mountpoint);
58619397407SSherry Moore 
58719397407SSherry Moore 	if (stat64(kernpath, &statbuf) != 0) {
58819397407SSherry Moore 		(void) fprintf(stderr,
58919397407SSherry Moore 		    gettext("%s: %s is not a root disk or dataset\n"),
59019397407SSherry Moore 		    cmdname, arg);
59119397407SSherry Moore 		return (1);
59219397407SSherry Moore 	}
59319397407SSherry Moore 
59419397407SSherry Moore 	return (0);
59519397407SSherry Moore }
59619397407SSherry Moore 
59719397407SSherry Moore 
59819397407SSherry Moore static int
59919397407SSherry Moore validate_ufs_disk(char *arg, char *mountpoint)
60019397407SSherry Moore {
6018e07a41fSKonstantin Ananyev 	struct ufs_args	ufs_args = { 0 };
6028e07a41fSKonstantin Ananyev 	char mntopts[MNT_LINE_MAX] = MNTOPT_LARGEFILES;
60319397407SSherry Moore 
60419397407SSherry Moore 	/* perform the mount */
6058e07a41fSKonstantin Ananyev 	ufs_args.flags = UFSMNT_LARGEFILES;
60619397407SSherry Moore 	if (mount(arg, mountpoint, MS_DATA|MS_OPTIONSTR,
6078e07a41fSKonstantin Ananyev 	    MNTTYPE_UFS, &ufs_args, sizeof (ufs_args),
6088e07a41fSKonstantin Ananyev 	    mntopts, sizeof (mntopts)) != 0) {
60919397407SSherry Moore 		perror(cmdname);
61019397407SSherry Moore 		(void) fprintf(stderr,
61119397407SSherry Moore 		    gettext("%s: failed to mount %s\n"), cmdname, arg);
61219397407SSherry Moore 		return (-1);
61319397407SSherry Moore 	}
61419397407SSherry Moore 
61519397407SSherry Moore 	return (0);
61619397407SSherry Moore }
61719397407SSherry Moore 
61819397407SSherry Moore static int
61919397407SSherry Moore validate_zfs_pool(char *arg, char *mountpoint)
62019397407SSherry Moore {
62119397407SSherry Moore 	zfs_handle_t *zhp = NULL;
62219397407SSherry Moore 	char mntopts[MNT_LINE_MAX] = { '\0' };
62319397407SSherry Moore 	int rc = 0;
62419397407SSherry Moore 
62519397407SSherry Moore 	if ((g_zfs = libzfs_init()) == NULL) {
62619397407SSherry Moore 		(void) fprintf(stderr, gettext("internal error: failed to "
62719397407SSherry Moore 		    "initialize ZFS library\n"));
62819397407SSherry Moore 		return (-1);
62919397407SSherry Moore 	}
63019397407SSherry Moore 
63119397407SSherry Moore 	/* Try to open the dataset */
63219397407SSherry Moore 	if ((zhp = zfs_open(g_zfs, arg,
63319397407SSherry Moore 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_DATASET)) == NULL)
63419397407SSherry Moore 		return (-1);
63519397407SSherry Moore 
63619397407SSherry Moore 	/* perform the mount */
63719397407SSherry Moore 	if (mount(zfs_get_name(zhp), mountpoint, MS_DATA|MS_OPTIONSTR,
63819397407SSherry Moore 	    MNTTYPE_ZFS, NULL, 0, mntopts, sizeof (mntopts)) != 0) {
63919397407SSherry Moore 		perror(cmdname);
64019397407SSherry Moore 		(void) fprintf(stderr,
64119397407SSherry Moore 		    gettext("%s: failed to mount %s\n"), cmdname, arg);
64219397407SSherry Moore 		rc = -1;
64319397407SSherry Moore 	}
64419397407SSherry Moore 
64519397407SSherry Moore validate_zfs_err_out:
64619397407SSherry Moore 	if (zhp != NULL)
64719397407SSherry Moore 		zfs_close(zhp);
64819397407SSherry Moore 
64919397407SSherry Moore 	libzfs_fini(g_zfs);
65019397407SSherry Moore 	return (rc);
65119397407SSherry Moore }
65219397407SSherry Moore 
65319397407SSherry Moore /*
65419397407SSherry Moore  * Return 0 if not zfs, or is zfs and have successfully constructed the
65519397407SSherry Moore  * boot argument; returns non-zero otherwise.
65619397407SSherry Moore  * At successful completion fpth contains pointer where mount point ends.
65719397407SSherry Moore  * NOTE: arg is supposed to be the resolved path
65819397407SSherry Moore  */
65919397407SSherry Moore static int
66019397407SSherry Moore get_zfs_bootfs_arg(const char *arg, const char ** fpth, int *is_zfs,
66119397407SSherry Moore 		char *bootfs_arg)
66219397407SSherry Moore {
66319397407SSherry Moore 	zfs_handle_t *zhp = NULL;
66419397407SSherry Moore 	zpool_handle_t *zpoolp = NULL;
66519397407SSherry Moore 	FILE *mtabp = NULL;
66619397407SSherry Moore 	struct mnttab mnt;
66719397407SSherry Moore 	char *poolname = NULL;
66819397407SSherry Moore 	char physpath[MAXNAMELEN];
66919397407SSherry Moore 	char mntsp[ZPOOL_MAXNAMELEN];
67019397407SSherry Moore 	char bootfs[ZPOOL_MAXNAMELEN];
67119397407SSherry Moore 	int rc = 0;
67219397407SSherry Moore 	size_t mntlen = 0;
67319397407SSherry Moore 	size_t msz;
67419397407SSherry Moore 
67519397407SSherry Moore 	*fpth = arg;
67619397407SSherry Moore 	*is_zfs = 0;
67719397407SSherry Moore 
67819397407SSherry Moore 	bzero(physpath, sizeof (physpath));
67919397407SSherry Moore 	bzero(bootfs, sizeof (bootfs));
68019397407SSherry Moore 
68119397407SSherry Moore 	if ((mtabp = fopen(MNTTAB, "r")) == NULL) {
68219397407SSherry Moore 		return (-1);
68319397407SSherry Moore 	}
68419397407SSherry Moore 
68519397407SSherry Moore 	while (getmntent(mtabp, &mnt) == 0) {
68619397407SSherry Moore 		if (strstr(arg, mnt.mnt_mountp) == arg &&
68719397407SSherry Moore 		    (msz = strlen(mnt.mnt_mountp)) > mntlen) {
68819397407SSherry Moore 			mntlen = msz;
68919397407SSherry Moore 			*is_zfs = strcmp(MNTTYPE_ZFS, mnt.mnt_fstype) == 0;
69019397407SSherry Moore 			(void) strlcpy(mntsp, mnt.mnt_special, sizeof (mntsp));
69119397407SSherry Moore 		}
69219397407SSherry Moore 	}
69319397407SSherry Moore 
69419397407SSherry Moore 	(void) fclose(mtabp);
69519397407SSherry Moore 
69619397407SSherry Moore 	if (mntlen > 1)
69719397407SSherry Moore 		*fpth += mntlen;
69819397407SSherry Moore 
69919397407SSherry Moore 	if (!*is_zfs)
70019397407SSherry Moore 		return (0);
70119397407SSherry Moore 
70219397407SSherry Moore 	if ((g_zfs = libzfs_init()) == NULL)
70319397407SSherry Moore 		return (-1);
70419397407SSherry Moore 
70519397407SSherry Moore 	/* Try to open the dataset */
70619397407SSherry Moore 	if ((zhp = zfs_open(g_zfs, mntsp,
70719397407SSherry Moore 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_DATASET)) == NULL) {
70819397407SSherry Moore 		(void) fprintf(stderr, gettext("cannot open %s\n"), mntsp);
70919397407SSherry Moore 		rc = -1;
71019397407SSherry Moore 		goto validate_zfs_err_out;
71119397407SSherry Moore 	}
71219397407SSherry Moore 
71319397407SSherry Moore 	(void) strlcpy(bootfs, mntsp, sizeof (bootfs));
71419397407SSherry Moore 
71519397407SSherry Moore 	if ((poolname = strtok(mntsp, "/")) == NULL) {
71619397407SSherry Moore 		rc = -1;
71719397407SSherry Moore 		goto validate_zfs_err_out;
71819397407SSherry Moore 	}
71919397407SSherry Moore 
72019397407SSherry Moore 	if ((zpoolp = zpool_open(g_zfs, poolname)) == NULL) {
72119397407SSherry Moore 		(void) fprintf(stderr, gettext("cannot open %s\n"), poolname);
72219397407SSherry Moore 		rc = -1;
72319397407SSherry Moore 		goto validate_zfs_err_out;
72419397407SSherry Moore 	}
72519397407SSherry Moore 
72619397407SSherry Moore 	if (zpool_get_physpath(zpoolp, physpath) != 0) {
72719397407SSherry Moore 		(void) fprintf(stderr, gettext("cannot find phys_path\n"));
72819397407SSherry Moore 		rc = -1;
72919397407SSherry Moore 		goto validate_zfs_err_out;
73019397407SSherry Moore 	}
73119397407SSherry Moore 
73219397407SSherry Moore 	if (zpool_set_prop(zpoolp, "bootfs", bootfs) != 0) {
73319397407SSherry Moore 		(void) fprintf(stderr, gettext("cannot set bootfs to %s\n"),
73419397407SSherry Moore 		    bootfs);
73519397407SSherry Moore 		rc = -1;
73619397407SSherry Moore 		goto validate_zfs_err_out;
73719397407SSherry Moore 	}
73819397407SSherry Moore 
73919397407SSherry Moore 	(void) snprintf(bootfs_arg, BOOTARGS_MAX,
74019397407SSherry Moore 	    "-B zfs-bootfs=%s,bootpath=\"%s\"", bootfs, physpath);
74119397407SSherry Moore 
74219397407SSherry Moore validate_zfs_err_out:
74319397407SSherry Moore 	if (zhp != NULL)
74419397407SSherry Moore 		zfs_close(zhp);
74519397407SSherry Moore 
74619397407SSherry Moore 	if (zpoolp != NULL)
74719397407SSherry Moore 		zpool_close(zpoolp);
74819397407SSherry Moore 
74919397407SSherry Moore 	libzfs_fini(g_zfs);
75019397407SSherry Moore 	return (rc);
75119397407SSherry Moore }
75219397407SSherry Moore 
75319397407SSherry Moore /*
75419397407SSherry Moore  * Validate that the file exists, and is an ELF file.
75519397407SSherry Moore  * Returns 0 on success, -1 on failure.
75619397407SSherry Moore  */
75719397407SSherry Moore static int
75819397407SSherry Moore validate_unix(char *arg, int *mplen, int *is_zfs, char *bootfs_arg,
75919397407SSherry Moore     int *failsafe)
76019397407SSherry Moore {
76119397407SSherry Moore 	const char *location;
76219397407SSherry Moore 	int class, format;
76319397407SSherry Moore 	unsigned char ident[EI_NIDENT];
76419397407SSherry Moore 	char physpath[MAXPATHLEN];
76519397407SSherry Moore 	int elffd = -1;
76619397407SSherry Moore 	size_t	sz;
76719397407SSherry Moore 
76819397407SSherry Moore 	if ((sz = resolvepath(arg, physpath, sizeof (physpath) - 1)) ==
76919397407SSherry Moore 	    (size_t)-1) {
77019397407SSherry Moore 		(void) fprintf(stderr,
77119397407SSherry Moore 		    gettext("cannot resolve path for %s: %s\n"),
77219397407SSherry Moore 		    arg, strerror(errno));
77319397407SSherry Moore 		return (-1);
77419397407SSherry Moore 	}
77519397407SSherry Moore 	(void) strlcpy(arg, physpath, sz + 1);
77619397407SSherry Moore 
77719397407SSherry Moore 	if (strlen(arg) > MAXPATHLEN) {
77819397407SSherry Moore 		(void) fprintf(stderr,
77919397407SSherry Moore 		    gettext("%s: new kernel name is too long\n"), cmdname);
78019397407SSherry Moore 		return (-1);
78119397407SSherry Moore 	}
78219397407SSherry Moore 
78319397407SSherry Moore 	if (strncmp(basename(arg), "unix", 4) != 0) {
78419397407SSherry Moore 		(void) fprintf(stderr,
78519397407SSherry Moore 		    gettext("%s: %s: kernel name must be unix\n"),
78619397407SSherry Moore 		    cmdname, arg);
78719397407SSherry Moore 		return (-1);
78819397407SSherry Moore 	}
78919397407SSherry Moore 
79019397407SSherry Moore 	if (get_zfs_bootfs_arg(arg, &location, is_zfs, bootfs_arg) != 0)
79119397407SSherry Moore 		goto err_out;
79219397407SSherry Moore 
79319397407SSherry Moore 	*mplen = location - arg;
79419397407SSherry Moore 
79519397407SSherry Moore 	if ((strstr(location, "/boot/platform")) == location)
79619397407SSherry Moore 		*failsafe = 1;
79719397407SSherry Moore 	else if ((strstr(location, "/platform")) == location)
79819397407SSherry Moore 		*failsafe = 0;
79919397407SSherry Moore 	else	{
80019397407SSherry Moore 		(void) fprintf(stderr,
80119397407SSherry Moore 		    gettext("%s: %s: no /boot/platform or /platform in"
80219397407SSherry Moore 		    " file name\n"), cmdname, arg);
80319397407SSherry Moore 			goto err_out;
80419397407SSherry Moore 	}
80519397407SSherry Moore 
80619397407SSherry Moore 	if ((elffd = open64(arg, O_RDONLY)) < 0 ||
80719397407SSherry Moore 	    (pread64(elffd, ident, EI_NIDENT, 0) != EI_NIDENT)) {
80819397407SSherry Moore 		(void) fprintf(stderr, "%s: %s: %s\n",
80919397407SSherry Moore 		    cmdname, arg, strerror(errno));
81019397407SSherry Moore 		goto err_out;
81119397407SSherry Moore 	}
81219397407SSherry Moore 
81319397407SSherry Moore 	class = ident[EI_CLASS];
81419397407SSherry Moore 
81519397407SSherry Moore 	if ((class != ELFCLASS32 && class != ELFCLASS64) ||
81619397407SSherry Moore 	    ident[EI_MAG0] != ELFMAG0 || ident[EI_MAG1] != ELFMAG1 ||
81719397407SSherry Moore 	    ident[EI_MAG2] != ELFMAG2 || ident[EI_MAG3] != ELFMAG3) {
81819397407SSherry Moore 		(void) fprintf(stderr,
81919397407SSherry Moore 		    gettext("%s: %s: not a valid ELF file\n"),
82019397407SSherry Moore 		    cmdname, arg);
82119397407SSherry Moore 		goto err_out;
82219397407SSherry Moore 	}
82319397407SSherry Moore 
82419397407SSherry Moore 	format = ident[EI_DATA];
82519397407SSherry Moore 
82619397407SSherry Moore 	if (format != CUR_ELFDATA) {
82719397407SSherry Moore 		(void) fprintf(stderr, gettext("%s: %s: invalid data format\n"),
82819397407SSherry Moore 		    cmdname, arg);
82919397407SSherry Moore 		goto err_out;
83019397407SSherry Moore 	}
83119397407SSherry Moore 
83219397407SSherry Moore 	return (0);
83319397407SSherry Moore 
83419397407SSherry Moore err_out:
83519397407SSherry Moore 	if (elffd >= 0) {
83619397407SSherry Moore 		(void) close(elffd);
83719397407SSherry Moore 		elffd = -1;
83819397407SSherry Moore 	}
83919397407SSherry Moore 	return (-1);
84019397407SSherry Moore }
84119397407SSherry Moore 
84219397407SSherry Moore #ifndef	__i386
84319397407SSherry Moore /* ARGSUSED */
84419397407SSherry Moore #endif	/* __i386 */
84519397407SSherry Moore static int
84619397407SSherry Moore is_fastboot_default(uid_t uid)
84719397407SSherry Moore {
84819397407SSherry Moore #if defined(__i386)
84919397407SSherry Moore 	int		ret;
85019397407SSherry Moore 	struct stat	st;
85119397407SSherry Moore 	static const char	fastboot_default[] = "/etc/fastreboot";
85219397407SSherry Moore 
85319397407SSherry Moore 	ret = (lstat(fastboot_default, &st) == 0 &&
85419397407SSherry Moore 	    S_ISREG(st.st_mode) &&
85519397407SSherry Moore 	    (st.st_mode & S_IRUSR) != 0 &&
85619397407SSherry Moore 	    uid == st.st_uid);
85719397407SSherry Moore 
85819397407SSherry Moore 	return (ret);
85919397407SSherry Moore #else
86019397407SSherry Moore 	return (0);
86119397407SSherry Moore #endif	/* __i386 */
86219397407SSherry Moore }
86319397407SSherry Moore 
8644e1f1c13SKonstantin Ananyev static int
8654e1f1c13SKonstantin Ananyev halt_exec(const char *path, ...)
8664e1f1c13SKonstantin Ananyev {
8674e1f1c13SKonstantin Ananyev 	pid_t		pid;
8684e1f1c13SKonstantin Ananyev 	int		i;
8694e1f1c13SKonstantin Ananyev 	int		st;
8704e1f1c13SKonstantin Ananyev 	const char	*arg;
8714e1f1c13SKonstantin Ananyev 	va_list	vp;
8724e1f1c13SKonstantin Ananyev 	const char	*argv[256];
8734e1f1c13SKonstantin Ananyev 
8744e1f1c13SKonstantin Ananyev 	if ((pid = fork()) == -1) {
8754e1f1c13SKonstantin Ananyev 		return (errno);
8764e1f1c13SKonstantin Ananyev 	} else if (pid == 0) {
8774e1f1c13SKonstantin Ananyev 		(void) fclose(stdout);
8784e1f1c13SKonstantin Ananyev 		(void) fclose(stderr);
8794e1f1c13SKonstantin Ananyev 
8804e1f1c13SKonstantin Ananyev 		argv[0] = path;
8814e1f1c13SKonstantin Ananyev 		i = 1;
8824e1f1c13SKonstantin Ananyev 
8834e1f1c13SKonstantin Ananyev 		va_start(vp, path);
8844e1f1c13SKonstantin Ananyev 
8854e1f1c13SKonstantin Ananyev 		do {
8864e1f1c13SKonstantin Ananyev 			arg = va_arg(vp, const char *);
8874e1f1c13SKonstantin Ananyev 			argv[i] = arg;
8884e1f1c13SKonstantin Ananyev 		} while (arg != NULL &&
8894e1f1c13SKonstantin Ananyev 		    ++i != sizeof (argv) / sizeof (argv[0]));
8904e1f1c13SKonstantin Ananyev 
8914e1f1c13SKonstantin Ananyev 		va_end(vp);
8924e1f1c13SKonstantin Ananyev 
8934e1f1c13SKonstantin Ananyev 		(void) execve(path, (char * const *)argv, NULL);
8944e1f1c13SKonstantin Ananyev 		(void) fprintf(stderr, gettext("cannot execute %s: %s\n"),
8954e1f1c13SKonstantin Ananyev 		    path, strerror(errno));
8964e1f1c13SKonstantin Ananyev 		exit(-1);
8974e1f1c13SKonstantin Ananyev 	} else {
8984e1f1c13SKonstantin Ananyev 		if (waitpid(pid, &st, 0) == pid &&
8994e1f1c13SKonstantin Ananyev 		    !WIFSIGNALED(st) && WIFEXITED(st))
9004e1f1c13SKonstantin Ananyev 			st = WEXITSTATUS(st);
9014e1f1c13SKonstantin Ananyev 		else
9024e1f1c13SKonstantin Ananyev 			st = -1;
9034e1f1c13SKonstantin Ananyev 	}
9044e1f1c13SKonstantin Ananyev 	return (st);
9054e1f1c13SKonstantin Ananyev }
9064e1f1c13SKonstantin Ananyev 
9074e1f1c13SKonstantin Ananyev /*
9084e1f1c13SKonstantin Ananyev  * Invokes lumount for bename.
9094e1f1c13SKonstantin Ananyev  * At successfull completion returns zero and copies contents of bename
9104e1f1c13SKonstantin Ananyev  * into mountpoint[]
9114e1f1c13SKonstantin Ananyev  */
91219397407SSherry Moore static int
91319397407SSherry Moore fastboot_bename(const char *bename, char *mountpoint, size_t mpsz)
91419397407SSherry Moore {
91519397407SSherry Moore 	int rc;
91619397407SSherry Moore 
9174e1f1c13SKonstantin Ananyev 	(void) halt_exec(LUUMOUNT_PROG, "-n", bename, NULL);
91819397407SSherry Moore 
9194e1f1c13SKonstantin Ananyev 	if ((rc = halt_exec(LUMOUNT_PROG, "-n", bename, FASTBOOT_MOUNTPOINT,
9204e1f1c13SKonstantin Ananyev 	    NULL)) != 0)
92119397407SSherry Moore 		(void) fprintf(stderr, gettext("%s: cannot mount BE %s\n"),
92219397407SSherry Moore 		    cmdname, bename);
92319397407SSherry Moore 	else
92419397407SSherry Moore 		(void) strlcpy(mountpoint, FASTBOOT_MOUNTPOINT, mpsz);
92519397407SSherry Moore 
92619397407SSherry Moore 	return (rc);
92719397407SSherry Moore }
92819397407SSherry Moore 
92919397407SSherry Moore /*
93019397407SSherry Moore  * Returns 0 on successful parsing of the arguments;
93119397407SSherry Moore  * retuens non-zero on failure.
93219397407SSherry Moore  */
93319397407SSherry Moore static int
93419397407SSherry Moore parse_fastboot_args(char *bootargs_buf, int *is_dryrun, const char *bename,
93519397407SSherry Moore     int *failsafe)
93619397407SSherry Moore {
93719397407SSherry Moore 	char mountpoint[MAXPATHLEN];
93819397407SSherry Moore 	char bootargs_saved[BOOTARGS_MAX];
93919397407SSherry Moore 	char bootargs_scratch[BOOTARGS_MAX];
94019397407SSherry Moore 	char bootfs_arg[BOOTARGS_MAX];
94119397407SSherry Moore 	char unixfile[BOOTARGS_MAX];
94219397407SSherry Moore 	char *head, *newarg;
94319397407SSherry Moore 	int buflen;		/* length of the bootargs_buf */
94419397407SSherry Moore 	int mplen;		/* length of the mount point */
94519397407SSherry Moore 	int rootlen = 0;	/* length of the root argument */
94619397407SSherry Moore 	int unixlen = 0;	/* length of the unix argument */
94719397407SSherry Moore 	int off = 0;		/* offset into the new boot argument */
94819397407SSherry Moore 	int is_zfs = 0;
94919397407SSherry Moore 	int rc = 0;
95019397407SSherry Moore 
95119397407SSherry Moore 	bzero(mountpoint, sizeof (mountpoint));
95219397407SSherry Moore 
95319397407SSherry Moore 	/*
95419397407SSherry Moore 	 * If argc is not 0, buflen is length of the argument being passed in;
95519397407SSherry Moore 	 * else it is 0 as bootargs_buf has been initialized to all 0's.
95619397407SSherry Moore 	 */
95719397407SSherry Moore 	buflen = strlen(bootargs_buf);
95819397407SSherry Moore 
95919397407SSherry Moore 	/* Save a copy of the original argument */
96019397407SSherry Moore 	bcopy(bootargs_buf, bootargs_saved, buflen);
96119397407SSherry Moore 	bzero(&bootargs_saved[buflen], sizeof (bootargs_saved) - buflen);
96219397407SSherry Moore 
96319397407SSherry Moore 	/* Save another copy to be used by strtok */
96419397407SSherry Moore 	bcopy(bootargs_buf, bootargs_scratch, buflen);
96519397407SSherry Moore 	bzero(&bootargs_scratch[buflen], sizeof (bootargs_scratch) - buflen);
96619397407SSherry Moore 	head = &bootargs_scratch[0];
96719397407SSherry Moore 
96819397407SSherry Moore 	/* Zero out the boot argument buffer as we will reconstruct it */
96919397407SSherry Moore 	bzero(bootargs_buf, BOOTARGS_MAX);
97019397407SSherry Moore 	bzero(bootfs_arg, BOOTARGS_MAX);
97119397407SSherry Moore 	bzero(unixfile, sizeof (unixfile));
97219397407SSherry Moore 
97319397407SSherry Moore 	/* Get the first argument */
97419397407SSherry Moore 	newarg = strtok(bootargs_scratch, " ");
97519397407SSherry Moore 
97619397407SSherry Moore 	/*
97719397407SSherry Moore 	 * If this is a dry run request, verify that the drivers can handle
97819397407SSherry Moore 	 * fast reboot.
97919397407SSherry Moore 	 */
98019397407SSherry Moore 	if (newarg && strncasecmp(newarg, "dryrun", strlen("dryrun")) == 0) {
98119397407SSherry Moore 		*is_dryrun = 1;
98219397407SSherry Moore 		(void) system("/usr/sbin/devfsadm");
98319397407SSherry Moore 	}
98419397407SSherry Moore 
98519397407SSherry Moore 	/*
98619397407SSherry Moore 	 * Always perform a dry run to identify all the drivers that
98719397407SSherry Moore 	 * need to implement devo_reset().
98819397407SSherry Moore 	 */
98919397407SSherry Moore 	if (uadmin(A_SHUTDOWN, AD_FASTREBOOT_DRYRUN,
99019397407SSherry Moore 	    (uintptr_t)bootargs_saved) != 0) {
99119397407SSherry Moore 		(void) fprintf(stderr, gettext("%s: not all drivers "
99219397407SSherry Moore 		    "have implemented quiesce(9E)\n"), cmdname);
99319397407SSherry Moore 	} else if (*is_dryrun) {
99419397407SSherry Moore 		(void) fprintf(stderr, gettext("%s: all drivers have "
99519397407SSherry Moore 		    "implemented quiesce(9E)\n"), cmdname);
99619397407SSherry Moore 	}
99719397407SSherry Moore 
99819397407SSherry Moore 	/*
99919397407SSherry Moore 	 * Return if it is a true dry run.
100019397407SSherry Moore 	 */
100119397407SSherry Moore 	if (*is_dryrun)
100219397407SSherry Moore 		return (rc);
100319397407SSherry Moore 
100419397407SSherry Moore 	if (bename && (rc = fastboot_bename(bename, mountpoint,
100519397407SSherry Moore 	    sizeof (mountpoint))) != 0)
100619397407SSherry Moore 		return (rc);
100719397407SSherry Moore 
100819397407SSherry Moore 	/*
100919397407SSherry Moore 	 * If BE is not specified, look for disk argument to construct
101019397407SSherry Moore 	 * mountpoint; if BE has been specified, mountpoint has already been
101119397407SSherry Moore 	 * constructed.
101219397407SSherry Moore 	 */
101319397407SSherry Moore 	if (newarg && newarg[0] != '-' && !bename) {
101419397407SSherry Moore 		int tmprc;
101519397407SSherry Moore 
101619397407SSherry Moore 		if ((tmprc = validate_disk(newarg, mountpoint)) == 0) {
101719397407SSherry Moore 			/*
101819397407SSherry Moore 			 * The first argument is a valid root argument.
101919397407SSherry Moore 			 * Get the next argument.
102019397407SSherry Moore 			 */
102119397407SSherry Moore 			newarg = strtok(NULL, " ");
102219397407SSherry Moore 			rootlen = (newarg) ? (newarg - head) : buflen;
102319397407SSherry Moore 			(void) strlcpy(fastboot_mounted, mountpoint,
102419397407SSherry Moore 			    sizeof (fastboot_mounted));
102519397407SSherry Moore 
102619397407SSherry Moore 		} else if (tmprc == -1) {
102719397407SSherry Moore 			/*
102819397407SSherry Moore 			 * Not a disk argument.  Use / as default root.
102919397407SSherry Moore 			 */
103019397407SSherry Moore 			bcopy("/", mountpoint, 1);
103119397407SSherry Moore 			bzero(&mountpoint[1], sizeof (mountpoint) - 1);
103219397407SSherry Moore 		} else {
103319397407SSherry Moore 			/*
103419397407SSherry Moore 			 * Disk argument, but not valid or not root.
103519397407SSherry Moore 			 * Return failure.
103619397407SSherry Moore 			 */
103719397407SSherry Moore 			return (EINVAL);
103819397407SSherry Moore 		}
103919397407SSherry Moore 	}
104019397407SSherry Moore 
104119397407SSherry Moore 	/*
104219397407SSherry Moore 	 * Make mountpoint the first part of unixfile.
104319397407SSherry Moore 	 * If there is not disk argument, and BE has not been specified,
104419397407SSherry Moore 	 * mountpoint could be empty.
104519397407SSherry Moore 	 */
104619397407SSherry Moore 	mplen = strlen(mountpoint);
104719397407SSherry Moore 	bcopy(mountpoint, unixfile, mplen);
104819397407SSherry Moore 
104919397407SSherry Moore 	/*
105019397407SSherry Moore 	 * Look for unix argument
105119397407SSherry Moore 	 */
105219397407SSherry Moore 	if (newarg && newarg[0] != '-') {
105319397407SSherry Moore 		bcopy(newarg, &unixfile[mplen], strlen(newarg));
105419397407SSherry Moore 		newarg = strtok(NULL, " ");
105519397407SSherry Moore 		rootlen = (newarg) ? (newarg - head) : buflen;
105619397407SSherry Moore 	} else if (mplen != 0) {
105719397407SSherry Moore 		/*
105819397407SSherry Moore 		 * No unix argument, but mountpoint is not empty, use
105919397407SSherry Moore 		 * /platform/i86pc/$ISADIR/kernel/unix as default.
106019397407SSherry Moore 		 */
106119397407SSherry Moore 		char isa[20];
106219397407SSherry Moore 
106319397407SSherry Moore 		if (sysinfo(SI_ARCHITECTURE_64, isa, sizeof (isa)) != -1)
106419397407SSherry Moore 			(void) snprintf(&unixfile[mplen],
106519397407SSherry Moore 			    sizeof (unixfile) - mplen,
106619397407SSherry Moore 			    "/platform/i86pc/kernel/%s/unix", isa);
106719397407SSherry Moore 		else if (sysinfo(SI_ARCHITECTURE_32, isa, sizeof (isa)) != -1) {
106819397407SSherry Moore 			(void) snprintf(&unixfile[mplen],
106919397407SSherry Moore 			    sizeof (unixfile) - mplen,
107019397407SSherry Moore 			    "/platform/i86pc/kernel/unix");
107119397407SSherry Moore 		} else {
107219397407SSherry Moore 			(void) fprintf(stderr,
107319397407SSherry Moore 			    gettext("%s: unknown architecture"), cmdname);
107419397407SSherry Moore 			return (EINVAL);
107519397407SSherry Moore 		}
107619397407SSherry Moore 	}
107719397407SSherry Moore 
107819397407SSherry Moore 	/*
107919397407SSherry Moore 	 * We now have the complete unix argument.  Verify that it exists and
108019397407SSherry Moore 	 * is an ELF file.  Split the argument up into mountpoint and unix
108119397407SSherry Moore 	 * portions again.  This is necessary to handle cases where mountpoint
108219397407SSherry Moore 	 * is specified on the command line as part of the unix argument,
108319397407SSherry Moore 	 * such as this:
108419397407SSherry Moore 	 *	# reboot -f /.alt/platform/i86pc/kernel/amd64/unix
108519397407SSherry Moore 	 */
108619397407SSherry Moore 	unixlen = strlen(unixfile);
108719397407SSherry Moore 	if (unixlen > 0) {
108819397407SSherry Moore 		if (validate_unix(unixfile, &mplen, &is_zfs,
108919397407SSherry Moore 		    bootfs_arg, failsafe) != 0) {
109019397407SSherry Moore 			/* Not a valid unix file */
109119397407SSherry Moore 			return (EINVAL);
109219397407SSherry Moore 		} else {
10936bc8bc6aSSherry Moore 			int space = 0;
109419397407SSherry Moore 			/*
109519397407SSherry Moore 			 * Construct boot argument.
109619397407SSherry Moore 			 */
109719397407SSherry Moore 			unixlen = strlen(unixfile);
10986bc8bc6aSSherry Moore 
10996bc8bc6aSSherry Moore 			/*
11006bc8bc6aSSherry Moore 			 * mdep cannot start with space because bootadm
11016bc8bc6aSSherry Moore 			 * creates bogus menu entries if it does.
11026bc8bc6aSSherry Moore 			 */
11036bc8bc6aSSherry Moore 			if (mplen > 0) {
11046bc8bc6aSSherry Moore 				bcopy(unixfile, bootargs_buf, mplen);
11056bc8bc6aSSherry Moore 				(void) strcat(bootargs_buf, " ");
11066bc8bc6aSSherry Moore 				space = 1;
11076bc8bc6aSSherry Moore 			}
11086bc8bc6aSSherry Moore 			bcopy(&unixfile[mplen], &bootargs_buf[mplen + space],
110919397407SSherry Moore 			    unixlen - mplen);
111019397407SSherry Moore 			(void) strcat(bootargs_buf, " ");
11116bc8bc6aSSherry Moore 			off += unixlen + space + 1;
111219397407SSherry Moore 		}
111319397407SSherry Moore 	} else {
111419397407SSherry Moore 		/* Check to see if root is zfs */
111519397407SSherry Moore 		const char	*dp;
111619397407SSherry Moore 		(void) get_zfs_bootfs_arg("/", &dp, &is_zfs, bootfs_arg);
111719397407SSherry Moore 	}
111819397407SSherry Moore 
111919397407SSherry Moore 	if (is_zfs && (buflen != 0 || bename != NULL))	{
112019397407SSherry Moore 		/* LINTED E_SEC_SPRINTF_UNBOUNDED_COPY */
112119397407SSherry Moore 		off += sprintf(bootargs_buf + off, "%s ", bootfs_arg);
112219397407SSherry Moore 	}
112319397407SSherry Moore 
112419397407SSherry Moore 	/*
112519397407SSherry Moore 	 * Copy the rest of the arguments
112619397407SSherry Moore 	 */
112719397407SSherry Moore 	bcopy(&bootargs_saved[rootlen], &bootargs_buf[off], buflen - rootlen);
112819397407SSherry Moore 
112919397407SSherry Moore 	return (rc);
113019397407SSherry Moore }
113119397407SSherry Moore 
11327c478bd9Sstevel@tonic-gate int
11337c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
11347c478bd9Sstevel@tonic-gate {
11357c478bd9Sstevel@tonic-gate 	char *ttyn = ttyname(STDERR_FILENO);
11367c478bd9Sstevel@tonic-gate 
113719397407SSherry Moore 	uid_t	euid;
11387c478bd9Sstevel@tonic-gate 	int qflag = 0, needlog = 1, nosync = 0;
113919397407SSherry Moore 	int fast_reboot = 0;
11407c478bd9Sstevel@tonic-gate 	uintptr_t mdep = NULL;
11417c478bd9Sstevel@tonic-gate 	int cmd, fcn, c, aval, r;
11427c478bd9Sstevel@tonic-gate 	const char *usage;
11438e07a41fSKonstantin Ananyev 	const char *optstring;
11447c478bd9Sstevel@tonic-gate 	zoneid_t zoneid = getzoneid();
11453f2f09c1Sdp 	int need_check_zones = 0;
11463f2f09c1Sdp 	char bootargs_buf[BOOTARGS_MAX];
114719397407SSherry Moore 	int failsafe = 0;
114819397407SSherry Moore 	char *bename = NULL;
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate 	const char * const resetting = "/etc/svc/volatile/resetting";
11517c478bd9Sstevel@tonic-gate 
11527c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
11537c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
11547c478bd9Sstevel@tonic-gate 
11553f2f09c1Sdp 	cmdname = basename(argv[0]);
11563f2f09c1Sdp 
11577c478bd9Sstevel@tonic-gate 	if (strcmp(cmdname, "halt") == 0) {
11587c478bd9Sstevel@tonic-gate 		(void) audit_halt_setup(argc, argv);
11598e07a41fSKonstantin Ananyev 		optstring = "dlnqy";
11607c478bd9Sstevel@tonic-gate 		usage = gettext("usage: %s [ -dlnqy ]\n");
11617c478bd9Sstevel@tonic-gate 		cmd = A_SHUTDOWN;
11627c478bd9Sstevel@tonic-gate 		fcn = AD_HALT;
11637c478bd9Sstevel@tonic-gate 	} else if (strcmp(cmdname, "poweroff") == 0) {
11647c478bd9Sstevel@tonic-gate 		(void) audit_halt_setup(argc, argv);
11658e07a41fSKonstantin Ananyev 		optstring = "dlnqy";
11667c478bd9Sstevel@tonic-gate 		usage = gettext("usage: %s [ -dlnqy ]\n");
11677c478bd9Sstevel@tonic-gate 		cmd = A_SHUTDOWN;
11687c478bd9Sstevel@tonic-gate 		fcn = AD_POWEROFF;
11697c478bd9Sstevel@tonic-gate 	} else if (strcmp(cmdname, "reboot") == 0) {
11707c478bd9Sstevel@tonic-gate 		(void) audit_reboot_setup();
117119397407SSherry Moore #if defined(__i386)
11728e07a41fSKonstantin Ananyev 		optstring = "dlnqfe:";
117319397407SSherry Moore 		usage = gettext("usage: %s [ -dlnqfe: ] [ boot args ]\n");
117419397407SSherry Moore #else
11758e07a41fSKonstantin Ananyev 		optstring = "dlnq";
11767c478bd9Sstevel@tonic-gate 		usage = gettext("usage: %s [ -dlnq ] [ boot args ]\n");
117719397407SSherry Moore #endif
11787c478bd9Sstevel@tonic-gate 		cmd = A_SHUTDOWN;
11797c478bd9Sstevel@tonic-gate 		fcn = AD_BOOT;
11807c478bd9Sstevel@tonic-gate 	} else {
11817c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
11827c478bd9Sstevel@tonic-gate 		    gettext("%s: not installed properly\n"), cmdname);
11837c478bd9Sstevel@tonic-gate 		return (1);
11847c478bd9Sstevel@tonic-gate 	}
11857c478bd9Sstevel@tonic-gate 
11868e07a41fSKonstantin Ananyev 	while ((c = getopt(argc, argv, optstring)) != EOF) {
11877c478bd9Sstevel@tonic-gate 		switch (c) {
11887c478bd9Sstevel@tonic-gate 		case 'd':
11897c478bd9Sstevel@tonic-gate 			if (zoneid == GLOBAL_ZONEID)
11907c478bd9Sstevel@tonic-gate 				cmd = A_DUMP;
11917c478bd9Sstevel@tonic-gate 			else {
11927c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
11937c478bd9Sstevel@tonic-gate 				    gettext("%s: -d only valid from global"
11947c478bd9Sstevel@tonic-gate 				    " zone\n"), cmdname);
11957c478bd9Sstevel@tonic-gate 				return (1);
11967c478bd9Sstevel@tonic-gate 			}
11977c478bd9Sstevel@tonic-gate 			break;
11987c478bd9Sstevel@tonic-gate 		case 'l':
11997c478bd9Sstevel@tonic-gate 			needlog = 0;
12007c478bd9Sstevel@tonic-gate 			break;
12017c478bd9Sstevel@tonic-gate 		case 'n':
12027c478bd9Sstevel@tonic-gate 			nosync = 1;
12037c478bd9Sstevel@tonic-gate 			break;
12047c478bd9Sstevel@tonic-gate 		case 'q':
12057c478bd9Sstevel@tonic-gate 			qflag = 1;
12067c478bd9Sstevel@tonic-gate 			break;
12077c478bd9Sstevel@tonic-gate 		case 'y':
12087c478bd9Sstevel@tonic-gate 			ttyn = NULL;
12097c478bd9Sstevel@tonic-gate 			break;
121019397407SSherry Moore #if defined(__i386)
121119397407SSherry Moore 		case 'f':
121219397407SSherry Moore 			fast_reboot = 1;
121319397407SSherry Moore 			break;
121419397407SSherry Moore 		case 'e':
121519397407SSherry Moore 			bename = optarg;
121619397407SSherry Moore 			break;
121719397407SSherry Moore #endif
12187c478bd9Sstevel@tonic-gate 		default:
12197c478bd9Sstevel@tonic-gate 			/*
12207c478bd9Sstevel@tonic-gate 			 * TRANSLATION_NOTE
12217c478bd9Sstevel@tonic-gate 			 * Don't translate the words "halt" or "reboot"
12227c478bd9Sstevel@tonic-gate 			 */
12237c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, usage, cmdname);
12247c478bd9Sstevel@tonic-gate 			return (1);
12257c478bd9Sstevel@tonic-gate 		}
12267c478bd9Sstevel@tonic-gate 	}
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate 	argc -= optind;
12297c478bd9Sstevel@tonic-gate 	argv += optind;
12307c478bd9Sstevel@tonic-gate 
12317c478bd9Sstevel@tonic-gate 	if (argc != 0) {
12327c478bd9Sstevel@tonic-gate 		if (fcn != AD_BOOT) {
12337c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, usage, cmdname);
12347c478bd9Sstevel@tonic-gate 			return (1);
12357c478bd9Sstevel@tonic-gate 		}
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate 		/* Gather the arguments into bootargs_buf. */
12387c478bd9Sstevel@tonic-gate 		if (gather_args(argv, bootargs_buf, sizeof (bootargs_buf)) !=
12397c478bd9Sstevel@tonic-gate 		    0) {
12407c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
12417c478bd9Sstevel@tonic-gate 			    gettext("%s: Boot arguments too long.\n"), cmdname);
12427c478bd9Sstevel@tonic-gate 			return (1);
12437c478bd9Sstevel@tonic-gate 		}
124419397407SSherry Moore 
12457c478bd9Sstevel@tonic-gate 		mdep = (uintptr_t)bootargs_buf;
124619397407SSherry Moore 	} else {
124719397407SSherry Moore 		/*
124819397407SSherry Moore 		 * Initialize it to 0 in case of fastboot, the buffer
124919397407SSherry Moore 		 * will be used.
125019397407SSherry Moore 		 */
125119397407SSherry Moore 		bzero(bootargs_buf, sizeof (bootargs_buf));
12527c478bd9Sstevel@tonic-gate 	}
12537c478bd9Sstevel@tonic-gate 
125419397407SSherry Moore 	if ((euid = geteuid()) != 0) {
12557c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
12567c478bd9Sstevel@tonic-gate 		    gettext("%s: permission denied\n"), cmdname);
12577c478bd9Sstevel@tonic-gate 		goto fail;
12587c478bd9Sstevel@tonic-gate 	}
12597c478bd9Sstevel@tonic-gate 
126019397407SSherry Moore 	/*
126119397407SSherry Moore 	 * Check whether fast  reboot is the default operating mode
126219397407SSherry Moore 	 */
12638e07a41fSKonstantin Ananyev 	if (fcn == AD_BOOT && !fast_reboot)
126419397407SSherry Moore 		fast_reboot = is_fastboot_default(euid);
126519397407SSherry Moore 
126619397407SSherry Moore 	if (bename && !fast_reboot)	{
126719397407SSherry Moore 		(void) fprintf(stderr, gettext("%s: -e only valid with -f\n"),
126819397407SSherry Moore 		    cmdname);
126919397407SSherry Moore 		return (EINVAL);
127019397407SSherry Moore 	}
127119397407SSherry Moore 
127219397407SSherry Moore 
127319397407SSherry Moore 	/*
127419397407SSherry Moore 	 * If fast reboot, do some sanity check on the argument
127519397407SSherry Moore 	 */
127619397407SSherry Moore 	if (fast_reboot) {
127719397407SSherry Moore 		int rc;
127819397407SSherry Moore 		int is_dryrun = 0;
127919397407SSherry Moore 
128019397407SSherry Moore 		if (zoneid != GLOBAL_ZONEID)	{
128119397407SSherry Moore 			(void) fprintf(stderr,
128219397407SSherry Moore 			    gettext("%s: fast reboot only valid from global"
128319397407SSherry Moore 			    " zone\n"), cmdname);
128419397407SSherry Moore 			return (EINVAL);
128519397407SSherry Moore 		}
128619397407SSherry Moore 
128719397407SSherry Moore 		rc = parse_fastboot_args(bootargs_buf, &is_dryrun,
128819397407SSherry Moore 		    bename, &failsafe);
128919397407SSherry Moore 
129019397407SSherry Moore 		/*
129119397407SSherry Moore 		 * If dry run, or if arguments are invalid, return.
129219397407SSherry Moore 		 */
129319397407SSherry Moore 		if (is_dryrun)
129419397407SSherry Moore 			return (rc);
129519397407SSherry Moore 		else if (rc != 0)
129619397407SSherry Moore 			goto fail;
129719397407SSherry Moore 
129819397407SSherry Moore 		/*
129919397407SSherry Moore 		 * For all the other errors, we continue on in case user
130019397407SSherry Moore 		 * user want to force fast reboot.
130119397407SSherry Moore 		 */
130219397407SSherry Moore 		if (strlen(bootargs_buf) != 0)
130319397407SSherry Moore 			mdep = (uintptr_t)bootargs_buf;
130419397407SSherry Moore 	}
130519397407SSherry Moore 
130619397407SSherry Moore #if 0	/* For debugging */
130719397407SSherry Moore 	if (mdep != NULL)
130819397407SSherry Moore 		(void) fprintf(stderr, "mdep = %s\n", (char *)mdep);
130919397407SSherry Moore #endif
131019397407SSherry Moore 
13117c478bd9Sstevel@tonic-gate 	if (fcn != AD_BOOT && ttyn != NULL &&
13127c478bd9Sstevel@tonic-gate 	    strncmp(ttyn, "/dev/term/", strlen("/dev/term/")) == 0) {
13137c478bd9Sstevel@tonic-gate 		/*
13147c478bd9Sstevel@tonic-gate 		 * TRANSLATION_NOTE
13157c478bd9Sstevel@tonic-gate 		 * Don't translate ``halt -y''
13167c478bd9Sstevel@tonic-gate 		 */
13177c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
13187c478bd9Sstevel@tonic-gate 		    gettext("%s: dangerous on a dialup;"), cmdname);
13197c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
13207c478bd9Sstevel@tonic-gate 		    gettext("use ``%s -y'' if you are really sure\n"), cmdname);
13217c478bd9Sstevel@tonic-gate 		goto fail;
13227c478bd9Sstevel@tonic-gate 	}
13237c478bd9Sstevel@tonic-gate 
13247c478bd9Sstevel@tonic-gate 	if (needlog) {
13257c478bd9Sstevel@tonic-gate 		char *user = getlogin();
13267c478bd9Sstevel@tonic-gate 		struct passwd *pw;
1327f040a7a6Ssetje 		char *tty;
13287c478bd9Sstevel@tonic-gate 
13297c478bd9Sstevel@tonic-gate 		openlog(cmdname, 0, LOG_AUTH);
13307c478bd9Sstevel@tonic-gate 		if (user == NULL && (pw = getpwuid(getuid())) != NULL)
13317c478bd9Sstevel@tonic-gate 			user = pw->pw_name;
13327c478bd9Sstevel@tonic-gate 		if (user == NULL)
13337c478bd9Sstevel@tonic-gate 			user = "root";
1334f040a7a6Ssetje 
1335f040a7a6Ssetje 		tty = ttyname(1);
1336f040a7a6Ssetje 
1337f040a7a6Ssetje 		if (tty == NULL)
1338f040a7a6Ssetje 			syslog(LOG_CRIT, "initiated by %s", user);
1339f040a7a6Ssetje 		else
1340f040a7a6Ssetje 			syslog(LOG_CRIT, "initiated by %s on %s", user, tty);
13417c478bd9Sstevel@tonic-gate 	}
13427c478bd9Sstevel@tonic-gate 
13437c478bd9Sstevel@tonic-gate 	/*
13447c478bd9Sstevel@tonic-gate 	 * We must assume success and log it before auditd is terminated.
13457c478bd9Sstevel@tonic-gate 	 */
13467c478bd9Sstevel@tonic-gate 	if (fcn == AD_BOOT)
13477c478bd9Sstevel@tonic-gate 		aval = audit_reboot_success();
13487c478bd9Sstevel@tonic-gate 	else
13497c478bd9Sstevel@tonic-gate 		aval = audit_halt_success();
13507c478bd9Sstevel@tonic-gate 
13517c478bd9Sstevel@tonic-gate 	if (aval == -1) {
13527c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
13537c478bd9Sstevel@tonic-gate 		    gettext("%s: can't turn off auditd\n"), cmdname);
13547c478bd9Sstevel@tonic-gate 		if (needlog)
13557c478bd9Sstevel@tonic-gate 			(void) sleep(5); /* Give syslogd time to record this */
13567c478bd9Sstevel@tonic-gate 	}
13577c478bd9Sstevel@tonic-gate 
13587c478bd9Sstevel@tonic-gate 	(void) signal(SIGHUP, SIG_IGN);	/* for remote connections */
13597c478bd9Sstevel@tonic-gate 
136026f665e8Sdstaff 	/*
136126f665e8Sdstaff 	 * We start to fork a bunch of zoneadms to halt any active zones.
136226f665e8Sdstaff 	 * This will proceed with halt in parallel until we call
136326f665e8Sdstaff 	 * check_zone_haltedness later on.
136426f665e8Sdstaff 	 */
136526f665e8Sdstaff 	if (zoneid == GLOBAL_ZONEID && cmd != A_DUMP) {
13663f2f09c1Sdp 		need_check_zones = halt_zones();
136726f665e8Sdstaff 	}
136826f665e8Sdstaff 
13697c478bd9Sstevel@tonic-gate 	/* sync boot archive in the global zone */
13703f2f09c1Sdp 	if (zoneid == GLOBAL_ZONEID && !nosync) {
1371*3a0fa635SEnrico Perla - Sun Microsystems 		char *fast_argv[] = {"/sbin/bootadm", "-a", "update_all",
1372*3a0fa635SEnrico Perla - Sun Microsystems 		    "fastboot", NULL};
1373*3a0fa635SEnrico Perla - Sun Microsystems 		char *b_argv[] = {"/sbin/bootadm", "-a", "update_all", NULL};
1374*3a0fa635SEnrico Perla - Sun Microsystems 
137519397407SSherry Moore 		if (fast_reboot)
1376*3a0fa635SEnrico Perla - Sun Microsystems 			r = posix_spawn(NULL, fast_argv[0], NULL, NULL,
1377*3a0fa635SEnrico Perla - Sun Microsystems 			    fast_argv, NULL);
1378*3a0fa635SEnrico Perla - Sun Microsystems 		else
1379*3a0fa635SEnrico Perla - Sun Microsystems 			r = posix_spawn(NULL, b_argv[0], NULL, NULL, b_argv,
1380*3a0fa635SEnrico Perla - Sun Microsystems 			    NULL);
1381*3a0fa635SEnrico Perla - Sun Microsystems 
1382*3a0fa635SEnrico Perla - Sun Microsystems 	/* if posix_spawn fails we emit a warning and continue rebooting */
1383*3a0fa635SEnrico Perla - Sun Microsystems 
1384*3a0fa635SEnrico Perla - Sun Microsystems 		if (r != 0)
1385*3a0fa635SEnrico Perla - Sun Microsystems 			(void) fprintf(stderr, gettext("%s: WARNING, unable to"
1386*3a0fa635SEnrico Perla - Sun Microsystems 			    "start boot archive update\n"), cmdname);
138719397407SSherry Moore 		else
1388*3a0fa635SEnrico Perla - Sun Microsystems 			(void) wait(NULL);
13897c478bd9Sstevel@tonic-gate 	}
13907c478bd9Sstevel@tonic-gate 
13917c478bd9Sstevel@tonic-gate 	/*
13927c478bd9Sstevel@tonic-gate 	 * If we're not forcing a crash dump, mark the system as quiescing for
13937c478bd9Sstevel@tonic-gate 	 * smf(5)'s benefit, and idle the init process.
13947c478bd9Sstevel@tonic-gate 	 */
13957c478bd9Sstevel@tonic-gate 	if (cmd != A_DUMP) {
13963f2f09c1Sdp 		if (direct_init(PCDSTOP) == -1) {
13977c478bd9Sstevel@tonic-gate 			/*
13987c478bd9Sstevel@tonic-gate 			 * TRANSLATION_NOTE
13997c478bd9Sstevel@tonic-gate 			 * Don't translate the word "init"
14007c478bd9Sstevel@tonic-gate 			 */
14017c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
14027c478bd9Sstevel@tonic-gate 			    gettext("%s: can't idle init\n"), cmdname);
14037c478bd9Sstevel@tonic-gate 			goto fail;
14047c478bd9Sstevel@tonic-gate 		}
14057c478bd9Sstevel@tonic-gate 
14067c478bd9Sstevel@tonic-gate 		if (creat(resetting, 0755) == -1)
14077c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
14087c478bd9Sstevel@tonic-gate 			    gettext("%s: could not create %s.\n"),
14097c478bd9Sstevel@tonic-gate 			    cmdname, resetting);
14107c478bd9Sstevel@tonic-gate 
14117c478bd9Sstevel@tonic-gate 		/*
14127c478bd9Sstevel@tonic-gate 		 * Stop all restarters so they do not try to restart services
14137c478bd9Sstevel@tonic-gate 		 * that are terminated.
14147c478bd9Sstevel@tonic-gate 		 */
14157c478bd9Sstevel@tonic-gate 		stop_restarters();
141626f665e8Sdstaff 
141726f665e8Sdstaff 		/*
141826f665e8Sdstaff 		 * Wait a little while for zones to shutdown.
141926f665e8Sdstaff 		 */
142026f665e8Sdstaff 		if (need_check_zones) {
14213f2f09c1Sdp 			check_zones_haltedness();
142226f665e8Sdstaff 
142326f665e8Sdstaff 			(void) fprintf(stderr,
142426f665e8Sdstaff 			    gettext("%s: Completing system halt.\n"),
142526f665e8Sdstaff 			    cmdname);
142626f665e8Sdstaff 		}
14277c478bd9Sstevel@tonic-gate 	}
14287c478bd9Sstevel@tonic-gate 
14297c478bd9Sstevel@tonic-gate 	/*
14307c478bd9Sstevel@tonic-gate 	 * Make sure we don't get stopped by a jobcontrol shell
14317c478bd9Sstevel@tonic-gate 	 * once we start killing everybody.
14327c478bd9Sstevel@tonic-gate 	 */
14337c478bd9Sstevel@tonic-gate 	(void) signal(SIGTSTP, SIG_IGN);
14347c478bd9Sstevel@tonic-gate 	(void) signal(SIGTTIN, SIG_IGN);
14357c478bd9Sstevel@tonic-gate 	(void) signal(SIGTTOU, SIG_IGN);
14367c478bd9Sstevel@tonic-gate 	(void) signal(SIGTERM, SIG_IGN);
14377c478bd9Sstevel@tonic-gate 
14387c478bd9Sstevel@tonic-gate 	/*
14397c478bd9Sstevel@tonic-gate 	 * If we're not forcing a crash dump, give everyone 5 seconds to
14407c478bd9Sstevel@tonic-gate 	 * handle a SIGTERM and clean up properly.
14417c478bd9Sstevel@tonic-gate 	 */
14427c478bd9Sstevel@tonic-gate 	if (cmd != A_DUMP) {
14437c478bd9Sstevel@tonic-gate 		(void) kill(-1, SIGTERM);
14447c478bd9Sstevel@tonic-gate 		(void) sleep(5);
14457c478bd9Sstevel@tonic-gate 	}
14467c478bd9Sstevel@tonic-gate 
1447*3a0fa635SEnrico Perla - Sun Microsystems 	(void) signal(SIGINT, SIG_IGN);
1448*3a0fa635SEnrico Perla - Sun Microsystems 
14497c478bd9Sstevel@tonic-gate 	if (!qflag && !nosync) {
14507c478bd9Sstevel@tonic-gate 		struct utmpx wtmpx;
14517c478bd9Sstevel@tonic-gate 
14527c478bd9Sstevel@tonic-gate 		bzero(&wtmpx, sizeof (struct utmpx));
14537c478bd9Sstevel@tonic-gate 		(void) strcpy(wtmpx.ut_line, "~");
14547c478bd9Sstevel@tonic-gate 		(void) time(&wtmpx.ut_tv.tv_sec);
14557c478bd9Sstevel@tonic-gate 
14567c478bd9Sstevel@tonic-gate 		if (cmd == A_DUMP)
14577c478bd9Sstevel@tonic-gate 			(void) strcpy(wtmpx.ut_name, "crash dump");
14587c478bd9Sstevel@tonic-gate 		else
14597c478bd9Sstevel@tonic-gate 			(void) strcpy(wtmpx.ut_name, "shutdown");
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate 		(void) updwtmpx(WTMPX_FILE, &wtmpx);
14627c478bd9Sstevel@tonic-gate 		sync();
14637c478bd9Sstevel@tonic-gate 	}
14647c478bd9Sstevel@tonic-gate 
14657c478bd9Sstevel@tonic-gate 	if (cmd == A_DUMP && nosync != 0)
14667c478bd9Sstevel@tonic-gate 		(void) uadmin(A_DUMP, AD_NOSYNC, NULL);
14677c478bd9Sstevel@tonic-gate 
146819397407SSherry Moore 	if (fast_reboot) {
146919397407SSherry Moore 		if (failsafe)
147019397407SSherry Moore 			(void) fprintf(stderr, "Fast reboot - failsafe.\n");
147119397407SSherry Moore 		else
147219397407SSherry Moore 			(void) fprintf(stderr, "Fast reboot.\n");
147319397407SSherry Moore 
147419397407SSherry Moore 		fcn = AD_FASTREBOOT;
147519397407SSherry Moore 	}
147619397407SSherry Moore 
14773f2f09c1Sdp 	if (uadmin(cmd, fcn, mdep) == -1)
14783f2f09c1Sdp 		(void) fprintf(stderr, "%s: uadmin failed: %s\n",
14793f2f09c1Sdp 		    cmdname, strerror(errno));
14803f2f09c1Sdp 	else
14813f2f09c1Sdp 		(void) fprintf(stderr, "%s: uadmin unexpectedly returned 0\n",
14823f2f09c1Sdp 		    cmdname);
14833f2f09c1Sdp 
14843f2f09c1Sdp 	do {
14857c478bd9Sstevel@tonic-gate 		r = remove(resetting);
14863f2f09c1Sdp 	} while (r != 0 && errno == EINTR);
14873f2f09c1Sdp 
14887c478bd9Sstevel@tonic-gate 	if (r != 0 && errno != ENOENT)
14897c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: could not remove %s.\n"),
14907c478bd9Sstevel@tonic-gate 		    cmdname, resetting);
14917c478bd9Sstevel@tonic-gate 
14923f2f09c1Sdp 	if (direct_init(PCRUN) == -1) {
14933f2f09c1Sdp 		/*
14943f2f09c1Sdp 		 * TRANSLATION_NOTE
14953f2f09c1Sdp 		 * Don't translate the word "init"
14963f2f09c1Sdp 		 */
14973f2f09c1Sdp 		(void) fprintf(stderr,
14983f2f09c1Sdp 		    gettext("%s: can't resume init\n"), cmdname);
14993f2f09c1Sdp 	}
15003f2f09c1Sdp 
15017c478bd9Sstevel@tonic-gate 	continue_restarters();
15027c478bd9Sstevel@tonic-gate 
15033f2f09c1Sdp 	if (get_initpid() != -1)
15047c478bd9Sstevel@tonic-gate 		/* tell init to restate current level */
15053f2f09c1Sdp 		(void) kill(get_initpid(), SIGHUP);
15067c478bd9Sstevel@tonic-gate 
15077c478bd9Sstevel@tonic-gate fail:
15087c478bd9Sstevel@tonic-gate 	if (fcn == AD_BOOT)
15097c478bd9Sstevel@tonic-gate 		(void) audit_reboot_fail();
15107c478bd9Sstevel@tonic-gate 	else
15117c478bd9Sstevel@tonic-gate 		(void) audit_halt_fail();
15127c478bd9Sstevel@tonic-gate 
151319397407SSherry Moore 	if (fast_reboot) {
151419397407SSherry Moore 		if (bename) {
15154e1f1c13SKonstantin Ananyev 			(void) halt_exec(LUUMOUNT_PROG, "-n", bename, NULL);
151619397407SSherry Moore 
151719397407SSherry Moore 		} else if (strlen(fastboot_mounted) != 0) {
151819397407SSherry Moore 			(void) umount(fastboot_mounted);
151919397407SSherry Moore 		}
152019397407SSherry Moore 	}
152119397407SSherry Moore 
15227c478bd9Sstevel@tonic-gate 	return (1);
15237c478bd9Sstevel@tonic-gate }
1524