Ensure "make distcheck" really runs without errors.
[tinc] / src / names.c
1 /*
2     names.c -- generate commonly used (file)names
3     Copyright (C) 1998-2005 Ivo Timmermans
4                   2000-2017 Guus Sliepen <guus@tinc-vpn.org>
5
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.
10
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.
15
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.
19 */
20
21 #include "system.h"
22
23 #include "logger.h"
24 #include "names.h"
25 #include "xalloc.h"
26
27 char *netname = NULL;
28 char *myname = NULL;
29 char *confdir = NULL;           /* base configuration directory */
30 char *confbase = NULL;          /* base configuration directory for this instance of tinc */
31 bool confbase_given;
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;
37
38 /*
39   Set all files and paths according to netname
40 */
41 void make_names(bool daemon) {
42 #ifdef HAVE_MINGW
43         HKEY key;
44         char installdir[1024] = "";
45         DWORD len = sizeof(installdir);
46 #endif
47         confbase_given = confbase;
48
49         if(netname && confbase) {
50                 logger(DEBUG_ALWAYS, LOG_INFO, "Both netname and configuration directory given, using the latter...");
51         }
52
53         if(netname) {
54                 xasprintf(&identname, "tinc.%s", netname);
55         } else {
56                 identname = xstrdup("tinc");
57         }
58
59 #ifdef HAVE_MINGW
60
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);
64
65                         if(!confbase) {
66                                 if(netname) {
67                                         xasprintf(&confbase, "%s" SLASH "%s", installdir, netname);
68                                 } else {
69                                         xasprintf(&confbase, "%s", installdir);
70                                 }
71                         }
72
73                         if(!logfilename) {
74                                 xasprintf(&logfilename, "%s" SLASH "tinc.log", confbase);
75                         }
76                 }
77
78                 RegCloseKey(key);
79         }
80
81 #endif
82
83         if(!confdir) {
84                 confdir = xstrdup(CONFDIR SLASH "tinc");
85         }
86
87         if(!confbase) {
88                 if(netname) {
89                         xasprintf(&confbase, CONFDIR SLASH "tinc" SLASH "%s", netname);
90                 } else {
91                         xasprintf(&confbase, CONFDIR SLASH "tinc");
92                 }
93         }
94
95 #ifdef HAVE_MINGW
96
97         if(!logfilename) {
98                 xasprintf(&logfilename, "%s" SLASH "log", confbase);
99         }
100
101         if(!pidfilename) {
102                 xasprintf(&pidfilename, "%s" SLASH "pid", confbase);
103         }
104
105 #else
106         bool fallback = false;
107
108         if(daemon) {
109                 if(access(LOCALSTATEDIR, R_OK | W_OK | X_OK)) {
110                         fallback = true;
111                 }
112         } else {
113                 char fname[PATH_MAX];
114                 snprintf(fname, sizeof(fname), LOCALSTATEDIR SLASH "run" SLASH "%s.pid", identname);
115
116                 if(access(fname, R_OK)) {
117                         snprintf(fname, sizeof(fname), "%s" SLASH "pid", confbase);
118
119                         if(!access(fname, R_OK)) {
120                                 fallback = true;
121                         }
122                 }
123         }
124
125         if(!fallback) {
126                 if(!logfilename) {
127                         xasprintf(&logfilename, LOCALSTATEDIR SLASH "log" SLASH "%s.log", identname);
128                 }
129
130                 if(!pidfilename) {
131                         xasprintf(&pidfilename, LOCALSTATEDIR SLASH "run" SLASH "%s.pid", identname);
132                 }
133         } else {
134                 if(!logfilename) {
135                         xasprintf(&logfilename, "%s" SLASH "log", confbase);
136                 }
137
138                 if(!pidfilename) {
139                         if(daemon) {
140                                 logger(DEBUG_ALWAYS, LOG_WARNING, "Could not access " LOCALSTATEDIR SLASH " (%s), storing pid and socket files in %s" SLASH, strerror(errno), confbase);
141                         }
142
143                         xasprintf(&pidfilename, "%s" SLASH "pid", confbase);
144                 }
145         }
146
147 #endif
148
149         if(!unixsocketname) {
150                 int len = strlen(pidfilename);
151                 unixsocketname = xmalloc(len + 8);
152                 memcpy(unixsocketname, pidfilename, len);
153
154                 if(len > 4 && !strcmp(pidfilename + len - 4, ".pid")) {
155                         strncpy(unixsocketname + len - 4, ".socket", 8);
156                 } else {
157                         strncpy(unixsocketname + len, ".socket", 8);
158                 }
159         }
160 }
161
162 void free_names(void) {
163         free(identname);
164         free(netname);
165         free(unixsocketname);
166         free(pidfilename);
167         free(logfilename);
168         free(confbase);
169         free(confdir);
170         free(myname);
171
172         identname = NULL;
173         netname = NULL;
174         unixsocketname = NULL;
175         pidfilename = NULL;
176         logfilename = NULL;
177         confbase = NULL;
178         confdir = NULL;
179         myname = NULL;
180 }