ovsdb-tool: Add "show-log" command for use in debugging.
[openvswitch] / ovsdb / ovsdb-server.c
index 17a9970ea9e1d17ac33b27b546a44eb844a3b33c..3bd5bc50c723aff295d607ab4862f5778fad658a 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"
 #include "vlog.h"
 #define THIS_MODULE VLM_ovsdb_server
 
-static const struct jsonrpc_server_cbs ovsdb_jsonrpc_cbs;
+static unixctl_cb_func ovsdb_server_exit;
 
 static void parse_options(int argc, char *argv[], char **file_namep,
-                          struct svec *active, struct svec *passive);
+                          struct svec *active, struct svec *passive,
+                          char **unixctl_pathp);
 static void usage(void) NO_RETURN;
 
-static void ovsdb_transact(struct unixctl_conn *, const char *args, void *db);
-
 int
 main(int argc, char *argv[])
 {
+    char *unixctl_path = NULL;
     struct unixctl_server *unixctl;
     struct ovsdb_jsonrpc_server *jsonrpc;
     struct svec active, passive;
+    struct pstream **listeners;
     struct ovsdb_error *error;
     struct ovsdb *db;
+    const char *name;
     char *file_name;
+    bool do_chdir;
+    bool exiting;
     int retval;
+    size_t i;
 
     set_program_name(argv[0]);
     register_fault_handlers();
@@ -68,31 +75,62 @@ main(int argc, char *argv[])
     signal(SIGPIPE, SIG_IGN);
     process_init();
 
-    parse_options(argc, argv, &file_name, &active, &passive);
+    parse_options(argc, argv, &file_name, &active, &passive, &unixctl_path);
+
+    /* Open all the passive sockets before detaching, to avoid race with
+     * processes that start up later. */
+    listeners = xmalloc(passive.n * sizeof *listeners);
+    for (i = 0; i < passive.n; i++) {
+        int error;
+
+        error = pstream_open(passive.names[i], &listeners[i]);
+        if (error) {
+            ovs_fatal(error, "failed to listen on \"%s\"", passive.names[i]);
+        }
+    }
+
+    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_open(file_name, false, &db);
+    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);
+    }
+    for (i = 0; i < passive.n; i++) {
+        ovsdb_jsonrpc_server_listen(jsonrpc, listeners[i]);
     }
     svec_destroy(&active);
     svec_destroy(&passive);
 
-    die_if_already_running();
-    daemonize();
-
-    retval = unixctl_server_create(NULL, &unixctl);
+    retval = unixctl_server_create(unixctl_path, &unixctl);
     if (retval) {
         ovs_fatal(retval, "could not listen for control connections");
     }
 
-    unixctl_command_register("ovsdb/transact", ovsdb_transact, db);
+    unixctl_command_register("exit", ovsdb_server_exit, &exiting);
 
-    for (;;) {
+    if (do_chdir) {
+        ignore(chdir("/"));
+    }
+
+    exiting = false;
+    while (!exiting) {
         ovsdb_jsonrpc_server_run(jsonrpc);
         unixctl_server_run(unixctl);
         ovsdb_trigger_run(db, time_msec());
@@ -106,20 +144,32 @@ main(int argc, char *argv[])
     return 0;
 }
 
+static void
+ovsdb_server_exit(struct unixctl_conn *conn, const char *args UNUSED,
+                  void *exiting_)
+{
+    bool *exiting = exiting_;
+    *exiting = true;
+    unixctl_command_reply(conn, 200, NULL);
+}
+
 static void
 parse_options(int argc, char *argv[], char **file_namep,
-              struct svec *active, struct svec *passive)
+              struct svec *active, struct svec *passive,
+              char **unixctl_pathp)
 {
     enum {
         OPT_DUMMY = UCHAR_MAX + 1,
         OPT_CONNECT,
         OPT_LISTEN,
+        OPT_UNIXCTL,
         VLOG_OPTION_ENUMS,
         LEAK_CHECKER_OPTION_ENUMS
     };
     static struct option long_options[] = {
         {"connect",     required_argument, 0, OPT_CONNECT},
         {"listen",      required_argument, 0, OPT_LISTEN},
+        {"unixctl",     required_argument, 0, OPT_UNIXCTL},
         {"help",        no_argument, 0, 'h'},
         {"version",     no_argument, 0, 'V'},
         DAEMON_LONG_OPTIONS,
@@ -148,6 +198,10 @@ parse_options(int argc, char *argv[], char **file_namep,
             svec_add(passive, optarg);
             break;
 
+        case OPT_UNIXCTL:
+            *unixctl_pathp = optarg;
+            break;
+
         case 'h':
             usage();
 
@@ -171,9 +225,11 @@ parse_options(int argc, char *argv[], char **file_namep,
     argc -= optind;
     argv += optind;
 
-    if (argc != 1) {
+    if (argc > 1) {
         ovs_fatal(0, "database file is only non-option argument; "
                 "use --help for usage");
+    } else if (argc < 1) {
+        ovs_fatal(0, "missing database file argument; use --help for usage");
     }
 
     *file_namep = argv[0];
@@ -198,26 +254,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);
-}