ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/vped.C
Revision: 1.22
Committed: Thu Mar 3 16:54:34 2005 UTC (19 years, 2 months ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.21: +5 -3 lines
Log Message:
*** empty log message ***

File Contents

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