diff options
author | Amitay Isaacs <amitay@gmail.com> | 2018-05-08 13:02:33 +1000 |
---|---|---|
committer | Martin Schwenke <martins@samba.org> | 2018-05-12 12:06:28 +0200 |
commit | fe25aa75386298b50192d91e57844cafe39ee571 (patch) | |
tree | a3167034528049a131179a378519ed87dd1f9378 /ctdb/common | |
parent | 356dacc6d701b6d4ffa91f20f208e0d09a4136e6 (diff) | |
download | samba-fe25aa75386298b50192d91e57844cafe39ee571.tar.gz |
ctdb-common: Add utility code to get various paths
This will construct correct paths when running with CTDB_TEST_MODE.
Signed-off-by: Amitay Isaacs <amitay@gmail.com>
Reviewed-by: Martin Schwenke <martin@meltin.net>
Diffstat (limited to 'ctdb/common')
-rw-r--r-- | ctdb/common/path.c | 179 | ||||
-rw-r--r-- | ctdb/common/path.h | 37 |
2 files changed, 216 insertions, 0 deletions
diff --git a/ctdb/common/path.c b/ctdb/common/path.c new file mode 100644 index 00000000000..4e69739ba86 --- /dev/null +++ b/ctdb/common/path.c @@ -0,0 +1,179 @@ +/* + Construct runtime paths + + Copyright (C) Amitay Isaacs 2018 + + 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 3 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, see <http://www.gnu.org/licenses/>. +*/ + +#include "replace.h" +#include "system/filesys.h" + +#include "lib/util/debug.h" + +#include "common/path.h" + +#define CTDB_CONFIG_FILE "ctdb.conf" + +struct { + char *basedir; + char etcdir[PATH_MAX]; + char rundir[PATH_MAX]; + char vardir[PATH_MAX]; + bool test_mode; + bool basedir_set; + bool etcdir_set; + bool rundir_set; + bool vardir_set; +} ctdb_paths = { + .etcdir = CTDB_ETCDIR, + .rundir = CTDB_RUNDIR, + .vardir = CTDB_VARDIR, +}; + +static void path_set_basedir(void) +{ + const char *t; + + t = getenv("CTDB_TEST_MODE"); + if (t == NULL) { + goto done; + } + + ctdb_paths.test_mode = true; + + ctdb_paths.basedir = getenv("CTDB_BASE"); + if (ctdb_paths.basedir == NULL) { + D_ERR("Broken CTDB setup, CTDB_BASE not set in test mode\n"); + abort(); + } + +done: + ctdb_paths.basedir_set = true; +} + +static bool path_construct(char *path, const char *subdir) +{ + char p[PATH_MAX]; + int len; + + if (! ctdb_paths.basedir_set) { + path_set_basedir(); + } + + if (! ctdb_paths.test_mode) { + return true; + } + + if (subdir == NULL) { + len = snprintf(p, sizeof(p), "%s", ctdb_paths.basedir); + } else { + len = snprintf(p, + sizeof(p), + "%s/%s", + ctdb_paths.basedir, + subdir); + } + + if (len >= sizeof(p)) { + return false; + } + + strncpy(path, p, PATH_MAX); + return true; +} + +const char *path_etcdir(void) +{ + bool ok; + + if (! ctdb_paths.etcdir_set) { + ok = path_construct(ctdb_paths.etcdir, NULL); + if (!ok) { + D_ERR("Failed to construct ETCDIR\n"); + } else { + ctdb_paths.etcdir_set = true; + } + } + + return ctdb_paths.etcdir; +} + +const char *path_rundir(void) +{ + bool ok; + + if (! ctdb_paths.rundir_set) { + ok = path_construct(ctdb_paths.rundir, "run"); + if (!ok) { + D_ERR("Failed to construct RUNDIR\n"); + } else { + ctdb_paths.rundir_set = true; + } + } + + return ctdb_paths.rundir; +} + +const char *path_vardir(void) +{ + bool ok; + + if (! ctdb_paths.rundir_set) { + ok = path_construct(ctdb_paths.vardir, "var"); + if (!ok) { + D_ERR("Failed to construct VARDIR\n"); + } else { + ctdb_paths.vardir_set = true; + } + } + + return ctdb_paths.vardir; +} + +char *path_etcdir_append(TALLOC_CTX *mem_ctx, const char *path) +{ + return talloc_asprintf(mem_ctx, "%s/%s", path_etcdir(), path); +} + +char *path_rundir_append(TALLOC_CTX *mem_ctx, const char *path) +{ + return talloc_asprintf(mem_ctx, "%s/%s", path_rundir(), path); +} + +char *path_vardir_append(TALLOC_CTX *mem_ctx, const char *path) +{ + return talloc_asprintf(mem_ctx, "%s/%s", path_vardir(), path); +} + +char *path_config(TALLOC_CTX *mem_ctx) +{ + return path_etcdir_append(mem_ctx, CTDB_CONFIG_FILE); +} + +char *path_socket(TALLOC_CTX *mem_ctx, const char *daemon) +{ + return talloc_asprintf(mem_ctx, + "%s/%s.socket", + path_rundir(), + daemon); +} + +char *path_pidfile(TALLOC_CTX *mem_ctx, const char *daemon) +{ + return talloc_asprintf(mem_ctx, + "%s/%s.pid", + path_rundir(), + daemon); +} diff --git a/ctdb/common/path.h b/ctdb/common/path.h new file mode 100644 index 00000000000..370250a0cfe --- /dev/null +++ b/ctdb/common/path.h @@ -0,0 +1,37 @@ +/* + Construct runtime paths + + Copyright (C) Amitay Isaacs 2018 + + 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 3 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, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef __CTDB_PATH_H__ +#define __CTDB_PATH_H__ + +#include <talloc.h> + +const char *path_etcdir(void); +const char *path_rundir(void); +const char *path_vardir(void); + +char *path_etcdir_append(TALLOC_CTX *mem_ctx, const char *path); +char *path_rundir_append(TALLOC_CTX *mem_ctx, const char *path); +char *path_vardir_append(TALLOC_CTX *mem_ctx, const char *path); + +char *path_config(TALLOC_CTX *mem_ctx); +char *path_socket(TALLOC_CTX *mem_ctx, const char *daemon); +char *path_pidfile(TALLOC_CTX *mem_ctx, const char *daemon); + +#endif /* __CTDB_PATH_H__ */ |