Skip to content

Commit

Permalink
Add shutdown and poweroff into reboot.c
Browse files Browse the repository at this point in the history
  • Loading branch information
ghaerr committed Sep 5, 2024
1 parent 0faa69a commit 400df2c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 34 deletions.
8 changes: 5 additions & 3 deletions elks/include/arch/system.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#ifndef __ARCH_8086_SYSTEM_H
#define __ARCH_8086_SYSTEM_H

#include <linuxmt/types.h>
#include <linuxmt/init.h>

/* sys_reboot flag parameter */
#define RB_REBOOT 0x0123 /* hard reset */
#define RB_SHUTDOWN 0x6789 /* halt system */
#define RB_POWEROFF 0xDEAD /* call BIOS APM */

#ifdef __KERNEL__
#include <linuxmt/types.h>
#include <linuxmt/init.h>

extern byte_t sys_caps; /* system capabilities bits*/
extern seg_t membase; /* start and end segment of available main memory */
extern seg_t memend;
Expand All @@ -17,5 +18,6 @@ unsigned int INITPROC setup_arch(void);
void ctrl_alt_del(void);
void hard_reset_now(void);
void apm_shutdown_now(void);
#endif

#endif
68 changes: 37 additions & 31 deletions elkscmd/sys_utils/reboot.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* reboot.c
* reboot/shutdown/poweroff
*
* Original version from
* Copyright 2000 Alistair Riddoch
* [email protected]
*
Expand All @@ -9,44 +10,49 @@
*/

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

int try_unmount(dev_t dev)
static int try_unmount(dev_t dev)
{
struct statfs statfs;
if (ustatfs(dev, &statfs, UF_NOFREESPACE) < 0) {
return 0;
}
if (umount(statfs.f_mntonname)) {
perror("umount");
return 1;
}
return 0;
struct statfs statfs;

if (ustatfs(dev, &statfs, UF_NOFREESPACE) < 0) {
return 0;
}
if (umount(statfs.f_mntonname)) {
perror("umount");
return 1;
}
return 0;
}

int main(int argc, char **argv)
{
int i, ret;
int force = 1;
if (argc < 2 || argv[1][0] != '-' || argv[1][1] != 'f')
force = 0;
int i, force, flag, ret;
char *progname;

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 :
!strcmp(progname, "poweroff")? RB_POWEROFF:
RB_SHUTDOWN;

sync();
for (i = NR_SUPER - 1; i >= 0; --i) {
ret = try_unmount(i);
/* -f forces reboot even if mount fails */
if (ret && !force) return 1;
}
sleep(3);
if (reboot(0x1D1E,0xC0DE,0x0123)) {
perror("reboot");
return 1;
}
return 0;
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;
}
sleep(2);
if (reboot(0, 0, flag)) {
perror(progname);
return 1;
}
return 0;
}

0 comments on commit 400df2c

Please sign in to comment.