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