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 fcdc922343
commit 79851394b3
9 changed files with 100 additions and 2 deletions

View File

@ -111,6 +111,8 @@ namespace Kernel
BAN::ErrorOr<long> sys_sync(bool should_block); BAN::ErrorOr<long> sys_sync(bool should_block);
BAN::ErrorOr<long> sys_poweroff(int command);
BAN::ErrorOr<void> mount(BAN::StringView source, BAN::StringView target); BAN::ErrorOr<void> mount(BAN::StringView source, BAN::StringView target);
BAN::ErrorOr<long> sys_read_dir_entries(int fd, DirectoryEntryList* buffer, size_t buffer_size); BAN::ErrorOr<long> sys_read_dir_entries(int fd, DirectoryEntryList* buffer, size_t buffer_size);

View File

@ -14,8 +14,11 @@
#include <LibELF/ELF.h> #include <LibELF/ELF.h>
#include <LibELF/Values.h> #include <LibELF/Values.h>
#include <lai/helpers/pm.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h> #include <stdio.h>
#include <sys/banan-os.h>
#include <sys/sysmacros.h> #include <sys/sysmacros.h>
#include <sys/wait.h> #include <sys/wait.h>
@ -773,6 +776,33 @@ namespace Kernel
return 0; return 0;
} }
BAN::ErrorOr<long> Process::sys_poweroff(int command)
{
if (command != POWEROFF_REBOOT && command != POWEROFF_SHUTDOWN)
return BAN::Error::from_errno(EINVAL);
// FIXME: gracefully kill all processes
DevFileSystem::get().initiate_sync(true);
lai_api_error_t error;
switch (command)
{
case POWEROFF_REBOOT:
error = lai_acpi_reset();
break;
case POWEROFF_SHUTDOWN:
error = lai_enter_sleep(5);
break;
default:
ASSERT_NOT_REACHED();
}
// If we reach here, there was an error
dprintln("{}", lai_api_error_to_string(error));
return BAN::Error::from_errno(EUNKNOWN);
}
BAN::ErrorOr<long> Process::sys_read_dir_entries(int fd, DirectoryEntryList* list, size_t list_size) BAN::ErrorOr<long> Process::sys_read_dir_entries(int fd, DirectoryEntryList* list, size_t list_size)
{ {
LockGuard _(m_lock); LockGuard _(m_lock);

View File

@ -196,6 +196,9 @@ namespace Kernel
case SYS_TTY_CTRL: case SYS_TTY_CTRL:
ret = Process::current().sys_tty_ctrl((int)arg1, (int)arg2, (int)arg3); ret = Process::current().sys_tty_ctrl((int)arg1, (int)arg2, (int)arg3);
break; break;
case SYS_POWEROFF:
ret = Process::current().sys_poweroff((int)arg1);
break;
default: default:
dwarnln("Unknown syscall {}", syscall); dwarnln("Unknown syscall {}", syscall);
break; break;

View File

@ -11,8 +11,8 @@ __BEGIN_DECLS
#define TTY_FLAG_ENABLE_OUTPUT 1 #define TTY_FLAG_ENABLE_OUTPUT 1
#define TTY_FLAG_ENABLE_INPUT 2 #define TTY_FLAG_ENABLE_INPUT 2
#define POWER_SHUTDOWN 0 #define POWEROFF_SHUTDOWN 0
#define POWER_REBOOT 1 #define POWEROFF_REBOOT 1
/* /*
fildes: refers to valid tty device fildes: refers to valid tty device
@ -22,6 +22,7 @@ flags: bitwise or of TTY_FLAG_* definitions
return value: 0 on success, -1 on failure and errno set to the error return value: 0 on success, -1 on failure and errno set to the error
*/ */
int tty_ctrl(int fildes, int command, int flags); int tty_ctrl(int fildes, int command, int flags);
int poweroff(int command);
__END_DECLS __END_DECLS

View File

@ -54,6 +54,7 @@ __BEGIN_DECLS
#define SYS_MMAP 51 #define SYS_MMAP 51
#define SYS_MUNMAP 52 #define SYS_MUNMAP 52
#define SYS_TTY_CTRL 53 #define SYS_TTY_CTRL 53
#define SYS_POWEROFF 54
__END_DECLS __END_DECLS

View File

@ -6,3 +6,8 @@ int tty_ctrl(int fildes, int command, int flags)
{ {
return syscall(SYS_TTY_CTRL, fildes, command, flags); return syscall(SYS_TTY_CTRL, fildes, command, flags);
} }
int poweroff(int command)
{
return syscall(SYS_POWEROFF, command);
}

View File

@ -9,6 +9,7 @@ set(USERSPACE_PROJECTS
id id
init init
ls ls
poweroff
Shell Shell
snake snake
stat stat

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;
}