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]
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
39 * University Acknowledgment- Portions of this document are derived from
40 * software developed by the University of California, Berkeley, and its
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.
53 #include <sys/types.h>
55 #include <sys/systeminfo.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>
68 #include <libscf_priv.h>
92 #if !defined(TEXT_DOMAIN)
93 #define TEXT_DOMAIN "SYS_TEST"
97 #define CUR_ELFDATA ELFDATA2MSB
99 #define CUR_ELFDATA ELFDATA2LSB
102 static libzfs_handle_t
*g_zfs
;
104 static char *cmdname
; /* basename(argv[0]), the name of the command */
106 typedef struct ctidlist_struct
{
108 struct ctidlist_struct
*next
;
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
];
133 static char *fbarg_used
;
134 static int fbarg_entnum
= BE_ENTRY_DEFAULT
;
137 static int validate_ufs_disk(char *, char *);
138 static int validate_zfs_pool(char *, char *);
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
);
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
161 direct_init(long command
)
163 char ctlfile
[MAXPATHLEN
];
167 assert(command
== PCDSTOP
|| command
== PCRUN
);
168 if ((pid
= get_initpid()) == -1) {
172 (void) snprintf(ctlfile
, sizeof (ctlfile
), "/proc/%d/ctl", pid
);
173 if ((ctlfd
= open(ctlfile
, O_WRONLY
)) == -1)
176 if (command
== PCDSTOP
) {
177 if (write(ctlfd
, &command
, sizeof (long)) == -1) {
181 } else { /* command == PCRUN */
185 if (write(ctlfd
, cmds
, sizeof (cmds
)) == -1) {
198 scf_property_t
*prop
= NULL
;
199 scf_value_t
*val
= NULL
;
202 if ((h
= scf_handle_create(SCF_VERSION
)) == NULL
)
205 if ((scf_handle_bind(h
) != 0) ||
206 ((prop
= scf_property_create(h
)) == NULL
) ||
207 ((val
= scf_value_create(h
)) == NULL
))
210 if (scf_handle_decode_fmri(h
, FMRI_STARTD_CONTRACT
,
211 NULL
, NULL
, NULL
, NULL
, prop
, SCF_DECODE_FMRI_EXACT
) != 0)
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)
219 startdct
= (ctid_t
)uint64
;
220 (void) sigsend(P_CTID
, startdct
, SIGSTOP
);
223 scf_property_destroy(prop
);
224 scf_value_destroy(val
);
225 scf_handle_destroy(h
);
232 (void) sigsend(P_CTID
, startdct
, SIGCONT
);
235 #define FMRI_RESTARTER_PROP "/:properties/general/restarter"
236 #define FMRI_CONTRACT_PROP "/:properties/restarter/contract"
239 save_ctid(ctid_t ctid
)
243 for (next
= ctidlist
; next
!= NULL
; next
= next
->next
)
244 if (next
->ctid
== ctid
)
247 next
= (ctidlist_t
*)malloc(sizeof (ctidlist_t
));
252 next
->next
= ctidlist
;
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
;
278 length
= scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH
);
283 fmri
= alloca(length
* sizeof (char));
285 if ((h
= scf_handle_create(SCF_VERSION
)) == NULL
)
288 if (scf_handle_bind(h
) != 0) {
289 scf_handle_destroy(h
);
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
)
304 if (scf_handle_get_scope(h
, SCF_SCOPE_LOCAL
, sc
) != 0)
307 if (scf_iter_scope_services(siter
, sc
) != 0)
310 while (scf_iter_next_service(siter
, svc
) == 1) {
312 if (scf_iter_service_instances(iiter
, svc
) != 0)
315 while (scf_iter_next_instance(iiter
, inst
) == 1) {
317 if ((scf_instance_get_snapshot(inst
, "running",
323 if (scf_instance_get_pg_composed(inst
, isnap
,
324 SCF_PG_GENERAL
, pg
) != 0)
327 if (scf_pg_get_property(pg
, SCF_PROPERTY_RESTARTER
,
329 scf_property_get_value(prop
, val
) != 0)
332 bytes
= scf_value_get_astring(val
, fmri
, length
);
333 if (bytes
<= 0 || bytes
>= length
)
336 if (strlcat(fmri
, FMRI_CONTRACT_PROP
, length
) >=
340 if (scf_handle_decode_fmri(h
, fmri
, NULL
, NULL
,
341 NULL
, NULL
, prop
, SCF_DECODE_FMRI_EXACT
) != 0)
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)
349 ctid
= (ctid_t
)uint64
;
350 if (save_ctid(ctid
) == 0) {
351 (void) sigsend(P_CTID
, ctid
, SIGSTOP
);
356 scf_scope_destroy(sc
);
357 scf_service_destroy(svc
);
358 scf_instance_destroy(inst
);
359 scf_snapshot_destroy(snap
);
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
);
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.
388 char *gdm_state
= NULL
;
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
) {
401 * Only need to disable once.
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()));
413 if (retry
>= GDM_STOP_TIMEOUT
) {
414 (void) fprintf(stderr
, gettext("%s: Failed to stop %s.\n"),
431 continue_restarters()
434 continue_delegates();
438 * Copy an array of strings into buf, separated by spaces. Returns 0 on
442 gather_args(char **args
, char *buf
, size_t buf_sz
)
444 if (strlcpy(buf
, *args
, buf_sz
) >= buf_sz
)
447 for (++args
; *args
!= NULL
; ++args
) {
448 if (strlcat(buf
, " ", buf_sz
) >= buf_sz
)
450 if (strlcat(buf
, *args
, buf_sz
) >= buf_sz
)
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
468 size_t nz
= 0, old_nz
;
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.
478 (void) zone_list(NULL
, &nz
);
482 zones
= calloc(sizeof (zoneid_t
), nz
);
484 (void) fprintf(stderr
,
485 gettext("%s: Could not halt zones"
486 " (out of memory).\n"), cmdname
);
490 (void) zone_list(zones
, &nz
);
497 (void) fprintf(stderr
, gettext("%s: Halting 1 zone.\n"),
500 (void) fprintf(stderr
, gettext("%s: Halting %i zones.\n"),
504 for (i
= 0; i
< nz
; i
++) {
505 if (zones
[i
] == GLOBAL_ZONEID
)
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
));
524 (void) fprintf(stderr
,
525 gettext("%s: Zone \"%s\" could not be"
526 " halted (could not fork(): %s).\n"),
527 cmdname
, zname
, strerror(errno
));
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
));
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.
551 check_zones_haltedness()
553 int t
= 0, t_prog
= 0;
554 size_t nz
= 0, last_nz
;
558 (void) zone_list(NULL
, &nz
);
572 (void) fprintf(stderr
,
573 gettext("%s: Still waiting for 1 zone to "
574 "halt. Will wait up to 20 seconds.\n"),
577 (void) fprintf(stderr
,
578 gettext("%s: Still waiting for %i zones "
579 "to halt. Will wait up to 20 seconds.\n"),
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.
595 validate_disk(char *arg
, char *mountpoint
)
597 static char root_dev_path
[] = "/dev/dsk";
598 char kernpath
[MAXPATHLEN
];
602 if (strlen(arg
) > MAXPATHLEN
) {
603 (void) fprintf(stderr
,
604 gettext("%s: Argument is too long\n"), cmdname
);
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"),
628 if (strncmp(arg
, root_dev_path
, strlen(root_dev_path
)) == 0) {
629 /* ufs root disk argument */
630 rc
= validate_ufs_disk(arg
, mountpoint
);
632 /* zfs root pool argument */
633 rc
= validate_zfs_pool(arg
, mountpoint
);
639 (void) snprintf(kernpath
, MAXPATHLEN
, "%s/platform/kernel/unix",
642 if (stat(kernpath
, &statbuf
) != 0) {
643 (void) fprintf(stderr
,
644 gettext("%s: %s is not a root disk or dataset\n"),
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) {
665 (void) fprintf(stderr
,
666 gettext("%s: Failed to mount %s\n"), cmdname
, arg
);
674 validate_zfs_pool(char *arg
, char *mountpoint
)
676 zfs_handle_t
*zhp
= NULL
;
677 char mntopts
[MNT_LINE_MAX
] = { '\0' };
680 if ((g_zfs
= libzfs_init()) == NULL
) {
681 (void) fprintf(stderr
, gettext("Internal error: failed to "
682 "initialize ZFS library\n"));
686 /* Try to open the dataset */
687 if ((zhp
= zfs_open(g_zfs
, arg
,
688 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_DATASET
)) == NULL
)
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) {
695 (void) fprintf(stderr
,
696 gettext("%s: Failed to mount %s\n"), cmdname
, arg
);
700 validate_zfs_err_out
:
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
715 get_zfs_bootfs_arg(const char *arg
, const char ** fpth
, int *is_zfs
,
718 zfs_handle_t
*zhp
= NULL
;
719 zpool_handle_t
*zpoolp
= NULL
;
722 char *poolname
= NULL
;
723 char physpath
[MAXPATHLEN
];
724 char mntsp
[ZFS_MAX_DATASET_NAME_LEN
];
725 char bootfs
[ZFS_MAX_DATASET_NAME_LEN
];
729 static char fmt
[] = "-B zfs-bootfs=%s,bootpath=\"%s\"";
734 bzero(physpath
, sizeof (physpath
));
735 bzero(bootfs
, sizeof (bootfs
));
737 if ((mtabp
= fopen(MNTTAB
, "r")) == NULL
) {
741 while (getmntent(mtabp
, &mnt
) == 0) {
742 if (strstr(arg
, mnt
.mnt_mountp
) == arg
&&
743 (msz
= strlen(mnt
.mnt_mountp
)) > mntlen
) {
745 *is_zfs
= strcmp(MNTTYPE_ZFS
, mnt
.mnt_fstype
) == 0;
746 (void) strlcpy(mntsp
, mnt
.mnt_special
, sizeof (mntsp
));
750 (void) fclose(mtabp
);
758 if ((g_zfs
= libzfs_init()) == NULL
)
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
);
766 goto validate_zfs_err_out
;
769 (void) strlcpy(bootfs
, mntsp
, sizeof (bootfs
));
771 if ((poolname
= strtok(mntsp
, "/")) == NULL
) {
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
);
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"));
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
) >=
796 (void) fprintf(stderr
,
797 gettext("Boot arguments are too long\n"));
800 validate_zfs_err_out
:
812 * Validate that the file exists, and is an ELF file.
813 * Returns 0 on success, -1 on failure.
816 validate_unix(char *arg
, int *mplen
, int *is_zfs
, char *bootfs_arg
)
818 const char *location
;
820 unsigned char ident
[EI_NIDENT
];
821 char physpath
[MAXPATHLEN
];
825 if ((sz
= resolvepath(arg
, physpath
, sizeof (physpath
) - 1)) ==
827 (void) fprintf(stderr
,
828 gettext("Cannot resolve path for %s: %s\n"),
829 arg
, strerror(errno
));
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
);
840 if (strncmp(basename(arg
), "unix", 4) != 0) {
841 (void) fprintf(stderr
,
842 gettext("%s: %s: Kernel name must be unix\n"),
847 if (get_zfs_bootfs_arg(arg
, &location
, is_zfs
, bootfs_arg
) != 0)
850 *mplen
= location
- arg
;
852 if (strstr(location
, "/boot/platform") == location
) {
854 * Rebooting to failsafe.
855 * Clear bootfs_arg and is_zfs flag.
859 } else if (strstr(location
, "/platform") != location
) {
860 (void) fprintf(stderr
,
861 gettext("%s: %s: No /platform in file name\n"),
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
));
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
);
882 format
= ident
[EI_DATA
];
884 if (format
!= CUR_ELFDATA
) {
885 (void) fprintf(stderr
, gettext("%s: %s: Invalid data format\n"),
901 halt_exec(const char *path
, ...)
908 const char *argv
[256];
910 if ((pid
= fork()) == -1) {
912 } else if (pid
== 0) {
913 (void) fclose(stdout
);
914 (void) fclose(stderr
);
922 arg
= va_arg(vp
, const char *);
924 } while (arg
!= NULL
&&
925 ++i
!= sizeof (argv
) / sizeof (argv
[0]));
929 (void) execve(path
, (char * const *)argv
, NULL
);
930 (void) fprintf(stderr
, gettext("Cannot execute %s: %s\n"),
931 path
, strerror(errno
));
934 if (waitpid(pid
, &st
, 0) == pid
&&
935 !WIFSIGNALED(st
) && WIFEXITED(st
))
936 st
= WEXITSTATUS(st
);
944 * Mount the specified BE.
946 * Upon success returns zero and copies bename string to mountpoint[]
949 fastboot_bename(const char *bename
, char *mountpoint
, size_t mpsz
)
954 * Attempt to unmount the BE first in case it's already mounted
957 (void) halt_exec(BEADM_PROG
, "umount", bename
, NULL
);
959 if ((rc
= halt_exec(BEADM_PROG
, "mount", bename
, FASTBOOT_MOUNTPOINT
,
961 (void) fprintf(stderr
,
962 gettext("%s: Unable to mount BE \"%s\" at %s\n"),
963 cmdname
, bename
, FASTBOOT_MOUNTPOINT
);
965 (void) strlcpy(mountpoint
, FASTBOOT_MOUNTPOINT
, mpsz
);
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.
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
];
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 */
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
1017 if (newarg
&& strncasecmp(newarg
, "dryrun", strlen("dryrun")) == 0) {
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. */
1042 /* Read boot args from Boot Environment */
1043 if ((bootargs_buf
[0] == 0 || isdigit(bootargs_buf
[0])) &&
1046 * If no boot arguments are given, or a BE entry
1047 * number is provided, process the boot arguments from BE.
1050 if (bootargs_buf
[0] == 0)
1051 entnum
= BE_ENTRY_DEFAULT
;
1054 entnum
= strtoul(bootargs_buf
, NULL
, 10);
1058 if (rc
== 0 && (rc
= be_get_boot_args(&fbarg
, entnum
)) == 0) {
1059 if (strlcpy(bootargs_buf
, fbarg
,
1060 buf_size
) >= buf_size
) {
1062 bcopy(bootargs_saved
, bootargs_buf
, buf_size
);
1066 /* Failed to read FB args, fall back to normal reboot */
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"),
1076 /* No need to process further */
1078 fbarg_entnum
= entnum
;
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)
1094 * If BE is not specified, look for disk argument to construct
1095 * mountpoint; if BE has been specified, mountpoint has already been
1098 if (newarg
&& newarg
[0] != '-' && !bename
) {
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);
1119 * Disk argument, but not valid or not root.
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.
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");
1157 (void) fprintf(stderr
,
1158 gettext("%s: Unknown architecture"), cmdname
);
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,
1169 * # reboot -f /.alt/platform/kernel/amd64/unix
1171 unixlen
= strlen(unixfile
);
1173 if (validate_unix(unixfile
, &mplen
, &is_zfs
,
1175 /* Not a valid unix file */
1180 * Construct boot argument.
1182 unixlen
= strlen(unixfile
);
1185 * mdep cannot start with space because bootadm
1186 * creates bogus menu entries if it does.
1189 bcopy(unixfile
, bootargs_buf
, mplen
);
1190 (void) strcat(bootargs_buf
, " ");
1193 bcopy(&unixfile
[mplen
], &bootargs_buf
[mplen
+ space
],
1195 (void) strcat(bootargs_buf
, " ");
1196 off
+= unixlen
+ space
+ 1;
1199 /* Check to see if root is zfs */
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
);
1225 do_archives_update(int do_fast_reboot
)
1229 char *cmd_argv
[MAXARGS
];
1232 cmd_argv
[i
++] = "/sbin/bootadm";
1233 cmd_argv
[i
++] = "-a";
1234 cmd_argv
[i
++] = "update_all";
1236 cmd_argv
[i
++] = "fastboot";
1239 r
= posix_spawn(&pid
, cmd_argv
[0], NULL
, NULL
, cmd_argv
, NULL
);
1241 /* if posix_spawn fails we emit a warning and continue */
1244 (void) fprintf(stderr
, gettext("%s: WARNING, unable to start "
1245 "boot archive update\n"), cmdname
);
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
;
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");
1279 } else if (strcmp(cmdname
, "poweroff") == 0) {
1280 optstring
= "dlnqy";
1281 usage
= gettext("usage: %s [ -dlnqy ]\n");
1284 } else if (strcmp(cmdname
, "reboot") == 0) {
1286 optstring
= "dlnqpfe:";
1287 usage
= gettext("usage: %s [ -dlnq(p|fe:) ] [ boot args ]\n");
1289 optstring
= "dlnqfp";
1290 usage
= gettext("usage: %s [ -dlnq(p|f) ] [ boot args ]\n");
1295 (void) fprintf(stderr
,
1296 gettext("%s: not installed properly\n"), cmdname
);
1300 while ((c
= getopt(argc
, argv
, optstring
)) != EOF
) {
1303 if (zoneid
== GLOBAL_ZONEID
)
1306 (void) fprintf(stderr
,
1307 gettext("%s: -d only valid from global"
1308 " zone\n"), cmdname
);
1323 * Option ignored for backwards compatibility.
1340 * Don't translate the words "halt" or "reboot"
1342 (void) fprintf(stderr
, usage
, cmdname
);
1351 if (fcn
!= AD_BOOT
) {
1352 (void) fprintf(stderr
, usage
, cmdname
);
1356 /* Gather the arguments into bootargs_buf. */
1357 if (gather_args(argv
, bootargs_buf
, sizeof (bootargs_buf
)) !=
1359 (void) fprintf(stderr
,
1360 gettext("%s: Boot arguments too long.\n"), cmdname
);
1364 bootargs_orig
= strdup(bootargs_buf
);
1365 mdep
= (uintptr_t)bootargs_buf
;
1368 * Initialize it to 0 in case of fastboot, the buffer
1371 bzero(bootargs_buf
, sizeof (bootargs_buf
));
1374 if (geteuid() != 0) {
1375 (void) fprintf(stderr
,
1376 gettext("%s: permission denied\n"), cmdname
);
1380 if (fast_reboot
&& prom_reboot
) {
1381 (void) fprintf(stderr
,
1382 gettext("%s: -p and -f are mutually exclusive\n"),
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"),
1403 * If fast reboot, do some sanity check on the argument
1405 if (fast_reboot
== 1) {
1409 if (zoneid
!= GLOBAL_ZONEID
) {
1410 (void) fprintf(stderr
,
1411 gettext("%s: Fast reboot only valid from global"
1412 " zone\n"), cmdname
);
1416 rc
= parse_fastboot_args(bootargs_buf
, sizeof (bootargs_buf
),
1417 &is_dryrun
, bename
);
1420 * If dry run, or if arguments are invalid, return.
1424 else if (rc
== EINVAL
)
1430 * For all the other errors, we continue on in case user
1431 * user want to force fast reboot, or fall back to regular
1434 if (strlen(bootargs_buf
) != 0)
1435 mdep
= (uintptr_t)bootargs_buf
;
1438 #if 0 /* For debugging */
1440 (void) fprintf(stderr
, "mdep = %s\n", (char *)mdep
);
1444 char *user
= getlogin();
1448 openlog(cmdname
, 0, LOG_AUTH
);
1449 if (user
== NULL
&& (pw
= getpwuid(getuid())) != NULL
)
1457 syslog(LOG_CRIT
, "initiated by %s", user
);
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();
1474 /* set new default boot environment */
1475 if (fbarg_entnum
!= BE_ENTRY_DEFAULT
) {
1477 (void) snprintf(buf
, sizeof (buf
), "default=%u", fbarg_entnum
);
1478 (void) halt_exec(BOOTADM_PROG
, "set-menu", buf
, NULL
);
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) {
1494 * Don't translate the word "init"
1496 (void) fprintf(stderr
,
1497 gettext("%s: can't idle init\n"), cmdname
);
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
);
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.
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"),
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
);
1559 if (zoneid
== GLOBAL_ZONEID
&& !nosync
)
1560 do_archives_update(fast_reboot
);
1563 delta
= end
- start
;
1565 (void) sleep(5 - delta
);
1568 (void) signal(SIGINT
, SIG_IGN
);
1570 if (!qflag
&& !nosync
) {
1573 bzero(&wtmpx
, sizeof (struct utmpx
));
1574 (void) strcpy(wtmpx
.ut_line
, "~");
1575 (void) time(&wtmpx
.ut_tv
.tv_sec
);
1578 (void) strcpy(wtmpx
.ut_name
, "crash dump");
1580 (void) strcpy(wtmpx
.ut_name
, "shutdown");
1582 (void) updwtmpx(WTMPX_FILE
, &wtmpx
);
1586 if (cmd
== A_DUMP
&& nosync
!= 0)
1587 (void) uadmin(A_DUMP
, AD_NOSYNC
, (uintptr_t)NULL
);
1590 fcn
= AD_FASTREBOOT
;
1592 if (uadmin(cmd
, fcn
, mdep
) == -1)
1593 (void) fprintf(stderr
, "%s: uadmin failed: %s\n",
1594 cmdname
, strerror(errno
));
1596 (void) fprintf(stderr
, "%s: uadmin unexpectedly returned 0\n",
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) {
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
);
1623 if (fast_reboot
== 1) {
1625 (void) halt_exec(BEADM_PROG
, "umount", bename
, NULL
);
1627 } else if (strlen(fastboot_mounted
) != 0) {
1628 (void) umount(fastboot_mounted
);