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