Skip to content

Commit

Permalink
Merge pull request #1999 from ghaerr/reboot
Browse files Browse the repository at this point in the history
[kernel] Consolidate reboot, shutdown and poweroff into single program
  • Loading branch information
ghaerr authored Sep 5, 2024
2 parents 0d262da + b8f770c commit 3def215
Show file tree
Hide file tree
Showing 15 changed files with 115 additions and 233 deletions.
10 changes: 10 additions & 0 deletions elks/arch/i86/kernel/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <linuxmt/debug.h>

#include <arch/segment.h>
#include <arch/system.h>
#include <arch/io.h>

seg_t membase, memend; /* start and end segment of available main memory */
Expand Down Expand Up @@ -83,6 +84,15 @@ unsigned int INITPROC setup_arch(void)
* The following routines may need porting on non-IBM PC architectures
*/

/*
* This function gets called by the keyboard interrupt handler.
* As it's called within an interrupt, it may NOT sync.
*/
void ctrl_alt_del(void)
{
hard_reset_now();
}

void hard_reset_now(void)
{
#ifdef CONFIG_ARCH_IBMPC
Expand Down
14 changes: 11 additions & 3 deletions elks/include/arch/system.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
#ifndef __ARCH_8086_SYSTEM_H
#define __ARCH_8086_SYSTEM_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;

extern unsigned int INITPROC setup_arch(void);
extern void hard_reset_now(void);
extern void apm_shutdown_now(void);
unsigned int INITPROC setup_arch(void);
void ctrl_alt_del(void);
void hard_reset_now(void);
void apm_shutdown_now(void);
#endif

#endif
10 changes: 3 additions & 7 deletions elks/include/linuxmt/signal.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,6 @@ typedef unsigned char __sigdisposition_t;
#define SIGDISP_CUSTOM ((__sigdisposition_t) 2)
#endif

/*@end@*/

struct __kern_sigaction_struct {
__sigdisposition_t sa_dispose;
#if UNUSED
Expand All @@ -234,11 +232,9 @@ struct __kern_sigaction_struct {

#ifdef __KERNEL__
struct task_struct;
extern int send_sig(sig_t,struct task_struct *,int);
extern void arch_setup_sighandler_stack(register struct task_struct *,
__kern_sighandler_t,unsigned);
extern void ctrl_alt_del(void);
extern int sys_kill(pid_t, sig_t);
int send_sig(sig_t,struct task_struct *,int);
void arch_setup_sighandler_stack(struct task_struct *, __kern_sighandler_t,unsigned);
int sys_kill(pid_t, sig_t);
#endif /* __KERNEL__*/

#endif
79 changes: 21 additions & 58 deletions elks/kernel/sys.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,79 +17,42 @@
#include <linuxmt/debug.h>

#include <arch/segment.h>
#include <arch/system.h>
#include <arch/io.h>

/*
* Indicates whether you can reboot with ctrl-alt-del: the default is yes
*/

static int C_A_D = 1;

/*
* Reboot system call: for obvious reasons only root may call it, and even
* root needs to set up some magic numbers in the registers so that some
* mistake won't make this reboot the whole machine.
*
* You can also set the meaning of the ctrl-alt-del-key here.
*
* reboot doesn't sync: do that yourself before calling this.
*/

int sys_reboot(unsigned int magic, unsigned int magic_too, int flag)
{
if (!suser())
return -EPERM;
return -EPERM;

if (magic == 0x1D1E && magic_too == 0xC0DE) {
switch(flag) {
case 0x4567:
flag = 1;
/* fall through*/
case 0:
C_A_D = flag;
return 0;
case 0x0123: /* reboot*/
#ifdef CONFIG_BLK_DEV_BHD
bios_disk_park_all();
#endif
hard_reset_now();
printk("Reboot failed\n");
/* fall through*/
case 0x6789: /* shutdown*/
sys_kill(1, SIGKILL);
sys_kill(-1, SIGKILL);
#ifdef CONFIG_BLK_DEV_BHD
bios_disk_park_all();
bios_disk_park_all();
#endif
printk("System halted\n");
do_exit(0);
/* no return*/
case 0xDEAD: /* poweroff*/
#ifdef CONFIG_BLK_DEV_BHD
bios_disk_park_all();
#endif
apm_shutdown_now();
printk("APM shutdown failed\n");
}
switch(flag) {
case RB_REBOOT:
hard_reset_now();
printk("Reboot failed\n");
/* fall through */

case RB_SHUTDOWN:
shutdown:
sys_kill(1, SIGKILL);
sys_kill(-1, SIGKILL);
printk("System halted\n");
do_exit(0);
/* no return*/

case RB_POWEROFF:
apm_shutdown_now();
printk("Poweroff failed\n");
goto shutdown;
}

return -EINVAL;
}

/*
* This function gets called by ctrl-alt-del - ie the keyboard interrupt.
* As it's called within an interrupt, it may NOT sync: the only choice
* is whether to reboot at once, or just ignore the ctrl-alt-del.
*/

void ctrl_alt_del(void)
{
if (C_A_D)
hard_reset_now();

kill_process(1, (sig_t) SIGINT, 1);
}

/*
* This function returns the version number associated with this kernel.
*/
Expand Down
4 changes: 2 additions & 2 deletions elkscmd/Applications
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ sys_utils/chmem :sysutil :720k
sys_utils/kill :sysutil :360c :720k
sys_utils/ps :sash :sysutil :360k :128k :nocomp
sys_utils/ps :::uptime :sysutil :128k :1440k
sys_utils/reboot :sysutil :360k :128k
sys_utils/makeboot :sysutil :360k
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/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
30 changes: 7 additions & 23 deletions elkscmd/man/man2/reboot.2
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ reboot \- close down the system or reboot
.SH SYNTAX
.ft B
.nf
#define _MINIX_SOURCE 1

#include <unistd.h>
#include <sys/mount.h>
#include <arch/system.h>

int reboot(int \fIhow\fP)
int reboot(int \fImagic1\fP, int \fImagic2\fP, int \fIhow\fP)
.fi
.ft P
.SH DESCRIPTION
Expand All @@ -18,27 +18,13 @@ down depending on
.IR how :
.PP
.TP 5
.BI "reboot(RBT_DEFAULT)"
Default shut-down action, the same as used when CTRL+ALT+DEL is pressed
on the keyboard.
.TP
.BI "reboot(RBT_HALT)"
.BI "reboot(0, 0, RB_SHUTDOWN)"
Halt the system.
.TP
.BI "reboot(RBT_PANIC)"
Cause a system panic. This is not normally done from user mode, but by
servers using the
.B sys_abort()
kernel call.
.TP
.BI "reboot(RBT_POWEROFF)"
.BI "reboot(0, 0, RB_POWEROFF)"
Power off the system if possible, reset otherwise.
.TP
.BI "reboot(RBT_REBOOT)"
Reboot the system with a software reset (currently not supported, so
a hardware reset is used).
.TP
.BI "reboot(RBT_RESET)"
.BI "reboot(0, 0, RBT_REBOOT)"
Reboot the system with a hardware reset.
.PP
.B Reboot()
Expand All @@ -50,7 +36,5 @@ the return value is -1 and an error is indicated by
.SH SEE ALSO
.BR shutdown (8),
.BR reboot (8),
.BR halt (8),
.BR poweroff (8),
.BR sync (2).
.SH AUTHOR
Edvard Tuinder ([email protected])
13 changes: 9 additions & 4 deletions elkscmd/man/man8/poweroff.8
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,24 @@
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 are performed, and after 3 seconds
have elapsed, the system is powered off.
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 (1),
.BR shutdown (1),
.BR reboot (8),
.BR shutdown (8),
.BR reboot (2).
8 changes: 4 additions & 4 deletions elkscmd/man/man8/reboot.8
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ Force reboot even if root volume cannot be remounted read-only.
is a program that allows the superuser to shut down and reboot the system immediately.
A
.B sync
and unmount of the root filesystem are performed, and if successful, after 3 seconds
have elapsed, the system is rebooted.
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 (1),
.BR poweroff (1),
.BR shutdown (8),
.BR poweroff (8),
.BR reboot (2).
13 changes: 9 additions & 4 deletions elkscmd/man/man8/shutdown.8
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@
shutdown \- shut down the system quickly
.SH SYNOPSIS
.B shutdown
.RB [ -f ]
.SS OPTIONS
.TP 5
.B \-f
Force shutdown 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 are performed, and after 3 seconds
have elapsed, the system is halted.
and unmount of the root filesystem and all other mounted filesystems
are performed, and after 3 seconds have elapsed, the system is halted.
.SH "SEE ALSO"
.BR sync (1),
.BR unmount (1),
.BR reboot (1),
.BR poweroff (1),
.BR reboot (8),
.BR poweroff (8),
.BR reboot (2).
2 changes: 0 additions & 2 deletions elkscmd/sys_utils/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ min_init
mouse
mount
passwd
poweroff
ps
reboot
sercat
shutdown
sysctl
Expand Down
8 changes: 0 additions & 8 deletions elkscmd/sys_utils/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@ PRGS = \
mount \
umount \
passwd \
reboot \
shutdown \
ps \
meminfo \
who \
man \
poweroff \
chmem \
clock \
unreal16 \
Expand Down Expand Up @@ -75,9 +73,6 @@ umount: umount.o
passwd: passwd.o
$(LD) $(LDFLAGS) -o passwd passwd.o $(LDLIBS)

reboot: reboot.o
$(LD) $(LDFLAGS) -o reboot reboot.o $(LDLIBS)

shutdown: shutdown.o
$(LD) $(LDFLAGS) -o shutdown shutdown.o $(LDLIBS)

Expand All @@ -102,9 +97,6 @@ knl: knl.o
man: man.o
$(LD) $(LDFLAGS) -o man man.o $(LDLIBS)

poweroff: poweroff.o
$(LD) $(LDFLAGS) -o poweroff poweroff.o $(LDLIBS)

chmem: chmem.o $(TINYPRINTF)
$(LD) $(LDFLAGS) -o chmem chmem.o $(TINYPRINTF) $(LDLIBS)

Expand Down
35 changes: 0 additions & 35 deletions elkscmd/sys_utils/poweroff.c

This file was deleted.

Loading

0 comments on commit 3def215

Please sign in to comment.