Properly detach from background process.

This commit is contained in:
Jari Sundell
2011-10-09 19:23:30 +09:00
parent 4d313e6353
commit dcf9c5a0fc
+17 -7
View File
@@ -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);
@@ -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();