commit 96d88deeda5b81084098c4731ba1e29dca0e53ba
parent 895ecc80544b7877427d495e87864af12d879ae7
Author: xplshn <anto@xplshn.com.ar>
Date: Tue, 24 Nov 2026 14:38:44 -0300
halt: reboot behind a backend
Diffstat:
5 files changed, 66 insertions(+), 7 deletions(-)
diff --git a/cmd/pseudo/halt.c b/cmd/pseudo/halt.c
@@ -9,8 +9,6 @@
#endif
#endif
-#include <sys/syscall.h>
-
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
@@ -30,7 +28,7 @@ int
main(int argc, char *argv[])
{
int pflag = 0, rflag = 0;
- int cmd = LINUX_REBOOT_CMD_HALT;
+ int how = REBOOT_HALT;
ARGBEGIN
{
@@ -60,12 +58,12 @@ main(int argc, char *argv[])
usage();
if (pflag)
- cmd = LINUX_REBOOT_CMD_POWER_OFF;
+ how = REBOOT_POWEROFF;
if (rflag)
- cmd = LINUX_REBOOT_CMD_RESTART;
+ how = REBOOT_RESTART;
#if FEATURE_LINUX_SYSCALL
- if (syscall(__NR_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, cmd, NULL) < 0)
+ if (machine_reboot(how) < 0)
eprintf("reboot:");
#endif
return 0;
diff --git a/ninqu.rules b/ninqu.rules
@@ -49,6 +49,7 @@
"passwd_dragonfly.c" "passwd_freebsd.c" "passwd_linux.c"
"passwd_netbsd.c" "passwd_openbsd.c" "passwd_other.c"
"proc_linux.c" "proc_other.c"
+ "reboot_linux.c" "reboot_other.c"
"session_linux.c" "session_other.c"
"sysinfo_linux.c" "sysinfo_other.c"
"tty_linux.c" "tty_other.c")
@@ -82,6 +83,8 @@
"shared/libutil/passwd_other.c"
"shared/libutil/proc_linux.c"
"shared/libutil/proc_other.c"
+ "shared/libutil/reboot_linux.c"
+ "shared/libutil/reboot_other.c"
"shared/libutil/session_linux.c"
"shared/libutil/session_other.c"
"shared/libutil/sysinfo_linux.c"
@@ -89,7 +92,7 @@
"shared/libutil/tty_linux.c"
"shared/libutil/tty_other.c")
(dep $(MATCH) "shared/diskutil.h" "shared/filesystem.h" "shared/net.h" "shared/passwd.h"
- "shared/proc.h" "shared/session.h" "shared/sysinfo.h" "shared/util.h"
+ "shared/proc.h" "shared/reboot.h" "shared/session.h" "shared/sysinfo.h" "shared/util.h"
FEATURES_H)
(after NOFORK_LIST_H)
(out "shared/libutil/$(BASESTEM).o")
diff --git a/shared/libutil/reboot_linux.c b/shared/libutil/reboot_linux.c
@@ -0,0 +1,30 @@
+/* see LICENSE file for copyright and license details */
+#ifndef _DEFAULT_SOURCE
+#define _DEFAULT_SOURCE
+#endif
+#include "../reboot.h"
+
+#if defined(__linux__) && !defined(ARUU_FORCE_OTHER)
+#include <stddef.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+
+int
+machine_reboot(int how)
+{
+ int cmd;
+
+ switch (how) {
+ case REBOOT_POWEROFF:
+ cmd = LINUX_REBOOT_CMD_POWER_OFF;
+ break;
+ case REBOOT_RESTART:
+ cmd = LINUX_REBOOT_CMD_RESTART;
+ break;
+ default:
+ cmd = LINUX_REBOOT_CMD_HALT;
+ break;
+ }
+ return syscall(__NR_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, cmd, NULL) < 0 ? -1 : 0;
+}
+#endif
diff --git a/shared/libutil/reboot_other.c b/shared/libutil/reboot_other.c
@@ -0,0 +1,14 @@
+/* see LICENSE file for copyright and license details */
+#include "../reboot.h"
+
+#include <errno.h>
+
+#if !defined(__linux__) || defined(ARUU_FORCE_OTHER)
+int
+machine_reboot(int how)
+{
+ (void)how;
+ errno = ENOTSUP;
+ return -1;
+}
+#endif
diff --git a/shared/reboot.h b/shared/reboot.h
@@ -1,3 +1,15 @@
+#ifndef ARUU_REBOOT_H
+#define ARUU_REBOOT_H
+
+enum {
+ REBOOT_HALT,
+ REBOOT_POWEROFF,
+ REBOOT_RESTART,
+};
+
+/* a backend per kernel: stop or restart the machine */
+int machine_reboot(int how);
+
/*
* magic values required to use _reboot() system call
*/
@@ -29,3 +41,5 @@
#define LINUX_REBOOT_CMD_RESTART2 0xA1B2C3D4
#define LINUX_REBOOT_CMD_SW_SUSPEND 0xD000FCE2
#define LINUX_REBOOT_CMD_KEXEC 0x45584543
+
+#endif