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