-
Notifications
You must be signed in to change notification settings - Fork 80
/
instance.c
261 lines (228 loc) · 7.24 KB
/
instance.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
Serval DNA instance paths
Copyright (C) 2012-2015 Serval Project Inc.
Copyright (C) 2016-2018 Flinders University
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#ifdef __APPLE__
#include "TargetConditionals.h"
#endif
#include "instance.h"
#include "conf.h"
#include "str.h"
#include "os.h"
#include "strbuf.h"
#include "strbuf_helpers.h"
/*
* A default INSTANCE_PATH can be set on the ./configure command line, eg:
*
* ./configure INSTANCE_PATH=/var/local/serval/node
*
* This will cause servald to never use FHS paths, and always use an instance path, even if the
* SERVALINSTANCE_PATH environment variable is not set.
*
* On Android systems, the INSTANCE_PATH macro is set in Android.mk, so Android builds of servald
* always use an instance path and never fall back to FHS paths.
*/
#ifdef ANDROID
# define SERVAL_ETC_PATH ""
# define SERVAL_RUN_PATH ""
# define SYSTEM_LOG_PATH ""
# define SERVAL_LOG_PATH ""
# define SERVAL_CACHE_PATH ""
# define SERVAL_TMP_PATH ""
# define RHIZOME_STORE_PATH ""
#endif
/* The following paths are based on the Filesystem Hierarchy Standard (FHS) 2.3
* but can be altered using ./configure arguments, eg:
*
* ./configure SERVAL_LOG_PATH=/tmp/serval/log
*/
#ifndef SERVAL_ETC_PATH
#define SERVAL_ETC_PATH SYSCONFDIR "/serval"
#endif
#ifndef SERVAL_RUN_PATH
#define SERVAL_RUN_PATH LOCALSTATEDIR "/run/serval"
#endif
#ifndef SYSTEM_LOG_PATH
#define SYSTEM_LOG_PATH LOCALSTATEDIR "/log"
#endif
#ifndef SERVAL_LOG_PATH
#define SERVAL_LOG_PATH SYSTEM_LOG_PATH "/serval"
#endif
#ifndef SERVAL_CACHE_PATH
#define SERVAL_CACHE_PATH LOCALSTATEDIR "/cache/serval"
#endif
#ifndef SERVAL_TMP_PATH
#define SERVAL_TMP_PATH "/tmp/serval"
#endif
#ifndef RHIZOME_STORE_PATH
#define RHIZOME_STORE_PATH SERVAL_CACHE_PATH
#endif
static int know_instancepath = 0;
static char *instancepath = NULL;
const char *instance_path()
{
if (!know_instancepath) {
instancepath = getenv("SERVALINSTANCE_PATH");
#ifdef INSTANCE_PATH
if (instancepath == NULL)
instancepath = INSTANCE_PATH;
#endif
know_instancepath = 1;
}
return instancepath;
}
void set_instance_path(const char *path)
{
instancepath = strdup(path);
know_instancepath = 1;
}
static int vformf_path(struct __sourceloc __whence, strbuf b, const char *fhs_path, const char *configured_path, const char *fmt, va_list ap)
{
const char *ipath = instance_path();
strbuf_path_join(b, ipath ? ipath : fhs_path, NULL);
if (configured_path)
strbuf_path_join(b, configured_path, NULL);
assert(strbuf_str(b)[0] == '/');
int fmt_overrun = 0;
if (fmt) {
strbuf sb = strbuf_alloca(strbuf_size(b));
strbuf_va_vprintf(sb, fmt, ap);
strbuf_path_join(b, strbuf_str(sb), NULL);
fmt_overrun = strbuf_overrun(sb);
}
if (!strbuf_overrun(b) && !fmt_overrun)
return 1;
WHYF("instance path overflow (strlen %lu, sizeof buffer %lu): %s",
(unsigned long)strbuf_count(b),
(unsigned long)strbuf_size(b),
alloca_str_toprint(strbuf_str(b)));
return 0;
}
int _formf_serval_etc_path(struct __sourceloc __whence, char *buf, size_t bufsiz, const char *fmt, ...)
{
va_list ap;
char *base_path = SERVAL_ETC_PATH;
#ifdef __APPLE__
#ifdef TARGET_OS_IPHONE
// iOS device
char containerised_path[8192];
if (getenv("HOME")) {
snprintf(containerised_path,8192,"%s/%s",getenv("HOME"),SERVAL_ETC_PATH);
base_path=containerised_path;
}
#endif
#endif
va_start(ap, fmt);
int ret = vformf_path(__whence, strbuf_local(buf, bufsiz), SERVAL_ETC_PATH, NULL, fmt, ap);
va_end(ap);
return ret;
}
int _formf_serval_run_path(struct __sourceloc __whence, char *buf, size_t bufsiz, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
int ret = _vformf_serval_run_path(__whence, buf, bufsiz, fmt, ap);
va_end(ap);
return ret;
}
int _vformf_serval_run_path(struct __sourceloc __whence, char *buf, size_t bufsiz, const char *fmt, va_list ap)
{
return vformf_path(__whence, strbuf_local(buf, bufsiz), SERVAL_RUN_PATH, NULL, fmt, ap);
}
strbuf strbuf_system_log_path(strbuf sb)
{
const char *ipath = instance_path();
strbuf_puts(sb, ipath ? ipath : SYSTEM_LOG_PATH);
return sb;
}
strbuf strbuf_serval_log_path(strbuf sb)
{
const char *ipath = instance_path();
if (ipath)
strbuf_path_join(sb, ipath, "log", NULL);
else
strbuf_puts(sb, SERVAL_LOG_PATH);
return sb;
}
int _formf_serval_cache_path(struct __sourceloc __whence, char *buf, size_t bufsiz, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
int ret = vformf_path(__whence, strbuf_local(buf, bufsiz), SERVAL_CACHE_PATH, NULL, fmt, ap);
va_end(ap);
return ret;
}
int _formf_rhizome_store_path(struct __sourceloc __whence, char *buf, size_t bufsiz, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
int ret = vformf_path(__whence, strbuf_local(buf, bufsiz), RHIZOME_STORE_PATH, config.rhizome.datastore_path, fmt, ap);
va_end(ap);
return ret;
}
int _formf_rhizome_store_legacy_path(struct __sourceloc __whence, char *buf, size_t bufsiz, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
int ret = vformf_path(__whence, strbuf_local(buf, bufsiz), RHIZOME_STORE_PATH, NULL, fmt, ap);
va_end(ap);
return ret;
}
int _formf_serval_tmp_path(struct __sourceloc __whence, char *buf, size_t bufsiz, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
int ret = vformf_path(__whence, strbuf_local(buf, bufsiz), SERVAL_TMP_PATH, NULL, fmt, ap);
va_end(ap);
return ret;
}
int _formf_servald_proc_path(struct __sourceloc __whence, char *buf, size_t bufsiz, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
int ret = vformf_path(__whence, strbuf_local(buf, bufsiz), SERVAL_RUN_PATH "/proc", NULL, fmt, ap);
va_end(ap);
return ret;
}
int create_serval_instance_dir()
{
int ret = 0;
char path[PATH_MAX];
// emkdire_info can log if paths don't exist, which will also try to create paths...
// so try to create logging folders first
strbuf sb = strbuf_local_buf(path);
strbuf_system_log_path(sb);
if (!strbuf_overrun(sb) && emkdirs_info(path, 0700) == -1)
ret = -1;
strbuf_reset(sb);
strbuf_serval_log_path(sb);
if (!strbuf_overrun(sb) && emkdirs_info(path, 0700) == -1)
ret = -1;
if (FORMF_SERVAL_ETC_PATH(path, NULL) && emkdirs_info(path, 0755) == -1)
ret = -1;
if (FORMF_SERVAL_RUN_PATH(path, NULL) && emkdirs_info(path, 0700) == -1)
ret = -1;
if (FORMF_SERVAL_CACHE_PATH(path, NULL) && emkdirs_info(path, 0700) == -1)
ret = -1;
if (FORMF_SERVAL_TMP_PATH(path, NULL) && emkdirs_info(path, 0700) == -1)
ret = -1;
if (FORMF_SERVALD_PROC_PATH(path, NULL) && emkdirs_info(path, 0755) == -1)
ret = -1;
return ret;
}