os/process: add fd control for async

This commit is contained in:
Vaxry 2025-04-06 15:34:28 +01:00
parent 7248194a2c
commit 966d0c0b6a
2 changed files with 19 additions and 0 deletions

View file

@ -14,6 +14,11 @@ namespace Hyprutils {
void addEnv(const std::string& name, const std::string& value);
// only for async, sync doesn't make sense
void setStdoutFD(int fd);
// only for async, sync doesn't make sense
void setStderrFD(int fd);
/* Run the process, synchronously, get the stdout and stderr. False on fail */
bool runSync();
@ -31,6 +36,7 @@ namespace Hyprutils {
std::vector<std::string> args;
std::vector<std::pair<std::string, std::string>> env;
pid_t grandchildPid = 0;
int stdoutFD = -1, stderrFD = -1;
};
}
}

View file

@ -189,6 +189,11 @@ bool Hyprutils::OS::CProcess::runAsync() {
argsC.emplace_back(nullptr);
if (stdoutFD != -1)
dup2(stdoutFD, 1);
if (stderrFD != -1)
dup2(stderrFD, 2);
execvp(binary.c_str(), (char* const*)argsC.data());
_exit(0);
}
@ -230,3 +235,11 @@ const std::string& Hyprutils::OS::CProcess::stdErr() {
const pid_t Hyprutils::OS::CProcess::pid() {
return grandchildPid;
}
void Hyprutils::OS::CProcess::setStdoutFD(int fd) {
stdoutFD = fd;
}
void Hyprutils::OS::CProcess::setStderrFD(int fd) {
stderrFD = fd;
}