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