Skip to content

Commit

Permalink
Merge pull request #2000 from ghaerr/shutdown
Browse files Browse the repository at this point in the history
[cmds] Add -s, -r, and -p options to shutdown, remove reboot, poweroff
  • Loading branch information
ghaerr authored Sep 5, 2024
2 parents 3def215 + 587f2f9 commit a4f544f
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 66 deletions.
4 changes: 2 additions & 2 deletions elkscmd/Applications
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 0 additions & 26 deletions elkscmd/man/man8/poweroff.8

This file was deleted.

23 changes: 0 additions & 23 deletions elkscmd/man/man8/reboot.8

This file was deleted.

20 changes: 15 additions & 5 deletions elkscmd/man/man8/shutdown.8
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
.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.
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).
50 changes: 40 additions & 10 deletions elkscmd/sys_utils/shutdown.c
Original file line number Diff line number Diff line change
@@ -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
* [email protected]
*
* This file may be distributed under the terms of the GNU General Public
* License v2, or at your option any later version.
*/

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mount.h>
#include <linuxmt/limits.h>
#include <arch/system.h>

#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;
Expand All @@ -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;
}
Expand Down

0 comments on commit a4f544f

Please sign in to comment.