lib/daemon: support --user option for all OVS daemon
[cascardo/ovs.git] / ovsdb / ovsdb-server.c
1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 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 <errno.h>
19 #include <getopt.h>
20 #include <inttypes.h>
21 #include <signal.h>
22 #include <sys/stat.h>
23 #include <unistd.h>
24
25 #include "column.h"
26 #include "command-line.h"
27 #include "daemon.h"
28 #include "dirs.h"
29 #include "dynamic-string.h"
30 #include "fatal-signal.h"
31 #include "file.h"
32 #include "hash.h"
33 #include "json.h"
34 #include "jsonrpc.h"
35 #include "jsonrpc-server.h"
36 #include "list.h"
37 #include "memory.h"
38 #include "ovsdb.h"
39 #include "ovsdb-data.h"
40 #include "ovsdb-types.h"
41 #include "ovsdb-error.h"
42 #include "poll-loop.h"
43 #include "process.h"
44 #include "row.h"
45 #include "simap.h"
46 #include "shash.h"
47 #include "stream-ssl.h"
48 #include "stream.h"
49 #include "sset.h"
50 #include "table.h"
51 #include "timeval.h"
52 #include "transaction.h"
53 #include "trigger.h"
54 #include "util.h"
55 #include "unixctl.h"
56 #include "perf-counter.h"
57 #include "openvswitch/vlog.h"
58
59 VLOG_DEFINE_THIS_MODULE(ovsdb_server);
60
61 struct db {
62     /* Initialized in main(). */
63     char *filename;
64     struct ovsdb_file *file;
65     struct ovsdb *db;
66
67     /* Only used by update_remote_status(). */
68     struct ovsdb_txn *txn;
69 };
70
71 /* SSL configuration. */
72 static char *private_key_file;
73 static char *certificate_file;
74 static char *ca_cert_file;
75 static bool bootstrap_ca_cert;
76
77 static unixctl_cb_func ovsdb_server_exit;
78 static unixctl_cb_func ovsdb_server_compact;
79 static unixctl_cb_func ovsdb_server_reconnect;
80 static unixctl_cb_func ovsdb_server_perf_counters_clear;
81 static unixctl_cb_func ovsdb_server_perf_counters_show;
82
83 struct server_config {
84     struct sset *remotes;
85     struct shash *all_dbs;
86     FILE *config_tmpfile;
87     struct ovsdb_jsonrpc_server *jsonrpc;
88 };
89 static unixctl_cb_func ovsdb_server_add_remote;
90 static unixctl_cb_func ovsdb_server_remove_remote;
91 static unixctl_cb_func ovsdb_server_list_remotes;
92
93 static unixctl_cb_func ovsdb_server_add_database;
94 static unixctl_cb_func ovsdb_server_remove_database;
95 static unixctl_cb_func ovsdb_server_list_databases;
96
97 static char *open_db(struct server_config *config, const char *filename);
98 static void close_db(struct db *db);
99
100 static void parse_options(int *argc, char **argvp[],
101                           struct sset *remotes, char **unixctl_pathp,
102                           char **run_command);
103 OVS_NO_RETURN static void usage(void);
104
105 static char *reconfigure_remotes(struct ovsdb_jsonrpc_server *,
106                                  const struct shash *all_dbs,
107                                  struct sset *remotes);
108 static char *reconfigure_ssl(const struct shash *all_dbs);
109 static void report_error_if_changed(char *error, char **last_errorp);
110
111 static void update_remote_status(const struct ovsdb_jsonrpc_server *jsonrpc,
112                                  const struct sset *remotes,
113                                  struct shash *all_dbs);
114
115 static void save_config__(FILE *config_file, const struct sset *remotes,
116                           const struct sset *db_filenames);
117 static void save_config(struct server_config *);
118 static void load_config(FILE *config_file, struct sset *remotes,
119                         struct sset *db_filenames);
120
121 static void
122 main_loop(struct ovsdb_jsonrpc_server *jsonrpc, struct shash *all_dbs,
123           struct unixctl_server *unixctl, struct sset *remotes,
124           struct process *run_process, bool *exiting)
125 {
126     char *remotes_error, *ssl_error;
127     struct shash_node *node;
128     long long int status_timer = LLONG_MIN;
129
130     *exiting = false;
131     ssl_error = NULL;
132     remotes_error = NULL;
133     while (!*exiting) {
134         memory_run();
135         if (memory_should_report()) {
136             struct simap usage;
137
138             simap_init(&usage);
139             ovsdb_jsonrpc_server_get_memory_usage(jsonrpc, &usage);
140             SHASH_FOR_EACH(node, all_dbs) {
141                 struct db *db = node->data;
142                 ovsdb_get_memory_usage(db->db, &usage);
143             }
144             memory_report(&usage);
145             simap_destroy(&usage);
146         }
147
148         /* Run unixctl_server_run() before reconfigure_remotes() because
149          * ovsdb-server/add-remote and ovsdb-server/remove-remote can change
150          * the set of remotes that reconfigure_remotes() uses. */
151         unixctl_server_run(unixctl);
152
153         report_error_if_changed(
154             reconfigure_remotes(jsonrpc, all_dbs, remotes),
155             &remotes_error);
156         report_error_if_changed(reconfigure_ssl(all_dbs), &ssl_error);
157         ovsdb_jsonrpc_server_run(jsonrpc);
158
159         SHASH_FOR_EACH(node, all_dbs) {
160             struct db *db = node->data;
161             ovsdb_trigger_run(db->db, time_msec());
162         }
163         if (run_process) {
164             process_run();
165             if (process_exited(run_process)) {
166                 *exiting = true;
167             }
168         }
169
170         /* update Manager status(es) every 5 seconds */
171         if (time_msec() >= status_timer) {
172             status_timer = time_msec() + 5000;
173             update_remote_status(jsonrpc, remotes, all_dbs);
174         }
175
176         memory_wait();
177         ovsdb_jsonrpc_server_wait(jsonrpc);
178         unixctl_server_wait(unixctl);
179         SHASH_FOR_EACH(node, all_dbs) {
180             struct db *db = node->data;
181             ovsdb_trigger_wait(db->db, time_msec());
182         }
183         if (run_process) {
184             process_wait(run_process);
185         }
186         if (*exiting) {
187             poll_immediate_wake();
188         }
189         poll_timer_wait_until(status_timer);
190         poll_block();
191         if (should_service_stop()) {
192             *exiting = true;
193         }
194     }
195
196 }
197
198 int
199 main(int argc, char *argv[])
200 {
201     char *unixctl_path = NULL;
202     char *run_command = NULL;
203     struct unixctl_server *unixctl;
204     struct ovsdb_jsonrpc_server *jsonrpc;
205     struct sset remotes, db_filenames;
206     const char *db_filename;
207     struct process *run_process;
208     bool exiting;
209     int retval;
210     FILE *config_tmpfile;
211     struct server_config server_config;
212     struct shash all_dbs;
213     struct shash_node *node, *next;
214     char *error;
215     int i;
216
217     ovs_cmdl_proctitle_init(argc, argv);
218     set_program_name(argv[0]);
219     service_start(&argc, &argv);
220     fatal_ignore_sigpipe();
221     process_init();
222
223     parse_options(&argc, &argv, &remotes, &unixctl_path, &run_command);
224     daemon_become_new_user(false);
225
226     /* Create and initialize 'config_tmpfile' as a temporary file to hold
227      * ovsdb-server's most basic configuration, and then save our initial
228      * configuration to it.  When --monitor is used, this preserves the effects
229      * of ovs-appctl commands such as ovsdb-server/add-remote (which saves the
230      * new configuration) across crashes. */
231     config_tmpfile = tmpfile();
232     if (!config_tmpfile) {
233         ovs_fatal(errno, "failed to create temporary file");
234     }
235
236     sset_init(&db_filenames);
237     if (argc > 0) {
238         for (i = 0; i < argc; i++) {
239             sset_add(&db_filenames, argv[i]);
240          }
241     } else {
242         char *default_db = xasprintf("%s/conf.db", ovs_dbdir());
243         sset_add(&db_filenames, default_db);
244         free(default_db);
245     }
246
247     server_config.remotes = &remotes;
248     server_config.config_tmpfile = config_tmpfile;
249
250     save_config__(config_tmpfile, &remotes, &db_filenames);
251
252     daemonize_start(false);
253
254     /* Load the saved config. */
255     load_config(config_tmpfile, &remotes, &db_filenames);
256     jsonrpc = ovsdb_jsonrpc_server_create();
257
258     shash_init(&all_dbs);
259     server_config.all_dbs = &all_dbs;
260     server_config.jsonrpc = jsonrpc;
261     SSET_FOR_EACH (db_filename, &db_filenames) {
262         error = open_db(&server_config, db_filename);
263         if (error) {
264             ovs_fatal(0, "%s", error);
265         }
266     }
267
268     error = reconfigure_remotes(jsonrpc, &all_dbs, &remotes);
269     if (!error) {
270         error = reconfigure_ssl(&all_dbs);
271     }
272     if (error) {
273         ovs_fatal(0, "%s", error);
274     }
275
276     retval = unixctl_server_create(unixctl_path, &unixctl);
277     if (retval) {
278         exit(EXIT_FAILURE);
279     }
280
281     if (run_command) {
282         char *run_argv[4];
283
284         run_argv[0] = "/bin/sh";
285         run_argv[1] = "-c";
286         run_argv[2] = run_command;
287         run_argv[3] = NULL;
288
289         retval = process_start(run_argv, &run_process);
290         if (retval) {
291             ovs_fatal(retval, "%s: process failed to start", run_command);
292         }
293     } else {
294         run_process = NULL;
295     }
296
297     daemonize_complete();
298
299     perf_counters_init();
300
301     if (!run_command) {
302         /* ovsdb-server is usually a long-running process, in which case it
303          * makes plenty of sense to log the version, but --run makes
304          * ovsdb-server more like a command-line tool, so skip it.  */
305         VLOG_INFO("%s (Open vSwitch) %s", program_name, VERSION);
306     }
307
308     unixctl_command_register("exit", "", 0, 0, ovsdb_server_exit, &exiting);
309     unixctl_command_register("ovsdb-server/compact", "", 0, 1,
310                              ovsdb_server_compact, &all_dbs);
311     unixctl_command_register("ovsdb-server/reconnect", "", 0, 0,
312                              ovsdb_server_reconnect, jsonrpc);
313
314     unixctl_command_register("ovsdb-server/add-remote", "REMOTE", 1, 1,
315                              ovsdb_server_add_remote, &server_config);
316     unixctl_command_register("ovsdb-server/remove-remote", "REMOTE", 1, 1,
317                              ovsdb_server_remove_remote, &server_config);
318     unixctl_command_register("ovsdb-server/list-remotes", "", 0, 0,
319                              ovsdb_server_list_remotes, &remotes);
320
321     unixctl_command_register("ovsdb-server/add-db", "DB", 1, 1,
322                              ovsdb_server_add_database, &server_config);
323     unixctl_command_register("ovsdb-server/remove-db", "DB", 1, 1,
324                              ovsdb_server_remove_database, &server_config);
325     unixctl_command_register("ovsdb-server/list-dbs", "", 0, 0,
326                              ovsdb_server_list_databases, &all_dbs);
327     unixctl_command_register("ovsdb-server/perf-counters-show", "", 0, 0,
328                              ovsdb_server_perf_counters_show, NULL);
329     unixctl_command_register("ovsdb-server/perf-counters-clear", "", 0, 0,
330                              ovsdb_server_perf_counters_clear, NULL);
331
332     main_loop(jsonrpc, &all_dbs, unixctl, &remotes, run_process, &exiting);
333
334     ovsdb_jsonrpc_server_destroy(jsonrpc);
335     SHASH_FOR_EACH_SAFE(node, next, &all_dbs) {
336         struct db *db = node->data;
337         close_db(db);
338         shash_delete(&all_dbs, node);
339     }
340     sset_destroy(&remotes);
341     sset_destroy(&db_filenames);
342     unixctl_server_destroy(unixctl);
343
344     if (run_process && process_exited(run_process)) {
345         int status = process_status(run_process);
346         if (status) {
347             ovs_fatal(0, "%s: child exited, %s",
348                       run_command, process_status_msg(status));
349         }
350     }
351     perf_counters_destroy();
352     service_stop();
353     return 0;
354 }
355
356 /* Returns true if 'filename' is known to be already open as a database,
357  * false if not.
358  *
359  * "False negatives" are possible. */
360 static bool
361 is_already_open(struct server_config *config OVS_UNUSED,
362                 const char *filename OVS_UNUSED)
363 {
364 #ifndef _WIN32
365     struct stat s;
366
367     if (!stat(filename, &s)) {
368         struct shash_node *node;
369
370         SHASH_FOR_EACH (node, config->all_dbs) {
371             struct db *db = node->data;
372             struct stat s2;
373
374             if (!stat(db->filename, &s2)
375                 && s.st_dev == s2.st_dev
376                 && s.st_ino == s2.st_ino) {
377                 return true;
378             }
379         }
380     }
381 #endif  /* !_WIN32 */
382
383     return false;
384 }
385
386 static void
387 close_db(struct db *db)
388 {
389     ovsdb_destroy(db->db);
390     free(db->filename);
391     free(db);
392 }
393
394 static char *
395 open_db(struct server_config *config, const char *filename)
396 {
397     struct ovsdb_error *db_error;
398     struct db *db;
399     char *error;
400
401     /* If we know that the file is already open, return a good error message.
402      * Otherwise, if the file is open, we'll fail later on with a harder to
403      * interpret file locking error. */
404     if (is_already_open(config, filename)) {
405         return xasprintf("%s: already open", filename);
406     }
407
408     db = xzalloc(sizeof *db);
409     db->filename = xstrdup(filename);
410
411     db_error = ovsdb_file_open(db->filename, false, &db->db, &db->file);
412     if (db_error) {
413         error = ovsdb_error_to_string(db_error);
414     } else if (!ovsdb_jsonrpc_server_add_db(config->jsonrpc, db->db)) {
415         error = xasprintf("%s: duplicate database name", db->db->schema->name);
416     } else {
417         shash_add_assert(config->all_dbs, db->db->schema->name, db);
418         return NULL;
419     }
420
421     ovsdb_error_destroy(db_error);
422     close_db(db);
423     return error;
424 }
425
426 static const struct db *
427 find_db(const struct shash *all_dbs, const char *db_name)
428 {
429     struct shash_node *node;
430
431     SHASH_FOR_EACH(node, all_dbs) {
432         struct db *db = node->data;
433         if (!strcmp(db->db->schema->name, db_name)) {
434             return db;
435         }
436     }
437
438     return NULL;
439 }
440
441 static char * OVS_WARN_UNUSED_RESULT
442 parse_db_column__(const struct shash *all_dbs,
443                   const char *name_, char *name,
444                   const struct db **dbp,
445                   const struct ovsdb_table **tablep,
446                   const struct ovsdb_column **columnp)
447 {
448     const char *db_name, *table_name, *column_name;
449     const struct ovsdb_column *column;
450     const struct ovsdb_table *table;
451     const char *tokens[3];
452     char *save_ptr = NULL;
453     const struct db *db;
454
455     *dbp = NULL;
456     *tablep = NULL;
457     *columnp = NULL;
458
459     strtok_r(name, ":", &save_ptr); /* "db:" */
460     tokens[0] = strtok_r(NULL, ",", &save_ptr);
461     tokens[1] = strtok_r(NULL, ",", &save_ptr);
462     tokens[2] = strtok_r(NULL, ",", &save_ptr);
463     if (!tokens[0] || !tokens[1] || !tokens[2]) {
464         return xasprintf("\"%s\": invalid syntax", name_);
465     }
466
467     db_name = tokens[0];
468     table_name = tokens[1];
469     column_name = tokens[2];
470
471     db = find_db(all_dbs, tokens[0]);
472     if (!db) {
473         return xasprintf("\"%s\": no database named %s", name_, db_name);
474     }
475
476     table = ovsdb_get_table(db->db, table_name);
477     if (!table) {
478         return xasprintf("\"%s\": no table named %s", name_, table_name);
479     }
480
481     column = ovsdb_table_schema_get_column(table->schema, column_name);
482     if (!column) {
483         return xasprintf("\"%s\": table \"%s\" has no column \"%s\"",
484                          name_, table_name, column_name);
485     }
486
487     *dbp = db;
488     *columnp = column;
489     *tablep = table;
490     return NULL;
491 }
492
493 /* Returns NULL if successful, otherwise a malloc()'d string describing the
494  * error. */
495 static char * OVS_WARN_UNUSED_RESULT
496 parse_db_column(const struct shash *all_dbs,
497                 const char *name_,
498                 const struct db **dbp,
499                 const struct ovsdb_table **tablep,
500                 const struct ovsdb_column **columnp)
501 {
502     char *name = xstrdup(name_);
503     char *retval = parse_db_column__(all_dbs, name_, name,
504                                      dbp, tablep, columnp);
505     free(name);
506     return retval;
507 }
508
509 /* Returns NULL if successful, otherwise a malloc()'d string describing the
510  * error. */
511 static char * OVS_WARN_UNUSED_RESULT
512 parse_db_string_column(const struct shash *all_dbs,
513                        const char *name,
514                        const struct db **dbp,
515                        const struct ovsdb_table **tablep,
516                        const struct ovsdb_column **columnp)
517 {
518     char *retval;
519
520     retval = parse_db_column(all_dbs, name, dbp, tablep, columnp);
521     if (retval) {
522         return retval;
523     }
524
525     if ((*columnp)->type.key.type != OVSDB_TYPE_STRING
526         || (*columnp)->type.value.type != OVSDB_TYPE_VOID) {
527         return xasprintf("\"%s\": table \"%s\" column \"%s\" is "
528                          "not string or set of strings",
529                          name, (*tablep)->schema->name, (*columnp)->name);
530     }
531
532     return NULL;
533 }
534
535 static const char *
536 query_db_string(const struct shash *all_dbs, const char *name,
537                 struct ds *errors)
538 {
539     if (!name || strncmp(name, "db:", 3)) {
540         return name;
541     } else {
542         const struct ovsdb_column *column;
543         const struct ovsdb_table *table;
544         const struct ovsdb_row *row;
545         const struct db *db;
546         char *retval;
547
548         retval = parse_db_string_column(all_dbs, name,
549                                         &db, &table, &column);
550         if (retval) {
551             ds_put_format(errors, "%s\n", retval);
552             free(retval);
553             return NULL;
554         }
555
556         HMAP_FOR_EACH (row, hmap_node, &table->rows) {
557             const struct ovsdb_datum *datum;
558             size_t i;
559
560             datum = &row->fields[column->index];
561             for (i = 0; i < datum->n; i++) {
562                 if (datum->keys[i].string[0]) {
563                     return datum->keys[i].string;
564                 }
565             }
566         }
567         return NULL;
568     }
569 }
570
571 static struct ovsdb_jsonrpc_options *
572 add_remote(struct shash *remotes, const char *target)
573 {
574     struct ovsdb_jsonrpc_options *options;
575
576     options = shash_find_data(remotes, target);
577     if (!options) {
578         options = ovsdb_jsonrpc_default_options(target);
579         shash_add(remotes, target, options);
580     }
581
582     return options;
583 }
584
585 static struct ovsdb_datum *
586 get_datum(struct ovsdb_row *row, const char *column_name,
587           const enum ovsdb_atomic_type key_type,
588           const enum ovsdb_atomic_type value_type,
589           const size_t n_max)
590 {
591     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
592     const struct ovsdb_table_schema *schema = row->table->schema;
593     const struct ovsdb_column *column;
594
595     column = ovsdb_table_schema_get_column(schema, column_name);
596     if (!column) {
597         VLOG_DBG_RL(&rl, "Table `%s' has no `%s' column",
598                     schema->name, column_name);
599         return NULL;
600     }
601
602     if (column->type.key.type != key_type
603         || column->type.value.type != value_type
604         || column->type.n_max != n_max) {
605         if (!VLOG_DROP_DBG(&rl)) {
606             char *type_name = ovsdb_type_to_english(&column->type);
607             VLOG_DBG("Table `%s' column `%s' has type %s, not expected "
608                      "key type %s, value type %s, max elements %"PRIuSIZE".",
609                      schema->name, column_name, type_name,
610                      ovsdb_atomic_type_to_string(key_type),
611                      ovsdb_atomic_type_to_string(value_type),
612                      n_max);
613             free(type_name);
614         }
615         return NULL;
616     }
617
618     return &row->fields[column->index];
619 }
620
621 /* Read string-string key-values from a map.  Returns the value associated with
622  * 'key', if found, or NULL */
623 static const char *
624 read_map_string_column(const struct ovsdb_row *row, const char *column_name,
625                        const char *key)
626 {
627     const struct ovsdb_datum *datum;
628     union ovsdb_atom *atom_key = NULL, *atom_value = NULL;
629     size_t i;
630
631     datum = get_datum(CONST_CAST(struct ovsdb_row *, row), column_name,
632                       OVSDB_TYPE_STRING, OVSDB_TYPE_STRING, UINT_MAX);
633
634     if (!datum) {
635         return NULL;
636     }
637
638     for (i = 0; i < datum->n; i++) {
639         atom_key = &datum->keys[i];
640         if (!strcmp(atom_key->string, key)){
641             atom_value = &datum->values[i];
642             break;
643         }
644     }
645
646     return atom_value ? atom_value->string : NULL;
647 }
648
649 static const union ovsdb_atom *
650 read_column(const struct ovsdb_row *row, const char *column_name,
651             enum ovsdb_atomic_type type)
652 {
653     const struct ovsdb_datum *datum;
654
655     datum = get_datum(CONST_CAST(struct ovsdb_row *, row), column_name, type,
656                       OVSDB_TYPE_VOID, 1);
657     return datum && datum->n ? datum->keys : NULL;
658 }
659
660 static bool
661 read_integer_column(const struct ovsdb_row *row, const char *column_name,
662                     long long int *integerp)
663 {
664     const union ovsdb_atom *atom;
665
666     atom = read_column(row, column_name, OVSDB_TYPE_INTEGER);
667     *integerp = atom ? atom->integer : 0;
668     return atom != NULL;
669 }
670
671 static bool
672 read_string_column(const struct ovsdb_row *row, const char *column_name,
673                    const char **stringp)
674 {
675     const union ovsdb_atom *atom;
676
677     atom = read_column(row, column_name, OVSDB_TYPE_STRING);
678     *stringp = atom ? atom->string : NULL;
679     return atom != NULL;
680 }
681
682 static void
683 write_bool_column(struct ovsdb_row *row, const char *column_name, bool value)
684 {
685     const struct ovsdb_column *column;
686     struct ovsdb_datum *datum;
687
688     column = ovsdb_table_schema_get_column(row->table->schema, column_name);
689     datum = get_datum(row, column_name, OVSDB_TYPE_BOOLEAN,
690                       OVSDB_TYPE_VOID, 1);
691     if (!datum) {
692         return;
693     }
694
695     if (datum->n != 1) {
696         ovsdb_datum_destroy(datum, &column->type);
697
698         datum->n = 1;
699         datum->keys = xmalloc(sizeof *datum->keys);
700         datum->values = NULL;
701     }
702
703     datum->keys[0].boolean = value;
704 }
705
706 static void
707 write_string_string_column(struct ovsdb_row *row, const char *column_name,
708                            char **keys, char **values, size_t n)
709 {
710     const struct ovsdb_column *column;
711     struct ovsdb_datum *datum;
712     size_t i;
713
714     column = ovsdb_table_schema_get_column(row->table->schema, column_name);
715     datum = get_datum(row, column_name, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING,
716                       UINT_MAX);
717     if (!datum) {
718         for (i = 0; i < n; i++) {
719             free(keys[i]);
720             free(values[i]);
721         }
722         return;
723     }
724
725     /* Free existing data. */
726     ovsdb_datum_destroy(datum, &column->type);
727
728     /* Allocate space for new values. */
729     datum->n = n;
730     datum->keys = xmalloc(n * sizeof *datum->keys);
731     datum->values = xmalloc(n * sizeof *datum->values);
732
733     for (i = 0; i < n; ++i) {
734         datum->keys[i].string = keys[i];
735         datum->values[i].string = values[i];
736     }
737
738     /* Sort and check constraints. */
739     ovsdb_datum_sort_assert(datum, column->type.key.type);
740 }
741
742 /* Adds a remote and options to 'remotes', based on the Manager table row in
743  * 'row'. */
744 static void
745 add_manager_options(struct shash *remotes, const struct ovsdb_row *row)
746 {
747     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
748     struct ovsdb_jsonrpc_options *options;
749     long long int max_backoff, probe_interval;
750     const char *target, *dscp_string;
751
752     if (!read_string_column(row, "target", &target) || !target) {
753         VLOG_INFO_RL(&rl, "Table `%s' has missing or invalid `target' column",
754                      row->table->schema->name);
755         return;
756     }
757
758     options = add_remote(remotes, target);
759     if (read_integer_column(row, "max_backoff", &max_backoff)) {
760         options->max_backoff = max_backoff;
761     }
762     if (read_integer_column(row, "inactivity_probe", &probe_interval)) {
763         options->probe_interval = probe_interval;
764     }
765
766     options->dscp = DSCP_DEFAULT;
767     dscp_string = read_map_string_column(row, "other_config", "dscp");
768     if (dscp_string) {
769         int dscp = atoi(dscp_string);
770         if (dscp >= 0 && dscp <= 63) {
771             options->dscp = dscp;
772         }
773     }
774 }
775
776 static void
777 query_db_remotes(const char *name, const struct shash *all_dbs,
778                  struct shash *remotes, struct ds *errors)
779 {
780     const struct ovsdb_column *column;
781     const struct ovsdb_table *table;
782     const struct ovsdb_row *row;
783     const struct db *db;
784     char *retval;
785
786     retval = parse_db_column(all_dbs, name, &db, &table, &column);
787     if (retval) {
788         ds_put_format(errors, "%s\n", retval);
789         free(retval);
790         return;
791     }
792
793     if (column->type.key.type == OVSDB_TYPE_STRING
794         && column->type.value.type == OVSDB_TYPE_VOID) {
795         HMAP_FOR_EACH (row, hmap_node, &table->rows) {
796             const struct ovsdb_datum *datum;
797             size_t i;
798
799             datum = &row->fields[column->index];
800             for (i = 0; i < datum->n; i++) {
801                 add_remote(remotes, datum->keys[i].string);
802             }
803         }
804     } else if (column->type.key.type == OVSDB_TYPE_UUID
805                && column->type.key.u.uuid.refTable
806                && column->type.value.type == OVSDB_TYPE_VOID) {
807         const struct ovsdb_table *ref_table = column->type.key.u.uuid.refTable;
808         HMAP_FOR_EACH (row, hmap_node, &table->rows) {
809             const struct ovsdb_datum *datum;
810             size_t i;
811
812             datum = &row->fields[column->index];
813             for (i = 0; i < datum->n; i++) {
814                 const struct ovsdb_row *ref_row;
815
816                 ref_row = ovsdb_table_get_row(ref_table, &datum->keys[i].uuid);
817                 if (ref_row) {
818                     add_manager_options(remotes, ref_row);
819                 }
820             }
821         }
822     }
823 }
824
825 static void
826 update_remote_row(const struct ovsdb_row *row, struct ovsdb_txn *txn,
827                   const struct ovsdb_jsonrpc_server *jsonrpc)
828 {
829     struct ovsdb_jsonrpc_remote_status status;
830     struct ovsdb_row *rw_row;
831     const char *target;
832     char *keys[9], *values[9];
833     size_t n = 0;
834
835     /* Get the "target" (protocol/host/port) spec. */
836     if (!read_string_column(row, "target", &target)) {
837         /* Bad remote spec or incorrect schema. */
838         return;
839     }
840     rw_row = ovsdb_txn_row_modify(txn, row);
841     ovsdb_jsonrpc_server_get_remote_status(jsonrpc, target, &status);
842
843     /* Update status information columns. */
844     write_bool_column(rw_row, "is_connected", status.is_connected);
845
846     if (status.state) {
847         keys[n] = xstrdup("state");
848         values[n++] = xstrdup(status.state);
849     }
850     if (status.sec_since_connect != UINT_MAX) {
851         keys[n] = xstrdup("sec_since_connect");
852         values[n++] = xasprintf("%u", status.sec_since_connect);
853     }
854     if (status.sec_since_disconnect != UINT_MAX) {
855         keys[n] = xstrdup("sec_since_disconnect");
856         values[n++] = xasprintf("%u", status.sec_since_disconnect);
857     }
858     if (status.last_error) {
859         keys[n] = xstrdup("last_error");
860         values[n++] =
861             xstrdup(ovs_retval_to_string(status.last_error));
862     }
863     if (status.locks_held && status.locks_held[0]) {
864         keys[n] = xstrdup("locks_held");
865         values[n++] = xstrdup(status.locks_held);
866     }
867     if (status.locks_waiting && status.locks_waiting[0]) {
868         keys[n] = xstrdup("locks_waiting");
869         values[n++] = xstrdup(status.locks_waiting);
870     }
871     if (status.locks_lost && status.locks_lost[0]) {
872         keys[n] = xstrdup("locks_lost");
873         values[n++] = xstrdup(status.locks_lost);
874     }
875     if (status.n_connections > 1) {
876         keys[n] = xstrdup("n_connections");
877         values[n++] = xasprintf("%d", status.n_connections);
878     }
879     if (status.bound_port != htons(0)) {
880         keys[n] = xstrdup("bound_port");
881         values[n++] = xasprintf("%"PRIu16, ntohs(status.bound_port));
882     }
883     write_string_string_column(rw_row, "status", keys, values, n);
884
885     ovsdb_jsonrpc_server_free_remote_status(&status);
886 }
887
888 static void
889 update_remote_rows(const struct shash *all_dbs,
890                    const char *remote_name,
891                    const struct ovsdb_jsonrpc_server *jsonrpc)
892 {
893     const struct ovsdb_table *table, *ref_table;
894     const struct ovsdb_column *column;
895     const struct ovsdb_row *row;
896     const struct db *db;
897     char *retval;
898
899     if (strncmp("db:", remote_name, 3)) {
900         return;
901     }
902
903     retval = parse_db_column(all_dbs, remote_name, &db, &table, &column);
904     if (retval) {
905         free(retval);
906         return;
907     }
908
909     if (column->type.key.type != OVSDB_TYPE_UUID
910         || !column->type.key.u.uuid.refTable
911         || column->type.value.type != OVSDB_TYPE_VOID) {
912         return;
913     }
914
915     ref_table = column->type.key.u.uuid.refTable;
916
917     HMAP_FOR_EACH (row, hmap_node, &table->rows) {
918         const struct ovsdb_datum *datum;
919         size_t i;
920
921         datum = &row->fields[column->index];
922         for (i = 0; i < datum->n; i++) {
923             const struct ovsdb_row *ref_row;
924
925             ref_row = ovsdb_table_get_row(ref_table, &datum->keys[i].uuid);
926             if (ref_row) {
927                 update_remote_row(ref_row, db->txn, jsonrpc);
928             }
929         }
930     }
931 }
932
933 static void
934 update_remote_status(const struct ovsdb_jsonrpc_server *jsonrpc,
935                      const struct sset *remotes,
936                      struct shash *all_dbs)
937 {
938     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
939     const char *remote;
940     struct db *db;
941     struct shash_node *node;
942
943     SHASH_FOR_EACH(node, all_dbs) {
944         db = node->data;
945         db->txn = ovsdb_txn_create(db->db);
946     }
947
948     /* Iterate over --remote arguments given on command line. */
949     SSET_FOR_EACH (remote, remotes) {
950         update_remote_rows(all_dbs, remote, jsonrpc);
951     }
952
953     SHASH_FOR_EACH(node, all_dbs) {
954         struct ovsdb_error *error;
955         db = node->data;
956         error = ovsdb_txn_commit(db->txn, false);
957         if (error) {
958             VLOG_ERR_RL(&rl, "Failed to update remote status: %s",
959                         ovsdb_error_to_string(error));
960             ovsdb_error_destroy(error);
961         }
962     }
963 }
964
965 /* Reconfigures ovsdb-server's remotes based on information in the database. */
966 static char *
967 reconfigure_remotes(struct ovsdb_jsonrpc_server *jsonrpc,
968                     const struct shash *all_dbs, struct sset *remotes)
969 {
970     struct ds errors = DS_EMPTY_INITIALIZER;
971     struct shash resolved_remotes;
972     const char *name;
973
974     /* Configure remotes. */
975     shash_init(&resolved_remotes);
976     SSET_FOR_EACH (name, remotes) {
977         if (!strncmp(name, "db:", 3)) {
978             query_db_remotes(name, all_dbs, &resolved_remotes, &errors);
979         } else {
980             add_remote(&resolved_remotes, name);
981         }
982     }
983     ovsdb_jsonrpc_server_set_remotes(jsonrpc, &resolved_remotes);
984     shash_destroy_free_data(&resolved_remotes);
985
986     return errors.string;
987 }
988
989 static char *
990 reconfigure_ssl(const struct shash *all_dbs)
991 {
992     struct ds errors = DS_EMPTY_INITIALIZER;
993     const char *resolved_private_key;
994     const char *resolved_certificate;
995     const char *resolved_ca_cert;
996
997     resolved_private_key = query_db_string(all_dbs, private_key_file, &errors);
998     resolved_certificate = query_db_string(all_dbs, certificate_file, &errors);
999     resolved_ca_cert = query_db_string(all_dbs, ca_cert_file, &errors);
1000
1001     stream_ssl_set_key_and_cert(resolved_private_key, resolved_certificate);
1002     stream_ssl_set_ca_cert_file(resolved_ca_cert, bootstrap_ca_cert);
1003
1004     return errors.string;
1005 }
1006
1007 static void
1008 report_error_if_changed(char *error, char **last_errorp)
1009 {
1010     if (error) {
1011         if (!*last_errorp || strcmp(error, *last_errorp)) {
1012             VLOG_WARN("%s", error);
1013             free(*last_errorp);
1014             *last_errorp = error;
1015             return;
1016         }
1017         free(error);
1018     } else {
1019         free(*last_errorp);
1020         *last_errorp = NULL;
1021     }
1022 }
1023
1024 static void
1025 ovsdb_server_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
1026                   const char *argv[] OVS_UNUSED,
1027                   void *exiting_)
1028 {
1029     bool *exiting = exiting_;
1030     *exiting = true;
1031     unixctl_command_reply(conn, NULL);
1032 }
1033
1034 static void
1035 ovsdb_server_perf_counters_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
1036                                 const char *argv[] OVS_UNUSED,
1037                                 void *arg_ OVS_UNUSED)
1038 {
1039     char *s = perf_counters_to_string();
1040
1041     unixctl_command_reply(conn, s);
1042     free(s);
1043 }
1044
1045 static void
1046 ovsdb_server_perf_counters_clear(struct unixctl_conn *conn, int argc OVS_UNUSED,
1047                                  const char *argv[] OVS_UNUSED,
1048                                  void *arg_ OVS_UNUSED)
1049 {
1050     perf_counters_clear();
1051     unixctl_command_reply(conn, NULL);
1052 }
1053
1054 static void
1055 ovsdb_server_compact(struct unixctl_conn *conn, int argc,
1056                      const char *argv[], void *dbs_)
1057 {
1058     struct shash *all_dbs = dbs_;
1059     struct ds reply;
1060     struct db *db;
1061     struct shash_node *node;
1062     int n = 0;
1063
1064     ds_init(&reply);
1065     SHASH_FOR_EACH(node, all_dbs) {
1066         const char *name;
1067
1068         db = node->data;
1069         name = db->db->schema->name;
1070
1071         if (argc < 2 || !strcmp(argv[1], name)) {
1072             struct ovsdb_error *error;
1073
1074             VLOG_INFO("compacting %s database by user request", name);
1075
1076             error = ovsdb_file_compact(db->file);
1077             if (error) {
1078                 char *s = ovsdb_error_to_string(error);
1079                 ds_put_format(&reply, "%s\n", s);
1080                 free(s);
1081                 ovsdb_error_destroy(error);
1082             }
1083
1084             n++;
1085         }
1086     }
1087
1088     if (!n) {
1089         unixctl_command_reply_error(conn, "no database by that name");
1090     } else if (reply.length) {
1091         unixctl_command_reply_error(conn, ds_cstr(&reply));
1092     } else {
1093         unixctl_command_reply(conn, NULL);
1094     }
1095     ds_destroy(&reply);
1096 }
1097
1098 /* "ovsdb-server/reconnect": makes ovsdb-server drop all of its JSON-RPC
1099  * connections and reconnect. */
1100 static void
1101 ovsdb_server_reconnect(struct unixctl_conn *conn, int argc OVS_UNUSED,
1102                        const char *argv[] OVS_UNUSED, void *jsonrpc_)
1103 {
1104     struct ovsdb_jsonrpc_server *jsonrpc = jsonrpc_;
1105
1106     ovsdb_jsonrpc_server_reconnect(jsonrpc);
1107     unixctl_command_reply(conn, NULL);
1108 }
1109
1110 /* "ovsdb-server/add-remote REMOTE": adds REMOTE to the set of remotes that
1111  * ovsdb-server services. */
1112 static void
1113 ovsdb_server_add_remote(struct unixctl_conn *conn, int argc OVS_UNUSED,
1114                         const char *argv[], void *config_)
1115 {
1116     struct server_config *config = config_;
1117     const char *remote = argv[1];
1118
1119     const struct ovsdb_column *column;
1120     const struct ovsdb_table *table;
1121     const struct db *db;
1122     char *retval;
1123
1124     retval = (strncmp("db:", remote, 3)
1125               ? NULL
1126               : parse_db_column(config->all_dbs, remote,
1127                                 &db, &table, &column));
1128     if (!retval) {
1129         if (sset_add(config->remotes, remote)) {
1130             save_config(config);
1131         }
1132         unixctl_command_reply(conn, NULL);
1133     } else {
1134         unixctl_command_reply_error(conn, retval);
1135         free(retval);
1136     }
1137 }
1138
1139 /* "ovsdb-server/remove-remote REMOTE": removes REMOTE frmo the set of remotes
1140  * that ovsdb-server services. */
1141 static void
1142 ovsdb_server_remove_remote(struct unixctl_conn *conn, int argc OVS_UNUSED,
1143                            const char *argv[], void *config_)
1144 {
1145     struct server_config *config = config_;
1146     struct sset_node *node;
1147
1148     node = sset_find(config->remotes, argv[1]);
1149     if (node) {
1150         sset_delete(config->remotes, node);
1151         save_config(config);
1152         unixctl_command_reply(conn, NULL);
1153     } else {
1154         unixctl_command_reply_error(conn, "no such remote");
1155     }
1156 }
1157
1158 /* "ovsdb-server/list-remotes": outputs a list of configured rmeotes. */
1159 static void
1160 ovsdb_server_list_remotes(struct unixctl_conn *conn, int argc OVS_UNUSED,
1161                           const char *argv[] OVS_UNUSED, void *remotes_)
1162 {
1163     struct sset *remotes = remotes_;
1164     const char **list, **p;
1165     struct ds s;
1166
1167     ds_init(&s);
1168
1169     list = sset_sort(remotes);
1170     for (p = list; *p; p++) {
1171         ds_put_format(&s, "%s\n", *p);
1172     }
1173     free(list);
1174
1175     unixctl_command_reply(conn, ds_cstr(&s));
1176     ds_destroy(&s);
1177 }
1178
1179
1180 /* "ovsdb-server/add-db DB": adds the DB to ovsdb-server. */
1181 static void
1182 ovsdb_server_add_database(struct unixctl_conn *conn, int argc OVS_UNUSED,
1183                           const char *argv[], void *config_)
1184 {
1185     struct server_config *config = config_;
1186     const char *filename = argv[1];
1187     char *error;
1188
1189     error = open_db(config, filename);
1190     if (!error) {
1191         save_config(config);
1192         unixctl_command_reply(conn, NULL);
1193     } else {
1194         unixctl_command_reply_error(conn, error);
1195         free(error);
1196     }
1197 }
1198
1199 static void
1200 ovsdb_server_remove_database(struct unixctl_conn *conn, int argc OVS_UNUSED,
1201                              const char *argv[], void *config_)
1202 {
1203     struct server_config *config = config_;
1204     struct shash_node *node;
1205     struct db *db;
1206     bool ok;
1207
1208     node = shash_find(config->all_dbs, argv[1]);
1209     if (!node)  {
1210         unixctl_command_reply_error(conn, "Failed to find the database.");
1211         return;
1212     }
1213     db = node->data;
1214
1215     ok = ovsdb_jsonrpc_server_remove_db(config->jsonrpc, db->db);
1216     ovs_assert(ok);
1217
1218     close_db(db);
1219     shash_delete(config->all_dbs, node);
1220
1221     save_config(config);
1222     unixctl_command_reply(conn, NULL);
1223 }
1224
1225 static void
1226 ovsdb_server_list_databases(struct unixctl_conn *conn, int argc OVS_UNUSED,
1227                             const char *argv[] OVS_UNUSED, void *all_dbs_)
1228 {
1229     struct shash *all_dbs = all_dbs_;
1230     const struct shash_node **nodes;
1231     struct ds s;
1232     size_t i;
1233
1234     ds_init(&s);
1235
1236     nodes = shash_sort(all_dbs);
1237     for (i = 0; i < shash_count(all_dbs); i++) {
1238         struct db *db = nodes[i]->data;
1239         ds_put_format(&s, "%s\n", db->db->schema->name);
1240     }
1241     free(nodes);
1242
1243     unixctl_command_reply(conn, ds_cstr(&s));
1244     ds_destroy(&s);
1245 }
1246
1247 static void
1248 parse_options(int *argcp, char **argvp[],
1249               struct sset *remotes, char **unixctl_pathp, char **run_command)
1250 {
1251     enum {
1252         OPT_REMOTE = UCHAR_MAX + 1,
1253         OPT_UNIXCTL,
1254         OPT_RUN,
1255         OPT_BOOTSTRAP_CA_CERT,
1256         OPT_PEER_CA_CERT,
1257         VLOG_OPTION_ENUMS,
1258         DAEMON_OPTION_ENUMS
1259     };
1260     static const struct option long_options[] = {
1261         {"remote",      required_argument, NULL, OPT_REMOTE},
1262         {"unixctl",     required_argument, NULL, OPT_UNIXCTL},
1263 #ifndef _WIN32
1264         {"run",         required_argument, NULL, OPT_RUN},
1265 #endif
1266         {"help",        no_argument, NULL, 'h'},
1267         {"version",     no_argument, NULL, 'V'},
1268         DAEMON_LONG_OPTIONS,
1269         VLOG_LONG_OPTIONS,
1270         {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
1271         {"peer-ca-cert", required_argument, NULL, OPT_PEER_CA_CERT},
1272         {"private-key", required_argument, NULL, 'p'},
1273         {"certificate", required_argument, NULL, 'c'},
1274         {"ca-cert",     required_argument, NULL, 'C'},
1275         {NULL, 0, NULL, 0},
1276     };
1277     char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
1278     int argc = *argcp;
1279     char **argv = *argvp;
1280
1281     sset_init(remotes);
1282     for (;;) {
1283         int c;
1284
1285         c = getopt_long(argc, argv, short_options, long_options, NULL);
1286         if (c == -1) {
1287             break;
1288         }
1289
1290         switch (c) {
1291         case OPT_REMOTE:
1292             sset_add(remotes, optarg);
1293             break;
1294
1295         case OPT_UNIXCTL:
1296             *unixctl_pathp = optarg;
1297             break;
1298
1299         case OPT_RUN:
1300             *run_command = optarg;
1301             break;
1302
1303         case 'h':
1304             usage();
1305
1306         case 'V':
1307             ovs_print_version(0, 0);
1308             exit(EXIT_SUCCESS);
1309
1310         VLOG_OPTION_HANDLERS
1311         DAEMON_OPTION_HANDLERS
1312
1313         case 'p':
1314             private_key_file = optarg;
1315             break;
1316
1317         case 'c':
1318             certificate_file = optarg;
1319             break;
1320
1321         case 'C':
1322             ca_cert_file = optarg;
1323             bootstrap_ca_cert = false;
1324             break;
1325
1326         case OPT_BOOTSTRAP_CA_CERT:
1327             ca_cert_file = optarg;
1328             bootstrap_ca_cert = true;
1329             break;
1330
1331         case OPT_PEER_CA_CERT:
1332             stream_ssl_set_peer_ca_cert_file(optarg);
1333             break;
1334
1335         case '?':
1336             exit(EXIT_FAILURE);
1337
1338         default:
1339             abort();
1340         }
1341     }
1342     free(short_options);
1343
1344     *argcp -= optind;
1345     *argvp += optind;
1346 }
1347
1348 static void
1349 usage(void)
1350 {
1351     printf("%s: Open vSwitch database server\n"
1352            "usage: %s [OPTIONS] [DATABASE...]\n"
1353            "where each DATABASE is a database file in ovsdb format.\n"
1354            "The default DATABASE, if none is given, is\n%s/conf.db.\n",
1355            program_name, program_name, ovs_dbdir());
1356     printf("\nJSON-RPC options (may be specified any number of times):\n"
1357            "  --remote=REMOTE         connect or listen to REMOTE\n");
1358     stream_usage("JSON-RPC", true, true, true);
1359     daemon_usage();
1360     vlog_usage();
1361     printf("\nOther options:\n"
1362            "  --run COMMAND           run COMMAND as subprocess then exit\n"
1363            "  --unixctl=SOCKET        override default control socket name\n"
1364            "  -h, --help              display this help message\n"
1365            "  -V, --version           display version information\n");
1366     exit(EXIT_SUCCESS);
1367 }
1368 \f
1369 static struct json *
1370 sset_to_json(const struct sset *sset)
1371 {
1372     struct json *array;
1373     const char *s;
1374
1375     array = json_array_create_empty();
1376     SSET_FOR_EACH (s, sset) {
1377         json_array_add(array, json_string_create(s));
1378     }
1379     return array;
1380 }
1381
1382 /* Truncates and replaces the contents of 'config_file' by a representation of
1383  * 'remotes' and 'db_filenames'. */
1384 static void
1385 save_config__(FILE *config_file, const struct sset *remotes,
1386               const struct sset *db_filenames)
1387 {
1388     struct json *obj;
1389     char *s;
1390
1391     if (ftruncate(fileno(config_file), 0) == -1) {
1392         VLOG_FATAL("failed to truncate temporary file (%s)",
1393                    ovs_strerror(errno));
1394     }
1395
1396     obj = json_object_create();
1397     json_object_put(obj, "remotes", sset_to_json(remotes));
1398     json_object_put(obj, "db_filenames", sset_to_json(db_filenames));
1399     s = json_to_string(obj, 0);
1400     json_destroy(obj);
1401
1402     if (fseek(config_file, 0, SEEK_SET) != 0
1403         || fputs(s, config_file) == EOF
1404         || fflush(config_file) == EOF) {
1405         VLOG_FATAL("failed to write temporary file (%s)", ovs_strerror(errno));
1406     }
1407     free(s);
1408 }
1409
1410 /* Truncates and replaces the contents of 'config_file' by a representation of
1411  * 'config'. */
1412 static void
1413 save_config(struct server_config *config)
1414 {
1415     struct sset db_filenames;
1416     struct shash_node *node;
1417
1418     sset_init(&db_filenames);
1419     SHASH_FOR_EACH (node, config->all_dbs) {
1420         struct db *db = node->data;
1421         sset_add(&db_filenames, db->filename);
1422     }
1423
1424     save_config__(config->config_tmpfile, config->remotes, &db_filenames);
1425
1426     sset_destroy(&db_filenames);
1427 }
1428
1429 static void
1430 sset_from_json(struct sset *sset, const struct json *array)
1431 {
1432     size_t i;
1433
1434     sset_clear(sset);
1435
1436     ovs_assert(array->type == JSON_ARRAY);
1437     for (i = 0; i < array->u.array.n; i++) {
1438         const struct json *elem = array->u.array.elems[i];
1439         sset_add(sset, json_string(elem));
1440     }
1441 }
1442
1443 /* Clears and replaces 'remotes' and 'dbnames' by a configuration read from
1444  * 'config_file', which must have been previously written by save_config(). */
1445 static void
1446 load_config(FILE *config_file, struct sset *remotes, struct sset *db_filenames)
1447 {
1448     struct json *json;
1449
1450     if (fseek(config_file, 0, SEEK_SET) != 0) {
1451         VLOG_FATAL("seek failed in temporary file (%s)", ovs_strerror(errno));
1452     }
1453     json = json_from_stream(config_file);
1454     if (json->type == JSON_STRING) {
1455         VLOG_FATAL("reading json failed (%s)", json_string(json));
1456     }
1457     ovs_assert(json->type == JSON_OBJECT);
1458
1459     sset_from_json(remotes, shash_find_data(json_object(json), "remotes"));
1460     sset_from_json(db_filenames,
1461                    shash_find_data(json_object(json), "db_filenames"));
1462     json_destroy(json);
1463 }