1 |
pcg |
1.1 |
/* |
2 |
pcg |
1.4 |
gvpe.C -- the main file for gvpe |
3 |
pcg |
1.1 |
Copyright (C) 1998-2002 Ivo Timmermans <ivo@o2w.nl> |
4 |
|
|
2000-2002 Guus Sliepen <guus@sliepen.eu.org> |
5 |
|
|
2003-2005 Marc Lehmann <gvpe@schmorp.de> |
6 |
|
|
|
7 |
|
|
This file is part of GVPE. |
8 |
|
|
|
9 |
|
|
GVPE is free software; you can redistribute it and/or modify |
10 |
|
|
it under the terms of the GNU General Public License as published by |
11 |
|
|
the Free Software Foundation; either version 2 of the License, or |
12 |
|
|
(at your option) any later version. |
13 |
|
|
|
14 |
|
|
This program is distributed in the hope that it will be useful, |
15 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 |
|
|
GNU General Public License for more details. |
18 |
|
|
|
19 |
|
|
You should have received a copy of the GNU General Public License |
20 |
|
|
along with gvpe; if not, write to the Free Software |
21 |
|
|
Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
22 |
|
|
*/ |
23 |
|
|
|
24 |
|
|
#include "config.h" |
25 |
|
|
|
26 |
|
|
#include <cstdio> |
27 |
|
|
#include <cstring> |
28 |
|
|
#include <cstdlib> |
29 |
|
|
|
30 |
|
|
#include <errno.h> |
31 |
|
|
#include <fcntl.h> |
32 |
|
|
#include <getopt.h> |
33 |
|
|
#include <signal.h> |
34 |
|
|
#include <sys/types.h> |
35 |
|
|
#include <unistd.h> |
36 |
|
|
#include <signal.h> |
37 |
|
|
#include <termios.h> |
38 |
|
|
|
39 |
|
|
#if HAVE_SYS_MMAN_H |
40 |
|
|
# include <sys/mman.h> |
41 |
|
|
#endif |
42 |
|
|
|
43 |
|
|
#include <openssl/err.h> |
44 |
|
|
#include <openssl/rand.h> |
45 |
|
|
|
46 |
pcg |
1.2 |
#include "gettext.h" |
47 |
pcg |
1.1 |
#include "pidfile.h" |
48 |
|
|
|
49 |
|
|
#include "conf.h" |
50 |
|
|
#include "slog.h" |
51 |
|
|
#include "util.h" |
52 |
|
|
#include "vpn.h" |
53 |
|
|
#include "iom.h" |
54 |
|
|
|
55 |
|
|
static loglevel llevel = L_NONE; |
56 |
|
|
|
57 |
|
|
/* If nonzero, display usage information and exit. */ |
58 |
|
|
static int show_help; |
59 |
|
|
|
60 |
|
|
/* If nonzero, print the version on standard output and exit. */ |
61 |
|
|
static int show_version; |
62 |
|
|
|
63 |
|
|
/* If nonzero, disable swapping for this process. */ |
64 |
|
|
static int do_mlock = 0; |
65 |
|
|
|
66 |
|
|
/* If zero, don't detach from the terminal. */ |
67 |
|
|
static int do_detach = 1; |
68 |
|
|
|
69 |
|
|
static struct option const long_options[] = |
70 |
|
|
{ |
71 |
|
|
{"config", required_argument, NULL, 'c'}, |
72 |
|
|
{"help", no_argument, &show_help, 1}, |
73 |
|
|
{"version", no_argument, &show_version, 1}, |
74 |
|
|
{"no-detach", no_argument, &do_detach, 0}, |
75 |
|
|
{"log-level", required_argument, NULL, 'l'}, |
76 |
|
|
{"mlock", no_argument, &do_mlock, 1}, |
77 |
|
|
{NULL, 0, NULL, 0} |
78 |
|
|
}; |
79 |
|
|
|
80 |
|
|
static void |
81 |
|
|
usage (int status) |
82 |
|
|
{ |
83 |
|
|
if (status != 0) |
84 |
|
|
fprintf (stderr, _("Try `%s --help\' for more information.\n"), get_identity ()); |
85 |
|
|
else |
86 |
|
|
{ |
87 |
|
|
printf (_("Usage: %s [option]... NODENAME\n\n"), get_identity ()); |
88 |
|
|
printf (_ |
89 |
|
|
(" -c, --config=DIR Read configuration options from DIR.\n" |
90 |
|
|
" -D, --no-detach Don't fork and detach.\n" |
91 |
|
|
" -l, --log-level=LEVEL Set logging level (info, notice, warn are common).\n" |
92 |
|
|
" -L, --mlock Lock tinc into main memory.\n" |
93 |
|
|
" --help Display this help and exit.\n" |
94 |
|
|
" --version Output version information and exit.\n\n")); |
95 |
|
|
printf (_("Report bugs to <vpe@plan9.de>.\n")); |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
exit (status); |
99 |
|
|
} |
100 |
|
|
|
101 |
|
|
void |
102 |
|
|
parse_options (int argc, char **argv, char **envp) |
103 |
|
|
{ |
104 |
|
|
int r; |
105 |
|
|
int option_index = 0; |
106 |
|
|
|
107 |
pcg |
1.6 |
while ((r = getopt_long (argc, argv, "c:DLl:", long_options, &option_index)) != EOF) |
108 |
pcg |
1.1 |
{ |
109 |
|
|
switch (r) |
110 |
|
|
{ |
111 |
|
|
case 0: /* long option */ |
112 |
|
|
break; |
113 |
|
|
|
114 |
|
|
case 'c': /* config file */ |
115 |
|
|
confbase = strdup (optarg); |
116 |
|
|
break; |
117 |
|
|
|
118 |
|
|
case 'D': /* no detach */ |
119 |
|
|
do_detach = 0; |
120 |
|
|
break; |
121 |
|
|
|
122 |
|
|
case 'L': /* lock into memory */ |
123 |
|
|
do_mlock = 1; |
124 |
|
|
break; |
125 |
|
|
|
126 |
|
|
case 'l': /* inc debug level */ |
127 |
|
|
{ |
128 |
|
|
llevel = string_to_loglevel (optarg); |
129 |
|
|
|
130 |
|
|
if (llevel == L_NONE) |
131 |
|
|
slog (L_WARN, "'%s': %s", optarg, UNKNOWN_LOGLEVEL); |
132 |
|
|
} |
133 |
|
|
break; |
134 |
|
|
|
135 |
|
|
case '?': |
136 |
|
|
usage (1); |
137 |
|
|
|
138 |
|
|
default: |
139 |
|
|
break; |
140 |
|
|
} |
141 |
|
|
} |
142 |
|
|
} |
143 |
|
|
|
144 |
|
|
/* |
145 |
|
|
Close network connections, and terminate neatly |
146 |
|
|
*/ |
147 |
|
|
void cleanup_and_exit(int c) |
148 |
|
|
{ |
149 |
|
|
network.shutdown_all (); |
150 |
|
|
|
151 |
|
|
if (conf.pidfilename) |
152 |
|
|
remove_pid (conf.pidfilename); |
153 |
|
|
|
154 |
|
|
slog (L_INFO, _("terminating with exit code %d"), c); |
155 |
|
|
|
156 |
|
|
exit (c); |
157 |
|
|
} |
158 |
|
|
|
159 |
|
|
/* |
160 |
|
|
Signal handlers. |
161 |
|
|
*/ |
162 |
|
|
RETSIGTYPE |
163 |
|
|
sigterm_handler (int a) |
164 |
|
|
{ |
165 |
|
|
network.events |= vpn::EVENT_SHUTDOWN; |
166 |
|
|
network.event.start (0); |
167 |
|
|
} |
168 |
|
|
|
169 |
|
|
RETSIGTYPE |
170 |
|
|
sighup_handler (int a) |
171 |
|
|
{ |
172 |
|
|
network.events |= vpn::EVENT_RECONNECT; |
173 |
|
|
network.event.start (0); |
174 |
|
|
} |
175 |
|
|
|
176 |
|
|
RETSIGTYPE |
177 |
|
|
sigusr1_handler (int a) |
178 |
|
|
{ |
179 |
|
|
network.dump_status (); |
180 |
|
|
} |
181 |
|
|
|
182 |
|
|
RETSIGTYPE |
183 |
|
|
sigusr2_handler (int a) |
184 |
|
|
{ |
185 |
|
|
} |
186 |
|
|
|
187 |
|
|
void |
188 |
|
|
setup_signals (void) |
189 |
|
|
{ |
190 |
|
|
struct sigaction act; |
191 |
|
|
|
192 |
|
|
sigfillset (&act.sa_mask); |
193 |
|
|
act.sa_flags = 0; |
194 |
|
|
|
195 |
|
|
act.sa_handler = sighup_handler; sigaction (SIGHUP , &act, NULL); |
196 |
|
|
act.sa_handler = sigusr1_handler; sigaction (SIGUSR1, &act, NULL); |
197 |
|
|
act.sa_handler = sigusr2_handler; sigaction (SIGUSR2, &act, NULL); |
198 |
pcg |
1.5 |
// act.sa_handler = SIG_IGN; sigaction (SIGCHLD, &act, NULL); |
199 |
pcg |
1.1 |
act.sa_handler = SIG_IGN; sigaction (SIGPIPE, &act, NULL); |
200 |
|
|
act.sa_flags = SA_RESETHAND; |
201 |
|
|
act.sa_handler = sigterm_handler; sigaction (SIGINT , &act, NULL); |
202 |
|
|
act.sa_handler = sigterm_handler; sigaction (SIGTERM, &act, NULL); |
203 |
|
|
} |
204 |
|
|
|
205 |
|
|
int |
206 |
|
|
main (int argc, char **argv, char **envp) |
207 |
|
|
{ |
208 |
|
|
ERR_load_crypto_strings (); // we have the RAM |
209 |
|
|
|
210 |
|
|
set_loglevel (L_INFO); |
211 |
|
|
set_identity (argv[0]); |
212 |
|
|
log_to (LOGTO_SYSLOG | LOGTO_STDERR); |
213 |
|
|
|
214 |
|
|
setlocale (LC_ALL, ""); |
215 |
|
|
bindtextdomain (PACKAGE, LOCALEDIR); |
216 |
|
|
textdomain (PACKAGE); |
217 |
|
|
|
218 |
|
|
parse_options (argc, argv, envp); |
219 |
|
|
|
220 |
pcg |
1.6 |
argc -= optind; |
221 |
|
|
argv += optind; |
222 |
|
|
|
223 |
pcg |
1.1 |
if (show_version) |
224 |
|
|
{ |
225 |
|
|
printf (_("%s version %s (built %s %s, protocol %d.%d)\n"), get_identity (), |
226 |
|
|
VERSION, __DATE__, __TIME__, PROTOCOL_MAJOR, PROTOCOL_MINOR); |
227 |
|
|
printf (_("Built with kernel interface %s/%s.\n"), IFTYPE, IFSUBTYPE); |
228 |
|
|
printf (_ |
229 |
|
|
("Copyright (C) 2003 Marc Lehmann <vpe@plan9.de> and others.\n" |
230 |
|
|
"See the AUTHORS file for a complete list.\n\n" |
231 |
|
|
"tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n" |
232 |
|
|
"and you are welcome to redistribute it under certain conditions;\n" |
233 |
|
|
"see the file COPYING for details.\n")); |
234 |
|
|
|
235 |
|
|
return 0; |
236 |
|
|
} |
237 |
|
|
|
238 |
|
|
if (show_help) |
239 |
|
|
usage (0); |
240 |
|
|
|
241 |
|
|
log_to (LOGTO_SYSLOG | LOGTO_STDERR); |
242 |
|
|
|
243 |
|
|
/* Lock all pages into memory if requested */ |
244 |
|
|
|
245 |
|
|
#if HAVE_MLOCKALL && HAVE_SYS_MMAN_H && _POSIX_MEMLOCK |
246 |
|
|
if (do_mlock) |
247 |
|
|
if (mlockall (MCL_CURRENT | MCL_FUTURE)) |
248 |
|
|
slog (L_ERR, _("system call `%s' failed: %s"), "mlockall", strerror (errno)); |
249 |
|
|
#endif |
250 |
|
|
|
251 |
pcg |
1.6 |
if (argc >= 1) |
252 |
|
|
{ |
253 |
|
|
thisnode = *argv++; |
254 |
|
|
argc--; |
255 |
|
|
} |
256 |
|
|
|
257 |
|
|
{ |
258 |
|
|
configuration_parser (conf, true, argc, argv); |
259 |
|
|
} |
260 |
pcg |
1.1 |
|
261 |
|
|
set_loglevel (llevel != L_NONE ? llevel : conf.llevel); |
262 |
|
|
|
263 |
|
|
RAND_load_file ("/dev/urandom", 1024); |
264 |
|
|
|
265 |
|
|
if (!THISNODE) |
266 |
|
|
{ |
267 |
pcg |
1.3 |
slog (L_ERR, _("current node not set, or node '%s' not found in configfile, specify the nodename when starting gvpe."), |
268 |
pcg |
1.1 |
thisnode ? thisnode : "<unset>"); |
269 |
|
|
exit (EXIT_FAILURE); |
270 |
|
|
} |
271 |
|
|
|
272 |
|
|
if (detach (do_detach)) |
273 |
|
|
exit (EXIT_SUCCESS); |
274 |
|
|
|
275 |
|
|
setup_signals (); |
276 |
|
|
|
277 |
|
|
if (!network.setup ()) |
278 |
|
|
{ |
279 |
|
|
io_manager::loop (); |
280 |
|
|
cleanup_and_exit (EXIT_FAILURE); |
281 |
|
|
} |
282 |
|
|
|
283 |
|
|
slog (L_ERR, _("unable to setup network, unrecoverable error, exiting.")); |
284 |
|
|
cleanup_and_exit (EXIT_FAILURE); |
285 |
|
|
} |
286 |
|
|
|