Prepare Open vSwitch 1.1.2 release.
[sliver-openvswitch.git] / ovsdb / ovsdb-server.c
1 /* Copyright (c) 2009, 2010, 2011 Nicira Networks
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <config.h>
17
18 #include "ovsdb.h"
19
20 #include <errno.h>
21 #include <getopt.h>
22 #include <signal.h>
23 #include <unistd.h>
24
25 #include "column.h"
26 #include "command-line.h"
27 #include "daemon.h"
28 #include "file.h"
29 #include "json.h"
30 #include "jsonrpc.h"
31 #include "jsonrpc-server.h"
32 #include "leak-checker.h"
33 #include "list.h"
34 #include "ovsdb-data.h"
35 #include "ovsdb-types.h"
36 #include "ovsdb-error.h"
37 #include "poll-loop.h"
38 #include "process.h"
39 #include "row.h"
40 #include "stream-ssl.h"
41 #include "stream.h"
42 #include "stress.h"
43 #include "sset.h"
44 #include "table.h"
45 #include "timeval.h"
46 #include "transaction.h"
47 #include "trigger.h"
48 #include "util.h"
49 #include "unixctl.h"
50 #include "vlog.h"
51
52 VLOG_DEFINE_THIS_MODULE(ovsdb_server);
53
54 #if HAVE_OPENSSL
55 /* SSL configuration. */
56 static char *private_key_file;
57 static char *certificate_file;
58 static char *ca_cert_file;
59 static bool bootstrap_ca_cert;
60 #endif
61
62 static unixctl_cb_func ovsdb_server_exit;
63 static unixctl_cb_func ovsdb_server_compact;
64 static unixctl_cb_func ovsdb_server_reconnect;
65
66 static void parse_options(int argc, char *argv[], char **file_namep,
67                           struct sset *remotes, char **unixctl_pathp,
68                           char **run_command);
69 static void usage(void) NO_RETURN;
70
71 static void reconfigure_from_db(struct ovsdb_jsonrpc_server *jsonrpc,
72                                 const struct ovsdb *db, struct sset *remotes);
73
74 static void update_remote_status(const struct ovsdb_jsonrpc_server *jsonrpc,
75                                  const struct sset *remotes,
76                                  struct ovsdb *db);
77
78 int
79 main(int argc, char *argv[])
80 {
81     char *unixctl_path = NULL;
82     char *run_command = NULL;
83     struct unixctl_server *unixctl;
84     struct ovsdb_jsonrpc_server *jsonrpc;
85     struct sset remotes;
86     struct ovsdb_error *error;
87     struct ovsdb_file *file;
88     struct ovsdb *db;
89     struct process *run_process;
90     char *file_name;
91     bool exiting;
92     int retval;
93     long long int status_timer = LLONG_MIN;
94
95     proctitle_init(argc, argv);
96     set_program_name(argv[0]);
97     stress_init_command();
98     signal(SIGPIPE, SIG_IGN);
99     process_init();
100
101     parse_options(argc, argv, &file_name, &remotes, &unixctl_path,
102                   &run_command);
103
104     daemonize_start();
105
106     error = ovsdb_file_open(file_name, false, &db, &file);
107     if (error) {
108         ovs_fatal(0, "%s", ovsdb_error_to_string(error));
109     }
110
111     jsonrpc = ovsdb_jsonrpc_server_create(db);
112     reconfigure_from_db(jsonrpc, db, &remotes);
113
114     retval = unixctl_server_create(unixctl_path, &unixctl);
115     if (retval) {
116         exit(EXIT_FAILURE);
117     }
118
119     if (run_command) {
120         char *run_argv[4];
121
122         run_argv[0] = "/bin/sh";
123         run_argv[1] = "-c";
124         run_argv[2] = run_command;
125         run_argv[3] = NULL;
126
127         retval = process_start(run_argv, NULL, 0, NULL, 0, &run_process);
128         if (retval) {
129             ovs_fatal(retval, "%s: process failed to start", run_command);
130         }
131     } else {
132         run_process = NULL;
133     }
134
135     daemonize_complete();
136
137     unixctl_command_register("exit", ovsdb_server_exit, &exiting);
138     unixctl_command_register("ovsdb-server/compact", ovsdb_server_compact,
139                              file);
140     unixctl_command_register("ovsdb-server/reconnect", ovsdb_server_reconnect,
141                              jsonrpc);
142
143     exiting = false;
144     while (!exiting) {
145         reconfigure_from_db(jsonrpc, db, &remotes);
146         ovsdb_jsonrpc_server_run(jsonrpc);
147         unixctl_server_run(unixctl);
148         ovsdb_trigger_run(db, time_msec());
149         if (run_process && process_exited(run_process)) {
150             exiting = true;
151         }
152
153         /* update Manager status(es) every 5 seconds */
154         if (time_msec() >= status_timer) {
155             status_timer = time_msec() + 5000;
156             update_remote_status(jsonrpc, &remotes, db);
157         }
158
159         ovsdb_jsonrpc_server_wait(jsonrpc);
160         unixctl_server_wait(unixctl);
161         ovsdb_trigger_wait(db, time_msec());
162         if (run_process) {
163             process_wait(run_process);
164         }
165         if (exiting) {
166             poll_immediate_wake();
167         }
168         poll_timer_wait_until(status_timer);
169         poll_block();
170     }
171     ovsdb_jsonrpc_server_destroy(jsonrpc);
172     ovsdb_destroy(db);
173     sset_destroy(&remotes);
174     unixctl_server_destroy(unixctl);
175
176     if (run_process && process_exited(run_process)) {
177         int status = process_status(run_process);
178         if (status) {
179             ovs_fatal(0, "%s: child exited, %s",
180                       run_command, process_status_msg(status));
181         }
182     }
183
184     return 0;
185 }
186
187 static void
188 parse_db_column(const struct ovsdb *db,
189                 const char *name_,
190                 const struct ovsdb_table **tablep,
191                 const struct ovsdb_column **columnp)
192 {
193     char *name, *table_name, *column_name;
194     const struct ovsdb_column *column;
195     const struct ovsdb_table *table;
196     char *save_ptr = NULL;
197
198     name = xstrdup(name_);
199     strtok_r(name, ":", &save_ptr); /* "db:" */
200     table_name = strtok_r(NULL, ",", &save_ptr);
201     column_name = strtok_r(NULL, ",", &save_ptr);
202     if (!table_name || !column_name) {
203         ovs_fatal(0, "\"%s\": invalid syntax", name_);
204     }
205
206     table = ovsdb_get_table(db, table_name);
207     if (!table) {
208         ovs_fatal(0, "\"%s\": no table named %s", name_, table_name);
209     }
210
211     column = ovsdb_table_schema_get_column(table->schema, column_name);
212     if (!column) {
213         ovs_fatal(0, "\"%s\": table \"%s\" has no column \"%s\"",
214                   name_, table_name, column_name);
215     }
216     free(name);
217
218     *columnp = column;
219     *tablep = table;
220 }
221
222 static void
223 parse_db_string_column(const struct ovsdb *db,
224                        const char *name,
225                        const struct ovsdb_table **tablep,
226                        const struct ovsdb_column **columnp)
227 {
228     const struct ovsdb_column *column;
229     const struct ovsdb_table *table;
230
231     parse_db_column(db, name, &table, &column);
232
233     if (column->type.key.type != OVSDB_TYPE_STRING
234         || column->type.value.type != OVSDB_TYPE_VOID) {
235         ovs_fatal(0, "\"%s\": table \"%s\" column \"%s\" is "
236                   "not string or set of strings",
237                   name, table->schema->name, column->name);
238     }
239
240     *columnp = column;
241     *tablep = table;
242 }
243
244 static OVS_UNUSED const char *
245 query_db_string(const struct ovsdb *db, const char *name)
246 {
247     if (!name || strncmp(name, "db:", 3)) {
248         return name;
249     } else {
250         const struct ovsdb_column *column;
251         const struct ovsdb_table *table;
252         const struct ovsdb_row *row;
253
254         parse_db_string_column(db, name, &table, &column);
255
256         HMAP_FOR_EACH (row, hmap_node, &table->rows) {
257             const struct ovsdb_datum *datum;
258             size_t i;
259
260             datum = &row->fields[column->index];
261             for (i = 0; i < datum->n; i++) {
262                 if (datum->keys[i].string[0]) {
263                     return datum->keys[i].string;
264                 }
265             }
266         }
267         return NULL;
268     }
269 }
270
271 static struct ovsdb_jsonrpc_options *
272 add_remote(struct shash *remotes, const char *target)
273 {
274     struct ovsdb_jsonrpc_options *options;
275
276     options = shash_find_data(remotes, target);
277     if (!options) {
278         options = ovsdb_jsonrpc_default_options();
279         shash_add(remotes, target, options);
280     }
281
282     return options;
283 }
284
285 static struct ovsdb_datum *
286 get_datum(struct ovsdb_row *row, const char *column_name,
287           const enum ovsdb_atomic_type key_type,
288           const enum ovsdb_atomic_type value_type,
289           const size_t n_max)
290 {
291     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
292     const struct ovsdb_table_schema *schema = row->table->schema;
293     const struct ovsdb_column *column;
294
295     column = ovsdb_table_schema_get_column(schema, column_name);
296     if (!column) {
297         VLOG_DBG_RL(&rl, "Table `%s' has no `%s' column",
298                     schema->name, column_name);
299         return NULL;
300     }
301
302     if (column->type.key.type != key_type
303         || column->type.value.type != value_type
304         || column->type.n_max != n_max) {
305         if (!VLOG_DROP_DBG(&rl)) {
306             char *type_name = ovsdb_type_to_english(&column->type);
307             VLOG_DBG("Table `%s' column `%s' has type %s, not expected "
308                      "key type %s, value type %s, max elements %zd.",
309                      schema->name, column_name, type_name,
310                      ovsdb_atomic_type_to_string(key_type),
311                      ovsdb_atomic_type_to_string(value_type),
312                      n_max);
313             free(type_name);
314         }
315         return NULL;
316     }
317
318     return &row->fields[column->index];
319 }
320
321 static const union ovsdb_atom *
322 read_column(const struct ovsdb_row *row, const char *column_name,
323             enum ovsdb_atomic_type type)
324 {
325     const struct ovsdb_datum *datum;
326
327     datum = get_datum((struct ovsdb_row *) row, column_name, type, OVSDB_TYPE_VOID, 1);
328     return datum && datum->n ? datum->keys : NULL;
329 }
330
331 static bool
332 read_integer_column(const struct ovsdb_row *row, const char *column_name,
333                     long long int *integerp)
334 {
335     const union ovsdb_atom *atom;
336
337     atom = read_column(row, column_name, OVSDB_TYPE_INTEGER);
338     *integerp = atom ? atom->integer : 0;
339     return atom != NULL;
340 }
341
342 static bool
343 read_string_column(const struct ovsdb_row *row, const char *column_name,
344                    const char **stringp)
345 {
346     const union ovsdb_atom *atom;
347
348     atom = read_column(row, column_name, OVSDB_TYPE_STRING);
349     *stringp = atom ? atom->string : 0;
350     return atom != NULL;
351 }
352
353 static void
354 write_bool_column(struct ovsdb_row *row, const char *column_name, bool value)
355 {
356     struct ovsdb_datum *datum = get_datum(row, column_name, OVSDB_TYPE_BOOLEAN,
357                                           OVSDB_TYPE_VOID, 1);
358
359     if (!datum) {
360         return;
361     }
362     datum->keys[0].boolean = value;
363 }
364
365 static void
366 write_string_string_column(struct ovsdb_row *row, const char *column_name,
367                            char **keys, char **values, size_t n)
368 {
369     const struct ovsdb_column *column;
370     struct ovsdb_datum *datum;
371     size_t i;
372
373     column = ovsdb_table_schema_get_column(row->table->schema, column_name);
374     datum = get_datum(row, column_name, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING,
375                       UINT_MAX);
376     if (!datum) {
377         return;
378     }
379
380     /* Free existing data. */
381     ovsdb_datum_destroy(datum, &column->type);
382
383     /* Allocate space for new values. */
384     datum->n = n;
385     datum->keys = xmalloc(n * sizeof *datum->keys);
386     datum->values = xmalloc(n * sizeof *datum->values);
387
388     for (i = 0; i < n; ++i) {
389         datum->keys[i].string = keys[i];
390         datum->values[i].string = values[i];
391     }
392
393     /* Sort and check constraints. */
394     ovsdb_datum_sort_assert(datum, column->type.key.type);
395 }
396
397 /* Adds a remote and options to 'remotes', based on the Manager table row in
398  * 'row'. */
399 static void
400 add_manager_options(struct shash *remotes, const struct ovsdb_row *row)
401 {
402     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
403     struct ovsdb_jsonrpc_options *options;
404     long long int max_backoff, probe_interval;
405     const char *target;
406
407     if (!read_string_column(row, "target", &target) || !target) {
408         VLOG_INFO_RL(&rl, "Table `%s' has missing or invalid `target' column",
409                      row->table->schema->name);
410         return;
411     }
412
413     options = add_remote(remotes, target);
414     if (read_integer_column(row, "max_backoff", &max_backoff)) {
415         options->max_backoff = max_backoff;
416     }
417     if (read_integer_column(row, "inactivity_probe", &probe_interval)) {
418         options->probe_interval = probe_interval;
419     }
420 }
421
422 static void
423 query_db_remotes(const char *name, const struct ovsdb *db,
424                  struct shash *remotes)
425 {
426     const struct ovsdb_column *column;
427     const struct ovsdb_table *table;
428     const struct ovsdb_row *row;
429
430     parse_db_column(db, name, &table, &column);
431
432     if (column->type.key.type == OVSDB_TYPE_STRING
433         && column->type.value.type == OVSDB_TYPE_VOID) {
434         HMAP_FOR_EACH (row, hmap_node, &table->rows) {
435             const struct ovsdb_datum *datum;
436             size_t i;
437
438             datum = &row->fields[column->index];
439             for (i = 0; i < datum->n; i++) {
440                 add_remote(remotes, datum->keys[i].string);
441             }
442         }
443     } else if (column->type.key.type == OVSDB_TYPE_UUID
444                && column->type.key.u.uuid.refTable
445                && column->type.value.type == OVSDB_TYPE_VOID) {
446         const struct ovsdb_table *ref_table = column->type.key.u.uuid.refTable;
447         HMAP_FOR_EACH (row, hmap_node, &table->rows) {
448             const struct ovsdb_datum *datum;
449             size_t i;
450
451             datum = &row->fields[column->index];
452             for (i = 0; i < datum->n; i++) {
453                 const struct ovsdb_row *ref_row;
454
455                 ref_row = ovsdb_table_get_row(ref_table, &datum->keys[i].uuid);
456                 if (ref_row) {
457                     add_manager_options(remotes, ref_row);
458                 }
459             }
460         }
461     }
462 }
463
464 static void
465 update_remote_row(const struct ovsdb_row *row, struct ovsdb_txn *txn,
466                   const struct shash *statuses)
467 {
468     struct ovsdb_row *rw_row;
469     const char *target;
470     const struct ovsdb_jsonrpc_remote_status *status;
471     char *keys[4], *values[4];
472     size_t n = 0;
473
474     /* Get the "target" (protocol/host/port) spec. */
475     if (!read_string_column(row, "target", &target)) {
476         /* Bad remote spec or incorrect schema. */
477         return;
478     }
479
480     /* Prepare to modify this row. */
481     rw_row = ovsdb_txn_row_modify(txn, row);
482
483     /* Find status information for this target. */
484     status = shash_find_data(statuses, target);
485     if (!status) {
486         /* Should never happen, but just in case... */
487         return;
488     }
489
490     /* Update status information columns. */
491
492     write_bool_column(rw_row, "is_connected",
493                       status->is_connected);
494
495     keys[n] = xstrdup("state");
496     values[n++] = xstrdup(status->state);
497     if (status->sec_since_connect != UINT_MAX) {
498         keys[n] = xstrdup("sec_since_connect");
499         values[n++] = xasprintf("%u", status->sec_since_connect);
500     }
501     if (status->sec_since_disconnect != UINT_MAX) {
502         keys[n] = xstrdup("sec_since_disconnect");
503         values[n++] = xasprintf("%u", status->sec_since_disconnect);
504     }
505     if (status->last_error) {
506         keys[n] = xstrdup("last_error");
507         values[n++] =
508             xstrdup(ovs_retval_to_string(status->last_error));
509     }
510     write_string_string_column(rw_row, "status", keys, values, n);
511 }
512
513 static void
514 update_remote_rows(const struct ovsdb *db, struct ovsdb_txn *txn,
515                    const char *remote_name, const struct shash *statuses)
516 {
517     const struct ovsdb_table *table, *ref_table;
518     const struct ovsdb_column *column;
519     const struct ovsdb_row *row;
520
521     if (strncmp("db:", remote_name, 3)) {
522         return;
523     }
524
525     parse_db_column(db, remote_name, &table, &column);
526
527     if (column->type.key.type != OVSDB_TYPE_UUID
528         || !column->type.key.u.uuid.refTable
529         || column->type.value.type != OVSDB_TYPE_VOID) {
530         return;
531     }
532
533     ref_table = column->type.key.u.uuid.refTable;
534
535     HMAP_FOR_EACH (row, hmap_node, &table->rows) {
536         const struct ovsdb_datum *datum;
537         size_t i;
538
539         datum = &row->fields[column->index];
540         for (i = 0; i < datum->n; i++) {
541             const struct ovsdb_row *ref_row;
542
543             ref_row = ovsdb_table_get_row(ref_table, &datum->keys[i].uuid);
544             if (ref_row) {
545                 update_remote_row(ref_row, txn, statuses);
546             }
547         }
548     }
549 }
550
551 static void
552 update_remote_status(const struct ovsdb_jsonrpc_server *jsonrpc,
553                      const struct sset *remotes, struct ovsdb *db)
554 {
555     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
556     struct shash statuses;
557     struct ovsdb_txn *txn;
558     const bool durable_txn = false;
559     struct ovsdb_error *error;
560     const char *remote;
561
562     /* Get status of current connections. */
563     ovsdb_jsonrpc_server_get_remote_status(jsonrpc, &statuses);
564
565     txn = ovsdb_txn_create(db);
566
567     /* Iterate over --remote arguments given on command line. */
568     SSET_FOR_EACH (remote, remotes) {
569         update_remote_rows(db, txn, remote, &statuses);
570     }
571
572     error = ovsdb_txn_commit(txn, durable_txn);
573     if (error) {
574         VLOG_ERR_RL(&rl, "Failed to update remote status: %s",
575                     ovsdb_error_to_string(error));
576     }
577
578     shash_destroy_free_data(&statuses);
579 }
580
581 /* Reconfigures ovsdb-server based on information in the database. */
582 static void
583 reconfigure_from_db(struct ovsdb_jsonrpc_server *jsonrpc,
584                     const struct ovsdb *db, struct sset *remotes)
585 {
586     struct shash resolved_remotes;
587     const char *name;
588
589     /* Configure remotes. */
590     shash_init(&resolved_remotes);
591     SSET_FOR_EACH (name, remotes) {
592         if (!strncmp(name, "db:", 3)) {
593             query_db_remotes(name, db, &resolved_remotes);
594         } else {
595             add_remote(&resolved_remotes, name);
596         }
597     }
598     ovsdb_jsonrpc_server_set_remotes(jsonrpc, &resolved_remotes);
599     shash_destroy_free_data(&resolved_remotes);
600
601 #if HAVE_OPENSSL
602     /* Configure SSL. */
603     stream_ssl_set_key_and_cert(query_db_string(db, private_key_file),
604                                 query_db_string(db, certificate_file));
605     stream_ssl_set_ca_cert_file(query_db_string(db, ca_cert_file),
606                                 bootstrap_ca_cert);
607 #endif
608 }
609
610 static void
611 ovsdb_server_exit(struct unixctl_conn *conn, const char *args OVS_UNUSED,
612                   void *exiting_)
613 {
614     bool *exiting = exiting_;
615     *exiting = true;
616     unixctl_command_reply(conn, 200, NULL);
617 }
618
619 static void
620 ovsdb_server_compact(struct unixctl_conn *conn, const char *args OVS_UNUSED,
621                      void *file_)
622 {
623     struct ovsdb_file *file = file_;
624     struct ovsdb_error *error;
625
626     VLOG_INFO("compacting database by user request");
627     error = ovsdb_file_compact(file);
628     if (!error) {
629         unixctl_command_reply(conn, 200, NULL);
630     } else {
631         char *s = ovsdb_error_to_string(error);
632         ovsdb_error_destroy(error);
633         unixctl_command_reply(conn, 503, s);
634         free(s);
635     }
636 }
637
638 /* "ovsdb-server/reconnect": makes ovsdb-server drop all of its JSON-RPC
639  * connections and reconnect. */
640 static void
641 ovsdb_server_reconnect(struct unixctl_conn *conn, const char *args OVS_UNUSED,
642                        void *jsonrpc_)
643 {
644     struct ovsdb_jsonrpc_server *jsonrpc = jsonrpc_;
645
646     ovsdb_jsonrpc_server_reconnect(jsonrpc);
647     unixctl_command_reply(conn, 200, NULL);
648 }
649
650 static void
651 parse_options(int argc, char *argv[], char **file_namep,
652               struct sset *remotes, char **unixctl_pathp,
653               char **run_command)
654 {
655     enum {
656         OPT_DUMMY = UCHAR_MAX + 1,
657         OPT_REMOTE,
658         OPT_UNIXCTL,
659         OPT_RUN,
660         OPT_BOOTSTRAP_CA_CERT,
661         VLOG_OPTION_ENUMS,
662         LEAK_CHECKER_OPTION_ENUMS,
663         DAEMON_OPTION_ENUMS
664     };
665     static struct option long_options[] = {
666         {"remote",      required_argument, 0, OPT_REMOTE},
667         {"unixctl",     required_argument, 0, OPT_UNIXCTL},
668         {"run",         required_argument, 0, OPT_RUN},
669         {"help",        no_argument, 0, 'h'},
670         {"version",     no_argument, 0, 'V'},
671         DAEMON_LONG_OPTIONS,
672         VLOG_LONG_OPTIONS,
673         LEAK_CHECKER_LONG_OPTIONS,
674 #ifdef HAVE_OPENSSL
675         {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
676         {"private-key", required_argument, 0, 'p'},
677         {"certificate", required_argument, 0, 'c'},
678         {"ca-cert",     required_argument, 0, 'C'},
679 #endif
680         {0, 0, 0, 0},
681     };
682     char *short_options = long_options_to_short_options(long_options);
683
684     sset_init(remotes);
685     for (;;) {
686         int c;
687
688         c = getopt_long(argc, argv, short_options, long_options, NULL);
689         if (c == -1) {
690             break;
691         }
692
693         switch (c) {
694         case OPT_REMOTE:
695             sset_add(remotes, optarg);
696             break;
697
698         case OPT_UNIXCTL:
699             *unixctl_pathp = optarg;
700             break;
701
702         case OPT_RUN:
703             *run_command = optarg;
704             break;
705
706         case 'h':
707             usage();
708
709         case 'V':
710             OVS_PRINT_VERSION(0, 0);
711             exit(EXIT_SUCCESS);
712
713         VLOG_OPTION_HANDLERS
714         DAEMON_OPTION_HANDLERS
715         LEAK_CHECKER_OPTION_HANDLERS
716
717 #ifdef HAVE_OPENSSL
718         case 'p':
719             private_key_file = optarg;
720             break;
721
722         case 'c':
723             certificate_file = optarg;
724             break;
725
726         case 'C':
727             ca_cert_file = optarg;
728             bootstrap_ca_cert = false;
729             break;
730
731         case OPT_BOOTSTRAP_CA_CERT:
732             ca_cert_file = optarg;
733             bootstrap_ca_cert = true;
734             break;
735 #endif
736
737         case '?':
738             exit(EXIT_FAILURE);
739
740         default:
741             abort();
742         }
743     }
744     free(short_options);
745
746     argc -= optind;
747     argv += optind;
748
749     if (argc > 1) {
750         ovs_fatal(0, "database file is only non-option argument; "
751                 "use --help for usage");
752     } else if (argc < 1) {
753         ovs_fatal(0, "missing database file argument; use --help for usage");
754     }
755
756     *file_namep = argv[0];
757 }
758
759 static void
760 usage(void)
761 {
762     printf("%s: Open vSwitch database server\n"
763            "usage: %s [OPTIONS] DATABASE\n"
764            "where DATABASE is a database file in ovsdb format.\n",
765            program_name, program_name);
766     printf("\nJSON-RPC options (may be specified any number of times):\n"
767            "  --remote=REMOTE         connect or listen to REMOTE\n");
768     stream_usage("JSON-RPC", true, true, true);
769     daemon_usage();
770     vlog_usage();
771     printf("\nOther options:\n"
772            "  --run COMMAND           run COMMAND as subprocess then exit\n"
773            "  --unixctl=SOCKET        override default control socket name\n"
774            "  -h, --help              display this help message\n"
775            "  -V, --version           display version information\n");
776     leak_checker_usage();
777     exit(EXIT_SUCCESS);
778 }