1 | /* |
1 | /* |
2 | gvpe.C -- the main file for gvpe |
2 | gvpe.C -- the main file for gvpe |
3 | Copyright (C) 1998-2002 Ivo Timmermans <ivo@o2w.nl> |
3 | Copyright (C) 1998-2002 Ivo Timmermans <ivo@o2w.nl> |
4 | 2000-2002 Guus Sliepen <guus@sliepen.eu.org> |
4 | 2000-2002 Guus Sliepen <guus@sliepen.eu.org> |
5 | 2003-2008 Marc Lehmann <gvpe@schmorp.de> |
5 | 2003-2011 Marc Lehmann <gvpe@schmorp.de> |
6 | |
6 | |
7 | This file is part of GVPE. |
7 | This file is part of GVPE. |
8 | |
8 | |
9 | GVPE is free software; you can redistribute it and/or modify it |
9 | GVPE is free software; you can redistribute it and/or modify it |
10 | under the terms of the GNU General Public License as published by the |
10 | under the terms of the GNU General Public License as published by the |
… | |
… | |
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) |
… | |
… | |
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); |
… | |
… | |
288 | exit (EXIT_SUCCESS); |
285 | exit (EXIT_SUCCESS); |
289 | |
286 | |
290 | setup_signals (); |
287 | setup_signals (); |
291 | |
288 | |
292 | if (!network.setup ()) |
289 | if (!network.setup ()) |
|
|
290 | if (network.drop_privileges ()) |
293 | { |
291 | { |
294 | ev_loop (EV_DEFAULT_ 0); |
292 | ev_run (EV_DEFAULT_ 0); |
295 | cleanup_and_exit (EXIT_FAILURE); |
293 | cleanup_and_exit (EXIT_FAILURE); |
296 | } |
294 | } |
297 | |
295 | |
298 | slog (L_ERR, _("unrecoverable error while setting up network, exiting.")); |
296 | slog (L_CRIT, _("unrecoverable error while setting up network, exiting.")); |
299 | cleanup_and_exit (EXIT_FAILURE); |
297 | cleanup_and_exit (EXIT_FAILURE); |
300 | } |
298 | } |
301 | |
299 | |