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.
30 #include "logger/logger.h"
31 #include "support/avl.h"
32 #include "support/sockaddr.h"
33 #include "support/xalloc.h"
37 static bool show_help = false;
38 static bool show_version = false;
39 static int kill_tincd = 0;
40 static bool bypass_security = false;
41 static bool do_mlock = false;
42 static bool use_logfile = false;
43 static bool do_detach = true;
44 static int debug_level = 1;
46 static char *confbase = NULL;
47 static char *identname = NULL;
48 static char *pidfilename = NULL;
49 static char *logfilename = NULL;
50 static char *cfgfilename = NULL;
56 static struct option const long_options[] = {
57 {"config", required_argument, NULL, 'c'},
58 {"kill", optional_argument, NULL, 'k'},
59 {"net", required_argument, NULL, 'n'},
60 {"help", no_argument, NULL, 1},
61 {"version", no_argument, NULL, 2},
62 {"no-detach", no_argument, NULL, 'D'},
63 {"debug", optional_argument, NULL, 'd'},
64 {"bypass-security", no_argument, NULL, 3},
65 {"mlock", no_argument, NULL, 'L'},
66 {"logfile", optional_argument, NULL, 4},
67 {"pidfile", required_argument, NULL, 5},
72 static struct WSAData wsa_state;
75 static void usage(bool status) {
77 fprintf(stderr, _("Try `%s --help\' for more information.\n"), tinc_argv[0]);
79 printf(_("Usage: %s [option]...\n\n"), tinc_argv[0]);
80 printf(_(" -c, --config=DIR Read configuration options from DIR.\n"
81 " -D, --no-detach Don't fork and detach.\n"
82 " -d, --debug[=LEVEL] Increase debug level or set it to LEVEL.\n"
83 " -k, --kill[=SIGNAL] Attempt to kill a running tincd and exit.\n"
84 " -n, --net=NETNAME Connect to net NETNAME.\n"
85 " -L, --mlock Lock tinc into main memory.\n"
86 " --logfile[=FILENAME] Write log entries to a logfile.\n"
87 " --pidfile=FILENAME Write PID to FILENAME.\n"
88 " --help Display this help and exit.\n"
89 " --version Output version information and exit.\n\n"));
90 printf(_("Report bugs to tinc@tinc-vpn.org.\n"));
94 static bool parse_options(int argc, char **argv) {
98 while((result = getopt_long(argc, argv, "c:DLd::k::n:", long_options, &option_index)) != EOF) {
103 case 'c': /* --config */
104 confbase = xstrdup(optarg);
107 case 'D': /* --no-detach */
111 case 'L': /* --mlock */
115 case 'd': /* --debug */
117 debug_level = atoi(optarg);
122 case 'k': /* --kill */
125 if(!strcasecmp(optarg, "HUP"))
127 else if(!strcasecmp(optarg, "TERM"))
128 kill_tincd = SIGTERM;
129 else if(!strcasecmp(optarg, "KILL"))
130 kill_tincd = SIGKILL;
131 else if(!strcasecmp(optarg, "USR1"))
132 kill_tincd = SIGUSR1;
133 else if(!strcasecmp(optarg, "USR2"))
134 kill_tincd = SIGUSR2;
135 else if(!strcasecmp(optarg, "WINCH"))
136 kill_tincd = SIGWINCH;
137 else if(!strcasecmp(optarg, "INT"))
139 else if(!strcasecmp(optarg, "ALRM"))
140 kill_tincd = SIGALRM;
142 kill_tincd = atoi(optarg);
145 fprintf(stderr, _("Invalid argument `%s'; SIGNAL must be a number or one of HUP, TERM, KILL, USR1, USR2, WINCH, INT or ALRM.\n"),
152 kill_tincd = SIGTERM;
158 case 'n': /* --net */
159 netname = xstrdup(optarg);
166 case 2: /* --version */
170 case 3: /* --bypass-security */
171 bypass_security = true;
174 case 4: /* --logfile */
177 logfilename = xstrdup(optarg);
180 case 5: /* --pidfile */
181 pidfilename = xstrdup(optarg);
196 static void make_names(void)
200 char installdir[1024] = "";
201 long len = sizeof(installdir);
205 asprintf(&identname, "tinc.%s", netname);
207 identname = xstrdup("tinc");
210 if(!RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\tinc", 0, KEY_READ, &key)) {
211 if(!RegQueryValueEx(key, NULL, 0, 0, installdir, &len)) {
213 asprintf(&logfilename, "%s/log/%s.log", identname);
216 asprintf(&confbase, "%s/%s", installdir, netname);
218 asprintf(&confbase, "%s", installdir);
228 asprintf(&pidfilename, LOCALSTATEDIR "/run/%s.pid", identname);
231 asprintf(&logfilename, LOCALSTATEDIR "/log/%s.log", identname);
235 asprintf(&confbase, CONFDIR "/tinc/%s", netname);
237 asprintf(&confbase, CONFDIR "/tinc");
240 asprintf(&cfgfilename, "%s/tinc.conf", confbase);
243 int main(int argc, char **argv) {
247 setlocale(LC_ALL, "");
248 bindtextdomain(PACKAGE, LOCALEDIR);
251 if(!parse_options(argc, argv))
257 printf(_("%s version %s (built %s %s, protocol %d)\n"), PACKAGE,
258 VERSION, __DATE__, __TIME__, PROT_CURRENT);
259 printf(_("Copyright (C) 1998-2004 Ivo Timmermans, Guus Sliepen and others.\n"
260 "See the AUTHORS file for a complete list.\n\n"
261 "tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
262 "and you are welcome to redistribute it under certain conditions;\n"
263 "see the file COPYING for details.\n"));
274 return !kill_other(kill_tincd);
276 openlogger("tinc", use_logfile?LOGMODE_FILE:LOGMODE_STDERR);
278 /* Lock all pages into memory if requested */
282 if(mlockall(MCL_CURRENT | MCL_FUTURE)) {
283 logger(LOG_ERR, _("System call `%s' failed: %s"), "mlockall",
287 logger(LOG_ERR, _("mlockall() not supported on this platform!"));
292 tinc_cfg = cfg_tree_new();
294 asprintf(cfgfilename, "%s/tinc.conf", confbase);
296 if(!cfg_read_file(tinc_cfg, cfgfilename))
300 if(WSAStartup(MAKEWORD(2, 2), &wsa_state)) {
301 logger(LOG_ERR, _("System call `%s' failed: %s"), "WSAStartup", winerror(GetLastError()));
306 if(do_detach && !detach())
309 if(!fd_init() || !tnl_init() || !rt_init())
318 logger(LOG_NOTICE, _("Terminating"));
321 remove_pid(pidfilename);