ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/vped.C
Revision: 1.7
Committed: Tue Apr 15 03:48:40 2003 UTC (21 years, 1 month ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.6: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 /*
2 vped.C -- the main file for vped
3 Copyright (C) 1998-2002 Ivo Timmermans <ivo@o2w.nl>
4 2000-2002 Guus Sliepen <guus@sliepen.eu.org>
5 2003 Marc Lehmannn <pcg@goof.com>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 #include "config.h"
23
24 #include <cstdio>
25 #include <cstring>
26
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <getopt.h>
30 #include <signal.h>
31 #include <sys/types.h>
32 #include <unistd.h>
33 #include <signal.h>
34 #include <termios.h>
35
36 #include <sys/mman.h>
37
38 #include <openssl/rand.h>
39
40 #include "gettext.h"
41 #include "pidfile.h"
42
43 #include "conf.h"
44 #include "slog.h"
45 #include "util.h"
46 #include "vpn.h"
47 #include "iom.h"
48
49 vpn network;
50
51 static loglevel llevel = L_NONE;
52
53 /* If nonzero, display usage information and exit. */
54 static int show_help;
55
56 /* If nonzero, print the version on standard output and exit. */
57 static int show_version;
58
59 #if HAVE_MLOCKALL
60 /* If nonzero, disable swapping for this process. */
61 static int do_mlock = 0;
62 #endif
63
64 /* If zero, don't detach from the terminal. */
65 static int do_detach = 1;
66
67 static struct option const long_options[] =
68 {
69 {"config", required_argument, NULL, 'c'},
70 {"help", no_argument, &show_help, 1},
71 {"version", no_argument, &show_version, 1},
72 {"no-detach", no_argument, &do_detach, 0},
73 {"log-level", required_argument, NULL, 'l'},
74 #if HAVE_MLOCKALL
75 {"mlock", no_argument, &do_mlock, 1},
76 #endif
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 while ((r = getopt_long (argc, argv, "-c:DLl:", long_options, &option_index)) != EOF)
108 {
109 switch (r)
110 {
111 case 0: /* long option */
112 break;
113
114 case 1: /* this node name */
115 thisnode = strdup (optarg);
116 break;
117
118 case 'c': /* config file */
119 confbase = strdup (optarg);
120 break;
121
122 case 'D': /* no detach */
123 do_detach = 0;
124 break;
125
126 #if HAVE_MLOCKALL
127 case 'L': /* lock into memory */
128 do_mlock = 1;
129 break;
130 #endif
131
132 case 'l': /* inc debug level */
133 {
134 llevel = string_to_loglevel (optarg);
135
136 if (llevel == L_NONE)
137 slog (L_WARN, "'%s': %s", optarg, UNKNOWN_LOGLEVEL);
138 }
139 break;
140
141 case '?':
142 usage (1);
143
144 default:
145 break;
146 }
147 }
148 }
149
150 /*
151 Close network connections, and terminate neatly
152 */
153 void cleanup_and_exit(int c)
154 {
155 network.shutdown_all ();
156
157 if (pidfilename)
158 remove_pid (pidfilename);
159
160 slog (L_INFO, _("terminating with exit code %d"), c);
161
162 exit (c);
163 }
164
165 /*
166 Signal handlers.
167 */
168 RETSIGTYPE
169 sigterm_handler (int a)
170 {
171 network.events |= vpn::EVENT_SHUTDOWN;
172 network.event.start (0);
173 }
174
175 RETSIGTYPE
176 sighup_handler (int a)
177 {
178 network.events |= vpn::EVENT_RECONNECT;
179 network.event.start (0);
180 }
181
182 RETSIGTYPE
183 sigusr1_handler (int a)
184 {
185 network.dump_status ();
186 }
187
188 RETSIGTYPE
189 sigusr2_handler (int a)
190 {
191 }
192
193 void
194 setup_signals (void)
195 {
196 struct sigaction act;
197
198 sigfillset (&act.sa_mask);
199 act.sa_flags = 0;
200
201 act.sa_handler = sighup_handler; sigaction (SIGHUP , &act, NULL);
202 act.sa_handler = sigusr1_handler; sigaction (SIGUSR1, &act, NULL);
203 act.sa_handler = sigusr2_handler; sigaction (SIGUSR2, &act, NULL);
204 act.sa_handler = SIG_IGN; sigaction (SIGCHLD, &act, NULL);
205 act.sa_handler = SIG_IGN; sigaction (SIGPIPE, &act, NULL);
206 act.sa_flags = SA_RESETHAND;
207 act.sa_handler = sigterm_handler; sigaction (SIGINT , &act, NULL);
208 act.sa_handler = sigterm_handler; sigaction (SIGTERM, &act, NULL);
209 }
210
211 int
212 main (int argc, char **argv, char **envp)
213 {
214 set_loglevel (L_INFO);
215 set_identity (argv[0]);
216 log_to (LOGTO_SYSLOG | LOGTO_STDERR);
217
218 setlocale (LC_ALL, "");
219 bindtextdomain (PACKAGE, LOCALEDIR);
220 textdomain (PACKAGE);
221
222 parse_options (argc, argv, envp);
223
224 if (show_version)
225 {
226 printf (_("%s version %s (built %s %s, protocol %d.%d)\n"), get_identity (),
227 VERSION, __DATE__, __TIME__, PROTOCOL_MAJOR, PROTOCOL_MINOR);
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
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 make_names ();
252 conf.read_config (true);
253
254 set_loglevel (llevel != L_NONE ? llevel : conf.llevel);
255
256 RAND_load_file ("/dev/urandom", 1024);
257
258 if (!THISNODE)
259 {
260 slog (L_ERR, _("current node not set, or node '%s' not found in configfile, use the -n switch when starting vped."),
261 thisnode ? thisnode : "<unset>");
262 exit (1);
263 }
264
265 if (detach (do_detach))
266 exit (0);
267
268 setup_signals ();
269
270 if (!network.setup ())
271 {
272 iom.loop ();
273 cleanup_and_exit (1);
274 }
275
276 slog (L_ERR, _("unable to setup network, unrecoverable error, exiting."));
277 cleanup_and_exit (1);
278 }
279