ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/vped.C
Revision: 1.8
Committed: Mon Sep 1 21:23:35 2003 UTC (20 years, 8 months ago) by pcg
Content type: text/plain
Branch: MAIN
CVS Tags: VPE_1_0
Changes since 1.7: +3 -0 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 pcg 1.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 pcg 1.8 #include <openssl/err.h>
39 pcg 1.1 #include <openssl/rand.h>
40    
41     #include "gettext.h"
42     #include "pidfile.h"
43    
44     #include "conf.h"
45     #include "slog.h"
46     #include "util.h"
47 pcg 1.5 #include "vpn.h"
48 pcg 1.3 #include "iom.h"
49 pcg 1.1
50     vpn network;
51    
52 pcg 1.2 static loglevel llevel = L_NONE;
53    
54 pcg 1.1 /* If nonzero, display usage information and exit. */
55     static int show_help;
56    
57     /* If nonzero, print the version on standard output and exit. */
58     static int show_version;
59    
60     #if HAVE_MLOCKALL
61     /* If nonzero, disable swapping for this process. */
62     static int do_mlock = 0;
63     #endif
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     #if HAVE_MLOCKALL
76     {"mlock", no_argument, &do_mlock, 1},
77     #endif
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     #if HAVE_MLOCKALL
128     case 'L': /* lock into memory */
129     do_mlock = 1;
130     break;
131     #endif
132    
133     case 'l': /* inc debug level */
134     {
135 pcg 1.2 llevel = string_to_loglevel (optarg);
136 pcg 1.1
137 pcg 1.2 if (llevel == L_NONE)
138 pcg 1.1 slog (L_WARN, "'%s': %s", optarg, UNKNOWN_LOGLEVEL);
139     }
140     break;
141    
142     case '?':
143     usage (1);
144    
145     default:
146     break;
147     }
148     }
149     }
150    
151     /*
152     Close network connections, and terminate neatly
153     */
154     void cleanup_and_exit(int c)
155     {
156     network.shutdown_all ();
157    
158     if (pidfilename)
159     remove_pid (pidfilename);
160    
161     slog (L_INFO, _("terminating with exit code %d"), c);
162    
163     exit (c);
164     }
165    
166     /*
167     Signal handlers.
168     */
169     RETSIGTYPE
170     sigterm_handler (int a)
171     {
172     network.events |= vpn::EVENT_SHUTDOWN;
173 pcg 1.3 network.event.start (0);
174 pcg 1.1 }
175    
176     RETSIGTYPE
177     sighup_handler (int a)
178     {
179     network.events |= vpn::EVENT_RECONNECT;
180 pcg 1.3 network.event.start (0);
181 pcg 1.1 }
182    
183     RETSIGTYPE
184     sigusr1_handler (int a)
185     {
186 pcg 1.4 network.dump_status ();
187 pcg 1.1 }
188    
189     RETSIGTYPE
190     sigusr2_handler (int a)
191     {
192     }
193    
194     void
195     setup_signals (void)
196     {
197     struct sigaction act;
198    
199     sigfillset (&act.sa_mask);
200     act.sa_flags = 0;
201    
202     act.sa_handler = sighup_handler; sigaction (SIGHUP , &act, NULL);
203     act.sa_handler = sigusr1_handler; sigaction (SIGUSR1, &act, NULL);
204     act.sa_handler = sigusr2_handler; sigaction (SIGUSR2, &act, NULL);
205     act.sa_handler = SIG_IGN; sigaction (SIGCHLD, &act, NULL);
206 pcg 1.6 act.sa_handler = SIG_IGN; sigaction (SIGPIPE, &act, NULL);
207 pcg 1.1 act.sa_flags = SA_RESETHAND;
208     act.sa_handler = sigterm_handler; sigaction (SIGINT , &act, NULL);
209     act.sa_handler = sigterm_handler; sigaction (SIGTERM, &act, NULL);
210     }
211    
212     int
213     main (int argc, char **argv, char **envp)
214     {
215 pcg 1.8 ERR_load_crypto_strings (); // we have the RAM
216    
217 pcg 1.1 set_loglevel (L_INFO);
218     set_identity (argv[0]);
219     log_to (LOGTO_SYSLOG | LOGTO_STDERR);
220    
221     setlocale (LC_ALL, "");
222     bindtextdomain (PACKAGE, LOCALEDIR);
223     textdomain (PACKAGE);
224    
225     parse_options (argc, argv, envp);
226    
227     if (show_version)
228     {
229 pcg 1.7 printf (_("%s version %s (built %s %s, protocol %d.%d)\n"), get_identity (),
230 pcg 1.1 VERSION, __DATE__, __TIME__, PROTOCOL_MAJOR, PROTOCOL_MINOR);
231     printf (_
232     ("Copyright (C) 2003 Marc Lehmann <vpe@plan9.de> and others.\n"
233     "See the AUTHORS file for a complete list.\n\n"
234     "tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
235     "and you are welcome to redistribute it under certain conditions;\n"
236     "see the file COPYING for details.\n"));
237    
238     return 0;
239     }
240    
241     if (show_help)
242     usage (0);
243    
244     log_to (LOGTO_SYSLOG | LOGTO_STDERR);
245    
246     /* Lock all pages into memory if requested */
247    
248     #if HAVE_MLOCKALL
249     if (do_mlock)
250     if (mlockall (MCL_CURRENT | MCL_FUTURE))
251     slog (L_ERR, _("system call `%s' failed: %s"), "mlockall", strerror (errno));
252     #endif
253    
254     make_names ();
255     conf.read_config (true);
256    
257 pcg 1.2 set_loglevel (llevel != L_NONE ? llevel : conf.llevel);
258    
259 pcg 1.1 RAND_load_file ("/dev/urandom", 1024);
260    
261 pcg 1.3 if (!THISNODE)
262 pcg 1.1 {
263     slog (L_ERR, _("current node not set, or node '%s' not found in configfile, use the -n switch when starting vped."),
264     thisnode ? thisnode : "<unset>");
265     exit (1);
266     }
267    
268     if (detach (do_detach))
269     exit (0);
270    
271     setup_signals ();
272    
273     if (!network.setup ())
274     {
275 pcg 1.3 iom.loop ();
276 pcg 1.1 cleanup_and_exit (1);
277     }
278    
279     slog (L_ERR, _("unable to setup network, unrecoverable error, exiting."));
280     cleanup_and_exit (1);
281     }
282