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