2012-12-04 03:42:28 +00:00
|
|
|
/*
|
2016-10-19 05:56:00 +00:00
|
|
|
Serval DNA instance paths
|
|
|
|
Copyright (C) 2012-2015 Serval Project Inc.
|
2012-12-04 03:42:28 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2016-10-19 05:57:20 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2012-12-04 03:42:28 +00:00
|
|
|
#include <stdlib.h>
|
2014-05-23 08:19:00 +00:00
|
|
|
#include "instance.h"
|
2013-04-04 07:16:09 +00:00
|
|
|
#include "str.h"
|
2012-12-04 03:42:28 +00:00
|
|
|
#include "os.h"
|
|
|
|
#include "strbuf.h"
|
|
|
|
#include "strbuf_helpers.h"
|
|
|
|
|
2014-03-26 05:05:43 +00:00
|
|
|
/*
|
|
|
|
* A default INSTANCE_PATH can be set on the ./configure command line, eg:
|
|
|
|
*
|
|
|
|
* ./configure INSTANCE_PATH=/var/local/serval/node
|
|
|
|
*
|
2014-03-26 05:37:28 +00:00
|
|
|
* 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.
|
2014-03-26 05:05:43 +00:00
|
|
|
*/
|
|
|
|
#ifdef ANDROID
|
2014-03-26 05:37:28 +00:00
|
|
|
# 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 ""
|
2014-03-26 05:05:43 +00:00
|
|
|
#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
|
2012-12-04 03:42:28 +00:00
|
|
|
|
2014-03-26 05:05:43 +00:00
|
|
|
#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()
|
2013-09-19 07:49:37 +00:00
|
|
|
{
|
2014-03-26 05:05:43 +00:00
|
|
|
if (!know_instancepath) {
|
|
|
|
instancepath = getenv("SERVALINSTANCE_PATH");
|
2014-03-26 05:37:28 +00:00
|
|
|
#ifdef INSTANCE_PATH
|
2014-03-26 05:05:43 +00:00
|
|
|
if (instancepath == NULL)
|
2014-03-26 05:37:28 +00:00
|
|
|
instancepath = INSTANCE_PATH;
|
|
|
|
#endif
|
2014-03-26 05:05:43 +00:00
|
|
|
know_instancepath = 1;
|
|
|
|
}
|
|
|
|
return instancepath;
|
2013-09-19 07:49:37 +00:00
|
|
|
}
|
|
|
|
|
2016-10-19 05:56:00 +00:00
|
|
|
void set_instance_path(const char *path)
|
2016-01-18 01:29:43 +00:00
|
|
|
{
|
2016-10-19 05:56:00 +00:00
|
|
|
instancepath = strdup(path);
|
2016-01-18 01:29:43 +00:00
|
|
|
know_instancepath = 1;
|
|
|
|
}
|
|
|
|
|
2018-04-10 01:20:06 +00:00
|
|
|
static int vformf_basepath(struct __sourceloc __whence, strbuf b, const char *basepath, const char *fmt, va_list ap)
|
2012-12-04 03:42:28 +00:00
|
|
|
{
|
2014-03-26 05:05:43 +00:00
|
|
|
if (fmt)
|
2013-09-19 07:49:37 +00:00
|
|
|
strbuf_va_vprintf(b, fmt, ap);
|
2014-03-26 05:05:43 +00:00
|
|
|
if (!strbuf_overrun(b) && (strbuf_len(b) == 0 || strbuf_str(b)[0] != '/')) {
|
|
|
|
strbuf_reset(b);
|
2018-04-10 01:20:06 +00:00
|
|
|
strbuf_puts(b, basepath);
|
2014-03-26 05:05:43 +00:00
|
|
|
if (fmt) {
|
|
|
|
if (strbuf_substr(b, -1)[0] != '/')
|
|
|
|
strbuf_putc(b, '/');
|
|
|
|
strbuf_va_vprintf(b, fmt, ap);
|
|
|
|
}
|
2013-09-19 07:49:37 +00:00
|
|
|
}
|
2012-12-04 03:42:28 +00:00
|
|
|
if (!strbuf_overrun(b))
|
|
|
|
return 1;
|
2013-09-18 07:06:28 +00:00
|
|
|
WHYF("instance path overflow (strlen %lu, sizeof buffer %lu): %s",
|
|
|
|
(unsigned long)strbuf_count(b),
|
2014-03-26 05:05:43 +00:00
|
|
|
(unsigned long)strbuf_size(b),
|
|
|
|
alloca_str_toprint(strbuf_str(b)));
|
2012-12-04 03:42:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-04-10 01:20:06 +00:00
|
|
|
int _formf_path(struct __sourceloc __whence, char *buf, size_t bufsiz, const char *basepath, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
int ret = vformf_basepath(__whence, strbuf_local(buf, bufsiz), basepath, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int vformf_path(struct __sourceloc __whence, strbuf b, const char *syspath, const char *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
const char *ipath = instance_path();
|
|
|
|
if (!ipath)
|
|
|
|
ipath=syspath;
|
|
|
|
return vformf_basepath(__whence, b, ipath, fmt, ap);
|
|
|
|
}
|
|
|
|
|
2014-03-26 05:05:43 +00:00
|
|
|
int _formf_serval_etc_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_ETC_PATH, 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, 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, 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, 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, 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", fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int create_serval_instance_dir()
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
char path[PATH_MAX];
|
2015-08-24 07:08:55 +00:00
|
|
|
// emkdire_info can log if paths don't exist, which will also try to create paths...
|
|
|
|
// so try to create logging folders first
|
2015-10-13 08:12:22 +00:00
|
|
|
strbuf sb = strbuf_local_buf(path);
|
2014-03-26 05:05:43 +00:00
|
|
|
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;
|
2015-08-24 07:08:55 +00:00
|
|
|
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;
|
2014-03-26 05:05:43 +00:00
|
|
|
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;
|
2012-12-04 03:42:28 +00:00
|
|
|
}
|