xref: /titanic_52/usr/src/cmd/halt/halt.c (revision 47b4653e9ff2a8aebb64f9e357713fd04108674b)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
27 /*	  All Rights Reserved  	*/
28 
29 /*
30  * University Copyright- Copyright (c) 1982, 1986, 1988
31  * The Regents of the University of California
32  * All Rights Reserved
33  *
34  * University Acknowledgment- Portions of this document are derived from
35  * software developed by the University of California, Berkeley, and its
36  * contributors.
37  * Portions contributed by Juergen Keil, <jk@tools.de>.
38  */
39 
40 
41 /*
42  * Common code for halt(1M), poweroff(1M), and reboot(1M).  We use
43  * argv[0] to determine which behavior to exhibit.
44  */
45 
46 #include <stdio.h>
47 #include <procfs.h>
48 #include <sys/types.h>
49 #include <sys/elf.h>
50 #include <sys/systeminfo.h>
51 #include <sys/stat.h>
52 #include <sys/uadmin.h>
53 #include <sys/mntent.h>
54 #include <sys/mnttab.h>
55 #include <sys/mount.h>
56 #include <sys/fs/ufs_mount.h>
57 #include <alloca.h>
58 #include <assert.h>
59 #include <errno.h>
60 #include <fcntl.h>
61 #include <libgen.h>
62 #include <libscf.h>
63 #include <limits.h>
64 #include <locale.h>
65 #include <libintl.h>
66 #include <syslog.h>
67 #include <signal.h>
68 #include <strings.h>
69 #include <unistd.h>
70 #include <stdlib.h>
71 #include <stdio.h>
72 #include <strings.h>
73 #include <time.h>
74 #include <wait.h>
75 #include <ctype.h>
76 #include <utmpx.h>
77 #include <pwd.h>
78 #include <zone.h>
79 #include <spawn.h>
80 
81 #include <libzfs.h>
82 
83 #if !defined(TEXT_DOMAIN)
84 #define	TEXT_DOMAIN	"SYS_TEST"
85 #endif
86 
87 #if defined(__sparc)
88 #define	CUR_ELFDATA	ELFDATA2MSB
89 #elif defined(__i386)
90 #define	CUR_ELFDATA	ELFDATA2LSB
91 #endif
92 
93 static libzfs_handle_t *g_zfs;
94 
95 extern int audit_halt_setup(int, char **);
96 extern int audit_halt_success(void);
97 extern int audit_halt_fail(void);
98 
99 extern int audit_reboot_setup(void);
100 extern int audit_reboot_success(void);
101 extern int audit_reboot_fail(void);
102 
103 static char *cmdname;	/* basename(argv[0]), the name of the command */
104 
105 typedef struct ctidlist_struct {
106 	ctid_t ctid;
107 	struct ctidlist_struct *next;
108 } ctidlist_t;
109 
110 static ctidlist_t *ctidlist = NULL;
111 static ctid_t startdct = -1;
112 
113 #define	FMRI_STARTD_CONTRACT \
114 	"svc:/system/svc/restarter:default/:properties/restarter/contract"
115 
116 #define	ZONEADM_PROG "/usr/sbin/zoneadm"
117 
118 #define	LUUMOUNT_PROG	"/usr/sbin/luumount"
119 #define	LUMOUNT_PROG	"/usr/sbin/lumount"
120 
121 /*
122  * The length of FASTBOOT_MOUNTPOINT must be less than MAXPATHLEN.
123  */
124 #define	FASTBOOT_MOUNTPOINT	"/tmp/.fastboot.root"
125 
126 static char	fastboot_mounted[MAXPATHLEN];
127 
128 static int validate_ufs_disk(char *, char *);
129 static int validate_zfs_pool(char *, char *);
130 
131 static pid_t
132 get_initpid()
133 {
134 	static int init_pid = -1;
135 
136 	if (init_pid == -1) {
137 		if (zone_getattr(getzoneid(), ZONE_ATTR_INITPID, &init_pid,
138 		    sizeof (init_pid)) != sizeof (init_pid)) {
139 			assert(errno == ESRCH);
140 			init_pid = -1;
141 		}
142 	}
143 	return (init_pid);
144 }
145 
146 /*
147  * Quiesce or resume init using /proc.  When stopping init, we can't send
148  * SIGTSTP (since init ignores it) or SIGSTOP (since the kernel won't permit
149  * it).
150  */
151 static int
152 direct_init(long command)
153 {
154 	char ctlfile[MAXPATHLEN];
155 	pid_t pid;
156 	int ctlfd;
157 
158 	assert(command == PCDSTOP || command == PCRUN);
159 	if ((pid = get_initpid()) == -1) {
160 		return (-1);
161 	}
162 
163 	(void) snprintf(ctlfile, sizeof (ctlfile), "/proc/%d/ctl", pid);
164 	if ((ctlfd = open(ctlfile, O_WRONLY)) == -1)
165 		return (-1);
166 
167 	if (command == PCDSTOP) {
168 		if (write(ctlfd, &command, sizeof (long)) == -1) {
169 			(void) close(ctlfd);
170 			return (-1);
171 		}
172 	} else {	/* command == PCRUN */
173 		long cmds[2];
174 		cmds[0] = command;
175 		cmds[1] = 0;
176 		if (write(ctlfd, cmds, sizeof (cmds)) == -1) {
177 			(void) close(ctlfd);
178 			return (-1);
179 		}
180 	}
181 	(void) close(ctlfd);
182 	return (0);
183 }
184 
185 static void
186 stop_startd()
187 {
188 	scf_handle_t *h;
189 	scf_property_t *prop = NULL;
190 	scf_value_t *val = NULL;
191 	uint64_t uint64;
192 
193 	if ((h = scf_handle_create(SCF_VERSION)) == NULL)
194 		return;
195 
196 	if ((scf_handle_bind(h) != 0) ||
197 	    ((prop = scf_property_create(h)) == NULL) ||
198 	    ((val = scf_value_create(h)) == NULL))
199 		goto out;
200 
201 	if (scf_handle_decode_fmri(h, FMRI_STARTD_CONTRACT,
202 	    NULL, NULL, NULL, NULL, prop, SCF_DECODE_FMRI_EXACT) != 0)
203 		goto out;
204 
205 	if (scf_property_is_type(prop, SCF_TYPE_COUNT) != 0 ||
206 	    scf_property_get_value(prop, val) != 0 ||
207 	    scf_value_get_count(val, &uint64) != 0)
208 		goto out;
209 
210 	startdct = (ctid_t)uint64;
211 	(void) sigsend(P_CTID, startdct, SIGSTOP);
212 
213 out:
214 	scf_property_destroy(prop);
215 	scf_value_destroy(val);
216 	scf_handle_destroy(h);
217 }
218 
219 static void
220 continue_startd()
221 {
222 	if (startdct != -1)
223 		(void) sigsend(P_CTID, startdct, SIGCONT);
224 }
225 
226 #define	FMRI_RESTARTER_PROP "/:properties/general/restarter"
227 #define	FMRI_CONTRACT_PROP "/:properties/restarter/contract"
228 
229 static int
230 save_ctid(ctid_t ctid)
231 {
232 	ctidlist_t *next;
233 
234 	for (next = ctidlist; next != NULL; next = next->next)
235 		if (next->ctid == ctid)
236 			return (-1);
237 
238 	next = (ctidlist_t *)malloc(sizeof (ctidlist_t));
239 	if (next == NULL)
240 		return (-1);
241 
242 	next->ctid = ctid;
243 	next->next = ctidlist;
244 	ctidlist = next;
245 	return (0);
246 }
247 
248 static void
249 stop_delegates()
250 {
251 	ctid_t ctid;
252 	scf_handle_t *h;
253 	scf_scope_t *sc = NULL;
254 	scf_service_t *svc = NULL;
255 	scf_instance_t *inst = NULL;
256 	scf_snapshot_t *snap = NULL;
257 	scf_snapshot_t *isnap = NULL;
258 	scf_propertygroup_t *pg = NULL;
259 	scf_property_t *prop = NULL;
260 	scf_value_t *val = NULL;
261 	scf_iter_t *siter = NULL;
262 	scf_iter_t *iiter = NULL;
263 	char *fmri;
264 	ssize_t length;
265 
266 	uint64_t uint64;
267 	ssize_t bytes;
268 
269 	length = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH);
270 	if (length <= 0)
271 		return;
272 
273 	length++;
274 	fmri = alloca(length * sizeof (char));
275 
276 	if ((h = scf_handle_create(SCF_VERSION)) == NULL)
277 		return;
278 
279 	if (scf_handle_bind(h) != 0) {
280 		scf_handle_destroy(h);
281 		return;
282 	}
283 
284 	if ((sc = scf_scope_create(h)) == NULL ||
285 	    (svc = scf_service_create(h)) == NULL ||
286 	    (inst = scf_instance_create(h)) == NULL ||
287 	    (snap = scf_snapshot_create(h)) == NULL ||
288 	    (pg = scf_pg_create(h)) == NULL ||
289 	    (prop = scf_property_create(h)) == NULL ||
290 	    (val = scf_value_create(h)) == NULL ||
291 	    (siter = scf_iter_create(h)) == NULL ||
292 	    (iiter = scf_iter_create(h)) == NULL)
293 		goto out;
294 
295 	if (scf_handle_get_scope(h, SCF_SCOPE_LOCAL, sc) != 0)
296 		goto out;
297 
298 	if (scf_iter_scope_services(siter, sc) != 0)
299 		goto out;
300 
301 	while (scf_iter_next_service(siter, svc) == 1) {
302 
303 		if (scf_iter_service_instances(iiter, svc) != 0)
304 			continue;
305 
306 		while (scf_iter_next_instance(iiter, inst) == 1) {
307 
308 			if ((scf_instance_get_snapshot(inst, "running",
309 			    snap)) != 0)
310 				isnap = NULL;
311 			else
312 				isnap = snap;
313 
314 			if (scf_instance_get_pg_composed(inst, isnap,
315 			    SCF_PG_GENERAL, pg) != 0)
316 				continue;
317 
318 			if (scf_pg_get_property(pg, SCF_PROPERTY_RESTARTER,
319 			    prop) != 0 ||
320 			    scf_property_get_value(prop, val) != 0)
321 				continue;
322 
323 			bytes = scf_value_get_astring(val, fmri, length);
324 			if (bytes <= 0 || bytes >= length)
325 				continue;
326 
327 			if (strlcat(fmri, FMRI_CONTRACT_PROP, length) >=
328 			    length)
329 				continue;
330 
331 			if (scf_handle_decode_fmri(h, fmri, NULL, NULL,
332 			    NULL, NULL, prop, SCF_DECODE_FMRI_EXACT) != 0)
333 				continue;
334 
335 			if (scf_property_is_type(prop, SCF_TYPE_COUNT) != 0 ||
336 			    scf_property_get_value(prop, val) != 0 ||
337 			    scf_value_get_count(val, &uint64) != 0)
338 				continue;
339 
340 			ctid = (ctid_t)uint64;
341 			if (save_ctid(ctid) == 0) {
342 				(void) sigsend(P_CTID, ctid, SIGSTOP);
343 			}
344 		}
345 	}
346 out:
347 	scf_scope_destroy(sc);
348 	scf_service_destroy(svc);
349 	scf_instance_destroy(inst);
350 	scf_snapshot_destroy(snap);
351 	scf_pg_destroy(pg);
352 	scf_property_destroy(prop);
353 	scf_value_destroy(val);
354 	scf_iter_destroy(siter);
355 	scf_iter_destroy(iiter);
356 
357 	(void) scf_handle_unbind(h);
358 	scf_handle_destroy(h);
359 }
360 
361 static void
362 continue_delegates()
363 {
364 	ctidlist_t *next;
365 	for (next = ctidlist; next != NULL; next = next->next)
366 		(void) sigsend(P_CTID, next->ctid, SIGCONT);
367 }
368 
369 static void
370 stop_restarters()
371 {
372 	stop_startd();
373 	stop_delegates();
374 }
375 
376 static void
377 continue_restarters()
378 {
379 	continue_startd();
380 	continue_delegates();
381 }
382 
383 /*
384  * Copy an array of strings into buf, separated by spaces.  Returns 0 on
385  * success.
386  */
387 static int
388 gather_args(char **args, char *buf, size_t buf_sz)
389 {
390 	if (strlcpy(buf, *args, buf_sz) >= buf_sz)
391 		return (-1);
392 
393 	for (++args; *args != NULL; ++args) {
394 		if (strlcat(buf, " ", buf_sz) >= buf_sz)
395 			return (-1);
396 		if (strlcat(buf, *args, buf_sz) >= buf_sz)
397 			return (-1);
398 	}
399 
400 	return (0);
401 }
402 
403 /*
404  * Halt every zone on the system.  We are committed to doing a shutdown
405  * even if something goes wrong here. If something goes wrong, we just
406  * continue with the shutdown.  Return non-zero if we need to wait for zones to
407  * halt later on.
408  */
409 static int
410 halt_zones()
411 {
412 	pid_t pid;
413 	zoneid_t *zones;
414 	size_t nz = 0, old_nz;
415 	int i;
416 	char zname[ZONENAME_MAX];
417 
418 	/*
419 	 * Get a list of zones. If the number of zones changes in between the
420 	 * two zone_list calls, try again.
421 	 */
422 
423 	for (;;) {
424 		(void) zone_list(NULL, &nz);
425 		if (nz == 1)
426 			return (0);
427 		old_nz = nz;
428 		zones = calloc(sizeof (zoneid_t), nz);
429 		if (zones == NULL) {
430 			(void) fprintf(stderr,
431 			    gettext("%s: Could not halt zones"
432 			    " (out of memory).\n"), cmdname);
433 			return (0);
434 		}
435 
436 		(void) zone_list(zones, &nz);
437 		if (old_nz == nz)
438 			break;
439 		free(zones);
440 	}
441 
442 	if (nz == 2) {
443 		(void) fprintf(stderr, gettext("%s: Halting 1 zone.\n"),
444 		    cmdname);
445 	} else {
446 		(void) fprintf(stderr, gettext("%s: Halting %i zones.\n"),
447 		    cmdname, nz - 1);
448 	}
449 
450 	for (i = 0; i < nz; i++) {
451 		if (zones[i] == GLOBAL_ZONEID)
452 			continue;
453 		if (getzonenamebyid(zones[i], zname, sizeof (zname)) < 0) {
454 			/*
455 			 * getzonenamebyid should only fail if we raced with
456 			 * another process trying to shut down the zone.
457 			 * We assume this happened and ignore the error.
458 			 */
459 			if (errno != EINVAL) {
460 				(void) fprintf(stderr,
461 				    gettext("%s: Unexpected error while "
462 				    "looking up zone %ul: %s.\n"),
463 				    cmdname, zones[i], strerror(errno));
464 			}
465 
466 			continue;
467 		}
468 		pid = fork();
469 		if (pid < 0) {
470 			(void) fprintf(stderr,
471 			    gettext("%s: Zone \"%s\" could not be"
472 			    " halted (could not fork(): %s).\n"),
473 			    cmdname, zname, strerror(errno));
474 			continue;
475 		}
476 		if (pid == 0) {
477 			(void) execl(ZONEADM_PROG, ZONEADM_PROG,
478 			    "-z", zname, "halt", NULL);
479 			(void) fprintf(stderr,
480 			    gettext("%s: Zone \"%s\" could not be halted"
481 			    " (cannot exec(" ZONEADM_PROG "): %s).\n"),
482 			    cmdname, zname, strerror(errno));
483 			exit(0);
484 		}
485 	}
486 
487 	return (1);
488 }
489 
490 /*
491  * This function tries to wait for all non-global zones to go away.
492  * It will timeout if no progress is made for 5 seconds, or a total of
493  * 30 seconds elapses.
494  */
495 
496 static void
497 check_zones_haltedness()
498 {
499 	int t = 0, t_prog = 0;
500 	size_t nz = 0, last_nz;
501 
502 	do {
503 		last_nz = nz;
504 		(void) zone_list(NULL, &nz);
505 		if (nz == 1)
506 			return;
507 
508 		(void) sleep(1);
509 
510 		if (last_nz > nz)
511 			t_prog = 0;
512 
513 		t++;
514 		t_prog++;
515 
516 		if (t == 10) {
517 			if (nz == 2) {
518 				(void) fprintf(stderr,
519 				    gettext("%s: Still waiting for 1 zone to "
520 				    "halt. Will wait up to 20 seconds.\n"),
521 				    cmdname);
522 			} else {
523 				(void) fprintf(stderr,
524 				    gettext("%s: Still waiting for %i zones "
525 				    "to halt. Will wait up to 20 seconds.\n"),
526 				    cmdname, nz - 1);
527 			}
528 		}
529 
530 	} while ((t < 30) && (t_prog < 5));
531 }
532 
533 
534 /*
535  * Validate that this is a root disk or dataset
536  * Returns 0 if it is a root disk or dataset;
537  * returns 1 if it is a disk argument or dataset, but not valid or not root;
538  * returns -1 if it is not a valid argument or a disk argument.
539  */
540 static int
541 validate_disk(char *arg, char *mountpoint)
542 {
543 	static char root_dev_path[] = "/dev/dsk";
544 	char kernpath[MAXPATHLEN];
545 	struct stat buf;
546 	struct stat64 statbuf;
547 	int rc = 0;
548 
549 	if (strlen(arg) > MAXPATHLEN) {
550 		(void) fprintf(stderr,
551 		    gettext("%s: argument is too long\n"), cmdname);
552 		return (-1);
553 	}
554 
555 	bcopy(FASTBOOT_MOUNTPOINT, mountpoint, sizeof (FASTBOOT_MOUNTPOINT));
556 
557 	/*
558 	 * Do a force umount just in case some other filesystem has
559 	 * been mounted there.
560 	 */
561 	(void) umount2(mountpoint, MS_FORCE);
562 
563 	/* Create the directory if it doesn't already exist */
564 	if (lstat(mountpoint, &buf) != 0) {
565 		if (mkdirp(mountpoint, 0755) != 0) {
566 			(void) fprintf(stderr,
567 			    gettext("failed to create mountpoint %s\n"),
568 			    mountpoint);
569 			return (-1);
570 		}
571 	}
572 
573 	if (strncmp(arg, root_dev_path, strlen(root_dev_path)) == 0) {
574 		/* ufs root disk argument */
575 		rc = validate_ufs_disk(arg, mountpoint);
576 	} else {
577 		/* zfs root pool argument */
578 		rc = validate_zfs_pool(arg, mountpoint);
579 	}
580 
581 	if (rc != 0)
582 		return (rc);
583 
584 	(void) snprintf(kernpath, MAXPATHLEN, "%s/platform/i86pc/kernel/unix",
585 	    mountpoint);
586 
587 	if (stat64(kernpath, &statbuf) != 0) {
588 		(void) fprintf(stderr,
589 		    gettext("%s: %s is not a root disk or dataset\n"),
590 		    cmdname, arg);
591 		return (1);
592 	}
593 
594 	return (0);
595 }
596 
597 
598 static int
599 validate_ufs_disk(char *arg, char *mountpoint)
600 {
601 	struct ufs_args	ufs_args = { 0 };
602 	char mntopts[MNT_LINE_MAX] = MNTOPT_LARGEFILES;
603 
604 	/* perform the mount */
605 	ufs_args.flags = UFSMNT_LARGEFILES;
606 	if (mount(arg, mountpoint, MS_DATA|MS_OPTIONSTR,
607 	    MNTTYPE_UFS, &ufs_args, sizeof (ufs_args),
608 	    mntopts, sizeof (mntopts)) != 0) {
609 		perror(cmdname);
610 		(void) fprintf(stderr,
611 		    gettext("%s: failed to mount %s\n"), cmdname, arg);
612 		return (-1);
613 	}
614 
615 	return (0);
616 }
617 
618 static int
619 validate_zfs_pool(char *arg, char *mountpoint)
620 {
621 	zfs_handle_t *zhp = NULL;
622 	char mntopts[MNT_LINE_MAX] = { '\0' };
623 	int rc = 0;
624 
625 	if ((g_zfs = libzfs_init()) == NULL) {
626 		(void) fprintf(stderr, gettext("internal error: failed to "
627 		    "initialize ZFS library\n"));
628 		return (-1);
629 	}
630 
631 	/* Try to open the dataset */
632 	if ((zhp = zfs_open(g_zfs, arg,
633 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_DATASET)) == NULL)
634 		return (-1);
635 
636 	/* perform the mount */
637 	if (mount(zfs_get_name(zhp), mountpoint, MS_DATA|MS_OPTIONSTR,
638 	    MNTTYPE_ZFS, NULL, 0, mntopts, sizeof (mntopts)) != 0) {
639 		perror(cmdname);
640 		(void) fprintf(stderr,
641 		    gettext("%s: failed to mount %s\n"), cmdname, arg);
642 		rc = -1;
643 	}
644 
645 validate_zfs_err_out:
646 	if (zhp != NULL)
647 		zfs_close(zhp);
648 
649 	libzfs_fini(g_zfs);
650 	return (rc);
651 }
652 
653 /*
654  * Return 0 if not zfs, or is zfs and have successfully constructed the
655  * boot argument; returns non-zero otherwise.
656  * At successful completion fpth contains pointer where mount point ends.
657  * NOTE: arg is supposed to be the resolved path
658  */
659 static int
660 get_zfs_bootfs_arg(const char *arg, const char ** fpth, int *is_zfs,
661 		char *bootfs_arg)
662 {
663 	zfs_handle_t *zhp = NULL;
664 	zpool_handle_t *zpoolp = NULL;
665 	FILE *mtabp = NULL;
666 	struct mnttab mnt;
667 	char *poolname = NULL;
668 	char physpath[MAXNAMELEN];
669 	char mntsp[ZPOOL_MAXNAMELEN];
670 	char bootfs[ZPOOL_MAXNAMELEN];
671 	int rc = 0;
672 	size_t mntlen = 0;
673 	size_t msz;
674 
675 	*fpth = arg;
676 	*is_zfs = 0;
677 
678 	bzero(physpath, sizeof (physpath));
679 	bzero(bootfs, sizeof (bootfs));
680 
681 	if ((mtabp = fopen(MNTTAB, "r")) == NULL) {
682 		return (-1);
683 	}
684 
685 	while (getmntent(mtabp, &mnt) == 0) {
686 		if (strstr(arg, mnt.mnt_mountp) == arg &&
687 		    (msz = strlen(mnt.mnt_mountp)) > mntlen) {
688 			mntlen = msz;
689 			*is_zfs = strcmp(MNTTYPE_ZFS, mnt.mnt_fstype) == 0;
690 			(void) strlcpy(mntsp, mnt.mnt_special, sizeof (mntsp));
691 		}
692 	}
693 
694 	(void) fclose(mtabp);
695 
696 	if (mntlen > 1)
697 		*fpth += mntlen;
698 
699 	if (!*is_zfs)
700 		return (0);
701 
702 	if ((g_zfs = libzfs_init()) == NULL)
703 		return (-1);
704 
705 	/* Try to open the dataset */
706 	if ((zhp = zfs_open(g_zfs, mntsp,
707 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_DATASET)) == NULL) {
708 		(void) fprintf(stderr, gettext("cannot open %s\n"), mntsp);
709 		rc = -1;
710 		goto validate_zfs_err_out;
711 	}
712 
713 	(void) strlcpy(bootfs, mntsp, sizeof (bootfs));
714 
715 	if ((poolname = strtok(mntsp, "/")) == NULL) {
716 		rc = -1;
717 		goto validate_zfs_err_out;
718 	}
719 
720 	if ((zpoolp = zpool_open(g_zfs, poolname)) == NULL) {
721 		(void) fprintf(stderr, gettext("cannot open %s\n"), poolname);
722 		rc = -1;
723 		goto validate_zfs_err_out;
724 	}
725 
726 	if (zpool_get_physpath(zpoolp, physpath) != 0) {
727 		(void) fprintf(stderr, gettext("cannot find phys_path\n"));
728 		rc = -1;
729 		goto validate_zfs_err_out;
730 	}
731 
732 	if (zpool_set_prop(zpoolp, "bootfs", bootfs) != 0) {
733 		(void) fprintf(stderr, gettext("cannot set bootfs to %s\n"),
734 		    bootfs);
735 		rc = -1;
736 		goto validate_zfs_err_out;
737 	}
738 
739 	(void) snprintf(bootfs_arg, BOOTARGS_MAX,
740 	    "-B zfs-bootfs=%s,bootpath=\"%s\"", bootfs, physpath);
741 
742 validate_zfs_err_out:
743 	if (zhp != NULL)
744 		zfs_close(zhp);
745 
746 	if (zpoolp != NULL)
747 		zpool_close(zpoolp);
748 
749 	libzfs_fini(g_zfs);
750 	return (rc);
751 }
752 
753 /*
754  * Validate that the file exists, and is an ELF file.
755  * Returns 0 on success, -1 on failure.
756  */
757 static int
758 validate_unix(char *arg, int *mplen, int *is_zfs, char *bootfs_arg,
759     int *failsafe)
760 {
761 	const char *location;
762 	int class, format;
763 	unsigned char ident[EI_NIDENT];
764 	char physpath[MAXPATHLEN];
765 	int elffd = -1;
766 	size_t	sz;
767 
768 	if ((sz = resolvepath(arg, physpath, sizeof (physpath) - 1)) ==
769 	    (size_t)-1) {
770 		(void) fprintf(stderr,
771 		    gettext("cannot resolve path for %s: %s\n"),
772 		    arg, strerror(errno));
773 		return (-1);
774 	}
775 	(void) strlcpy(arg, physpath, sz + 1);
776 
777 	if (strlen(arg) > MAXPATHLEN) {
778 		(void) fprintf(stderr,
779 		    gettext("%s: new kernel name is too long\n"), cmdname);
780 		return (-1);
781 	}
782 
783 	if (strncmp(basename(arg), "unix", 4) != 0) {
784 		(void) fprintf(stderr,
785 		    gettext("%s: %s: kernel name must be unix\n"),
786 		    cmdname, arg);
787 		return (-1);
788 	}
789 
790 	if (get_zfs_bootfs_arg(arg, &location, is_zfs, bootfs_arg) != 0)
791 		goto err_out;
792 
793 	*mplen = location - arg;
794 
795 	if ((strstr(location, "/boot/platform")) == location)
796 		*failsafe = 1;
797 	else if ((strstr(location, "/platform")) == location)
798 		*failsafe = 0;
799 	else	{
800 		(void) fprintf(stderr,
801 		    gettext("%s: %s: no /boot/platform or /platform in"
802 		    " file name\n"), cmdname, arg);
803 			goto err_out;
804 	}
805 
806 	if ((elffd = open64(arg, O_RDONLY)) < 0 ||
807 	    (pread64(elffd, ident, EI_NIDENT, 0) != EI_NIDENT)) {
808 		(void) fprintf(stderr, "%s: %s: %s\n",
809 		    cmdname, arg, strerror(errno));
810 		goto err_out;
811 	}
812 
813 	class = ident[EI_CLASS];
814 
815 	if ((class != ELFCLASS32 && class != ELFCLASS64) ||
816 	    ident[EI_MAG0] != ELFMAG0 || ident[EI_MAG1] != ELFMAG1 ||
817 	    ident[EI_MAG2] != ELFMAG2 || ident[EI_MAG3] != ELFMAG3) {
818 		(void) fprintf(stderr,
819 		    gettext("%s: %s: not a valid ELF file\n"),
820 		    cmdname, arg);
821 		goto err_out;
822 	}
823 
824 	format = ident[EI_DATA];
825 
826 	if (format != CUR_ELFDATA) {
827 		(void) fprintf(stderr, gettext("%s: %s: invalid data format\n"),
828 		    cmdname, arg);
829 		goto err_out;
830 	}
831 
832 	return (0);
833 
834 err_out:
835 	if (elffd >= 0) {
836 		(void) close(elffd);
837 		elffd = -1;
838 	}
839 	return (-1);
840 }
841 
842 #ifndef	__i386
843 /* ARGSUSED */
844 #endif	/* __i386 */
845 static int
846 is_fastboot_default(uid_t uid)
847 {
848 #if defined(__i386)
849 	int		ret;
850 	struct stat	st;
851 	static const char	fastboot_default[] = "/etc/fastreboot";
852 
853 	ret = (lstat(fastboot_default, &st) == 0 &&
854 	    S_ISREG(st.st_mode) &&
855 	    (st.st_mode & S_IRUSR) != 0 &&
856 	    uid == st.st_uid);
857 
858 	return (ret);
859 #else
860 	return (0);
861 #endif	/* __i386 */
862 }
863 
864 static int
865 halt_exec(const char *path, ...)
866 {
867 	pid_t		pid;
868 	int		i;
869 	int		st;
870 	const char	*arg;
871 	va_list	vp;
872 	const char	*argv[256];
873 
874 	if ((pid = fork()) == -1) {
875 		return (errno);
876 	} else if (pid == 0) {
877 		(void) fclose(stdout);
878 		(void) fclose(stderr);
879 
880 		argv[0] = path;
881 		i = 1;
882 
883 		va_start(vp, path);
884 
885 		do {
886 			arg = va_arg(vp, const char *);
887 			argv[i] = arg;
888 		} while (arg != NULL &&
889 		    ++i != sizeof (argv) / sizeof (argv[0]));
890 
891 		va_end(vp);
892 
893 		(void) execve(path, (char * const *)argv, NULL);
894 		(void) fprintf(stderr, gettext("cannot execute %s: %s\n"),
895 		    path, strerror(errno));
896 		exit(-1);
897 	} else {
898 		if (waitpid(pid, &st, 0) == pid &&
899 		    !WIFSIGNALED(st) && WIFEXITED(st))
900 			st = WEXITSTATUS(st);
901 		else
902 			st = -1;
903 	}
904 	return (st);
905 }
906 
907 /*
908  * Invokes lumount for bename.
909  * At successfull completion returns zero and copies contents of bename
910  * into mountpoint[]
911  */
912 static int
913 fastboot_bename(const char *bename, char *mountpoint, size_t mpsz)
914 {
915 	int rc;
916 
917 	(void) halt_exec(LUUMOUNT_PROG, "-n", bename, NULL);
918 
919 	if ((rc = halt_exec(LUMOUNT_PROG, "-n", bename, FASTBOOT_MOUNTPOINT,
920 	    NULL)) != 0)
921 		(void) fprintf(stderr, gettext("%s: cannot mount BE %s\n"),
922 		    cmdname, bename);
923 	else
924 		(void) strlcpy(mountpoint, FASTBOOT_MOUNTPOINT, mpsz);
925 
926 	return (rc);
927 }
928 
929 /*
930  * Returns 0 on successful parsing of the arguments;
931  * retuens non-zero on failure.
932  */
933 static int
934 parse_fastboot_args(char *bootargs_buf, int *is_dryrun, const char *bename,
935     int *failsafe)
936 {
937 	char mountpoint[MAXPATHLEN];
938 	char bootargs_saved[BOOTARGS_MAX];
939 	char bootargs_scratch[BOOTARGS_MAX];
940 	char bootfs_arg[BOOTARGS_MAX];
941 	char unixfile[BOOTARGS_MAX];
942 	char *head, *newarg;
943 	int buflen;		/* length of the bootargs_buf */
944 	int mplen;		/* length of the mount point */
945 	int rootlen = 0;	/* length of the root argument */
946 	int unixlen = 0;	/* length of the unix argument */
947 	int off = 0;		/* offset into the new boot argument */
948 	int is_zfs = 0;
949 	int rc = 0;
950 
951 	bzero(mountpoint, sizeof (mountpoint));
952 
953 	/*
954 	 * If argc is not 0, buflen is length of the argument being passed in;
955 	 * else it is 0 as bootargs_buf has been initialized to all 0's.
956 	 */
957 	buflen = strlen(bootargs_buf);
958 
959 	/* Save a copy of the original argument */
960 	bcopy(bootargs_buf, bootargs_saved, buflen);
961 	bzero(&bootargs_saved[buflen], sizeof (bootargs_saved) - buflen);
962 
963 	/* Save another copy to be used by strtok */
964 	bcopy(bootargs_buf, bootargs_scratch, buflen);
965 	bzero(&bootargs_scratch[buflen], sizeof (bootargs_scratch) - buflen);
966 	head = &bootargs_scratch[0];
967 
968 	/* Zero out the boot argument buffer as we will reconstruct it */
969 	bzero(bootargs_buf, BOOTARGS_MAX);
970 	bzero(bootfs_arg, BOOTARGS_MAX);
971 	bzero(unixfile, sizeof (unixfile));
972 
973 	/* Get the first argument */
974 	newarg = strtok(bootargs_scratch, " ");
975 
976 	/*
977 	 * If this is a dry run request, verify that the drivers can handle
978 	 * fast reboot.
979 	 */
980 	if (newarg && strncasecmp(newarg, "dryrun", strlen("dryrun")) == 0) {
981 		*is_dryrun = 1;
982 		(void) system("/usr/sbin/devfsadm");
983 	}
984 
985 	/*
986 	 * Always perform a dry run to identify all the drivers that
987 	 * need to implement devo_reset().
988 	 */
989 	if (uadmin(A_SHUTDOWN, AD_FASTREBOOT_DRYRUN,
990 	    (uintptr_t)bootargs_saved) != 0) {
991 		(void) fprintf(stderr, gettext("%s: not all drivers "
992 		    "have implemented quiesce(9E)\n"), cmdname);
993 	} else if (*is_dryrun) {
994 		(void) fprintf(stderr, gettext("%s: all drivers have "
995 		    "implemented quiesce(9E)\n"), cmdname);
996 	}
997 
998 	/*
999 	 * Return if it is a true dry run.
1000 	 */
1001 	if (*is_dryrun)
1002 		return (rc);
1003 
1004 	if (bename && (rc = fastboot_bename(bename, mountpoint,
1005 	    sizeof (mountpoint))) != 0)
1006 		return (rc);
1007 
1008 	/*
1009 	 * If BE is not specified, look for disk argument to construct
1010 	 * mountpoint; if BE has been specified, mountpoint has already been
1011 	 * constructed.
1012 	 */
1013 	if (newarg && newarg[0] != '-' && !bename) {
1014 		int tmprc;
1015 
1016 		if ((tmprc = validate_disk(newarg, mountpoint)) == 0) {
1017 			/*
1018 			 * The first argument is a valid root argument.
1019 			 * Get the next argument.
1020 			 */
1021 			newarg = strtok(NULL, " ");
1022 			rootlen = (newarg) ? (newarg - head) : buflen;
1023 			(void) strlcpy(fastboot_mounted, mountpoint,
1024 			    sizeof (fastboot_mounted));
1025 
1026 		} else if (tmprc == -1) {
1027 			/*
1028 			 * Not a disk argument.  Use / as default root.
1029 			 */
1030 			bcopy("/", mountpoint, 1);
1031 			bzero(&mountpoint[1], sizeof (mountpoint) - 1);
1032 		} else {
1033 			/*
1034 			 * Disk argument, but not valid or not root.
1035 			 * Return failure.
1036 			 */
1037 			return (EINVAL);
1038 		}
1039 	}
1040 
1041 	/*
1042 	 * Make mountpoint the first part of unixfile.
1043 	 * If there is not disk argument, and BE has not been specified,
1044 	 * mountpoint could be empty.
1045 	 */
1046 	mplen = strlen(mountpoint);
1047 	bcopy(mountpoint, unixfile, mplen);
1048 
1049 	/*
1050 	 * Look for unix argument
1051 	 */
1052 	if (newarg && newarg[0] != '-') {
1053 		bcopy(newarg, &unixfile[mplen], strlen(newarg));
1054 		newarg = strtok(NULL, " ");
1055 		rootlen = (newarg) ? (newarg - head) : buflen;
1056 	} else if (mplen != 0) {
1057 		/*
1058 		 * No unix argument, but mountpoint is not empty, use
1059 		 * /platform/i86pc/$ISADIR/kernel/unix as default.
1060 		 */
1061 		char isa[20];
1062 
1063 		if (sysinfo(SI_ARCHITECTURE_64, isa, sizeof (isa)) != -1)
1064 			(void) snprintf(&unixfile[mplen],
1065 			    sizeof (unixfile) - mplen,
1066 			    "/platform/i86pc/kernel/%s/unix", isa);
1067 		else if (sysinfo(SI_ARCHITECTURE_32, isa, sizeof (isa)) != -1) {
1068 			(void) snprintf(&unixfile[mplen],
1069 			    sizeof (unixfile) - mplen,
1070 			    "/platform/i86pc/kernel/unix");
1071 		} else {
1072 			(void) fprintf(stderr,
1073 			    gettext("%s: unknown architecture"), cmdname);
1074 			return (EINVAL);
1075 		}
1076 	}
1077 
1078 	/*
1079 	 * We now have the complete unix argument.  Verify that it exists and
1080 	 * is an ELF file.  Split the argument up into mountpoint and unix
1081 	 * portions again.  This is necessary to handle cases where mountpoint
1082 	 * is specified on the command line as part of the unix argument,
1083 	 * such as this:
1084 	 *	# reboot -f /.alt/platform/i86pc/kernel/amd64/unix
1085 	 */
1086 	unixlen = strlen(unixfile);
1087 	if (unixlen > 0) {
1088 		if (validate_unix(unixfile, &mplen, &is_zfs,
1089 		    bootfs_arg, failsafe) != 0) {
1090 			/* Not a valid unix file */
1091 			return (EINVAL);
1092 		} else {
1093 			int space = 0;
1094 			/*
1095 			 * Construct boot argument.
1096 			 */
1097 			unixlen = strlen(unixfile);
1098 
1099 			/*
1100 			 * mdep cannot start with space because bootadm
1101 			 * creates bogus menu entries if it does.
1102 			 */
1103 			if (mplen > 0) {
1104 				bcopy(unixfile, bootargs_buf, mplen);
1105 				(void) strcat(bootargs_buf, " ");
1106 				space = 1;
1107 			}
1108 			bcopy(&unixfile[mplen], &bootargs_buf[mplen + space],
1109 			    unixlen - mplen);
1110 			(void) strcat(bootargs_buf, " ");
1111 			off += unixlen + space + 1;
1112 		}
1113 	} else {
1114 		/* Check to see if root is zfs */
1115 		const char	*dp;
1116 		(void) get_zfs_bootfs_arg("/", &dp, &is_zfs, bootfs_arg);
1117 	}
1118 
1119 	if (is_zfs && (buflen != 0 || bename != NULL))	{
1120 		/* LINTED E_SEC_SPRINTF_UNBOUNDED_COPY */
1121 		off += sprintf(bootargs_buf + off, "%s ", bootfs_arg);
1122 	}
1123 
1124 	/*
1125 	 * Copy the rest of the arguments
1126 	 */
1127 	bcopy(&bootargs_saved[rootlen], &bootargs_buf[off], buflen - rootlen);
1128 
1129 	return (rc);
1130 }
1131 
1132 int
1133 main(int argc, char *argv[])
1134 {
1135 	char *ttyn = ttyname(STDERR_FILENO);
1136 
1137 	uid_t	euid;
1138 	int qflag = 0, needlog = 1, nosync = 0;
1139 	int fast_reboot = 0;
1140 	uintptr_t mdep = NULL;
1141 	int cmd, fcn, c, aval, r;
1142 	const char *usage;
1143 	const char *optstring;
1144 	zoneid_t zoneid = getzoneid();
1145 	int need_check_zones = 0;
1146 	char bootargs_buf[BOOTARGS_MAX];
1147 	int failsafe = 0;
1148 	char *bename = NULL;
1149 
1150 	const char * const resetting = "/etc/svc/volatile/resetting";
1151 
1152 	(void) setlocale(LC_ALL, "");
1153 	(void) textdomain(TEXT_DOMAIN);
1154 
1155 	cmdname = basename(argv[0]);
1156 
1157 	if (strcmp(cmdname, "halt") == 0) {
1158 		(void) audit_halt_setup(argc, argv);
1159 		optstring = "dlnqy";
1160 		usage = gettext("usage: %s [ -dlnqy ]\n");
1161 		cmd = A_SHUTDOWN;
1162 		fcn = AD_HALT;
1163 	} else if (strcmp(cmdname, "poweroff") == 0) {
1164 		(void) audit_halt_setup(argc, argv);
1165 		optstring = "dlnqy";
1166 		usage = gettext("usage: %s [ -dlnqy ]\n");
1167 		cmd = A_SHUTDOWN;
1168 		fcn = AD_POWEROFF;
1169 	} else if (strcmp(cmdname, "reboot") == 0) {
1170 		(void) audit_reboot_setup();
1171 #if defined(__i386)
1172 		optstring = "dlnqfe:";
1173 		usage = gettext("usage: %s [ -dlnqfe: ] [ boot args ]\n");
1174 #else
1175 		optstring = "dlnq";
1176 		usage = gettext("usage: %s [ -dlnq ] [ boot args ]\n");
1177 #endif
1178 		cmd = A_SHUTDOWN;
1179 		fcn = AD_BOOT;
1180 	} else {
1181 		(void) fprintf(stderr,
1182 		    gettext("%s: not installed properly\n"), cmdname);
1183 		return (1);
1184 	}
1185 
1186 	while ((c = getopt(argc, argv, optstring)) != EOF) {
1187 		switch (c) {
1188 		case 'd':
1189 			if (zoneid == GLOBAL_ZONEID)
1190 				cmd = A_DUMP;
1191 			else {
1192 				(void) fprintf(stderr,
1193 				    gettext("%s: -d only valid from global"
1194 				    " zone\n"), cmdname);
1195 				return (1);
1196 			}
1197 			break;
1198 		case 'l':
1199 			needlog = 0;
1200 			break;
1201 		case 'n':
1202 			nosync = 1;
1203 			break;
1204 		case 'q':
1205 			qflag = 1;
1206 			break;
1207 		case 'y':
1208 			ttyn = NULL;
1209 			break;
1210 #if defined(__i386)
1211 		case 'f':
1212 			fast_reboot = 1;
1213 			break;
1214 		case 'e':
1215 			bename = optarg;
1216 			break;
1217 #endif
1218 		default:
1219 			/*
1220 			 * TRANSLATION_NOTE
1221 			 * Don't translate the words "halt" or "reboot"
1222 			 */
1223 			(void) fprintf(stderr, usage, cmdname);
1224 			return (1);
1225 		}
1226 	}
1227 
1228 	argc -= optind;
1229 	argv += optind;
1230 
1231 	if (argc != 0) {
1232 		if (fcn != AD_BOOT) {
1233 			(void) fprintf(stderr, usage, cmdname);
1234 			return (1);
1235 		}
1236 
1237 		/* Gather the arguments into bootargs_buf. */
1238 		if (gather_args(argv, bootargs_buf, sizeof (bootargs_buf)) !=
1239 		    0) {
1240 			(void) fprintf(stderr,
1241 			    gettext("%s: Boot arguments too long.\n"), cmdname);
1242 			return (1);
1243 		}
1244 
1245 		mdep = (uintptr_t)bootargs_buf;
1246 	} else {
1247 		/*
1248 		 * Initialize it to 0 in case of fastboot, the buffer
1249 		 * will be used.
1250 		 */
1251 		bzero(bootargs_buf, sizeof (bootargs_buf));
1252 	}
1253 
1254 	if ((euid = geteuid()) != 0) {
1255 		(void) fprintf(stderr,
1256 		    gettext("%s: permission denied\n"), cmdname);
1257 		goto fail;
1258 	}
1259 
1260 	/*
1261 	 * Check whether fast  reboot is the default operating mode
1262 	 */
1263 	if (fcn == AD_BOOT && !fast_reboot)
1264 		fast_reboot = is_fastboot_default(euid);
1265 
1266 	if (bename && !fast_reboot)	{
1267 		(void) fprintf(stderr, gettext("%s: -e only valid with -f\n"),
1268 		    cmdname);
1269 		return (EINVAL);
1270 	}
1271 
1272 
1273 	/*
1274 	 * If fast reboot, do some sanity check on the argument
1275 	 */
1276 	if (fast_reboot) {
1277 		int rc;
1278 		int is_dryrun = 0;
1279 
1280 		if (zoneid != GLOBAL_ZONEID)	{
1281 			(void) fprintf(stderr,
1282 			    gettext("%s: fast reboot only valid from global"
1283 			    " zone\n"), cmdname);
1284 			return (EINVAL);
1285 		}
1286 
1287 		rc = parse_fastboot_args(bootargs_buf, &is_dryrun,
1288 		    bename, &failsafe);
1289 
1290 		/*
1291 		 * If dry run, or if arguments are invalid, return.
1292 		 */
1293 		if (is_dryrun)
1294 			return (rc);
1295 		else if (rc != 0)
1296 			goto fail;
1297 
1298 		/*
1299 		 * For all the other errors, we continue on in case user
1300 		 * user want to force fast reboot.
1301 		 */
1302 		if (strlen(bootargs_buf) != 0)
1303 			mdep = (uintptr_t)bootargs_buf;
1304 	}
1305 
1306 #if 0	/* For debugging */
1307 	if (mdep != NULL)
1308 		(void) fprintf(stderr, "mdep = %s\n", (char *)mdep);
1309 #endif
1310 
1311 	if (fcn != AD_BOOT && ttyn != NULL &&
1312 	    strncmp(ttyn, "/dev/term/", strlen("/dev/term/")) == 0) {
1313 		/*
1314 		 * TRANSLATION_NOTE
1315 		 * Don't translate ``halt -y''
1316 		 */
1317 		(void) fprintf(stderr,
1318 		    gettext("%s: dangerous on a dialup;"), cmdname);
1319 		(void) fprintf(stderr,
1320 		    gettext("use ``%s -y'' if you are really sure\n"), cmdname);
1321 		goto fail;
1322 	}
1323 
1324 	if (needlog) {
1325 		char *user = getlogin();
1326 		struct passwd *pw;
1327 		char *tty;
1328 
1329 		openlog(cmdname, 0, LOG_AUTH);
1330 		if (user == NULL && (pw = getpwuid(getuid())) != NULL)
1331 			user = pw->pw_name;
1332 		if (user == NULL)
1333 			user = "root";
1334 
1335 		tty = ttyname(1);
1336 
1337 		if (tty == NULL)
1338 			syslog(LOG_CRIT, "initiated by %s", user);
1339 		else
1340 			syslog(LOG_CRIT, "initiated by %s on %s", user, tty);
1341 	}
1342 
1343 	/*
1344 	 * We must assume success and log it before auditd is terminated.
1345 	 */
1346 	if (fcn == AD_BOOT)
1347 		aval = audit_reboot_success();
1348 	else
1349 		aval = audit_halt_success();
1350 
1351 	if (aval == -1) {
1352 		(void) fprintf(stderr,
1353 		    gettext("%s: can't turn off auditd\n"), cmdname);
1354 		if (needlog)
1355 			(void) sleep(5); /* Give syslogd time to record this */
1356 	}
1357 
1358 	(void) signal(SIGHUP, SIG_IGN);	/* for remote connections */
1359 
1360 	/*
1361 	 * We start to fork a bunch of zoneadms to halt any active zones.
1362 	 * This will proceed with halt in parallel until we call
1363 	 * check_zone_haltedness later on.
1364 	 */
1365 	if (zoneid == GLOBAL_ZONEID && cmd != A_DUMP) {
1366 		need_check_zones = halt_zones();
1367 	}
1368 
1369 	/* if we're dumping, do the archive update here and don't defer it */
1370 
1371 	if (cmd == A_DUMP && zoneid == GLOBAL_ZONEID && !nosync) {
1372 		char *fast_argv[] = {"/sbin/bootadm", "-ea", "update_all",
1373 		    "fastboot", NULL};
1374 		char *b_argv[] = {"/sbin/bootadm", "-ea", "update_all", NULL};
1375 
1376 		if (fast_reboot)
1377 			r = posix_spawn(NULL, fast_argv[0], NULL, NULL,
1378 			    fast_argv, NULL);
1379 			else
1380 			r = posix_spawn(NULL, b_argv[0], NULL, NULL, b_argv,
1381 			    NULL);
1382 
1383 		/* if posix_spawn fails we emit a warning and continue */
1384 
1385 		if (r != 0)
1386 			(void) fprintf(stderr, gettext("%s: WARNING, unable to"
1387 			    " start boot archive update\n"), cmdname);
1388 		else
1389 			(void) wait(NULL);
1390 	}
1391 
1392 	/*
1393 	 * If we're not forcing a crash dump, mark the system as quiescing for
1394 	 * smf(5)'s benefit, and idle the init process.
1395 	 */
1396 	if (cmd != A_DUMP) {
1397 		if (direct_init(PCDSTOP) == -1) {
1398 			/*
1399 			 * TRANSLATION_NOTE
1400 			 * Don't translate the word "init"
1401 			 */
1402 			(void) fprintf(stderr,
1403 			    gettext("%s: can't idle init\n"), cmdname);
1404 			goto fail;
1405 		}
1406 
1407 		if (creat(resetting, 0755) == -1)
1408 			(void) fprintf(stderr,
1409 			    gettext("%s: could not create %s.\n"),
1410 			    cmdname, resetting);
1411 
1412 		/*
1413 		 * Stop all restarters so they do not try to restart services
1414 		 * that are terminated.
1415 		 */
1416 		stop_restarters();
1417 
1418 		/*
1419 		 * Wait a little while for zones to shutdown.
1420 		 */
1421 		if (need_check_zones) {
1422 			check_zones_haltedness();
1423 
1424 			(void) fprintf(stderr,
1425 			    gettext("%s: Completing system halt.\n"),
1426 			    cmdname);
1427 		}
1428 	}
1429 
1430 	/*
1431 	 * Make sure we don't get stopped by a jobcontrol shell
1432 	 * once we start killing everybody.
1433 	 */
1434 	(void) signal(SIGTSTP, SIG_IGN);
1435 	(void) signal(SIGTTIN, SIG_IGN);
1436 	(void) signal(SIGTTOU, SIG_IGN);
1437 	(void) signal(SIGPIPE, SIG_IGN);
1438 	(void) signal(SIGTERM, SIG_IGN);
1439 
1440 	/*
1441 	 * If we're not forcing a crash dump, give everyone 5 seconds to
1442 	 * handle a SIGTERM and clean up properly.
1443 	 */
1444 	if (cmd != A_DUMP) {
1445 		int	start, end, delta;
1446 
1447 		(void) kill(-1, SIGTERM);
1448 		start = time(NULL);
1449 
1450 		if (zoneid == GLOBAL_ZONEID && !nosync) {
1451 			char *fast_argv[] = {"/sbin/bootadm", "-ea",
1452 			    "update_all", "fastboot", NULL};
1453 			char *b_argv[] = {"/sbin/bootadm", "-ea", "update_all",
1454 			    NULL};
1455 
1456 			if (fast_reboot)
1457 				r = posix_spawn(NULL, fast_argv[0], NULL, NULL,
1458 				    fast_argv, NULL);
1459 			else
1460 				r = posix_spawn(NULL, b_argv[0], NULL, NULL,
1461 				    b_argv, NULL);
1462 
1463 			/*
1464 			 * if posix_spawn fails we emit a warning and
1465 			 * continue
1466 			 */
1467 
1468 			if (r != 0)
1469 				(void) fprintf(stderr, gettext("%s: WARNING, "
1470 				    "unable to start boot archive update\n"),
1471 				    cmdname);
1472 			else
1473 				(void) wait(NULL);
1474 		}
1475 
1476 		end = time(NULL);
1477 		delta = end - start;
1478 		if (delta < 5)
1479 			(void) sleep(5 - delta);
1480 	}
1481 
1482 	(void) signal(SIGINT, SIG_IGN);
1483 
1484 	if (!qflag && !nosync) {
1485 		struct utmpx wtmpx;
1486 
1487 		bzero(&wtmpx, sizeof (struct utmpx));
1488 		(void) strcpy(wtmpx.ut_line, "~");
1489 		(void) time(&wtmpx.ut_tv.tv_sec);
1490 
1491 		if (cmd == A_DUMP)
1492 			(void) strcpy(wtmpx.ut_name, "crash dump");
1493 		else
1494 			(void) strcpy(wtmpx.ut_name, "shutdown");
1495 
1496 		(void) updwtmpx(WTMPX_FILE, &wtmpx);
1497 		sync();
1498 	}
1499 
1500 	if (cmd == A_DUMP && nosync != 0)
1501 		(void) uadmin(A_DUMP, AD_NOSYNC, NULL);
1502 
1503 	if (fast_reboot) {
1504 		if (failsafe)
1505 			(void) fprintf(stderr, "Fast reboot - failsafe.\n");
1506 		else
1507 			(void) fprintf(stderr, "Fast reboot.\n");
1508 
1509 		fcn = AD_FASTREBOOT;
1510 	}
1511 
1512 	if (uadmin(cmd, fcn, mdep) == -1)
1513 		(void) fprintf(stderr, "%s: uadmin failed: %s\n",
1514 		    cmdname, strerror(errno));
1515 	else
1516 		(void) fprintf(stderr, "%s: uadmin unexpectedly returned 0\n",
1517 		    cmdname);
1518 
1519 	do {
1520 		r = remove(resetting);
1521 	} while (r != 0 && errno == EINTR);
1522 
1523 	if (r != 0 && errno != ENOENT)
1524 		(void) fprintf(stderr, gettext("%s: could not remove %s.\n"),
1525 		    cmdname, resetting);
1526 
1527 	if (direct_init(PCRUN) == -1) {
1528 		/*
1529 		 * TRANSLATION_NOTE
1530 		 * Don't translate the word "init"
1531 		 */
1532 		(void) fprintf(stderr,
1533 		    gettext("%s: can't resume init\n"), cmdname);
1534 	}
1535 
1536 	continue_restarters();
1537 
1538 	if (get_initpid() != -1)
1539 		/* tell init to restate current level */
1540 		(void) kill(get_initpid(), SIGHUP);
1541 
1542 fail:
1543 	if (fcn == AD_BOOT)
1544 		(void) audit_reboot_fail();
1545 	else
1546 		(void) audit_halt_fail();
1547 
1548 	if (fast_reboot) {
1549 		if (bename) {
1550 			(void) halt_exec(LUUMOUNT_PROG, "-n", bename, NULL);
1551 
1552 		} else if (strlen(fastboot_mounted) != 0) {
1553 			(void) umount(fastboot_mounted);
1554 		}
1555 	}
1556 
1557 	return (1);
1558 }
1559