From c3331116ebd0b71df5ae8c6efe9a7f94148b03bf Mon Sep 17 00:00:00 2001 From: Vaxry Date: Sat, 21 Dec 2024 15:47:46 +0000 Subject: [PATCH] os/process: add pid() --- include/hyprutils/os/Process.hpp | 5 +++++ src/os/Process.cpp | 10 ++++++++-- tests/os.cpp | 11 +++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/include/hyprutils/os/Process.hpp b/include/hyprutils/os/Process.hpp index 115fa66..73c8450 100644 --- a/include/hyprutils/os/Process.hpp +++ b/include/hyprutils/os/Process.hpp @@ -3,6 +3,7 @@ #include #include #include +#include namespace Hyprutils { namespace OS { @@ -23,10 +24,14 @@ namespace Hyprutils { const std::string& stdOut(); const std::string& stdErr(); + // only populated when ran async + const pid_t pid(); + private: std::string binary, out, err; std::vector args; std::vector> env; + pid_t grandchildPid = 0; }; } } \ No newline at end of file diff --git a/src/os/Process.cpp b/src/os/Process.cpp index 2aab580..e5f20e1 100644 --- a/src/os/Process.cpp +++ b/src/os/Process.cpp @@ -209,7 +209,9 @@ bool Hyprutils::OS::CProcess::runAsync() { read(socket[0], &grandchild, sizeof(grandchild)); close(socket[0]); // clear child and leave grandchild to init - waitpid(child, NULL, 0); + waitpid(child, nullptr, 0); + + grandchildPid = grandchild; return true; } @@ -220,4 +222,8 @@ const std::string& Hyprutils::OS::CProcess::stdOut() { const std::string& Hyprutils::OS::CProcess::stdErr() { return err; -} \ No newline at end of file +} + +const pid_t Hyprutils::OS::CProcess::pid() { + return grandchildPid; +} diff --git a/tests/os.cpp b/tests/os.cpp index 1e6159b..9fdde7b 100644 --- a/tests/os.cpp +++ b/tests/os.cpp @@ -1,3 +1,7 @@ +#include +#include +#include +#include #include #include "shared.hpp" @@ -15,5 +19,12 @@ int main(int argc, char** argv, char** envp) { EXPECT(process.stdOut(), std::string{"Hello World!\n"}); EXPECT(process.stdErr(), std::string{""}); + CProcess process2("sh", {"-c", "while true; do sleep 1; done;"}); + + EXPECT(process2.runAsync(), true); + EXPECT(getpgid(process2.pid()) >= 0, true); + + kill(process2.pid(), SIGKILL); + return ret; } \ No newline at end of file