ctdb-common: Add utility code to get various paths
authorAmitay Isaacs <amitay@gmail.com>
Tue, 8 May 2018 03:02:33 +0000 (13:02 +1000)
committerMartin Schwenke <martins@samba.org>
Sat, 12 May 2018 10:06:28 +0000 (12:06 +0200)
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>
ctdb/common/path.c [new file with mode: 0644]
ctdb/common/path.h [new file with mode: 0644]
ctdb/wscript

diff --git a/ctdb/common/path.c b/ctdb/common/path.c
new file mode 100644 (file)
index 0000000..4e69739
--- /dev/null
@@ -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 (file)
index 0000000..370250a
--- /dev/null
@@ -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__ */
index c1a3dad28700d5d43ce90fa5973ed561e1eec63f..e8aafe1970a589496ae254c28c4906139fa44baa 100644 (file)
@@ -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''')