From fe25aa75386298b50192d91e57844cafe39ee571 Mon Sep 17 00:00:00 2001 From: Amitay Isaacs Date: Tue, 8 May 2018 13:02:33 +1000 Subject: [PATCH] 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 Reviewed-by: Martin Schwenke --- ctdb/common/path.c | 179 +++++++++++++++++++++++++++++++++++++++++++++++++++++ ctdb/common/path.h | 37 +++++++++++ ctdb/wscript | 2 +- 3 files changed, 217 insertions(+), 1 deletion(-) create mode 100644 ctdb/common/path.c create mode 100644 ctdb/common/path.h 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 . +*/ + +#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 . +*/ + +#ifndef __CTDB_PATH_H__ +#define __CTDB_PATH_H__ + +#include + +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__ */ diff --git a/ctdb/wscript b/ctdb/wscript index c1a3dad2870..e8aafe1970a 100644 --- a/ctdb/wscript +++ b/ctdb/wscript @@ -400,7 +400,7 @@ def build(bld): pidfile.c run_proc.c hash_count.c run_event.c sock_client.c version.c - cmdline.c + cmdline.c path.c '''), deps='''samba-util sys_rw tevent-util replace talloc tevent tdb popt''') -- 2.11.4.GIT