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