I am trying to power off my machine using the raw reboot
system call. Here is my code:
#include <unistd.h>
#include <linux/reboot.h>
#include <stdio.h>
#include <errno.h>
int main(void) {
int ret = reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_POWER_OFF, NULL);
if (ret == -1) {
printf("error: %d\n", errno);
}
return 0;
}
According to the man page, "Only the superuser may call reboot()". So I run the program as root and get the following output:
alex@laptop:~/code$ sudo ./a.out
[sudo] password for alex:
error: 22
What is error #22? That would be EINVAL
, which according to the reboot man page signifies "Bad magic numbers or cmd". As I'm using the definitions provided by #include <linux/reboot.h>
, I can't imagine the values are incorrect, but just in case, I also tried hardcoding the magic number values provided in the man page, to no avail (same error).
However, if I try glibc's wrapper function reboot(LINUX_REBOOT_CMD_POWER_OFF)
provided by <sys/reboot.h>
, it works fine (instantly shuts down machine).
There's no practical reason why I want to do this, just playing with syscall's and trying to make it work. If it helps, here are some details about the system I'm running this on:
Operating System: Ubuntu 20.04
Kernel: Linux version 5.4.0-53-generic
Compiler: gcc version 9.3.0
Let me know if any additional information might be useful!
EINVAL
means "Invalid argument " - Krishna Kanth Yenumula