From 792b39f941abcad4c39269abcb03c8ba364cab50 Mon Sep 17 00:00:00 2001
From: Gavin Hurlbut <ghurlbut@mythtv.org>
Date: Sun, 9 Jan 2011 14:49:47 -0800
Subject: [PATCH 2/2] Move timeout setting to before fork

---
 mythtv/libs/libmythdb/system-unix.cpp    |   12 ++++++------
 mythtv/libs/libmythdb/system-windows.cpp |   11 +++++------
 2 files changed, 11 insertions(+), 12 deletions(-)

diff --git a/mythtv/libs/libmythdb/system-unix.cpp b/mythtv/libs/libmythdb/system-unix.cpp
index 0f29c6e..f96339d 100644
--- a/mythtv/libs/libmythdb/system-unix.cpp
+++ b/mythtv/libs/libmythdb/system-unix.cpp
@@ -626,6 +626,11 @@ void MythSystemUnix::Fork(time_t timeout)
     if (GetSetting("SetDirectory") && !dir.isEmpty())
         directory = strdup(dir.toUtf8().constData());
 
+    /* Do this before forking in case the child miserably fails */
+    m_timeout = timeout;
+    if( timeout )
+        m_timeout += time(NULL);
+
     pid_t child = fork();
 
     if (child < 0)
@@ -642,17 +647,12 @@ void MythSystemUnix::Fork(time_t timeout)
         m_pid = child;
         SetStatus( GENERIC_EXIT_RUNNING );
 
-        m_timeout = timeout;
-
         VERBOSE(VB_SYSTEM|VB_EXTRA,
                     QString("Managed child (PID: %1) has started! "
                             "%2%3 command=%4, timeout=%5")
                         .arg(m_pid) .arg(GetSetting("UseShell") ? "*" : "")
                         .arg(GetSetting("RunInBackground") ? "&" : "")
-                        .arg(GetLogCmd()) .arg(m_timeout));
-
-        if( timeout )
-            m_timeout += time(NULL);
+                        .arg(GetLogCmd()) .arg(timeout));
 
         /* close unused pipe ends */
         CLOSE(p_stdin[0]);
diff --git a/mythtv/libs/libmythdb/system-windows.cpp b/mythtv/libs/libmythdb/system-windows.cpp
index 2e390c5..df92dfd 100644
--- a/mythtv/libs/libmythdb/system-windows.cpp
+++ b/mythtv/libs/libmythdb/system-windows.cpp
@@ -633,6 +633,10 @@ void MythSystemWindows::Fork(time_t timeout)
     PROCESS_INFORMATION pi;
     ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
 
+    m_timeout = timeout;
+    if( timeout )
+        m_timeout += time(NULL);
+
     bool success = CreateProcess(NULL, 
                     command,       // command line 
                     NULL,          // process security attributes 
@@ -655,18 +659,13 @@ void MythSystemWindows::Fork(time_t timeout)
         m_child = pi.hProcess;
         SetStatus( GENERIC_EXIT_RUNNING );
 
-        m_timeout = timeout;
-
         VERBOSE(VB_SYSTEM|VB_EXTRA,
                 QString("Managed child (Handle: %1) has started! "
                         "%2%3 command=%4, timeout=%5")
                     .arg((long long)m_child) 
                     .arg(GetSetting("UseShell") ? "*" : "")
                     .arg(GetSetting("RunInBackground") ? "&" : "")
-                    .arg(GetLogCmd()) .arg(m_timeout));
-
-        if( timeout )
-            m_timeout += time(NULL);
+                    .arg(GetLogCmd()) .arg(timeout));
 
         /* close unused pipe ends */
         CLOSE(p_stdin[0]);
-- 
1.7.0.4

