From 82411789e87a2dad2649ce4343ff5e278b3a31e8 Mon Sep 17 00:00:00 2001 From: apio Date: Mon, 10 Jul 2023 22:17:05 +0200 Subject: [PATCH] libos+apps: Add kill --- apps/CMakeLists.txt | 1 + apps/kill.cpp | 23 +++++++++++++++++++++++ libos/include/os/Process.h | 9 +++++++++ libos/src/Process.cpp | 6 ++++++ 4 files changed, 39 insertions(+) create mode 100644 apps/kill.cpp diff --git a/apps/CMakeLists.txt b/apps/CMakeLists.txt index d9d89610..cafa82f8 100644 --- a/apps/CMakeLists.txt +++ b/apps/CMakeLists.txt @@ -40,3 +40,4 @@ luna_app(mktemp.cpp mktemp) luna_app(sysfuzz.cpp sysfuzz) luna_app(pivot_root.cpp pivot_root) luna_app(cp.cpp cp) +luna_app(kill.cpp kill) diff --git a/apps/kill.cpp b/apps/kill.cpp new file mode 100644 index 00000000..6c88df05 --- /dev/null +++ b/apps/kill.cpp @@ -0,0 +1,23 @@ +#include +#include +#include + +Result luna_main(int argc, char** argv) +{ + StringView signo_sv = "15"; + StringView process; + + os::ArgumentParser parser; + parser.add_description("Send a signal to another process."_sv); + parser.add_system_program_info("kill"_sv); + parser.add_value_argument(signo_sv, 's', "signal", "the signal number to send"); + parser.add_positional_argument(process, "pid", true); + parser.parse(argc, argv); + + int signo = atoi(signo_sv.chars()); + pid_t pid = atoi(process.chars()); + + TRY(os::Process::kill(pid, signo)); + + return 0; +} diff --git a/libos/include/os/Process.h b/libos/include/os/Process.h index 8010424e..bcf902e9 100644 --- a/libos/include/os/Process.h +++ b/libos/include/os/Process.h @@ -72,5 +72,14 @@ namespace os * child argument if ANY_CHILD was passed). */ static Result wait(pid_t child, int* status, int options = 0); + + /** + * @brief Send a signal to a process. + * + * @param pid The process ID of the process. + * @param signo The signal number to send. Can be 0 to simply test for a process's existence. + * @return Result Whether the function succeeded. + */ + static Result kill(pid_t pid, int signo); }; } diff --git a/libos/src/Process.cpp b/libos/src/Process.cpp index 72b72bec..cc896942 100644 --- a/libos/src/Process.cpp +++ b/libos/src/Process.cpp @@ -68,4 +68,10 @@ namespace os long rc = syscall(SYS_waitpid, child, status, options); return Result::from_syscall(rc); } + + Result Process::kill(pid_t pid, int signo) + { + long rc = syscall(SYS_kill, pid, signo); + return Result::from_syscall(rc); + } }