[ovs-dev] [PATCH 2/2] daemon: Call make_pidfile after the creation of the worker.

Gurucharan Shetty shettyg at nicira.com
Mon Apr 29 15:16:36 UTC 2013


Currently we are creating the worker after creation of the pidfile.
This means two things for ovs-vswitchd. One, the pidfile's file descriptor
is open in both main process and the worker process and closing of the file
descriptor in either of the process means we will loose the lock on the
pidfile.  Two, the responsibility of deleting the pidfile after process
termination rests with the worker process.

When we restart openvswitch using the startup scripts, we SIGTERM the main
process and once it is cleaned up, we start ovs-vswitchd again. This results
in a race condition. The new ovs-vswitchd will create a pidfile because it is
unlocked. But, if the old worker process exits after the start of new
ovs-vswitchd, it will simply delete the pidfile underneath the new ovs-vswitchd.
This will eventually result in multiple ovs-vswitchd daemons.

This patch moves make_pidfile to daemonize_complete. This way, we create the
pidfile after the creation of worker process. This means the responsibility of
deleting it lies with the main process and also as a side-effect, only one
process owns the file descriptor of the pidfile.

Bug #16669.
Signed-off-by: Gurucharan Shetty <gshetty at nicira.com>
---
 lib/daemon.c |    9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/lib/daemon.c b/lib/daemon.c
index e7ee56c..f644584 100644
--- a/lib/daemon.c
+++ b/lib/daemon.c
@@ -512,10 +512,6 @@ daemonize_start(void)
         /* Running in daemon process. */
     }
 
-    if (pidfile) {
-        make_pidfile();
-    }
-
     /* Make sure that the unixctl commands for vlog get registered in a
      * daemon, even before the first log message. */
     vlog_init();
@@ -529,9 +525,12 @@ daemonize_start(void)
 void
 daemonize_complete(void)
 {
+    if (pidfile) {
+        make_pidfile();
+    }
+
     if (!detached) {
         detached = true;
-
         fork_notify_startup(daemonize_fd);
         daemonize_fd = -1;
         daemonize_post_detach();
-- 
1.7.9.5




More information about the dev mailing list