Fix version_get.h generation on BSD.
[tinc] / src / names.c
1 /*
2     names.c -- generate commonly used (file)names
3     Copyright (C) 1998-2005 Ivo Timmermans
4                   2000-2015 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 *confdir = NULL;           /* base configuration directory */
29 char *confbase = NULL;          /* base configuration directory for this instance of tinc */
30 bool confbase_given;
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;
36
37 /*
38   Set all files and paths according to netname
39 */
40 void make_names(bool daemon) {
41 #ifdef HAVE_MINGW
42         HKEY key;
43         char installdir[1024] = "";
44         DWORD len = sizeof installdir;
45 #endif
46         confbase_given = confbase;
47
48         if(netname && confbase)
49                 logger(DEBUG_ALWAYS, LOG_INFO, "Both netname and configuration directory given, using the latter...");
50
51         if(netname)
52                 xasprintf(&identname, "tinc.%s", netname);
53         else
54                 identname = xstrdup("tinc");
55
56 #ifdef HAVE_MINGW
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);
60                         if(!confbase) {
61                                 if(netname)
62                                         xasprintf(&confbase, "%s" SLASH "%s", installdir, netname);
63                                 else
64                                         xasprintf(&confbase, "%s", installdir);
65                         }
66                         if(!logfilename)
67                                 xasprintf(&logfilename, "%s" SLASH "tinc.log", confbase);
68                 }
69                 RegCloseKey(key);
70         }
71 #endif
72         if(!confdir)
73                 confdir = xstrdup(CONFDIR SLASH "tinc");
74
75         if(!confbase) {
76                 if(netname)
77                         xasprintf(&confbase, CONFDIR SLASH "tinc" SLASH "%s", netname);
78                 else
79                         xasprintf(&confbase, CONFDIR SLASH "tinc");
80         }
81
82 #ifdef HAVE_MINGW
83         if(!logfilename)
84                 xasprintf(&logfilename, "%s" SLASH "log", confbase);
85
86         if(!pidfilename)
87                 xasprintf(&pidfilename, "%s" SLASH "pid", confbase);
88 #else
89         bool fallback = false;
90         if(daemon) {
91                 if(access(LOCALSTATEDIR, R_OK | W_OK | X_OK))
92                         fallback = true;
93         } else {
94                 char fname[PATH_MAX];
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))
99                                 fallback = true;
100                 }
101         }
102
103         if(!fallback) {
104                 if(!logfilename)
105                         xasprintf(&logfilename, LOCALSTATEDIR SLASH "log" SLASH "%s.log", identname);
106
107                 if(!pidfilename)
108                         xasprintf(&pidfilename, LOCALSTATEDIR SLASH "run" SLASH "%s.pid", identname);
109         } else {
110                 if(!logfilename)
111                         xasprintf(&logfilename, "%s" SLASH "log", confbase);
112
113                 if(!pidfilename) {
114                         if(daemon)
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);
117                 }
118         }
119 #endif
120
121         if(!unixsocketname) {
122                 int len = strlen(pidfilename);
123                 unixsocketname = xmalloc(len + 8);
124                 strcpy(unixsocketname, pidfilename);
125                 if(len > 4 && !strcmp(pidfilename + len - 4, ".pid"))
126                         strcpy(unixsocketname + len - 4, ".socket");
127                 else
128                         strcpy(unixsocketname + len, ".socket");
129         }
130 }
131
132 void free_names(void) {
133         free(identname);
134         free(netname);
135         free(unixsocketname);
136         free(pidfilename);
137         free(logfilename);
138         free(confbase);
139         free(confdir);
140 }