ovsdb-server: Maintain the database lock with --detach.
[sliver-openvswitch.git] / ovsdb / ovsdb-server.c
index 17a9970..3dc5992 100644 (file)
 #include <errno.h>
 #include <getopt.h>
 #include <signal.h>
+#include <unistd.h>
 
 #include "command-line.h"
 #include "daemon.h"
 #include "fault.h"
+#include "file.h"
 #include "json.h"
 #include "jsonrpc.h"
 #include "jsonrpc-server.h"
@@ -48,8 +50,6 @@ static void parse_options(int argc, char *argv[], char **file_namep,
                           struct svec *active, struct svec *passive);
 static void usage(void) NO_RETURN;
 
-static void ovsdb_transact(struct unixctl_conn *, const char *args, void *db);
-
 int
 main(int argc, char *argv[])
 {
@@ -58,8 +58,11 @@ main(int argc, char *argv[])
     struct svec active, passive;
     struct ovsdb_error *error;
     struct ovsdb *db;
+    const char *name;
     char *file_name;
+    bool do_chdir;
     int retval;
+    size_t i;
 
     set_program_name(argv[0]);
     register_fault_handlers();
@@ -70,28 +73,46 @@ main(int argc, char *argv[])
 
     parse_options(argc, argv, &file_name, &active, &passive);
 
-    error = ovsdb_open(file_name, false, &db);
+    if (get_detach() && is_chdir_enabled()) {
+        /* We need to skip chdir("/") in daemonize() and do it later, because
+         * we need to open the database and possible set up up Unix domain
+         * sockets in the current working directory after we daemonize.  We
+         * can't open the database before we daemonize because file locks
+         * aren't inherited by child processes.  */
+        do_chdir = true;
+        set_no_chdir();
+    } else {
+        do_chdir = false;
+    }
+    die_if_already_running();
+    daemonize();
+
+    error = ovsdb_file_open(file_name, false, &db);
     if (error) {
         ovs_fatal(0, "%s", ovsdb_error_to_string(error));
     }
 
-    retval = ovsdb_jsonrpc_server_create(db, &active, &passive, &jsonrpc);
-    if (retval) {
-        ovs_fatal(retval, "failed to initialize JSON-RPC server for OVSDB");
+    jsonrpc = ovsdb_jsonrpc_server_create(db);
+    SVEC_FOR_EACH (i, name, &active) {
+        ovsdb_jsonrpc_server_connect(jsonrpc, name);
+    }
+    SVEC_FOR_EACH (i, name, &passive) {
+        retval = ovsdb_jsonrpc_server_listen(jsonrpc, name);
+        if (retval) {
+            ovs_fatal(retval, "failed to listen on %s", name);
+        }
     }
     svec_destroy(&active);
     svec_destroy(&passive);
 
-    die_if_already_running();
-    daemonize();
-
     retval = unixctl_server_create(NULL, &unixctl);
     if (retval) {
         ovs_fatal(retval, "could not listen for control connections");
     }
 
-    unixctl_command_register("ovsdb/transact", ovsdb_transact, db);
-
+    if (do_chdir) {
+        chdir("/");
+    }
     for (;;) {
         ovsdb_jsonrpc_server_run(jsonrpc);
         unixctl_server_run(unixctl);
@@ -198,26 +219,3 @@ usage(void)
     leak_checker_usage();
     exit(EXIT_SUCCESS);
 }
-\f
-static void
-ovsdb_transact(struct unixctl_conn *conn, const char *args, void *db_)
-{
-    struct ovsdb *db = db_;
-    struct json *request, *reply;
-    char *reply_string;
-
-    /* Parse JSON. */
-    request = json_from_string(args);
-    if (request->type == JSON_STRING) {
-        unixctl_command_reply(conn, 501, request->u.string);
-        json_destroy(request);
-        return;
-    }
-
-    /* Execute command. */
-    reply = ovsdb_execute(db, request, 0, NULL);
-    reply_string = json_to_string(reply, 0);
-    unixctl_command_reply(conn, 200, reply_string);
-    free(reply_string);
-    json_destroy(reply);
-}