ovsdb-server: Maintain the database lock with --detach.
[sliver-openvswitch.git] / ovsdb / ovsdb-server.c
1 /* Copyright (c) 2009 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 "command-line.h"
26 #include "daemon.h"
27 #include "fault.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-error.h"
35 #include "poll-loop.h"
36 #include "process.h"
37 #include "stream.h"
38 #include "svec.h"
39 #include "timeval.h"
40 #include "trigger.h"
41 #include "util.h"
42 #include "unixctl.h"
43
44 #include "vlog.h"
45 #define THIS_MODULE VLM_ovsdb_server
46
47 static const struct jsonrpc_server_cbs ovsdb_jsonrpc_cbs;
48
49 static void parse_options(int argc, char *argv[], char **file_namep,
50                           struct svec *active, struct svec *passive);
51 static void usage(void) NO_RETURN;
52
53 int
54 main(int argc, char *argv[])
55 {
56     struct unixctl_server *unixctl;
57     struct ovsdb_jsonrpc_server *jsonrpc;
58     struct svec active, passive;
59     struct ovsdb_error *error;
60     struct ovsdb *db;
61     const char *name;
62     char *file_name;
63     bool do_chdir;
64     int retval;
65     size_t i;
66
67     set_program_name(argv[0]);
68     register_fault_handlers();
69     time_init();
70     vlog_init();
71     signal(SIGPIPE, SIG_IGN);
72     process_init();
73
74     parse_options(argc, argv, &file_name, &active, &passive);
75
76     if (get_detach() && is_chdir_enabled()) {
77         /* We need to skip chdir("/") in daemonize() and do it later, because
78          * we need to open the database and possible set up up Unix domain
79          * sockets in the current working directory after we daemonize.  We
80          * can't open the database before we daemonize because file locks
81          * aren't inherited by child processes.  */
82         do_chdir = true;
83         set_no_chdir();
84     } else {
85         do_chdir = false;
86     }
87     die_if_already_running();
88     daemonize();
89
90     error = ovsdb_file_open(file_name, false, &db);
91     if (error) {
92         ovs_fatal(0, "%s", ovsdb_error_to_string(error));
93     }
94
95     jsonrpc = ovsdb_jsonrpc_server_create(db);
96     SVEC_FOR_EACH (i, name, &active) {
97         ovsdb_jsonrpc_server_connect(jsonrpc, name);
98     }
99     SVEC_FOR_EACH (i, name, &passive) {
100         retval = ovsdb_jsonrpc_server_listen(jsonrpc, name);
101         if (retval) {
102             ovs_fatal(retval, "failed to listen on %s", name);
103         }
104     }
105     svec_destroy(&active);
106     svec_destroy(&passive);
107
108     retval = unixctl_server_create(NULL, &unixctl);
109     if (retval) {
110         ovs_fatal(retval, "could not listen for control connections");
111     }
112
113     if (do_chdir) {
114         chdir("/");
115     }
116     for (;;) {
117         ovsdb_jsonrpc_server_run(jsonrpc);
118         unixctl_server_run(unixctl);
119         ovsdb_trigger_run(db, time_msec());
120
121         ovsdb_jsonrpc_server_wait(jsonrpc);
122         unixctl_server_wait(unixctl);
123         ovsdb_trigger_wait(db, time_msec());
124         poll_block();
125     }
126
127     return 0;
128 }
129
130 static void
131 parse_options(int argc, char *argv[], char **file_namep,
132               struct svec *active, struct svec *passive)
133 {
134     enum {
135         OPT_DUMMY = UCHAR_MAX + 1,
136         OPT_CONNECT,
137         OPT_LISTEN,
138         VLOG_OPTION_ENUMS,
139         LEAK_CHECKER_OPTION_ENUMS
140     };
141     static struct option long_options[] = {
142         {"connect",     required_argument, 0, OPT_CONNECT},
143         {"listen",      required_argument, 0, OPT_LISTEN},
144         {"help",        no_argument, 0, 'h'},
145         {"version",     no_argument, 0, 'V'},
146         DAEMON_LONG_OPTIONS,
147         VLOG_LONG_OPTIONS,
148         LEAK_CHECKER_LONG_OPTIONS,
149         {0, 0, 0, 0},
150     };
151     char *short_options = long_options_to_short_options(long_options);
152
153     svec_init(active);
154     svec_init(passive);
155     for (;;) {
156         int c;
157
158         c = getopt_long(argc, argv, short_options, long_options, NULL);
159         if (c == -1) {
160             break;
161         }
162
163         switch (c) {
164         case OPT_CONNECT:
165             svec_add(active, optarg);
166             break;
167
168         case OPT_LISTEN:
169             svec_add(passive, optarg);
170             break;
171
172         case 'h':
173             usage();
174
175         case 'V':
176             OVS_PRINT_VERSION(0, 0);
177             exit(EXIT_SUCCESS);
178
179         VLOG_OPTION_HANDLERS
180         DAEMON_OPTION_HANDLERS
181         LEAK_CHECKER_OPTION_HANDLERS
182
183         case '?':
184             exit(EXIT_FAILURE);
185
186         default:
187             abort();
188         }
189     }
190     free(short_options);
191
192     argc -= optind;
193     argv += optind;
194
195     if (argc != 1) {
196         ovs_fatal(0, "database file is only non-option argument; "
197                 "use --help for usage");
198     }
199
200     *file_namep = argv[0];
201 }
202
203 static void
204 usage(void)
205 {
206     printf("%s: Open vSwitch database server\n"
207            "usage: %s [OPTIONS] DATABASE\n"
208            "where DATABASE is a database file in ovsdb format.\n",
209            program_name, program_name);
210     printf("\nJSON-RPC options (may be specified any number of times):\n"
211            "  --connect=REMOTE        make active connection to REMOTE\n"
212            "  --listen=LOCAL          passively listen on LOCAL\n");
213     stream_usage("JSON-RPC", true, true);
214     daemon_usage();
215     vlog_usage();
216     printf("\nOther options:\n"
217            "  -h, --help              display this help message\n"
218            "  -V, --version           display version information\n");
219     leak_checker_usage();
220     exit(EXIT_SUCCESS);
221 }