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