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...");
54 xasprintf(&identname, "tinc.%s", netname);
56 identname = xstrdup("tinc");
61 if(!RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\tinc", 0, KEY_READ, &key)) {
62 if(!RegQueryValueEx(key, NULL, 0, 0, (LPBYTE)installdir, &len)) {
63 confdir = xstrdup(installdir);
67 xasprintf(&confbase, "%s" SLASH "%s", installdir, netname);
69 xasprintf(&confbase, "%s", installdir);
74 xasprintf(&logfilename, "%s" SLASH "tinc.log", confbase);
84 confdir = xstrdup(CONFDIR SLASH "tinc");
89 xasprintf(&confbase, CONFDIR SLASH "tinc" SLASH "%s", netname);
91 xasprintf(&confbase, CONFDIR SLASH "tinc");
98 xasprintf(&logfilename, "%s" SLASH "log", confbase);
102 xasprintf(&pidfilename, "%s" SLASH "pid", confbase);
106 bool fallback = false;
109 if(access(LOCALSTATEDIR, R_OK | W_OK | X_OK)) {
113 char fname[PATH_MAX];
114 snprintf(fname, sizeof(fname), LOCALSTATEDIR SLASH "run" SLASH "%s.pid", identname);
116 if(access(fname, R_OK)) {
117 snprintf(fname, sizeof(fname), "%s" SLASH "pid", confbase);
119 if(!access(fname, R_OK)) {
127 xasprintf(&logfilename, LOCALSTATEDIR SLASH "log" SLASH "%s.log", identname);
131 xasprintf(&pidfilename, LOCALSTATEDIR SLASH "run" SLASH "%s.pid", identname);
135 xasprintf(&logfilename, "%s" SLASH "log", confbase);
140 logger(DEBUG_ALWAYS, LOG_WARNING, "Could not access " LOCALSTATEDIR SLASH " (%s), storing pid and socket files in %s" SLASH, strerror(errno), confbase);
143 xasprintf(&pidfilename, "%s" SLASH "pid", confbase);
149 if(!unixsocketname) {
150 int len = strlen(pidfilename);
151 unixsocketname = xmalloc(len + 8);
152 memcpy(unixsocketname, pidfilename, len);
154 if(len > 4 && !strcmp(pidfilename + len - 4, ".pid")) {
155 strncpy(unixsocketname + len - 4, ".socket", 8);
157 strncpy(unixsocketname + len, ".socket", 8);
162 void free_names(void) {
165 free(unixsocketname);
174 unixsocketname = NULL;