Move everything from /var/adm to /var/log
[unleashed/lotheac.git] / usr / src / cmd / halt / halt.c
blob8b7cfd111fdbb48e73e34631c78fd72f68748185
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright 2016 Toomas Soome <tsoome@me.com>
23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
28 * Copyright (c) 2015 by Delphix. All rights reserved.
31 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
32 /* All Rights Reserved */
35 * University Copyright- Copyright (c) 1982, 1986, 1988
36 * The Regents of the University of California
37 * All Rights Reserved
39 * University Acknowledgment- Portions of this document are derived from
40 * software developed by the University of California, Berkeley, and its
41 * contributors.
42 * Portions contributed by Juergen Keil, <jk@tools.de>.
47 * Common code for halt(8), poweroff(8), and reboot(8). We use
48 * argv[0] to determine which behavior to exhibit.
51 #include <stdio.h>
52 #include <procfs.h>
53 #include <sys/types.h>
54 #include <sys/elf.h>
55 #include <sys/systeminfo.h>
56 #include <sys/stat.h>
57 #include <sys/uadmin.h>
58 #include <sys/mntent.h>
59 #include <sys/mnttab.h>
60 #include <sys/mount.h>
61 #include <sys/fs/ufs_mount.h>
62 #include <alloca.h>
63 #include <assert.h>
64 #include <errno.h>
65 #include <fcntl.h>
66 #include <libgen.h>
67 #include <libscf.h>
68 #include <libscf_priv.h>
69 #include <limits.h>
70 #include <locale.h>
71 #include <libintl.h>
72 #include <syslog.h>
73 #include <signal.h>
74 #include <strings.h>
75 #include <unistd.h>
76 #include <stdlib.h>
77 #include <stdio.h>
78 #include <strings.h>
79 #include <time.h>
80 #include <wait.h>
81 #include <ctype.h>
82 #include <utmpx.h>
83 #include <pwd.h>
84 #include <zone.h>
85 #include <spawn.h>
87 #include <libzfs.h>
88 #if defined(__x86)
89 #include <libbe.h>
90 #endif
92 #if !defined(TEXT_DOMAIN)
93 #define TEXT_DOMAIN "SYS_TEST"
94 #endif
96 #if defined(__sparc)
97 #define CUR_ELFDATA ELFDATA2MSB
98 #elif defined(__x86)
99 #define CUR_ELFDATA ELFDATA2LSB
100 #endif
102 static libzfs_handle_t *g_zfs;
104 static char *cmdname; /* basename(argv[0]), the name of the command */
106 typedef struct ctidlist_struct {
107 ctid_t ctid;
108 struct ctidlist_struct *next;
109 } ctidlist_t;
111 static ctidlist_t *ctidlist = NULL;
112 static ctid_t startdct = -1;
114 #define FMRI_STARTD_CONTRACT \
115 "svc:/system/svc/restarter:default/:properties/restarter/contract"
117 #define BEADM_PROG "/usr/sbin/beadm"
118 #define BOOTADM_PROG "/sbin/bootadm"
119 #define ZONEADM_PROG "/usr/sbin/zoneadm"
122 * The length of FASTBOOT_MOUNTPOINT must be less than MAXPATHLEN.
124 #define FASTBOOT_MOUNTPOINT "/tmp/.fastboot.root"
127 * Fast Reboot related variables
129 static char fastboot_mounted[MAXPATHLEN];
131 #if defined(__x86)
132 static char *fbarg;
133 static char *fbarg_used;
134 static int fbarg_entnum = BE_ENTRY_DEFAULT;
135 #endif /* __x86 */
137 static int validate_ufs_disk(char *, char *);
138 static int validate_zfs_pool(char *, char *);
140 static pid_t
141 get_initpid()
143 static int init_pid = -1;
145 if (init_pid == -1) {
146 if (zone_getattr(getzoneid(), ZONE_ATTR_INITPID, &init_pid,
147 sizeof (init_pid)) != sizeof (init_pid)) {
148 assert(errno == ESRCH);
149 init_pid = -1;
152 return (init_pid);
156 * Quiesce or resume init using /proc. When stopping init, we can't send
157 * SIGTSTP (since init ignores it) or SIGSTOP (since the kernel won't permit
158 * it).
160 static int
161 direct_init(long command)
163 char ctlfile[MAXPATHLEN];
164 pid_t pid;
165 int ctlfd;
167 assert(command == PCDSTOP || command == PCRUN);
168 if ((pid = get_initpid()) == -1) {
169 return (-1);
172 (void) snprintf(ctlfile, sizeof (ctlfile), "/proc/%d/ctl", pid);
173 if ((ctlfd = open(ctlfile, O_WRONLY)) == -1)
174 return (-1);
176 if (command == PCDSTOP) {
177 if (write(ctlfd, &command, sizeof (long)) == -1) {
178 (void) close(ctlfd);
179 return (-1);
181 } else { /* command == PCRUN */
182 long cmds[2];
183 cmds[0] = command;
184 cmds[1] = 0;
185 if (write(ctlfd, cmds, sizeof (cmds)) == -1) {
186 (void) close(ctlfd);
187 return (-1);
190 (void) close(ctlfd);
191 return (0);
194 static void
195 stop_startd()
197 scf_handle_t *h;
198 scf_property_t *prop = NULL;
199 scf_value_t *val = NULL;
200 uint64_t uint64;
202 if ((h = scf_handle_create(SCF_VERSION)) == NULL)
203 return;
205 if ((scf_handle_bind(h) != 0) ||
206 ((prop = scf_property_create(h)) == NULL) ||
207 ((val = scf_value_create(h)) == NULL))
208 goto out;
210 if (scf_handle_decode_fmri(h, FMRI_STARTD_CONTRACT,
211 NULL, NULL, NULL, NULL, prop, SCF_DECODE_FMRI_EXACT) != 0)
212 goto out;
214 if (scf_property_is_type(prop, SCF_TYPE_COUNT) != 0 ||
215 scf_property_get_value(prop, val) != 0 ||
216 scf_value_get_count(val, &uint64) != 0)
217 goto out;
219 startdct = (ctid_t)uint64;
220 (void) sigsend(P_CTID, startdct, SIGSTOP);
222 out:
223 scf_property_destroy(prop);
224 scf_value_destroy(val);
225 scf_handle_destroy(h);
228 static void
229 continue_startd()
231 if (startdct != -1)
232 (void) sigsend(P_CTID, startdct, SIGCONT);
235 #define FMRI_RESTARTER_PROP "/:properties/general/restarter"
236 #define FMRI_CONTRACT_PROP "/:properties/restarter/contract"
238 static int
239 save_ctid(ctid_t ctid)
241 ctidlist_t *next;
243 for (next = ctidlist; next != NULL; next = next->next)
244 if (next->ctid == ctid)
245 return (-1);
247 next = (ctidlist_t *)malloc(sizeof (ctidlist_t));
248 if (next == NULL)
249 return (-1);
251 next->ctid = ctid;
252 next->next = ctidlist;
253 ctidlist = next;
254 return (0);
257 static void
258 stop_delegates()
260 ctid_t ctid;
261 scf_handle_t *h;
262 scf_scope_t *sc = NULL;
263 scf_service_t *svc = NULL;
264 scf_instance_t *inst = NULL;
265 scf_snapshot_t *snap = NULL;
266 scf_snapshot_t *isnap = NULL;
267 scf_propertygroup_t *pg = NULL;
268 scf_property_t *prop = NULL;
269 scf_value_t *val = NULL;
270 scf_iter_t *siter = NULL;
271 scf_iter_t *iiter = NULL;
272 char *fmri;
273 ssize_t length;
275 uint64_t uint64;
276 ssize_t bytes;
278 length = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH);
279 if (length <= 0)
280 return;
282 length++;
283 fmri = alloca(length * sizeof (char));
285 if ((h = scf_handle_create(SCF_VERSION)) == NULL)
286 return;
288 if (scf_handle_bind(h) != 0) {
289 scf_handle_destroy(h);
290 return;
293 if ((sc = scf_scope_create(h)) == NULL ||
294 (svc = scf_service_create(h)) == NULL ||
295 (inst = scf_instance_create(h)) == NULL ||
296 (snap = scf_snapshot_create(h)) == NULL ||
297 (pg = scf_pg_create(h)) == NULL ||
298 (prop = scf_property_create(h)) == NULL ||
299 (val = scf_value_create(h)) == NULL ||
300 (siter = scf_iter_create(h)) == NULL ||
301 (iiter = scf_iter_create(h)) == NULL)
302 goto out;
304 if (scf_handle_get_scope(h, SCF_SCOPE_LOCAL, sc) != 0)
305 goto out;
307 if (scf_iter_scope_services(siter, sc) != 0)
308 goto out;
310 while (scf_iter_next_service(siter, svc) == 1) {
312 if (scf_iter_service_instances(iiter, svc) != 0)
313 continue;
315 while (scf_iter_next_instance(iiter, inst) == 1) {
317 if ((scf_instance_get_snapshot(inst, "running",
318 snap)) != 0)
319 isnap = NULL;
320 else
321 isnap = snap;
323 if (scf_instance_get_pg_composed(inst, isnap,
324 SCF_PG_GENERAL, pg) != 0)
325 continue;
327 if (scf_pg_get_property(pg, SCF_PROPERTY_RESTARTER,
328 prop) != 0 ||
329 scf_property_get_value(prop, val) != 0)
330 continue;
332 bytes = scf_value_get_astring(val, fmri, length);
333 if (bytes <= 0 || bytes >= length)
334 continue;
336 if (strlcat(fmri, FMRI_CONTRACT_PROP, length) >=
337 length)
338 continue;
340 if (scf_handle_decode_fmri(h, fmri, NULL, NULL,
341 NULL, NULL, prop, SCF_DECODE_FMRI_EXACT) != 0)
342 continue;
344 if (scf_property_is_type(prop, SCF_TYPE_COUNT) != 0 ||
345 scf_property_get_value(prop, val) != 0 ||
346 scf_value_get_count(val, &uint64) != 0)
347 continue;
349 ctid = (ctid_t)uint64;
350 if (save_ctid(ctid) == 0) {
351 (void) sigsend(P_CTID, ctid, SIGSTOP);
355 out:
356 scf_scope_destroy(sc);
357 scf_service_destroy(svc);
358 scf_instance_destroy(inst);
359 scf_snapshot_destroy(snap);
360 scf_pg_destroy(pg);
361 scf_property_destroy(prop);
362 scf_value_destroy(val);
363 scf_iter_destroy(siter);
364 scf_iter_destroy(iiter);
366 (void) scf_handle_unbind(h);
367 scf_handle_destroy(h);
370 static void
371 continue_delegates()
373 ctidlist_t *next;
374 for (next = ctidlist; next != NULL; next = next->next)
375 (void) sigsend(P_CTID, next->ctid, SIGCONT);
378 #define FMRI_GDM "svc:/application/graphical-login/gdm:default"
379 #define GDM_STOP_TIMEOUT 10 /* Give gdm 10 seconds to shut down */
382 * If gdm is running, try to stop gdm.
383 * Returns 0 on success, -1 on failure.
385 static int
386 stop_gdm()
388 char *gdm_state = NULL;
389 int retry = 0;
392 * If gdm is running, try to stop gdm.
394 while ((gdm_state = smf_get_state(FMRI_GDM)) != NULL &&
395 strcmp(gdm_state, SCF_STATE_STRING_ONLINE) == 0 &&
396 retry++ < GDM_STOP_TIMEOUT) {
398 free(gdm_state);
401 * Only need to disable once.
403 if (retry == 1 &&
404 smf_disable_instance(FMRI_GDM, SMF_TEMPORARY) != 0) {
405 (void) fprintf(stderr,
406 gettext("%s: Failed to stop %s: %s.\n"),
407 cmdname, FMRI_GDM, scf_strerror(scf_error()));
408 return (-1);
410 (void) sleep(1);
413 if (retry >= GDM_STOP_TIMEOUT) {
414 (void) fprintf(stderr, gettext("%s: Failed to stop %s.\n"),
415 cmdname, FMRI_GDM);
416 return (-1);
419 return (0);
423 static void
424 stop_restarters()
426 stop_startd();
427 stop_delegates();
430 static void
431 continue_restarters()
433 continue_startd();
434 continue_delegates();
438 * Copy an array of strings into buf, separated by spaces. Returns 0 on
439 * success.
441 static int
442 gather_args(char **args, char *buf, size_t buf_sz)
444 if (strlcpy(buf, *args, buf_sz) >= buf_sz)
445 return (-1);
447 for (++args; *args != NULL; ++args) {
448 if (strlcat(buf, " ", buf_sz) >= buf_sz)
449 return (-1);
450 if (strlcat(buf, *args, buf_sz) >= buf_sz)
451 return (-1);
454 return (0);
458 * Halt every zone on the system. We are committed to doing a shutdown
459 * even if something goes wrong here. If something goes wrong, we just
460 * continue with the shutdown. Return non-zero if we need to wait for zones to
461 * halt later on.
463 static int
464 halt_zones()
466 pid_t pid;
467 zoneid_t *zones;
468 size_t nz = 0, old_nz;
469 int i;
470 char zname[ZONENAME_MAX];
473 * Get a list of zones. If the number of zones changes in between the
474 * two zone_list calls, try again.
477 for (;;) {
478 (void) zone_list(NULL, &nz);
479 if (nz == 1)
480 return (0);
481 old_nz = nz;
482 zones = calloc(sizeof (zoneid_t), nz);
483 if (zones == NULL) {
484 (void) fprintf(stderr,
485 gettext("%s: Could not halt zones"
486 " (out of memory).\n"), cmdname);
487 return (0);
490 (void) zone_list(zones, &nz);
491 if (old_nz == nz)
492 break;
493 free(zones);
496 if (nz == 2) {
497 (void) fprintf(stderr, gettext("%s: Halting 1 zone.\n"),
498 cmdname);
499 } else {
500 (void) fprintf(stderr, gettext("%s: Halting %i zones.\n"),
501 cmdname, nz - 1);
504 for (i = 0; i < nz; i++) {
505 if (zones[i] == GLOBAL_ZONEID)
506 continue;
507 if (getzonenamebyid(zones[i], zname, sizeof (zname)) < 0) {
509 * getzonenamebyid should only fail if we raced with
510 * another process trying to shut down the zone.
511 * We assume this happened and ignore the error.
513 if (errno != EINVAL) {
514 (void) fprintf(stderr,
515 gettext("%s: Unexpected error while "
516 "looking up zone %ul: %s.\n"),
517 cmdname, zones[i], strerror(errno));
520 continue;
522 pid = fork();
523 if (pid < 0) {
524 (void) fprintf(stderr,
525 gettext("%s: Zone \"%s\" could not be"
526 " halted (could not fork(): %s).\n"),
527 cmdname, zname, strerror(errno));
528 continue;
530 if (pid == 0) {
531 (void) execl(ZONEADM_PROG, ZONEADM_PROG,
532 "-z", zname, "halt", NULL);
533 (void) fprintf(stderr,
534 gettext("%s: Zone \"%s\" could not be halted"
535 " (cannot exec(" ZONEADM_PROG "): %s).\n"),
536 cmdname, zname, strerror(errno));
537 exit(0);
541 return (1);
545 * This function tries to wait for all non-global zones to go away.
546 * It will timeout if no progress is made for 5 seconds, or a total of
547 * 30 seconds elapses.
550 static void
551 check_zones_haltedness()
553 int t = 0, t_prog = 0;
554 size_t nz = 0, last_nz;
556 do {
557 last_nz = nz;
558 (void) zone_list(NULL, &nz);
559 if (nz == 1)
560 return;
562 (void) sleep(1);
564 if (last_nz > nz)
565 t_prog = 0;
567 t++;
568 t_prog++;
570 if (t == 10) {
571 if (nz == 2) {
572 (void) fprintf(stderr,
573 gettext("%s: Still waiting for 1 zone to "
574 "halt. Will wait up to 20 seconds.\n"),
575 cmdname);
576 } else {
577 (void) fprintf(stderr,
578 gettext("%s: Still waiting for %i zones "
579 "to halt. Will wait up to 20 seconds.\n"),
580 cmdname, nz - 1);
584 } while ((t < 30) && (t_prog < 5));
589 * Validate that this is a root disk or dataset
590 * Returns 0 if it is a root disk or dataset;
591 * returns 1 if it is a disk argument or dataset, but not valid or not root;
592 * returns -1 if it is not a valid argument or a disk argument.
594 static int
595 validate_disk(char *arg, char *mountpoint)
597 static char root_dev_path[] = "/dev/dsk";
598 char kernpath[MAXPATHLEN];
599 struct stat statbuf;
600 int rc = 0;
602 if (strlen(arg) > MAXPATHLEN) {
603 (void) fprintf(stderr,
604 gettext("%s: Argument is too long\n"), cmdname);
605 return (-1);
608 bcopy(FASTBOOT_MOUNTPOINT, mountpoint, sizeof (FASTBOOT_MOUNTPOINT));
610 if (strstr(arg, mountpoint) == NULL) {
612 * Do a force umount just in case some other filesystem has
613 * been mounted there.
615 (void) umount2(mountpoint, MS_FORCE);
618 /* Create the directory if it doesn't already exist */
619 if (lstat(mountpoint, &statbuf) != 0) {
620 if (mkdirp(mountpoint, 0755) != 0) {
621 (void) fprintf(stderr,
622 gettext("Failed to create mountpoint %s\n"),
623 mountpoint);
624 return (-1);
628 if (strncmp(arg, root_dev_path, strlen(root_dev_path)) == 0) {
629 /* ufs root disk argument */
630 rc = validate_ufs_disk(arg, mountpoint);
631 } else {
632 /* zfs root pool argument */
633 rc = validate_zfs_pool(arg, mountpoint);
636 if (rc != 0)
637 return (rc);
639 (void) snprintf(kernpath, MAXPATHLEN, "%s/platform/kernel/unix",
640 mountpoint);
642 if (stat(kernpath, &statbuf) != 0) {
643 (void) fprintf(stderr,
644 gettext("%s: %s is not a root disk or dataset\n"),
645 cmdname, arg);
646 return (1);
649 return (0);
653 static int
654 validate_ufs_disk(char *arg, char *mountpoint)
656 struct ufs_args ufs_args = { 0 };
657 char mntopts[MNT_LINE_MAX] = MNTOPT_LARGEFILES;
659 /* perform the mount */
660 ufs_args.flags = UFSMNT_LARGEFILES;
661 if (mount(arg, mountpoint, MS_DATA|MS_OPTIONSTR,
662 MNTTYPE_UFS, &ufs_args, sizeof (ufs_args),
663 mntopts, sizeof (mntopts)) != 0) {
664 perror(cmdname);
665 (void) fprintf(stderr,
666 gettext("%s: Failed to mount %s\n"), cmdname, arg);
667 return (-1);
670 return (0);
673 static int
674 validate_zfs_pool(char *arg, char *mountpoint)
676 zfs_handle_t *zhp = NULL;
677 char mntopts[MNT_LINE_MAX] = { '\0' };
678 int rc = 0;
680 if ((g_zfs = libzfs_init()) == NULL) {
681 (void) fprintf(stderr, gettext("Internal error: failed to "
682 "initialize ZFS library\n"));
683 return (-1);
686 /* Try to open the dataset */
687 if ((zhp = zfs_open(g_zfs, arg,
688 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_DATASET)) == NULL)
689 return (-1);
691 /* perform the mount */
692 if (mount(zfs_get_name(zhp), mountpoint, MS_DATA|MS_OPTIONSTR|MS_RDONLY,
693 MNTTYPE_ZFS, NULL, 0, mntopts, sizeof (mntopts)) != 0) {
694 perror(cmdname);
695 (void) fprintf(stderr,
696 gettext("%s: Failed to mount %s\n"), cmdname, arg);
697 rc = -1;
700 validate_zfs_err_out:
701 if (zhp != NULL)
702 zfs_close(zhp);
704 libzfs_fini(g_zfs);
705 return (rc);
709 * Return 0 if not zfs, or is zfs and have successfully constructed the
710 * boot argument; returns non-zero otherwise.
711 * At successful completion fpth contains pointer where mount point ends.
712 * NOTE: arg is supposed to be the resolved path
714 static int
715 get_zfs_bootfs_arg(const char *arg, const char ** fpth, int *is_zfs,
716 char *bootfs_arg)
718 zfs_handle_t *zhp = NULL;
719 zpool_handle_t *zpoolp = NULL;
720 FILE *mtabp = NULL;
721 struct mnttab mnt;
722 char *poolname = NULL;
723 char physpath[MAXPATHLEN];
724 char mntsp[ZFS_MAX_DATASET_NAME_LEN];
725 char bootfs[ZFS_MAX_DATASET_NAME_LEN];
726 int rc = 0;
727 size_t mntlen = 0;
728 size_t msz;
729 static char fmt[] = "-B zfs-bootfs=%s,bootpath=\"%s\"";
731 *fpth = arg;
732 *is_zfs = 0;
734 bzero(physpath, sizeof (physpath));
735 bzero(bootfs, sizeof (bootfs));
737 if ((mtabp = fopen(MNTTAB, "r")) == NULL) {
738 return (-1);
741 while (getmntent(mtabp, &mnt) == 0) {
742 if (strstr(arg, mnt.mnt_mountp) == arg &&
743 (msz = strlen(mnt.mnt_mountp)) > mntlen) {
744 mntlen = msz;
745 *is_zfs = strcmp(MNTTYPE_ZFS, mnt.mnt_fstype) == 0;
746 (void) strlcpy(mntsp, mnt.mnt_special, sizeof (mntsp));
750 (void) fclose(mtabp);
752 if (mntlen > 1)
753 *fpth += mntlen;
755 if (!*is_zfs)
756 return (0);
758 if ((g_zfs = libzfs_init()) == NULL)
759 return (-1);
761 /* Try to open the dataset */
762 if ((zhp = zfs_open(g_zfs, mntsp,
763 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_DATASET)) == NULL) {
764 (void) fprintf(stderr, gettext("Cannot open %s\n"), mntsp);
765 rc = -1;
766 goto validate_zfs_err_out;
769 (void) strlcpy(bootfs, mntsp, sizeof (bootfs));
771 if ((poolname = strtok(mntsp, "/")) == NULL) {
772 rc = -1;
773 goto validate_zfs_err_out;
776 if ((zpoolp = zpool_open(g_zfs, poolname)) == NULL) {
777 (void) fprintf(stderr, gettext("Cannot open %s\n"), poolname);
778 rc = -1;
779 goto validate_zfs_err_out;
782 if (zpool_get_physpath(zpoolp, physpath, sizeof (physpath)) != 0) {
783 (void) fprintf(stderr, gettext("Cannot find phys_path\n"));
784 rc = -1;
785 goto validate_zfs_err_out;
789 * For the mirror physpath would contain the list of all
790 * bootable devices, pick up the first one.
792 (void) strtok(physpath, " ");
793 if (snprintf(bootfs_arg, BOOTARGS_MAX, fmt, bootfs, physpath) >=
794 BOOTARGS_MAX) {
795 rc = E2BIG;
796 (void) fprintf(stderr,
797 gettext("Boot arguments are too long\n"));
800 validate_zfs_err_out:
801 if (zhp != NULL)
802 zfs_close(zhp);
804 if (zpoolp != NULL)
805 zpool_close(zpoolp);
807 libzfs_fini(g_zfs);
808 return (rc);
812 * Validate that the file exists, and is an ELF file.
813 * Returns 0 on success, -1 on failure.
815 static int
816 validate_unix(char *arg, int *mplen, int *is_zfs, char *bootfs_arg)
818 const char *location;
819 int class, format;
820 unsigned char ident[EI_NIDENT];
821 char physpath[MAXPATHLEN];
822 int elffd = -1;
823 size_t sz;
825 if ((sz = resolvepath(arg, physpath, sizeof (physpath) - 1)) ==
826 (size_t)-1) {
827 (void) fprintf(stderr,
828 gettext("Cannot resolve path for %s: %s\n"),
829 arg, strerror(errno));
830 return (-1);
832 (void) strlcpy(arg, physpath, sz + 1);
834 if (strlen(arg) > MAXPATHLEN) {
835 (void) fprintf(stderr,
836 gettext("%s: New kernel name is too long\n"), cmdname);
837 return (-1);
840 if (strncmp(basename(arg), "unix", 4) != 0) {
841 (void) fprintf(stderr,
842 gettext("%s: %s: Kernel name must be unix\n"),
843 cmdname, arg);
844 return (-1);
847 if (get_zfs_bootfs_arg(arg, &location, is_zfs, bootfs_arg) != 0)
848 goto err_out;
850 *mplen = location - arg;
852 if (strstr(location, "/boot/platform") == location) {
854 * Rebooting to failsafe.
855 * Clear bootfs_arg and is_zfs flag.
857 bootfs_arg[0] = 0;
858 *is_zfs = 0;
859 } else if (strstr(location, "/platform") != location) {
860 (void) fprintf(stderr,
861 gettext("%s: %s: No /platform in file name\n"),
862 cmdname, arg);
863 goto err_out;
866 if ((elffd = open(arg, O_RDONLY)) < 0 ||
867 (pread(elffd, ident, EI_NIDENT, 0) != EI_NIDENT)) {
868 (void) fprintf(stderr, "%s: %s: %s\n",
869 cmdname, arg, strerror(errno));
870 goto err_out;
873 class = ident[EI_CLASS];
875 if ((class != ELFCLASS32 && class != ELFCLASS64) ||
876 memcmp(&ident[EI_MAG0], ELFMAG, 4) != 0) {
877 (void) fprintf(stderr,
878 gettext("%s: %s: Not a valid ELF file\n"), cmdname, arg);
879 goto err_out;
882 format = ident[EI_DATA];
884 if (format != CUR_ELFDATA) {
885 (void) fprintf(stderr, gettext("%s: %s: Invalid data format\n"),
886 cmdname, arg);
887 goto err_out;
890 return (0);
892 err_out:
893 if (elffd >= 0) {
894 (void) close(elffd);
895 elffd = -1;
897 return (-1);
900 static int
901 halt_exec(const char *path, ...)
903 pid_t pid;
904 int i;
905 int st;
906 const char *arg;
907 va_list vp;
908 const char *argv[256];
910 if ((pid = fork()) == -1) {
911 return (errno);
912 } else if (pid == 0) {
913 (void) fclose(stdout);
914 (void) fclose(stderr);
916 argv[0] = path;
917 i = 1;
919 va_start(vp, path);
921 do {
922 arg = va_arg(vp, const char *);
923 argv[i] = arg;
924 } while (arg != NULL &&
925 ++i != sizeof (argv) / sizeof (argv[0]));
927 va_end(vp);
929 (void) execve(path, (char * const *)argv, NULL);
930 (void) fprintf(stderr, gettext("Cannot execute %s: %s\n"),
931 path, strerror(errno));
932 exit(-1);
933 } else {
934 if (waitpid(pid, &st, 0) == pid &&
935 !WIFSIGNALED(st) && WIFEXITED(st))
936 st = WEXITSTATUS(st);
937 else
938 st = -1;
940 return (st);
944 * Mount the specified BE.
946 * Upon success returns zero and copies bename string to mountpoint[]
948 static int
949 fastboot_bename(const char *bename, char *mountpoint, size_t mpsz)
951 int rc;
954 * Attempt to unmount the BE first in case it's already mounted
955 * elsewhere.
957 (void) halt_exec(BEADM_PROG, "umount", bename, NULL);
959 if ((rc = halt_exec(BEADM_PROG, "mount", bename, FASTBOOT_MOUNTPOINT,
960 NULL)) != 0)
961 (void) fprintf(stderr,
962 gettext("%s: Unable to mount BE \"%s\" at %s\n"),
963 cmdname, bename, FASTBOOT_MOUNTPOINT);
964 else
965 (void) strlcpy(mountpoint, FASTBOOT_MOUNTPOINT, mpsz);
967 return (rc);
971 * Returns 0 on successful parsing of the arguments;
972 * returns EINVAL on parsing failures that should abort the reboot attempt;
973 * returns other error code to fall back to regular reboot.
975 static int
976 parse_fastboot_args(char *bootargs_buf, size_t buf_size,
977 int *is_dryrun, const char *bename)
979 char mountpoint[MAXPATHLEN];
980 char bootargs_saved[BOOTARGS_MAX];
981 char bootargs_scratch[BOOTARGS_MAX];
982 char bootfs_arg[BOOTARGS_MAX];
983 char unixfile[BOOTARGS_MAX];
984 char *head, *newarg;
985 int buflen; /* length of the bootargs_buf */
986 int mplen; /* length of the mount point */
987 int rootlen = 0; /* length of the root argument */
988 int unixlen = 0; /* length of the unix argument */
989 int off = 0; /* offset into the new boot argument */
990 int is_zfs = 0;
991 int rc = 0;
993 bzero(mountpoint, sizeof (mountpoint));
996 * If argc is not 0, buflen is length of the argument being passed in;
997 * else it is 0 as bootargs_buf has been initialized to all 0's.
999 buflen = strlen(bootargs_buf);
1001 /* Save a copy of the original argument */
1002 bcopy(bootargs_buf, bootargs_saved, buflen);
1003 bzero(&bootargs_saved[buflen], sizeof (bootargs_saved) - buflen);
1005 /* Save another copy to be used by strtok */
1006 bcopy(bootargs_buf, bootargs_scratch, buflen);
1007 bzero(&bootargs_scratch[buflen], sizeof (bootargs_scratch) - buflen);
1008 head = &bootargs_scratch[0];
1010 /* Get the first argument */
1011 newarg = strtok(bootargs_scratch, " ");
1014 * If this is a dry run request, verify that the drivers can handle
1015 * fast reboot.
1017 if (newarg && strncasecmp(newarg, "dryrun", strlen("dryrun")) == 0) {
1018 *is_dryrun = 1;
1019 (void) system("/usr/sbin/devfsadm");
1023 * Always perform a dry run to identify all the drivers that
1024 * need to implement devo_reset().
1026 if (uadmin(A_SHUTDOWN, AD_FASTREBOOT_DRYRUN,
1027 (uintptr_t)bootargs_saved) != 0) {
1028 (void) fprintf(stderr, gettext("%s: Not all drivers "
1029 "have implemented quiesce(9E)\n"
1030 "\tPlease see /var/log/messages for drivers that haven't\n"
1031 "\timplemented quiesce(9E).\n"), cmdname);
1032 } else if (*is_dryrun) {
1033 (void) fprintf(stderr, gettext("%s: All drivers have "
1034 "implemented quiesce(9E)\n"), cmdname);
1037 /* Return if it is a true dry run. */
1038 if (*is_dryrun)
1039 return (rc);
1041 #if defined(__x86)
1042 /* Read boot args from Boot Environment */
1043 if ((bootargs_buf[0] == 0 || isdigit(bootargs_buf[0])) &&
1044 bename == NULL) {
1046 * If no boot arguments are given, or a BE entry
1047 * number is provided, process the boot arguments from BE.
1049 int entnum;
1050 if (bootargs_buf[0] == 0)
1051 entnum = BE_ENTRY_DEFAULT;
1052 else {
1053 errno = 0;
1054 entnum = strtoul(bootargs_buf, NULL, 10);
1055 rc = errno;
1058 if (rc == 0 && (rc = be_get_boot_args(&fbarg, entnum)) == 0) {
1059 if (strlcpy(bootargs_buf, fbarg,
1060 buf_size) >= buf_size) {
1061 free(fbarg);
1062 bcopy(bootargs_saved, bootargs_buf, buf_size);
1063 rc = E2BIG;
1066 /* Failed to read FB args, fall back to normal reboot */
1067 if (rc != 0) {
1068 (void) fprintf(stderr,
1069 gettext("%s: Failed to process boot "
1070 "arguments from Boot Environment.\n"), cmdname);
1071 (void) fprintf(stderr,
1072 gettext("%s: Falling back to regular reboot.\n"),
1073 cmdname);
1074 return (-1);
1076 /* No need to process further */
1077 fbarg_used = fbarg;
1078 fbarg_entnum = entnum;
1079 return (0);
1081 #endif /* __x86 */
1083 /* Zero out the boot argument buffer as we will reconstruct it */
1084 bzero(bootargs_buf, buf_size);
1085 bzero(bootfs_arg, sizeof (bootfs_arg));
1086 bzero(unixfile, sizeof (unixfile));
1088 if (bename && (rc = fastboot_bename(bename, mountpoint,
1089 sizeof (mountpoint))) != 0)
1090 return (EINVAL);
1094 * If BE is not specified, look for disk argument to construct
1095 * mountpoint; if BE has been specified, mountpoint has already been
1096 * constructed.
1098 if (newarg && newarg[0] != '-' && !bename) {
1099 int tmprc;
1101 if ((tmprc = validate_disk(newarg, mountpoint)) == 0) {
1103 * The first argument is a valid root argument.
1104 * Get the next argument.
1106 newarg = strtok(NULL, " ");
1107 rootlen = (newarg) ? (newarg - head) : buflen;
1108 (void) strlcpy(fastboot_mounted, mountpoint,
1109 sizeof (fastboot_mounted));
1111 } else if (tmprc == -1) {
1113 * Not a disk argument. Use / as default root.
1115 bcopy("/", mountpoint, 1);
1116 bzero(&mountpoint[1], sizeof (mountpoint) - 1);
1117 } else {
1119 * Disk argument, but not valid or not root.
1120 * Return failure.
1122 return (EINVAL);
1127 * Make mountpoint the first part of unixfile.
1128 * If there is not disk argument, and BE has not been specified,
1129 * mountpoint could be empty.
1131 mplen = strlen(mountpoint);
1132 bcopy(mountpoint, unixfile, mplen);
1135 * Look for unix argument
1137 if (newarg && newarg[0] != '-') {
1138 bcopy(newarg, &unixfile[mplen], strlen(newarg));
1139 newarg = strtok(NULL, " ");
1140 rootlen = (newarg) ? (newarg - head) : buflen;
1141 } else if (mplen != 0) {
1143 * No unix argument, but mountpoint is not empty, use
1144 * /platform/$ISADIR/kernel/unix as default.
1146 char isa[20];
1148 if (sysinfo(SI_ARCHITECTURE_64, isa, sizeof (isa)) != -1)
1149 (void) snprintf(&unixfile[mplen],
1150 sizeof (unixfile) - mplen,
1151 "/platform/kernel/%s/unix", isa);
1152 else if (sysinfo(SI_ARCHITECTURE_32, isa, sizeof (isa)) != -1) {
1153 (void) snprintf(&unixfile[mplen],
1154 sizeof (unixfile) - mplen,
1155 "/platform/kernel/unix");
1156 } else {
1157 (void) fprintf(stderr,
1158 gettext("%s: Unknown architecture"), cmdname);
1159 return (EINVAL);
1164 * We now have the complete unix argument. Verify that it exists and
1165 * is an ELF file. Split the argument up into mountpoint and unix
1166 * portions again. This is necessary to handle cases where mountpoint
1167 * is specified on the command line as part of the unix argument,
1168 * such as this:
1169 * # reboot -f /.alt/platform/kernel/amd64/unix
1171 unixlen = strlen(unixfile);
1172 if (unixlen > 0) {
1173 if (validate_unix(unixfile, &mplen, &is_zfs,
1174 bootfs_arg) != 0) {
1175 /* Not a valid unix file */
1176 return (EINVAL);
1177 } else {
1178 int space = 0;
1180 * Construct boot argument.
1182 unixlen = strlen(unixfile);
1185 * mdep cannot start with space because bootadm
1186 * creates bogus menu entries if it does.
1188 if (mplen > 0) {
1189 bcopy(unixfile, bootargs_buf, mplen);
1190 (void) strcat(bootargs_buf, " ");
1191 space = 1;
1193 bcopy(&unixfile[mplen], &bootargs_buf[mplen + space],
1194 unixlen - mplen);
1195 (void) strcat(bootargs_buf, " ");
1196 off += unixlen + space + 1;
1198 } else {
1199 /* Check to see if root is zfs */
1200 const char *dp;
1201 (void) get_zfs_bootfs_arg("/", &dp, &is_zfs, bootfs_arg);
1204 if (is_zfs && (buflen != 0 || bename != NULL)) {
1205 /* do not copy existing zfs boot args */
1206 if (strstr(&bootargs_saved[rootlen], "-B") == NULL ||
1207 strstr(&bootargs_saved[rootlen], "zfs-bootfs=") == NULL ||
1208 (strstr(&bootargs_saved[rootlen], "bootpath=") == NULL &&
1209 strstr(&bootargs_saved[rootlen], "diskdevid=") == NULL))
1210 /* LINTED E_SEC_SPRINTF_UNBOUNDED_COPY */
1211 off += sprintf(bootargs_buf + off, "%s ", bootfs_arg);
1215 * Copy the rest of the arguments
1217 bcopy(&bootargs_saved[rootlen], &bootargs_buf[off], buflen - rootlen);
1219 return (rc);
1222 #define MAXARGS 5
1224 static void
1225 do_archives_update(int do_fast_reboot)
1227 int r, i = 0;
1228 pid_t pid;
1229 char *cmd_argv[MAXARGS];
1232 cmd_argv[i++] = "/sbin/bootadm";
1233 cmd_argv[i++] = "-a";
1234 cmd_argv[i++] = "update_all";
1235 if (do_fast_reboot)
1236 cmd_argv[i++] = "fastboot";
1237 cmd_argv[i] = NULL;
1239 r = posix_spawn(&pid, cmd_argv[0], NULL, NULL, cmd_argv, NULL);
1241 /* if posix_spawn fails we emit a warning and continue */
1243 if (r != 0)
1244 (void) fprintf(stderr, gettext("%s: WARNING, unable to start "
1245 "boot archive update\n"), cmdname);
1246 else
1247 while (waitpid(pid, NULL, 0) == -1 && errno == EINTR)
1252 main(int argc, char *argv[])
1254 int qflag = 0, needlog = 1, nosync = 0;
1255 int fast_reboot = 0;
1256 int prom_reboot = 0;
1257 uintptr_t mdep = (uintptr_t)NULL;
1258 int cmd, fcn, c, r;
1259 const char *usage;
1260 const char *optstring;
1261 zoneid_t zoneid = getzoneid();
1262 int need_check_zones = 0;
1263 char bootargs_buf[BOOTARGS_MAX];
1264 char *bootargs_orig = NULL;
1265 char *bename = NULL;
1267 const char * const resetting = "/etc/svc/volatile/resetting";
1269 (void) setlocale(LC_ALL, "");
1270 (void) textdomain(TEXT_DOMAIN);
1272 cmdname = basename(argv[0]);
1274 if (strcmp(cmdname, "halt") == 0) {
1275 optstring = "dlnqy";
1276 usage = gettext("usage: %s [ -dlnqy ]\n");
1277 cmd = A_SHUTDOWN;
1278 fcn = AD_HALT;
1279 } else if (strcmp(cmdname, "poweroff") == 0) {
1280 optstring = "dlnqy";
1281 usage = gettext("usage: %s [ -dlnqy ]\n");
1282 cmd = A_SHUTDOWN;
1283 fcn = AD_POWEROFF;
1284 } else if (strcmp(cmdname, "reboot") == 0) {
1285 #if defined(__x86)
1286 optstring = "dlnqpfe:";
1287 usage = gettext("usage: %s [ -dlnq(p|fe:) ] [ boot args ]\n");
1288 #else
1289 optstring = "dlnqfp";
1290 usage = gettext("usage: %s [ -dlnq(p|f) ] [ boot args ]\n");
1291 #endif
1292 cmd = A_SHUTDOWN;
1293 fcn = AD_BOOT;
1294 } else {
1295 (void) fprintf(stderr,
1296 gettext("%s: not installed properly\n"), cmdname);
1297 return (1);
1300 while ((c = getopt(argc, argv, optstring)) != EOF) {
1301 switch (c) {
1302 case 'd':
1303 if (zoneid == GLOBAL_ZONEID)
1304 cmd = A_DUMP;
1305 else {
1306 (void) fprintf(stderr,
1307 gettext("%s: -d only valid from global"
1308 " zone\n"), cmdname);
1309 return (1);
1311 break;
1312 case 'l':
1313 needlog = 0;
1314 break;
1315 case 'n':
1316 nosync = 1;
1317 break;
1318 case 'q':
1319 qflag = 1;
1320 break;
1321 case 'y':
1323 * Option ignored for backwards compatibility.
1325 break;
1326 case 'f':
1327 fast_reboot = 1;
1328 break;
1329 case 'p':
1330 prom_reboot = 1;
1331 break;
1332 #if defined(__x86)
1333 case 'e':
1334 bename = optarg;
1335 break;
1336 #endif
1337 default:
1339 * TRANSLATION_NOTE
1340 * Don't translate the words "halt" or "reboot"
1342 (void) fprintf(stderr, usage, cmdname);
1343 return (1);
1347 argc -= optind;
1348 argv += optind;
1350 if (argc != 0) {
1351 if (fcn != AD_BOOT) {
1352 (void) fprintf(stderr, usage, cmdname);
1353 return (1);
1356 /* Gather the arguments into bootargs_buf. */
1357 if (gather_args(argv, bootargs_buf, sizeof (bootargs_buf)) !=
1358 0) {
1359 (void) fprintf(stderr,
1360 gettext("%s: Boot arguments too long.\n"), cmdname);
1361 return (1);
1364 bootargs_orig = strdup(bootargs_buf);
1365 mdep = (uintptr_t)bootargs_buf;
1366 } else {
1368 * Initialize it to 0 in case of fastboot, the buffer
1369 * will be used.
1371 bzero(bootargs_buf, sizeof (bootargs_buf));
1374 if (geteuid() != 0) {
1375 (void) fprintf(stderr,
1376 gettext("%s: permission denied\n"), cmdname);
1377 goto fail;
1380 if (fast_reboot && prom_reboot) {
1381 (void) fprintf(stderr,
1382 gettext("%s: -p and -f are mutually exclusive\n"),
1383 cmdname);
1384 return (EINVAL);
1387 * Check whether fast reboot is the default operating mode
1389 if (fcn == AD_BOOT && !fast_reboot && !prom_reboot &&
1390 zoneid == GLOBAL_ZONEID) {
1391 fast_reboot = scf_is_fastboot_default();
1395 if (bename && !fast_reboot) {
1396 (void) fprintf(stderr, gettext("%s: -e only valid with -f\n"),
1397 cmdname);
1398 return (EINVAL);
1403 * If fast reboot, do some sanity check on the argument
1405 if (fast_reboot == 1) {
1406 int rc;
1407 int is_dryrun = 0;
1409 if (zoneid != GLOBAL_ZONEID) {
1410 (void) fprintf(stderr,
1411 gettext("%s: Fast reboot only valid from global"
1412 " zone\n"), cmdname);
1413 return (EINVAL);
1416 rc = parse_fastboot_args(bootargs_buf, sizeof (bootargs_buf),
1417 &is_dryrun, bename);
1420 * If dry run, or if arguments are invalid, return.
1422 if (is_dryrun)
1423 return (rc);
1424 else if (rc == EINVAL)
1425 goto fail;
1426 else if (rc != 0)
1427 fast_reboot = 0;
1430 * For all the other errors, we continue on in case user
1431 * user want to force fast reboot, or fall back to regular
1432 * reboot.
1434 if (strlen(bootargs_buf) != 0)
1435 mdep = (uintptr_t)bootargs_buf;
1438 #if 0 /* For debugging */
1439 if (mdep != NULL)
1440 (void) fprintf(stderr, "mdep = %s\n", (char *)mdep);
1441 #endif
1443 if (needlog) {
1444 char *user = getlogin();
1445 struct passwd *pw;
1446 char *tty;
1448 openlog(cmdname, 0, LOG_AUTH);
1449 if (user == NULL && (pw = getpwuid(getuid())) != NULL)
1450 user = pw->pw_name;
1451 if (user == NULL)
1452 user = "root";
1454 tty = ttyname(1);
1456 if (tty == NULL)
1457 syslog(LOG_CRIT, "initiated by %s", user);
1458 else
1459 syslog(LOG_CRIT, "initiated by %s on %s", user, tty);
1462 (void) signal(SIGHUP, SIG_IGN); /* for remote connections */
1465 * We start to fork a bunch of zoneadms to halt any active zones.
1466 * This will proceed with halt in parallel until we call
1467 * check_zone_haltedness later on.
1469 if (zoneid == GLOBAL_ZONEID && cmd != A_DUMP) {
1470 need_check_zones = halt_zones();
1473 #if defined(__x86)
1474 /* set new default boot environment */
1475 if (fbarg_entnum != BE_ENTRY_DEFAULT) {
1476 char buf[32];
1477 (void) snprintf(buf, sizeof (buf), "default=%u", fbarg_entnum);
1478 (void) halt_exec(BOOTADM_PROG, "set-menu", buf, NULL);
1480 #endif /* __x86 */
1482 /* if we're dumping, do the archive update here and don't defer it */
1483 if (cmd == A_DUMP && zoneid == GLOBAL_ZONEID && !nosync)
1484 do_archives_update(fast_reboot);
1487 * If we're not forcing a crash dump, mark the system as quiescing for
1488 * smf(5)'s benefit, and idle the init process.
1490 if (cmd != A_DUMP) {
1491 if (direct_init(PCDSTOP) == -1) {
1493 * TRANSLATION_NOTE
1494 * Don't translate the word "init"
1496 (void) fprintf(stderr,
1497 gettext("%s: can't idle init\n"), cmdname);
1498 goto fail;
1501 if (creat(resetting, 0755) == -1)
1502 (void) fprintf(stderr,
1503 gettext("%s: could not create %s.\n"),
1504 cmdname, resetting);
1508 * Make sure we don't get stopped by a jobcontrol shell
1509 * once we start killing everybody.
1511 (void) signal(SIGTSTP, SIG_IGN);
1512 (void) signal(SIGTTIN, SIG_IGN);
1513 (void) signal(SIGTTOU, SIG_IGN);
1514 (void) signal(SIGPIPE, SIG_IGN);
1515 (void) signal(SIGTERM, SIG_IGN);
1518 * Try to stop gdm so X has a chance to return the screen and
1519 * keyboard to a sane state.
1521 if (fast_reboot == 1 && stop_gdm() != 0) {
1522 (void) fprintf(stderr,
1523 gettext("%s: Falling back to regular reboot.\n"), cmdname);
1524 fast_reboot = 0;
1525 mdep = (uintptr_t)bootargs_orig;
1526 } else if (bootargs_orig) {
1527 free(bootargs_orig);
1530 if (cmd != A_DUMP) {
1532 * Stop all restarters so they do not try to restart services
1533 * that are terminated.
1535 stop_restarters();
1538 * Wait a little while for zones to shutdown.
1540 if (need_check_zones) {
1541 check_zones_haltedness();
1543 (void) fprintf(stderr,
1544 gettext("%s: Completing system halt.\n"),
1545 cmdname);
1550 * If we're not forcing a crash dump, give everyone 5 seconds to
1551 * handle a SIGTERM and clean up properly.
1553 if (cmd != A_DUMP) {
1554 int start, end, delta;
1556 (void) kill(-1, SIGTERM);
1557 start = time(NULL);
1559 if (zoneid == GLOBAL_ZONEID && !nosync)
1560 do_archives_update(fast_reboot);
1562 end = time(NULL);
1563 delta = end - start;
1564 if (delta < 5)
1565 (void) sleep(5 - delta);
1568 (void) signal(SIGINT, SIG_IGN);
1570 if (!qflag && !nosync) {
1571 struct utmpx wtmpx;
1573 bzero(&wtmpx, sizeof (struct utmpx));
1574 (void) strcpy(wtmpx.ut_line, "~");
1575 (void) time(&wtmpx.ut_tv.tv_sec);
1577 if (cmd == A_DUMP)
1578 (void) strcpy(wtmpx.ut_name, "crash dump");
1579 else
1580 (void) strcpy(wtmpx.ut_name, "shutdown");
1582 (void) updwtmpx(WTMPX_FILE, &wtmpx);
1583 sync();
1586 if (cmd == A_DUMP && nosync != 0)
1587 (void) uadmin(A_DUMP, AD_NOSYNC, (uintptr_t)NULL);
1589 if (fast_reboot)
1590 fcn = AD_FASTREBOOT;
1592 if (uadmin(cmd, fcn, mdep) == -1)
1593 (void) fprintf(stderr, "%s: uadmin failed: %s\n",
1594 cmdname, strerror(errno));
1595 else
1596 (void) fprintf(stderr, "%s: uadmin unexpectedly returned 0\n",
1597 cmdname);
1599 do {
1600 r = remove(resetting);
1601 } while (r != 0 && errno == EINTR);
1603 if (r != 0 && errno != ENOENT)
1604 (void) fprintf(stderr, gettext("%s: could not remove %s.\n"),
1605 cmdname, resetting);
1607 if (direct_init(PCRUN) == -1) {
1609 * TRANSLATION_NOTE
1610 * Don't translate the word "init"
1612 (void) fprintf(stderr,
1613 gettext("%s: can't resume init\n"), cmdname);
1616 continue_restarters();
1618 if (get_initpid() != -1)
1619 /* tell init to restate current level */
1620 (void) kill(get_initpid(), SIGHUP);
1622 fail:
1623 if (fast_reboot == 1) {
1624 if (bename) {
1625 (void) halt_exec(BEADM_PROG, "umount", bename, NULL);
1627 } else if (strlen(fastboot_mounted) != 0) {
1628 (void) umount(fastboot_mounted);
1629 #if defined(__x86)
1630 } else {
1631 free(fbarg_used);
1632 #endif /* __x86 */
1636 return (1);