mirror of
https://github.com/hyprwm/hyprutils.git
synced 2025-05-12 21:30:36 +01:00
os/process: add fd control for async
This commit is contained in:
parent
7248194a2c
commit
966d0c0b6a
2 changed files with 19 additions and 0 deletions
|
@ -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;
|
||||
};
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue