xref: /titanic_53/usr/src/cmd/halt/halt.c (revision 753a6d457b330b1b29b2d3eefcd0831116ce950d)
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 /*
2248847494SEnrico Perla - Sun Microsystems  * Copyright 2009 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>
63*753a6d45SSherry Moore #include <libscf_priv.h>
6419397407SSherry Moore #include <limits.h>
657c478bd9Sstevel@tonic-gate #include <locale.h>
667c478bd9Sstevel@tonic-gate #include <libintl.h>
677c478bd9Sstevel@tonic-gate #include <syslog.h>
687c478bd9Sstevel@tonic-gate #include <signal.h>
697c478bd9Sstevel@tonic-gate #include <strings.h>
707c478bd9Sstevel@tonic-gate #include <unistd.h>
717c478bd9Sstevel@tonic-gate #include <stdlib.h>
727c478bd9Sstevel@tonic-gate #include <stdio.h>
737c478bd9Sstevel@tonic-gate #include <strings.h>
747c478bd9Sstevel@tonic-gate #include <time.h>
754e1f1c13SKonstantin Ananyev #include <wait.h>
764e1f1c13SKonstantin Ananyev #include <ctype.h>
777c478bd9Sstevel@tonic-gate #include <utmpx.h>
787c478bd9Sstevel@tonic-gate #include <pwd.h>
797c478bd9Sstevel@tonic-gate #include <zone.h>
803a0fa635SEnrico Perla - Sun Microsystems #include <spawn.h>
8119397407SSherry Moore 
8219397407SSherry Moore #include <libzfs.h>
83*753a6d45SSherry Moore #if defined(__i386)
84*753a6d45SSherry Moore #include <libgrubmgmt.h>
85*753a6d45SSherry Moore #endif
8619397407SSherry Moore 
877c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
887c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"
897c478bd9Sstevel@tonic-gate #endif
907c478bd9Sstevel@tonic-gate 
9119397407SSherry Moore #if defined(__sparc)
9219397407SSherry Moore #define	CUR_ELFDATA	ELFDATA2MSB
9319397407SSherry Moore #elif defined(__i386)
9419397407SSherry Moore #define	CUR_ELFDATA	ELFDATA2LSB
9519397407SSherry Moore #endif
9619397407SSherry Moore 
9719397407SSherry Moore static libzfs_handle_t *g_zfs;
9819397407SSherry Moore 
997c478bd9Sstevel@tonic-gate extern int audit_halt_setup(int, char **);
1007c478bd9Sstevel@tonic-gate extern int audit_halt_success(void);
1017c478bd9Sstevel@tonic-gate extern int audit_halt_fail(void);
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate extern int audit_reboot_setup(void);
1047c478bd9Sstevel@tonic-gate extern int audit_reboot_success(void);
1057c478bd9Sstevel@tonic-gate extern int audit_reboot_fail(void);
1067c478bd9Sstevel@tonic-gate 
1073f2f09c1Sdp static char *cmdname;	/* basename(argv[0]), the name of the command */
1083f2f09c1Sdp 
1097c478bd9Sstevel@tonic-gate typedef struct ctidlist_struct {
1107c478bd9Sstevel@tonic-gate 	ctid_t ctid;
1117c478bd9Sstevel@tonic-gate 	struct ctidlist_struct *next;
1127c478bd9Sstevel@tonic-gate } ctidlist_t;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate static ctidlist_t *ctidlist = NULL;
1157c478bd9Sstevel@tonic-gate static ctid_t startdct = -1;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate #define	FMRI_STARTD_CONTRACT \
1187c478bd9Sstevel@tonic-gate 	"svc:/system/svc/restarter:default/:properties/restarter/contract"
1197c478bd9Sstevel@tonic-gate 
12026f665e8Sdstaff #define	ZONEADM_PROG "/usr/sbin/zoneadm"
12126f665e8Sdstaff 
1224e1f1c13SKonstantin Ananyev #define	LUUMOUNT_PROG	"/usr/sbin/luumount"
1234e1f1c13SKonstantin Ananyev #define	LUMOUNT_PROG	"/usr/sbin/lumount"
1244e1f1c13SKonstantin Ananyev 
125*753a6d45SSherry Moore #define	BOOTADM_PROG	"/sbin/bootadm"
12619397407SSherry Moore /*
12719397407SSherry Moore  * The length of FASTBOOT_MOUNTPOINT must be less than MAXPATHLEN.
12819397407SSherry Moore  */
12919397407SSherry Moore #define	FASTBOOT_MOUNTPOINT	"/tmp/.fastboot.root"
13019397407SSherry Moore 
131*753a6d45SSherry Moore /*
132*753a6d45SSherry Moore  * Fast Reboot related variables
133*753a6d45SSherry Moore  */
13419397407SSherry Moore static char	fastboot_mounted[MAXPATHLEN];
135*753a6d45SSherry Moore #if defined(__i386)
136*753a6d45SSherry Moore static grub_boot_args_t	fbarg;
137*753a6d45SSherry Moore static grub_boot_args_t	*fbarg_used;
138*753a6d45SSherry Moore static int fbarg_entnum = GRUB_ENTRY_DEFAULT;
139*753a6d45SSherry Moore #endif	/* __i386 */
14019397407SSherry Moore 
14119397407SSherry Moore static int validate_ufs_disk(char *, char *);
14219397407SSherry Moore static int validate_zfs_pool(char *, char *);
14319397407SSherry Moore 
1443f2f09c1Sdp static pid_t
1453f2f09c1Sdp get_initpid()
1463f2f09c1Sdp {
1473f2f09c1Sdp 	static int init_pid = -1;
1483f2f09c1Sdp 
1493f2f09c1Sdp 	if (init_pid == -1) {
1503f2f09c1Sdp 		if (zone_getattr(getzoneid(), ZONE_ATTR_INITPID, &init_pid,
1513f2f09c1Sdp 		    sizeof (init_pid)) != sizeof (init_pid)) {
1523f2f09c1Sdp 			assert(errno == ESRCH);
1533f2f09c1Sdp 			init_pid = -1;
1543f2f09c1Sdp 		}
1553f2f09c1Sdp 	}
1563f2f09c1Sdp 	return (init_pid);
1573f2f09c1Sdp }
1583f2f09c1Sdp 
1593f2f09c1Sdp /*
1603f2f09c1Sdp  * Quiesce or resume init using /proc.  When stopping init, we can't send
1613f2f09c1Sdp  * SIGTSTP (since init ignores it) or SIGSTOP (since the kernel won't permit
1623f2f09c1Sdp  * it).
1633f2f09c1Sdp  */
1643f2f09c1Sdp static int
1653f2f09c1Sdp direct_init(long command)
1663f2f09c1Sdp {
1673f2f09c1Sdp 	char ctlfile[MAXPATHLEN];
1683f2f09c1Sdp 	pid_t pid;
1693f2f09c1Sdp 	int ctlfd;
1703f2f09c1Sdp 
1713f2f09c1Sdp 	assert(command == PCDSTOP || command == PCRUN);
1723f2f09c1Sdp 	if ((pid = get_initpid()) == -1) {
1733f2f09c1Sdp 		return (-1);
1743f2f09c1Sdp 	}
1753f2f09c1Sdp 
1763f2f09c1Sdp 	(void) snprintf(ctlfile, sizeof (ctlfile), "/proc/%d/ctl", pid);
1773f2f09c1Sdp 	if ((ctlfd = open(ctlfile, O_WRONLY)) == -1)
1783f2f09c1Sdp 		return (-1);
1793f2f09c1Sdp 
1803f2f09c1Sdp 	if (command == PCDSTOP) {
1813f2f09c1Sdp 		if (write(ctlfd, &command, sizeof (long)) == -1) {
1823f2f09c1Sdp 			(void) close(ctlfd);
1833f2f09c1Sdp 			return (-1);
1843f2f09c1Sdp 		}
1853f2f09c1Sdp 	} else {	/* command == PCRUN */
1863f2f09c1Sdp 		long cmds[2];
1873f2f09c1Sdp 		cmds[0] = command;
1883f2f09c1Sdp 		cmds[1] = 0;
1893f2f09c1Sdp 		if (write(ctlfd, cmds, sizeof (cmds)) == -1) {
1903f2f09c1Sdp 			(void) close(ctlfd);
1913f2f09c1Sdp 			return (-1);
1923f2f09c1Sdp 		}
1933f2f09c1Sdp 	}
1943f2f09c1Sdp 	(void) close(ctlfd);
1953f2f09c1Sdp 	return (0);
1963f2f09c1Sdp }
1973f2f09c1Sdp 
1987c478bd9Sstevel@tonic-gate static void
1997c478bd9Sstevel@tonic-gate stop_startd()
2007c478bd9Sstevel@tonic-gate {
2017c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
2027c478bd9Sstevel@tonic-gate 	scf_property_t *prop = NULL;
2037c478bd9Sstevel@tonic-gate 	scf_value_t *val = NULL;
2047c478bd9Sstevel@tonic-gate 	uint64_t uint64;
2057c478bd9Sstevel@tonic-gate 
2063f2f09c1Sdp 	if ((h = scf_handle_create(SCF_VERSION)) == NULL)
2077c478bd9Sstevel@tonic-gate 		return;
2087c478bd9Sstevel@tonic-gate 
2093f2f09c1Sdp 	if ((scf_handle_bind(h) != 0) ||
2103f2f09c1Sdp 	    ((prop = scf_property_create(h)) == NULL) ||
2113f2f09c1Sdp 	    ((val = scf_value_create(h)) == NULL))
2127c478bd9Sstevel@tonic-gate 		goto out;
2137c478bd9Sstevel@tonic-gate 
2143f2f09c1Sdp 	if (scf_handle_decode_fmri(h, FMRI_STARTD_CONTRACT,
2153f2f09c1Sdp 	    NULL, NULL, NULL, NULL, prop, SCF_DECODE_FMRI_EXACT) != 0)
2167c478bd9Sstevel@tonic-gate 		goto out;
2177c478bd9Sstevel@tonic-gate 
2183f2f09c1Sdp 	if (scf_property_is_type(prop, SCF_TYPE_COUNT) != 0 ||
2193f2f09c1Sdp 	    scf_property_get_value(prop, val) != 0 ||
2203f2f09c1Sdp 	    scf_value_get_count(val, &uint64) != 0)
2217c478bd9Sstevel@tonic-gate 		goto out;
2227c478bd9Sstevel@tonic-gate 
2233f2f09c1Sdp 	startdct = (ctid_t)uint64;
2243f2f09c1Sdp 	(void) sigsend(P_CTID, startdct, SIGSTOP);
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate out:
2277c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
2287c478bd9Sstevel@tonic-gate 	scf_value_destroy(val);
2297c478bd9Sstevel@tonic-gate 	scf_handle_destroy(h);
2307c478bd9Sstevel@tonic-gate }
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate static void
2337c478bd9Sstevel@tonic-gate continue_startd()
2347c478bd9Sstevel@tonic-gate {
2357c478bd9Sstevel@tonic-gate 	if (startdct != -1)
2367c478bd9Sstevel@tonic-gate 		(void) sigsend(P_CTID, startdct, SIGCONT);
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate #define	FMRI_RESTARTER_PROP "/:properties/general/restarter"
2407c478bd9Sstevel@tonic-gate #define	FMRI_CONTRACT_PROP "/:properties/restarter/contract"
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate static int
2437c478bd9Sstevel@tonic-gate save_ctid(ctid_t ctid)
2447c478bd9Sstevel@tonic-gate {
2457c478bd9Sstevel@tonic-gate 	ctidlist_t *next;
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	for (next = ctidlist; next != NULL; next = next->next)
2487c478bd9Sstevel@tonic-gate 		if (next->ctid == ctid)
2497c478bd9Sstevel@tonic-gate 			return (-1);
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	next = (ctidlist_t *)malloc(sizeof (ctidlist_t));
2527c478bd9Sstevel@tonic-gate 	if (next == NULL)
2537c478bd9Sstevel@tonic-gate 		return (-1);
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	next->ctid = ctid;
2567c478bd9Sstevel@tonic-gate 	next->next = ctidlist;
2577c478bd9Sstevel@tonic-gate 	ctidlist = next;
2587c478bd9Sstevel@tonic-gate 	return (0);
2597c478bd9Sstevel@tonic-gate }
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate static void
2627c478bd9Sstevel@tonic-gate stop_delegates()
2637c478bd9Sstevel@tonic-gate {
2647c478bd9Sstevel@tonic-gate 	ctid_t ctid;
2657c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
2667c478bd9Sstevel@tonic-gate 	scf_scope_t *sc = NULL;
2677c478bd9Sstevel@tonic-gate 	scf_service_t *svc = NULL;
2687c478bd9Sstevel@tonic-gate 	scf_instance_t *inst = NULL;
2697c478bd9Sstevel@tonic-gate 	scf_snapshot_t *snap = NULL;
2707c478bd9Sstevel@tonic-gate 	scf_snapshot_t *isnap = NULL;
2717c478bd9Sstevel@tonic-gate 	scf_propertygroup_t *pg = NULL;
2727c478bd9Sstevel@tonic-gate 	scf_property_t *prop = NULL;
2737c478bd9Sstevel@tonic-gate 	scf_value_t *val = NULL;
2747c478bd9Sstevel@tonic-gate 	scf_iter_t *siter = NULL;
2757c478bd9Sstevel@tonic-gate 	scf_iter_t *iiter = NULL;
2767c478bd9Sstevel@tonic-gate 	char *fmri;
2777c478bd9Sstevel@tonic-gate 	ssize_t length;
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	uint64_t uint64;
2807c478bd9Sstevel@tonic-gate 	ssize_t bytes;
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	length = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH);
2837c478bd9Sstevel@tonic-gate 	if (length <= 0)
2847c478bd9Sstevel@tonic-gate 		return;
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	length++;
2877c478bd9Sstevel@tonic-gate 	fmri = alloca(length * sizeof (char));
2887c478bd9Sstevel@tonic-gate 
2893f2f09c1Sdp 	if ((h = scf_handle_create(SCF_VERSION)) == NULL)
2907c478bd9Sstevel@tonic-gate 		return;
2917c478bd9Sstevel@tonic-gate 
2923f2f09c1Sdp 	if (scf_handle_bind(h) != 0) {
2937c478bd9Sstevel@tonic-gate 		scf_handle_destroy(h);
2947c478bd9Sstevel@tonic-gate 		return;
2957c478bd9Sstevel@tonic-gate 	}
2967c478bd9Sstevel@tonic-gate 
2973f2f09c1Sdp 	if ((sc = scf_scope_create(h)) == NULL ||
2983f2f09c1Sdp 	    (svc = scf_service_create(h)) == NULL ||
2993f2f09c1Sdp 	    (inst = scf_instance_create(h)) == NULL ||
3003f2f09c1Sdp 	    (snap = scf_snapshot_create(h)) == NULL ||
3013f2f09c1Sdp 	    (pg = scf_pg_create(h)) == NULL ||
3023f2f09c1Sdp 	    (prop = scf_property_create(h)) == NULL ||
3033f2f09c1Sdp 	    (val = scf_value_create(h)) == NULL ||
3043f2f09c1Sdp 	    (siter = scf_iter_create(h)) == NULL ||
3053f2f09c1Sdp 	    (iiter = scf_iter_create(h)) == NULL)
3067c478bd9Sstevel@tonic-gate 		goto out;
3077c478bd9Sstevel@tonic-gate 
3083f2f09c1Sdp 	if (scf_handle_get_scope(h, SCF_SCOPE_LOCAL, sc) != 0)
3097c478bd9Sstevel@tonic-gate 		goto out;
3107c478bd9Sstevel@tonic-gate 
3113f2f09c1Sdp 	if (scf_iter_scope_services(siter, sc) != 0)
3127c478bd9Sstevel@tonic-gate 		goto out;
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	while (scf_iter_next_service(siter, svc) == 1) {
3157c478bd9Sstevel@tonic-gate 
3163f2f09c1Sdp 		if (scf_iter_service_instances(iiter, svc) != 0)
3177c478bd9Sstevel@tonic-gate 			continue;
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 		while (scf_iter_next_instance(iiter, inst) == 1) {
3207c478bd9Sstevel@tonic-gate 
3213f2f09c1Sdp 			if ((scf_instance_get_snapshot(inst, "running",
3223f2f09c1Sdp 			    snap)) != 0)
3237c478bd9Sstevel@tonic-gate 				isnap = NULL;
3247c478bd9Sstevel@tonic-gate 			else
3257c478bd9Sstevel@tonic-gate 				isnap = snap;
3267c478bd9Sstevel@tonic-gate 
3273f2f09c1Sdp 			if (scf_instance_get_pg_composed(inst, isnap,
3283f2f09c1Sdp 			    SCF_PG_GENERAL, pg) != 0)
3297c478bd9Sstevel@tonic-gate 				continue;
3307c478bd9Sstevel@tonic-gate 
3313f2f09c1Sdp 			if (scf_pg_get_property(pg, SCF_PROPERTY_RESTARTER,
3323f2f09c1Sdp 			    prop) != 0 ||
3333f2f09c1Sdp 			    scf_property_get_value(prop, val) != 0)
3347c478bd9Sstevel@tonic-gate 				continue;
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 			bytes = scf_value_get_astring(val, fmri, length);
3377c478bd9Sstevel@tonic-gate 			if (bytes <= 0 || bytes >= length)
3387c478bd9Sstevel@tonic-gate 				continue;
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 			if (strlcat(fmri, FMRI_CONTRACT_PROP, length) >=
3417c478bd9Sstevel@tonic-gate 			    length)
3427c478bd9Sstevel@tonic-gate 				continue;
3437c478bd9Sstevel@tonic-gate 
3443f2f09c1Sdp 			if (scf_handle_decode_fmri(h, fmri, NULL, NULL,
3453f2f09c1Sdp 			    NULL, NULL, prop, SCF_DECODE_FMRI_EXACT) != 0)
3467c478bd9Sstevel@tonic-gate 				continue;
3477c478bd9Sstevel@tonic-gate 
3483f2f09c1Sdp 			if (scf_property_is_type(prop, SCF_TYPE_COUNT) != 0 ||
3493f2f09c1Sdp 			    scf_property_get_value(prop, val) != 0 ||
3503f2f09c1Sdp 			    scf_value_get_count(val, &uint64) != 0)
3517c478bd9Sstevel@tonic-gate 				continue;
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 			ctid = (ctid_t)uint64;
3547c478bd9Sstevel@tonic-gate 			if (save_ctid(ctid) == 0) {
3557c478bd9Sstevel@tonic-gate 				(void) sigsend(P_CTID, ctid, SIGSTOP);
3567c478bd9Sstevel@tonic-gate 			}
3577c478bd9Sstevel@tonic-gate 		}
3587c478bd9Sstevel@tonic-gate 	}
3597c478bd9Sstevel@tonic-gate out:
3607c478bd9Sstevel@tonic-gate 	scf_scope_destroy(sc);
3617c478bd9Sstevel@tonic-gate 	scf_service_destroy(svc);
3627c478bd9Sstevel@tonic-gate 	scf_instance_destroy(inst);
3637c478bd9Sstevel@tonic-gate 	scf_snapshot_destroy(snap);
3647c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg);
3657c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
3667c478bd9Sstevel@tonic-gate 	scf_value_destroy(val);
3677c478bd9Sstevel@tonic-gate 	scf_iter_destroy(siter);
3687c478bd9Sstevel@tonic-gate 	scf_iter_destroy(iiter);
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	(void) scf_handle_unbind(h);
3717c478bd9Sstevel@tonic-gate 	scf_handle_destroy(h);
3727c478bd9Sstevel@tonic-gate }
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate static void
3757c478bd9Sstevel@tonic-gate continue_delegates()
3767c478bd9Sstevel@tonic-gate {
3777c478bd9Sstevel@tonic-gate 	ctidlist_t *next;
3787c478bd9Sstevel@tonic-gate 	for (next = ctidlist; next != NULL; next = next->next)
3797c478bd9Sstevel@tonic-gate 		(void) sigsend(P_CTID, next->ctid, SIGCONT);
3807c478bd9Sstevel@tonic-gate }
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate static void
3837c478bd9Sstevel@tonic-gate stop_restarters()
3847c478bd9Sstevel@tonic-gate {
3857c478bd9Sstevel@tonic-gate 	stop_startd();
3867c478bd9Sstevel@tonic-gate 	stop_delegates();
3877c478bd9Sstevel@tonic-gate }
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate static void
3907c478bd9Sstevel@tonic-gate continue_restarters()
3917c478bd9Sstevel@tonic-gate {
3927c478bd9Sstevel@tonic-gate 	continue_startd();
3937c478bd9Sstevel@tonic-gate 	continue_delegates();
3947c478bd9Sstevel@tonic-gate }
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate /*
3977c478bd9Sstevel@tonic-gate  * Copy an array of strings into buf, separated by spaces.  Returns 0 on
3987c478bd9Sstevel@tonic-gate  * success.
3997c478bd9Sstevel@tonic-gate  */
4007c478bd9Sstevel@tonic-gate static int
4017c478bd9Sstevel@tonic-gate gather_args(char **args, char *buf, size_t buf_sz)
4027c478bd9Sstevel@tonic-gate {
4037c478bd9Sstevel@tonic-gate 	if (strlcpy(buf, *args, buf_sz) >= buf_sz)
4047c478bd9Sstevel@tonic-gate 		return (-1);
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	for (++args; *args != NULL; ++args) {
4077c478bd9Sstevel@tonic-gate 		if (strlcat(buf, " ", buf_sz) >= buf_sz)
4087c478bd9Sstevel@tonic-gate 			return (-1);
4097c478bd9Sstevel@tonic-gate 		if (strlcat(buf, *args, buf_sz) >= buf_sz)
4107c478bd9Sstevel@tonic-gate 			return (-1);
4117c478bd9Sstevel@tonic-gate 	}
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	return (0);
4147c478bd9Sstevel@tonic-gate }
4157c478bd9Sstevel@tonic-gate 
41626f665e8Sdstaff /*
41726f665e8Sdstaff  * Halt every zone on the system.  We are committed to doing a shutdown
41826f665e8Sdstaff  * even if something goes wrong here. If something goes wrong, we just
41926f665e8Sdstaff  * continue with the shutdown.  Return non-zero if we need to wait for zones to
42026f665e8Sdstaff  * halt later on.
42126f665e8Sdstaff  */
42226f665e8Sdstaff static int
4233f2f09c1Sdp halt_zones()
42426f665e8Sdstaff {
42526f665e8Sdstaff 	pid_t pid;
42626f665e8Sdstaff 	zoneid_t *zones;
4273f2f09c1Sdp 	size_t nz = 0, old_nz;
42826f665e8Sdstaff 	int i;
42926f665e8Sdstaff 	char zname[ZONENAME_MAX];
43026f665e8Sdstaff 
43126f665e8Sdstaff 	/*
43226f665e8Sdstaff 	 * Get a list of zones. If the number of zones changes in between the
43326f665e8Sdstaff 	 * two zone_list calls, try again.
43426f665e8Sdstaff 	 */
43526f665e8Sdstaff 
43626f665e8Sdstaff 	for (;;) {
43726f665e8Sdstaff 		(void) zone_list(NULL, &nz);
43826f665e8Sdstaff 		if (nz == 1)
43926f665e8Sdstaff 			return (0);
44026f665e8Sdstaff 		old_nz = nz;
44126f665e8Sdstaff 		zones = calloc(sizeof (zoneid_t), nz);
44226f665e8Sdstaff 		if (zones == NULL) {
44326f665e8Sdstaff 			(void) fprintf(stderr,
44426f665e8Sdstaff 			    gettext("%s: Could not halt zones"
4453f2f09c1Sdp 			    " (out of memory).\n"), cmdname);
44626f665e8Sdstaff 			return (0);
44726f665e8Sdstaff 		}
44826f665e8Sdstaff 
44926f665e8Sdstaff 		(void) zone_list(zones, &nz);
45026f665e8Sdstaff 		if (old_nz == nz)
45126f665e8Sdstaff 			break;
45226f665e8Sdstaff 		free(zones);
45326f665e8Sdstaff 	}
45426f665e8Sdstaff 
45526f665e8Sdstaff 	if (nz == 2) {
4563f2f09c1Sdp 		(void) fprintf(stderr, gettext("%s: Halting 1 zone.\n"),
4573f2f09c1Sdp 		    cmdname);
45826f665e8Sdstaff 	} else {
4593f2f09c1Sdp 		(void) fprintf(stderr, gettext("%s: Halting %i zones.\n"),
4603f2f09c1Sdp 		    cmdname, nz - 1);
46126f665e8Sdstaff 	}
46226f665e8Sdstaff 
46326f665e8Sdstaff 	for (i = 0; i < nz; i++) {
46426f665e8Sdstaff 		if (zones[i] == GLOBAL_ZONEID)
46526f665e8Sdstaff 			continue;
46626f665e8Sdstaff 		if (getzonenamebyid(zones[i], zname, sizeof (zname)) < 0) {
46726f665e8Sdstaff 			/*
46826f665e8Sdstaff 			 * getzonenamebyid should only fail if we raced with
46926f665e8Sdstaff 			 * another process trying to shut down the zone.
47026f665e8Sdstaff 			 * We assume this happened and ignore the error.
47126f665e8Sdstaff 			 */
47226f665e8Sdstaff 			if (errno != EINVAL) {
47326f665e8Sdstaff 				(void) fprintf(stderr,
47426f665e8Sdstaff 				    gettext("%s: Unexpected error while "
47526f665e8Sdstaff 				    "looking up zone %ul: %s.\n"),
4763f2f09c1Sdp 				    cmdname, zones[i], strerror(errno));
47726f665e8Sdstaff 			}
47826f665e8Sdstaff 
47926f665e8Sdstaff 			continue;
48026f665e8Sdstaff 		}
48126f665e8Sdstaff 		pid = fork();
48226f665e8Sdstaff 		if (pid < 0) {
48326f665e8Sdstaff 			(void) fprintf(stderr,
48426f665e8Sdstaff 			    gettext("%s: Zone \"%s\" could not be"
48526f665e8Sdstaff 			    " halted (could not fork(): %s).\n"),
4863f2f09c1Sdp 			    cmdname, zname, strerror(errno));
48726f665e8Sdstaff 			continue;
48826f665e8Sdstaff 		}
48926f665e8Sdstaff 		if (pid == 0) {
49026f665e8Sdstaff 			(void) execl(ZONEADM_PROG, ZONEADM_PROG,
49126f665e8Sdstaff 			    "-z", zname, "halt", NULL);
49226f665e8Sdstaff 			(void) fprintf(stderr,
49326f665e8Sdstaff 			    gettext("%s: Zone \"%s\" could not be halted"
49426f665e8Sdstaff 			    " (cannot exec(" ZONEADM_PROG "): %s).\n"),
4953f2f09c1Sdp 			    cmdname, zname, strerror(errno));
49626f665e8Sdstaff 			exit(0);
49726f665e8Sdstaff 		}
49826f665e8Sdstaff 	}
49926f665e8Sdstaff 
50026f665e8Sdstaff 	return (1);
50126f665e8Sdstaff }
50226f665e8Sdstaff 
50326f665e8Sdstaff /*
50426f665e8Sdstaff  * This function tries to wait for all non-global zones to go away.
50526f665e8Sdstaff  * It will timeout if no progress is made for 5 seconds, or a total of
50626f665e8Sdstaff  * 30 seconds elapses.
50726f665e8Sdstaff  */
50826f665e8Sdstaff 
50926f665e8Sdstaff static void
5103f2f09c1Sdp check_zones_haltedness()
51126f665e8Sdstaff {
51226f665e8Sdstaff 	int t = 0, t_prog = 0;
51326f665e8Sdstaff 	size_t nz = 0, last_nz;
51426f665e8Sdstaff 
51526f665e8Sdstaff 	do {
51626f665e8Sdstaff 		last_nz = nz;
51726f665e8Sdstaff 		(void) zone_list(NULL, &nz);
51826f665e8Sdstaff 		if (nz == 1)
51926f665e8Sdstaff 			return;
52026f665e8Sdstaff 
52126f665e8Sdstaff 		(void) sleep(1);
52226f665e8Sdstaff 
52326f665e8Sdstaff 		if (last_nz > nz)
52426f665e8Sdstaff 			t_prog = 0;
52526f665e8Sdstaff 
52626f665e8Sdstaff 		t++;
52726f665e8Sdstaff 		t_prog++;
52826f665e8Sdstaff 
52926f665e8Sdstaff 		if (t == 10) {
53026f665e8Sdstaff 			if (nz == 2) {
53126f665e8Sdstaff 				(void) fprintf(stderr,
53226f665e8Sdstaff 				    gettext("%s: Still waiting for 1 zone to "
53326f665e8Sdstaff 				    "halt. Will wait up to 20 seconds.\n"),
5343f2f09c1Sdp 				    cmdname);
53526f665e8Sdstaff 			} else {
53626f665e8Sdstaff 				(void) fprintf(stderr,
53726f665e8Sdstaff 				    gettext("%s: Still waiting for %i zones "
53826f665e8Sdstaff 				    "to halt. Will wait up to 20 seconds.\n"),
5393f2f09c1Sdp 				    cmdname, nz - 1);
54026f665e8Sdstaff 			}
54126f665e8Sdstaff 		}
54226f665e8Sdstaff 
54326f665e8Sdstaff 	} while ((t < 30) && (t_prog < 5));
54426f665e8Sdstaff }
54526f665e8Sdstaff 
54619397407SSherry Moore 
54719397407SSherry Moore /*
54819397407SSherry Moore  * Validate that this is a root disk or dataset
54919397407SSherry Moore  * Returns 0 if it is a root disk or dataset;
55019397407SSherry Moore  * returns 1 if it is a disk argument or dataset, but not valid or not root;
55119397407SSherry Moore  * returns -1 if it is not a valid argument or a disk argument.
55219397407SSherry Moore  */
55319397407SSherry Moore static int
55419397407SSherry Moore validate_disk(char *arg, char *mountpoint)
55519397407SSherry Moore {
55619397407SSherry Moore 	static char root_dev_path[] = "/dev/dsk";
55719397407SSherry Moore 	char kernpath[MAXPATHLEN];
55819397407SSherry Moore 	struct stat64 statbuf;
55919397407SSherry Moore 	int rc = 0;
56019397407SSherry Moore 
56119397407SSherry Moore 	if (strlen(arg) > MAXPATHLEN) {
56219397407SSherry Moore 		(void) fprintf(stderr,
563*753a6d45SSherry Moore 		    gettext("%s: Argument is too long\n"), cmdname);
56419397407SSherry Moore 		return (-1);
56519397407SSherry Moore 	}
56619397407SSherry Moore 
56719397407SSherry Moore 	bcopy(FASTBOOT_MOUNTPOINT, mountpoint, sizeof (FASTBOOT_MOUNTPOINT));
56819397407SSherry Moore 
569*753a6d45SSherry Moore 	if (strstr(arg, mountpoint) == NULL) {
57019397407SSherry Moore 		/*
57119397407SSherry Moore 		 * Do a force umount just in case some other filesystem has
57219397407SSherry Moore 		 * been mounted there.
57319397407SSherry Moore 		 */
57419397407SSherry Moore 		(void) umount2(mountpoint, MS_FORCE);
575*753a6d45SSherry Moore 	}
57619397407SSherry Moore 
57719397407SSherry Moore 	/* Create the directory if it doesn't already exist */
578*753a6d45SSherry Moore 	if (lstat64(mountpoint, &statbuf) != 0) {
57919397407SSherry Moore 		if (mkdirp(mountpoint, 0755) != 0) {
58019397407SSherry Moore 			(void) fprintf(stderr,
581*753a6d45SSherry Moore 			    gettext("Failed to create mountpoint %s\n"),
58219397407SSherry Moore 			    mountpoint);
58319397407SSherry Moore 			return (-1);
58419397407SSherry Moore 		}
58519397407SSherry Moore 	}
58619397407SSherry Moore 
58719397407SSherry Moore 	if (strncmp(arg, root_dev_path, strlen(root_dev_path)) == 0) {
58819397407SSherry Moore 		/* ufs root disk argument */
58919397407SSherry Moore 		rc = validate_ufs_disk(arg, mountpoint);
59019397407SSherry Moore 	} else {
59119397407SSherry Moore 		/* zfs root pool argument */
59219397407SSherry Moore 		rc = validate_zfs_pool(arg, mountpoint);
59319397407SSherry Moore 	}
59419397407SSherry Moore 
59519397407SSherry Moore 	if (rc != 0)
59619397407SSherry Moore 		return (rc);
59719397407SSherry Moore 
59819397407SSherry Moore 	(void) snprintf(kernpath, MAXPATHLEN, "%s/platform/i86pc/kernel/unix",
59919397407SSherry Moore 	    mountpoint);
60019397407SSherry Moore 
60119397407SSherry Moore 	if (stat64(kernpath, &statbuf) != 0) {
60219397407SSherry Moore 		(void) fprintf(stderr,
60319397407SSherry Moore 		    gettext("%s: %s is not a root disk or dataset\n"),
60419397407SSherry Moore 		    cmdname, arg);
60519397407SSherry Moore 		return (1);
60619397407SSherry Moore 	}
60719397407SSherry Moore 
60819397407SSherry Moore 	return (0);
60919397407SSherry Moore }
61019397407SSherry Moore 
61119397407SSherry Moore 
61219397407SSherry Moore static int
61319397407SSherry Moore validate_ufs_disk(char *arg, char *mountpoint)
61419397407SSherry Moore {
6158e07a41fSKonstantin Ananyev 	struct ufs_args	ufs_args = { 0 };
6168e07a41fSKonstantin Ananyev 	char mntopts[MNT_LINE_MAX] = MNTOPT_LARGEFILES;
61719397407SSherry Moore 
61819397407SSherry Moore 	/* perform the mount */
6198e07a41fSKonstantin Ananyev 	ufs_args.flags = UFSMNT_LARGEFILES;
62019397407SSherry Moore 	if (mount(arg, mountpoint, MS_DATA|MS_OPTIONSTR,
6218e07a41fSKonstantin Ananyev 	    MNTTYPE_UFS, &ufs_args, sizeof (ufs_args),
6228e07a41fSKonstantin Ananyev 	    mntopts, sizeof (mntopts)) != 0) {
62319397407SSherry Moore 		perror(cmdname);
62419397407SSherry Moore 		(void) fprintf(stderr,
625*753a6d45SSherry Moore 		    gettext("%s: Failed to mount %s\n"), cmdname, arg);
62619397407SSherry Moore 		return (-1);
62719397407SSherry Moore 	}
62819397407SSherry Moore 
62919397407SSherry Moore 	return (0);
63019397407SSherry Moore }
63119397407SSherry Moore 
63219397407SSherry Moore static int
63319397407SSherry Moore validate_zfs_pool(char *arg, char *mountpoint)
63419397407SSherry Moore {
63519397407SSherry Moore 	zfs_handle_t *zhp = NULL;
63619397407SSherry Moore 	char mntopts[MNT_LINE_MAX] = { '\0' };
63719397407SSherry Moore 	int rc = 0;
63819397407SSherry Moore 
63919397407SSherry Moore 	if ((g_zfs = libzfs_init()) == NULL) {
640*753a6d45SSherry Moore 		(void) fprintf(stderr, gettext("Internal error: failed to "
64119397407SSherry Moore 		    "initialize ZFS library\n"));
64219397407SSherry Moore 		return (-1);
64319397407SSherry Moore 	}
64419397407SSherry Moore 
64519397407SSherry Moore 	/* Try to open the dataset */
64619397407SSherry Moore 	if ((zhp = zfs_open(g_zfs, arg,
64719397407SSherry Moore 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_DATASET)) == NULL)
64819397407SSherry Moore 		return (-1);
64919397407SSherry Moore 
65019397407SSherry Moore 	/* perform the mount */
651*753a6d45SSherry Moore 	if (mount(zfs_get_name(zhp), mountpoint, MS_DATA|MS_OPTIONSTR|MS_RDONLY,
65219397407SSherry Moore 	    MNTTYPE_ZFS, NULL, 0, mntopts, sizeof (mntopts)) != 0) {
65319397407SSherry Moore 		perror(cmdname);
65419397407SSherry Moore 		(void) fprintf(stderr,
655*753a6d45SSherry Moore 		    gettext("%s: Failed to mount %s\n"), cmdname, arg);
65619397407SSherry Moore 		rc = -1;
65719397407SSherry Moore 	}
65819397407SSherry Moore 
65919397407SSherry Moore validate_zfs_err_out:
66019397407SSherry Moore 	if (zhp != NULL)
66119397407SSherry Moore 		zfs_close(zhp);
66219397407SSherry Moore 
66319397407SSherry Moore 	libzfs_fini(g_zfs);
66419397407SSherry Moore 	return (rc);
66519397407SSherry Moore }
66619397407SSherry Moore 
66719397407SSherry Moore /*
66819397407SSherry Moore  * Return 0 if not zfs, or is zfs and have successfully constructed the
66919397407SSherry Moore  * boot argument; returns non-zero otherwise.
67019397407SSherry Moore  * At successful completion fpth contains pointer where mount point ends.
67119397407SSherry Moore  * NOTE: arg is supposed to be the resolved path
67219397407SSherry Moore  */
67319397407SSherry Moore static int
67419397407SSherry Moore get_zfs_bootfs_arg(const char *arg, const char ** fpth, int *is_zfs,
67519397407SSherry Moore 		char *bootfs_arg)
67619397407SSherry Moore {
67719397407SSherry Moore 	zfs_handle_t *zhp = NULL;
67819397407SSherry Moore 	zpool_handle_t *zpoolp = NULL;
67919397407SSherry Moore 	FILE *mtabp = NULL;
68019397407SSherry Moore 	struct mnttab mnt;
68119397407SSherry Moore 	char *poolname = NULL;
682*753a6d45SSherry Moore 	char physpath[MAXPATHLEN];
68319397407SSherry Moore 	char mntsp[ZPOOL_MAXNAMELEN];
68419397407SSherry Moore 	char bootfs[ZPOOL_MAXNAMELEN];
68519397407SSherry Moore 	int rc = 0;
68619397407SSherry Moore 	size_t mntlen = 0;
68719397407SSherry Moore 	size_t msz;
688*753a6d45SSherry Moore 	static char fmt[] = "-B zfs-bootfs=%s,bootpath=\"%s\"";
68919397407SSherry Moore 
69019397407SSherry Moore 	*fpth = arg;
69119397407SSherry Moore 	*is_zfs = 0;
69219397407SSherry Moore 
69319397407SSherry Moore 	bzero(physpath, sizeof (physpath));
69419397407SSherry Moore 	bzero(bootfs, sizeof (bootfs));
69519397407SSherry Moore 
69619397407SSherry Moore 	if ((mtabp = fopen(MNTTAB, "r")) == NULL) {
69719397407SSherry Moore 		return (-1);
69819397407SSherry Moore 	}
69919397407SSherry Moore 
70019397407SSherry Moore 	while (getmntent(mtabp, &mnt) == 0) {
70119397407SSherry Moore 		if (strstr(arg, mnt.mnt_mountp) == arg &&
70219397407SSherry Moore 		    (msz = strlen(mnt.mnt_mountp)) > mntlen) {
70319397407SSherry Moore 			mntlen = msz;
70419397407SSherry Moore 			*is_zfs = strcmp(MNTTYPE_ZFS, mnt.mnt_fstype) == 0;
70519397407SSherry Moore 			(void) strlcpy(mntsp, mnt.mnt_special, sizeof (mntsp));
70619397407SSherry Moore 		}
70719397407SSherry Moore 	}
70819397407SSherry Moore 
70919397407SSherry Moore 	(void) fclose(mtabp);
71019397407SSherry Moore 
71119397407SSherry Moore 	if (mntlen > 1)
71219397407SSherry Moore 		*fpth += mntlen;
71319397407SSherry Moore 
71419397407SSherry Moore 	if (!*is_zfs)
71519397407SSherry Moore 		return (0);
71619397407SSherry Moore 
71719397407SSherry Moore 	if ((g_zfs = libzfs_init()) == NULL)
71819397407SSherry Moore 		return (-1);
71919397407SSherry Moore 
72019397407SSherry Moore 	/* Try to open the dataset */
72119397407SSherry Moore 	if ((zhp = zfs_open(g_zfs, mntsp,
72219397407SSherry Moore 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_DATASET)) == NULL) {
723*753a6d45SSherry Moore 		(void) fprintf(stderr, gettext("Cannot open %s\n"), mntsp);
72419397407SSherry Moore 		rc = -1;
72519397407SSherry Moore 		goto validate_zfs_err_out;
72619397407SSherry Moore 	}
72719397407SSherry Moore 
72819397407SSherry Moore 	(void) strlcpy(bootfs, mntsp, sizeof (bootfs));
72919397407SSherry Moore 
73019397407SSherry Moore 	if ((poolname = strtok(mntsp, "/")) == NULL) {
73119397407SSherry Moore 		rc = -1;
73219397407SSherry Moore 		goto validate_zfs_err_out;
73319397407SSherry Moore 	}
73419397407SSherry Moore 
73519397407SSherry Moore 	if ((zpoolp = zpool_open(g_zfs, poolname)) == NULL) {
736*753a6d45SSherry Moore 		(void) fprintf(stderr, gettext("Cannot open %s\n"), poolname);
73719397407SSherry Moore 		rc = -1;
73819397407SSherry Moore 		goto validate_zfs_err_out;
73919397407SSherry Moore 	}
74019397407SSherry Moore 
741*753a6d45SSherry Moore 	if (zpool_get_physpath(zpoolp, physpath, sizeof (physpath)) != 0) {
742*753a6d45SSherry Moore 		(void) fprintf(stderr, gettext("Cannot find phys_path\n"));
74319397407SSherry Moore 		rc = -1;
74419397407SSherry Moore 		goto validate_zfs_err_out;
74519397407SSherry Moore 	}
74619397407SSherry Moore 
747*753a6d45SSherry Moore 	/*
748*753a6d45SSherry Moore 	 * For the mirror physpath would contain the list of all
749*753a6d45SSherry Moore 	 * bootable devices, pick up the first one.
750*753a6d45SSherry Moore 	 */
751*753a6d45SSherry Moore 	(void) strtok(physpath, " ");
752*753a6d45SSherry Moore 	if (snprintf(bootfs_arg, BOOTARGS_MAX, fmt, bootfs, physpath) >=
753*753a6d45SSherry Moore 	    BOOTARGS_MAX) {
754*753a6d45SSherry Moore 		rc = E2BIG;
755*753a6d45SSherry Moore 		(void) fprintf(stderr,
756*753a6d45SSherry Moore 		    gettext("Boot arguments are too long\n"));
75719397407SSherry Moore 	}
75819397407SSherry Moore 
75919397407SSherry Moore validate_zfs_err_out:
76019397407SSherry Moore 	if (zhp != NULL)
76119397407SSherry Moore 		zfs_close(zhp);
76219397407SSherry Moore 
76319397407SSherry Moore 	if (zpoolp != NULL)
76419397407SSherry Moore 		zpool_close(zpoolp);
76519397407SSherry Moore 
76619397407SSherry Moore 	libzfs_fini(g_zfs);
76719397407SSherry Moore 	return (rc);
76819397407SSherry Moore }
76919397407SSherry Moore 
77019397407SSherry Moore /*
77119397407SSherry Moore  * Validate that the file exists, and is an ELF file.
77219397407SSherry Moore  * Returns 0 on success, -1 on failure.
77319397407SSherry Moore  */
77419397407SSherry Moore static int
77519397407SSherry Moore validate_unix(char *arg, int *mplen, int *is_zfs, char *bootfs_arg,
77619397407SSherry Moore     int *failsafe)
77719397407SSherry Moore {
77819397407SSherry Moore 	const char *location;
77919397407SSherry Moore 	int class, format;
78019397407SSherry Moore 	unsigned char ident[EI_NIDENT];
78119397407SSherry Moore 	char physpath[MAXPATHLEN];
78219397407SSherry Moore 	int elffd = -1;
78319397407SSherry Moore 	size_t	sz;
78419397407SSherry Moore 
78519397407SSherry Moore 	if ((sz = resolvepath(arg, physpath, sizeof (physpath) - 1)) ==
78619397407SSherry Moore 	    (size_t)-1) {
78719397407SSherry Moore 		(void) fprintf(stderr,
788*753a6d45SSherry Moore 		    gettext("Cannot resolve path for %s: %s\n"),
78919397407SSherry Moore 		    arg, strerror(errno));
79019397407SSherry Moore 		return (-1);
79119397407SSherry Moore 	}
79219397407SSherry Moore 	(void) strlcpy(arg, physpath, sz + 1);
79319397407SSherry Moore 
79419397407SSherry Moore 	if (strlen(arg) > MAXPATHLEN) {
79519397407SSherry Moore 		(void) fprintf(stderr,
796*753a6d45SSherry Moore 		    gettext("%s: New kernel name is too long\n"), cmdname);
79719397407SSherry Moore 		return (-1);
79819397407SSherry Moore 	}
79919397407SSherry Moore 
80019397407SSherry Moore 	if (strncmp(basename(arg), "unix", 4) != 0) {
80119397407SSherry Moore 		(void) fprintf(stderr,
802*753a6d45SSherry Moore 		    gettext("%s: %s: Kernel name must be unix\n"),
80319397407SSherry Moore 		    cmdname, arg);
80419397407SSherry Moore 		return (-1);
80519397407SSherry Moore 	}
80619397407SSherry Moore 
80719397407SSherry Moore 	if (get_zfs_bootfs_arg(arg, &location, is_zfs, bootfs_arg) != 0)
80819397407SSherry Moore 		goto err_out;
80919397407SSherry Moore 
81019397407SSherry Moore 	*mplen = location - arg;
81119397407SSherry Moore 
81219397407SSherry Moore 	if ((strstr(location, "/boot/platform")) == location)
81319397407SSherry Moore 		*failsafe = 1;
81419397407SSherry Moore 	else if ((strstr(location, "/platform")) == location)
81519397407SSherry Moore 		*failsafe = 0;
81619397407SSherry Moore 	else	{
81719397407SSherry Moore 		(void) fprintf(stderr,
818*753a6d45SSherry Moore 		    gettext("%s: %s: No /boot/platform or /platform in"
81919397407SSherry Moore 		    " file name\n"), cmdname, arg);
82019397407SSherry Moore 			goto err_out;
82119397407SSherry Moore 	}
82219397407SSherry Moore 
82319397407SSherry Moore 	if ((elffd = open64(arg, O_RDONLY)) < 0 ||
82419397407SSherry Moore 	    (pread64(elffd, ident, EI_NIDENT, 0) != EI_NIDENT)) {
82519397407SSherry Moore 		(void) fprintf(stderr, "%s: %s: %s\n",
82619397407SSherry Moore 		    cmdname, arg, strerror(errno));
82719397407SSherry Moore 		goto err_out;
82819397407SSherry Moore 	}
82919397407SSherry Moore 
83019397407SSherry Moore 	class = ident[EI_CLASS];
83119397407SSherry Moore 
83219397407SSherry Moore 	if ((class != ELFCLASS32 && class != ELFCLASS64) ||
833*753a6d45SSherry Moore 	    memcmp(&ident[EI_MAG0], ELFMAG, 4) != 0) {
83419397407SSherry Moore 		(void) fprintf(stderr,
835*753a6d45SSherry Moore 		    gettext("%s: %s: Not a valid ELF file\n"), cmdname, arg);
83619397407SSherry Moore 		goto err_out;
83719397407SSherry Moore 	}
83819397407SSherry Moore 
83919397407SSherry Moore 	format = ident[EI_DATA];
84019397407SSherry Moore 
84119397407SSherry Moore 	if (format != CUR_ELFDATA) {
842*753a6d45SSherry Moore 		(void) fprintf(stderr, gettext("%s: %s: Invalid data format\n"),
84319397407SSherry Moore 		    cmdname, arg);
84419397407SSherry Moore 		goto err_out;
84519397407SSherry Moore 	}
84619397407SSherry Moore 
84719397407SSherry Moore 	return (0);
84819397407SSherry Moore 
84919397407SSherry Moore err_out:
85019397407SSherry Moore 	if (elffd >= 0) {
85119397407SSherry Moore 		(void) close(elffd);
85219397407SSherry Moore 		elffd = -1;
85319397407SSherry Moore 	}
85419397407SSherry Moore 	return (-1);
85519397407SSherry Moore }
85619397407SSherry Moore 
85719397407SSherry Moore static int
8584e1f1c13SKonstantin Ananyev halt_exec(const char *path, ...)
8594e1f1c13SKonstantin Ananyev {
8604e1f1c13SKonstantin Ananyev 	pid_t		pid;
8614e1f1c13SKonstantin Ananyev 	int		i;
8624e1f1c13SKonstantin Ananyev 	int		st;
8634e1f1c13SKonstantin Ananyev 	const char	*arg;
8644e1f1c13SKonstantin Ananyev 	va_list	vp;
8654e1f1c13SKonstantin Ananyev 	const char	*argv[256];
8664e1f1c13SKonstantin Ananyev 
8674e1f1c13SKonstantin Ananyev 	if ((pid = fork()) == -1) {
8684e1f1c13SKonstantin Ananyev 		return (errno);
8694e1f1c13SKonstantin Ananyev 	} else if (pid == 0) {
8704e1f1c13SKonstantin Ananyev 		(void) fclose(stdout);
8714e1f1c13SKonstantin Ananyev 		(void) fclose(stderr);
8724e1f1c13SKonstantin Ananyev 
8734e1f1c13SKonstantin Ananyev 		argv[0] = path;
8744e1f1c13SKonstantin Ananyev 		i = 1;
8754e1f1c13SKonstantin Ananyev 
8764e1f1c13SKonstantin Ananyev 		va_start(vp, path);
8774e1f1c13SKonstantin Ananyev 
8784e1f1c13SKonstantin Ananyev 		do {
8794e1f1c13SKonstantin Ananyev 			arg = va_arg(vp, const char *);
8804e1f1c13SKonstantin Ananyev 			argv[i] = arg;
8814e1f1c13SKonstantin Ananyev 		} while (arg != NULL &&
8824e1f1c13SKonstantin Ananyev 		    ++i != sizeof (argv) / sizeof (argv[0]));
8834e1f1c13SKonstantin Ananyev 
8844e1f1c13SKonstantin Ananyev 		va_end(vp);
8854e1f1c13SKonstantin Ananyev 
8864e1f1c13SKonstantin Ananyev 		(void) execve(path, (char * const *)argv, NULL);
887*753a6d45SSherry Moore 		(void) fprintf(stderr, gettext("Cannot execute %s: %s\n"),
8884e1f1c13SKonstantin Ananyev 		    path, strerror(errno));
8894e1f1c13SKonstantin Ananyev 		exit(-1);
8904e1f1c13SKonstantin Ananyev 	} else {
8914e1f1c13SKonstantin Ananyev 		if (waitpid(pid, &st, 0) == pid &&
8924e1f1c13SKonstantin Ananyev 		    !WIFSIGNALED(st) && WIFEXITED(st))
8934e1f1c13SKonstantin Ananyev 			st = WEXITSTATUS(st);
8944e1f1c13SKonstantin Ananyev 		else
8954e1f1c13SKonstantin Ananyev 			st = -1;
8964e1f1c13SKonstantin Ananyev 	}
8974e1f1c13SKonstantin Ananyev 	return (st);
8984e1f1c13SKonstantin Ananyev }
8994e1f1c13SKonstantin Ananyev 
9004e1f1c13SKonstantin Ananyev /*
9014e1f1c13SKonstantin Ananyev  * Invokes lumount for bename.
9024e1f1c13SKonstantin Ananyev  * At successfull completion returns zero and copies contents of bename
9034e1f1c13SKonstantin Ananyev  * into mountpoint[]
9044e1f1c13SKonstantin Ananyev  */
9054e1f1c13SKonstantin Ananyev static int
90619397407SSherry Moore fastboot_bename(const char *bename, char *mountpoint, size_t mpsz)
90719397407SSherry Moore {
90819397407SSherry Moore 	int rc;
90919397407SSherry Moore 
9104e1f1c13SKonstantin Ananyev 	(void) halt_exec(LUUMOUNT_PROG, "-n", bename, NULL);
91119397407SSherry Moore 
9124e1f1c13SKonstantin Ananyev 	if ((rc = halt_exec(LUMOUNT_PROG, "-n", bename, FASTBOOT_MOUNTPOINT,
9134e1f1c13SKonstantin Ananyev 	    NULL)) != 0)
914*753a6d45SSherry Moore 		(void) fprintf(stderr, gettext("%s: Cannot mount BE %s\n"),
91519397407SSherry Moore 		    cmdname, bename);
91619397407SSherry Moore 	else
91719397407SSherry Moore 		(void) strlcpy(mountpoint, FASTBOOT_MOUNTPOINT, mpsz);
91819397407SSherry Moore 
91919397407SSherry Moore 	return (rc);
92019397407SSherry Moore }
92119397407SSherry Moore 
92219397407SSherry Moore /*
92319397407SSherry Moore  * Returns 0 on successful parsing of the arguments;
924*753a6d45SSherry Moore  * returns EINVAL on parsing failures that should abort the reboot attempt;
925*753a6d45SSherry Moore  * returns other error code to fall back to regular reboot.
92619397407SSherry Moore  */
92719397407SSherry Moore static int
928*753a6d45SSherry Moore parse_fastboot_args(char *bootargs_buf, size_t buf_size,
929*753a6d45SSherry Moore     int *is_dryrun, const char *bename, int *failsafe)
93019397407SSherry Moore {
93119397407SSherry Moore 	char mountpoint[MAXPATHLEN];
93219397407SSherry Moore 	char bootargs_saved[BOOTARGS_MAX];
93319397407SSherry Moore 	char bootargs_scratch[BOOTARGS_MAX];
93419397407SSherry Moore 	char bootfs_arg[BOOTARGS_MAX];
93519397407SSherry Moore 	char unixfile[BOOTARGS_MAX];
93619397407SSherry Moore 	char *head, *newarg;
93719397407SSherry Moore 	int buflen;		/* length of the bootargs_buf */
93819397407SSherry Moore 	int mplen;		/* length of the mount point */
93919397407SSherry Moore 	int rootlen = 0;	/* length of the root argument */
94019397407SSherry Moore 	int unixlen = 0;	/* length of the unix argument */
94119397407SSherry Moore 	int off = 0;		/* offset into the new boot argument */
94219397407SSherry Moore 	int is_zfs = 0;
94319397407SSherry Moore 	int rc = 0;
94419397407SSherry Moore 
94519397407SSherry Moore 	bzero(mountpoint, sizeof (mountpoint));
94619397407SSherry Moore 
94719397407SSherry Moore 	/*
94819397407SSherry Moore 	 * If argc is not 0, buflen is length of the argument being passed in;
94919397407SSherry Moore 	 * else it is 0 as bootargs_buf has been initialized to all 0's.
95019397407SSherry Moore 	 */
95119397407SSherry Moore 	buflen = strlen(bootargs_buf);
95219397407SSherry Moore 
95319397407SSherry Moore 	/* Save a copy of the original argument */
95419397407SSherry Moore 	bcopy(bootargs_buf, bootargs_saved, buflen);
95519397407SSherry Moore 	bzero(&bootargs_saved[buflen], sizeof (bootargs_saved) - buflen);
95619397407SSherry Moore 
95719397407SSherry Moore 	/* Save another copy to be used by strtok */
95819397407SSherry Moore 	bcopy(bootargs_buf, bootargs_scratch, buflen);
95919397407SSherry Moore 	bzero(&bootargs_scratch[buflen], sizeof (bootargs_scratch) - buflen);
96019397407SSherry Moore 	head = &bootargs_scratch[0];
96119397407SSherry Moore 
96219397407SSherry Moore 	/* Get the first argument */
96319397407SSherry Moore 	newarg = strtok(bootargs_scratch, " ");
96419397407SSherry Moore 
96519397407SSherry Moore 	/*
96619397407SSherry Moore 	 * If this is a dry run request, verify that the drivers can handle
96719397407SSherry Moore 	 * fast reboot.
96819397407SSherry Moore 	 */
96919397407SSherry Moore 	if (newarg && strncasecmp(newarg, "dryrun", strlen("dryrun")) == 0) {
97019397407SSherry Moore 		*is_dryrun = 1;
97119397407SSherry Moore 		(void) system("/usr/sbin/devfsadm");
97219397407SSherry Moore 	}
97319397407SSherry Moore 
97419397407SSherry Moore 	/*
97519397407SSherry Moore 	 * Always perform a dry run to identify all the drivers that
97619397407SSherry Moore 	 * need to implement devo_reset().
97719397407SSherry Moore 	 */
97819397407SSherry Moore 	if (uadmin(A_SHUTDOWN, AD_FASTREBOOT_DRYRUN,
97919397407SSherry Moore 	    (uintptr_t)bootargs_saved) != 0) {
980*753a6d45SSherry Moore 		(void) fprintf(stderr, gettext("%s: Not all drivers "
981*753a6d45SSherry Moore 		    "have implemented quiesce(9E)\n"
982*753a6d45SSherry Moore 		    "\tPlease see /var/adm/messages for drivers that haven't\n"
983*753a6d45SSherry Moore 		    "\timplemented quiesce(9E).\n"), cmdname);
98419397407SSherry Moore 	} else if (*is_dryrun) {
985*753a6d45SSherry Moore 		(void) fprintf(stderr, gettext("%s: All drivers have "
98619397407SSherry Moore 		    "implemented quiesce(9E)\n"), cmdname);
98719397407SSherry Moore 	}
98819397407SSherry Moore 
989*753a6d45SSherry Moore 	/* Return if it is a true dry run. */
99019397407SSherry Moore 	if (*is_dryrun)
99119397407SSherry Moore 		return (rc);
99219397407SSherry Moore 
993*753a6d45SSherry Moore #if defined(__i386)
994*753a6d45SSherry Moore 	/* Read boot args from GRUB menu */
995*753a6d45SSherry Moore 	if ((bootargs_buf[0] == 0 || isdigit(bootargs_buf[0])) &&
996*753a6d45SSherry Moore 	    bename == NULL) {
997*753a6d45SSherry Moore 		/*
998*753a6d45SSherry Moore 		 * If no boot arguments are given, or a GRUB menu entry
999*753a6d45SSherry Moore 		 * number is provided, process the GRUB menu.
1000*753a6d45SSherry Moore 		 */
1001*753a6d45SSherry Moore 		int entnum;
1002*753a6d45SSherry Moore 		if (bootargs_buf[0] == 0)
1003*753a6d45SSherry Moore 			entnum = GRUB_ENTRY_DEFAULT;
1004*753a6d45SSherry Moore 		else {
1005*753a6d45SSherry Moore 			errno = 0;
1006*753a6d45SSherry Moore 			entnum = strtoul(bootargs_buf, NULL, 10);
1007*753a6d45SSherry Moore 			rc = errno;
1008*753a6d45SSherry Moore 		}
1009*753a6d45SSherry Moore 
1010*753a6d45SSherry Moore 		if (rc == 0 && (rc = grub_get_boot_args(&fbarg, NULL,
1011*753a6d45SSherry Moore 		    entnum)) == 0) {
1012*753a6d45SSherry Moore 			if (strlcpy(bootargs_buf, fbarg.gba_bootargs,
1013*753a6d45SSherry Moore 			    buf_size) >= buf_size) {
1014*753a6d45SSherry Moore 				grub_cleanup_boot_args(&fbarg);
1015*753a6d45SSherry Moore 				bcopy(bootargs_saved, bootargs_buf, buf_size);
1016*753a6d45SSherry Moore 				rc = E2BIG;
1017*753a6d45SSherry Moore 			}
1018*753a6d45SSherry Moore 		}
1019*753a6d45SSherry Moore 		/* Failed to read GRUB menu, fall back to normal reboot */
1020*753a6d45SSherry Moore 		if (rc != 0) {
1021*753a6d45SSherry Moore 			(void) fprintf(stderr,
1022*753a6d45SSherry Moore 			    gettext("%s: Failed to process GRUB menu "
1023*753a6d45SSherry Moore 			    "entry for fast reboot.\n\t%s\n"),
1024*753a6d45SSherry Moore 			    cmdname, grub_strerror(rc));
1025*753a6d45SSherry Moore 			(void) fprintf(stderr,
1026*753a6d45SSherry Moore 			    gettext("%s: Falling back to regular reboot.\n"),
1027*753a6d45SSherry Moore 			    cmdname);
1028*753a6d45SSherry Moore 			return (-1);
1029*753a6d45SSherry Moore 		}
1030*753a6d45SSherry Moore 		/* No need to process further */
1031*753a6d45SSherry Moore 		fbarg_used = &fbarg;
1032*753a6d45SSherry Moore 		fbarg_entnum = entnum;
1033*753a6d45SSherry Moore 		return (0);
1034*753a6d45SSherry Moore 	}
1035*753a6d45SSherry Moore #endif	/* __i386 */
1036*753a6d45SSherry Moore 
1037*753a6d45SSherry Moore 	/* Zero out the boot argument buffer as we will reconstruct it */
1038*753a6d45SSherry Moore 	bzero(bootargs_buf, buf_size);
1039*753a6d45SSherry Moore 	bzero(bootfs_arg, sizeof (bootfs_arg));
1040*753a6d45SSherry Moore 	bzero(unixfile, sizeof (unixfile));
1041*753a6d45SSherry Moore 
104219397407SSherry Moore 	if (bename && (rc = fastboot_bename(bename, mountpoint,
104319397407SSherry Moore 	    sizeof (mountpoint))) != 0)
1044*753a6d45SSherry Moore 		return (EINVAL);
1045*753a6d45SSherry Moore 
104619397407SSherry Moore 
104719397407SSherry Moore 	/*
104819397407SSherry Moore 	 * If BE is not specified, look for disk argument to construct
104919397407SSherry Moore 	 * mountpoint; if BE has been specified, mountpoint has already been
105019397407SSherry Moore 	 * constructed.
105119397407SSherry Moore 	 */
105219397407SSherry Moore 	if (newarg && newarg[0] != '-' && !bename) {
105319397407SSherry Moore 		int tmprc;
105419397407SSherry Moore 
105519397407SSherry Moore 		if ((tmprc = validate_disk(newarg, mountpoint)) == 0) {
105619397407SSherry Moore 			/*
105719397407SSherry Moore 			 * The first argument is a valid root argument.
105819397407SSherry Moore 			 * Get the next argument.
105919397407SSherry Moore 			 */
106019397407SSherry Moore 			newarg = strtok(NULL, " ");
106119397407SSherry Moore 			rootlen = (newarg) ? (newarg - head) : buflen;
106219397407SSherry Moore 			(void) strlcpy(fastboot_mounted, mountpoint,
106319397407SSherry Moore 			    sizeof (fastboot_mounted));
106419397407SSherry Moore 
106519397407SSherry Moore 		} else if (tmprc == -1) {
106619397407SSherry Moore 			/*
106719397407SSherry Moore 			 * Not a disk argument.  Use / as default root.
106819397407SSherry Moore 			 */
106919397407SSherry Moore 			bcopy("/", mountpoint, 1);
107019397407SSherry Moore 			bzero(&mountpoint[1], sizeof (mountpoint) - 1);
107119397407SSherry Moore 		} else {
107219397407SSherry Moore 			/*
107319397407SSherry Moore 			 * Disk argument, but not valid or not root.
107419397407SSherry Moore 			 * Return failure.
107519397407SSherry Moore 			 */
107619397407SSherry Moore 			return (EINVAL);
107719397407SSherry Moore 		}
107819397407SSherry Moore 	}
107919397407SSherry Moore 
108019397407SSherry Moore 	/*
108119397407SSherry Moore 	 * Make mountpoint the first part of unixfile.
108219397407SSherry Moore 	 * If there is not disk argument, and BE has not been specified,
108319397407SSherry Moore 	 * mountpoint could be empty.
108419397407SSherry Moore 	 */
108519397407SSherry Moore 	mplen = strlen(mountpoint);
108619397407SSherry Moore 	bcopy(mountpoint, unixfile, mplen);
108719397407SSherry Moore 
108819397407SSherry Moore 	/*
108919397407SSherry Moore 	 * Look for unix argument
109019397407SSherry Moore 	 */
109119397407SSherry Moore 	if (newarg && newarg[0] != '-') {
109219397407SSherry Moore 		bcopy(newarg, &unixfile[mplen], strlen(newarg));
109319397407SSherry Moore 		newarg = strtok(NULL, " ");
109419397407SSherry Moore 		rootlen = (newarg) ? (newarg - head) : buflen;
109519397407SSherry Moore 	} else if (mplen != 0) {
109619397407SSherry Moore 		/*
109719397407SSherry Moore 		 * No unix argument, but mountpoint is not empty, use
109819397407SSherry Moore 		 * /platform/i86pc/$ISADIR/kernel/unix as default.
109919397407SSherry Moore 		 */
110019397407SSherry Moore 		char isa[20];
110119397407SSherry Moore 
110219397407SSherry Moore 		if (sysinfo(SI_ARCHITECTURE_64, isa, sizeof (isa)) != -1)
110319397407SSherry Moore 			(void) snprintf(&unixfile[mplen],
110419397407SSherry Moore 			    sizeof (unixfile) - mplen,
110519397407SSherry Moore 			    "/platform/i86pc/kernel/%s/unix", isa);
110619397407SSherry Moore 		else if (sysinfo(SI_ARCHITECTURE_32, isa, sizeof (isa)) != -1) {
110719397407SSherry Moore 			(void) snprintf(&unixfile[mplen],
110819397407SSherry Moore 			    sizeof (unixfile) - mplen,
110919397407SSherry Moore 			    "/platform/i86pc/kernel/unix");
111019397407SSherry Moore 		} else {
111119397407SSherry Moore 			(void) fprintf(stderr,
1112*753a6d45SSherry Moore 			    gettext("%s: Unknown architecture"), cmdname);
111319397407SSherry Moore 			return (EINVAL);
111419397407SSherry Moore 		}
111519397407SSherry Moore 	}
111619397407SSherry Moore 
111719397407SSherry Moore 	/*
111819397407SSherry Moore 	 * We now have the complete unix argument.  Verify that it exists and
111919397407SSherry Moore 	 * is an ELF file.  Split the argument up into mountpoint and unix
112019397407SSherry Moore 	 * portions again.  This is necessary to handle cases where mountpoint
112119397407SSherry Moore 	 * is specified on the command line as part of the unix argument,
112219397407SSherry Moore 	 * such as this:
112319397407SSherry Moore 	 *	# reboot -f /.alt/platform/i86pc/kernel/amd64/unix
112419397407SSherry Moore 	 */
112519397407SSherry Moore 	unixlen = strlen(unixfile);
112619397407SSherry Moore 	if (unixlen > 0) {
112719397407SSherry Moore 		if (validate_unix(unixfile, &mplen, &is_zfs,
112819397407SSherry Moore 		    bootfs_arg, failsafe) != 0) {
112919397407SSherry Moore 			/* Not a valid unix file */
113019397407SSherry Moore 			return (EINVAL);
113119397407SSherry Moore 		} else {
11326bc8bc6aSSherry Moore 			int space = 0;
113319397407SSherry Moore 			/*
113419397407SSherry Moore 			 * Construct boot argument.
113519397407SSherry Moore 			 */
113619397407SSherry Moore 			unixlen = strlen(unixfile);
11376bc8bc6aSSherry Moore 
11386bc8bc6aSSherry Moore 			/*
11396bc8bc6aSSherry Moore 			 * mdep cannot start with space because bootadm
11406bc8bc6aSSherry Moore 			 * creates bogus menu entries if it does.
11416bc8bc6aSSherry Moore 			 */
11426bc8bc6aSSherry Moore 			if (mplen > 0) {
114319397407SSherry Moore 				bcopy(unixfile, bootargs_buf, mplen);
114419397407SSherry Moore 				(void) strcat(bootargs_buf, " ");
11456bc8bc6aSSherry Moore 				space = 1;
11466bc8bc6aSSherry Moore 			}
11476bc8bc6aSSherry Moore 			bcopy(&unixfile[mplen], &bootargs_buf[mplen + space],
114819397407SSherry Moore 			    unixlen - mplen);
114919397407SSherry Moore 			(void) strcat(bootargs_buf, " ");
11506bc8bc6aSSherry Moore 			off += unixlen + space + 1;
115119397407SSherry Moore 		}
115219397407SSherry Moore 	} else {
115319397407SSherry Moore 		/* Check to see if root is zfs */
115419397407SSherry Moore 		const char	*dp;
115519397407SSherry Moore 		(void) get_zfs_bootfs_arg("/", &dp, &is_zfs, bootfs_arg);
115619397407SSherry Moore 	}
115719397407SSherry Moore 
115819397407SSherry Moore 	if (is_zfs && (buflen != 0 || bename != NULL))	{
115919397407SSherry Moore 		/* LINTED E_SEC_SPRINTF_UNBOUNDED_COPY */
116019397407SSherry Moore 		off += sprintf(bootargs_buf + off, "%s ", bootfs_arg);
116119397407SSherry Moore 	}
116219397407SSherry Moore 
116319397407SSherry Moore 	/*
116419397407SSherry Moore 	 * Copy the rest of the arguments
116519397407SSherry Moore 	 */
116619397407SSherry Moore 	bcopy(&bootargs_saved[rootlen], &bootargs_buf[off], buflen - rootlen);
116719397407SSherry Moore 
116819397407SSherry Moore 	return (rc);
116919397407SSherry Moore }
117019397407SSherry Moore 
1171de0cf919SEnrico Perla - Sun Microsystems #define	MAXARGS		5
1172de0cf919SEnrico Perla - Sun Microsystems 
1173de0cf919SEnrico Perla - Sun Microsystems static void
1174de0cf919SEnrico Perla - Sun Microsystems do_archives_update(int do_fast_reboot)
1175de0cf919SEnrico Perla - Sun Microsystems {
1176de0cf919SEnrico Perla - Sun Microsystems 	int	r, i = 0;
1177de0cf919SEnrico Perla - Sun Microsystems 	pid_t	pid;
1178de0cf919SEnrico Perla - Sun Microsystems 	char	*cmd_argv[MAXARGS];
1179de0cf919SEnrico Perla - Sun Microsystems 
1180de0cf919SEnrico Perla - Sun Microsystems 
1181de0cf919SEnrico Perla - Sun Microsystems 	cmd_argv[i++] = "/sbin/bootadm";
1182de0cf919SEnrico Perla - Sun Microsystems 	cmd_argv[i++] = "-ea";
1183de0cf919SEnrico Perla - Sun Microsystems 	cmd_argv[i++] = "update_all";
1184de0cf919SEnrico Perla - Sun Microsystems 	if (do_fast_reboot)
1185de0cf919SEnrico Perla - Sun Microsystems 		cmd_argv[i++] = "fastboot";
1186de0cf919SEnrico Perla - Sun Microsystems 	cmd_argv[i] = NULL;
1187de0cf919SEnrico Perla - Sun Microsystems 
1188de0cf919SEnrico Perla - Sun Microsystems 	r = posix_spawn(&pid, cmd_argv[0], NULL, NULL, cmd_argv, NULL);
1189de0cf919SEnrico Perla - Sun Microsystems 
1190de0cf919SEnrico Perla - Sun Microsystems 	/* if posix_spawn fails we emit a warning and continue */
1191de0cf919SEnrico Perla - Sun Microsystems 
1192de0cf919SEnrico Perla - Sun Microsystems 	if (r != 0)
1193de0cf919SEnrico Perla - Sun Microsystems 		(void) fprintf(stderr, gettext("%s: WARNING, unable to start "
1194de0cf919SEnrico Perla - Sun Microsystems 		    "boot archive update\n"), cmdname);
1195de0cf919SEnrico Perla - Sun Microsystems 	else
1196de0cf919SEnrico Perla - Sun Microsystems 		while (waitpid(pid, NULL, 0) == -1 && errno == EINTR)
1197de0cf919SEnrico Perla - Sun Microsystems 			;
1198de0cf919SEnrico Perla - Sun Microsystems }
1199de0cf919SEnrico Perla - Sun Microsystems 
12007c478bd9Sstevel@tonic-gate int
12017c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
12027c478bd9Sstevel@tonic-gate {
12037c478bd9Sstevel@tonic-gate 	char *ttyn = ttyname(STDERR_FILENO);
12047c478bd9Sstevel@tonic-gate 
12057c478bd9Sstevel@tonic-gate 	int qflag = 0, needlog = 1, nosync = 0;
120619397407SSherry Moore 	int fast_reboot = 0;
1207*753a6d45SSherry Moore 	int prom_reboot = 0;
12087c478bd9Sstevel@tonic-gate 	uintptr_t mdep = NULL;
12097c478bd9Sstevel@tonic-gate 	int cmd, fcn, c, aval, r;
12107c478bd9Sstevel@tonic-gate 	const char *usage;
12118e07a41fSKonstantin Ananyev 	const char *optstring;
12127c478bd9Sstevel@tonic-gate 	zoneid_t zoneid = getzoneid();
12133f2f09c1Sdp 	int need_check_zones = 0;
12143f2f09c1Sdp 	char bootargs_buf[BOOTARGS_MAX];
121519397407SSherry Moore 	int failsafe = 0;
121619397407SSherry Moore 	char *bename = NULL;
12177c478bd9Sstevel@tonic-gate 
12187c478bd9Sstevel@tonic-gate 	const char * const resetting = "/etc/svc/volatile/resetting";
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
12217c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
12227c478bd9Sstevel@tonic-gate 
12233f2f09c1Sdp 	cmdname = basename(argv[0]);
12243f2f09c1Sdp 
12257c478bd9Sstevel@tonic-gate 	if (strcmp(cmdname, "halt") == 0) {
12267c478bd9Sstevel@tonic-gate 		(void) audit_halt_setup(argc, argv);
12278e07a41fSKonstantin Ananyev 		optstring = "dlnqy";
12287c478bd9Sstevel@tonic-gate 		usage = gettext("usage: %s [ -dlnqy ]\n");
12297c478bd9Sstevel@tonic-gate 		cmd = A_SHUTDOWN;
12307c478bd9Sstevel@tonic-gate 		fcn = AD_HALT;
12317c478bd9Sstevel@tonic-gate 	} else if (strcmp(cmdname, "poweroff") == 0) {
12327c478bd9Sstevel@tonic-gate 		(void) audit_halt_setup(argc, argv);
12338e07a41fSKonstantin Ananyev 		optstring = "dlnqy";
12347c478bd9Sstevel@tonic-gate 		usage = gettext("usage: %s [ -dlnqy ]\n");
12357c478bd9Sstevel@tonic-gate 		cmd = A_SHUTDOWN;
12367c478bd9Sstevel@tonic-gate 		fcn = AD_POWEROFF;
12377c478bd9Sstevel@tonic-gate 	} else if (strcmp(cmdname, "reboot") == 0) {
12387c478bd9Sstevel@tonic-gate 		(void) audit_reboot_setup();
123919397407SSherry Moore #if defined(__i386)
1240*753a6d45SSherry Moore 		optstring = "dlnqpfe:";
1241*753a6d45SSherry Moore 		usage = gettext("usage: %s [ -dlnq(p|fe:) ] [ boot args ]\n");
124219397407SSherry Moore #else
12438e07a41fSKonstantin Ananyev 		optstring = "dlnq";
12447c478bd9Sstevel@tonic-gate 		usage = gettext("usage: %s [ -dlnq ] [ boot args ]\n");
124519397407SSherry Moore #endif
12467c478bd9Sstevel@tonic-gate 		cmd = A_SHUTDOWN;
12477c478bd9Sstevel@tonic-gate 		fcn = AD_BOOT;
12487c478bd9Sstevel@tonic-gate 	} else {
12497c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
12507c478bd9Sstevel@tonic-gate 		    gettext("%s: not installed properly\n"), cmdname);
12517c478bd9Sstevel@tonic-gate 		return (1);
12527c478bd9Sstevel@tonic-gate 	}
12537c478bd9Sstevel@tonic-gate 
12548e07a41fSKonstantin Ananyev 	while ((c = getopt(argc, argv, optstring)) != EOF) {
12557c478bd9Sstevel@tonic-gate 		switch (c) {
12567c478bd9Sstevel@tonic-gate 		case 'd':
12577c478bd9Sstevel@tonic-gate 			if (zoneid == GLOBAL_ZONEID)
12587c478bd9Sstevel@tonic-gate 				cmd = A_DUMP;
12597c478bd9Sstevel@tonic-gate 			else {
12607c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
12617c478bd9Sstevel@tonic-gate 				    gettext("%s: -d only valid from global"
12627c478bd9Sstevel@tonic-gate 				    " zone\n"), cmdname);
12637c478bd9Sstevel@tonic-gate 				return (1);
12647c478bd9Sstevel@tonic-gate 			}
12657c478bd9Sstevel@tonic-gate 			break;
12667c478bd9Sstevel@tonic-gate 		case 'l':
12677c478bd9Sstevel@tonic-gate 			needlog = 0;
12687c478bd9Sstevel@tonic-gate 			break;
12697c478bd9Sstevel@tonic-gate 		case 'n':
12707c478bd9Sstevel@tonic-gate 			nosync = 1;
12717c478bd9Sstevel@tonic-gate 			break;
12727c478bd9Sstevel@tonic-gate 		case 'q':
12737c478bd9Sstevel@tonic-gate 			qflag = 1;
12747c478bd9Sstevel@tonic-gate 			break;
12757c478bd9Sstevel@tonic-gate 		case 'y':
12767c478bd9Sstevel@tonic-gate 			ttyn = NULL;
12777c478bd9Sstevel@tonic-gate 			break;
127819397407SSherry Moore #if defined(__i386)
1279*753a6d45SSherry Moore 		case 'p':
1280*753a6d45SSherry Moore 			prom_reboot = 1;
1281*753a6d45SSherry Moore 			break;
128219397407SSherry Moore 		case 'f':
128319397407SSherry Moore 			fast_reboot = 1;
128419397407SSherry Moore 			break;
128519397407SSherry Moore 		case 'e':
128619397407SSherry Moore 			bename = optarg;
128719397407SSherry Moore 			break;
128819397407SSherry Moore #endif
12897c478bd9Sstevel@tonic-gate 		default:
12907c478bd9Sstevel@tonic-gate 			/*
12917c478bd9Sstevel@tonic-gate 			 * TRANSLATION_NOTE
12927c478bd9Sstevel@tonic-gate 			 * Don't translate the words "halt" or "reboot"
12937c478bd9Sstevel@tonic-gate 			 */
12947c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, usage, cmdname);
12957c478bd9Sstevel@tonic-gate 			return (1);
12967c478bd9Sstevel@tonic-gate 		}
12977c478bd9Sstevel@tonic-gate 	}
12987c478bd9Sstevel@tonic-gate 
12997c478bd9Sstevel@tonic-gate 	argc -= optind;
13007c478bd9Sstevel@tonic-gate 	argv += optind;
13017c478bd9Sstevel@tonic-gate 
13027c478bd9Sstevel@tonic-gate 	if (argc != 0) {
13037c478bd9Sstevel@tonic-gate 		if (fcn != AD_BOOT) {
13047c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, usage, cmdname);
13057c478bd9Sstevel@tonic-gate 			return (1);
13067c478bd9Sstevel@tonic-gate 		}
13077c478bd9Sstevel@tonic-gate 
13087c478bd9Sstevel@tonic-gate 		/* Gather the arguments into bootargs_buf. */
13097c478bd9Sstevel@tonic-gate 		if (gather_args(argv, bootargs_buf, sizeof (bootargs_buf)) !=
13107c478bd9Sstevel@tonic-gate 		    0) {
13117c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
13127c478bd9Sstevel@tonic-gate 			    gettext("%s: Boot arguments too long.\n"), cmdname);
13137c478bd9Sstevel@tonic-gate 			return (1);
13147c478bd9Sstevel@tonic-gate 		}
131519397407SSherry Moore 
13167c478bd9Sstevel@tonic-gate 		mdep = (uintptr_t)bootargs_buf;
131719397407SSherry Moore 	} else {
131819397407SSherry Moore 		/*
131919397407SSherry Moore 		 * Initialize it to 0 in case of fastboot, the buffer
132019397407SSherry Moore 		 * will be used.
132119397407SSherry Moore 		 */
132219397407SSherry Moore 		bzero(bootargs_buf, sizeof (bootargs_buf));
13237c478bd9Sstevel@tonic-gate 	}
13247c478bd9Sstevel@tonic-gate 
1325*753a6d45SSherry Moore 	if (geteuid() != 0) {
13267c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
13277c478bd9Sstevel@tonic-gate 		    gettext("%s: permission denied\n"), cmdname);
13287c478bd9Sstevel@tonic-gate 		goto fail;
13297c478bd9Sstevel@tonic-gate 	}
13307c478bd9Sstevel@tonic-gate 
1331*753a6d45SSherry Moore 	if (fast_reboot && prom_reboot) {
1332*753a6d45SSherry Moore 		(void) fprintf(stderr,
1333*753a6d45SSherry Moore 		    gettext("%s: -p and -f are mutually exclusive\n"),
1334*753a6d45SSherry Moore 		    cmdname);
1335*753a6d45SSherry Moore 		return (EINVAL);
1336*753a6d45SSherry Moore 	}
1337*753a6d45SSherry Moore 
133819397407SSherry Moore 	/*
133919397407SSherry Moore 	 * Check whether fast reboot is the default operating mode
134019397407SSherry Moore 	 */
1341*753a6d45SSherry Moore 	if (fcn == AD_BOOT && !fast_reboot && !prom_reboot &&
1342*753a6d45SSherry Moore 	    zoneid == GLOBAL_ZONEID)
1343*753a6d45SSherry Moore 		fast_reboot = scf_is_fastboot_default();
134419397407SSherry Moore 
134519397407SSherry Moore 	if (bename && !fast_reboot)	{
134619397407SSherry Moore 		(void) fprintf(stderr, gettext("%s: -e only valid with -f\n"),
134719397407SSherry Moore 		    cmdname);
134819397407SSherry Moore 		return (EINVAL);
134919397407SSherry Moore 	}
135019397407SSherry Moore 
135119397407SSherry Moore 	/*
135219397407SSherry Moore 	 * If fast reboot, do some sanity check on the argument
135319397407SSherry Moore 	 */
135419397407SSherry Moore 	if (fast_reboot) {
135519397407SSherry Moore 		int rc;
135619397407SSherry Moore 		int is_dryrun = 0;
135719397407SSherry Moore 
135819397407SSherry Moore 		if (zoneid != GLOBAL_ZONEID)	{
135919397407SSherry Moore 			(void) fprintf(stderr,
1360*753a6d45SSherry Moore 			    gettext("%s: Fast reboot only valid from global"
136119397407SSherry Moore 			    " zone\n"), cmdname);
136219397407SSherry Moore 			return (EINVAL);
136319397407SSherry Moore 		}
136419397407SSherry Moore 
1365*753a6d45SSherry Moore 		rc = parse_fastboot_args(bootargs_buf, sizeof (bootargs_buf),
1366*753a6d45SSherry Moore 		    &is_dryrun, bename, &failsafe);
136719397407SSherry Moore 
136819397407SSherry Moore 		/*
136919397407SSherry Moore 		 * If dry run, or if arguments are invalid, return.
137019397407SSherry Moore 		 */
137119397407SSherry Moore 		if (is_dryrun)
137219397407SSherry Moore 			return (rc);
1373*753a6d45SSherry Moore 		else if (rc == EINVAL)
137419397407SSherry Moore 			goto fail;
1375*753a6d45SSherry Moore 		else if (rc != 0)
1376*753a6d45SSherry Moore 			fast_reboot = 0;
137719397407SSherry Moore 
137819397407SSherry Moore 		/*
137919397407SSherry Moore 		 * For all the other errors, we continue on in case user
1380*753a6d45SSherry Moore 		 * user want to force fast reboot, or fall back to regular
1381*753a6d45SSherry Moore 		 * reboot.
138219397407SSherry Moore 		 */
138319397407SSherry Moore 		if (strlen(bootargs_buf) != 0)
138419397407SSherry Moore 			mdep = (uintptr_t)bootargs_buf;
138519397407SSherry Moore 	}
138619397407SSherry Moore 
138719397407SSherry Moore #if 0	/* For debugging */
138819397407SSherry Moore 	if (mdep != NULL)
138919397407SSherry Moore 		(void) fprintf(stderr, "mdep = %s\n", (char *)mdep);
139019397407SSherry Moore #endif
139119397407SSherry Moore 
13927c478bd9Sstevel@tonic-gate 	if (fcn != AD_BOOT && ttyn != NULL &&
13937c478bd9Sstevel@tonic-gate 	    strncmp(ttyn, "/dev/term/", strlen("/dev/term/")) == 0) {
13947c478bd9Sstevel@tonic-gate 		/*
13957c478bd9Sstevel@tonic-gate 		 * TRANSLATION_NOTE
13967c478bd9Sstevel@tonic-gate 		 * Don't translate ``halt -y''
13977c478bd9Sstevel@tonic-gate 		 */
13987c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
13997c478bd9Sstevel@tonic-gate 		    gettext("%s: dangerous on a dialup;"), cmdname);
14007c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
14017c478bd9Sstevel@tonic-gate 		    gettext("use ``%s -y'' if you are really sure\n"), cmdname);
14027c478bd9Sstevel@tonic-gate 		goto fail;
14037c478bd9Sstevel@tonic-gate 	}
14047c478bd9Sstevel@tonic-gate 
14057c478bd9Sstevel@tonic-gate 	if (needlog) {
14067c478bd9Sstevel@tonic-gate 		char *user = getlogin();
14077c478bd9Sstevel@tonic-gate 		struct passwd *pw;
1408f040a7a6Ssetje 		char *tty;
14097c478bd9Sstevel@tonic-gate 
14107c478bd9Sstevel@tonic-gate 		openlog(cmdname, 0, LOG_AUTH);
14117c478bd9Sstevel@tonic-gate 		if (user == NULL && (pw = getpwuid(getuid())) != NULL)
14127c478bd9Sstevel@tonic-gate 			user = pw->pw_name;
14137c478bd9Sstevel@tonic-gate 		if (user == NULL)
14147c478bd9Sstevel@tonic-gate 			user = "root";
1415f040a7a6Ssetje 
1416f040a7a6Ssetje 		tty = ttyname(1);
1417f040a7a6Ssetje 
1418f040a7a6Ssetje 		if (tty == NULL)
1419f040a7a6Ssetje 			syslog(LOG_CRIT, "initiated by %s", user);
1420f040a7a6Ssetje 		else
1421f040a7a6Ssetje 			syslog(LOG_CRIT, "initiated by %s on %s", user, tty);
14227c478bd9Sstevel@tonic-gate 	}
14237c478bd9Sstevel@tonic-gate 
14247c478bd9Sstevel@tonic-gate 	/*
14257c478bd9Sstevel@tonic-gate 	 * We must assume success and log it before auditd is terminated.
14267c478bd9Sstevel@tonic-gate 	 */
14277c478bd9Sstevel@tonic-gate 	if (fcn == AD_BOOT)
14287c478bd9Sstevel@tonic-gate 		aval = audit_reboot_success();
14297c478bd9Sstevel@tonic-gate 	else
14307c478bd9Sstevel@tonic-gate 		aval = audit_halt_success();
14317c478bd9Sstevel@tonic-gate 
14327c478bd9Sstevel@tonic-gate 	if (aval == -1) {
14337c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
14347c478bd9Sstevel@tonic-gate 		    gettext("%s: can't turn off auditd\n"), cmdname);
14357c478bd9Sstevel@tonic-gate 		if (needlog)
14367c478bd9Sstevel@tonic-gate 			(void) sleep(5); /* Give syslogd time to record this */
14377c478bd9Sstevel@tonic-gate 	}
14387c478bd9Sstevel@tonic-gate 
14397c478bd9Sstevel@tonic-gate 	(void) signal(SIGHUP, SIG_IGN);	/* for remote connections */
14407c478bd9Sstevel@tonic-gate 
144126f665e8Sdstaff 	/*
144226f665e8Sdstaff 	 * We start to fork a bunch of zoneadms to halt any active zones.
144326f665e8Sdstaff 	 * This will proceed with halt in parallel until we call
144426f665e8Sdstaff 	 * check_zone_haltedness later on.
144526f665e8Sdstaff 	 */
144626f665e8Sdstaff 	if (zoneid == GLOBAL_ZONEID && cmd != A_DUMP) {
14473f2f09c1Sdp 		need_check_zones = halt_zones();
144826f665e8Sdstaff 	}
144926f665e8Sdstaff 
1450*753a6d45SSherry Moore #if defined(__i386)
1451*753a6d45SSherry Moore 	/* set new default entry in the GRUB entry */
1452*753a6d45SSherry Moore 	if (fbarg_entnum != GRUB_ENTRY_DEFAULT) {
1453*753a6d45SSherry Moore 		char buf[32];
1454*753a6d45SSherry Moore 		(void) snprintf(buf, sizeof (buf), "default=%u", fbarg_entnum);
1455*753a6d45SSherry Moore 		(void) halt_exec(BOOTADM_PROG, "set-menu", buf, NULL);
1456*753a6d45SSherry Moore 	}
1457*753a6d45SSherry Moore #endif	/* __i386 */
145848847494SEnrico Perla - Sun Microsystems 
1459*753a6d45SSherry Moore 	/* if we're dumping, do the archive update here and don't defer it */
1460de0cf919SEnrico Perla - Sun Microsystems 	if (cmd == A_DUMP && zoneid == GLOBAL_ZONEID && !nosync)
1461de0cf919SEnrico Perla - Sun Microsystems 		do_archives_update(fast_reboot);
14627c478bd9Sstevel@tonic-gate 
14637c478bd9Sstevel@tonic-gate 	/*
14647c478bd9Sstevel@tonic-gate 	 * If we're not forcing a crash dump, mark the system as quiescing for
14657c478bd9Sstevel@tonic-gate 	 * smf(5)'s benefit, and idle the init process.
14667c478bd9Sstevel@tonic-gate 	 */
14677c478bd9Sstevel@tonic-gate 	if (cmd != A_DUMP) {
14683f2f09c1Sdp 		if (direct_init(PCDSTOP) == -1) {
14697c478bd9Sstevel@tonic-gate 			/*
14707c478bd9Sstevel@tonic-gate 			 * TRANSLATION_NOTE
14717c478bd9Sstevel@tonic-gate 			 * Don't translate the word "init"
14727c478bd9Sstevel@tonic-gate 			 */
14737c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
14747c478bd9Sstevel@tonic-gate 			    gettext("%s: can't idle init\n"), cmdname);
14757c478bd9Sstevel@tonic-gate 			goto fail;
14767c478bd9Sstevel@tonic-gate 		}
14777c478bd9Sstevel@tonic-gate 
14787c478bd9Sstevel@tonic-gate 		if (creat(resetting, 0755) == -1)
14797c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
14807c478bd9Sstevel@tonic-gate 			    gettext("%s: could not create %s.\n"),
14817c478bd9Sstevel@tonic-gate 			    cmdname, resetting);
14827c478bd9Sstevel@tonic-gate 
14837c478bd9Sstevel@tonic-gate 		/*
14847c478bd9Sstevel@tonic-gate 		 * Stop all restarters so they do not try to restart services
14857c478bd9Sstevel@tonic-gate 		 * that are terminated.
14867c478bd9Sstevel@tonic-gate 		 */
14877c478bd9Sstevel@tonic-gate 		stop_restarters();
148826f665e8Sdstaff 
148926f665e8Sdstaff 		/*
149026f665e8Sdstaff 		 * Wait a little while for zones to shutdown.
149126f665e8Sdstaff 		 */
149226f665e8Sdstaff 		if (need_check_zones) {
14933f2f09c1Sdp 			check_zones_haltedness();
149426f665e8Sdstaff 
149526f665e8Sdstaff 			(void) fprintf(stderr,
149626f665e8Sdstaff 			    gettext("%s: Completing system halt.\n"),
149726f665e8Sdstaff 			    cmdname);
149826f665e8Sdstaff 		}
14997c478bd9Sstevel@tonic-gate 	}
15007c478bd9Sstevel@tonic-gate 
15017c478bd9Sstevel@tonic-gate 	/*
15027c478bd9Sstevel@tonic-gate 	 * Make sure we don't get stopped by a jobcontrol shell
15037c478bd9Sstevel@tonic-gate 	 * once we start killing everybody.
15047c478bd9Sstevel@tonic-gate 	 */
15057c478bd9Sstevel@tonic-gate 	(void) signal(SIGTSTP, SIG_IGN);
15067c478bd9Sstevel@tonic-gate 	(void) signal(SIGTTIN, SIG_IGN);
15077c478bd9Sstevel@tonic-gate 	(void) signal(SIGTTOU, SIG_IGN);
150888852e7dSKonstantin Ananyev 	(void) signal(SIGPIPE, SIG_IGN);
15097c478bd9Sstevel@tonic-gate 	(void) signal(SIGTERM, SIG_IGN);
15107c478bd9Sstevel@tonic-gate 
15117c478bd9Sstevel@tonic-gate 	/*
15127c478bd9Sstevel@tonic-gate 	 * If we're not forcing a crash dump, give everyone 5 seconds to
15137c478bd9Sstevel@tonic-gate 	 * handle a SIGTERM and clean up properly.
15147c478bd9Sstevel@tonic-gate 	 */
15157c478bd9Sstevel@tonic-gate 	if (cmd != A_DUMP) {
151648847494SEnrico Perla - Sun Microsystems 		int	start, end, delta;
151748847494SEnrico Perla - Sun Microsystems 
15187c478bd9Sstevel@tonic-gate 		(void) kill(-1, SIGTERM);
151948847494SEnrico Perla - Sun Microsystems 		start = time(NULL);
152048847494SEnrico Perla - Sun Microsystems 
1521de0cf919SEnrico Perla - Sun Microsystems 		if (zoneid == GLOBAL_ZONEID && !nosync)
1522de0cf919SEnrico Perla - Sun Microsystems 			do_archives_update(fast_reboot);
152348847494SEnrico Perla - Sun Microsystems 
152448847494SEnrico Perla - Sun Microsystems 		end = time(NULL);
152548847494SEnrico Perla - Sun Microsystems 		delta = end - start;
152648847494SEnrico Perla - Sun Microsystems 		if (delta < 5)
152748847494SEnrico Perla - Sun Microsystems 			(void) sleep(5 - delta);
15287c478bd9Sstevel@tonic-gate 	}
15297c478bd9Sstevel@tonic-gate 
15303a0fa635SEnrico Perla - Sun Microsystems 	(void) signal(SIGINT, SIG_IGN);
15313a0fa635SEnrico Perla - Sun Microsystems 
15327c478bd9Sstevel@tonic-gate 	if (!qflag && !nosync) {
15337c478bd9Sstevel@tonic-gate 		struct utmpx wtmpx;
15347c478bd9Sstevel@tonic-gate 
15357c478bd9Sstevel@tonic-gate 		bzero(&wtmpx, sizeof (struct utmpx));
15367c478bd9Sstevel@tonic-gate 		(void) strcpy(wtmpx.ut_line, "~");
15377c478bd9Sstevel@tonic-gate 		(void) time(&wtmpx.ut_tv.tv_sec);
15387c478bd9Sstevel@tonic-gate 
15397c478bd9Sstevel@tonic-gate 		if (cmd == A_DUMP)
15407c478bd9Sstevel@tonic-gate 			(void) strcpy(wtmpx.ut_name, "crash dump");
15417c478bd9Sstevel@tonic-gate 		else
15427c478bd9Sstevel@tonic-gate 			(void) strcpy(wtmpx.ut_name, "shutdown");
15437c478bd9Sstevel@tonic-gate 
15447c478bd9Sstevel@tonic-gate 		(void) updwtmpx(WTMPX_FILE, &wtmpx);
15457c478bd9Sstevel@tonic-gate 		sync();
15467c478bd9Sstevel@tonic-gate 	}
15477c478bd9Sstevel@tonic-gate 
15487c478bd9Sstevel@tonic-gate 	if (cmd == A_DUMP && nosync != 0)
15497c478bd9Sstevel@tonic-gate 		(void) uadmin(A_DUMP, AD_NOSYNC, NULL);
15507c478bd9Sstevel@tonic-gate 
155119397407SSherry Moore 	if (fast_reboot) {
155219397407SSherry Moore 		if (failsafe)
155319397407SSherry Moore 			(void) fprintf(stderr, "Fast reboot - failsafe.\n");
155419397407SSherry Moore 		else
155519397407SSherry Moore 			(void) fprintf(stderr, "Fast reboot.\n");
155619397407SSherry Moore 
155719397407SSherry Moore 		fcn = AD_FASTREBOOT;
155819397407SSherry Moore 	}
155919397407SSherry Moore 
15603f2f09c1Sdp 	if (uadmin(cmd, fcn, mdep) == -1)
15613f2f09c1Sdp 		(void) fprintf(stderr, "%s: uadmin failed: %s\n",
15623f2f09c1Sdp 		    cmdname, strerror(errno));
15633f2f09c1Sdp 	else
15643f2f09c1Sdp 		(void) fprintf(stderr, "%s: uadmin unexpectedly returned 0\n",
15653f2f09c1Sdp 		    cmdname);
15663f2f09c1Sdp 
15673f2f09c1Sdp 	do {
15687c478bd9Sstevel@tonic-gate 		r = remove(resetting);
15693f2f09c1Sdp 	} while (r != 0 && errno == EINTR);
15703f2f09c1Sdp 
15717c478bd9Sstevel@tonic-gate 	if (r != 0 && errno != ENOENT)
15727c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: could not remove %s.\n"),
15737c478bd9Sstevel@tonic-gate 		    cmdname, resetting);
15747c478bd9Sstevel@tonic-gate 
15753f2f09c1Sdp 	if (direct_init(PCRUN) == -1) {
15763f2f09c1Sdp 		/*
15773f2f09c1Sdp 		 * TRANSLATION_NOTE
15783f2f09c1Sdp 		 * Don't translate the word "init"
15793f2f09c1Sdp 		 */
15803f2f09c1Sdp 		(void) fprintf(stderr,
15813f2f09c1Sdp 		    gettext("%s: can't resume init\n"), cmdname);
15823f2f09c1Sdp 	}
15833f2f09c1Sdp 
15847c478bd9Sstevel@tonic-gate 	continue_restarters();
15857c478bd9Sstevel@tonic-gate 
15863f2f09c1Sdp 	if (get_initpid() != -1)
15877c478bd9Sstevel@tonic-gate 		/* tell init to restate current level */
15883f2f09c1Sdp 		(void) kill(get_initpid(), SIGHUP);
15897c478bd9Sstevel@tonic-gate 
15907c478bd9Sstevel@tonic-gate fail:
15917c478bd9Sstevel@tonic-gate 	if (fcn == AD_BOOT)
15927c478bd9Sstevel@tonic-gate 		(void) audit_reboot_fail();
15937c478bd9Sstevel@tonic-gate 	else
15947c478bd9Sstevel@tonic-gate 		(void) audit_halt_fail();
15957c478bd9Sstevel@tonic-gate 
159619397407SSherry Moore 	if (fast_reboot) {
159719397407SSherry Moore 		if (bename) {
15984e1f1c13SKonstantin Ananyev 			(void) halt_exec(LUUMOUNT_PROG, "-n", bename, NULL);
159919397407SSherry Moore 
160019397407SSherry Moore 		} else if (strlen(fastboot_mounted) != 0) {
160119397407SSherry Moore 			(void) umount(fastboot_mounted);
1602*753a6d45SSherry Moore #if defined(__i386)
1603*753a6d45SSherry Moore 		} else if (fbarg_used != NULL) {
1604*753a6d45SSherry Moore 			grub_cleanup_boot_args(fbarg_used);
1605*753a6d45SSherry Moore #endif	/* __i386 */
160619397407SSherry Moore 		}
160719397407SSherry Moore 	}
160819397407SSherry Moore 
16097c478bd9Sstevel@tonic-gate 	return (1);
16107c478bd9Sstevel@tonic-gate }
1611