summaryrefslogtreecommitdiff
path: root/process.cpp
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2018-08-29 22:54:57 +0200
committertomsmeding <tom.smeding@gmail.com>2018-08-29 22:54:57 +0200
commitaa8c92d0cb854dcc93d7f9076285332417354889 (patch)
tree583347bae0091b902c18efd9aaaf21d319a97649 /process.cpp
parent4d82d1ae95ee33dae80f71021ec15da03bcdde73 (diff)
Fix wait()'ing on a Process after terminate()'ing it
Diffstat (limited to 'process.cpp')
-rw-r--r--process.cpp6
1 files changed, 6 insertions, 0 deletions
diff --git a/process.cpp b/process.cpp
index d3ceb28..cab5484 100644
--- a/process.cpp
+++ b/process.cpp
@@ -64,6 +64,7 @@ void Process::run() {
}
void Process::wait() {
+ if (pid == -1) return;
while (true) {
int status;
if (waitpid(pid, &status, 0) < 0) {
@@ -88,6 +89,8 @@ void Process::unStop() {
}
bool Process::writeLine(const string_view line) {
+ if (pid == -1) return false;
+
string str;
str.reserve(line.size() + 1);
str += line;
@@ -108,6 +111,8 @@ bool Process::writeLine(const string_view line) {
}
optional<string> Process::readLine() {
+ if (pid == -1) return nullopt;
+
size_t idx = readBuf.find('\n');
if (idx != string::npos) {
string res = readBuf.substr(0, idx);
@@ -144,6 +149,7 @@ void Process::terminate() {
// SIGKILL if necessary
if (pid != -1) {
kill(pid, SIGKILL); // force kill
+ wait();
pid = -1;
}
}