git: 16f17042683b - stable/14 - shutdown(8): refuse to run if /var/run/noshutdown is present

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Sat, 10 May 2025 19:33:16 UTC
The branch stable/14 has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=16f17042683bca6ae46d668bcc30e416f88cc42a

commit 16f17042683bca6ae46d668bcc30e416f88cc42a
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2025-05-04 13:39:32 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2025-05-10 18:57:46 +0000

    shutdown(8): refuse to run if /var/run/noshutdown is present
    
    (cherry picked from commit 7fb88c20eccc3fd2118fda2ba58d7afe2b87f7e3)
---
 sbin/shutdown/shutdown.8 | 23 +++++++++++++++++++++--
 sbin/shutdown/shutdown.c | 18 +++++++++++++++---
 2 files changed, 36 insertions(+), 5 deletions(-)

diff --git a/sbin/shutdown/shutdown.8 b/sbin/shutdown/shutdown.8
index 7f60f22cc0e3..1b8b61df977e 100644
--- a/sbin/shutdown/shutdown.8
+++ b/sbin/shutdown/shutdown.8
@@ -38,7 +38,7 @@
 .Nm
 .Op Fl
 .Oo
-.Fl c | Fl h | Fl p |
+.Fl c | Fl f | Fl h | Fl p |
 .Fl r | Fl k
 .Oc
 .Oo
@@ -72,6 +72,12 @@ At the present time, only systems with BMC supported by the
 driver that implement this functionality support this flag.
 The amount of time the system is off is dependent on the device
 that implements this feature.
+.It Fl f
+The
+.Nm
+command ignores the presence of the
+.Pa /var/run/noshutdown
+file.
 .It Fl h
 The system is halted at the specified
 .Ar time .
@@ -201,6 +207,12 @@ file that
 .Nm
 created will be removed automatically.
 .Pp
+If the
+.Pa /var/run/noshutdown
+file is present,
+.Nm
+exits without executing any action on the system.
+.Pp
 When run without options, the
 .Nm
 utility will place the system into single user mode at the
@@ -214,11 +226,18 @@ is equivalent to running:
 shutdown -p now
 .Ed
 .Sh FILES
-.Bl -tag -width /var/run/nologin -compact
+.Bl -tag -width /var/run/noshutdown -compact
 .It Pa /var/run/nologin
 tells
 .Xr login 1
 not to let anyone log in
+.It Pa /var/run/noshutdown
+prevents
+.Nm
+from initiating an action on the system.
+Can be overridden with the
+.Fl f
+option.
 .El
 .Sh EXAMPLES
 Reboot the system in 30 minutes and display a warning message on the terminals
diff --git a/sbin/shutdown/shutdown.c b/sbin/shutdown/shutdown.c
index 3864e44025eb..624f17cb366e 100644
--- a/sbin/shutdown/shutdown.c
+++ b/sbin/shutdown/shutdown.c
@@ -44,6 +44,7 @@ static char sccsid[] = "@(#)shutdown.c	8.4 (Berkeley) 4/28/95";
 #include <sys/param.h>
 #include <sys/boottrace.h>
 #include <sys/resource.h>
+#include <sys/stat.h>
 #include <sys/syslog.h>
 #include <sys/time.h>
 
@@ -90,7 +91,8 @@ static struct interval {
 #undef S
 
 static time_t offset, shuttime;
-static int docycle, dohalt, dopower, doreboot, killflg, mbuflen, oflag;
+static int docycle, dohalt, dopower, doreboot, ign_noshutdown,
+    killflg, mbuflen, oflag;
 static char mbuf[BUFSIZ];
 static const char *nosync, *whom;
 
@@ -111,6 +113,7 @@ main(int argc, char **argv)
 {
 	char *p, *endp;
 	struct passwd *pw;
+	struct stat st;
 	int arglen, ch, len, readstdin;
 
 #ifndef DEBUG
@@ -142,7 +145,7 @@ main(int argc, char **argv)
 		goto poweroff;
 	}
 
-	while ((ch = getopt(argc, argv, "-chknopr")) != -1)
+	while ((ch = getopt(argc, argv, "-cfhknopr")) != -1)
 		switch (ch) {
 		case '-':
 			readstdin = 1;
@@ -150,6 +153,9 @@ main(int argc, char **argv)
 		case 'c':
 			docycle = 1;
 			break;
+		case 'f':
+			ign_noshutdown = 1;
+			break;
 		case 'h':
 			dohalt = 1;
 			break;
@@ -220,6 +226,12 @@ poweroff:
 	}
 	mbuflen = strlen(mbuf);
 
+	if (!ign_noshutdown && stat(_PATH_NOSHUTDOWN, &st) == 0) {
+		(void)printf("Shutdown cannot be done, " _PATH_NOSHUTDOWN
+		    " is present\n");
+		exit(2);
+	}
+
 	if (offset) {
 		BOOTTRACE("Shutdown at %s", ctime(&shuttime));
 		(void)printf("Shutdown at %.24s.\n", ctime(&shuttime));
@@ -596,7 +608,7 @@ usage(const char *cp)
 	if (cp != NULL)
 		warnx("%s", cp);
 	(void)fprintf(stderr,
-	    "usage: shutdown [-] [-c | -h | -p | -r | -k] [-o [-n]] time [warning-message ...]\n"
+	    "usage: shutdown [-] [-c | -f | -h | -p | -r | -k] [-o [-n]] time [warning-message ...]\n"
 	    "       poweroff\n");
 	exit(1);
 }