ovsdb-server: Open --listen sockets before detaching.
[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 unixctl_cb_func ovsdb_server_exit;
50
51 static void parse_options(int argc, char *argv[], char **file_namep,
52                           struct svec *active, struct svec *passive,
53                           char **unixctl_pathp);
54 static void usage(void) NO_RETURN;
55
56 int
57 main(int argc, char *argv[])
58 {
59     char *unixctl_path = NULL;
60     struct unixctl_server *unixctl;
61     struct ovsdb_jsonrpc_server *jsonrpc;
62     struct svec active, passive;
63     struct pstream **listeners;
64     struct ovsdb_error *error;
65     struct ovsdb *db;
66     const char *name;
67     char *file_name;
68     bool do_chdir;
69     bool exiting;
70     int retval;
71     size_t i;
72
73     set_program_name(argv[0]);
74     register_fault_handlers();
75     time_init();
76     vlog_init();
77     signal(SIGPIPE, SIG_IGN);
78     process_init();
79
80     parse_options(argc, argv, &file_name, &active, &passive, &unixctl_path);
81
82     /* Open all the passive sockets before detaching, to avoid race with
83      * processes that start up later. */
84     listeners = xmalloc(passive.n * sizeof *listeners);
85     for (i = 0; i < passive.n; i++) {
86         int error;
87
88         error = pstream_open(passive.names[i], &listeners[i]);
89         if (error) {
90             ovs_fatal(error, "failed to listen on \"%s\"", passive.names[i]);
91         }
92     }
93
94     if (get_detach() && is_chdir_enabled()) {
95         /* We need to skip chdir("/") in daemonize() and do it later, because
96          * we need to open the database and possible set up up Unix domain
97          * sockets in the current working directory after we daemonize.  We
98          * can't open the database before we daemonize because file locks
99          * aren't inherited by child processes.  */
100         do_chdir = true;
101         set_no_chdir();
102     } else {
103         do_chdir = false;
104     }
105     die_if_already_running();
106     daemonize();
107
108     error = ovsdb_file_open(file_name, false, &db);
109     if (error) {
110         ovs_fatal(0, "%s", ovsdb_error_to_string(error));
111     }
112
113     jsonrpc = ovsdb_jsonrpc_server_create(db);
114     SVEC_FOR_EACH (i, name, &active) {
115         ovsdb_jsonrpc_server_connect(jsonrpc, name);
116     }
117     for (i = 0; i < passive.n; i++) {
118         ovsdb_jsonrpc_server_listen(jsonrpc, listeners[i]);
119     }
120     svec_destroy(&active);
121     svec_destroy(&passive);
122
123     retval = unixctl_server_create(unixctl_path, &unixctl);
124     if (retval) {
125         ovs_fatal(retval, "could not listen for control connections");
126     }
127
128     unixctl_command_register("exit", ovsdb_server_exit, &exiting);
129
130     if (do_chdir) {
131         chdir("/");
132     }
133
134     exiting = false;
135     while (!exiting) {
136         ovsdb_jsonrpc_server_run(jsonrpc);
137         unixctl_server_run(unixctl);
138         ovsdb_trigger_run(db, time_msec());
139
140         ovsdb_jsonrpc_server_wait(jsonrpc);
141         unixctl_server_wait(unixctl);
142         ovsdb_trigger_wait(db, time_msec());
143         poll_block();
144     }
145
146     return 0;
147 }
148
149 static void
150 ovsdb_server_exit(struct unixctl_conn *conn, const char *args UNUSED,
151                   void *exiting_)
152 {
153     bool *exiting = exiting_;
154     *exiting = true;
155     unixctl_command_reply(conn, 200, NULL);
156 }
157
158 static void
159 parse_options(int argc, char *argv[], char **file_namep,
160               struct svec *active, struct svec *passive,
161               char **unixctl_pathp)
162 {
163     enum {
164         OPT_DUMMY = UCHAR_MAX + 1,
165         OPT_CONNECT,
166         OPT_LISTEN,
167         OPT_UNIXCTL,
168         VLOG_OPTION_ENUMS,
169         LEAK_CHECKER_OPTION_ENUMS
170     };
171     static struct option long_options[] = {
172         {"connect",     required_argument, 0, OPT_CONNECT},
173         {"listen",      required_argument, 0, OPT_LISTEN},
174         {"unixctl",     required_argument, 0, OPT_UNIXCTL},
175         {"help",        no_argument, 0, 'h'},
176         {"version",     no_argument, 0, 'V'},
177         DAEMON_LONG_OPTIONS,
178         VLOG_LONG_OPTIONS,
179         LEAK_CHECKER_LONG_OPTIONS,
180         {0, 0, 0, 0},
181     };
182     char *short_options = long_options_to_short_options(long_options);
183
184     svec_init(active);
185     svec_init(passive);
186     for (;;) {
187         int c;
188
189         c = getopt_long(argc, argv, short_options, long_options, NULL);
190         if (c == -1) {
191             break;
192         }
193
194         switch (c) {
195         case OPT_CONNECT:
196             svec_add(active, optarg);
197             break;
198
199         case OPT_LISTEN:
200             svec_add(passive, optarg);
201             break;
202
203         case OPT_UNIXCTL:
204             *unixctl_pathp = optarg;
205             break;
206
207         case 'h':
208             usage();
209
210         case 'V':
211             OVS_PRINT_VERSION(0, 0);
212             exit(EXIT_SUCCESS);
213
214         VLOG_OPTION_HANDLERS
215         DAEMON_OPTION_HANDLERS
216         LEAK_CHECKER_OPTION_HANDLERS
217
218         case '?':
219             exit(EXIT_FAILURE);
220
221         default:
222             abort();
223         }
224     }
225     free(short_options);
226
227     argc -= optind;
228     argv += optind;
229
230     if (argc != 1) {
231         ovs_fatal(0, "database file is only non-option argument; "
232                 "use --help for usage");
233     }
234
235     *file_namep = argv[0];
236 }
237
238 static void
239 usage(void)
240 {
241     printf("%s: Open vSwitch database server\n"
242            "usage: %s [OPTIONS] DATABASE\n"
243            "where DATABASE is a database file in ovsdb format.\n",
244            program_name, program_name);
245     printf("\nJSON-RPC options (may be specified any number of times):\n"
246            "  --connect=REMOTE        make active connection to REMOTE\n"
247            "  --listen=LOCAL          passively listen on LOCAL\n");
248     stream_usage("JSON-RPC", true, true);
249     daemon_usage();
250     vlog_usage();
251     printf("\nOther options:\n"
252            "  -h, --help              display this help message\n"
253            "  -V, --version           display version information\n");
254     leak_checker_usage();
255     exit(EXIT_SUCCESS);
256 }