daemon: Make --monitor process change its process title.
[sliver-openvswitch.git] / vswitchd / ovs-vswitchd.c
1 /* Copyright (c) 2008, 2009, 2010 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 <assert.h>
19 #include <errno.h>
20 #include <getopt.h>
21 #include <limits.h>
22 #include <signal.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "bridge.h"
27 #include "command-line.h"
28 #include "compiler.h"
29 #include "daemon.h"
30 #include "dpif.h"
31 #include "leak-checker.h"
32 #include "netdev.h"
33 #include "ovsdb-idl.h"
34 #include "poll-loop.h"
35 #include "proc-net-compat.h"
36 #include "process.h"
37 #include "signals.h"
38 #include "stream-ssl.h"
39 #include "stream.h"
40 #include "svec.h"
41 #include "timeval.h"
42 #include "unixctl.h"
43 #include "util.h"
44 #include "vconn.h"
45 #include "vswitchd/vswitch-idl.h"
46
47 #include "vlog.h"
48 #define THIS_MODULE VLM_vswitchd
49
50 static const char *parse_options(int argc, char *argv[]);
51 static void usage(void) NO_RETURN;
52
53 int
54 main(int argc, char *argv[])
55 {
56     struct unixctl_server *unixctl;
57     struct signal *sighup;
58     struct ovsdb_idl *idl;
59     const char *remote;
60     bool need_reconfigure;
61     bool inited;
62     unsigned int idl_seqno;
63     int retval;
64
65     proctitle_init(argc, argv);
66     set_program_name(argv[0]);
67     time_init();
68     vlog_init();
69     remote = parse_options(argc, argv);
70     signal(SIGPIPE, SIG_IGN);
71     sighup = signal_register(SIGHUP);
72     process_init();
73
74     die_if_already_running();
75     daemonize_start();
76
77     retval = unixctl_server_create(NULL, &unixctl);
78     if (retval) {
79         exit(EXIT_FAILURE);
80     }
81
82     daemonize_complete();
83
84     idl = ovsdb_idl_create(remote, &ovsrec_idl_class);
85     idl_seqno = ovsdb_idl_get_seqno(idl);
86
87     need_reconfigure = false;
88     inited = false;
89     for (;;) {
90         if (signal_poll(sighup)) {
91             vlog_reopen_log_file();
92         }
93         if (inited && bridge_run()) {
94             need_reconfigure = true;
95         }
96         ovsdb_idl_run(idl);
97         if (idl_seqno != ovsdb_idl_get_seqno(idl)) {
98             idl_seqno = ovsdb_idl_get_seqno(idl);
99             need_reconfigure = true;
100         }
101         if (need_reconfigure) {
102             const struct ovsrec_open_vswitch *cfg;
103
104             need_reconfigure = false;
105             cfg = ovsrec_open_vswitch_first(idl);
106             if (cfg) {
107                 if (inited) {
108                     bridge_reconfigure(cfg);
109                 } else {
110                     bridge_init(cfg);
111                     inited = true;
112                 }
113             }
114         }
115         unixctl_server_run(unixctl);
116         dp_run();
117         netdev_run();
118
119         signal_wait(sighup);
120         if (inited) {
121             bridge_wait();
122         }
123         ovsdb_idl_wait(idl);
124         unixctl_server_wait(unixctl);
125         dp_wait();
126         netdev_wait();
127         poll_block();
128     }
129
130     return 0;
131 }
132
133 static const char *
134 parse_options(int argc, char *argv[])
135 {
136     enum {
137         OPT_PEER_CA_CERT = UCHAR_MAX + 1,
138         OPT_FAKE_PROC_NET,
139         VLOG_OPTION_ENUMS,
140         LEAK_CHECKER_OPTION_ENUMS,
141         OPT_BOOTSTRAP_CA_CERT
142     };
143     static struct option long_options[] = {
144         {"help",        no_argument, 0, 'h'},
145         {"version",     no_argument, 0, 'V'},
146         {"fake-proc-net", no_argument, 0, OPT_FAKE_PROC_NET},
147         DAEMON_LONG_OPTIONS,
148         VLOG_LONG_OPTIONS,
149         LEAK_CHECKER_LONG_OPTIONS,
150 #ifdef HAVE_OPENSSL
151         STREAM_SSL_LONG_OPTIONS
152         {"peer-ca-cert", required_argument, 0, OPT_PEER_CA_CERT},
153         {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
154 #endif
155         {0, 0, 0, 0},
156     };
157     char *short_options = long_options_to_short_options(long_options);
158     int error;
159
160     for (;;) {
161         int c;
162
163         c = getopt_long(argc, argv, short_options, long_options, NULL);
164         if (c == -1) {
165             break;
166         }
167
168         switch (c) {
169         case 'H':
170         case 'h':
171             usage();
172
173         case 'V':
174             OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
175             exit(EXIT_SUCCESS);
176
177         case OPT_FAKE_PROC_NET:
178             error = proc_net_compat_init();
179             if (error) {
180                 ovs_fatal(error, "failed to initialize /proc/net "
181                           "compatibility");
182             }
183             break;
184
185         VLOG_OPTION_HANDLERS
186         DAEMON_OPTION_HANDLERS
187         LEAK_CHECKER_OPTION_HANDLERS
188
189 #ifdef HAVE_OPENSSL
190         STREAM_SSL_OPTION_HANDLERS
191
192         case OPT_PEER_CA_CERT:
193             stream_ssl_set_peer_ca_cert_file(optarg);
194             break;
195
196         case OPT_BOOTSTRAP_CA_CERT:
197             stream_ssl_set_ca_cert_file(optarg, true);
198             break;
199 #endif
200
201         case '?':
202             exit(EXIT_FAILURE);
203
204         default:
205             abort();
206         }
207     }
208     free(short_options);
209
210     argc -= optind;
211     argv += optind;
212
213     if (argc != 1) {
214         ovs_fatal(0, "database socket is only non-option argument; "
215                 "use --help for usage");
216     }
217
218     return argv[0];
219 }
220
221 static void
222 usage(void)
223 {
224     printf("%s: Open vSwitch daemon\n"
225            "usage: %s [OPTIONS] DATABASE\n"
226            "where DATABASE is a socket on which ovsdb-server is listening.\n",
227            program_name, program_name);
228     stream_usage("DATABASE", true, false, true);
229     daemon_usage();
230     vlog_usage();
231     printf("\nLegacy compatibility options:\n"
232            " --fake-proc-net          simulate some files in /proc/net\n"
233            "\nOther options:\n"
234            "  -h, --help              display this help message\n"
235            "  -V, --version           display version information\n");
236     leak_checker_usage();
237     exit(EXIT_SUCCESS);
238 }