daemon: Start monitor process, not daemon process, in new session.
[cascardo/ovs.git] / python / ovs / daemon.py
index a919c53..f74d7f0 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (c) 2010, 2011 Nicira Networks
+# Copyright (c) 2010, 2011, 2012 Nicira, Inc.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -141,7 +141,7 @@ def _make_pidfile():
         # This is global to keep Python from garbage-collecting and
         # therefore closing our file after this function exits.  That would
         # unlock the lock for us, and we don't want that.
-        global file
+        global file_handle
 
         file_handle = open(tmpfile, "w")
     except IOError, e:
@@ -245,13 +245,19 @@ def _fork_and_wait_for_startup():
                 break
         if len(s) != 1:
             retval, status = _waitpid(pid, 0)
-            if (retval == pid and
-                os.WIFEXITED(status) and os.WEXITSTATUS(status)):
-                # Child exited with an error.  Convey the same error to
-                # our parent process as a courtesy.
-                sys.exit(os.WEXITSTATUS(status))
+            if retval == pid:
+                if os.WIFEXITED(status) and os.WEXITSTATUS(status):
+                    # Child exited with an error.  Convey the same error to
+                    # our parent process as a courtesy.
+                    sys.exit(os.WEXITSTATUS(status))
+                else:
+                    sys.stderr.write("fork child failed to signal "
+                                     "startup (%s)\n"
+                                     % ovs.process.status_msg(status))
             else:
-                sys.stderr.write("fork child failed to signal startup\n")
+                assert retval < 0
+                sys.stderr.write("waitpid failed (%s)\n"
+                                 % os.strerror(-retval))
                 sys.exit(1)
 
         os.close(rfd)
@@ -356,7 +362,9 @@ def daemonize_start():
         if _fork_and_wait_for_startup() > 0:
             # Running in parent process.
             sys.exit(0)
+
         # Running in daemon or monitor process.
+        os.setsid()
 
     if _monitor:
         saved_daemonize_fd = _daemonize_fd
@@ -378,7 +386,6 @@ def daemonize_complete():
     _fork_notify_startup(_daemonize_fd)
 
     if _detach:
-        os.setsid()
         if _chdir:
             os.chdir("/")
         _close_standard_fds()