ctdb-common: Add utility code to get various paths
[Samba.git] / ctdb / common / path.c
blob4e69739ba8661b88428e9dc260fe112d8427d88f
1 /*
2 Construct runtime paths
4 Copyright (C) Amitay Isaacs 2018
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "replace.h"
21 #include "system/filesys.h"
23 #include "lib/util/debug.h"
25 #include "common/path.h"
27 #define CTDB_CONFIG_FILE "ctdb.conf"
29 struct {
30 char *basedir;
31 char etcdir[PATH_MAX];
32 char rundir[PATH_MAX];
33 char vardir[PATH_MAX];
34 bool test_mode;
35 bool basedir_set;
36 bool etcdir_set;
37 bool rundir_set;
38 bool vardir_set;
39 } ctdb_paths = {
40 .etcdir = CTDB_ETCDIR,
41 .rundir = CTDB_RUNDIR,
42 .vardir = CTDB_VARDIR,
45 static void path_set_basedir(void)
47 const char *t;
49 t = getenv("CTDB_TEST_MODE");
50 if (t == NULL) {
51 goto done;
54 ctdb_paths.test_mode = true;
56 ctdb_paths.basedir = getenv("CTDB_BASE");
57 if (ctdb_paths.basedir == NULL) {
58 D_ERR("Broken CTDB setup, CTDB_BASE not set in test mode\n");
59 abort();
62 done:
63 ctdb_paths.basedir_set = true;
66 static bool path_construct(char *path, const char *subdir)
68 char p[PATH_MAX];
69 int len;
71 if (! ctdb_paths.basedir_set) {
72 path_set_basedir();
75 if (! ctdb_paths.test_mode) {
76 return true;
79 if (subdir == NULL) {
80 len = snprintf(p, sizeof(p), "%s", ctdb_paths.basedir);
81 } else {
82 len = snprintf(p,
83 sizeof(p),
84 "%s/%s",
85 ctdb_paths.basedir,
86 subdir);
89 if (len >= sizeof(p)) {
90 return false;
93 strncpy(path, p, PATH_MAX);
94 return true;
97 const char *path_etcdir(void)
99 bool ok;
101 if (! ctdb_paths.etcdir_set) {
102 ok = path_construct(ctdb_paths.etcdir, NULL);
103 if (!ok) {
104 D_ERR("Failed to construct ETCDIR\n");
105 } else {
106 ctdb_paths.etcdir_set = true;
110 return ctdb_paths.etcdir;
113 const char *path_rundir(void)
115 bool ok;
117 if (! ctdb_paths.rundir_set) {
118 ok = path_construct(ctdb_paths.rundir, "run");
119 if (!ok) {
120 D_ERR("Failed to construct RUNDIR\n");
121 } else {
122 ctdb_paths.rundir_set = true;
126 return ctdb_paths.rundir;
129 const char *path_vardir(void)
131 bool ok;
133 if (! ctdb_paths.rundir_set) {
134 ok = path_construct(ctdb_paths.vardir, "var");
135 if (!ok) {
136 D_ERR("Failed to construct VARDIR\n");
137 } else {
138 ctdb_paths.vardir_set = true;
142 return ctdb_paths.vardir;
145 char *path_etcdir_append(TALLOC_CTX *mem_ctx, const char *path)
147 return talloc_asprintf(mem_ctx, "%s/%s", path_etcdir(), path);
150 char *path_rundir_append(TALLOC_CTX *mem_ctx, const char *path)
152 return talloc_asprintf(mem_ctx, "%s/%s", path_rundir(), path);
155 char *path_vardir_append(TALLOC_CTX *mem_ctx, const char *path)
157 return talloc_asprintf(mem_ctx, "%s/%s", path_vardir(), path);
160 char *path_config(TALLOC_CTX *mem_ctx)
162 return path_etcdir_append(mem_ctx, CTDB_CONFIG_FILE);
165 char *path_socket(TALLOC_CTX *mem_ctx, const char *daemon)
167 return talloc_asprintf(mem_ctx,
168 "%s/%s.socket",
169 path_rundir(),
170 daemon);
173 char *path_pidfile(TALLOC_CTX *mem_ctx, const char *daemon)
175 return talloc_asprintf(mem_ctx,
176 "%s/%s.pid",
177 path_rundir(),
178 daemon);