Skip to content

Commit

Permalink
Properly detach from background process.
Browse files Browse the repository at this point in the history
  • Loading branch information
rakshasa committed Oct 9, 2011
1 parent 4d313e6 commit dcf9c5a
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/rpc/exec_file.cc
Expand Up @@ -79,6 +79,23 @@ ExecFile::execute(const char* file, char* const* argv, int flags) {
throw torrent::input_error("ExecFile::execute(...) Fork failed.");

if (childPid == 0) {
if (flags & flag_background) {
pid_t detached_pid = fork();

if (detached_pid == -1)
_exit(-1);

if (detached_pid != 0) {
if (m_logFd != -1)
write(m_logFd, "\n--- Background task ---\n", sizeof("\n--- Background task ---\n"));

_exit(0);
}

m_logFd = -1;
flags &= ~flag_capture;
}

int devNull = open("/dev/null", O_RDWR);
if (devNull != -1)
dup2(devNull, 0);
Expand Down Expand Up @@ -110,13 +127,6 @@ ExecFile::execute(const char* file, char* const* argv, int flags) {
_exit(result);
}

if (flags & flag_background) {
if (m_logFd != -1)
write(m_logFd, "\n--- Background task ---\n", sizeof("\n--- Background task ---\n"));

return 0;
}

// We yield the global lock when waiting for the executed command to
// finish so that XMLRPC and other threads can continue working.
ThreadBase::release_global_lock();
Expand Down

0 comments on commit dcf9c5a

Please sign in to comment.