2 names.c -- generate commonly used (file)names
3 Copyright (C) 1998-2005 Ivo Timmermans
4 2000-2017 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.
29 char *confdir = NULL; /* base configuration directory */
30 char *confbase = NULL; /* base configuration directory for this instance of tinc */
32 char *identname = NULL; /* program name for syslog */
33 char *unixsocketname = NULL; /* UNIX socket location */
34 char *logfilename = NULL; /* log file location */
35 char *pidfilename = NULL;
36 char *program_name = NULL;
39 Set all files and paths according to netname
41 void make_names(bool daemon) {
44 char installdir[1024] = "";
45 DWORD len = sizeof installdir;
47 confbase_given = confbase;
49 if(netname && confbase)
50 logger(DEBUG_ALWAYS, LOG_INFO, "Both netname and configuration directory given, using the latter...");
53 xasprintf(&identname, "tinc.%s", netname);
55 identname = xstrdup("tinc");
58 if(!RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\tinc", 0, KEY_READ, &key)) {
59 if(!RegQueryValueEx(key, NULL, 0, 0, (LPBYTE)installdir, &len)) {
60 confdir = xstrdup(installdir);
63 xasprintf(&confbase, "%s" SLASH "%s", installdir, netname);
65 xasprintf(&confbase, "%s", installdir);
68 xasprintf(&logfilename, "%s" SLASH "tinc.log", confbase);
74 confdir = xstrdup(CONFDIR SLASH "tinc");
78 xasprintf(&confbase, CONFDIR SLASH "tinc" SLASH "%s", netname);
80 xasprintf(&confbase, CONFDIR SLASH "tinc");
85 xasprintf(&logfilename, "%s" SLASH "log", confbase);
88 xasprintf(&pidfilename, "%s" SLASH "pid", confbase);
90 bool fallback = false;
92 if(access(LOCALSTATEDIR, R_OK | W_OK | X_OK))
96 snprintf(fname, sizeof fname, LOCALSTATEDIR SLASH "run" SLASH "%s.pid", identname);
97 if(access(fname, R_OK)) {
98 snprintf(fname, sizeof fname, "%s" SLASH "pid", confbase);
99 if(!access(fname, R_OK))
106 xasprintf(&logfilename, LOCALSTATEDIR SLASH "log" SLASH "%s.log", identname);
109 xasprintf(&pidfilename, LOCALSTATEDIR SLASH "run" SLASH "%s.pid", identname);
112 xasprintf(&logfilename, "%s" SLASH "log", confbase);
116 logger(DEBUG_ALWAYS, LOG_WARNING, "Could not access " LOCALSTATEDIR SLASH " (%s), storing pid and socket files in %s" SLASH, strerror(errno), confbase);
117 xasprintf(&pidfilename, "%s" SLASH "pid", confbase);
122 if(!unixsocketname) {
123 int len = strlen(pidfilename);
124 unixsocketname = xmalloc(len + 8);
125 memcpy(unixsocketname, pidfilename, len);
126 if(len > 4 && !strcmp(pidfilename + len - 4, ".pid"))
127 strncpy(unixsocketname + len - 4, ".socket", 8);
129 strncpy(unixsocketname + len, ".socket", 8);
133 void free_names(void) {
136 free(unixsocketname);
145 unixsocketname = NULL;