… | |
… | |
75 | |
75 | |
76 | /* If zero, don't detach from the terminal. */ |
76 | /* If zero, don't detach from the terminal. */ |
77 | static int do_detach = 1; |
77 | static int do_detach = 1; |
78 | |
78 | |
79 | static struct option const long_options[] = |
79 | static struct option const long_options[] = |
80 | { |
80 | { |
81 | {"config", required_argument, NULL, 'c'}, |
81 | {"config", required_argument, NULL, 'c'}, |
82 | {"help", no_argument, &show_help, 1}, |
82 | {"help", no_argument, &show_help, 1}, |
83 | {"version", no_argument, &show_version, 1}, |
83 | {"version", no_argument, &show_version, 1}, |
84 | {"no-detach", no_argument, &do_detach, 0}, |
84 | {"no-detach", no_argument, &do_detach, 0}, |
85 | {"log-level", required_argument, NULL, 'l'}, |
85 | {"log-level", required_argument, NULL, 'l'}, |
86 | {"mlock", no_argument, &do_mlock, 1}, |
86 | {"mlock", no_argument, &do_mlock, 1}, |
87 | {NULL, 0, NULL, 0} |
87 | {NULL, 0, NULL, 0} |
88 | }; |
88 | }; |
89 | |
89 | |
90 | static void |
90 | static void |
91 | usage (int status) |
91 | usage (int status) |
92 | { |
92 | { |
93 | if (status != 0) |
93 | if (status != 0) |
… | |
… | |
97 | printf (_("Usage: %s [option]... NODENAME\n\n"), get_identity ()); |
97 | printf (_("Usage: %s [option]... NODENAME\n\n"), get_identity ()); |
98 | printf (_ |
98 | printf (_ |
99 | (" -c, --config=DIR Read configuration options from DIR.\n" |
99 | (" -c, --config=DIR Read configuration options from DIR.\n" |
100 | " -D, --no-detach Don't fork and detach.\n" |
100 | " -D, --no-detach Don't fork and detach.\n" |
101 | " -l, --log-level=LEVEL Set logging level (info, notice, warn are common).\n" |
101 | " -l, --log-level=LEVEL Set logging level (info, notice, warn are common).\n" |
102 | " -L, --mlock Lock tinc into main memory.\n" |
102 | " -L, --mlock Lock gvpe into main memory.\n" |
103 | " --help Display this help and exit.\n" |
103 | " --help Display this help and exit.\n" |
104 | " --version Output version information and exit.\n\n")); |
104 | " --version Output version information and exit.\n\n")); |
105 | printf (_("Report bugs to <gvpe@schmorp.de>.\n")); |
105 | printf (_("Report bugs to <gvpe@schmorp.de>.\n")); |
106 | } |
106 | } |
107 | |
107 | |
108 | exit (status); |
108 | exit (status); |
109 | } |
109 | } |
110 | |
110 | |
111 | void |
111 | static void |
112 | parse_options (int argc, char **argv, char **envp) |
112 | parse_options (int argc, char **argv, char **envp) |
113 | { |
113 | { |
114 | int r; |
114 | int r; |
115 | int option_index = 0; |
115 | int option_index = 0; |
116 | |
116 | |
… | |
… | |
149 | break; |
149 | break; |
150 | } |
150 | } |
151 | } |
151 | } |
152 | } |
152 | } |
153 | |
153 | |
154 | /* |
|
|
155 | Close network connections, and terminate neatly |
154 | // close network connections, and terminate neatly |
156 | */ |
155 | static void |
157 | void cleanup_and_exit(int c) |
156 | cleanup_and_exit (int c) |
158 | { |
157 | { |
159 | network.shutdown_all (); |
158 | network.shutdown_all (); |
160 | |
159 | |
161 | if (conf.pidfilename) |
160 | if (conf.pidfilename) |
162 | remove_pid (conf.pidfilename); |
161 | remove_pid (conf.pidfilename); |
… | |
… | |
164 | slog (L_INFO, _("terminating with exit code %d"), c); |
163 | slog (L_INFO, _("terminating with exit code %d"), c); |
165 | |
164 | |
166 | exit (c); |
165 | exit (c); |
167 | } |
166 | } |
168 | |
167 | |
169 | /* |
|
|
170 | Signal handlers. |
168 | // signal handlers |
171 | */ |
169 | static RETSIGTYPE |
172 | RETSIGTYPE |
|
|
173 | sigterm_handler (int a) |
170 | sigterm_handler (int a) |
174 | { |
171 | { |
175 | network.events |= vpn::EVENT_SHUTDOWN; |
172 | network.events |= vpn::EVENT_SHUTDOWN; |
176 | network.event.start (); |
173 | network.event.start (); |
177 | } |
174 | } |
178 | |
175 | |
179 | RETSIGTYPE |
176 | static RETSIGTYPE |
180 | sighup_handler (int a) |
177 | sighup_handler (int a) |
181 | { |
178 | { |
182 | network.events |= vpn::EVENT_RECONNECT; |
179 | network.events |= vpn::EVENT_RECONNECT; |
183 | network.event.start (); |
180 | network.event.start (); |
184 | } |
181 | } |
185 | |
182 | |
186 | RETSIGTYPE |
183 | static RETSIGTYPE |
187 | sigusr1_handler (int a) |
184 | sigusr1_handler (int a) |
188 | { |
185 | { |
189 | network.dump_status (); |
186 | network.dump_status (); |
190 | } |
187 | } |
191 | |
188 | |
192 | RETSIGTYPE |
189 | static RETSIGTYPE |
193 | sigusr2_handler (int a) |
190 | sigusr2_handler (int a) |
194 | { |
191 | { |
195 | } |
192 | } |
196 | |
193 | |
197 | void |
194 | static void |
198 | setup_signals (void) |
195 | setup_signals (void) |
199 | { |
196 | { |
200 | struct sigaction act; |
197 | struct sigaction act; |
201 | |
198 | |
202 | sigfillset (&act.sa_mask); |
199 | sigfillset (&act.sa_mask); |
203 | act.sa_flags = 0; |
200 | act.sa_flags = 0; |
204 | |
201 | |
205 | act.sa_handler = sighup_handler; sigaction (SIGHUP , &act, NULL); |
202 | act.sa_handler = sighup_handler; sigaction (SIGHUP , &act, NULL); |
206 | act.sa_handler = sigusr1_handler; sigaction (SIGUSR1, &act, NULL); |
203 | act.sa_handler = sigusr1_handler; sigaction (SIGUSR1, &act, NULL); |
207 | act.sa_handler = sigusr2_handler; sigaction (SIGUSR2, &act, NULL); |
204 | act.sa_handler = sigusr2_handler; sigaction (SIGUSR2, &act, NULL); |
208 | act.sa_handler = SIG_IGN; sigaction (SIGCHLD, &act, NULL); |
|
|
209 | act.sa_handler = SIG_IGN; sigaction (SIGPIPE, &act, NULL); |
205 | act.sa_handler = SIG_IGN; sigaction (SIGPIPE, &act, NULL); |
210 | act.sa_flags = SA_RESETHAND; |
206 | act.sa_flags = SA_RESETHAND; |
211 | act.sa_handler = sigterm_handler; sigaction (SIGINT , &act, NULL); |
207 | act.sa_handler = sigterm_handler; sigaction (SIGINT , &act, NULL); |
212 | act.sa_handler = sigterm_handler; sigaction (SIGTERM, &act, NULL); |
208 | act.sa_handler = sigterm_handler; sigaction (SIGTERM, &act, NULL); |
213 | } |
209 | } |
… | |
… | |
234 | { |
230 | { |
235 | printf (_("%s version %s (built %s %s, protocol version %d.%d)\n"), get_identity (), |
231 | printf (_("%s version %s (built %s %s, protocol version %d.%d)\n"), get_identity (), |
236 | VERSION, __DATE__, __TIME__, PROTOCOL_MAJOR, PROTOCOL_MINOR); |
232 | VERSION, __DATE__, __TIME__, PROTOCOL_MAJOR, PROTOCOL_MINOR); |
237 | printf (_("Built with kernel interface %s/%s.\n"), IFTYPE, IFSUBTYPE); |
233 | printf (_("Built with kernel interface %s/%s.\n"), IFTYPE, IFSUBTYPE); |
238 | printf (_ |
234 | printf (_ |
239 | ("Copyright (C) 2003 Marc Lehmann <gvpe@schmorp.de> and others.\n" |
235 | ("Copyright (C) 2003-2008 Marc Lehmann <gvpe@schmorp.de> and others.\n" |
240 | "See the AUTHORS file for a complete list.\n\n" |
236 | "See the AUTHORS file for a complete list.\n\n" |
241 | "tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n" |
237 | "GVPE comes with ABSOLUTELY NO WARRANTY. This is free software,\n" |
242 | "and you are welcome to redistribute it under certain conditions;\n" |
238 | "and you are welcome to redistribute it under certain conditions;\n" |
243 | "see the file COPYING for details.\n")); |
239 | "see the file COPYING for details.\n")); |
244 | |
240 | |
245 | return 0; |
241 | return 0; |
246 | } |
242 | } |
… | |
… | |
294 | { |
290 | { |
295 | ev_loop (EV_DEFAULT_ 0); |
291 | ev_loop (EV_DEFAULT_ 0); |
296 | cleanup_and_exit (EXIT_FAILURE); |
292 | cleanup_and_exit (EXIT_FAILURE); |
297 | } |
293 | } |
298 | |
294 | |
299 | slog (L_ERR, _("unable to setup network, unrecoverable error, exiting.")); |
295 | slog (L_ERR, _("unrecoverable error while setting up network, exiting.")); |
300 | cleanup_and_exit (EXIT_FAILURE); |
296 | cleanup_and_exit (EXIT_FAILURE); |
301 | } |
297 | } |
302 | |
298 | |