git: 994cc8392123 - main - reboot: Allow this to be installed as nextboot

From: Warner Losh <imp_at_FreeBSD.org>
Date: Mon, 12 Feb 2024 18:53:46 UTC
The branch main has been updated by imp:

URL: https://cgit.FreeBSD.org/src/commit/?id=994cc839212393cd2e591a6a9908ee9a94fdb18f

commit 994cc839212393cd2e591a6a9908ee9a94fdb18f
Author:     Warner Losh <imp@FreeBSD.org>
AuthorDate: 2024-02-12 18:46:11 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2024-02-12 18:46:11 +0000

    reboot: Allow this to be installed as nextboot
    
    Allow nextboot to be a symlink link to reboot. It does everything reboot
    does, except doesn't actually setup the sytem to reboot and reboot. Also,
    don't accept the reboot args related to rebooting when in nextboot mode.
    
    Sponsored by:           Netflix
    Reviewed by:            kib
    Differential Revision:  https://reviews.freebsd.org/D43830
---
 sbin/reboot/reboot.c | 34 ++++++++++++++++++++++++++--------
 1 file changed, 26 insertions(+), 8 deletions(-)

diff --git a/sbin/reboot/reboot.c b/sbin/reboot/reboot.c
index 21acf7514388..a31c31892745 100644
--- a/sbin/reboot/reboot.c
+++ b/sbin/reboot/reboot.c
@@ -56,6 +56,7 @@ static void usage(void) __dead2;
 static uint64_t get_pageins(void);
 
 static bool dohalt;
+static bool donextboot;
 
 #define E(...) do {				\
 		if (force) {			\
@@ -163,6 +164,12 @@ add_env(char **env, const char *key, const char *value)
 	free(oldenv);
 }
 
+/*
+ * Different options are valid for different programs.
+ */
+#define GETOPT_REBOOT "cDde:k:lNno:pqr"
+#define GETOPT_NEXTBOOT "De:k:o:"
+
 int
 main(int argc, char *argv[])
 {
@@ -171,16 +178,20 @@ main(int argc, char *argv[])
 	int ch, howto, i, sverrno;
 	bool Dflag, fflag, lflag, Nflag, nflag, qflag;
 	uint64_t pageins;
-	const char *user, *kernel = NULL;
+	const char *user, *kernel = NULL, *getopts = GETOPT_REBOOT;
 	char *env = NULL, *v;
 
 	if (strstr(getprogname(), "halt") != NULL) {
 		dohalt = true;
 		howto = RB_HALT;
-	} else
+	} else if (strcmp(getprogname(), "nextboot") == 0) {
+		donextboot = true;
+		getopts = GETOPT_NEXTBOOT; /* Note: reboot's extra opts return '?' */
+	} else {
 		howto = 0;
+	}
 	Dflag = fflag = lflag = Nflag = nflag = qflag = false;
-	while ((ch = getopt(argc, argv, "cDde:k:lNno:pqr")) != -1)
+	while ((ch = getopt(argc, argv, getopts)) != -1) {
 		switch(ch) {
 		case 'c':
 			howto |= RB_POWERCYCLE;
@@ -228,6 +239,8 @@ main(int argc, char *argv[])
 		default:
 			usage();
 		}
+	}
+
 	argc -= optind;
 	argv += optind;
 	if (argc != 0)
@@ -245,10 +258,6 @@ main(int argc, char *argv[])
 		errx(1, "-r cannot be used with -c, -d, -n, or -p");
 	if ((howto & RB_REROOT) != 0 && kernel != NULL)
 		errx(1, "-r and -k cannot be used together, there is no next kernel");
-	if (geteuid()) {
-		errno = EPERM;
-		err(1, NULL);
-	}
 
 	if (Dflag) {
 		if (unlink(PATH_NEXTBOOT) != 0)
@@ -256,6 +265,11 @@ main(int argc, char *argv[])
 		exit(0);
 	}
 
+	if (!donextboot && geteuid() != 0) {
+		errno = EPERM;
+		err(1, NULL);
+	}
+
 	if (qflag) {
 		reboot(howto);
 		err(1, NULL);
@@ -278,7 +292,11 @@ main(int argc, char *argv[])
 		add_env(&env, "kernel", kernel);
 	}
 
-	write_nextboot(PATH_NEXTBOOT, env, fflag);
+	if (env != NULL)
+		write_nextboot(PATH_NEXTBOOT, env, fflag);
+	if (donextboot)
+		exit (0);
+
 	/* Log the reboot. */
 	if (!lflag)  {
 		if ((user = getlogin()) == NULL)