2 tincd.c -- the main file for tincd
4 Copyright (C) 2000-2004 Guus Sliepen <guus@tinc-vpn.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 /* Darwin (MacOS/X) needs the following definition... */
28 #ifndef _P1003_1B_VISIBLE
29 #define _P1003_1B_VISIBLE
32 #ifdef HAVE_SYS_MMAN_H
40 #include "logger/logger.h"
42 #include "support/avl.h"
43 #include "support/sockaddr.h"
44 #include "support/xalloc.h"
48 static bool show_help = false;
49 static bool show_version = false;
50 static int kill_tincd = 0;
51 static bool bypass_security = false;
52 static bool do_mlock = false;
53 static bool do_detach = true;
54 static int debug_level = 1;
56 char *tinc_confbase = NULL;
57 char *tinc_netname = NULL;
58 char *tinc_identname = NULL;
59 char *tinc_pidfilename = NULL;
60 char *tinc_logfilename = NULL;
61 char *tinc_cfgfilename = NULL;
63 bool tinc_use_logfile = false;
69 static struct option const long_options[] = {
70 {"config", required_argument, NULL, 'c'},
71 {"kill", optional_argument, NULL, 'k'},
72 {"net", required_argument, NULL, 'n'},
73 {"help", no_argument, NULL, 1},
74 {"version", no_argument, NULL, 2},
75 {"no-detach", no_argument, NULL, 'D'},
76 {"debug", optional_argument, NULL, 'd'},
77 {"bypass-security", no_argument, NULL, 3},
78 {"mlock", no_argument, NULL, 'L'},
79 {"logfile", optional_argument, NULL, 4},
80 {"pidfile", required_argument, NULL, 5},
85 static struct WSAData wsa_state;
88 static void usage(bool status) {
90 fprintf(stderr, _("Try `%s --help\' for more information.\n"), tinc_argv[0]);
92 printf(_("Usage: %s [option]...\n\n"), tinc_argv[0]);
93 printf(_(" -c, --config=DIR Read configuration options from DIR.\n"
94 " -D, --no-detach Don't fork and detach.\n"
95 " -d, --debug[=LEVEL] Increase debug level or set it to LEVEL.\n"
96 " -k, --kill[=SIGNAL] Attempt to kill a running tincd and exit.\n"
97 " -n, --net=NETNAME Connect to net NETNAME.\n"
98 " -L, --mlock Lock tinc into main memory.\n"
99 " --logfile[=FILENAME] Write log entries to a logfile.\n"
100 " --pidfile=FILENAME Write PID to FILENAME.\n"
101 " --help Display this help and exit.\n"
102 " --version Output version information and exit.\n\n"));
103 printf(_("Report bugs to tinc@tinc-vpn.org.\n"));
107 static bool parse_options(int argc, char **argv) {
109 int option_index = 0;
111 while((result = getopt_long(argc, argv, "c:DLd::k::n:", long_options, &option_index)) != EOF) {
116 case 'c': /* --config */
117 tinc_confbase = xstrdup(optarg);
120 case 'D': /* --no-detach */
124 case 'L': /* --mlock */
128 case 'd': /* --debug */
130 debug_level = atoi(optarg);
135 case 'k': /* --kill */
138 if(!strcasecmp(optarg, "HUP"))
140 else if(!strcasecmp(optarg, "TERM"))
141 kill_tincd = SIGTERM;
142 else if(!strcasecmp(optarg, "KILL"))
143 kill_tincd = SIGKILL;
144 else if(!strcasecmp(optarg, "USR1"))
145 kill_tincd = SIGUSR1;
146 else if(!strcasecmp(optarg, "USR2"))
147 kill_tincd = SIGUSR2;
148 else if(!strcasecmp(optarg, "WINCH"))
149 kill_tincd = SIGWINCH;
150 else if(!strcasecmp(optarg, "INT"))
152 else if(!strcasecmp(optarg, "ALRM"))
153 kill_tincd = SIGALRM;
155 kill_tincd = atoi(optarg);
158 fprintf(stderr, _("Invalid argument `%s'; SIGNAL must be a number or one of HUP, TERM, KILL, USR1, USR2, WINCH, INT or ALRM.\n"),
165 kill_tincd = SIGTERM;
171 case 'n': /* --net */
172 tinc_netname = xstrdup(optarg);
179 case 2: /* --version */
183 case 3: /* --bypass-security */
184 bypass_security = true;
187 case 4: /* --logfile */
188 tinc_use_logfile = true;
190 tinc_logfilename = xstrdup(optarg);
193 case 5: /* --pidfile */
194 tinc_pidfilename = xstrdup(optarg);
209 static void make_names(void)
213 char installdir[1024] = "";
214 long len = sizeof(installdir);
218 asprintf(&tinc_identname, "tinc.%s", tinc_netname);
220 tinc_identname = xstrdup("tinc");
223 if(!RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\tinc", 0, KEY_READ, &key)) {
224 if(!RegQueryValueEx(key, NULL, 0, 0, installdir, &len)) {
225 if(!tinc_logfilename)
226 asprintf(&tinc_logfilename, "%s/log/%s.log", tinc_identname);
229 asprintf(&tinc_confbase, "%s/%s", installdir, tinc_netname);
231 asprintf(&tinc_confbase, "%s", installdir);
240 if(!tinc_pidfilename)
241 asprintf(&tinc_pidfilename, LOCALSTATEDIR "/run/%s.pid", tinc_identname);
243 if(!tinc_logfilename)
244 asprintf(&tinc_logfilename, LOCALSTATEDIR "/log/%s.log", tinc_identname);
248 asprintf(&tinc_confbase, CONFDIR "/tinc/%s", tinc_netname);
250 asprintf(&tinc_confbase, CONFDIR "/tinc");
253 asprintf(&tinc_cfgfilename, "%s/tinc.conf", tinc_confbase);
256 int main(int argc, char **argv) {
260 setlocale(LC_ALL, "");
261 bindtextdomain(PACKAGE, LOCALEDIR);
264 if(!parse_options(argc, argv))
270 printf(_("%s version %s (built %s %s, protocol %d/%d)\n"), PACKAGE,
271 VERSION, __DATE__, __TIME__, TNL_PROTOCOL, RT_PROTOCOL);
272 printf(_("Copyright (C) 1998-2004 Ivo Timmermans, Guus Sliepen and others.\n"
273 "See the AUTHORS file for a complete list.\n\n"
274 "tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
275 "and you are welcome to redistribute it under certain conditions;\n"
276 "see the file COPYING for details.\n"));
287 return !kill_other(kill_tincd);
289 logger_init("tinc", tinc_use_logfile ? LOGGER_MODE_FILE : LOGGER_MODE_STDERR);
291 /* Lock all pages into memory if requested */
295 if(mlockall(MCL_CURRENT | MCL_FUTURE)) {
296 logger(LOG_ERR, _("System call `%s' failed: %s"), "mlockall",
300 logger(LOG_ERR, _("mlockall() not supported on this platform!"));
305 tinc_cfg = cfg_tree_new();
307 asprintf(&tinc_cfgfilename, "%s/tinc.conf", tinc_confbase);
309 if(!cfg_read_file(tinc_cfg, tinc_cfgfilename))
313 if(WSAStartup(MAKEWORD(2, 2), &wsa_state)) {
314 logger(LOG_ERR, _("System call `%s' failed: %s"), "WSAStartup", winerror(GetLastError()));
319 if(do_detach && !detach())
322 logger(LOG_NOTICE, _("tincd %s (%s %s) starting, debug level %d"),
323 VERSION, __DATE__, __TIME__, logger_level);
325 if(!fd_init() || !rt_init())
333 logger(LOG_NOTICE, _("Terminating"));
336 remove_pid(tinc_pidfilename);