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(1M), poweroff(1M), and reboot(1M). 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 extern int audit_halt_setup(int, char **);
105 extern int audit_halt_success(void);
106 extern int audit_halt_fail(void);
108 extern int audit_reboot_setup(void);
109 extern int audit_reboot_success(void);
110 extern int audit_reboot_fail(void);
112 static char *cmdname
; /* basename(argv[0]), the name of the command */
114 typedef struct ctidlist_struct
{
116 struct ctidlist_struct
*next
;
119 static ctidlist_t
*ctidlist
= NULL
;
120 static ctid_t startdct
= -1;
122 #define FMRI_STARTD_CONTRACT \
123 "svc:/system/svc/restarter:default/:properties/restarter/contract"
125 #define BEADM_PROG "/usr/sbin/beadm"
126 #define BOOTADM_PROG "/sbin/bootadm"
127 #define ZONEADM_PROG "/usr/sbin/zoneadm"
130 * The length of FASTBOOT_MOUNTPOINT must be less than MAXPATHLEN.
132 #define FASTBOOT_MOUNTPOINT "/tmp/.fastboot.root"
135 * Fast Reboot related variables
137 static char fastboot_mounted
[MAXPATHLEN
];
141 static char *fbarg_used
;
142 static int fbarg_entnum
= BE_ENTRY_DEFAULT
;
145 static int validate_ufs_disk(char *, char *);
146 static int validate_zfs_pool(char *, char *);
151 static int init_pid
= -1;
153 if (init_pid
== -1) {
154 if (zone_getattr(getzoneid(), ZONE_ATTR_INITPID
, &init_pid
,
155 sizeof (init_pid
)) != sizeof (init_pid
)) {
156 assert(errno
== ESRCH
);
164 * Quiesce or resume init using /proc. When stopping init, we can't send
165 * SIGTSTP (since init ignores it) or SIGSTOP (since the kernel won't permit
169 direct_init(long command
)
171 char ctlfile
[MAXPATHLEN
];
175 assert(command
== PCDSTOP
|| command
== PCRUN
);
176 if ((pid
= get_initpid()) == -1) {
180 (void) snprintf(ctlfile
, sizeof (ctlfile
), "/proc/%d/ctl", pid
);
181 if ((ctlfd
= open(ctlfile
, O_WRONLY
)) == -1)
184 if (command
== PCDSTOP
) {
185 if (write(ctlfd
, &command
, sizeof (long)) == -1) {
189 } else { /* command == PCRUN */
193 if (write(ctlfd
, cmds
, sizeof (cmds
)) == -1) {
206 scf_property_t
*prop
= NULL
;
207 scf_value_t
*val
= NULL
;
210 if ((h
= scf_handle_create(SCF_VERSION
)) == NULL
)
213 if ((scf_handle_bind(h
) != 0) ||
214 ((prop
= scf_property_create(h
)) == NULL
) ||
215 ((val
= scf_value_create(h
)) == NULL
))
218 if (scf_handle_decode_fmri(h
, FMRI_STARTD_CONTRACT
,
219 NULL
, NULL
, NULL
, NULL
, prop
, SCF_DECODE_FMRI_EXACT
) != 0)
222 if (scf_property_is_type(prop
, SCF_TYPE_COUNT
) != 0 ||
223 scf_property_get_value(prop
, val
) != 0 ||
224 scf_value_get_count(val
, &uint64
) != 0)
227 startdct
= (ctid_t
)uint64
;
228 (void) sigsend(P_CTID
, startdct
, SIGSTOP
);
231 scf_property_destroy(prop
);
232 scf_value_destroy(val
);
233 scf_handle_destroy(h
);
240 (void) sigsend(P_CTID
, startdct
, SIGCONT
);
243 #define FMRI_RESTARTER_PROP "/:properties/general/restarter"
244 #define FMRI_CONTRACT_PROP "/:properties/restarter/contract"
247 save_ctid(ctid_t ctid
)
251 for (next
= ctidlist
; next
!= NULL
; next
= next
->next
)
252 if (next
->ctid
== ctid
)
255 next
= (ctidlist_t
*)malloc(sizeof (ctidlist_t
));
260 next
->next
= ctidlist
;
270 scf_scope_t
*sc
= NULL
;
271 scf_service_t
*svc
= NULL
;
272 scf_instance_t
*inst
= NULL
;
273 scf_snapshot_t
*snap
= NULL
;
274 scf_snapshot_t
*isnap
= NULL
;
275 scf_propertygroup_t
*pg
= NULL
;
276 scf_property_t
*prop
= NULL
;
277 scf_value_t
*val
= NULL
;
278 scf_iter_t
*siter
= NULL
;
279 scf_iter_t
*iiter
= NULL
;
286 length
= scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH
);
291 fmri
= alloca(length
* sizeof (char));
293 if ((h
= scf_handle_create(SCF_VERSION
)) == NULL
)
296 if (scf_handle_bind(h
) != 0) {
297 scf_handle_destroy(h
);
301 if ((sc
= scf_scope_create(h
)) == NULL
||
302 (svc
= scf_service_create(h
)) == NULL
||
303 (inst
= scf_instance_create(h
)) == NULL
||
304 (snap
= scf_snapshot_create(h
)) == NULL
||
305 (pg
= scf_pg_create(h
)) == NULL
||
306 (prop
= scf_property_create(h
)) == NULL
||
307 (val
= scf_value_create(h
)) == NULL
||
308 (siter
= scf_iter_create(h
)) == NULL
||
309 (iiter
= scf_iter_create(h
)) == NULL
)
312 if (scf_handle_get_scope(h
, SCF_SCOPE_LOCAL
, sc
) != 0)
315 if (scf_iter_scope_services(siter
, sc
) != 0)
318 while (scf_iter_next_service(siter
, svc
) == 1) {
320 if (scf_iter_service_instances(iiter
, svc
) != 0)
323 while (scf_iter_next_instance(iiter
, inst
) == 1) {
325 if ((scf_instance_get_snapshot(inst
, "running",
331 if (scf_instance_get_pg_composed(inst
, isnap
,
332 SCF_PG_GENERAL
, pg
) != 0)
335 if (scf_pg_get_property(pg
, SCF_PROPERTY_RESTARTER
,
337 scf_property_get_value(prop
, val
) != 0)
340 bytes
= scf_value_get_astring(val
, fmri
, length
);
341 if (bytes
<= 0 || bytes
>= length
)
344 if (strlcat(fmri
, FMRI_CONTRACT_PROP
, length
) >=
348 if (scf_handle_decode_fmri(h
, fmri
, NULL
, NULL
,
349 NULL
, NULL
, prop
, SCF_DECODE_FMRI_EXACT
) != 0)
352 if (scf_property_is_type(prop
, SCF_TYPE_COUNT
) != 0 ||
353 scf_property_get_value(prop
, val
) != 0 ||
354 scf_value_get_count(val
, &uint64
) != 0)
357 ctid
= (ctid_t
)uint64
;
358 if (save_ctid(ctid
) == 0) {
359 (void) sigsend(P_CTID
, ctid
, SIGSTOP
);
364 scf_scope_destroy(sc
);
365 scf_service_destroy(svc
);
366 scf_instance_destroy(inst
);
367 scf_snapshot_destroy(snap
);
369 scf_property_destroy(prop
);
370 scf_value_destroy(val
);
371 scf_iter_destroy(siter
);
372 scf_iter_destroy(iiter
);
374 (void) scf_handle_unbind(h
);
375 scf_handle_destroy(h
);
382 for (next
= ctidlist
; next
!= NULL
; next
= next
->next
)
383 (void) sigsend(P_CTID
, next
->ctid
, SIGCONT
);
386 #define FMRI_GDM "svc:/application/graphical-login/gdm:default"
387 #define GDM_STOP_TIMEOUT 10 /* Give gdm 10 seconds to shut down */
390 * If gdm is running, try to stop gdm.
391 * Returns 0 on success, -1 on failure.
396 char *gdm_state
= NULL
;
400 * If gdm is running, try to stop gdm.
402 while ((gdm_state
= smf_get_state(FMRI_GDM
)) != NULL
&&
403 strcmp(gdm_state
, SCF_STATE_STRING_ONLINE
) == 0 &&
404 retry
++ < GDM_STOP_TIMEOUT
) {
409 * Only need to disable once.
412 smf_disable_instance(FMRI_GDM
, SMF_TEMPORARY
) != 0) {
413 (void) fprintf(stderr
,
414 gettext("%s: Failed to stop %s: %s.\n"),
415 cmdname
, FMRI_GDM
, scf_strerror(scf_error()));
421 if (retry
>= GDM_STOP_TIMEOUT
) {
422 (void) fprintf(stderr
, gettext("%s: Failed to stop %s.\n"),
439 continue_restarters()
442 continue_delegates();
446 * Copy an array of strings into buf, separated by spaces. Returns 0 on
450 gather_args(char **args
, char *buf
, size_t buf_sz
)
452 if (strlcpy(buf
, *args
, buf_sz
) >= buf_sz
)
455 for (++args
; *args
!= NULL
; ++args
) {
456 if (strlcat(buf
, " ", buf_sz
) >= buf_sz
)
458 if (strlcat(buf
, *args
, buf_sz
) >= buf_sz
)
466 * Halt every zone on the system. We are committed to doing a shutdown
467 * even if something goes wrong here. If something goes wrong, we just
468 * continue with the shutdown. Return non-zero if we need to wait for zones to
476 size_t nz
= 0, old_nz
;
478 char zname
[ZONENAME_MAX
];
481 * Get a list of zones. If the number of zones changes in between the
482 * two zone_list calls, try again.
486 (void) zone_list(NULL
, &nz
);
490 zones
= calloc(sizeof (zoneid_t
), nz
);
492 (void) fprintf(stderr
,
493 gettext("%s: Could not halt zones"
494 " (out of memory).\n"), cmdname
);
498 (void) zone_list(zones
, &nz
);
505 (void) fprintf(stderr
, gettext("%s: Halting 1 zone.\n"),
508 (void) fprintf(stderr
, gettext("%s: Halting %i zones.\n"),
512 for (i
= 0; i
< nz
; i
++) {
513 if (zones
[i
] == GLOBAL_ZONEID
)
515 if (getzonenamebyid(zones
[i
], zname
, sizeof (zname
)) < 0) {
517 * getzonenamebyid should only fail if we raced with
518 * another process trying to shut down the zone.
519 * We assume this happened and ignore the error.
521 if (errno
!= EINVAL
) {
522 (void) fprintf(stderr
,
523 gettext("%s: Unexpected error while "
524 "looking up zone %ul: %s.\n"),
525 cmdname
, zones
[i
], strerror(errno
));
532 (void) fprintf(stderr
,
533 gettext("%s: Zone \"%s\" could not be"
534 " halted (could not fork(): %s).\n"),
535 cmdname
, zname
, strerror(errno
));
539 (void) execl(ZONEADM_PROG
, ZONEADM_PROG
,
540 "-z", zname
, "halt", NULL
);
541 (void) fprintf(stderr
,
542 gettext("%s: Zone \"%s\" could not be halted"
543 " (cannot exec(" ZONEADM_PROG
"): %s).\n"),
544 cmdname
, zname
, strerror(errno
));
553 * This function tries to wait for all non-global zones to go away.
554 * It will timeout if no progress is made for 5 seconds, or a total of
555 * 30 seconds elapses.
559 check_zones_haltedness()
561 int t
= 0, t_prog
= 0;
562 size_t nz
= 0, last_nz
;
566 (void) zone_list(NULL
, &nz
);
580 (void) fprintf(stderr
,
581 gettext("%s: Still waiting for 1 zone to "
582 "halt. Will wait up to 20 seconds.\n"),
585 (void) fprintf(stderr
,
586 gettext("%s: Still waiting for %i zones "
587 "to halt. Will wait up to 20 seconds.\n"),
592 } while ((t
< 30) && (t_prog
< 5));
597 * Validate that this is a root disk or dataset
598 * Returns 0 if it is a root disk or dataset;
599 * returns 1 if it is a disk argument or dataset, but not valid or not root;
600 * returns -1 if it is not a valid argument or a disk argument.
603 validate_disk(char *arg
, char *mountpoint
)
605 static char root_dev_path
[] = "/dev/dsk";
606 char kernpath
[MAXPATHLEN
];
607 struct stat64 statbuf
;
610 if (strlen(arg
) > MAXPATHLEN
) {
611 (void) fprintf(stderr
,
612 gettext("%s: Argument is too long\n"), cmdname
);
616 bcopy(FASTBOOT_MOUNTPOINT
, mountpoint
, sizeof (FASTBOOT_MOUNTPOINT
));
618 if (strstr(arg
, mountpoint
) == NULL
) {
620 * Do a force umount just in case some other filesystem has
621 * been mounted there.
623 (void) umount2(mountpoint
, MS_FORCE
);
626 /* Create the directory if it doesn't already exist */
627 if (lstat64(mountpoint
, &statbuf
) != 0) {
628 if (mkdirp(mountpoint
, 0755) != 0) {
629 (void) fprintf(stderr
,
630 gettext("Failed to create mountpoint %s\n"),
636 if (strncmp(arg
, root_dev_path
, strlen(root_dev_path
)) == 0) {
637 /* ufs root disk argument */
638 rc
= validate_ufs_disk(arg
, mountpoint
);
640 /* zfs root pool argument */
641 rc
= validate_zfs_pool(arg
, mountpoint
);
647 (void) snprintf(kernpath
, MAXPATHLEN
, "%s/platform/i86pc/kernel/unix",
650 if (stat64(kernpath
, &statbuf
) != 0) {
651 (void) fprintf(stderr
,
652 gettext("%s: %s is not a root disk or dataset\n"),
662 validate_ufs_disk(char *arg
, char *mountpoint
)
664 struct ufs_args ufs_args
= { 0 };
665 char mntopts
[MNT_LINE_MAX
] = MNTOPT_LARGEFILES
;
667 /* perform the mount */
668 ufs_args
.flags
= UFSMNT_LARGEFILES
;
669 if (mount(arg
, mountpoint
, MS_DATA
|MS_OPTIONSTR
,
670 MNTTYPE_UFS
, &ufs_args
, sizeof (ufs_args
),
671 mntopts
, sizeof (mntopts
)) != 0) {
673 (void) fprintf(stderr
,
674 gettext("%s: Failed to mount %s\n"), cmdname
, arg
);
682 validate_zfs_pool(char *arg
, char *mountpoint
)
684 zfs_handle_t
*zhp
= NULL
;
685 char mntopts
[MNT_LINE_MAX
] = { '\0' };
688 if ((g_zfs
= libzfs_init()) == NULL
) {
689 (void) fprintf(stderr
, gettext("Internal error: failed to "
690 "initialize ZFS library\n"));
694 /* Try to open the dataset */
695 if ((zhp
= zfs_open(g_zfs
, arg
,
696 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_DATASET
)) == NULL
)
699 /* perform the mount */
700 if (mount(zfs_get_name(zhp
), mountpoint
, MS_DATA
|MS_OPTIONSTR
|MS_RDONLY
,
701 MNTTYPE_ZFS
, NULL
, 0, mntopts
, sizeof (mntopts
)) != 0) {
703 (void) fprintf(stderr
,
704 gettext("%s: Failed to mount %s\n"), cmdname
, arg
);
708 validate_zfs_err_out
:
717 * Return 0 if not zfs, or is zfs and have successfully constructed the
718 * boot argument; returns non-zero otherwise.
719 * At successful completion fpth contains pointer where mount point ends.
720 * NOTE: arg is supposed to be the resolved path
723 get_zfs_bootfs_arg(const char *arg
, const char ** fpth
, int *is_zfs
,
726 zfs_handle_t
*zhp
= NULL
;
727 zpool_handle_t
*zpoolp
= NULL
;
730 char *poolname
= NULL
;
731 char physpath
[MAXPATHLEN
];
732 char mntsp
[ZFS_MAX_DATASET_NAME_LEN
];
733 char bootfs
[ZFS_MAX_DATASET_NAME_LEN
];
737 static char fmt
[] = "-B zfs-bootfs=%s,bootpath=\"%s\"";
742 bzero(physpath
, sizeof (physpath
));
743 bzero(bootfs
, sizeof (bootfs
));
745 if ((mtabp
= fopen(MNTTAB
, "r")) == NULL
) {
749 while (getmntent(mtabp
, &mnt
) == 0) {
750 if (strstr(arg
, mnt
.mnt_mountp
) == arg
&&
751 (msz
= strlen(mnt
.mnt_mountp
)) > mntlen
) {
753 *is_zfs
= strcmp(MNTTYPE_ZFS
, mnt
.mnt_fstype
) == 0;
754 (void) strlcpy(mntsp
, mnt
.mnt_special
, sizeof (mntsp
));
758 (void) fclose(mtabp
);
766 if ((g_zfs
= libzfs_init()) == NULL
)
769 /* Try to open the dataset */
770 if ((zhp
= zfs_open(g_zfs
, mntsp
,
771 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_DATASET
)) == NULL
) {
772 (void) fprintf(stderr
, gettext("Cannot open %s\n"), mntsp
);
774 goto validate_zfs_err_out
;
777 (void) strlcpy(bootfs
, mntsp
, sizeof (bootfs
));
779 if ((poolname
= strtok(mntsp
, "/")) == NULL
) {
781 goto validate_zfs_err_out
;
784 if ((zpoolp
= zpool_open(g_zfs
, poolname
)) == NULL
) {
785 (void) fprintf(stderr
, gettext("Cannot open %s\n"), poolname
);
787 goto validate_zfs_err_out
;
790 if (zpool_get_physpath(zpoolp
, physpath
, sizeof (physpath
)) != 0) {
791 (void) fprintf(stderr
, gettext("Cannot find phys_path\n"));
793 goto validate_zfs_err_out
;
797 * For the mirror physpath would contain the list of all
798 * bootable devices, pick up the first one.
800 (void) strtok(physpath
, " ");
801 if (snprintf(bootfs_arg
, BOOTARGS_MAX
, fmt
, bootfs
, physpath
) >=
804 (void) fprintf(stderr
,
805 gettext("Boot arguments are too long\n"));
808 validate_zfs_err_out
:
820 * Validate that the file exists, and is an ELF file.
821 * Returns 0 on success, -1 on failure.
824 validate_unix(char *arg
, int *mplen
, int *is_zfs
, char *bootfs_arg
)
826 const char *location
;
828 unsigned char ident
[EI_NIDENT
];
829 char physpath
[MAXPATHLEN
];
833 if ((sz
= resolvepath(arg
, physpath
, sizeof (physpath
) - 1)) ==
835 (void) fprintf(stderr
,
836 gettext("Cannot resolve path for %s: %s\n"),
837 arg
, strerror(errno
));
840 (void) strlcpy(arg
, physpath
, sz
+ 1);
842 if (strlen(arg
) > MAXPATHLEN
) {
843 (void) fprintf(stderr
,
844 gettext("%s: New kernel name is too long\n"), cmdname
);
848 if (strncmp(basename(arg
), "unix", 4) != 0) {
849 (void) fprintf(stderr
,
850 gettext("%s: %s: Kernel name must be unix\n"),
855 if (get_zfs_bootfs_arg(arg
, &location
, is_zfs
, bootfs_arg
) != 0)
858 *mplen
= location
- arg
;
860 if (strstr(location
, "/boot/platform") == location
) {
862 * Rebooting to failsafe.
863 * Clear bootfs_arg and is_zfs flag.
867 } else if (strstr(location
, "/platform") != location
) {
868 (void) fprintf(stderr
,
869 gettext("%s: %s: No /platform in file name\n"),
874 if ((elffd
= open64(arg
, O_RDONLY
)) < 0 ||
875 (pread64(elffd
, ident
, EI_NIDENT
, 0) != EI_NIDENT
)) {
876 (void) fprintf(stderr
, "%s: %s: %s\n",
877 cmdname
, arg
, strerror(errno
));
881 class = ident
[EI_CLASS
];
883 if ((class != ELFCLASS32
&& class != ELFCLASS64
) ||
884 memcmp(&ident
[EI_MAG0
], ELFMAG
, 4) != 0) {
885 (void) fprintf(stderr
,
886 gettext("%s: %s: Not a valid ELF file\n"), cmdname
, arg
);
890 format
= ident
[EI_DATA
];
892 if (format
!= CUR_ELFDATA
) {
893 (void) fprintf(stderr
, gettext("%s: %s: Invalid data format\n"),
909 halt_exec(const char *path
, ...)
916 const char *argv
[256];
918 if ((pid
= fork()) == -1) {
920 } else if (pid
== 0) {
921 (void) fclose(stdout
);
922 (void) fclose(stderr
);
930 arg
= va_arg(vp
, const char *);
932 } while (arg
!= NULL
&&
933 ++i
!= sizeof (argv
) / sizeof (argv
[0]));
937 (void) execve(path
, (char * const *)argv
, NULL
);
938 (void) fprintf(stderr
, gettext("Cannot execute %s: %s\n"),
939 path
, strerror(errno
));
942 if (waitpid(pid
, &st
, 0) == pid
&&
943 !WIFSIGNALED(st
) && WIFEXITED(st
))
944 st
= WEXITSTATUS(st
);
952 * Mount the specified BE.
954 * Upon success returns zero and copies bename string to mountpoint[]
957 fastboot_bename(const char *bename
, char *mountpoint
, size_t mpsz
)
962 * Attempt to unmount the BE first in case it's already mounted
965 (void) halt_exec(BEADM_PROG
, "umount", bename
, NULL
);
967 if ((rc
= halt_exec(BEADM_PROG
, "mount", bename
, FASTBOOT_MOUNTPOINT
,
969 (void) fprintf(stderr
,
970 gettext("%s: Unable to mount BE \"%s\" at %s\n"),
971 cmdname
, bename
, FASTBOOT_MOUNTPOINT
);
973 (void) strlcpy(mountpoint
, FASTBOOT_MOUNTPOINT
, mpsz
);
979 * Returns 0 on successful parsing of the arguments;
980 * returns EINVAL on parsing failures that should abort the reboot attempt;
981 * returns other error code to fall back to regular reboot.
984 parse_fastboot_args(char *bootargs_buf
, size_t buf_size
,
985 int *is_dryrun
, const char *bename
)
987 char mountpoint
[MAXPATHLEN
];
988 char bootargs_saved
[BOOTARGS_MAX
];
989 char bootargs_scratch
[BOOTARGS_MAX
];
990 char bootfs_arg
[BOOTARGS_MAX
];
991 char unixfile
[BOOTARGS_MAX
];
993 int buflen
; /* length of the bootargs_buf */
994 int mplen
; /* length of the mount point */
995 int rootlen
= 0; /* length of the root argument */
996 int unixlen
= 0; /* length of the unix argument */
997 int off
= 0; /* offset into the new boot argument */
1001 bzero(mountpoint
, sizeof (mountpoint
));
1004 * If argc is not 0, buflen is length of the argument being passed in;
1005 * else it is 0 as bootargs_buf has been initialized to all 0's.
1007 buflen
= strlen(bootargs_buf
);
1009 /* Save a copy of the original argument */
1010 bcopy(bootargs_buf
, bootargs_saved
, buflen
);
1011 bzero(&bootargs_saved
[buflen
], sizeof (bootargs_saved
) - buflen
);
1013 /* Save another copy to be used by strtok */
1014 bcopy(bootargs_buf
, bootargs_scratch
, buflen
);
1015 bzero(&bootargs_scratch
[buflen
], sizeof (bootargs_scratch
) - buflen
);
1016 head
= &bootargs_scratch
[0];
1018 /* Get the first argument */
1019 newarg
= strtok(bootargs_scratch
, " ");
1022 * If this is a dry run request, verify that the drivers can handle
1025 if (newarg
&& strncasecmp(newarg
, "dryrun", strlen("dryrun")) == 0) {
1027 (void) system("/usr/sbin/devfsadm");
1031 * Always perform a dry run to identify all the drivers that
1032 * need to implement devo_reset().
1034 if (uadmin(A_SHUTDOWN
, AD_FASTREBOOT_DRYRUN
,
1035 (uintptr_t)bootargs_saved
) != 0) {
1036 (void) fprintf(stderr
, gettext("%s: Not all drivers "
1037 "have implemented quiesce(9E)\n"
1038 "\tPlease see /var/adm/messages for drivers that haven't\n"
1039 "\timplemented quiesce(9E).\n"), cmdname
);
1040 } else if (*is_dryrun
) {
1041 (void) fprintf(stderr
, gettext("%s: All drivers have "
1042 "implemented quiesce(9E)\n"), cmdname
);
1045 /* Return if it is a true dry run. */
1050 /* Read boot args from Boot Environment */
1051 if ((bootargs_buf
[0] == 0 || isdigit(bootargs_buf
[0])) &&
1054 * If no boot arguments are given, or a BE entry
1055 * number is provided, process the boot arguments from BE.
1058 if (bootargs_buf
[0] == 0)
1059 entnum
= BE_ENTRY_DEFAULT
;
1062 entnum
= strtoul(bootargs_buf
, NULL
, 10);
1066 if (rc
== 0 && (rc
= be_get_boot_args(&fbarg
, entnum
)) == 0) {
1067 if (strlcpy(bootargs_buf
, fbarg
,
1068 buf_size
) >= buf_size
) {
1070 bcopy(bootargs_saved
, bootargs_buf
, buf_size
);
1074 /* Failed to read FB args, fall back to normal reboot */
1076 (void) fprintf(stderr
,
1077 gettext("%s: Failed to process boot "
1078 "arguments from Boot Environment.\n"), cmdname
);
1079 (void) fprintf(stderr
,
1080 gettext("%s: Falling back to regular reboot.\n"),
1084 /* No need to process further */
1086 fbarg_entnum
= entnum
;
1091 /* Zero out the boot argument buffer as we will reconstruct it */
1092 bzero(bootargs_buf
, buf_size
);
1093 bzero(bootfs_arg
, sizeof (bootfs_arg
));
1094 bzero(unixfile
, sizeof (unixfile
));
1096 if (bename
&& (rc
= fastboot_bename(bename
, mountpoint
,
1097 sizeof (mountpoint
))) != 0)
1102 * If BE is not specified, look for disk argument to construct
1103 * mountpoint; if BE has been specified, mountpoint has already been
1106 if (newarg
&& newarg
[0] != '-' && !bename
) {
1109 if ((tmprc
= validate_disk(newarg
, mountpoint
)) == 0) {
1111 * The first argument is a valid root argument.
1112 * Get the next argument.
1114 newarg
= strtok(NULL
, " ");
1115 rootlen
= (newarg
) ? (newarg
- head
) : buflen
;
1116 (void) strlcpy(fastboot_mounted
, mountpoint
,
1117 sizeof (fastboot_mounted
));
1119 } else if (tmprc
== -1) {
1121 * Not a disk argument. Use / as default root.
1123 bcopy("/", mountpoint
, 1);
1124 bzero(&mountpoint
[1], sizeof (mountpoint
) - 1);
1127 * Disk argument, but not valid or not root.
1135 * Make mountpoint the first part of unixfile.
1136 * If there is not disk argument, and BE has not been specified,
1137 * mountpoint could be empty.
1139 mplen
= strlen(mountpoint
);
1140 bcopy(mountpoint
, unixfile
, mplen
);
1143 * Look for unix argument
1145 if (newarg
&& newarg
[0] != '-') {
1146 bcopy(newarg
, &unixfile
[mplen
], strlen(newarg
));
1147 newarg
= strtok(NULL
, " ");
1148 rootlen
= (newarg
) ? (newarg
- head
) : buflen
;
1149 } else if (mplen
!= 0) {
1151 * No unix argument, but mountpoint is not empty, use
1152 * /platform/i86pc/$ISADIR/kernel/unix as default.
1156 if (sysinfo(SI_ARCHITECTURE_64
, isa
, sizeof (isa
)) != -1)
1157 (void) snprintf(&unixfile
[mplen
],
1158 sizeof (unixfile
) - mplen
,
1159 "/platform/i86pc/kernel/%s/unix", isa
);
1160 else if (sysinfo(SI_ARCHITECTURE_32
, isa
, sizeof (isa
)) != -1) {
1161 (void) snprintf(&unixfile
[mplen
],
1162 sizeof (unixfile
) - mplen
,
1163 "/platform/i86pc/kernel/unix");
1165 (void) fprintf(stderr
,
1166 gettext("%s: Unknown architecture"), cmdname
);
1172 * We now have the complete unix argument. Verify that it exists and
1173 * is an ELF file. Split the argument up into mountpoint and unix
1174 * portions again. This is necessary to handle cases where mountpoint
1175 * is specified on the command line as part of the unix argument,
1177 * # reboot -f /.alt/platform/i86pc/kernel/amd64/unix
1179 unixlen
= strlen(unixfile
);
1181 if (validate_unix(unixfile
, &mplen
, &is_zfs
,
1183 /* Not a valid unix file */
1188 * Construct boot argument.
1190 unixlen
= strlen(unixfile
);
1193 * mdep cannot start with space because bootadm
1194 * creates bogus menu entries if it does.
1197 bcopy(unixfile
, bootargs_buf
, mplen
);
1198 (void) strcat(bootargs_buf
, " ");
1201 bcopy(&unixfile
[mplen
], &bootargs_buf
[mplen
+ space
],
1203 (void) strcat(bootargs_buf
, " ");
1204 off
+= unixlen
+ space
+ 1;
1207 /* Check to see if root is zfs */
1209 (void) get_zfs_bootfs_arg("/", &dp
, &is_zfs
, bootfs_arg
);
1212 if (is_zfs
&& (buflen
!= 0 || bename
!= NULL
)) {
1213 /* do not copy existing zfs boot args */
1214 if (strstr(&bootargs_saved
[rootlen
], "-B") == NULL
||
1215 strstr(&bootargs_saved
[rootlen
], "zfs-bootfs=") == NULL
||
1216 (strstr(&bootargs_saved
[rootlen
], "bootpath=") == NULL
&&
1217 strstr(&bootargs_saved
[rootlen
], "diskdevid=") == NULL
))
1218 /* LINTED E_SEC_SPRINTF_UNBOUNDED_COPY */
1219 off
+= sprintf(bootargs_buf
+ off
, "%s ", bootfs_arg
);
1223 * Copy the rest of the arguments
1225 bcopy(&bootargs_saved
[rootlen
], &bootargs_buf
[off
], buflen
- rootlen
);
1233 do_archives_update(int do_fast_reboot
)
1237 char *cmd_argv
[MAXARGS
];
1240 cmd_argv
[i
++] = "/sbin/bootadm";
1241 cmd_argv
[i
++] = "-ea";
1242 cmd_argv
[i
++] = "update_all";
1244 cmd_argv
[i
++] = "fastboot";
1247 r
= posix_spawn(&pid
, cmd_argv
[0], NULL
, NULL
, cmd_argv
, NULL
);
1249 /* if posix_spawn fails we emit a warning and continue */
1252 (void) fprintf(stderr
, gettext("%s: WARNING, unable to start "
1253 "boot archive update\n"), cmdname
);
1255 while (waitpid(pid
, NULL
, 0) == -1 && errno
== EINTR
)
1260 main(int argc
, char *argv
[])
1262 int qflag
= 0, needlog
= 1, nosync
= 0;
1263 int fast_reboot
= 0;
1264 int prom_reboot
= 0;
1265 uintptr_t mdep
= NULL
;
1266 int cmd
, fcn
, c
, aval
, r
;
1268 const char *optstring
;
1269 zoneid_t zoneid
= getzoneid();
1270 int need_check_zones
= 0;
1271 char bootargs_buf
[BOOTARGS_MAX
];
1272 char *bootargs_orig
= NULL
;
1273 char *bename
= NULL
;
1275 const char * const resetting
= "/etc/svc/volatile/resetting";
1277 (void) setlocale(LC_ALL
, "");
1278 (void) textdomain(TEXT_DOMAIN
);
1280 cmdname
= basename(argv
[0]);
1282 if (strcmp(cmdname
, "halt") == 0) {
1283 (void) audit_halt_setup(argc
, argv
);
1284 optstring
= "dlnqy";
1285 usage
= gettext("usage: %s [ -dlnqy ]\n");
1288 } else if (strcmp(cmdname
, "poweroff") == 0) {
1289 (void) audit_halt_setup(argc
, argv
);
1290 optstring
= "dlnqy";
1291 usage
= gettext("usage: %s [ -dlnqy ]\n");
1294 } else if (strcmp(cmdname
, "reboot") == 0) {
1295 (void) audit_reboot_setup();
1297 optstring
= "dlnqpfe:";
1298 usage
= gettext("usage: %s [ -dlnq(p|fe:) ] [ boot args ]\n");
1300 optstring
= "dlnqfp";
1301 usage
= gettext("usage: %s [ -dlnq(p|f) ] [ boot args ]\n");
1306 (void) fprintf(stderr
,
1307 gettext("%s: not installed properly\n"), cmdname
);
1311 while ((c
= getopt(argc
, argv
, optstring
)) != EOF
) {
1314 if (zoneid
== GLOBAL_ZONEID
)
1317 (void) fprintf(stderr
,
1318 gettext("%s: -d only valid from global"
1319 " zone\n"), cmdname
);
1334 * Option ignored for backwards compatibility.
1351 * Don't translate the words "halt" or "reboot"
1353 (void) fprintf(stderr
, usage
, cmdname
);
1362 if (fcn
!= AD_BOOT
) {
1363 (void) fprintf(stderr
, usage
, cmdname
);
1367 /* Gather the arguments into bootargs_buf. */
1368 if (gather_args(argv
, bootargs_buf
, sizeof (bootargs_buf
)) !=
1370 (void) fprintf(stderr
,
1371 gettext("%s: Boot arguments too long.\n"), cmdname
);
1375 bootargs_orig
= strdup(bootargs_buf
);
1376 mdep
= (uintptr_t)bootargs_buf
;
1379 * Initialize it to 0 in case of fastboot, the buffer
1382 bzero(bootargs_buf
, sizeof (bootargs_buf
));
1385 if (geteuid() != 0) {
1386 (void) fprintf(stderr
,
1387 gettext("%s: permission denied\n"), cmdname
);
1391 if (fast_reboot
&& prom_reboot
) {
1392 (void) fprintf(stderr
,
1393 gettext("%s: -p and -f are mutually exclusive\n"),
1398 * Check whether fast reboot is the default operating mode
1400 if (fcn
== AD_BOOT
&& !fast_reboot
&& !prom_reboot
&&
1401 zoneid
== GLOBAL_ZONEID
) {
1402 fast_reboot
= scf_is_fastboot_default();
1406 if (bename
&& !fast_reboot
) {
1407 (void) fprintf(stderr
, gettext("%s: -e only valid with -f\n"),
1412 #if defined(__sparc)
1414 fast_reboot
= 2; /* need to distinguish each case */
1419 * If fast reboot, do some sanity check on the argument
1421 if (fast_reboot
== 1) {
1425 if (zoneid
!= GLOBAL_ZONEID
) {
1426 (void) fprintf(stderr
,
1427 gettext("%s: Fast reboot only valid from global"
1428 " zone\n"), cmdname
);
1432 rc
= parse_fastboot_args(bootargs_buf
, sizeof (bootargs_buf
),
1433 &is_dryrun
, bename
);
1436 * If dry run, or if arguments are invalid, return.
1440 else if (rc
== EINVAL
)
1446 * For all the other errors, we continue on in case user
1447 * user want to force fast reboot, or fall back to regular
1450 if (strlen(bootargs_buf
) != 0)
1451 mdep
= (uintptr_t)bootargs_buf
;
1454 #if 0 /* For debugging */
1456 (void) fprintf(stderr
, "mdep = %s\n", (char *)mdep
);
1460 char *user
= getlogin();
1464 openlog(cmdname
, 0, LOG_AUTH
);
1465 if (user
== NULL
&& (pw
= getpwuid(getuid())) != NULL
)
1473 syslog(LOG_CRIT
, "initiated by %s", user
);
1475 syslog(LOG_CRIT
, "initiated by %s on %s", user
, tty
);
1479 * We must assume success and log it before auditd is terminated.
1482 aval
= audit_reboot_success();
1484 aval
= audit_halt_success();
1487 (void) fprintf(stderr
,
1488 gettext("%s: can't turn off auditd\n"), cmdname
);
1490 (void) sleep(5); /* Give syslogd time to record this */
1493 (void) signal(SIGHUP
, SIG_IGN
); /* for remote connections */
1496 * We start to fork a bunch of zoneadms to halt any active zones.
1497 * This will proceed with halt in parallel until we call
1498 * check_zone_haltedness later on.
1500 if (zoneid
== GLOBAL_ZONEID
&& cmd
!= A_DUMP
) {
1501 need_check_zones
= halt_zones();
1505 /* set new default entry in the GRUB entry */
1506 if (fbarg_entnum
!= BE_ENTRY_DEFAULT
) {
1508 (void) snprintf(buf
, sizeof (buf
), "default=%u", fbarg_entnum
);
1509 (void) halt_exec(BOOTADM_PROG
, "set-menu", buf
, NULL
);
1513 /* if we're dumping, do the archive update here and don't defer it */
1514 if (cmd
== A_DUMP
&& zoneid
== GLOBAL_ZONEID
&& !nosync
)
1515 do_archives_update(fast_reboot
);
1518 * If we're not forcing a crash dump, mark the system as quiescing for
1519 * smf(5)'s benefit, and idle the init process.
1521 if (cmd
!= A_DUMP
) {
1522 if (direct_init(PCDSTOP
) == -1) {
1525 * Don't translate the word "init"
1527 (void) fprintf(stderr
,
1528 gettext("%s: can't idle init\n"), cmdname
);
1532 if (creat(resetting
, 0755) == -1)
1533 (void) fprintf(stderr
,
1534 gettext("%s: could not create %s.\n"),
1535 cmdname
, resetting
);
1539 * Make sure we don't get stopped by a jobcontrol shell
1540 * once we start killing everybody.
1542 (void) signal(SIGTSTP
, SIG_IGN
);
1543 (void) signal(SIGTTIN
, SIG_IGN
);
1544 (void) signal(SIGTTOU
, SIG_IGN
);
1545 (void) signal(SIGPIPE
, SIG_IGN
);
1546 (void) signal(SIGTERM
, SIG_IGN
);
1549 * Try to stop gdm so X has a chance to return the screen and
1550 * keyboard to a sane state.
1552 if (fast_reboot
== 1 && stop_gdm() != 0) {
1553 (void) fprintf(stderr
,
1554 gettext("%s: Falling back to regular reboot.\n"), cmdname
);
1556 mdep
= (uintptr_t)bootargs_orig
;
1557 } else if (bootargs_orig
) {
1558 free(bootargs_orig
);
1561 if (cmd
!= A_DUMP
) {
1563 * Stop all restarters so they do not try to restart services
1564 * that are terminated.
1569 * Wait a little while for zones to shutdown.
1571 if (need_check_zones
) {
1572 check_zones_haltedness();
1574 (void) fprintf(stderr
,
1575 gettext("%s: Completing system halt.\n"),
1581 * If we're not forcing a crash dump, give everyone 5 seconds to
1582 * handle a SIGTERM and clean up properly.
1584 if (cmd
!= A_DUMP
) {
1585 int start
, end
, delta
;
1587 (void) kill(-1, SIGTERM
);
1590 if (zoneid
== GLOBAL_ZONEID
&& !nosync
)
1591 do_archives_update(fast_reboot
);
1594 delta
= end
- start
;
1596 (void) sleep(5 - delta
);
1599 (void) signal(SIGINT
, SIG_IGN
);
1601 if (!qflag
&& !nosync
) {
1604 bzero(&wtmpx
, sizeof (struct utmpx
));
1605 (void) strcpy(wtmpx
.ut_line
, "~");
1606 (void) time(&wtmpx
.ut_tv
.tv_sec
);
1609 (void) strcpy(wtmpx
.ut_name
, "crash dump");
1611 (void) strcpy(wtmpx
.ut_name
, "shutdown");
1613 (void) updwtmpx(WTMPX_FILE
, &wtmpx
);
1617 if (cmd
== A_DUMP
&& nosync
!= 0)
1618 (void) uadmin(A_DUMP
, AD_NOSYNC
, NULL
);
1621 fcn
= AD_FASTREBOOT
;
1623 if (uadmin(cmd
, fcn
, mdep
) == -1)
1624 (void) fprintf(stderr
, "%s: uadmin failed: %s\n",
1625 cmdname
, strerror(errno
));
1627 (void) fprintf(stderr
, "%s: uadmin unexpectedly returned 0\n",
1631 r
= remove(resetting
);
1632 } while (r
!= 0 && errno
== EINTR
);
1634 if (r
!= 0 && errno
!= ENOENT
)
1635 (void) fprintf(stderr
, gettext("%s: could not remove %s.\n"),
1636 cmdname
, resetting
);
1638 if (direct_init(PCRUN
) == -1) {
1641 * Don't translate the word "init"
1643 (void) fprintf(stderr
,
1644 gettext("%s: can't resume init\n"), cmdname
);
1647 continue_restarters();
1649 if (get_initpid() != -1)
1650 /* tell init to restate current level */
1651 (void) kill(get_initpid(), SIGHUP
);
1655 (void) audit_reboot_fail();
1657 (void) audit_halt_fail();
1659 if (fast_reboot
== 1) {
1661 (void) halt_exec(BEADM_PROG
, "umount", bename
, NULL
);
1663 } else if (strlen(fastboot_mounted
) != 0) {
1664 (void) umount(fastboot_mounted
);