2 names.c -- generate commonly used (file)names
3 Copyright (C) 1998-2005 Ivo Timmermans
4 2000-2015 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 along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 char *confdir = NULL; /* base configuration directory */
29 char *confbase = NULL; /* base configuration directory for this instance of tinc */
31 char *identname = NULL; /* program name for syslog */
32 char *unixsocketname = NULL; /* UNIX socket location */
33 char *logfilename = NULL; /* log file location */
34 char *pidfilename = NULL;
35 char *program_name = NULL;
38 Set all files and paths according to netname
40 void make_names(bool daemon) {
43 char installdir[1024] = "";
44 DWORD len = sizeof installdir;
46 confbase_given = confbase;
48 if(netname && confbase)
49 logger(DEBUG_ALWAYS, LOG_INFO, "Both netname and configuration directory given, using the latter...");
52 xasprintf(&identname, "tinc.%s", netname);
54 identname = xstrdup("tinc");
57 if(!RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\tinc", 0, KEY_READ, &key)) {
58 if(!RegQueryValueEx(key, NULL, 0, 0, (LPBYTE)installdir, &len)) {
59 confdir = xstrdup(installdir);
62 xasprintf(&confbase, "%s" SLASH "%s", installdir, netname);
64 xasprintf(&confbase, "%s", installdir);
67 xasprintf(&logfilename, "%s" SLASH "tinc.log", confbase);
73 confdir = xstrdup(CONFDIR SLASH "tinc");
77 xasprintf(&confbase, CONFDIR SLASH "tinc" SLASH "%s", netname);
79 xasprintf(&confbase, CONFDIR SLASH "tinc");
84 xasprintf(&logfilename, "%s" SLASH "log", confbase);
87 xasprintf(&pidfilename, "%s" SLASH "pid", confbase);
89 bool fallback = false;
91 if(access(LOCALSTATEDIR, R_OK | W_OK | X_OK))
95 snprintf(fname, sizeof fname, LOCALSTATEDIR SLASH "run" SLASH "%s.pid", identname);
96 if(access(fname, R_OK)) {
97 snprintf(fname, sizeof fname, "%s" SLASH "pid", confbase);
98 if(!access(fname, R_OK))
105 xasprintf(&logfilename, LOCALSTATEDIR SLASH "log" SLASH "%s.log", identname);
108 xasprintf(&pidfilename, LOCALSTATEDIR SLASH "run" SLASH "%s.pid", identname);
111 xasprintf(&logfilename, "%s" SLASH "log", confbase);
115 logger(DEBUG_ALWAYS, LOG_WARNING, "Could not access " LOCALSTATEDIR SLASH " (%s), storing pid and socket files in %s" SLASH, strerror(errno), confbase);
116 xasprintf(&pidfilename, "%s" SLASH "pid", confbase);
121 if(!unixsocketname) {
122 int len = strlen(pidfilename);
123 unixsocketname = xmalloc(len + 8);
124 memcpy(unixsocketname, pidfilename, len);
125 if(len > 4 && !strcmp(pidfilename + len - 4, ".pid"))
126 strncpy(unixsocketname + len - 4, ".socket", 8);
128 strncpy(unixsocketname + len, ".socket", 8);
132 void free_names(void) {
135 free(unixsocketname);