git: 6c7ec630c24f - main - shutdown: add a -q(uiet) flag to suppress warning messages

From: Kyle Evans <kevans_at_FreeBSD.org>
Date: Fri, 09 Aug 2024 16:01:19 UTC
The branch main has been updated by kevans:

URL: https://cgit.FreeBSD.org/src/commit/?id=6c7ec630c24fded12e237b5ffda115cc268b75c2

commit 6c7ec630c24fded12e237b5ffda115cc268b75c2
Author:     Kyle Evans <kevans@FreeBSD.org>
AuthorDate: 2024-08-09 16:01:11 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2024-08-09 16:01:11 +0000

    shutdown: add a -q(uiet) flag to suppress warning messages
    
    The system-wide warning makes sense in multi-user environments, but
    shutdown(8) may be used on systems or in scenarios where there's nobody
    to warn and wall(1) just introduces unnecessary complexity and overhead
    to the shutdown process.
    
    Add an option to suppress the warning entirely for those contexts that
    want to do so, which are anticipated to mainly be in appliance or
    single-user desktop-style systems.
    
    Reviewed by:    des
    Reviewed by:    allanjude, imp (earlier version)
    Sponsored by:   Klara, Inc.
    Differential Revision:  https://reviews.freebsd.org/D46216
---
 sbin/shutdown/shutdown.8 |  9 ++++++++-
 sbin/shutdown/shutdown.c | 23 ++++++++++++++++-------
 2 files changed, 24 insertions(+), 8 deletions(-)

diff --git a/sbin/shutdown/shutdown.8 b/sbin/shutdown/shutdown.8
index 70df537b8bf5..ab90af6244e9 100644
--- a/sbin/shutdown/shutdown.8
+++ b/sbin/shutdown/shutdown.8
@@ -25,7 +25,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.Dd November 7, 2022
+.Dd August 4, 2024
 .Dt SHUTDOWN 8
 .Os
 .Sh NAME
@@ -43,6 +43,7 @@
 .Fl o
 .Op Fl n
 .Oc
+.Op Fl q
 .Ar time
 .Op Ar warning-message ...
 .Nm poweroff
@@ -113,6 +114,12 @@ to
 or
 .Xr reboot 8 .
 This option should probably not be used.
+.It Fl q
+Shut down quietly.
+Suppress the warning message to all logged in users about system shutdown.
+It is an error to supply a
+.Ar warning-message
+when warnings are suppressed.
 .It Ar time
 .Ar Time
 is the time at which
diff --git a/sbin/shutdown/shutdown.c b/sbin/shutdown/shutdown.c
index 80a9e14fc8d3..101e003b44e2 100644
--- a/sbin/shutdown/shutdown.c
+++ b/sbin/shutdown/shutdown.c
@@ -43,6 +43,7 @@
 #include <pwd.h>
 #include <setjmp.h>
 #include <signal.h>
+#include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -86,7 +87,7 @@ static void badtime(void);
 static void die_you_gravy_sucking_pig_dog(void);
 static void finish(int);
 static void getoffset(char *);
-static void loop(void);
+static void loop(bool);
 static void nolog(void);
 static void timeout(int);
 static void timewarn(int);
@@ -100,12 +101,14 @@ main(int argc, char **argv)
 	char *p, *endp;
 	struct passwd *pw;
 	int arglen, ch, len, readstdin;
+	bool dowarn;
 
 #ifndef DEBUG
 	if (geteuid())
 		errx(1, "NOT super-user");
 #endif
 
+	dowarn = true;
 	nosync = NULL;
 	readstdin = 0;
 
@@ -130,7 +133,7 @@ main(int argc, char **argv)
 		goto poweroff;
 	}
 
-	while ((ch = getopt(argc, argv, "-chknopr")) != -1)
+	while ((ch = getopt(argc, argv, "-chknoprq")) != -1)
 		switch (ch) {
 		case '-':
 			readstdin = 1;
@@ -156,6 +159,9 @@ main(int argc, char **argv)
 		case 'r':
 			doreboot = 1;
 			break;
+		case 'q':
+			dowarn = false;
+			break;
 		case '?':
 		default:
 			usage((char *)NULL);
@@ -178,6 +184,8 @@ main(int argc, char **argv)
 	getoffset(*argv++);
 
 poweroff:
+	if (!dowarn && *argv != NULL)
+		usage("warning-message supplied but suppressed with -q");
 	if (*argv) {
 		for (p = mbuf, len = sizeof(mbuf); *argv; ++argv) {
 			arglen = strlen(*argv);
@@ -235,12 +243,12 @@ poweroff:
 	setsid();
 #endif
 	openlog("shutdown", LOG_CONS, LOG_AUTH);
-	loop();
+	loop(dowarn);
 	return(0);
 }
 
 static void
-loop(void)
+loop(bool dowarn)
 {
 	struct interval *tp;
 	u_int sltime;
@@ -263,13 +271,14 @@ loop(void)
 		 * the next wait time.
 		 */
 		if ((sltime = offset - tp->timeleft)) {
-			if (sltime > (u_int)(tp->timetowait / 5))
+			if (dowarn && sltime > (u_int)(tp->timetowait / 5))
 				timewarn(offset);
 			(void)sleep(sltime);
 		}
 	}
 	for (;; ++tp) {
-		timewarn(tp->timeleft);
+		if (dowarn)
+			timewarn(tp->timeleft);
 		if (!logged && tp->timeleft <= NOLOG_TIME) {
 			logged = 1;
 			nolog();
@@ -584,7 +593,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 | -h | -p | -r | -k] [-o [-n]] [-q] time [warning-message ...]\n"
 	    "       poweroff\n");
 	exit(1);
 }