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