xref: /titanic_41/usr/src/cmd/halt/halt.c (revision 82a2fc4751cef28c0bdc327d02012bf8796083b9)
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 #define	MAXARGS		5
1133 
1134 static void
1135 do_archives_update(int do_fast_reboot)
1136 {
1137 	int	r, i = 0;
1138 	pid_t	pid;
1139 	char	*cmd_argv[MAXARGS];
1140 
1141 
1142 	cmd_argv[i++] = "/sbin/bootadm";
1143 	cmd_argv[i++] = "-ea";
1144 	cmd_argv[i++] = "update_all";
1145 	if (do_fast_reboot)
1146 		cmd_argv[i++] = "fastboot";
1147 	cmd_argv[i] = NULL;
1148 
1149 	r = posix_spawn(&pid, cmd_argv[0], NULL, NULL, cmd_argv, NULL);
1150 
1151 	/* if posix_spawn fails we emit a warning and continue */
1152 
1153 	if (r != 0)
1154 		(void) fprintf(stderr, gettext("%s: WARNING, unable to start "
1155 		    "boot archive update\n"), cmdname);
1156 	else
1157 		while (waitpid(pid, NULL, 0) == -1 && errno == EINTR)
1158 			;
1159 }
1160 
1161 int
1162 main(int argc, char *argv[])
1163 {
1164 	char *ttyn = ttyname(STDERR_FILENO);
1165 
1166 	uid_t	euid;
1167 	int qflag = 0, needlog = 1, nosync = 0;
1168 	int fast_reboot = 0;
1169 	uintptr_t mdep = NULL;
1170 	int cmd, fcn, c, aval, r;
1171 	const char *usage;
1172 	const char *optstring;
1173 	zoneid_t zoneid = getzoneid();
1174 	int need_check_zones = 0;
1175 	char bootargs_buf[BOOTARGS_MAX];
1176 	int failsafe = 0;
1177 	char *bename = NULL;
1178 
1179 	const char * const resetting = "/etc/svc/volatile/resetting";
1180 
1181 	(void) setlocale(LC_ALL, "");
1182 	(void) textdomain(TEXT_DOMAIN);
1183 
1184 	cmdname = basename(argv[0]);
1185 
1186 	if (strcmp(cmdname, "halt") == 0) {
1187 		(void) audit_halt_setup(argc, argv);
1188 		optstring = "dlnqy";
1189 		usage = gettext("usage: %s [ -dlnqy ]\n");
1190 		cmd = A_SHUTDOWN;
1191 		fcn = AD_HALT;
1192 	} else if (strcmp(cmdname, "poweroff") == 0) {
1193 		(void) audit_halt_setup(argc, argv);
1194 		optstring = "dlnqy";
1195 		usage = gettext("usage: %s [ -dlnqy ]\n");
1196 		cmd = A_SHUTDOWN;
1197 		fcn = AD_POWEROFF;
1198 	} else if (strcmp(cmdname, "reboot") == 0) {
1199 		(void) audit_reboot_setup();
1200 #if defined(__i386)
1201 		optstring = "dlnqfe:";
1202 		usage = gettext("usage: %s [ -dlnqfe: ] [ boot args ]\n");
1203 #else
1204 		optstring = "dlnq";
1205 		usage = gettext("usage: %s [ -dlnq ] [ boot args ]\n");
1206 #endif
1207 		cmd = A_SHUTDOWN;
1208 		fcn = AD_BOOT;
1209 	} else {
1210 		(void) fprintf(stderr,
1211 		    gettext("%s: not installed properly\n"), cmdname);
1212 		return (1);
1213 	}
1214 
1215 	while ((c = getopt(argc, argv, optstring)) != EOF) {
1216 		switch (c) {
1217 		case 'd':
1218 			if (zoneid == GLOBAL_ZONEID)
1219 				cmd = A_DUMP;
1220 			else {
1221 				(void) fprintf(stderr,
1222 				    gettext("%s: -d only valid from global"
1223 				    " zone\n"), cmdname);
1224 				return (1);
1225 			}
1226 			break;
1227 		case 'l':
1228 			needlog = 0;
1229 			break;
1230 		case 'n':
1231 			nosync = 1;
1232 			break;
1233 		case 'q':
1234 			qflag = 1;
1235 			break;
1236 		case 'y':
1237 			ttyn = NULL;
1238 			break;
1239 #if defined(__i386)
1240 		case 'f':
1241 			fast_reboot = 1;
1242 			break;
1243 		case 'e':
1244 			bename = optarg;
1245 			break;
1246 #endif
1247 		default:
1248 			/*
1249 			 * TRANSLATION_NOTE
1250 			 * Don't translate the words "halt" or "reboot"
1251 			 */
1252 			(void) fprintf(stderr, usage, cmdname);
1253 			return (1);
1254 		}
1255 	}
1256 
1257 	argc -= optind;
1258 	argv += optind;
1259 
1260 	if (argc != 0) {
1261 		if (fcn != AD_BOOT) {
1262 			(void) fprintf(stderr, usage, cmdname);
1263 			return (1);
1264 		}
1265 
1266 		/* Gather the arguments into bootargs_buf. */
1267 		if (gather_args(argv, bootargs_buf, sizeof (bootargs_buf)) !=
1268 		    0) {
1269 			(void) fprintf(stderr,
1270 			    gettext("%s: Boot arguments too long.\n"), cmdname);
1271 			return (1);
1272 		}
1273 
1274 		mdep = (uintptr_t)bootargs_buf;
1275 	} else {
1276 		/*
1277 		 * Initialize it to 0 in case of fastboot, the buffer
1278 		 * will be used.
1279 		 */
1280 		bzero(bootargs_buf, sizeof (bootargs_buf));
1281 	}
1282 
1283 	if ((euid = geteuid()) != 0) {
1284 		(void) fprintf(stderr,
1285 		    gettext("%s: permission denied\n"), cmdname);
1286 		goto fail;
1287 	}
1288 
1289 	/*
1290 	 * Check whether fast  reboot is the default operating mode
1291 	 */
1292 	if (fcn == AD_BOOT && !fast_reboot)
1293 		fast_reboot = is_fastboot_default(euid);
1294 
1295 	if (bename && !fast_reboot)	{
1296 		(void) fprintf(stderr, gettext("%s: -e only valid with -f\n"),
1297 		    cmdname);
1298 		return (EINVAL);
1299 	}
1300 
1301 
1302 	/*
1303 	 * If fast reboot, do some sanity check on the argument
1304 	 */
1305 	if (fast_reboot) {
1306 		int rc;
1307 		int is_dryrun = 0;
1308 
1309 		if (zoneid != GLOBAL_ZONEID)	{
1310 			(void) fprintf(stderr,
1311 			    gettext("%s: fast reboot only valid from global"
1312 			    " zone\n"), cmdname);
1313 			return (EINVAL);
1314 		}
1315 
1316 		rc = parse_fastboot_args(bootargs_buf, &is_dryrun,
1317 		    bename, &failsafe);
1318 
1319 		/*
1320 		 * If dry run, or if arguments are invalid, return.
1321 		 */
1322 		if (is_dryrun)
1323 			return (rc);
1324 		else if (rc != 0)
1325 			goto fail;
1326 
1327 		/*
1328 		 * For all the other errors, we continue on in case user
1329 		 * user want to force fast reboot.
1330 		 */
1331 		if (strlen(bootargs_buf) != 0)
1332 			mdep = (uintptr_t)bootargs_buf;
1333 	}
1334 
1335 #if 0	/* For debugging */
1336 	if (mdep != NULL)
1337 		(void) fprintf(stderr, "mdep = %s\n", (char *)mdep);
1338 #endif
1339 
1340 	if (fcn != AD_BOOT && ttyn != NULL &&
1341 	    strncmp(ttyn, "/dev/term/", strlen("/dev/term/")) == 0) {
1342 		/*
1343 		 * TRANSLATION_NOTE
1344 		 * Don't translate ``halt -y''
1345 		 */
1346 		(void) fprintf(stderr,
1347 		    gettext("%s: dangerous on a dialup;"), cmdname);
1348 		(void) fprintf(stderr,
1349 		    gettext("use ``%s -y'' if you are really sure\n"), cmdname);
1350 		goto fail;
1351 	}
1352 
1353 	if (needlog) {
1354 		char *user = getlogin();
1355 		struct passwd *pw;
1356 		char *tty;
1357 
1358 		openlog(cmdname, 0, LOG_AUTH);
1359 		if (user == NULL && (pw = getpwuid(getuid())) != NULL)
1360 			user = pw->pw_name;
1361 		if (user == NULL)
1362 			user = "root";
1363 
1364 		tty = ttyname(1);
1365 
1366 		if (tty == NULL)
1367 			syslog(LOG_CRIT, "initiated by %s", user);
1368 		else
1369 			syslog(LOG_CRIT, "initiated by %s on %s", user, tty);
1370 	}
1371 
1372 	/*
1373 	 * We must assume success and log it before auditd is terminated.
1374 	 */
1375 	if (fcn == AD_BOOT)
1376 		aval = audit_reboot_success();
1377 	else
1378 		aval = audit_halt_success();
1379 
1380 	if (aval == -1) {
1381 		(void) fprintf(stderr,
1382 		    gettext("%s: can't turn off auditd\n"), cmdname);
1383 		if (needlog)
1384 			(void) sleep(5); /* Give syslogd time to record this */
1385 	}
1386 
1387 	(void) signal(SIGHUP, SIG_IGN);	/* for remote connections */
1388 
1389 	/*
1390 	 * We start to fork a bunch of zoneadms to halt any active zones.
1391 	 * This will proceed with halt in parallel until we call
1392 	 * check_zone_haltedness later on.
1393 	 */
1394 	if (zoneid == GLOBAL_ZONEID && cmd != A_DUMP) {
1395 		need_check_zones = halt_zones();
1396 	}
1397 
1398 	/* if we're dumping, do the archive update here and don't defer it */
1399 
1400 	if (cmd == A_DUMP && zoneid == GLOBAL_ZONEID && !nosync)
1401 		do_archives_update(fast_reboot);
1402 
1403 	/*
1404 	 * If we're not forcing a crash dump, mark the system as quiescing for
1405 	 * smf(5)'s benefit, and idle the init process.
1406 	 */
1407 	if (cmd != A_DUMP) {
1408 		if (direct_init(PCDSTOP) == -1) {
1409 			/*
1410 			 * TRANSLATION_NOTE
1411 			 * Don't translate the word "init"
1412 			 */
1413 			(void) fprintf(stderr,
1414 			    gettext("%s: can't idle init\n"), cmdname);
1415 			goto fail;
1416 		}
1417 
1418 		if (creat(resetting, 0755) == -1)
1419 			(void) fprintf(stderr,
1420 			    gettext("%s: could not create %s.\n"),
1421 			    cmdname, resetting);
1422 
1423 		/*
1424 		 * Stop all restarters so they do not try to restart services
1425 		 * that are terminated.
1426 		 */
1427 		stop_restarters();
1428 
1429 		/*
1430 		 * Wait a little while for zones to shutdown.
1431 		 */
1432 		if (need_check_zones) {
1433 			check_zones_haltedness();
1434 
1435 			(void) fprintf(stderr,
1436 			    gettext("%s: Completing system halt.\n"),
1437 			    cmdname);
1438 		}
1439 	}
1440 
1441 	/*
1442 	 * Make sure we don't get stopped by a jobcontrol shell
1443 	 * once we start killing everybody.
1444 	 */
1445 	(void) signal(SIGTSTP, SIG_IGN);
1446 	(void) signal(SIGTTIN, SIG_IGN);
1447 	(void) signal(SIGTTOU, SIG_IGN);
1448 	(void) signal(SIGPIPE, SIG_IGN);
1449 	(void) signal(SIGTERM, SIG_IGN);
1450 
1451 	/*
1452 	 * If we're not forcing a crash dump, give everyone 5 seconds to
1453 	 * handle a SIGTERM and clean up properly.
1454 	 */
1455 	if (cmd != A_DUMP) {
1456 		int	start, end, delta;
1457 
1458 		(void) kill(-1, SIGTERM);
1459 		start = time(NULL);
1460 
1461 		if (zoneid == GLOBAL_ZONEID && !nosync)
1462 			do_archives_update(fast_reboot);
1463 
1464 		end = time(NULL);
1465 		delta = end - start;
1466 		if (delta < 5)
1467 			(void) sleep(5 - delta);
1468 	}
1469 
1470 	(void) signal(SIGINT, SIG_IGN);
1471 
1472 	if (!qflag && !nosync) {
1473 		struct utmpx wtmpx;
1474 
1475 		bzero(&wtmpx, sizeof (struct utmpx));
1476 		(void) strcpy(wtmpx.ut_line, "~");
1477 		(void) time(&wtmpx.ut_tv.tv_sec);
1478 
1479 		if (cmd == A_DUMP)
1480 			(void) strcpy(wtmpx.ut_name, "crash dump");
1481 		else
1482 			(void) strcpy(wtmpx.ut_name, "shutdown");
1483 
1484 		(void) updwtmpx(WTMPX_FILE, &wtmpx);
1485 		sync();
1486 	}
1487 
1488 	if (cmd == A_DUMP && nosync != 0)
1489 		(void) uadmin(A_DUMP, AD_NOSYNC, NULL);
1490 
1491 	if (fast_reboot) {
1492 		if (failsafe)
1493 			(void) fprintf(stderr, "Fast reboot - failsafe.\n");
1494 		else
1495 			(void) fprintf(stderr, "Fast reboot.\n");
1496 
1497 		fcn = AD_FASTREBOOT;
1498 	}
1499 
1500 	if (uadmin(cmd, fcn, mdep) == -1)
1501 		(void) fprintf(stderr, "%s: uadmin failed: %s\n",
1502 		    cmdname, strerror(errno));
1503 	else
1504 		(void) fprintf(stderr, "%s: uadmin unexpectedly returned 0\n",
1505 		    cmdname);
1506 
1507 	do {
1508 		r = remove(resetting);
1509 	} while (r != 0 && errno == EINTR);
1510 
1511 	if (r != 0 && errno != ENOENT)
1512 		(void) fprintf(stderr, gettext("%s: could not remove %s.\n"),
1513 		    cmdname, resetting);
1514 
1515 	if (direct_init(PCRUN) == -1) {
1516 		/*
1517 		 * TRANSLATION_NOTE
1518 		 * Don't translate the word "init"
1519 		 */
1520 		(void) fprintf(stderr,
1521 		    gettext("%s: can't resume init\n"), cmdname);
1522 	}
1523 
1524 	continue_restarters();
1525 
1526 	if (get_initpid() != -1)
1527 		/* tell init to restate current level */
1528 		(void) kill(get_initpid(), SIGHUP);
1529 
1530 fail:
1531 	if (fcn == AD_BOOT)
1532 		(void) audit_reboot_fail();
1533 	else
1534 		(void) audit_halt_fail();
1535 
1536 	if (fast_reboot) {
1537 		if (bename) {
1538 			(void) halt_exec(LUUMOUNT_PROG, "-n", bename, NULL);
1539 
1540 		} else if (strlen(fastboot_mounted) != 0) {
1541 			(void) umount(fastboot_mounted);
1542 		}
1543 	}
1544 
1545 	return (1);
1546 }
1547