xref: /titanic_51/usr/src/cmd/halt/halt.c (revision e557d412e15c7f384b2ea3bf316a739a0f81cd55)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
53f2f09c1Sdp  * Common Development and Distribution License (the "License").
63f2f09c1Sdp  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*e557d412SChristopher Kiick  * Copyright 2010 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>
63753a6d45SSherry 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>
83753a6d45SSherry Moore #if defined(__i386)
84753a6d45SSherry Moore #include <libgrubmgmt.h>
85753a6d45SSherry 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 
125753a6d45SSherry 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 
131753a6d45SSherry Moore /*
132753a6d45SSherry Moore  * Fast Reboot related variables
133753a6d45SSherry Moore  */
13419397407SSherry Moore static char	fastboot_mounted[MAXPATHLEN];
135*e557d412SChristopher Kiick 
136753a6d45SSherry Moore #if defined(__i386)
137753a6d45SSherry Moore static grub_boot_args_t	fbarg;
138753a6d45SSherry Moore static grub_boot_args_t	*fbarg_used;
139753a6d45SSherry Moore static int fbarg_entnum = GRUB_ENTRY_DEFAULT;
140753a6d45SSherry Moore #endif	/* __i386 */
14119397407SSherry Moore 
14219397407SSherry Moore static int validate_ufs_disk(char *, char *);
14319397407SSherry Moore static int validate_zfs_pool(char *, char *);
14419397407SSherry Moore 
1453f2f09c1Sdp static pid_t
1463f2f09c1Sdp get_initpid()
1473f2f09c1Sdp {
1483f2f09c1Sdp 	static int init_pid = -1;
1493f2f09c1Sdp 
1503f2f09c1Sdp 	if (init_pid == -1) {
1513f2f09c1Sdp 		if (zone_getattr(getzoneid(), ZONE_ATTR_INITPID, &init_pid,
1523f2f09c1Sdp 		    sizeof (init_pid)) != sizeof (init_pid)) {
1533f2f09c1Sdp 			assert(errno == ESRCH);
1543f2f09c1Sdp 			init_pid = -1;
1553f2f09c1Sdp 		}
1563f2f09c1Sdp 	}
1573f2f09c1Sdp 	return (init_pid);
1583f2f09c1Sdp }
1593f2f09c1Sdp 
1603f2f09c1Sdp /*
1613f2f09c1Sdp  * Quiesce or resume init using /proc.  When stopping init, we can't send
1623f2f09c1Sdp  * SIGTSTP (since init ignores it) or SIGSTOP (since the kernel won't permit
1633f2f09c1Sdp  * it).
1643f2f09c1Sdp  */
1653f2f09c1Sdp static int
1663f2f09c1Sdp direct_init(long command)
1673f2f09c1Sdp {
1683f2f09c1Sdp 	char ctlfile[MAXPATHLEN];
1693f2f09c1Sdp 	pid_t pid;
1703f2f09c1Sdp 	int ctlfd;
1713f2f09c1Sdp 
1723f2f09c1Sdp 	assert(command == PCDSTOP || command == PCRUN);
1733f2f09c1Sdp 	if ((pid = get_initpid()) == -1) {
1743f2f09c1Sdp 		return (-1);
1753f2f09c1Sdp 	}
1763f2f09c1Sdp 
1773f2f09c1Sdp 	(void) snprintf(ctlfile, sizeof (ctlfile), "/proc/%d/ctl", pid);
1783f2f09c1Sdp 	if ((ctlfd = open(ctlfile, O_WRONLY)) == -1)
1793f2f09c1Sdp 		return (-1);
1803f2f09c1Sdp 
1813f2f09c1Sdp 	if (command == PCDSTOP) {
1823f2f09c1Sdp 		if (write(ctlfd, &command, sizeof (long)) == -1) {
1833f2f09c1Sdp 			(void) close(ctlfd);
1843f2f09c1Sdp 			return (-1);
1853f2f09c1Sdp 		}
1863f2f09c1Sdp 	} else {	/* command == PCRUN */
1873f2f09c1Sdp 		long cmds[2];
1883f2f09c1Sdp 		cmds[0] = command;
1893f2f09c1Sdp 		cmds[1] = 0;
1903f2f09c1Sdp 		if (write(ctlfd, cmds, sizeof (cmds)) == -1) {
1913f2f09c1Sdp 			(void) close(ctlfd);
1923f2f09c1Sdp 			return (-1);
1933f2f09c1Sdp 		}
1943f2f09c1Sdp 	}
1953f2f09c1Sdp 	(void) close(ctlfd);
1963f2f09c1Sdp 	return (0);
1973f2f09c1Sdp }
1983f2f09c1Sdp 
1997c478bd9Sstevel@tonic-gate static void
2007c478bd9Sstevel@tonic-gate stop_startd()
2017c478bd9Sstevel@tonic-gate {
2027c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
2037c478bd9Sstevel@tonic-gate 	scf_property_t *prop = NULL;
2047c478bd9Sstevel@tonic-gate 	scf_value_t *val = NULL;
2057c478bd9Sstevel@tonic-gate 	uint64_t uint64;
2067c478bd9Sstevel@tonic-gate 
2073f2f09c1Sdp 	if ((h = scf_handle_create(SCF_VERSION)) == NULL)
2087c478bd9Sstevel@tonic-gate 		return;
2097c478bd9Sstevel@tonic-gate 
2103f2f09c1Sdp 	if ((scf_handle_bind(h) != 0) ||
2113f2f09c1Sdp 	    ((prop = scf_property_create(h)) == NULL) ||
2123f2f09c1Sdp 	    ((val = scf_value_create(h)) == NULL))
2137c478bd9Sstevel@tonic-gate 		goto out;
2147c478bd9Sstevel@tonic-gate 
2153f2f09c1Sdp 	if (scf_handle_decode_fmri(h, FMRI_STARTD_CONTRACT,
2163f2f09c1Sdp 	    NULL, NULL, NULL, NULL, prop, SCF_DECODE_FMRI_EXACT) != 0)
2177c478bd9Sstevel@tonic-gate 		goto out;
2187c478bd9Sstevel@tonic-gate 
2193f2f09c1Sdp 	if (scf_property_is_type(prop, SCF_TYPE_COUNT) != 0 ||
2203f2f09c1Sdp 	    scf_property_get_value(prop, val) != 0 ||
2213f2f09c1Sdp 	    scf_value_get_count(val, &uint64) != 0)
2227c478bd9Sstevel@tonic-gate 		goto out;
2237c478bd9Sstevel@tonic-gate 
2243f2f09c1Sdp 	startdct = (ctid_t)uint64;
2253f2f09c1Sdp 	(void) sigsend(P_CTID, startdct, SIGSTOP);
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate out:
2287c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
2297c478bd9Sstevel@tonic-gate 	scf_value_destroy(val);
2307c478bd9Sstevel@tonic-gate 	scf_handle_destroy(h);
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate static void
2347c478bd9Sstevel@tonic-gate continue_startd()
2357c478bd9Sstevel@tonic-gate {
2367c478bd9Sstevel@tonic-gate 	if (startdct != -1)
2377c478bd9Sstevel@tonic-gate 		(void) sigsend(P_CTID, startdct, SIGCONT);
2387c478bd9Sstevel@tonic-gate }
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate #define	FMRI_RESTARTER_PROP "/:properties/general/restarter"
2417c478bd9Sstevel@tonic-gate #define	FMRI_CONTRACT_PROP "/:properties/restarter/contract"
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate static int
2447c478bd9Sstevel@tonic-gate save_ctid(ctid_t ctid)
2457c478bd9Sstevel@tonic-gate {
2467c478bd9Sstevel@tonic-gate 	ctidlist_t *next;
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	for (next = ctidlist; next != NULL; next = next->next)
2497c478bd9Sstevel@tonic-gate 		if (next->ctid == ctid)
2507c478bd9Sstevel@tonic-gate 			return (-1);
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	next = (ctidlist_t *)malloc(sizeof (ctidlist_t));
2537c478bd9Sstevel@tonic-gate 	if (next == NULL)
2547c478bd9Sstevel@tonic-gate 		return (-1);
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	next->ctid = ctid;
2577c478bd9Sstevel@tonic-gate 	next->next = ctidlist;
2587c478bd9Sstevel@tonic-gate 	ctidlist = next;
2597c478bd9Sstevel@tonic-gate 	return (0);
2607c478bd9Sstevel@tonic-gate }
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate static void
2637c478bd9Sstevel@tonic-gate stop_delegates()
2647c478bd9Sstevel@tonic-gate {
2657c478bd9Sstevel@tonic-gate 	ctid_t ctid;
2667c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
2677c478bd9Sstevel@tonic-gate 	scf_scope_t *sc = NULL;
2687c478bd9Sstevel@tonic-gate 	scf_service_t *svc = NULL;
2697c478bd9Sstevel@tonic-gate 	scf_instance_t *inst = NULL;
2707c478bd9Sstevel@tonic-gate 	scf_snapshot_t *snap = NULL;
2717c478bd9Sstevel@tonic-gate 	scf_snapshot_t *isnap = NULL;
2727c478bd9Sstevel@tonic-gate 	scf_propertygroup_t *pg = NULL;
2737c478bd9Sstevel@tonic-gate 	scf_property_t *prop = NULL;
2747c478bd9Sstevel@tonic-gate 	scf_value_t *val = NULL;
2757c478bd9Sstevel@tonic-gate 	scf_iter_t *siter = NULL;
2767c478bd9Sstevel@tonic-gate 	scf_iter_t *iiter = NULL;
2777c478bd9Sstevel@tonic-gate 	char *fmri;
2787c478bd9Sstevel@tonic-gate 	ssize_t length;
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	uint64_t uint64;
2817c478bd9Sstevel@tonic-gate 	ssize_t bytes;
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	length = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH);
2847c478bd9Sstevel@tonic-gate 	if (length <= 0)
2857c478bd9Sstevel@tonic-gate 		return;
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	length++;
2887c478bd9Sstevel@tonic-gate 	fmri = alloca(length * sizeof (char));
2897c478bd9Sstevel@tonic-gate 
2903f2f09c1Sdp 	if ((h = scf_handle_create(SCF_VERSION)) == NULL)
2917c478bd9Sstevel@tonic-gate 		return;
2927c478bd9Sstevel@tonic-gate 
2933f2f09c1Sdp 	if (scf_handle_bind(h) != 0) {
2947c478bd9Sstevel@tonic-gate 		scf_handle_destroy(h);
2957c478bd9Sstevel@tonic-gate 		return;
2967c478bd9Sstevel@tonic-gate 	}
2977c478bd9Sstevel@tonic-gate 
2983f2f09c1Sdp 	if ((sc = scf_scope_create(h)) == NULL ||
2993f2f09c1Sdp 	    (svc = scf_service_create(h)) == NULL ||
3003f2f09c1Sdp 	    (inst = scf_instance_create(h)) == NULL ||
3013f2f09c1Sdp 	    (snap = scf_snapshot_create(h)) == NULL ||
3023f2f09c1Sdp 	    (pg = scf_pg_create(h)) == NULL ||
3033f2f09c1Sdp 	    (prop = scf_property_create(h)) == NULL ||
3043f2f09c1Sdp 	    (val = scf_value_create(h)) == NULL ||
3053f2f09c1Sdp 	    (siter = scf_iter_create(h)) == NULL ||
3063f2f09c1Sdp 	    (iiter = scf_iter_create(h)) == NULL)
3077c478bd9Sstevel@tonic-gate 		goto out;
3087c478bd9Sstevel@tonic-gate 
3093f2f09c1Sdp 	if (scf_handle_get_scope(h, SCF_SCOPE_LOCAL, sc) != 0)
3107c478bd9Sstevel@tonic-gate 		goto out;
3117c478bd9Sstevel@tonic-gate 
3123f2f09c1Sdp 	if (scf_iter_scope_services(siter, sc) != 0)
3137c478bd9Sstevel@tonic-gate 		goto out;
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	while (scf_iter_next_service(siter, svc) == 1) {
3167c478bd9Sstevel@tonic-gate 
3173f2f09c1Sdp 		if (scf_iter_service_instances(iiter, svc) != 0)
3187c478bd9Sstevel@tonic-gate 			continue;
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 		while (scf_iter_next_instance(iiter, inst) == 1) {
3217c478bd9Sstevel@tonic-gate 
3223f2f09c1Sdp 			if ((scf_instance_get_snapshot(inst, "running",
3233f2f09c1Sdp 			    snap)) != 0)
3247c478bd9Sstevel@tonic-gate 				isnap = NULL;
3257c478bd9Sstevel@tonic-gate 			else
3267c478bd9Sstevel@tonic-gate 				isnap = snap;
3277c478bd9Sstevel@tonic-gate 
3283f2f09c1Sdp 			if (scf_instance_get_pg_composed(inst, isnap,
3293f2f09c1Sdp 			    SCF_PG_GENERAL, pg) != 0)
3307c478bd9Sstevel@tonic-gate 				continue;
3317c478bd9Sstevel@tonic-gate 
3323f2f09c1Sdp 			if (scf_pg_get_property(pg, SCF_PROPERTY_RESTARTER,
3333f2f09c1Sdp 			    prop) != 0 ||
3343f2f09c1Sdp 			    scf_property_get_value(prop, val) != 0)
3357c478bd9Sstevel@tonic-gate 				continue;
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 			bytes = scf_value_get_astring(val, fmri, length);
3387c478bd9Sstevel@tonic-gate 			if (bytes <= 0 || bytes >= length)
3397c478bd9Sstevel@tonic-gate 				continue;
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 			if (strlcat(fmri, FMRI_CONTRACT_PROP, length) >=
3427c478bd9Sstevel@tonic-gate 			    length)
3437c478bd9Sstevel@tonic-gate 				continue;
3447c478bd9Sstevel@tonic-gate 
3453f2f09c1Sdp 			if (scf_handle_decode_fmri(h, fmri, NULL, NULL,
3463f2f09c1Sdp 			    NULL, NULL, prop, SCF_DECODE_FMRI_EXACT) != 0)
3477c478bd9Sstevel@tonic-gate 				continue;
3487c478bd9Sstevel@tonic-gate 
3493f2f09c1Sdp 			if (scf_property_is_type(prop, SCF_TYPE_COUNT) != 0 ||
3503f2f09c1Sdp 			    scf_property_get_value(prop, val) != 0 ||
3513f2f09c1Sdp 			    scf_value_get_count(val, &uint64) != 0)
3527c478bd9Sstevel@tonic-gate 				continue;
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 			ctid = (ctid_t)uint64;
3557c478bd9Sstevel@tonic-gate 			if (save_ctid(ctid) == 0) {
3567c478bd9Sstevel@tonic-gate 				(void) sigsend(P_CTID, ctid, SIGSTOP);
3577c478bd9Sstevel@tonic-gate 			}
3587c478bd9Sstevel@tonic-gate 		}
3597c478bd9Sstevel@tonic-gate 	}
3607c478bd9Sstevel@tonic-gate out:
3617c478bd9Sstevel@tonic-gate 	scf_scope_destroy(sc);
3627c478bd9Sstevel@tonic-gate 	scf_service_destroy(svc);
3637c478bd9Sstevel@tonic-gate 	scf_instance_destroy(inst);
3647c478bd9Sstevel@tonic-gate 	scf_snapshot_destroy(snap);
3657c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg);
3667c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
3677c478bd9Sstevel@tonic-gate 	scf_value_destroy(val);
3687c478bd9Sstevel@tonic-gate 	scf_iter_destroy(siter);
3697c478bd9Sstevel@tonic-gate 	scf_iter_destroy(iiter);
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	(void) scf_handle_unbind(h);
3727c478bd9Sstevel@tonic-gate 	scf_handle_destroy(h);
3737c478bd9Sstevel@tonic-gate }
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate static void
3767c478bd9Sstevel@tonic-gate continue_delegates()
3777c478bd9Sstevel@tonic-gate {
3787c478bd9Sstevel@tonic-gate 	ctidlist_t *next;
3797c478bd9Sstevel@tonic-gate 	for (next = ctidlist; next != NULL; next = next->next)
3807c478bd9Sstevel@tonic-gate 		(void) sigsend(P_CTID, next->ctid, SIGCONT);
3817c478bd9Sstevel@tonic-gate }
3827c478bd9Sstevel@tonic-gate 
3839db7147eSSherry Moore #define	FMRI_GDM "svc:/application/graphical-login/gdm:default"
3843b860eeeSSherry Moore #define	GDM_STOP_TIMEOUT	10	/* Give gdm 10 seconds to shut down */
3859db7147eSSherry Moore 
3869db7147eSSherry Moore /*
3879db7147eSSherry Moore  * If gdm is running, try to stop gdm.
3889db7147eSSherry Moore  * Returns  0 on success, -1 on failure.
3899db7147eSSherry Moore  */
3909db7147eSSherry Moore static int
3919db7147eSSherry Moore stop_gdm()
3929db7147eSSherry Moore {
3939db7147eSSherry Moore 	char *gdm_state = NULL;
3949db7147eSSherry Moore 	int retry = 0;
3959db7147eSSherry Moore 
3969db7147eSSherry Moore 	/*
3979db7147eSSherry Moore 	 * If gdm is running, try to stop gdm.
3989db7147eSSherry Moore 	 */
3999db7147eSSherry Moore 	while ((gdm_state = smf_get_state(FMRI_GDM)) != NULL &&
4003b860eeeSSherry Moore 	    strcmp(gdm_state, SCF_STATE_STRING_ONLINE) == 0 &&
4013b860eeeSSherry Moore 	    retry++ < GDM_STOP_TIMEOUT) {
4023b860eeeSSherry Moore 
4033b860eeeSSherry Moore 		free(gdm_state);
4043b860eeeSSherry Moore 
4053b860eeeSSherry Moore 		/*
4063b860eeeSSherry Moore 		 * Only need to disable once.
4073b860eeeSSherry Moore 		 */
4083b860eeeSSherry Moore 		if (retry == 1 &&
4093b860eeeSSherry Moore 		    smf_disable_instance(FMRI_GDM, SMF_TEMPORARY) != 0) {
4109db7147eSSherry Moore 			(void) fprintf(stderr,
4119db7147eSSherry Moore 			    gettext("%s: Failed to stop %s: %s.\n"),
4129db7147eSSherry Moore 			    cmdname, FMRI_GDM, scf_strerror(scf_error()));
4139db7147eSSherry Moore 			return (-1);
4149db7147eSSherry Moore 		}
4159db7147eSSherry Moore 		(void) sleep(1);
4169db7147eSSherry Moore 	}
4179db7147eSSherry Moore 
4183b860eeeSSherry Moore 	if (retry >= GDM_STOP_TIMEOUT) {
4199db7147eSSherry Moore 		(void) fprintf(stderr, gettext("%s: Failed to stop %s.\n"),
4209db7147eSSherry Moore 		    cmdname, FMRI_GDM);
4219db7147eSSherry Moore 		return (-1);
4229db7147eSSherry Moore 	}
4239db7147eSSherry Moore 
4249db7147eSSherry Moore 	return (0);
4259db7147eSSherry Moore }
4269db7147eSSherry Moore 
4279db7147eSSherry Moore 
4287c478bd9Sstevel@tonic-gate static void
4297c478bd9Sstevel@tonic-gate stop_restarters()
4307c478bd9Sstevel@tonic-gate {
4317c478bd9Sstevel@tonic-gate 	stop_startd();
4327c478bd9Sstevel@tonic-gate 	stop_delegates();
4337c478bd9Sstevel@tonic-gate }
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate static void
4367c478bd9Sstevel@tonic-gate continue_restarters()
4377c478bd9Sstevel@tonic-gate {
4387c478bd9Sstevel@tonic-gate 	continue_startd();
4397c478bd9Sstevel@tonic-gate 	continue_delegates();
4407c478bd9Sstevel@tonic-gate }
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate /*
4437c478bd9Sstevel@tonic-gate  * Copy an array of strings into buf, separated by spaces.  Returns 0 on
4447c478bd9Sstevel@tonic-gate  * success.
4457c478bd9Sstevel@tonic-gate  */
4467c478bd9Sstevel@tonic-gate static int
4477c478bd9Sstevel@tonic-gate gather_args(char **args, char *buf, size_t buf_sz)
4487c478bd9Sstevel@tonic-gate {
4497c478bd9Sstevel@tonic-gate 	if (strlcpy(buf, *args, buf_sz) >= buf_sz)
4507c478bd9Sstevel@tonic-gate 		return (-1);
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 	for (++args; *args != NULL; ++args) {
4537c478bd9Sstevel@tonic-gate 		if (strlcat(buf, " ", buf_sz) >= buf_sz)
4547c478bd9Sstevel@tonic-gate 			return (-1);
4557c478bd9Sstevel@tonic-gate 		if (strlcat(buf, *args, buf_sz) >= buf_sz)
4567c478bd9Sstevel@tonic-gate 			return (-1);
4577c478bd9Sstevel@tonic-gate 	}
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	return (0);
4607c478bd9Sstevel@tonic-gate }
4617c478bd9Sstevel@tonic-gate 
46226f665e8Sdstaff /*
46326f665e8Sdstaff  * Halt every zone on the system.  We are committed to doing a shutdown
46426f665e8Sdstaff  * even if something goes wrong here. If something goes wrong, we just
46526f665e8Sdstaff  * continue with the shutdown.  Return non-zero if we need to wait for zones to
46626f665e8Sdstaff  * halt later on.
46726f665e8Sdstaff  */
46826f665e8Sdstaff static int
4693f2f09c1Sdp halt_zones()
47026f665e8Sdstaff {
47126f665e8Sdstaff 	pid_t pid;
47226f665e8Sdstaff 	zoneid_t *zones;
4733f2f09c1Sdp 	size_t nz = 0, old_nz;
47426f665e8Sdstaff 	int i;
47526f665e8Sdstaff 	char zname[ZONENAME_MAX];
47626f665e8Sdstaff 
47726f665e8Sdstaff 	/*
47826f665e8Sdstaff 	 * Get a list of zones. If the number of zones changes in between the
47926f665e8Sdstaff 	 * two zone_list calls, try again.
48026f665e8Sdstaff 	 */
48126f665e8Sdstaff 
48226f665e8Sdstaff 	for (;;) {
48326f665e8Sdstaff 		(void) zone_list(NULL, &nz);
48426f665e8Sdstaff 		if (nz == 1)
48526f665e8Sdstaff 			return (0);
48626f665e8Sdstaff 		old_nz = nz;
48726f665e8Sdstaff 		zones = calloc(sizeof (zoneid_t), nz);
48826f665e8Sdstaff 		if (zones == NULL) {
48926f665e8Sdstaff 			(void) fprintf(stderr,
49026f665e8Sdstaff 			    gettext("%s: Could not halt zones"
4913f2f09c1Sdp 			    " (out of memory).\n"), cmdname);
49226f665e8Sdstaff 			return (0);
49326f665e8Sdstaff 		}
49426f665e8Sdstaff 
49526f665e8Sdstaff 		(void) zone_list(zones, &nz);
49626f665e8Sdstaff 		if (old_nz == nz)
49726f665e8Sdstaff 			break;
49826f665e8Sdstaff 		free(zones);
49926f665e8Sdstaff 	}
50026f665e8Sdstaff 
50126f665e8Sdstaff 	if (nz == 2) {
5023f2f09c1Sdp 		(void) fprintf(stderr, gettext("%s: Halting 1 zone.\n"),
5033f2f09c1Sdp 		    cmdname);
50426f665e8Sdstaff 	} else {
5053f2f09c1Sdp 		(void) fprintf(stderr, gettext("%s: Halting %i zones.\n"),
5063f2f09c1Sdp 		    cmdname, nz - 1);
50726f665e8Sdstaff 	}
50826f665e8Sdstaff 
50926f665e8Sdstaff 	for (i = 0; i < nz; i++) {
51026f665e8Sdstaff 		if (zones[i] == GLOBAL_ZONEID)
51126f665e8Sdstaff 			continue;
51226f665e8Sdstaff 		if (getzonenamebyid(zones[i], zname, sizeof (zname)) < 0) {
51326f665e8Sdstaff 			/*
51426f665e8Sdstaff 			 * getzonenamebyid should only fail if we raced with
51526f665e8Sdstaff 			 * another process trying to shut down the zone.
51626f665e8Sdstaff 			 * We assume this happened and ignore the error.
51726f665e8Sdstaff 			 */
51826f665e8Sdstaff 			if (errno != EINVAL) {
51926f665e8Sdstaff 				(void) fprintf(stderr,
52026f665e8Sdstaff 				    gettext("%s: Unexpected error while "
52126f665e8Sdstaff 				    "looking up zone %ul: %s.\n"),
5223f2f09c1Sdp 				    cmdname, zones[i], strerror(errno));
52326f665e8Sdstaff 			}
52426f665e8Sdstaff 
52526f665e8Sdstaff 			continue;
52626f665e8Sdstaff 		}
52726f665e8Sdstaff 		pid = fork();
52826f665e8Sdstaff 		if (pid < 0) {
52926f665e8Sdstaff 			(void) fprintf(stderr,
53026f665e8Sdstaff 			    gettext("%s: Zone \"%s\" could not be"
53126f665e8Sdstaff 			    " halted (could not fork(): %s).\n"),
5323f2f09c1Sdp 			    cmdname, zname, strerror(errno));
53326f665e8Sdstaff 			continue;
53426f665e8Sdstaff 		}
53526f665e8Sdstaff 		if (pid == 0) {
53626f665e8Sdstaff 			(void) execl(ZONEADM_PROG, ZONEADM_PROG,
53726f665e8Sdstaff 			    "-z", zname, "halt", NULL);
53826f665e8Sdstaff 			(void) fprintf(stderr,
53926f665e8Sdstaff 			    gettext("%s: Zone \"%s\" could not be halted"
54026f665e8Sdstaff 			    " (cannot exec(" ZONEADM_PROG "): %s).\n"),
5413f2f09c1Sdp 			    cmdname, zname, strerror(errno));
54226f665e8Sdstaff 			exit(0);
54326f665e8Sdstaff 		}
54426f665e8Sdstaff 	}
54526f665e8Sdstaff 
54626f665e8Sdstaff 	return (1);
54726f665e8Sdstaff }
54826f665e8Sdstaff 
54926f665e8Sdstaff /*
55026f665e8Sdstaff  * This function tries to wait for all non-global zones to go away.
55126f665e8Sdstaff  * It will timeout if no progress is made for 5 seconds, or a total of
55226f665e8Sdstaff  * 30 seconds elapses.
55326f665e8Sdstaff  */
55426f665e8Sdstaff 
55526f665e8Sdstaff static void
5563f2f09c1Sdp check_zones_haltedness()
55726f665e8Sdstaff {
55826f665e8Sdstaff 	int t = 0, t_prog = 0;
55926f665e8Sdstaff 	size_t nz = 0, last_nz;
56026f665e8Sdstaff 
56126f665e8Sdstaff 	do {
56226f665e8Sdstaff 		last_nz = nz;
56326f665e8Sdstaff 		(void) zone_list(NULL, &nz);
56426f665e8Sdstaff 		if (nz == 1)
56526f665e8Sdstaff 			return;
56626f665e8Sdstaff 
56726f665e8Sdstaff 		(void) sleep(1);
56826f665e8Sdstaff 
56926f665e8Sdstaff 		if (last_nz > nz)
57026f665e8Sdstaff 			t_prog = 0;
57126f665e8Sdstaff 
57226f665e8Sdstaff 		t++;
57326f665e8Sdstaff 		t_prog++;
57426f665e8Sdstaff 
57526f665e8Sdstaff 		if (t == 10) {
57626f665e8Sdstaff 			if (nz == 2) {
57726f665e8Sdstaff 				(void) fprintf(stderr,
57826f665e8Sdstaff 				    gettext("%s: Still waiting for 1 zone to "
57926f665e8Sdstaff 				    "halt. Will wait up to 20 seconds.\n"),
5803f2f09c1Sdp 				    cmdname);
58126f665e8Sdstaff 			} else {
58226f665e8Sdstaff 				(void) fprintf(stderr,
58326f665e8Sdstaff 				    gettext("%s: Still waiting for %i zones "
58426f665e8Sdstaff 				    "to halt. Will wait up to 20 seconds.\n"),
5853f2f09c1Sdp 				    cmdname, nz - 1);
58626f665e8Sdstaff 			}
58726f665e8Sdstaff 		}
58826f665e8Sdstaff 
58926f665e8Sdstaff 	} while ((t < 30) && (t_prog < 5));
59026f665e8Sdstaff }
59126f665e8Sdstaff 
59219397407SSherry Moore 
59319397407SSherry Moore /*
59419397407SSherry Moore  * Validate that this is a root disk or dataset
59519397407SSherry Moore  * Returns 0 if it is a root disk or dataset;
59619397407SSherry Moore  * returns 1 if it is a disk argument or dataset, but not valid or not root;
59719397407SSherry Moore  * returns -1 if it is not a valid argument or a disk argument.
59819397407SSherry Moore  */
59919397407SSherry Moore static int
60019397407SSherry Moore validate_disk(char *arg, char *mountpoint)
60119397407SSherry Moore {
60219397407SSherry Moore 	static char root_dev_path[] = "/dev/dsk";
60319397407SSherry Moore 	char kernpath[MAXPATHLEN];
60419397407SSherry Moore 	struct stat64 statbuf;
60519397407SSherry Moore 	int rc = 0;
60619397407SSherry Moore 
60719397407SSherry Moore 	if (strlen(arg) > MAXPATHLEN) {
60819397407SSherry Moore 		(void) fprintf(stderr,
609753a6d45SSherry Moore 		    gettext("%s: Argument is too long\n"), cmdname);
61019397407SSherry Moore 		return (-1);
61119397407SSherry Moore 	}
61219397407SSherry Moore 
61319397407SSherry Moore 	bcopy(FASTBOOT_MOUNTPOINT, mountpoint, sizeof (FASTBOOT_MOUNTPOINT));
61419397407SSherry Moore 
615753a6d45SSherry Moore 	if (strstr(arg, mountpoint) == NULL) {
61619397407SSherry Moore 		/*
61719397407SSherry Moore 		 * Do a force umount just in case some other filesystem has
61819397407SSherry Moore 		 * been mounted there.
61919397407SSherry Moore 		 */
62019397407SSherry Moore 		(void) umount2(mountpoint, MS_FORCE);
621753a6d45SSherry Moore 	}
62219397407SSherry Moore 
62319397407SSherry Moore 	/* Create the directory if it doesn't already exist */
624753a6d45SSherry Moore 	if (lstat64(mountpoint, &statbuf) != 0) {
62519397407SSherry Moore 		if (mkdirp(mountpoint, 0755) != 0) {
62619397407SSherry Moore 			(void) fprintf(stderr,
627753a6d45SSherry Moore 			    gettext("Failed to create mountpoint %s\n"),
62819397407SSherry Moore 			    mountpoint);
62919397407SSherry Moore 			return (-1);
63019397407SSherry Moore 		}
63119397407SSherry Moore 	}
63219397407SSherry Moore 
63319397407SSherry Moore 	if (strncmp(arg, root_dev_path, strlen(root_dev_path)) == 0) {
63419397407SSherry Moore 		/* ufs root disk argument */
63519397407SSherry Moore 		rc = validate_ufs_disk(arg, mountpoint);
63619397407SSherry Moore 	} else {
63719397407SSherry Moore 		/* zfs root pool argument */
63819397407SSherry Moore 		rc = validate_zfs_pool(arg, mountpoint);
63919397407SSherry Moore 	}
64019397407SSherry Moore 
64119397407SSherry Moore 	if (rc != 0)
64219397407SSherry Moore 		return (rc);
64319397407SSherry Moore 
64419397407SSherry Moore 	(void) snprintf(kernpath, MAXPATHLEN, "%s/platform/i86pc/kernel/unix",
64519397407SSherry Moore 	    mountpoint);
64619397407SSherry Moore 
64719397407SSherry Moore 	if (stat64(kernpath, &statbuf) != 0) {
64819397407SSherry Moore 		(void) fprintf(stderr,
64919397407SSherry Moore 		    gettext("%s: %s is not a root disk or dataset\n"),
65019397407SSherry Moore 		    cmdname, arg);
65119397407SSherry Moore 		return (1);
65219397407SSherry Moore 	}
65319397407SSherry Moore 
65419397407SSherry Moore 	return (0);
65519397407SSherry Moore }
65619397407SSherry Moore 
65719397407SSherry Moore 
65819397407SSherry Moore static int
65919397407SSherry Moore validate_ufs_disk(char *arg, char *mountpoint)
66019397407SSherry Moore {
6618e07a41fSKonstantin Ananyev 	struct ufs_args	ufs_args = { 0 };
6628e07a41fSKonstantin Ananyev 	char mntopts[MNT_LINE_MAX] = MNTOPT_LARGEFILES;
66319397407SSherry Moore 
66419397407SSherry Moore 	/* perform the mount */
6658e07a41fSKonstantin Ananyev 	ufs_args.flags = UFSMNT_LARGEFILES;
66619397407SSherry Moore 	if (mount(arg, mountpoint, MS_DATA|MS_OPTIONSTR,
6678e07a41fSKonstantin Ananyev 	    MNTTYPE_UFS, &ufs_args, sizeof (ufs_args),
6688e07a41fSKonstantin Ananyev 	    mntopts, sizeof (mntopts)) != 0) {
66919397407SSherry Moore 		perror(cmdname);
67019397407SSherry Moore 		(void) fprintf(stderr,
671753a6d45SSherry Moore 		    gettext("%s: Failed to mount %s\n"), cmdname, arg);
67219397407SSherry Moore 		return (-1);
67319397407SSherry Moore 	}
67419397407SSherry Moore 
67519397407SSherry Moore 	return (0);
67619397407SSherry Moore }
67719397407SSherry Moore 
67819397407SSherry Moore static int
67919397407SSherry Moore validate_zfs_pool(char *arg, char *mountpoint)
68019397407SSherry Moore {
68119397407SSherry Moore 	zfs_handle_t *zhp = NULL;
68219397407SSherry Moore 	char mntopts[MNT_LINE_MAX] = { '\0' };
68319397407SSherry Moore 	int rc = 0;
68419397407SSherry Moore 
68519397407SSherry Moore 	if ((g_zfs = libzfs_init()) == NULL) {
686753a6d45SSherry Moore 		(void) fprintf(stderr, gettext("Internal error: failed to "
68719397407SSherry Moore 		    "initialize ZFS library\n"));
68819397407SSherry Moore 		return (-1);
68919397407SSherry Moore 	}
69019397407SSherry Moore 
69119397407SSherry Moore 	/* Try to open the dataset */
69219397407SSherry Moore 	if ((zhp = zfs_open(g_zfs, arg,
69319397407SSherry Moore 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_DATASET)) == NULL)
69419397407SSherry Moore 		return (-1);
69519397407SSherry Moore 
69619397407SSherry Moore 	/* perform the mount */
697753a6d45SSherry Moore 	if (mount(zfs_get_name(zhp), mountpoint, MS_DATA|MS_OPTIONSTR|MS_RDONLY,
69819397407SSherry Moore 	    MNTTYPE_ZFS, NULL, 0, mntopts, sizeof (mntopts)) != 0) {
69919397407SSherry Moore 		perror(cmdname);
70019397407SSherry Moore 		(void) fprintf(stderr,
701753a6d45SSherry Moore 		    gettext("%s: Failed to mount %s\n"), cmdname, arg);
70219397407SSherry Moore 		rc = -1;
70319397407SSherry Moore 	}
70419397407SSherry Moore 
70519397407SSherry Moore validate_zfs_err_out:
70619397407SSherry Moore 	if (zhp != NULL)
70719397407SSherry Moore 		zfs_close(zhp);
70819397407SSherry Moore 
70919397407SSherry Moore 	libzfs_fini(g_zfs);
71019397407SSherry Moore 	return (rc);
71119397407SSherry Moore }
71219397407SSherry Moore 
71319397407SSherry Moore /*
71419397407SSherry Moore  * Return 0 if not zfs, or is zfs and have successfully constructed the
71519397407SSherry Moore  * boot argument; returns non-zero otherwise.
71619397407SSherry Moore  * At successful completion fpth contains pointer where mount point ends.
71719397407SSherry Moore  * NOTE: arg is supposed to be the resolved path
71819397407SSherry Moore  */
71919397407SSherry Moore static int
72019397407SSherry Moore get_zfs_bootfs_arg(const char *arg, const char ** fpth, int *is_zfs,
72119397407SSherry Moore 		char *bootfs_arg)
72219397407SSherry Moore {
72319397407SSherry Moore 	zfs_handle_t *zhp = NULL;
72419397407SSherry Moore 	zpool_handle_t *zpoolp = NULL;
72519397407SSherry Moore 	FILE *mtabp = NULL;
72619397407SSherry Moore 	struct mnttab mnt;
72719397407SSherry Moore 	char *poolname = NULL;
728753a6d45SSherry Moore 	char physpath[MAXPATHLEN];
72919397407SSherry Moore 	char mntsp[ZPOOL_MAXNAMELEN];
73019397407SSherry Moore 	char bootfs[ZPOOL_MAXNAMELEN];
73119397407SSherry Moore 	int rc = 0;
73219397407SSherry Moore 	size_t mntlen = 0;
73319397407SSherry Moore 	size_t msz;
734753a6d45SSherry Moore 	static char fmt[] = "-B zfs-bootfs=%s,bootpath=\"%s\"";
73519397407SSherry Moore 
73619397407SSherry Moore 	*fpth = arg;
73719397407SSherry Moore 	*is_zfs = 0;
73819397407SSherry Moore 
73919397407SSherry Moore 	bzero(physpath, sizeof (physpath));
74019397407SSherry Moore 	bzero(bootfs, sizeof (bootfs));
74119397407SSherry Moore 
74219397407SSherry Moore 	if ((mtabp = fopen(MNTTAB, "r")) == NULL) {
74319397407SSherry Moore 		return (-1);
74419397407SSherry Moore 	}
74519397407SSherry Moore 
74619397407SSherry Moore 	while (getmntent(mtabp, &mnt) == 0) {
74719397407SSherry Moore 		if (strstr(arg, mnt.mnt_mountp) == arg &&
74819397407SSherry Moore 		    (msz = strlen(mnt.mnt_mountp)) > mntlen) {
74919397407SSherry Moore 			mntlen = msz;
75019397407SSherry Moore 			*is_zfs = strcmp(MNTTYPE_ZFS, mnt.mnt_fstype) == 0;
75119397407SSherry Moore 			(void) strlcpy(mntsp, mnt.mnt_special, sizeof (mntsp));
75219397407SSherry Moore 		}
75319397407SSherry Moore 	}
75419397407SSherry Moore 
75519397407SSherry Moore 	(void) fclose(mtabp);
75619397407SSherry Moore 
75719397407SSherry Moore 	if (mntlen > 1)
75819397407SSherry Moore 		*fpth += mntlen;
75919397407SSherry Moore 
76019397407SSherry Moore 	if (!*is_zfs)
76119397407SSherry Moore 		return (0);
76219397407SSherry Moore 
76319397407SSherry Moore 	if ((g_zfs = libzfs_init()) == NULL)
76419397407SSherry Moore 		return (-1);
76519397407SSherry Moore 
76619397407SSherry Moore 	/* Try to open the dataset */
76719397407SSherry Moore 	if ((zhp = zfs_open(g_zfs, mntsp,
76819397407SSherry Moore 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_DATASET)) == NULL) {
769753a6d45SSherry Moore 		(void) fprintf(stderr, gettext("Cannot open %s\n"), mntsp);
77019397407SSherry Moore 		rc = -1;
77119397407SSherry Moore 		goto validate_zfs_err_out;
77219397407SSherry Moore 	}
77319397407SSherry Moore 
77419397407SSherry Moore 	(void) strlcpy(bootfs, mntsp, sizeof (bootfs));
77519397407SSherry Moore 
77619397407SSherry Moore 	if ((poolname = strtok(mntsp, "/")) == NULL) {
77719397407SSherry Moore 		rc = -1;
77819397407SSherry Moore 		goto validate_zfs_err_out;
77919397407SSherry Moore 	}
78019397407SSherry Moore 
78119397407SSherry Moore 	if ((zpoolp = zpool_open(g_zfs, poolname)) == NULL) {
782753a6d45SSherry Moore 		(void) fprintf(stderr, gettext("Cannot open %s\n"), poolname);
78319397407SSherry Moore 		rc = -1;
78419397407SSherry Moore 		goto validate_zfs_err_out;
78519397407SSherry Moore 	}
78619397407SSherry Moore 
787753a6d45SSherry Moore 	if (zpool_get_physpath(zpoolp, physpath, sizeof (physpath)) != 0) {
788753a6d45SSherry Moore 		(void) fprintf(stderr, gettext("Cannot find phys_path\n"));
78919397407SSherry Moore 		rc = -1;
79019397407SSherry Moore 		goto validate_zfs_err_out;
79119397407SSherry Moore 	}
79219397407SSherry Moore 
793753a6d45SSherry Moore 	/*
794753a6d45SSherry Moore 	 * For the mirror physpath would contain the list of all
795753a6d45SSherry Moore 	 * bootable devices, pick up the first one.
796753a6d45SSherry Moore 	 */
797753a6d45SSherry Moore 	(void) strtok(physpath, " ");
798753a6d45SSherry Moore 	if (snprintf(bootfs_arg, BOOTARGS_MAX, fmt, bootfs, physpath) >=
799753a6d45SSherry Moore 	    BOOTARGS_MAX) {
800753a6d45SSherry Moore 		rc = E2BIG;
801753a6d45SSherry Moore 		(void) fprintf(stderr,
802753a6d45SSherry Moore 		    gettext("Boot arguments are too long\n"));
80319397407SSherry Moore 	}
80419397407SSherry Moore 
80519397407SSherry Moore validate_zfs_err_out:
80619397407SSherry Moore 	if (zhp != NULL)
80719397407SSherry Moore 		zfs_close(zhp);
80819397407SSherry Moore 
80919397407SSherry Moore 	if (zpoolp != NULL)
81019397407SSherry Moore 		zpool_close(zpoolp);
81119397407SSherry Moore 
81219397407SSherry Moore 	libzfs_fini(g_zfs);
81319397407SSherry Moore 	return (rc);
81419397407SSherry Moore }
81519397407SSherry Moore 
81619397407SSherry Moore /*
81719397407SSherry Moore  * Validate that the file exists, and is an ELF file.
81819397407SSherry Moore  * Returns 0 on success, -1 on failure.
81919397407SSherry Moore  */
82019397407SSherry Moore static int
8215b89e3f7SSherry Moore validate_unix(char *arg, int *mplen, int *is_zfs, char *bootfs_arg)
82219397407SSherry Moore {
82319397407SSherry Moore 	const char *location;
82419397407SSherry Moore 	int class, format;
82519397407SSherry Moore 	unsigned char ident[EI_NIDENT];
82619397407SSherry Moore 	char physpath[MAXPATHLEN];
82719397407SSherry Moore 	int elffd = -1;
82819397407SSherry Moore 	size_t	sz;
82919397407SSherry Moore 
83019397407SSherry Moore 	if ((sz = resolvepath(arg, physpath, sizeof (physpath) - 1)) ==
83119397407SSherry Moore 	    (size_t)-1) {
83219397407SSherry Moore 		(void) fprintf(stderr,
833753a6d45SSherry Moore 		    gettext("Cannot resolve path for %s: %s\n"),
83419397407SSherry Moore 		    arg, strerror(errno));
83519397407SSherry Moore 		return (-1);
83619397407SSherry Moore 	}
83719397407SSherry Moore 	(void) strlcpy(arg, physpath, sz + 1);
83819397407SSherry Moore 
83919397407SSherry Moore 	if (strlen(arg) > MAXPATHLEN) {
84019397407SSherry Moore 		(void) fprintf(stderr,
841753a6d45SSherry Moore 		    gettext("%s: New kernel name is too long\n"), cmdname);
84219397407SSherry Moore 		return (-1);
84319397407SSherry Moore 	}
84419397407SSherry Moore 
84519397407SSherry Moore 	if (strncmp(basename(arg), "unix", 4) != 0) {
84619397407SSherry Moore 		(void) fprintf(stderr,
847753a6d45SSherry Moore 		    gettext("%s: %s: Kernel name must be unix\n"),
84819397407SSherry Moore 		    cmdname, arg);
84919397407SSherry Moore 		return (-1);
85019397407SSherry Moore 	}
85119397407SSherry Moore 
85219397407SSherry Moore 	if (get_zfs_bootfs_arg(arg, &location, is_zfs, bootfs_arg) != 0)
85319397407SSherry Moore 		goto err_out;
85419397407SSherry Moore 
85519397407SSherry Moore 	*mplen = location - arg;
85619397407SSherry Moore 
8575b89e3f7SSherry Moore 	if (strstr(location, "/boot/platform") == location) {
8585b89e3f7SSherry Moore 		/*
8595b89e3f7SSherry Moore 		 * Rebooting to failsafe.
8605b89e3f7SSherry Moore 		 * Clear bootfs_arg and is_zfs flag.
8615b89e3f7SSherry Moore 		 */
8625b89e3f7SSherry Moore 		bootfs_arg[0] = 0;
8635b89e3f7SSherry Moore 		*is_zfs = 0;
8645b89e3f7SSherry Moore 	} else if (strstr(location, "/platform") != location) {
86519397407SSherry Moore 		(void) fprintf(stderr,
8665b89e3f7SSherry Moore 		    gettext("%s: %s: No /platform in file name\n"),
8675b89e3f7SSherry Moore 		    cmdname, arg);
86819397407SSherry Moore 		goto err_out;
86919397407SSherry Moore 	}
87019397407SSherry Moore 
87119397407SSherry Moore 	if ((elffd = open64(arg, O_RDONLY)) < 0 ||
87219397407SSherry Moore 	    (pread64(elffd, ident, EI_NIDENT, 0) != EI_NIDENT)) {
87319397407SSherry Moore 		(void) fprintf(stderr, "%s: %s: %s\n",
87419397407SSherry Moore 		    cmdname, arg, strerror(errno));
87519397407SSherry Moore 		goto err_out;
87619397407SSherry Moore 	}
87719397407SSherry Moore 
87819397407SSherry Moore 	class = ident[EI_CLASS];
87919397407SSherry Moore 
88019397407SSherry Moore 	if ((class != ELFCLASS32 && class != ELFCLASS64) ||
881753a6d45SSherry Moore 	    memcmp(&ident[EI_MAG0], ELFMAG, 4) != 0) {
88219397407SSherry Moore 		(void) fprintf(stderr,
883753a6d45SSherry Moore 		    gettext("%s: %s: Not a valid ELF file\n"), cmdname, arg);
88419397407SSherry Moore 		goto err_out;
88519397407SSherry Moore 	}
88619397407SSherry Moore 
88719397407SSherry Moore 	format = ident[EI_DATA];
88819397407SSherry Moore 
88919397407SSherry Moore 	if (format != CUR_ELFDATA) {
890753a6d45SSherry Moore 		(void) fprintf(stderr, gettext("%s: %s: Invalid data format\n"),
89119397407SSherry Moore 		    cmdname, arg);
89219397407SSherry Moore 		goto err_out;
89319397407SSherry Moore 	}
89419397407SSherry Moore 
89519397407SSherry Moore 	return (0);
89619397407SSherry Moore 
89719397407SSherry Moore err_out:
89819397407SSherry Moore 	if (elffd >= 0) {
89919397407SSherry Moore 		(void) close(elffd);
90019397407SSherry Moore 		elffd = -1;
90119397407SSherry Moore 	}
90219397407SSherry Moore 	return (-1);
90319397407SSherry Moore }
90419397407SSherry Moore 
90519397407SSherry Moore static int
9064e1f1c13SKonstantin Ananyev halt_exec(const char *path, ...)
9074e1f1c13SKonstantin Ananyev {
9084e1f1c13SKonstantin Ananyev 	pid_t		pid;
9094e1f1c13SKonstantin Ananyev 	int		i;
9104e1f1c13SKonstantin Ananyev 	int		st;
9114e1f1c13SKonstantin Ananyev 	const char	*arg;
9124e1f1c13SKonstantin Ananyev 	va_list	vp;
9134e1f1c13SKonstantin Ananyev 	const char	*argv[256];
9144e1f1c13SKonstantin Ananyev 
9154e1f1c13SKonstantin Ananyev 	if ((pid = fork()) == -1) {
9164e1f1c13SKonstantin Ananyev 		return (errno);
9174e1f1c13SKonstantin Ananyev 	} else if (pid == 0) {
9184e1f1c13SKonstantin Ananyev 		(void) fclose(stdout);
9194e1f1c13SKonstantin Ananyev 		(void) fclose(stderr);
9204e1f1c13SKonstantin Ananyev 
9214e1f1c13SKonstantin Ananyev 		argv[0] = path;
9224e1f1c13SKonstantin Ananyev 		i = 1;
9234e1f1c13SKonstantin Ananyev 
9244e1f1c13SKonstantin Ananyev 		va_start(vp, path);
9254e1f1c13SKonstantin Ananyev 
9264e1f1c13SKonstantin Ananyev 		do {
9274e1f1c13SKonstantin Ananyev 			arg = va_arg(vp, const char *);
9284e1f1c13SKonstantin Ananyev 			argv[i] = arg;
9294e1f1c13SKonstantin Ananyev 		} while (arg != NULL &&
9304e1f1c13SKonstantin Ananyev 		    ++i != sizeof (argv) / sizeof (argv[0]));
9314e1f1c13SKonstantin Ananyev 
9324e1f1c13SKonstantin Ananyev 		va_end(vp);
9334e1f1c13SKonstantin Ananyev 
9344e1f1c13SKonstantin Ananyev 		(void) execve(path, (char * const *)argv, NULL);
935753a6d45SSherry Moore 		(void) fprintf(stderr, gettext("Cannot execute %s: %s\n"),
9364e1f1c13SKonstantin Ananyev 		    path, strerror(errno));
9374e1f1c13SKonstantin Ananyev 		exit(-1);
9384e1f1c13SKonstantin Ananyev 	} else {
9394e1f1c13SKonstantin Ananyev 		if (waitpid(pid, &st, 0) == pid &&
9404e1f1c13SKonstantin Ananyev 		    !WIFSIGNALED(st) && WIFEXITED(st))
9414e1f1c13SKonstantin Ananyev 			st = WEXITSTATUS(st);
9424e1f1c13SKonstantin Ananyev 		else
9434e1f1c13SKonstantin Ananyev 			st = -1;
9444e1f1c13SKonstantin Ananyev 	}
9454e1f1c13SKonstantin Ananyev 	return (st);
9464e1f1c13SKonstantin Ananyev }
9474e1f1c13SKonstantin Ananyev 
9484e1f1c13SKonstantin Ananyev /*
9494e1f1c13SKonstantin Ananyev  * Invokes lumount for bename.
9504e1f1c13SKonstantin Ananyev  * At successfull completion returns zero and copies contents of bename
9514e1f1c13SKonstantin Ananyev  * into mountpoint[]
9524e1f1c13SKonstantin Ananyev  */
9534e1f1c13SKonstantin Ananyev static int
95419397407SSherry Moore fastboot_bename(const char *bename, char *mountpoint, size_t mpsz)
95519397407SSherry Moore {
95619397407SSherry Moore 	int rc;
95719397407SSherry Moore 
9584e1f1c13SKonstantin Ananyev 	(void) halt_exec(LUUMOUNT_PROG, "-n", bename, NULL);
95919397407SSherry Moore 
9604e1f1c13SKonstantin Ananyev 	if ((rc = halt_exec(LUMOUNT_PROG, "-n", bename, FASTBOOT_MOUNTPOINT,
9614e1f1c13SKonstantin Ananyev 	    NULL)) != 0)
962753a6d45SSherry Moore 		(void) fprintf(stderr, gettext("%s: Cannot mount BE %s\n"),
96319397407SSherry Moore 		    cmdname, bename);
96419397407SSherry Moore 	else
96519397407SSherry Moore 		(void) strlcpy(mountpoint, FASTBOOT_MOUNTPOINT, mpsz);
96619397407SSherry Moore 
96719397407SSherry Moore 	return (rc);
96819397407SSherry Moore }
96919397407SSherry Moore 
97019397407SSherry Moore /*
97119397407SSherry Moore  * Returns 0 on successful parsing of the arguments;
972753a6d45SSherry Moore  * returns EINVAL on parsing failures that should abort the reboot attempt;
973753a6d45SSherry Moore  * returns other error code to fall back to regular reboot.
97419397407SSherry Moore  */
97519397407SSherry Moore static int
976753a6d45SSherry Moore parse_fastboot_args(char *bootargs_buf, size_t buf_size,
9775b89e3f7SSherry Moore     int *is_dryrun, const char *bename)
97819397407SSherry Moore {
97919397407SSherry Moore 	char mountpoint[MAXPATHLEN];
98019397407SSherry Moore 	char bootargs_saved[BOOTARGS_MAX];
98119397407SSherry Moore 	char bootargs_scratch[BOOTARGS_MAX];
98219397407SSherry Moore 	char bootfs_arg[BOOTARGS_MAX];
98319397407SSherry Moore 	char unixfile[BOOTARGS_MAX];
98419397407SSherry Moore 	char *head, *newarg;
98519397407SSherry Moore 	int buflen;		/* length of the bootargs_buf */
98619397407SSherry Moore 	int mplen;		/* length of the mount point */
98719397407SSherry Moore 	int rootlen = 0;	/* length of the root argument */
98819397407SSherry Moore 	int unixlen = 0;	/* length of the unix argument */
98919397407SSherry Moore 	int off = 0;		/* offset into the new boot argument */
99019397407SSherry Moore 	int is_zfs = 0;
99119397407SSherry Moore 	int rc = 0;
99219397407SSherry Moore 
99319397407SSherry Moore 	bzero(mountpoint, sizeof (mountpoint));
99419397407SSherry Moore 
99519397407SSherry Moore 	/*
99619397407SSherry Moore 	 * If argc is not 0, buflen is length of the argument being passed in;
99719397407SSherry Moore 	 * else it is 0 as bootargs_buf has been initialized to all 0's.
99819397407SSherry Moore 	 */
99919397407SSherry Moore 	buflen = strlen(bootargs_buf);
100019397407SSherry Moore 
100119397407SSherry Moore 	/* Save a copy of the original argument */
100219397407SSherry Moore 	bcopy(bootargs_buf, bootargs_saved, buflen);
100319397407SSherry Moore 	bzero(&bootargs_saved[buflen], sizeof (bootargs_saved) - buflen);
100419397407SSherry Moore 
100519397407SSherry Moore 	/* Save another copy to be used by strtok */
100619397407SSherry Moore 	bcopy(bootargs_buf, bootargs_scratch, buflen);
100719397407SSherry Moore 	bzero(&bootargs_scratch[buflen], sizeof (bootargs_scratch) - buflen);
100819397407SSherry Moore 	head = &bootargs_scratch[0];
100919397407SSherry Moore 
101019397407SSherry Moore 	/* Get the first argument */
101119397407SSherry Moore 	newarg = strtok(bootargs_scratch, " ");
101219397407SSherry Moore 
101319397407SSherry Moore 	/*
101419397407SSherry Moore 	 * If this is a dry run request, verify that the drivers can handle
101519397407SSherry Moore 	 * fast reboot.
101619397407SSherry Moore 	 */
101719397407SSherry Moore 	if (newarg && strncasecmp(newarg, "dryrun", strlen("dryrun")) == 0) {
101819397407SSherry Moore 		*is_dryrun = 1;
101919397407SSherry Moore 		(void) system("/usr/sbin/devfsadm");
102019397407SSherry Moore 	}
102119397407SSherry Moore 
102219397407SSherry Moore 	/*
102319397407SSherry Moore 	 * Always perform a dry run to identify all the drivers that
102419397407SSherry Moore 	 * need to implement devo_reset().
102519397407SSherry Moore 	 */
102619397407SSherry Moore 	if (uadmin(A_SHUTDOWN, AD_FASTREBOOT_DRYRUN,
102719397407SSherry Moore 	    (uintptr_t)bootargs_saved) != 0) {
1028753a6d45SSherry Moore 		(void) fprintf(stderr, gettext("%s: Not all drivers "
1029753a6d45SSherry Moore 		    "have implemented quiesce(9E)\n"
1030753a6d45SSherry Moore 		    "\tPlease see /var/adm/messages for drivers that haven't\n"
1031753a6d45SSherry Moore 		    "\timplemented quiesce(9E).\n"), cmdname);
103219397407SSherry Moore 	} else if (*is_dryrun) {
1033753a6d45SSherry Moore 		(void) fprintf(stderr, gettext("%s: All drivers have "
103419397407SSherry Moore 		    "implemented quiesce(9E)\n"), cmdname);
103519397407SSherry Moore 	}
103619397407SSherry Moore 
1037753a6d45SSherry Moore 	/* Return if it is a true dry run. */
103819397407SSherry Moore 	if (*is_dryrun)
103919397407SSherry Moore 		return (rc);
104019397407SSherry Moore 
1041753a6d45SSherry Moore #if defined(__i386)
1042753a6d45SSherry Moore 	/* Read boot args from GRUB menu */
1043753a6d45SSherry Moore 	if ((bootargs_buf[0] == 0 || isdigit(bootargs_buf[0])) &&
1044753a6d45SSherry Moore 	    bename == NULL) {
1045753a6d45SSherry Moore 		/*
1046753a6d45SSherry Moore 		 * If no boot arguments are given, or a GRUB menu entry
1047753a6d45SSherry Moore 		 * number is provided, process the GRUB menu.
1048753a6d45SSherry Moore 		 */
1049753a6d45SSherry Moore 		int entnum;
1050753a6d45SSherry Moore 		if (bootargs_buf[0] == 0)
1051753a6d45SSherry Moore 			entnum = GRUB_ENTRY_DEFAULT;
1052753a6d45SSherry Moore 		else {
1053753a6d45SSherry Moore 			errno = 0;
1054753a6d45SSherry Moore 			entnum = strtoul(bootargs_buf, NULL, 10);
1055753a6d45SSherry Moore 			rc = errno;
1056753a6d45SSherry Moore 		}
1057753a6d45SSherry Moore 
1058753a6d45SSherry Moore 		if (rc == 0 && (rc = grub_get_boot_args(&fbarg, NULL,
1059753a6d45SSherry Moore 		    entnum)) == 0) {
1060753a6d45SSherry Moore 			if (strlcpy(bootargs_buf, fbarg.gba_bootargs,
1061753a6d45SSherry Moore 			    buf_size) >= buf_size) {
1062753a6d45SSherry Moore 				grub_cleanup_boot_args(&fbarg);
1063753a6d45SSherry Moore 				bcopy(bootargs_saved, bootargs_buf, buf_size);
1064753a6d45SSherry Moore 				rc = E2BIG;
1065753a6d45SSherry Moore 			}
1066753a6d45SSherry Moore 		}
1067753a6d45SSherry Moore 		/* Failed to read GRUB menu, fall back to normal reboot */
1068753a6d45SSherry Moore 		if (rc != 0) {
1069753a6d45SSherry Moore 			(void) fprintf(stderr,
1070753a6d45SSherry Moore 			    gettext("%s: Failed to process GRUB menu "
1071753a6d45SSherry Moore 			    "entry for fast reboot.\n\t%s\n"),
1072753a6d45SSherry Moore 			    cmdname, grub_strerror(rc));
1073753a6d45SSherry Moore 			(void) fprintf(stderr,
1074753a6d45SSherry Moore 			    gettext("%s: Falling back to regular reboot.\n"),
1075753a6d45SSherry Moore 			    cmdname);
1076753a6d45SSherry Moore 			return (-1);
1077753a6d45SSherry Moore 		}
1078753a6d45SSherry Moore 		/* No need to process further */
1079753a6d45SSherry Moore 		fbarg_used = &fbarg;
1080753a6d45SSherry Moore 		fbarg_entnum = entnum;
1081753a6d45SSherry Moore 		return (0);
1082753a6d45SSherry Moore 	}
1083753a6d45SSherry Moore #endif	/* __i386 */
1084753a6d45SSherry Moore 
1085753a6d45SSherry Moore 	/* Zero out the boot argument buffer as we will reconstruct it */
1086753a6d45SSherry Moore 	bzero(bootargs_buf, buf_size);
1087753a6d45SSherry Moore 	bzero(bootfs_arg, sizeof (bootfs_arg));
1088753a6d45SSherry Moore 	bzero(unixfile, sizeof (unixfile));
1089753a6d45SSherry Moore 
109019397407SSherry Moore 	if (bename && (rc = fastboot_bename(bename, mountpoint,
109119397407SSherry Moore 	    sizeof (mountpoint))) != 0)
1092753a6d45SSherry Moore 		return (EINVAL);
1093753a6d45SSherry Moore 
109419397407SSherry Moore 
109519397407SSherry Moore 	/*
109619397407SSherry Moore 	 * If BE is not specified, look for disk argument to construct
109719397407SSherry Moore 	 * mountpoint; if BE has been specified, mountpoint has already been
109819397407SSherry Moore 	 * constructed.
109919397407SSherry Moore 	 */
110019397407SSherry Moore 	if (newarg && newarg[0] != '-' && !bename) {
110119397407SSherry Moore 		int tmprc;
110219397407SSherry Moore 
110319397407SSherry Moore 		if ((tmprc = validate_disk(newarg, mountpoint)) == 0) {
110419397407SSherry Moore 			/*
110519397407SSherry Moore 			 * The first argument is a valid root argument.
110619397407SSherry Moore 			 * Get the next argument.
110719397407SSherry Moore 			 */
110819397407SSherry Moore 			newarg = strtok(NULL, " ");
110919397407SSherry Moore 			rootlen = (newarg) ? (newarg - head) : buflen;
111019397407SSherry Moore 			(void) strlcpy(fastboot_mounted, mountpoint,
111119397407SSherry Moore 			    sizeof (fastboot_mounted));
111219397407SSherry Moore 
111319397407SSherry Moore 		} else if (tmprc == -1) {
111419397407SSherry Moore 			/*
111519397407SSherry Moore 			 * Not a disk argument.  Use / as default root.
111619397407SSherry Moore 			 */
111719397407SSherry Moore 			bcopy("/", mountpoint, 1);
111819397407SSherry Moore 			bzero(&mountpoint[1], sizeof (mountpoint) - 1);
111919397407SSherry Moore 		} else {
112019397407SSherry Moore 			/*
112119397407SSherry Moore 			 * Disk argument, but not valid or not root.
112219397407SSherry Moore 			 * Return failure.
112319397407SSherry Moore 			 */
112419397407SSherry Moore 			return (EINVAL);
112519397407SSherry Moore 		}
112619397407SSherry Moore 	}
112719397407SSherry Moore 
112819397407SSherry Moore 	/*
112919397407SSherry Moore 	 * Make mountpoint the first part of unixfile.
113019397407SSherry Moore 	 * If there is not disk argument, and BE has not been specified,
113119397407SSherry Moore 	 * mountpoint could be empty.
113219397407SSherry Moore 	 */
113319397407SSherry Moore 	mplen = strlen(mountpoint);
113419397407SSherry Moore 	bcopy(mountpoint, unixfile, mplen);
113519397407SSherry Moore 
113619397407SSherry Moore 	/*
113719397407SSherry Moore 	 * Look for unix argument
113819397407SSherry Moore 	 */
113919397407SSherry Moore 	if (newarg && newarg[0] != '-') {
114019397407SSherry Moore 		bcopy(newarg, &unixfile[mplen], strlen(newarg));
114119397407SSherry Moore 		newarg = strtok(NULL, " ");
114219397407SSherry Moore 		rootlen = (newarg) ? (newarg - head) : buflen;
114319397407SSherry Moore 	} else if (mplen != 0) {
114419397407SSherry Moore 		/*
114519397407SSherry Moore 		 * No unix argument, but mountpoint is not empty, use
114619397407SSherry Moore 		 * /platform/i86pc/$ISADIR/kernel/unix as default.
114719397407SSherry Moore 		 */
114819397407SSherry Moore 		char isa[20];
114919397407SSherry Moore 
115019397407SSherry Moore 		if (sysinfo(SI_ARCHITECTURE_64, isa, sizeof (isa)) != -1)
115119397407SSherry Moore 			(void) snprintf(&unixfile[mplen],
115219397407SSherry Moore 			    sizeof (unixfile) - mplen,
115319397407SSherry Moore 			    "/platform/i86pc/kernel/%s/unix", isa);
115419397407SSherry Moore 		else if (sysinfo(SI_ARCHITECTURE_32, isa, sizeof (isa)) != -1) {
115519397407SSherry Moore 			(void) snprintf(&unixfile[mplen],
115619397407SSherry Moore 			    sizeof (unixfile) - mplen,
115719397407SSherry Moore 			    "/platform/i86pc/kernel/unix");
115819397407SSherry Moore 		} else {
115919397407SSherry Moore 			(void) fprintf(stderr,
1160753a6d45SSherry Moore 			    gettext("%s: Unknown architecture"), cmdname);
116119397407SSherry Moore 			return (EINVAL);
116219397407SSherry Moore 		}
116319397407SSherry Moore 	}
116419397407SSherry Moore 
116519397407SSherry Moore 	/*
116619397407SSherry Moore 	 * We now have the complete unix argument.  Verify that it exists and
116719397407SSherry Moore 	 * is an ELF file.  Split the argument up into mountpoint and unix
116819397407SSherry Moore 	 * portions again.  This is necessary to handle cases where mountpoint
116919397407SSherry Moore 	 * is specified on the command line as part of the unix argument,
117019397407SSherry Moore 	 * such as this:
117119397407SSherry Moore 	 *	# reboot -f /.alt/platform/i86pc/kernel/amd64/unix
117219397407SSherry Moore 	 */
117319397407SSherry Moore 	unixlen = strlen(unixfile);
117419397407SSherry Moore 	if (unixlen > 0) {
117519397407SSherry Moore 		if (validate_unix(unixfile, &mplen, &is_zfs,
11765b89e3f7SSherry Moore 		    bootfs_arg) != 0) {
117719397407SSherry Moore 			/* Not a valid unix file */
117819397407SSherry Moore 			return (EINVAL);
117919397407SSherry Moore 		} else {
11806bc8bc6aSSherry Moore 			int space = 0;
118119397407SSherry Moore 			/*
118219397407SSherry Moore 			 * Construct boot argument.
118319397407SSherry Moore 			 */
118419397407SSherry Moore 			unixlen = strlen(unixfile);
11856bc8bc6aSSherry Moore 
11866bc8bc6aSSherry Moore 			/*
11876bc8bc6aSSherry Moore 			 * mdep cannot start with space because bootadm
11886bc8bc6aSSherry Moore 			 * creates bogus menu entries if it does.
11896bc8bc6aSSherry Moore 			 */
11906bc8bc6aSSherry Moore 			if (mplen > 0) {
119119397407SSherry Moore 				bcopy(unixfile, bootargs_buf, mplen);
119219397407SSherry Moore 				(void) strcat(bootargs_buf, " ");
11936bc8bc6aSSherry Moore 				space = 1;
11946bc8bc6aSSherry Moore 			}
11956bc8bc6aSSherry Moore 			bcopy(&unixfile[mplen], &bootargs_buf[mplen + space],
119619397407SSherry Moore 			    unixlen - mplen);
119719397407SSherry Moore 			(void) strcat(bootargs_buf, " ");
11986bc8bc6aSSherry Moore 			off += unixlen + space + 1;
119919397407SSherry Moore 		}
120019397407SSherry Moore 	} else {
120119397407SSherry Moore 		/* Check to see if root is zfs */
120219397407SSherry Moore 		const char	*dp;
120319397407SSherry Moore 		(void) get_zfs_bootfs_arg("/", &dp, &is_zfs, bootfs_arg);
120419397407SSherry Moore 	}
120519397407SSherry Moore 
120619397407SSherry Moore 	if (is_zfs && (buflen != 0 || bename != NULL))	{
120719397407SSherry Moore 		/* LINTED E_SEC_SPRINTF_UNBOUNDED_COPY */
120819397407SSherry Moore 		off += sprintf(bootargs_buf + off, "%s ", bootfs_arg);
120919397407SSherry Moore 	}
121019397407SSherry Moore 
121119397407SSherry Moore 	/*
121219397407SSherry Moore 	 * Copy the rest of the arguments
121319397407SSherry Moore 	 */
121419397407SSherry Moore 	bcopy(&bootargs_saved[rootlen], &bootargs_buf[off], buflen - rootlen);
121519397407SSherry Moore 
121619397407SSherry Moore 	return (rc);
121719397407SSherry Moore }
121819397407SSherry Moore 
1219de0cf919SEnrico Perla - Sun Microsystems #define	MAXARGS		5
1220de0cf919SEnrico Perla - Sun Microsystems 
1221de0cf919SEnrico Perla - Sun Microsystems static void
1222de0cf919SEnrico Perla - Sun Microsystems do_archives_update(int do_fast_reboot)
1223de0cf919SEnrico Perla - Sun Microsystems {
1224de0cf919SEnrico Perla - Sun Microsystems 	int	r, i = 0;
1225de0cf919SEnrico Perla - Sun Microsystems 	pid_t	pid;
1226de0cf919SEnrico Perla - Sun Microsystems 	char	*cmd_argv[MAXARGS];
1227de0cf919SEnrico Perla - Sun Microsystems 
1228de0cf919SEnrico Perla - Sun Microsystems 
1229de0cf919SEnrico Perla - Sun Microsystems 	cmd_argv[i++] = "/sbin/bootadm";
1230de0cf919SEnrico Perla - Sun Microsystems 	cmd_argv[i++] = "-ea";
1231de0cf919SEnrico Perla - Sun Microsystems 	cmd_argv[i++] = "update_all";
1232de0cf919SEnrico Perla - Sun Microsystems 	if (do_fast_reboot)
1233de0cf919SEnrico Perla - Sun Microsystems 		cmd_argv[i++] = "fastboot";
1234de0cf919SEnrico Perla - Sun Microsystems 	cmd_argv[i] = NULL;
1235de0cf919SEnrico Perla - Sun Microsystems 
1236de0cf919SEnrico Perla - Sun Microsystems 	r = posix_spawn(&pid, cmd_argv[0], NULL, NULL, cmd_argv, NULL);
1237de0cf919SEnrico Perla - Sun Microsystems 
1238de0cf919SEnrico Perla - Sun Microsystems 	/* if posix_spawn fails we emit a warning and continue */
1239de0cf919SEnrico Perla - Sun Microsystems 
1240de0cf919SEnrico Perla - Sun Microsystems 	if (r != 0)
1241de0cf919SEnrico Perla - Sun Microsystems 		(void) fprintf(stderr, gettext("%s: WARNING, unable to start "
1242de0cf919SEnrico Perla - Sun Microsystems 		    "boot archive update\n"), cmdname);
1243de0cf919SEnrico Perla - Sun Microsystems 	else
1244de0cf919SEnrico Perla - Sun Microsystems 		while (waitpid(pid, NULL, 0) == -1 && errno == EINTR)
1245de0cf919SEnrico Perla - Sun Microsystems 			;
1246de0cf919SEnrico Perla - Sun Microsystems }
1247de0cf919SEnrico Perla - Sun Microsystems 
12487c478bd9Sstevel@tonic-gate int
12497c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
12507c478bd9Sstevel@tonic-gate {
12517c478bd9Sstevel@tonic-gate 	char *ttyn = ttyname(STDERR_FILENO);
12527c478bd9Sstevel@tonic-gate 
12537c478bd9Sstevel@tonic-gate 	int qflag = 0, needlog = 1, nosync = 0;
125419397407SSherry Moore 	int fast_reboot = 0;
1255753a6d45SSherry Moore 	int prom_reboot = 0;
12567c478bd9Sstevel@tonic-gate 	uintptr_t mdep = NULL;
12577c478bd9Sstevel@tonic-gate 	int cmd, fcn, c, aval, r;
12587c478bd9Sstevel@tonic-gate 	const char *usage;
12598e07a41fSKonstantin Ananyev 	const char *optstring;
12607c478bd9Sstevel@tonic-gate 	zoneid_t zoneid = getzoneid();
12613f2f09c1Sdp 	int need_check_zones = 0;
12623f2f09c1Sdp 	char bootargs_buf[BOOTARGS_MAX];
12633b860eeeSSherry Moore 	char *bootargs_orig = NULL;
126419397407SSherry Moore 	char *bename = NULL;
12657c478bd9Sstevel@tonic-gate 
12667c478bd9Sstevel@tonic-gate 	const char * const resetting = "/etc/svc/volatile/resetting";
12677c478bd9Sstevel@tonic-gate 
12687c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
12697c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
12707c478bd9Sstevel@tonic-gate 
12713f2f09c1Sdp 	cmdname = basename(argv[0]);
12723f2f09c1Sdp 
12737c478bd9Sstevel@tonic-gate 	if (strcmp(cmdname, "halt") == 0) {
12747c478bd9Sstevel@tonic-gate 		(void) audit_halt_setup(argc, argv);
12758e07a41fSKonstantin Ananyev 		optstring = "dlnqy";
12767c478bd9Sstevel@tonic-gate 		usage = gettext("usage: %s [ -dlnqy ]\n");
12777c478bd9Sstevel@tonic-gate 		cmd = A_SHUTDOWN;
12787c478bd9Sstevel@tonic-gate 		fcn = AD_HALT;
12797c478bd9Sstevel@tonic-gate 	} else if (strcmp(cmdname, "poweroff") == 0) {
12807c478bd9Sstevel@tonic-gate 		(void) audit_halt_setup(argc, argv);
12818e07a41fSKonstantin Ananyev 		optstring = "dlnqy";
12827c478bd9Sstevel@tonic-gate 		usage = gettext("usage: %s [ -dlnqy ]\n");
12837c478bd9Sstevel@tonic-gate 		cmd = A_SHUTDOWN;
12847c478bd9Sstevel@tonic-gate 		fcn = AD_POWEROFF;
12857c478bd9Sstevel@tonic-gate 	} else if (strcmp(cmdname, "reboot") == 0) {
12867c478bd9Sstevel@tonic-gate 		(void) audit_reboot_setup();
128719397407SSherry Moore #if defined(__i386)
1288753a6d45SSherry Moore 		optstring = "dlnqpfe:";
1289753a6d45SSherry Moore 		usage = gettext("usage: %s [ -dlnq(p|fe:) ] [ boot args ]\n");
129019397407SSherry Moore #else
1291*e557d412SChristopher Kiick 		optstring = "dlnqfp";
1292*e557d412SChristopher Kiick 		usage = gettext("usage: %s [ -dlnq(p|f) ] [ boot args ]\n");
129319397407SSherry Moore #endif
12947c478bd9Sstevel@tonic-gate 		cmd = A_SHUTDOWN;
12957c478bd9Sstevel@tonic-gate 		fcn = AD_BOOT;
12967c478bd9Sstevel@tonic-gate 	} else {
12977c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
12987c478bd9Sstevel@tonic-gate 		    gettext("%s: not installed properly\n"), cmdname);
12997c478bd9Sstevel@tonic-gate 		return (1);
13007c478bd9Sstevel@tonic-gate 	}
13017c478bd9Sstevel@tonic-gate 
13028e07a41fSKonstantin Ananyev 	while ((c = getopt(argc, argv, optstring)) != EOF) {
13037c478bd9Sstevel@tonic-gate 		switch (c) {
13047c478bd9Sstevel@tonic-gate 		case 'd':
13057c478bd9Sstevel@tonic-gate 			if (zoneid == GLOBAL_ZONEID)
13067c478bd9Sstevel@tonic-gate 				cmd = A_DUMP;
13077c478bd9Sstevel@tonic-gate 			else {
13087c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
13097c478bd9Sstevel@tonic-gate 				    gettext("%s: -d only valid from global"
13107c478bd9Sstevel@tonic-gate 				    " zone\n"), cmdname);
13117c478bd9Sstevel@tonic-gate 				return (1);
13127c478bd9Sstevel@tonic-gate 			}
13137c478bd9Sstevel@tonic-gate 			break;
13147c478bd9Sstevel@tonic-gate 		case 'l':
13157c478bd9Sstevel@tonic-gate 			needlog = 0;
13167c478bd9Sstevel@tonic-gate 			break;
13177c478bd9Sstevel@tonic-gate 		case 'n':
13187c478bd9Sstevel@tonic-gate 			nosync = 1;
13197c478bd9Sstevel@tonic-gate 			break;
13207c478bd9Sstevel@tonic-gate 		case 'q':
13217c478bd9Sstevel@tonic-gate 			qflag = 1;
13227c478bd9Sstevel@tonic-gate 			break;
13237c478bd9Sstevel@tonic-gate 		case 'y':
13247c478bd9Sstevel@tonic-gate 			ttyn = NULL;
13257c478bd9Sstevel@tonic-gate 			break;
132619397407SSherry Moore 		case 'f':
132719397407SSherry Moore 			fast_reboot = 1;
132819397407SSherry Moore 			break;
1329*e557d412SChristopher Kiick 		case 'p':
1330*e557d412SChristopher Kiick 			prom_reboot = 1;
1331*e557d412SChristopher Kiick 			break;
1332*e557d412SChristopher Kiick #if defined(__i386)
133319397407SSherry Moore 		case 'e':
133419397407SSherry Moore 			bename = optarg;
133519397407SSherry Moore 			break;
133619397407SSherry Moore #endif
13377c478bd9Sstevel@tonic-gate 		default:
13387c478bd9Sstevel@tonic-gate 			/*
13397c478bd9Sstevel@tonic-gate 			 * TRANSLATION_NOTE
13407c478bd9Sstevel@tonic-gate 			 * Don't translate the words "halt" or "reboot"
13417c478bd9Sstevel@tonic-gate 			 */
13427c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, usage, cmdname);
13437c478bd9Sstevel@tonic-gate 			return (1);
13447c478bd9Sstevel@tonic-gate 		}
13457c478bd9Sstevel@tonic-gate 	}
13467c478bd9Sstevel@tonic-gate 
13477c478bd9Sstevel@tonic-gate 	argc -= optind;
13487c478bd9Sstevel@tonic-gate 	argv += optind;
13497c478bd9Sstevel@tonic-gate 
13507c478bd9Sstevel@tonic-gate 	if (argc != 0) {
13517c478bd9Sstevel@tonic-gate 		if (fcn != AD_BOOT) {
13527c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, usage, cmdname);
13537c478bd9Sstevel@tonic-gate 			return (1);
13547c478bd9Sstevel@tonic-gate 		}
13557c478bd9Sstevel@tonic-gate 
13567c478bd9Sstevel@tonic-gate 		/* Gather the arguments into bootargs_buf. */
13577c478bd9Sstevel@tonic-gate 		if (gather_args(argv, bootargs_buf, sizeof (bootargs_buf)) !=
13587c478bd9Sstevel@tonic-gate 		    0) {
13597c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
13607c478bd9Sstevel@tonic-gate 			    gettext("%s: Boot arguments too long.\n"), cmdname);
13617c478bd9Sstevel@tonic-gate 			return (1);
13627c478bd9Sstevel@tonic-gate 		}
136319397407SSherry Moore 
13643b860eeeSSherry Moore 		bootargs_orig = strdup(bootargs_buf);
13657c478bd9Sstevel@tonic-gate 		mdep = (uintptr_t)bootargs_buf;
136619397407SSherry Moore 	} else {
136719397407SSherry Moore 		/*
136819397407SSherry Moore 		 * Initialize it to 0 in case of fastboot, the buffer
136919397407SSherry Moore 		 * will be used.
137019397407SSherry Moore 		 */
137119397407SSherry Moore 		bzero(bootargs_buf, sizeof (bootargs_buf));
13727c478bd9Sstevel@tonic-gate 	}
13737c478bd9Sstevel@tonic-gate 
1374753a6d45SSherry Moore 	if (geteuid() != 0) {
13757c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
13767c478bd9Sstevel@tonic-gate 		    gettext("%s: permission denied\n"), cmdname);
13777c478bd9Sstevel@tonic-gate 		goto fail;
13787c478bd9Sstevel@tonic-gate 	}
13797c478bd9Sstevel@tonic-gate 
1380753a6d45SSherry Moore 	if (fast_reboot && prom_reboot) {
1381753a6d45SSherry Moore 		(void) fprintf(stderr,
1382753a6d45SSherry Moore 		    gettext("%s: -p and -f are mutually exclusive\n"),
1383753a6d45SSherry Moore 		    cmdname);
1384753a6d45SSherry Moore 		return (EINVAL);
1385753a6d45SSherry Moore 	}
138619397407SSherry Moore 	/*
138719397407SSherry Moore 	 * Check whether fast reboot is the default operating mode
138819397407SSherry Moore 	 */
1389753a6d45SSherry Moore 	if (fcn == AD_BOOT && !fast_reboot && !prom_reboot &&
1390*e557d412SChristopher Kiick 	    zoneid == GLOBAL_ZONEID) {
1391753a6d45SSherry Moore 		fast_reboot = scf_is_fastboot_default();
139219397407SSherry Moore 
1393*e557d412SChristopher Kiick 	}
1394*e557d412SChristopher Kiick 
139519397407SSherry Moore 	if (bename && !fast_reboot)	{
139619397407SSherry Moore 		(void) fprintf(stderr, gettext("%s: -e only valid with -f\n"),
139719397407SSherry Moore 		    cmdname);
139819397407SSherry Moore 		return (EINVAL);
139919397407SSherry Moore 	}
140019397407SSherry Moore 
1401*e557d412SChristopher Kiick #if defined(__sparc)
1402*e557d412SChristopher Kiick 	if (fast_reboot) {
1403*e557d412SChristopher Kiick 		fast_reboot = 2;	/* need to distinguish each case */
1404*e557d412SChristopher Kiick 	}
1405*e557d412SChristopher Kiick #endif
1406*e557d412SChristopher Kiick 
140719397407SSherry Moore 	/*
140819397407SSherry Moore 	 * If fast reboot, do some sanity check on the argument
140919397407SSherry Moore 	 */
1410*e557d412SChristopher Kiick 	if (fast_reboot == 1) {
141119397407SSherry Moore 		int rc;
141219397407SSherry Moore 		int is_dryrun = 0;
141319397407SSherry Moore 
141419397407SSherry Moore 		if (zoneid != GLOBAL_ZONEID)	{
141519397407SSherry Moore 			(void) fprintf(stderr,
1416753a6d45SSherry Moore 			    gettext("%s: Fast reboot only valid from global"
141719397407SSherry Moore 			    " zone\n"), cmdname);
141819397407SSherry Moore 			return (EINVAL);
141919397407SSherry Moore 		}
142019397407SSherry Moore 
1421753a6d45SSherry Moore 		rc = parse_fastboot_args(bootargs_buf, sizeof (bootargs_buf),
14225b89e3f7SSherry Moore 		    &is_dryrun, bename);
142319397407SSherry Moore 
142419397407SSherry Moore 		/*
142519397407SSherry Moore 		 * If dry run, or if arguments are invalid, return.
142619397407SSherry Moore 		 */
142719397407SSherry Moore 		if (is_dryrun)
142819397407SSherry Moore 			return (rc);
1429753a6d45SSherry Moore 		else if (rc == EINVAL)
143019397407SSherry Moore 			goto fail;
1431753a6d45SSherry Moore 		else if (rc != 0)
1432753a6d45SSherry Moore 			fast_reboot = 0;
143319397407SSherry Moore 
143419397407SSherry Moore 		/*
143519397407SSherry Moore 		 * For all the other errors, we continue on in case user
1436753a6d45SSherry Moore 		 * user want to force fast reboot, or fall back to regular
1437753a6d45SSherry Moore 		 * reboot.
143819397407SSherry Moore 		 */
143919397407SSherry Moore 		if (strlen(bootargs_buf) != 0)
144019397407SSherry Moore 			mdep = (uintptr_t)bootargs_buf;
144119397407SSherry Moore 	}
144219397407SSherry Moore 
144319397407SSherry Moore #if 0	/* For debugging */
144419397407SSherry Moore 	if (mdep != NULL)
144519397407SSherry Moore 		(void) fprintf(stderr, "mdep = %s\n", (char *)mdep);
144619397407SSherry Moore #endif
144719397407SSherry Moore 
14487c478bd9Sstevel@tonic-gate 	if (fcn != AD_BOOT && ttyn != NULL &&
14497c478bd9Sstevel@tonic-gate 	    strncmp(ttyn, "/dev/term/", strlen("/dev/term/")) == 0) {
14507c478bd9Sstevel@tonic-gate 		/*
14517c478bd9Sstevel@tonic-gate 		 * TRANSLATION_NOTE
14527c478bd9Sstevel@tonic-gate 		 * Don't translate ``halt -y''
14537c478bd9Sstevel@tonic-gate 		 */
14547c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
14557c478bd9Sstevel@tonic-gate 		    gettext("%s: dangerous on a dialup;"), cmdname);
14567c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
14577c478bd9Sstevel@tonic-gate 		    gettext("use ``%s -y'' if you are really sure\n"), cmdname);
14587c478bd9Sstevel@tonic-gate 		goto fail;
14597c478bd9Sstevel@tonic-gate 	}
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate 	if (needlog) {
14627c478bd9Sstevel@tonic-gate 		char *user = getlogin();
14637c478bd9Sstevel@tonic-gate 		struct passwd *pw;
1464f040a7a6Ssetje 		char *tty;
14657c478bd9Sstevel@tonic-gate 
14667c478bd9Sstevel@tonic-gate 		openlog(cmdname, 0, LOG_AUTH);
14677c478bd9Sstevel@tonic-gate 		if (user == NULL && (pw = getpwuid(getuid())) != NULL)
14687c478bd9Sstevel@tonic-gate 			user = pw->pw_name;
14697c478bd9Sstevel@tonic-gate 		if (user == NULL)
14707c478bd9Sstevel@tonic-gate 			user = "root";
1471f040a7a6Ssetje 
1472f040a7a6Ssetje 		tty = ttyname(1);
1473f040a7a6Ssetje 
1474f040a7a6Ssetje 		if (tty == NULL)
1475f040a7a6Ssetje 			syslog(LOG_CRIT, "initiated by %s", user);
1476f040a7a6Ssetje 		else
1477f040a7a6Ssetje 			syslog(LOG_CRIT, "initiated by %s on %s", user, tty);
14787c478bd9Sstevel@tonic-gate 	}
14797c478bd9Sstevel@tonic-gate 
14807c478bd9Sstevel@tonic-gate 	/*
14817c478bd9Sstevel@tonic-gate 	 * We must assume success and log it before auditd is terminated.
14827c478bd9Sstevel@tonic-gate 	 */
14837c478bd9Sstevel@tonic-gate 	if (fcn == AD_BOOT)
14847c478bd9Sstevel@tonic-gate 		aval = audit_reboot_success();
14857c478bd9Sstevel@tonic-gate 	else
14867c478bd9Sstevel@tonic-gate 		aval = audit_halt_success();
14877c478bd9Sstevel@tonic-gate 
14887c478bd9Sstevel@tonic-gate 	if (aval == -1) {
14897c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
14907c478bd9Sstevel@tonic-gate 		    gettext("%s: can't turn off auditd\n"), cmdname);
14917c478bd9Sstevel@tonic-gate 		if (needlog)
14927c478bd9Sstevel@tonic-gate 			(void) sleep(5); /* Give syslogd time to record this */
14937c478bd9Sstevel@tonic-gate 	}
14947c478bd9Sstevel@tonic-gate 
14957c478bd9Sstevel@tonic-gate 	(void) signal(SIGHUP, SIG_IGN);	/* for remote connections */
14967c478bd9Sstevel@tonic-gate 
149726f665e8Sdstaff 	/*
149826f665e8Sdstaff 	 * We start to fork a bunch of zoneadms to halt any active zones.
149926f665e8Sdstaff 	 * This will proceed with halt in parallel until we call
150026f665e8Sdstaff 	 * check_zone_haltedness later on.
150126f665e8Sdstaff 	 */
150226f665e8Sdstaff 	if (zoneid == GLOBAL_ZONEID && cmd != A_DUMP) {
15033f2f09c1Sdp 		need_check_zones = halt_zones();
150426f665e8Sdstaff 	}
150526f665e8Sdstaff 
1506753a6d45SSherry Moore #if defined(__i386)
1507753a6d45SSherry Moore 	/* set new default entry in the GRUB entry */
1508753a6d45SSherry Moore 	if (fbarg_entnum != GRUB_ENTRY_DEFAULT) {
1509753a6d45SSherry Moore 		char buf[32];
1510753a6d45SSherry Moore 		(void) snprintf(buf, sizeof (buf), "default=%u", fbarg_entnum);
1511753a6d45SSherry Moore 		(void) halt_exec(BOOTADM_PROG, "set-menu", buf, NULL);
1512753a6d45SSherry Moore 	}
1513753a6d45SSherry Moore #endif	/* __i386 */
151448847494SEnrico Perla - Sun Microsystems 
1515753a6d45SSherry Moore 	/* if we're dumping, do the archive update here and don't defer it */
1516de0cf919SEnrico Perla - Sun Microsystems 	if (cmd == A_DUMP && zoneid == GLOBAL_ZONEID && !nosync)
1517de0cf919SEnrico Perla - Sun Microsystems 		do_archives_update(fast_reboot);
15187c478bd9Sstevel@tonic-gate 
15197c478bd9Sstevel@tonic-gate 	/*
15207c478bd9Sstevel@tonic-gate 	 * If we're not forcing a crash dump, mark the system as quiescing for
15217c478bd9Sstevel@tonic-gate 	 * smf(5)'s benefit, and idle the init process.
15227c478bd9Sstevel@tonic-gate 	 */
15237c478bd9Sstevel@tonic-gate 	if (cmd != A_DUMP) {
15243f2f09c1Sdp 		if (direct_init(PCDSTOP) == -1) {
15257c478bd9Sstevel@tonic-gate 			/*
15267c478bd9Sstevel@tonic-gate 			 * TRANSLATION_NOTE
15277c478bd9Sstevel@tonic-gate 			 * Don't translate the word "init"
15287c478bd9Sstevel@tonic-gate 			 */
15297c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
15307c478bd9Sstevel@tonic-gate 			    gettext("%s: can't idle init\n"), cmdname);
15317c478bd9Sstevel@tonic-gate 			goto fail;
15327c478bd9Sstevel@tonic-gate 		}
15337c478bd9Sstevel@tonic-gate 
15347c478bd9Sstevel@tonic-gate 		if (creat(resetting, 0755) == -1)
15357c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
15367c478bd9Sstevel@tonic-gate 			    gettext("%s: could not create %s.\n"),
15377c478bd9Sstevel@tonic-gate 			    cmdname, resetting);
15389db7147eSSherry Moore 	}
15397c478bd9Sstevel@tonic-gate 
15407c478bd9Sstevel@tonic-gate 	/*
15419db7147eSSherry Moore 	 * Make sure we don't get stopped by a jobcontrol shell
15429db7147eSSherry Moore 	 * once we start killing everybody.
15439db7147eSSherry Moore 	 */
15449db7147eSSherry Moore 	(void) signal(SIGTSTP, SIG_IGN);
15459db7147eSSherry Moore 	(void) signal(SIGTTIN, SIG_IGN);
15469db7147eSSherry Moore 	(void) signal(SIGTTOU, SIG_IGN);
15479db7147eSSherry Moore 	(void) signal(SIGPIPE, SIG_IGN);
15489db7147eSSherry Moore 	(void) signal(SIGTERM, SIG_IGN);
15499db7147eSSherry Moore 
15509db7147eSSherry Moore 	/*
15519db7147eSSherry Moore 	 * Try to stop gdm so X has a chance to return the screen and
15529db7147eSSherry Moore 	 * keyboard to a sane state.
15539db7147eSSherry Moore 	 */
1554*e557d412SChristopher Kiick 	if (fast_reboot == 1 && stop_gdm() != 0) {
15559db7147eSSherry Moore 		(void) fprintf(stderr,
15569db7147eSSherry Moore 		    gettext("%s: Falling back to regular reboot.\n"), cmdname);
15579db7147eSSherry Moore 		fast_reboot = 0;
15583b860eeeSSherry Moore 		mdep = (uintptr_t)bootargs_orig;
15593b860eeeSSherry Moore 	} else if (bootargs_orig) {
15603b860eeeSSherry Moore 		free(bootargs_orig);
15619db7147eSSherry Moore 	}
15629db7147eSSherry Moore 
15639db7147eSSherry Moore 	if (cmd != A_DUMP) {
15649db7147eSSherry Moore 		/*
15657c478bd9Sstevel@tonic-gate 		 * Stop all restarters so they do not try to restart services
15667c478bd9Sstevel@tonic-gate 		 * that are terminated.
15677c478bd9Sstevel@tonic-gate 		 */
15687c478bd9Sstevel@tonic-gate 		stop_restarters();
156926f665e8Sdstaff 
157026f665e8Sdstaff 		/*
157126f665e8Sdstaff 		 * Wait a little while for zones to shutdown.
157226f665e8Sdstaff 		 */
157326f665e8Sdstaff 		if (need_check_zones) {
15743f2f09c1Sdp 			check_zones_haltedness();
157526f665e8Sdstaff 
157626f665e8Sdstaff 			(void) fprintf(stderr,
157726f665e8Sdstaff 			    gettext("%s: Completing system halt.\n"),
157826f665e8Sdstaff 			    cmdname);
157926f665e8Sdstaff 		}
15807c478bd9Sstevel@tonic-gate 	}
15817c478bd9Sstevel@tonic-gate 
15827c478bd9Sstevel@tonic-gate 	/*
15837c478bd9Sstevel@tonic-gate 	 * If we're not forcing a crash dump, give everyone 5 seconds to
15847c478bd9Sstevel@tonic-gate 	 * handle a SIGTERM and clean up properly.
15857c478bd9Sstevel@tonic-gate 	 */
15867c478bd9Sstevel@tonic-gate 	if (cmd != A_DUMP) {
158748847494SEnrico Perla - Sun Microsystems 		int	start, end, delta;
158848847494SEnrico Perla - Sun Microsystems 
15897c478bd9Sstevel@tonic-gate 		(void) kill(-1, SIGTERM);
159048847494SEnrico Perla - Sun Microsystems 		start = time(NULL);
159148847494SEnrico Perla - Sun Microsystems 
1592de0cf919SEnrico Perla - Sun Microsystems 		if (zoneid == GLOBAL_ZONEID && !nosync)
1593de0cf919SEnrico Perla - Sun Microsystems 			do_archives_update(fast_reboot);
159448847494SEnrico Perla - Sun Microsystems 
159548847494SEnrico Perla - Sun Microsystems 		end = time(NULL);
159648847494SEnrico Perla - Sun Microsystems 		delta = end - start;
159748847494SEnrico Perla - Sun Microsystems 		if (delta < 5)
159848847494SEnrico Perla - Sun Microsystems 			(void) sleep(5 - delta);
15997c478bd9Sstevel@tonic-gate 	}
16007c478bd9Sstevel@tonic-gate 
16013a0fa635SEnrico Perla - Sun Microsystems 	(void) signal(SIGINT, SIG_IGN);
16023a0fa635SEnrico Perla - Sun Microsystems 
16037c478bd9Sstevel@tonic-gate 	if (!qflag && !nosync) {
16047c478bd9Sstevel@tonic-gate 		struct utmpx wtmpx;
16057c478bd9Sstevel@tonic-gate 
16067c478bd9Sstevel@tonic-gate 		bzero(&wtmpx, sizeof (struct utmpx));
16077c478bd9Sstevel@tonic-gate 		(void) strcpy(wtmpx.ut_line, "~");
16087c478bd9Sstevel@tonic-gate 		(void) time(&wtmpx.ut_tv.tv_sec);
16097c478bd9Sstevel@tonic-gate 
16107c478bd9Sstevel@tonic-gate 		if (cmd == A_DUMP)
16117c478bd9Sstevel@tonic-gate 			(void) strcpy(wtmpx.ut_name, "crash dump");
16127c478bd9Sstevel@tonic-gate 		else
16137c478bd9Sstevel@tonic-gate 			(void) strcpy(wtmpx.ut_name, "shutdown");
16147c478bd9Sstevel@tonic-gate 
16157c478bd9Sstevel@tonic-gate 		(void) updwtmpx(WTMPX_FILE, &wtmpx);
16167c478bd9Sstevel@tonic-gate 		sync();
16177c478bd9Sstevel@tonic-gate 	}
16187c478bd9Sstevel@tonic-gate 
16197c478bd9Sstevel@tonic-gate 	if (cmd == A_DUMP && nosync != 0)
16207c478bd9Sstevel@tonic-gate 		(void) uadmin(A_DUMP, AD_NOSYNC, NULL);
16217c478bd9Sstevel@tonic-gate 
16225b89e3f7SSherry Moore 	if (fast_reboot)
162319397407SSherry Moore 		fcn = AD_FASTREBOOT;
162419397407SSherry Moore 
16253f2f09c1Sdp 	if (uadmin(cmd, fcn, mdep) == -1)
16263f2f09c1Sdp 		(void) fprintf(stderr, "%s: uadmin failed: %s\n",
16273f2f09c1Sdp 		    cmdname, strerror(errno));
16283f2f09c1Sdp 	else
16293f2f09c1Sdp 		(void) fprintf(stderr, "%s: uadmin unexpectedly returned 0\n",
16303f2f09c1Sdp 		    cmdname);
16313f2f09c1Sdp 
16323f2f09c1Sdp 	do {
16337c478bd9Sstevel@tonic-gate 		r = remove(resetting);
16343f2f09c1Sdp 	} while (r != 0 && errno == EINTR);
16353f2f09c1Sdp 
16367c478bd9Sstevel@tonic-gate 	if (r != 0 && errno != ENOENT)
16377c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: could not remove %s.\n"),
16387c478bd9Sstevel@tonic-gate 		    cmdname, resetting);
16397c478bd9Sstevel@tonic-gate 
16403f2f09c1Sdp 	if (direct_init(PCRUN) == -1) {
16413f2f09c1Sdp 		/*
16423f2f09c1Sdp 		 * TRANSLATION_NOTE
16433f2f09c1Sdp 		 * Don't translate the word "init"
16443f2f09c1Sdp 		 */
16453f2f09c1Sdp 		(void) fprintf(stderr,
16463f2f09c1Sdp 		    gettext("%s: can't resume init\n"), cmdname);
16473f2f09c1Sdp 	}
16483f2f09c1Sdp 
16497c478bd9Sstevel@tonic-gate 	continue_restarters();
16507c478bd9Sstevel@tonic-gate 
16513f2f09c1Sdp 	if (get_initpid() != -1)
16527c478bd9Sstevel@tonic-gate 		/* tell init to restate current level */
16533f2f09c1Sdp 		(void) kill(get_initpid(), SIGHUP);
16547c478bd9Sstevel@tonic-gate 
16557c478bd9Sstevel@tonic-gate fail:
16567c478bd9Sstevel@tonic-gate 	if (fcn == AD_BOOT)
16577c478bd9Sstevel@tonic-gate 		(void) audit_reboot_fail();
16587c478bd9Sstevel@tonic-gate 	else
16597c478bd9Sstevel@tonic-gate 		(void) audit_halt_fail();
16607c478bd9Sstevel@tonic-gate 
1661*e557d412SChristopher Kiick 	if (fast_reboot == 1) {
166219397407SSherry Moore 		if (bename) {
16634e1f1c13SKonstantin Ananyev 			(void) halt_exec(LUUMOUNT_PROG, "-n", bename, NULL);
166419397407SSherry Moore 
166519397407SSherry Moore 		} else if (strlen(fastboot_mounted) != 0) {
166619397407SSherry Moore 			(void) umount(fastboot_mounted);
1667753a6d45SSherry Moore #if defined(__i386)
1668753a6d45SSherry Moore 		} else if (fbarg_used != NULL) {
1669753a6d45SSherry Moore 			grub_cleanup_boot_args(fbarg_used);
1670753a6d45SSherry Moore #endif	/* __i386 */
167119397407SSherry Moore 		}
167219397407SSherry Moore 	}
167319397407SSherry Moore 
16747c478bd9Sstevel@tonic-gate 	return (1);
16757c478bd9Sstevel@tonic-gate }
1676