xref: /illumos-gate/usr/src/cmd/halt/halt.c (revision 3f2f09c1efd66f6d2995998ea72c5df8c70c9a97)
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
5*3f2f09c1Sdp  * Common Development and Distribution License (the "License").
6*3f2f09c1Sdp  * 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*3f2f09c1Sdp  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
317c478bd9Sstevel@tonic-gate  * The Regents of the University of California
327c478bd9Sstevel@tonic-gate  * All Rights Reserved
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
357c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
367c478bd9Sstevel@tonic-gate  * contributors.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
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 
46*3f2f09c1Sdp #include <procfs.h>
477c478bd9Sstevel@tonic-gate #include <sys/types.h>
487c478bd9Sstevel@tonic-gate #include <sys/uadmin.h>
497c478bd9Sstevel@tonic-gate #include <alloca.h>
507c478bd9Sstevel@tonic-gate #include <assert.h>
517c478bd9Sstevel@tonic-gate #include <errno.h>
527c478bd9Sstevel@tonic-gate #include <fcntl.h>
537c478bd9Sstevel@tonic-gate #include <libgen.h>
547c478bd9Sstevel@tonic-gate #include <libscf.h>
557c478bd9Sstevel@tonic-gate #include <locale.h>
567c478bd9Sstevel@tonic-gate #include <libintl.h>
577c478bd9Sstevel@tonic-gate #include <syslog.h>
587c478bd9Sstevel@tonic-gate #include <signal.h>
597c478bd9Sstevel@tonic-gate #include <strings.h>
607c478bd9Sstevel@tonic-gate #include <unistd.h>
617c478bd9Sstevel@tonic-gate #include <stdlib.h>
627c478bd9Sstevel@tonic-gate #include <stdio.h>
637c478bd9Sstevel@tonic-gate #include <strings.h>
647c478bd9Sstevel@tonic-gate #include <time.h>
657c478bd9Sstevel@tonic-gate #include <utmpx.h>
667c478bd9Sstevel@tonic-gate #include <pwd.h>
677c478bd9Sstevel@tonic-gate #include <zone.h>
687c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
697c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"
707c478bd9Sstevel@tonic-gate #endif
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate extern int audit_halt_setup(int, char **);
737c478bd9Sstevel@tonic-gate extern int audit_halt_success(void);
747c478bd9Sstevel@tonic-gate extern int audit_halt_fail(void);
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate extern int audit_reboot_setup(void);
777c478bd9Sstevel@tonic-gate extern int audit_reboot_success(void);
787c478bd9Sstevel@tonic-gate extern int audit_reboot_fail(void);
797c478bd9Sstevel@tonic-gate 
80*3f2f09c1Sdp static char *cmdname;	/* basename(argv[0]), the name of the command */
81*3f2f09c1Sdp 
827c478bd9Sstevel@tonic-gate typedef struct ctidlist_struct {
837c478bd9Sstevel@tonic-gate 	ctid_t ctid;
847c478bd9Sstevel@tonic-gate 	struct ctidlist_struct *next;
857c478bd9Sstevel@tonic-gate } ctidlist_t;
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate static ctidlist_t *ctidlist = NULL;
887c478bd9Sstevel@tonic-gate static ctid_t startdct = -1;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate #define	FMRI_STARTD_CONTRACT \
917c478bd9Sstevel@tonic-gate 	"svc:/system/svc/restarter:default/:properties/restarter/contract"
927c478bd9Sstevel@tonic-gate 
9326f665e8Sdstaff #define	ZONEADM_PROG "/usr/sbin/zoneadm"
9426f665e8Sdstaff 
95*3f2f09c1Sdp static pid_t
96*3f2f09c1Sdp get_initpid()
97*3f2f09c1Sdp {
98*3f2f09c1Sdp 	static int init_pid = -1;
99*3f2f09c1Sdp 
100*3f2f09c1Sdp 	if (init_pid == -1) {
101*3f2f09c1Sdp 		if (zone_getattr(getzoneid(), ZONE_ATTR_INITPID, &init_pid,
102*3f2f09c1Sdp 		    sizeof (init_pid)) != sizeof (init_pid)) {
103*3f2f09c1Sdp 			assert(errno == ESRCH);
104*3f2f09c1Sdp 			init_pid = -1;
105*3f2f09c1Sdp 		}
106*3f2f09c1Sdp 	}
107*3f2f09c1Sdp 	return (init_pid);
108*3f2f09c1Sdp }
109*3f2f09c1Sdp 
110*3f2f09c1Sdp /*
111*3f2f09c1Sdp  * Quiesce or resume init using /proc.  When stopping init, we can't send
112*3f2f09c1Sdp  * SIGTSTP (since init ignores it) or SIGSTOP (since the kernel won't permit
113*3f2f09c1Sdp  * it).
114*3f2f09c1Sdp  */
115*3f2f09c1Sdp static int
116*3f2f09c1Sdp direct_init(long command)
117*3f2f09c1Sdp {
118*3f2f09c1Sdp 	char ctlfile[MAXPATHLEN];
119*3f2f09c1Sdp 	pid_t pid;
120*3f2f09c1Sdp 	int ctlfd;
121*3f2f09c1Sdp 
122*3f2f09c1Sdp 	assert(command == PCDSTOP || command == PCRUN);
123*3f2f09c1Sdp 	if ((pid = get_initpid()) == -1) {
124*3f2f09c1Sdp 		return (-1);
125*3f2f09c1Sdp 	}
126*3f2f09c1Sdp 
127*3f2f09c1Sdp 	(void) snprintf(ctlfile, sizeof (ctlfile), "/proc/%d/ctl", pid);
128*3f2f09c1Sdp 	if ((ctlfd = open(ctlfile, O_WRONLY)) == -1)
129*3f2f09c1Sdp 		return (-1);
130*3f2f09c1Sdp 
131*3f2f09c1Sdp 	if (command == PCDSTOP) {
132*3f2f09c1Sdp 		if (write(ctlfd, &command, sizeof (long)) == -1) {
133*3f2f09c1Sdp 			(void) close(ctlfd);
134*3f2f09c1Sdp 			return (-1);
135*3f2f09c1Sdp 		}
136*3f2f09c1Sdp 	} else {	/* command == PCRUN */
137*3f2f09c1Sdp 		long cmds[2];
138*3f2f09c1Sdp 		cmds[0] = command;
139*3f2f09c1Sdp 		cmds[1] = 0;
140*3f2f09c1Sdp 		if (write(ctlfd, cmds, sizeof (cmds)) == -1) {
141*3f2f09c1Sdp 			(void) close(ctlfd);
142*3f2f09c1Sdp 			return (-1);
143*3f2f09c1Sdp 		}
144*3f2f09c1Sdp 	}
145*3f2f09c1Sdp 	(void) close(ctlfd);
146*3f2f09c1Sdp 	return (0);
147*3f2f09c1Sdp }
148*3f2f09c1Sdp 
1497c478bd9Sstevel@tonic-gate static void
1507c478bd9Sstevel@tonic-gate stop_startd()
1517c478bd9Sstevel@tonic-gate {
1527c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
1537c478bd9Sstevel@tonic-gate 	scf_property_t *prop = NULL;
1547c478bd9Sstevel@tonic-gate 	scf_value_t *val = NULL;
1557c478bd9Sstevel@tonic-gate 	uint64_t uint64;
1567c478bd9Sstevel@tonic-gate 
157*3f2f09c1Sdp 	if ((h = scf_handle_create(SCF_VERSION)) == NULL)
1587c478bd9Sstevel@tonic-gate 		return;
1597c478bd9Sstevel@tonic-gate 
160*3f2f09c1Sdp 	if ((scf_handle_bind(h) != 0) ||
161*3f2f09c1Sdp 	    ((prop = scf_property_create(h)) == NULL) ||
162*3f2f09c1Sdp 	    ((val = scf_value_create(h)) == NULL))
1637c478bd9Sstevel@tonic-gate 		goto out;
1647c478bd9Sstevel@tonic-gate 
165*3f2f09c1Sdp 	if (scf_handle_decode_fmri(h, FMRI_STARTD_CONTRACT,
166*3f2f09c1Sdp 	    NULL, NULL, NULL, NULL, prop, SCF_DECODE_FMRI_EXACT) != 0)
1677c478bd9Sstevel@tonic-gate 		goto out;
1687c478bd9Sstevel@tonic-gate 
169*3f2f09c1Sdp 	if (scf_property_is_type(prop, SCF_TYPE_COUNT) != 0 ||
170*3f2f09c1Sdp 	    scf_property_get_value(prop, val) != 0 ||
171*3f2f09c1Sdp 	    scf_value_get_count(val, &uint64) != 0)
1727c478bd9Sstevel@tonic-gate 		goto out;
1737c478bd9Sstevel@tonic-gate 
174*3f2f09c1Sdp 	startdct = (ctid_t)uint64;
175*3f2f09c1Sdp 	(void) sigsend(P_CTID, startdct, SIGSTOP);
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate out:
178*3f2f09c1Sdp 	scf_property_destroy(prop);
179*3f2f09c1Sdp 	scf_value_destroy(val);
1807c478bd9Sstevel@tonic-gate 	scf_handle_destroy(h);
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate static void
1847c478bd9Sstevel@tonic-gate continue_startd()
1857c478bd9Sstevel@tonic-gate {
1867c478bd9Sstevel@tonic-gate 	if (startdct != -1)
1877c478bd9Sstevel@tonic-gate 		(void) sigsend(P_CTID, startdct, SIGCONT);
1887c478bd9Sstevel@tonic-gate }
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate #define	FMRI_RESTARTER_PROP "/:properties/general/restarter"
1917c478bd9Sstevel@tonic-gate #define	FMRI_CONTRACT_PROP "/:properties/restarter/contract"
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate static int
1947c478bd9Sstevel@tonic-gate save_ctid(ctid_t ctid)
1957c478bd9Sstevel@tonic-gate {
1967c478bd9Sstevel@tonic-gate 	ctidlist_t *next;
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	for (next = ctidlist; next != NULL; next = next->next)
1997c478bd9Sstevel@tonic-gate 		if (next->ctid == ctid)
2007c478bd9Sstevel@tonic-gate 			return (-1);
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	next = (ctidlist_t *)malloc(sizeof (ctidlist_t));
2037c478bd9Sstevel@tonic-gate 	if (next == NULL)
2047c478bd9Sstevel@tonic-gate 		return (-1);
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	next->ctid = ctid;
2077c478bd9Sstevel@tonic-gate 	next->next = ctidlist;
2087c478bd9Sstevel@tonic-gate 	ctidlist = next;
2097c478bd9Sstevel@tonic-gate 	return (0);
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate static void
2137c478bd9Sstevel@tonic-gate stop_delegates()
2147c478bd9Sstevel@tonic-gate {
2157c478bd9Sstevel@tonic-gate 	ctid_t ctid;
2167c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
2177c478bd9Sstevel@tonic-gate 	scf_scope_t *sc = NULL;
2187c478bd9Sstevel@tonic-gate 	scf_service_t *svc = NULL;
2197c478bd9Sstevel@tonic-gate 	scf_instance_t *inst = NULL;
2207c478bd9Sstevel@tonic-gate 	scf_snapshot_t *snap = NULL;
2217c478bd9Sstevel@tonic-gate 	scf_snapshot_t *isnap = NULL;
2227c478bd9Sstevel@tonic-gate 	scf_propertygroup_t *pg = NULL;
2237c478bd9Sstevel@tonic-gate 	scf_property_t *prop = NULL;
2247c478bd9Sstevel@tonic-gate 	scf_value_t *val = NULL;
2257c478bd9Sstevel@tonic-gate 	scf_iter_t *siter = NULL;
2267c478bd9Sstevel@tonic-gate 	scf_iter_t *iiter = NULL;
2277c478bd9Sstevel@tonic-gate 	char *fmri;
2287c478bd9Sstevel@tonic-gate 	ssize_t length;
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	uint64_t uint64;
2317c478bd9Sstevel@tonic-gate 	ssize_t bytes;
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	length = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH);
2347c478bd9Sstevel@tonic-gate 	if (length <= 0)
2357c478bd9Sstevel@tonic-gate 		return;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	length++;
2387c478bd9Sstevel@tonic-gate 	fmri = alloca(length * sizeof (char));
2397c478bd9Sstevel@tonic-gate 
240*3f2f09c1Sdp 	if ((h = scf_handle_create(SCF_VERSION)) == NULL)
2417c478bd9Sstevel@tonic-gate 		return;
2427c478bd9Sstevel@tonic-gate 
243*3f2f09c1Sdp 	if (scf_handle_bind(h) != 0) {
2447c478bd9Sstevel@tonic-gate 		scf_handle_destroy(h);
2457c478bd9Sstevel@tonic-gate 		return;
2467c478bd9Sstevel@tonic-gate 	}
2477c478bd9Sstevel@tonic-gate 
248*3f2f09c1Sdp 	if ((sc = scf_scope_create(h)) == NULL ||
249*3f2f09c1Sdp 	    (svc = scf_service_create(h)) == NULL ||
250*3f2f09c1Sdp 	    (inst = scf_instance_create(h)) == NULL ||
251*3f2f09c1Sdp 	    (snap = scf_snapshot_create(h)) == NULL ||
252*3f2f09c1Sdp 	    (pg = scf_pg_create(h)) == NULL ||
253*3f2f09c1Sdp 	    (prop = scf_property_create(h)) == NULL ||
254*3f2f09c1Sdp 	    (val = scf_value_create(h)) == NULL ||
255*3f2f09c1Sdp 	    (siter = scf_iter_create(h)) == NULL ||
256*3f2f09c1Sdp 	    (iiter = scf_iter_create(h)) == NULL)
2577c478bd9Sstevel@tonic-gate 		goto out;
2587c478bd9Sstevel@tonic-gate 
259*3f2f09c1Sdp 	if (scf_handle_get_scope(h, SCF_SCOPE_LOCAL, sc) != 0)
2607c478bd9Sstevel@tonic-gate 		goto out;
2617c478bd9Sstevel@tonic-gate 
262*3f2f09c1Sdp 	if (scf_iter_scope_services(siter, sc) != 0)
2637c478bd9Sstevel@tonic-gate 		goto out;
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	while (scf_iter_next_service(siter, svc) == 1) {
2667c478bd9Sstevel@tonic-gate 
267*3f2f09c1Sdp 		if (scf_iter_service_instances(iiter, svc) != 0)
2687c478bd9Sstevel@tonic-gate 			continue;
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 		while (scf_iter_next_instance(iiter, inst) == 1) {
2717c478bd9Sstevel@tonic-gate 
272*3f2f09c1Sdp 			if ((scf_instance_get_snapshot(inst, "running",
273*3f2f09c1Sdp 			    snap)) != 0)
274*3f2f09c1Sdp 				isnap = NULL;
275*3f2f09c1Sdp 			else
276*3f2f09c1Sdp 				isnap = snap;
2777c478bd9Sstevel@tonic-gate 
278*3f2f09c1Sdp 			if (scf_instance_get_pg_composed(inst, isnap,
279*3f2f09c1Sdp 			    SCF_PG_GENERAL, pg) != 0)
2807c478bd9Sstevel@tonic-gate 				continue;
2817c478bd9Sstevel@tonic-gate 
282*3f2f09c1Sdp 			if (scf_pg_get_property(pg, SCF_PROPERTY_RESTARTER,
283*3f2f09c1Sdp 			    prop) != 0 ||
284*3f2f09c1Sdp 			    scf_property_get_value(prop, val) != 0)
2857c478bd9Sstevel@tonic-gate 				continue;
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 			bytes = scf_value_get_astring(val, fmri, length);
2887c478bd9Sstevel@tonic-gate 			if (bytes <= 0 || bytes >= length)
2897c478bd9Sstevel@tonic-gate 				continue;
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 			if (strlcat(fmri, FMRI_CONTRACT_PROP, length) >=
2927c478bd9Sstevel@tonic-gate 			    length)
2937c478bd9Sstevel@tonic-gate 				continue;
2947c478bd9Sstevel@tonic-gate 
295*3f2f09c1Sdp 			if (scf_handle_decode_fmri(h, fmri, NULL, NULL,
296*3f2f09c1Sdp 			    NULL, NULL, prop, SCF_DECODE_FMRI_EXACT) != 0)
2977c478bd9Sstevel@tonic-gate 				continue;
2987c478bd9Sstevel@tonic-gate 
299*3f2f09c1Sdp 			if (scf_property_is_type(prop, SCF_TYPE_COUNT) != 0 ||
300*3f2f09c1Sdp 			    scf_property_get_value(prop, val) != 0 ||
301*3f2f09c1Sdp 			    scf_value_get_count(val, &uint64) != 0)
3027c478bd9Sstevel@tonic-gate 				continue;
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 			ctid = (ctid_t)uint64;
3057c478bd9Sstevel@tonic-gate 			if (save_ctid(ctid) == 0) {
3067c478bd9Sstevel@tonic-gate 				(void) sigsend(P_CTID, ctid, SIGSTOP);
3077c478bd9Sstevel@tonic-gate 			}
3087c478bd9Sstevel@tonic-gate 		}
3097c478bd9Sstevel@tonic-gate 	}
3107c478bd9Sstevel@tonic-gate out:
311*3f2f09c1Sdp 	scf_scope_destroy(sc);
312*3f2f09c1Sdp 	scf_service_destroy(svc);
313*3f2f09c1Sdp 	scf_instance_destroy(inst);
314*3f2f09c1Sdp 	scf_snapshot_destroy(snap);
315*3f2f09c1Sdp 	scf_pg_destroy(pg);
316*3f2f09c1Sdp 	scf_property_destroy(prop);
317*3f2f09c1Sdp 	scf_value_destroy(val);
318*3f2f09c1Sdp 	scf_iter_destroy(siter);
319*3f2f09c1Sdp 	scf_iter_destroy(iiter);
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 	(void) scf_handle_unbind(h);
3227c478bd9Sstevel@tonic-gate 	scf_handle_destroy(h);
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate static void
3267c478bd9Sstevel@tonic-gate continue_delegates()
3277c478bd9Sstevel@tonic-gate {
3287c478bd9Sstevel@tonic-gate 	ctidlist_t *next;
3297c478bd9Sstevel@tonic-gate 	for (next = ctidlist; next != NULL; next = next->next)
3307c478bd9Sstevel@tonic-gate 		(void) sigsend(P_CTID, next->ctid, SIGCONT);
3317c478bd9Sstevel@tonic-gate }
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate static void
3347c478bd9Sstevel@tonic-gate stop_restarters()
3357c478bd9Sstevel@tonic-gate {
3367c478bd9Sstevel@tonic-gate 	stop_startd();
3377c478bd9Sstevel@tonic-gate 	stop_delegates();
3387c478bd9Sstevel@tonic-gate }
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate static void
3417c478bd9Sstevel@tonic-gate continue_restarters()
3427c478bd9Sstevel@tonic-gate {
3437c478bd9Sstevel@tonic-gate 	continue_startd();
3447c478bd9Sstevel@tonic-gate 	continue_delegates();
3457c478bd9Sstevel@tonic-gate }
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate /*
3487c478bd9Sstevel@tonic-gate  * Copy an array of strings into buf, separated by spaces.  Returns 0 on
3497c478bd9Sstevel@tonic-gate  * success.
3507c478bd9Sstevel@tonic-gate  */
3517c478bd9Sstevel@tonic-gate static int
3527c478bd9Sstevel@tonic-gate gather_args(char **args, char *buf, size_t buf_sz)
3537c478bd9Sstevel@tonic-gate {
3547c478bd9Sstevel@tonic-gate 	if (strlcpy(buf, *args, buf_sz) >= buf_sz)
3557c478bd9Sstevel@tonic-gate 		return (-1);
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	for (++args; *args != NULL; ++args) {
3587c478bd9Sstevel@tonic-gate 		if (strlcat(buf, " ", buf_sz) >= buf_sz)
3597c478bd9Sstevel@tonic-gate 			return (-1);
3607c478bd9Sstevel@tonic-gate 		if (strlcat(buf, *args, buf_sz) >= buf_sz)
3617c478bd9Sstevel@tonic-gate 			return (-1);
3627c478bd9Sstevel@tonic-gate 	}
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	return (0);
3657c478bd9Sstevel@tonic-gate }
3667c478bd9Sstevel@tonic-gate 
36726f665e8Sdstaff /*
36826f665e8Sdstaff  * Halt every zone on the system.  We are committed to doing a shutdown
36926f665e8Sdstaff  * even if something goes wrong here. If something goes wrong, we just
37026f665e8Sdstaff  * continue with the shutdown.  Return non-zero if we need to wait for zones to
37126f665e8Sdstaff  * halt later on.
37226f665e8Sdstaff  */
37326f665e8Sdstaff static int
374*3f2f09c1Sdp halt_zones()
37526f665e8Sdstaff {
37626f665e8Sdstaff 	pid_t pid;
37726f665e8Sdstaff 	zoneid_t *zones;
378*3f2f09c1Sdp 	size_t nz = 0, old_nz;
37926f665e8Sdstaff 	int i;
38026f665e8Sdstaff 	char zname[ZONENAME_MAX];
38126f665e8Sdstaff 
38226f665e8Sdstaff 	/*
38326f665e8Sdstaff 	 * Get a list of zones. If the number of zones changes in between the
38426f665e8Sdstaff 	 * two zone_list calls, try again.
38526f665e8Sdstaff 	 */
38626f665e8Sdstaff 
38726f665e8Sdstaff 	for (;;) {
38826f665e8Sdstaff 		(void) zone_list(NULL, &nz);
38926f665e8Sdstaff 		if (nz == 1)
39026f665e8Sdstaff 			return (0);
39126f665e8Sdstaff 		old_nz = nz;
39226f665e8Sdstaff 		zones = calloc(sizeof (zoneid_t), nz);
39326f665e8Sdstaff 		if (zones == NULL) {
39426f665e8Sdstaff 			(void) fprintf(stderr,
39526f665e8Sdstaff 			    gettext("%s: Could not halt zones"
396*3f2f09c1Sdp 			    " (out of memory).\n"), cmdname);
39726f665e8Sdstaff 			return (0);
39826f665e8Sdstaff 		}
39926f665e8Sdstaff 
40026f665e8Sdstaff 		(void) zone_list(zones, &nz);
40126f665e8Sdstaff 		if (old_nz == nz)
40226f665e8Sdstaff 			break;
40326f665e8Sdstaff 		free(zones);
40426f665e8Sdstaff 	}
40526f665e8Sdstaff 
40626f665e8Sdstaff 	if (nz == 2) {
407*3f2f09c1Sdp 		(void) fprintf(stderr, gettext("%s: Halting 1 zone.\n"),
408*3f2f09c1Sdp 		    cmdname);
40926f665e8Sdstaff 	} else {
410*3f2f09c1Sdp 		(void) fprintf(stderr, gettext("%s: Halting %i zones.\n"),
411*3f2f09c1Sdp 		    cmdname, nz - 1);
41226f665e8Sdstaff 	}
41326f665e8Sdstaff 
41426f665e8Sdstaff 	for (i = 0; i < nz; i++) {
41526f665e8Sdstaff 		if (zones[i] == GLOBAL_ZONEID)
41626f665e8Sdstaff 			continue;
41726f665e8Sdstaff 		if (getzonenamebyid(zones[i], zname, sizeof (zname)) < 0) {
41826f665e8Sdstaff 			/*
41926f665e8Sdstaff 			 * getzonenamebyid should only fail if we raced with
42026f665e8Sdstaff 			 * another process trying to shut down the zone.
42126f665e8Sdstaff 			 * We assume this happened and ignore the error.
42226f665e8Sdstaff 			 */
42326f665e8Sdstaff 			if (errno != EINVAL) {
42426f665e8Sdstaff 				(void) fprintf(stderr,
42526f665e8Sdstaff 				    gettext("%s: Unexpected error while "
42626f665e8Sdstaff 				    "looking up zone %ul: %s.\n"),
427*3f2f09c1Sdp 				    cmdname, zones[i], strerror(errno));
42826f665e8Sdstaff 			}
42926f665e8Sdstaff 
43026f665e8Sdstaff 			continue;
43126f665e8Sdstaff 		}
43226f665e8Sdstaff 		pid = fork();
43326f665e8Sdstaff 		if (pid < 0) {
43426f665e8Sdstaff 			(void) fprintf(stderr,
43526f665e8Sdstaff 			    gettext("%s: Zone \"%s\" could not be"
43626f665e8Sdstaff 			    " halted (could not fork(): %s).\n"),
437*3f2f09c1Sdp 			    cmdname, zname, strerror(errno));
43826f665e8Sdstaff 			continue;
43926f665e8Sdstaff 		}
44026f665e8Sdstaff 		if (pid == 0) {
44126f665e8Sdstaff 			(void) execl(ZONEADM_PROG, ZONEADM_PROG,
44226f665e8Sdstaff 			    "-z", zname, "halt", NULL);
44326f665e8Sdstaff 			(void) fprintf(stderr,
44426f665e8Sdstaff 			    gettext("%s: Zone \"%s\" could not be halted"
44526f665e8Sdstaff 			    " (cannot exec(" ZONEADM_PROG "): %s).\n"),
446*3f2f09c1Sdp 			    cmdname, zname, strerror(errno));
44726f665e8Sdstaff 			exit(0);
44826f665e8Sdstaff 		}
44926f665e8Sdstaff 	}
45026f665e8Sdstaff 
45126f665e8Sdstaff 	return (1);
45226f665e8Sdstaff }
45326f665e8Sdstaff 
45426f665e8Sdstaff /*
45526f665e8Sdstaff  * This function tries to wait for all non-global zones to go away.
45626f665e8Sdstaff  * It will timeout if no progress is made for 5 seconds, or a total of
45726f665e8Sdstaff  * 30 seconds elapses.
45826f665e8Sdstaff  */
45926f665e8Sdstaff 
46026f665e8Sdstaff static void
461*3f2f09c1Sdp check_zones_haltedness()
46226f665e8Sdstaff {
46326f665e8Sdstaff 	int t = 0, t_prog = 0;
46426f665e8Sdstaff 	size_t nz = 0, last_nz;
46526f665e8Sdstaff 
46626f665e8Sdstaff 	do {
46726f665e8Sdstaff 		last_nz = nz;
46826f665e8Sdstaff 		(void) zone_list(NULL, &nz);
46926f665e8Sdstaff 		if (nz == 1)
47026f665e8Sdstaff 			return;
47126f665e8Sdstaff 
47226f665e8Sdstaff 		(void) sleep(1);
47326f665e8Sdstaff 
47426f665e8Sdstaff 		if (last_nz > nz)
47526f665e8Sdstaff 			t_prog = 0;
47626f665e8Sdstaff 
47726f665e8Sdstaff 		t++;
47826f665e8Sdstaff 		t_prog++;
47926f665e8Sdstaff 
48026f665e8Sdstaff 		if (t == 10) {
48126f665e8Sdstaff 			if (nz == 2) {
48226f665e8Sdstaff 				(void) fprintf(stderr,
48326f665e8Sdstaff 				    gettext("%s: Still waiting for 1 zone to "
48426f665e8Sdstaff 				    "halt. Will wait up to 20 seconds.\n"),
485*3f2f09c1Sdp 				    cmdname);
48626f665e8Sdstaff 			} else {
48726f665e8Sdstaff 				(void) fprintf(stderr,
48826f665e8Sdstaff 				    gettext("%s: Still waiting for %i zones "
48926f665e8Sdstaff 				    "to halt. Will wait up to 20 seconds.\n"),
490*3f2f09c1Sdp 				    cmdname, nz - 1);
49126f665e8Sdstaff 			}
49226f665e8Sdstaff 		}
49326f665e8Sdstaff 
49426f665e8Sdstaff 	} while ((t < 30) && (t_prog < 5));
49526f665e8Sdstaff }
49626f665e8Sdstaff 
4977c478bd9Sstevel@tonic-gate int
4987c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
4997c478bd9Sstevel@tonic-gate {
5007c478bd9Sstevel@tonic-gate 	char *ttyn = ttyname(STDERR_FILENO);
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 	int qflag = 0, needlog = 1, nosync = 0;
5037c478bd9Sstevel@tonic-gate 	uintptr_t mdep = NULL;
5047c478bd9Sstevel@tonic-gate 	int cmd, fcn, c, aval, r;
5057c478bd9Sstevel@tonic-gate 	const char *usage;
5067c478bd9Sstevel@tonic-gate 	zoneid_t zoneid = getzoneid();
507*3f2f09c1Sdp 	int need_check_zones = 0;
5087c478bd9Sstevel@tonic-gate 
509*3f2f09c1Sdp 	char bootargs_buf[BOOTARGS_MAX];
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 	const char * const resetting = "/etc/svc/volatile/resetting";
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
5147c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
5157c478bd9Sstevel@tonic-gate 
516*3f2f09c1Sdp 	cmdname = basename(argv[0]);
517*3f2f09c1Sdp 
5187c478bd9Sstevel@tonic-gate 	if (strcmp(cmdname, "halt") == 0) {
5197c478bd9Sstevel@tonic-gate 		(void) audit_halt_setup(argc, argv);
5207c478bd9Sstevel@tonic-gate 		usage = gettext("usage: %s [ -dlnqy ]\n");
5217c478bd9Sstevel@tonic-gate 		cmd = A_SHUTDOWN;
5227c478bd9Sstevel@tonic-gate 		fcn = AD_HALT;
5237c478bd9Sstevel@tonic-gate 	} else if (strcmp(cmdname, "poweroff") == 0) {
5247c478bd9Sstevel@tonic-gate 		(void) audit_halt_setup(argc, argv);
5257c478bd9Sstevel@tonic-gate 		usage = gettext("usage: %s [ -dlnqy ]\n");
5267c478bd9Sstevel@tonic-gate 		cmd = A_SHUTDOWN;
5277c478bd9Sstevel@tonic-gate 		fcn = AD_POWEROFF;
5287c478bd9Sstevel@tonic-gate 	} else if (strcmp(cmdname, "reboot") == 0) {
5297c478bd9Sstevel@tonic-gate 		(void) audit_reboot_setup();
5307c478bd9Sstevel@tonic-gate 		usage = gettext("usage: %s [ -dlnq ] [ boot args ]\n");
5317c478bd9Sstevel@tonic-gate 		cmd = A_SHUTDOWN;
5327c478bd9Sstevel@tonic-gate 		fcn = AD_BOOT;
5337c478bd9Sstevel@tonic-gate 	} else {
5347c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
5357c478bd9Sstevel@tonic-gate 		    gettext("%s: not installed properly\n"), cmdname);
5367c478bd9Sstevel@tonic-gate 		return (1);
5377c478bd9Sstevel@tonic-gate 	}
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "dlnqy")) != EOF) {
5407c478bd9Sstevel@tonic-gate 		switch (c) {
5417c478bd9Sstevel@tonic-gate 		case 'd':
5427c478bd9Sstevel@tonic-gate 			if (zoneid == GLOBAL_ZONEID)
5437c478bd9Sstevel@tonic-gate 				cmd = A_DUMP;
5447c478bd9Sstevel@tonic-gate 			else {
5457c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
5467c478bd9Sstevel@tonic-gate 				    gettext("%s: -d only valid from global"
5477c478bd9Sstevel@tonic-gate 				    " zone\n"), cmdname);
5487c478bd9Sstevel@tonic-gate 				return (1);
5497c478bd9Sstevel@tonic-gate 			}
5507c478bd9Sstevel@tonic-gate 			break;
5517c478bd9Sstevel@tonic-gate 		case 'l':
5527c478bd9Sstevel@tonic-gate 			needlog = 0;
5537c478bd9Sstevel@tonic-gate 			break;
5547c478bd9Sstevel@tonic-gate 		case 'n':
5557c478bd9Sstevel@tonic-gate 			nosync = 1;
5567c478bd9Sstevel@tonic-gate 			break;
5577c478bd9Sstevel@tonic-gate 		case 'q':
5587c478bd9Sstevel@tonic-gate 			qflag = 1;
5597c478bd9Sstevel@tonic-gate 			break;
5607c478bd9Sstevel@tonic-gate 		case 'y':
5617c478bd9Sstevel@tonic-gate 			ttyn = NULL;
5627c478bd9Sstevel@tonic-gate 			break;
5637c478bd9Sstevel@tonic-gate 		default:
5647c478bd9Sstevel@tonic-gate 			/*
5657c478bd9Sstevel@tonic-gate 			 * TRANSLATION_NOTE
5667c478bd9Sstevel@tonic-gate 			 * Don't translate the words "halt" or "reboot"
5677c478bd9Sstevel@tonic-gate 			 */
5687c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, usage, cmdname);
5697c478bd9Sstevel@tonic-gate 			return (1);
5707c478bd9Sstevel@tonic-gate 		}
5717c478bd9Sstevel@tonic-gate 	}
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	argc -= optind;
5747c478bd9Sstevel@tonic-gate 	argv += optind;
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 	if (argc != 0) {
5777c478bd9Sstevel@tonic-gate 		if (fcn != AD_BOOT) {
5787c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, usage, cmdname);
5797c478bd9Sstevel@tonic-gate 			return (1);
5807c478bd9Sstevel@tonic-gate 		}
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 		/* Gather the arguments into bootargs_buf. */
5837c478bd9Sstevel@tonic-gate 		if (gather_args(argv, bootargs_buf, sizeof (bootargs_buf)) !=
5847c478bd9Sstevel@tonic-gate 		    0) {
5857c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
5867c478bd9Sstevel@tonic-gate 			    gettext("%s: Boot arguments too long.\n"), cmdname);
5877c478bd9Sstevel@tonic-gate 			return (1);
5887c478bd9Sstevel@tonic-gate 		}
5897c478bd9Sstevel@tonic-gate 		mdep = (uintptr_t)bootargs_buf;
5907c478bd9Sstevel@tonic-gate 	}
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 	if (geteuid() != 0) {
5937c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
5947c478bd9Sstevel@tonic-gate 		    gettext("%s: permission denied\n"), cmdname);
5957c478bd9Sstevel@tonic-gate 		goto fail;
5967c478bd9Sstevel@tonic-gate 	}
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 	if (fcn != AD_BOOT && ttyn != NULL &&
5997c478bd9Sstevel@tonic-gate 	    strncmp(ttyn, "/dev/term/", strlen("/dev/term/")) == 0) {
6007c478bd9Sstevel@tonic-gate 		/*
6017c478bd9Sstevel@tonic-gate 		 * TRANSLATION_NOTE
6027c478bd9Sstevel@tonic-gate 		 * Don't translate ``halt -y''
6037c478bd9Sstevel@tonic-gate 		 */
6047c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
6057c478bd9Sstevel@tonic-gate 		    gettext("%s: dangerous on a dialup;"), cmdname);
6067c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
6077c478bd9Sstevel@tonic-gate 		    gettext("use ``%s -y'' if you are really sure\n"), cmdname);
6087c478bd9Sstevel@tonic-gate 		goto fail;
6097c478bd9Sstevel@tonic-gate 	}
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 	if (needlog) {
6127c478bd9Sstevel@tonic-gate 		char *user = getlogin();
6137c478bd9Sstevel@tonic-gate 		struct passwd *pw;
614f040a7a6Ssetje 		char *tty;
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 		openlog(cmdname, 0, LOG_AUTH);
6177c478bd9Sstevel@tonic-gate 		if (user == NULL && (pw = getpwuid(getuid())) != NULL)
6187c478bd9Sstevel@tonic-gate 			user = pw->pw_name;
6197c478bd9Sstevel@tonic-gate 		if (user == NULL)
6207c478bd9Sstevel@tonic-gate 			user = "root";
621f040a7a6Ssetje 
622f040a7a6Ssetje 		tty = ttyname(1);
623f040a7a6Ssetje 
624f040a7a6Ssetje 		if (tty == NULL)
625f040a7a6Ssetje 			syslog(LOG_CRIT, "initiated by %s", user);
626f040a7a6Ssetje 		else
627f040a7a6Ssetje 			syslog(LOG_CRIT, "initiated by %s on %s", user, tty);
6287c478bd9Sstevel@tonic-gate 	}
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	/*
6317c478bd9Sstevel@tonic-gate 	 * We must assume success and log it before auditd is terminated.
6327c478bd9Sstevel@tonic-gate 	 */
6337c478bd9Sstevel@tonic-gate 	if (fcn == AD_BOOT)
6347c478bd9Sstevel@tonic-gate 		aval = audit_reboot_success();
6357c478bd9Sstevel@tonic-gate 	else
6367c478bd9Sstevel@tonic-gate 		aval = audit_halt_success();
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	if (aval == -1) {
6397c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
6407c478bd9Sstevel@tonic-gate 		    gettext("%s: can't turn off auditd\n"), cmdname);
6417c478bd9Sstevel@tonic-gate 		if (needlog)
6427c478bd9Sstevel@tonic-gate 			(void) sleep(5); /* Give syslogd time to record this */
6437c478bd9Sstevel@tonic-gate 	}
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 	(void) signal(SIGHUP, SIG_IGN);	/* for remote connections */
6467c478bd9Sstevel@tonic-gate 
64726f665e8Sdstaff 	/*
64826f665e8Sdstaff 	 * We start to fork a bunch of zoneadms to halt any active zones.
64926f665e8Sdstaff 	 * This will proceed with halt in parallel until we call
65026f665e8Sdstaff 	 * check_zone_haltedness later on.
65126f665e8Sdstaff 	 */
65226f665e8Sdstaff 	if (zoneid == GLOBAL_ZONEID && cmd != A_DUMP) {
653*3f2f09c1Sdp 		need_check_zones = halt_zones();
65426f665e8Sdstaff 	}
65526f665e8Sdstaff 
65626f665e8Sdstaff 
6577c478bd9Sstevel@tonic-gate 	/* sync boot archive in the global zone */
658*3f2f09c1Sdp 	if (zoneid == GLOBAL_ZONEID && !nosync) {
6597c478bd9Sstevel@tonic-gate 		(void) system("/sbin/bootadm -a update_all");
6607c478bd9Sstevel@tonic-gate 	}
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 	/*
6637c478bd9Sstevel@tonic-gate 	 * If we're not forcing a crash dump, mark the system as quiescing for
6647c478bd9Sstevel@tonic-gate 	 * smf(5)'s benefit, and idle the init process.
6657c478bd9Sstevel@tonic-gate 	 */
6667c478bd9Sstevel@tonic-gate 	if (cmd != A_DUMP) {
667*3f2f09c1Sdp 		if (direct_init(PCDSTOP) == -1) {
6687c478bd9Sstevel@tonic-gate 			/*
6697c478bd9Sstevel@tonic-gate 			 * TRANSLATION_NOTE
6707c478bd9Sstevel@tonic-gate 			 * Don't translate the word "init"
6717c478bd9Sstevel@tonic-gate 			 */
6727c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
6737c478bd9Sstevel@tonic-gate 			    gettext("%s: can't idle init\n"), cmdname);
6747c478bd9Sstevel@tonic-gate 			goto fail;
6757c478bd9Sstevel@tonic-gate 		}
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 		if (creat(resetting, 0755) == -1)
6787c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
6797c478bd9Sstevel@tonic-gate 			    gettext("%s: could not create %s.\n"),
6807c478bd9Sstevel@tonic-gate 			    cmdname, resetting);
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 		/*
6837c478bd9Sstevel@tonic-gate 		 * Stop all restarters so they do not try to restart services
6847c478bd9Sstevel@tonic-gate 		 * that are terminated.
6857c478bd9Sstevel@tonic-gate 		 */
6867c478bd9Sstevel@tonic-gate 		stop_restarters();
68726f665e8Sdstaff 
68826f665e8Sdstaff 		/*
68926f665e8Sdstaff 		 * Wait a little while for zones to shutdown.
69026f665e8Sdstaff 		 */
69126f665e8Sdstaff 		if (need_check_zones) {
692*3f2f09c1Sdp 			check_zones_haltedness();
69326f665e8Sdstaff 
69426f665e8Sdstaff 			(void) fprintf(stderr,
69526f665e8Sdstaff 			    gettext("%s: Completing system halt.\n"),
69626f665e8Sdstaff 			    cmdname);
69726f665e8Sdstaff 		}
6987c478bd9Sstevel@tonic-gate 	}
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 	/*
7017c478bd9Sstevel@tonic-gate 	 * Make sure we don't get stopped by a jobcontrol shell
7027c478bd9Sstevel@tonic-gate 	 * once we start killing everybody.
7037c478bd9Sstevel@tonic-gate 	 */
7047c478bd9Sstevel@tonic-gate 	(void) signal(SIGTSTP, SIG_IGN);
7057c478bd9Sstevel@tonic-gate 	(void) signal(SIGTTIN, SIG_IGN);
7067c478bd9Sstevel@tonic-gate 	(void) signal(SIGTTOU, SIG_IGN);
7077c478bd9Sstevel@tonic-gate 	(void) signal(SIGTERM, SIG_IGN);
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 	/*
7107c478bd9Sstevel@tonic-gate 	 * If we're not forcing a crash dump, give everyone 5 seconds to
7117c478bd9Sstevel@tonic-gate 	 * handle a SIGTERM and clean up properly.
7127c478bd9Sstevel@tonic-gate 	 */
7137c478bd9Sstevel@tonic-gate 	if (cmd != A_DUMP) {
7147c478bd9Sstevel@tonic-gate 		(void) kill(-1, SIGTERM);
7157c478bd9Sstevel@tonic-gate 		(void) sleep(5);
7167c478bd9Sstevel@tonic-gate 	}
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 	if (!qflag && !nosync) {
7197c478bd9Sstevel@tonic-gate 		struct utmpx wtmpx;
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 		bzero(&wtmpx, sizeof (struct utmpx));
7227c478bd9Sstevel@tonic-gate 		(void) strcpy(wtmpx.ut_line, "~");
7237c478bd9Sstevel@tonic-gate 		(void) time(&wtmpx.ut_tv.tv_sec);
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 		if (cmd == A_DUMP)
7267c478bd9Sstevel@tonic-gate 			(void) strcpy(wtmpx.ut_name, "crash dump");
7277c478bd9Sstevel@tonic-gate 		else
7287c478bd9Sstevel@tonic-gate 			(void) strcpy(wtmpx.ut_name, "shutdown");
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 		(void) updwtmpx(WTMPX_FILE, &wtmpx);
7317c478bd9Sstevel@tonic-gate 		sync();
7327c478bd9Sstevel@tonic-gate 	}
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 	if (cmd == A_DUMP && nosync != 0)
7357c478bd9Sstevel@tonic-gate 		(void) uadmin(A_DUMP, AD_NOSYNC, NULL);
7367c478bd9Sstevel@tonic-gate 
737*3f2f09c1Sdp 	if (uadmin(cmd, fcn, mdep) == -1)
738*3f2f09c1Sdp 		(void) fprintf(stderr, "%s: uadmin failed: %s\n",
739*3f2f09c1Sdp 		    cmdname, strerror(errno));
740*3f2f09c1Sdp 	else
741*3f2f09c1Sdp 		(void) fprintf(stderr, "%s: uadmin unexpectedly returned 0\n",
742*3f2f09c1Sdp 		    cmdname);
743*3f2f09c1Sdp 
744*3f2f09c1Sdp 	do {
7457c478bd9Sstevel@tonic-gate 		r = remove(resetting);
746*3f2f09c1Sdp 	} while (r != 0 && errno == EINTR);
747*3f2f09c1Sdp 
7487c478bd9Sstevel@tonic-gate 	if (r != 0 && errno != ENOENT)
7497c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: could not remove %s.\n"),
7507c478bd9Sstevel@tonic-gate 		    cmdname, resetting);
7517c478bd9Sstevel@tonic-gate 
752*3f2f09c1Sdp 	if (direct_init(PCRUN) == -1) {
753*3f2f09c1Sdp 		/*
754*3f2f09c1Sdp 		 * TRANSLATION_NOTE
755*3f2f09c1Sdp 		 * Don't translate the word "init"
756*3f2f09c1Sdp 		 */
757*3f2f09c1Sdp 		(void) fprintf(stderr,
758*3f2f09c1Sdp 		    gettext("%s: can't resume init\n"), cmdname);
759*3f2f09c1Sdp 	}
760*3f2f09c1Sdp 
7617c478bd9Sstevel@tonic-gate 	continue_restarters();
7627c478bd9Sstevel@tonic-gate 
763*3f2f09c1Sdp 	if (get_initpid() != -1)
7647c478bd9Sstevel@tonic-gate 		/* tell init to restate current level */
765*3f2f09c1Sdp 		(void) kill(get_initpid(), SIGHUP);
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate fail:
7687c478bd9Sstevel@tonic-gate 	if (fcn == AD_BOOT)
7697c478bd9Sstevel@tonic-gate 		(void) audit_reboot_fail();
7707c478bd9Sstevel@tonic-gate 	else
7717c478bd9Sstevel@tonic-gate 		(void) audit_halt_fail();
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 	return (1);
7747c478bd9Sstevel@tonic-gate }
775