Skip to content

Commit

Permalink
os/process: add pid()
Browse files Browse the repository at this point in the history
  • Loading branch information
vaxerski committed Dec 21, 2024
1 parent e6cf45c commit c333111
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
5 changes: 5 additions & 0 deletions include/hyprutils/os/Process.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <string>
#include <vector>
#include <utility>
#include <sys/types.h>

namespace Hyprutils {
namespace OS {
Expand All @@ -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<std::string> args;
std::vector<std::pair<std::string, std::string>> env;
pid_t grandchildPid = 0;
};
}
}
10 changes: 8 additions & 2 deletions src/os/Process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -220,4 +222,8 @@ const std::string& Hyprutils::OS::CProcess::stdOut() {

const std::string& Hyprutils::OS::CProcess::stdErr() {
return err;
}
}

const pid_t Hyprutils::OS::CProcess::pid() {
return grandchildPid;
}
11 changes: 11 additions & 0 deletions tests/os.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#include <csignal>
#include <cerrno>
#include <cstdlib>
#include <unistd.h>
#include <hyprutils/os/Process.hpp>
#include "shared.hpp"

Expand All @@ -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;
}

0 comments on commit c333111

Please sign in to comment.