Kernel/LibC/Userspace: Add SYS_POWEROFF + cli tool

You can now shutdown/reboot banan-os with the poweroff cli tool.

Reboot doesn't seem to work on qemu.
This commit is contained in:
Bananymous
2023-09-28 12:36:47 +03:00
parent d45bf363f1
commit 6eda65eea6
9 changed files with 100 additions and 2 deletions

View File

@@ -0,0 +1,17 @@
cmake_minimum_required(VERSION 3.26)
project(poweroff CXX)
set(SOURCES
main.cpp
)
add_executable(poweroff ${SOURCES})
target_compile_options(poweroff PUBLIC -O2 -g)
target_link_libraries(poweroff PUBLIC libc)
add_custom_target(poweroff-install
COMMAND sudo cp ${CMAKE_CURRENT_BINARY_DIR}/poweroff ${BANAN_BIN}/
DEPENDS poweroff
USES_TERMINAL
)

View File

@@ -0,0 +1,38 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/banan-os.h>
void usage(int ret, char* arg0)
{
FILE* fout = (ret == 0) ? stdout : stderr;
fprintf(fout, "usage: %s [OPTIONS]...\n");
fprintf(fout, " -s, --shutdown Shutdown the system (default)\n");
fprintf(fout, " -r, --reboot Reboot the system\n");
fprintf(fout, " -h, --help Show this message\n");
exit(ret);
}
int main(int argc, char** argv)
{
int operation = POWEROFF_SHUTDOWN;
for (int i = 1; i < argc; i++)
{
if (strcmp(argv[i], "-s") == 0 || strcmp(argv[i], "--shutdown") == 0)
operation = POWEROFF_SHUTDOWN;
else if (strcmp(argv[i], "-r") == 0 || strcmp(argv[i], "--reboot") == 0)
operation = POWEROFF_REBOOT;
else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)
usage(0, argv[0]);
else
usage(1, argv[0]);
}
if (poweroff(operation) == -1)
{
perror("poweroff");
return 1;
}
return 0;
}