Skip to content

Commit

Permalink
Return valid pid even when we've quit.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andersbakken committed Jun 19, 2014
1 parent 16d6b53 commit be51c76
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
17 changes: 8 additions & 9 deletions rct/Process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,11 @@ void ProcessThread::run()
break;
default:
//printf("successfully waited for pid (got %d)\n", p);
if (WIFEXITED(ret))
if (WIFEXITED(ret)) {
ret = WEXITSTATUS(ret);
else
ret = -1;
} else {
ret = Process::ReturnCrashed;
}
Process *process = proc->second.proc;
EventLoop::SharedPtr loop = proc->second.loop.lock();
sProcesses.erase(proc++);
Expand Down Expand Up @@ -172,7 +173,7 @@ void ProcessThread::installProcessHandler()
}

Process::Process()
: mPid(-1), mKilled(false), mReturn(0), mStdInIndex(0), mStdOutIndex(0), mStdErrIndex(0),
: mPid(-1), mReturn(ReturnUnset), mStdInIndex(0), mStdOutIndex(0), mStdErrIndex(0),
mWantStdInClosed(false), mMode(Sync)
{
std::call_once(sProcessHandler, ProcessThread::installProcessHandler);
Expand All @@ -187,7 +188,7 @@ Process::~Process()
{
{
std::lock_guard<std::mutex> lock(mMutex);
assert(mPid == -1 || mKilled);
assert(mFinished || mKilled);
}

if (mStdIn[0] != -1 && EventLoop::eventLoop()) {
Expand Down Expand Up @@ -435,7 +436,6 @@ Process::ExecState Process::startInternal(const Path& command, const List<String
// we're done
{
std::lock_guard<std::mutex> lock(mMutex);
assert(mPid == -1);
assert(mSync[1] == -1);

// try to read all remaining data on stdout and stderr
Expand Down Expand Up @@ -572,7 +572,6 @@ void Process::finish(int returnCode)
{
{
std::lock_guard<std::mutex> lock(mMutex);
mPid = -1;
mReturn = returnCode;

mStdInBuffer.clear();
Expand Down Expand Up @@ -685,10 +684,10 @@ void Process::handleOutput(int fd, String& buffer, int& index, Signal<std::funct

void Process::kill(int sig)
{
if (mPid == -1 && !mKilled)
if (mReturn != ReturnUnset)
return;

mKilled = true;
mReturn = ReturnKilled;
::kill(mPid, sig);
}

Expand Down
3 changes: 2 additions & 1 deletion rct/Process.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class Process

static Path findCommand(const String& command);

pid_t pid() const { return mPid; }
private:
void finish(int returnCode);
void processCallback(int fd, int mode);
Expand All @@ -77,7 +78,7 @@ class Process

mutable std::mutex mMutex;
pid_t mPid;
bool mKilled;
enum { ReturnCrashed = -1, ReturnUnset = -2, ReturnKilled = -3 };
int mReturn;

std::deque<String> mStdInBuffer;
Expand Down

0 comments on commit be51c76

Please sign in to comment.