daemon: Integrate checking for an existing pidfile into daemonize_start().
[sliver-openvswitch.git] / vswitchd / ovs-vswitchd.c
1 /* Copyright (c) 2008, 2009, 2010, 2011 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 #ifdef HAVE_MLOCKALL
26 #include <sys/mman.h>
27 #endif
28
29 #include "bridge.h"
30 #include "command-line.h"
31 #include "compiler.h"
32 #include "daemon.h"
33 #include "dpif.h"
34 #include "dummy.h"
35 #include "leak-checker.h"
36 #include "netdev.h"
37 #include "ovsdb-idl.h"
38 #include "poll-loop.h"
39 #include "process.h"
40 #include "signals.h"
41 #include "stream-ssl.h"
42 #include "stream.h"
43 #include "stress.h"
44 #include "svec.h"
45 #include "timeval.h"
46 #include "unixctl.h"
47 #include "util.h"
48 #include "vconn.h"
49 #include "vlog.h"
50 #include "vswitchd/vswitch-idl.h"
51
52 VLOG_DEFINE_THIS_MODULE(vswitchd);
53
54 static unixctl_cb_func ovs_vswitchd_exit;
55
56 static const char *parse_options(int argc, char *argv[]);
57 static void usage(void) NO_RETURN;
58
59 int
60 main(int argc, char *argv[])
61 {
62     struct unixctl_server *unixctl;
63     struct signal *sighup;
64     const char *remote;
65     bool exiting;
66     int retval;
67
68     proctitle_init(argc, argv);
69     set_program_name(argv[0]);
70     stress_init_command();
71     remote = parse_options(argc, argv);
72     signal(SIGPIPE, SIG_IGN);
73     sighup = signal_register(SIGHUP);
74     process_init();
75     ovsrec_init();
76
77     daemonize_start();
78
79     retval = unixctl_server_create(NULL, &unixctl);
80     if (retval) {
81         exit(EXIT_FAILURE);
82     }
83     unixctl_command_register("exit", ovs_vswitchd_exit, &exiting);
84
85     bridge_init(remote);
86     exiting = false;
87     while (!exiting) {
88         if (signal_poll(sighup)) {
89             vlog_reopen_log_file();
90         }
91         bridge_run();
92         unixctl_server_run(unixctl);
93         dp_run();
94         netdev_run();
95
96         signal_wait(sighup);
97         bridge_wait();
98         unixctl_server_wait(unixctl);
99         dp_wait();
100         netdev_wait();
101         if (exiting) {
102             poll_immediate_wake();
103         }
104         poll_block();
105     }
106     bridge_exit();
107     unixctl_server_destroy(unixctl);
108
109     return 0;
110 }
111
112 static const char *
113 parse_options(int argc, char *argv[])
114 {
115     enum {
116         OPT_PEER_CA_CERT = UCHAR_MAX + 1,
117         OPT_MLOCKALL,
118         VLOG_OPTION_ENUMS,
119         LEAK_CHECKER_OPTION_ENUMS,
120         OPT_BOOTSTRAP_CA_CERT,
121         OPT_ENABLE_DUMMY,
122         DAEMON_OPTION_ENUMS
123     };
124     static struct option long_options[] = {
125         {"help",        no_argument, 0, 'h'},
126         {"version",     no_argument, 0, 'V'},
127         {"mlockall",    no_argument, 0, OPT_MLOCKALL},
128         DAEMON_LONG_OPTIONS,
129         VLOG_LONG_OPTIONS,
130         LEAK_CHECKER_LONG_OPTIONS,
131 #ifdef HAVE_OPENSSL
132         STREAM_SSL_LONG_OPTIONS
133         {"peer-ca-cert", required_argument, 0, OPT_PEER_CA_CERT},
134         {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
135 #endif
136         {"enable-dummy", no_argument, 0, OPT_ENABLE_DUMMY},
137         {0, 0, 0, 0},
138     };
139     char *short_options = long_options_to_short_options(long_options);
140
141     for (;;) {
142         int c;
143
144         c = getopt_long(argc, argv, short_options, long_options, NULL);
145         if (c == -1) {
146             break;
147         }
148
149         switch (c) {
150         case 'H':
151         case 'h':
152             usage();
153
154         case 'V':
155             OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
156             exit(EXIT_SUCCESS);
157
158         case OPT_MLOCKALL:
159 #ifdef HAVE_MLOCKALL
160             if (mlockall(MCL_CURRENT | MCL_FUTURE)) {
161                 VLOG_ERR("mlockall failed: %s", strerror(errno));
162             }
163 #else
164             VLOG_ERR("mlockall not supported on this system");
165 #endif
166             break;
167
168         VLOG_OPTION_HANDLERS
169         DAEMON_OPTION_HANDLERS
170         LEAK_CHECKER_OPTION_HANDLERS
171
172 #ifdef HAVE_OPENSSL
173         STREAM_SSL_OPTION_HANDLERS
174
175         case OPT_PEER_CA_CERT:
176             stream_ssl_set_peer_ca_cert_file(optarg);
177             break;
178
179         case OPT_BOOTSTRAP_CA_CERT:
180             stream_ssl_set_ca_cert_file(optarg, true);
181             break;
182 #endif
183
184         case OPT_ENABLE_DUMMY:
185             dummy_enable();
186             break;
187
188         case '?':
189             exit(EXIT_FAILURE);
190
191         default:
192             abort();
193         }
194     }
195     free(short_options);
196
197     argc -= optind;
198     argv += optind;
199
200     if (argc != 1) {
201         VLOG_FATAL("database socket is only non-option argument; "
202                    "use --help for usage");
203     }
204
205     return argv[0];
206 }
207
208 static void
209 usage(void)
210 {
211     printf("%s: Open vSwitch daemon\n"
212            "usage: %s [OPTIONS] DATABASE\n"
213            "where DATABASE is a socket on which ovsdb-server is listening.\n",
214            program_name, program_name);
215     stream_usage("DATABASE", true, false, true);
216     daemon_usage();
217     vlog_usage();
218     printf("\nOther options:\n"
219            "  -h, --help              display this help message\n"
220            "  -V, --version           display version information\n");
221     leak_checker_usage();
222     exit(EXIT_SUCCESS);
223 }
224
225 static void
226 ovs_vswitchd_exit(struct unixctl_conn *conn, const char *args OVS_UNUSED,
227                   void *exiting_)
228 {
229     bool *exiting = exiting_;
230     *exiting = true;
231     unixctl_command_reply(conn, 200, NULL);
232 }