From 587f2f927e9b404305b490364c320b49124dcee4 Mon Sep 17 00:00:00 2001 From: Greg Haerr Date: Thu, 5 Sep 2024 10:10:56 -0700 Subject: [PATCH] [cmds] Add -s, -r, and -p options to shutdown, remove reboot, poweroff --- elkscmd/Applications | 4 +-- elkscmd/man/man8/poweroff.8 | 26 ------------------- elkscmd/man/man8/reboot.8 | 23 ----------------- elkscmd/man/man8/shutdown.8 | 20 +++++++++++---- elkscmd/sys_utils/shutdown.c | 50 ++++++++++++++++++++++++++++-------- 5 files changed, 57 insertions(+), 66 deletions(-) delete mode 100644 elkscmd/man/man8/poweroff.8 delete mode 100644 elkscmd/man/man8/reboot.8 diff --git a/elkscmd/Applications b/elkscmd/Applications index dc3bda54b..e9d44ab18 100644 --- a/elkscmd/Applications +++ b/elkscmd/Applications @@ -92,8 +92,8 @@ sys_utils/man :sysutil :1200k sys_utils/meminfo :sash :sysutil :360k :128k sys_utils/mouse :sysutil :1200c :1440k sys_utils/passwd :sysutil :1200k -sys_utils/shutdown :::reboot :sysutil :720k :128k -sys_utils/shutdown :::poweroff :sysutil :720k +#sys_utils/shutdown :::reboot :sysutil :720k :128k +#sys_utils/shutdown :::poweroff :sysutil :720k sys_utils/sercat :sysutil :1400c sys_utils/console :sysutil :1200k #sys_utils/who :sysutil :1200k diff --git a/elkscmd/man/man8/poweroff.8 b/elkscmd/man/man8/poweroff.8 deleted file mode 100644 index e8753bc9f..000000000 --- a/elkscmd/man/man8/poweroff.8 +++ /dev/null @@ -1,26 +0,0 @@ -.TH POWEROFF 8 -.SH NAME -poweroff \- power off the system immediately -.SH SYNOPSIS -.B poweroff -.RB [ -f ] -.SS OPTIONS -.TP 5 -.B \-f -Force poweroff even if root volume cannot be remounted read-only. -.SH DESCRIPTION -.B Poweroff -is a program that allows the superuser to power off the system immediately. -A -.B sync -and unmount of the root filesystem and all other mounted filesystems -are performed, and after 3 seconds have elapsed, the system is powered off. -.SH BUGS -Not all systems support powering off, in which case the system -will remain on but halted. -.SH "SEE ALSO" -.BR sync (1), -.BR unmount (1), -.BR reboot (8), -.BR shutdown (8), -.BR reboot (2). diff --git a/elkscmd/man/man8/reboot.8 b/elkscmd/man/man8/reboot.8 deleted file mode 100644 index 02262d5d1..000000000 --- a/elkscmd/man/man8/reboot.8 +++ /dev/null @@ -1,23 +0,0 @@ -.TH REBOOT 8 -.SH NAME -reboot \- reboot the system immediately -.SH SYNOPSIS -.B reboot -.RB [ -f ] -.SS OPTIONS -.TP 5 -.B \-f -Force reboot even if root volume cannot be remounted read-only. -.SH DESCRIPTION -.B Reboot -is a program that allows the superuser to shut down and reboot the system immediately. -A -.B sync -and unmount of the root filesystem and all other mounted filesystems -are performed, and after 3 seconds have elapsed, the system is rebooted. -.SH "SEE ALSO" -.BR sync (1), -.BR unmount (1), -.BR shutdown (8), -.BR poweroff (8), -.BR reboot (2). diff --git a/elkscmd/man/man8/shutdown.8 b/elkscmd/man/man8/shutdown.8 index 8dcfde235..fb14b64c8 100644 --- a/elkscmd/man/man8/shutdown.8 +++ b/elkscmd/man/man8/shutdown.8 @@ -1,13 +1,22 @@ .TH SHUTDOWN 8 .SH NAME -shutdown \- shut down the system quickly +shutdown \- shut down, power off or reboot the system quickly .SH SYNOPSIS .B shutdown -.RB [ -f ] +.RB [ -s ][ -r ][ -p ][ -f ] .SS OPTIONS .TP 5 +.B \-s +Perform shut down operation and halt the system (default action). +.TP 5 +.B \-r +Reboot the system. +.TP 5 +.B \-p +Power off the system. +.TP 5 .B \-f -Force shutdown even if root volume cannot be remounted read-only. +Continue even if root volume cannot be remounted read-only. .SH DESCRIPTION .B Shutdown is a program that allows the superuser to shut down the system immediately. @@ -15,9 +24,10 @@ A .B sync and unmount of the root filesystem and all other mounted filesystems are performed, and after 3 seconds have elapsed, the system is halted. +.SH BUGS +Not all systems support powering off, in which case the system +will remain on but halted. .SH "SEE ALSO" .BR sync (1), .BR unmount (1), -.BR reboot (8), -.BR poweroff (8), .BR reboot (2). diff --git a/elkscmd/sys_utils/shutdown.c b/elkscmd/sys_utils/shutdown.c index 6d6f7fc58..b07e5d666 100644 --- a/elkscmd/sys_utils/shutdown.c +++ b/elkscmd/sys_utils/shutdown.c @@ -1,21 +1,33 @@ /* * reboot/shutdown/poweroff * - * Original version from + * Usage: {shutdown,reboot,poweroff} [-s][-r][-p][-f] + * + * Completely rewritten from original version of * Copyright 2000 Alistair Riddoch * ajr@ecs.soton.ac.uk - * * This file may be distributed under the terms of the GNU General Public * License v2, or at your option any later version. */ #include #include +#include #include #include #include #include +#define errmsg(str) write(STDERR_FILENO, str, sizeof(str) - 1) + +static int f_flag; /* continue even if unmounts fail */ + +static void usage(void) +{ + errmsg("Usage: shutdown [-s][-r][-p][-f]\n"); + exit(1); +} + static int try_unmount(dev_t dev) { struct statfs statfs; @@ -32,25 +44,43 @@ static int try_unmount(dev_t dev) int main(int argc, char **argv) { - int i, force, flag, ret; - char *progname; + char *p, *progname; + int i, how; - force = (argc >= 2 && argv[1][0] == '-' && argv[1][1] == 'f'); if ((progname = strrchr(argv[0], '/')) != NULL) progname++; else progname = argv[0]; - flag = !strcmp(progname, "reboot")? RB_REBOOT : + how = !strcmp(progname, "reboot")? RB_REBOOT : !strcmp(progname, "poweroff")? RB_POWEROFF: RB_SHUTDOWN; + while (argv[1] && argv[1][0] == '-') { + for (p = &argv[1][1]; *p; p++) { + switch (*p) { + case 's': + how = RB_SHUTDOWN; break; + case 'r': + how = RB_REBOOT; break; + case 'p': + how = RB_POWEROFF; break; + case 'f': + f_flag = 1; break; + default: + usage(); + } + } + argv++; + argc--; + } + if (argc > 1) usage(); + sync(); for (i = NR_SUPER - 1; i >= 0; --i) { - ret = try_unmount(i); - if (ret && !force) /* -f forces reboot even if mount fails */ - return 1; + if (try_unmount(i) && !f_flag) + return 1; /* stop on failed unmount unless -f specified */ } sleep(2); - if (reboot(0, 0, flag)) { + if (reboot(0, 0, how)) { perror(progname); return 1; }