daemon: Integrate checking for an existing pidfile into daemonize_start().
authorBen Pfaff <blp@nicira.com>
Thu, 31 Mar 2011 16:44:30 +0000 (09:44 -0700)
committerBen Pfaff <blp@nicira.com>
Fri, 29 Apr 2011 21:31:49 +0000 (14:31 -0700)
Until now, it has been the responsibility of an individual daemon to call
die_if_already_running() at an appropriate time.  A long time ago, this
had to happen *before* daemonizing, because once the process daemonized
itself there was no way to report failure to the process that originally
started the daemon.  With the introduction of daemonize_start(), this is
now possible, but we haven't been taking advantage of it.

Therefore, this commit integrates the die_if_already_running() call into
daemonize_start() and deletes the calls to it from individual daemons.

13 files changed:
debian/ovs-monitor-ipsec
lib/daemon.c
lib/daemon.h
ovsdb/ovsdb-server.c
python/ovs/daemon.py
tests/test-daemon.py
tests/test-jsonrpc.c
tests/test-jsonrpc.py
utilities/ovs-controller.c
utilities/ovs-openflowd.c
vswitchd/ovs-brcompatd.c
vswitchd/ovs-vswitchd.c
xenserver/usr_share_openvswitch_scripts_ovs-xapi-sync

index 17f3997..febd569 100755 (executable)
@@ -1,5 +1,5 @@
 #!/usr/bin/python
-# Copyright (c) 2009, 2010 Nicira Networks
+# Copyright (c) 2009, 2010, 2011 Nicira Networks
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -450,8 +450,6 @@ def main(argv):
                          "(use --help for help)\n" % ovs.util.PROGRAM_NAME)
         sys.exit(1)
 
-    ovs.daemon.die_if_already_running()
     remote = args[0]
     idl = ovs.db.idl.Idl(remote, "Open_vSwitch", monitor_uuid_schema_cb)
 
index 173dabe..a9cada8 100644 (file)
@@ -107,9 +107,9 @@ is_chdir_enabled(void)
     return chdir_;
 }
 
-/* Normally, die_if_already_running() will terminate the program with a message
- * if a locked pidfile already exists.  If this function is called,
- * die_if_already_running() will merely log a warning. */
+/* Normally, daemonize() or damonize_start() will terminate the program with a
+ * message if a locked pidfile already exists.  If this function is called, an
+ * existing pidfile will be replaced, with a warning. */
 void
 ignore_existing_pidfile(void)
 {
@@ -141,7 +141,7 @@ daemon_set_monitor(void)
 
 /* If a locked pidfile exists, issue a warning message and, unless
  * ignore_existing_pidfile() has been called, terminate the program. */
-void
+static void
 die_if_already_running(void)
 {
     pid_t pid;
@@ -449,6 +449,7 @@ daemonize_start(void)
         /* Running in daemon process. */
     }
 
+    die_if_already_running();
     make_pidfile();
 
     /* Make sure that the unixctl commands for vlog get registered in a
index 8f4fe39..4af98f6 100644 (file)
@@ -67,7 +67,6 @@ void daemon_set_monitor(void);
 void daemonize(void);
 void daemonize_start(void);
 void daemonize_complete(void);
-void die_if_already_running(void);
 void ignore_existing_pidfile(void);
 void daemon_usage(void);
 pid_t read_pidfile(const char *name);
index df0bc95..c9b0fdd 100644 (file)
@@ -101,7 +101,6 @@ main(int argc, char *argv[])
     parse_options(argc, argv, &file_name, &remotes, &unixctl_path,
                   &run_command);
 
-    die_if_already_running();
     daemonize_start();
 
     error = ovsdb_file_open(file_name, false, &db, &file);
index 4df2371..cfb4178 100644 (file)
@@ -89,9 +89,9 @@ def is_chdir_enabled():
     return _chdir
 
 def ignore_existing_pidfile():
-    """Normally, die_if_already_running() will terminate the program with a
-    message if a locked pidfile already exists.  If this function is called,
-    die_if_already_running() will merely log a warning."""
+    """Normally, daemonize() or daemonize_start() will terminate the program
+    with a message if a locked pidfile already exists.  If this function is
+    called, an existing pidfile will be replaced, with a warning."""
     global _overwrite_pidfile
     _overwrite_pidfile = True
 
@@ -111,7 +111,7 @@ def set_monitor():
     global _monitor
     _monitor = True
 
-def die_if_already_running():
+def _die_if_already_running():
     """If a locked pidfile exists, issue a warning message and, unless
     ignore_existing_pidfile() has been called, terminate the program."""
     if _pidfile is None:
@@ -343,6 +343,7 @@ def daemonize_start():
             _monitor_daemon(daemon_pid)
         # Running in daemon process
     
+    _die_if_already_running()
     _make_pidfile()
 
 def daemonize_complete():
index 386445d..586e0ec 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (c) 2010 Nicira Networks.
+# Copyright (c) 2010, 2011 Nicira Networks.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -45,7 +45,6 @@ def main(argv):
                              % (ovs.util.PROGRAM_NAME, key))
             sys.exit(1)
 
-    ovs.daemon.die_if_already_running()
     ovs.daemon.daemonize_start()
     if bail:
         sys.stderr.write("%s: exiting after daemonize_start() as requested\n"
index 44edda8..12bbc97 100644 (file)
@@ -182,8 +182,6 @@ do_listen(int argc OVS_UNUSED, char *argv[])
     bool done;
     int error;
 
-    die_if_already_running();
-
     error = jsonrpc_pstream_open(argv[1], &pstream);
     if (error) {
         ovs_fatal(error, "could not listen on \"%s\"", argv[1]);
index a8bf553..064457e 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (c) 2009, 2010 Nicira Networks
+# Copyright (c) 2009, 2010, 2011 Nicira Networks
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -49,8 +49,6 @@ def handle_rpc(rpc, msg):
     return done
 
 def do_listen(name):
-    ovs.daemon.die_if_already_running()
-
     error, pstream = ovs.stream.PassiveStream.open(name)
     if error:
         sys.stderr.write("could not listen on \"%s\": %s\n"
index af9e11f..ae0ea3d 100644 (file)
@@ -139,7 +139,6 @@ main(int argc, char *argv[])
         ovs_fatal(0, "no active or passive switch connections");
     }
 
-    die_if_already_running();
     daemonize_start();
 
     retval = unixctl_server_create(unixctl_path, &unixctl);
index a58a9d2..86f5ca5 100644 (file)
@@ -103,7 +103,6 @@ main(int argc, char *argv[])
     parse_options(argc, argv, &s);
     signal(SIGPIPE, SIG_IGN);
 
-    die_if_already_running();
     daemonize_start();
 
     /* Start listening for ovs-appctl requests. */
index 667aa66..9d5cdfb 100644 (file)
@@ -1289,7 +1289,6 @@ main(int argc, char *argv[])
     process_init();
     ovsrec_init();
 
-    die_if_already_running();
     daemonize_start();
 
     retval = unixctl_server_create(NULL, &unixctl);
index bae03dd..b9a2461 100644 (file)
@@ -74,7 +74,6 @@ main(int argc, char *argv[])
     process_init();
     ovsrec_init();
 
-    die_if_already_running();
     daemonize_start();
 
     retval = unixctl_server_create(NULL, &unixctl);
index 07995d0..273ec04 100755 (executable)
@@ -254,8 +254,6 @@ def main(argv):
                          "(use --help for help)\n" % ovs.util.PROGRAM_NAME)
         sys.exit(1)
 
-    ovs.daemon.die_if_already_running()
-
     remote = args[0]
     idl = ovs.db.idl.Idl(remote, "Open_vSwitch", monitor_uuid_schema_cb)