s3:include: Remove trailing whitepaces in MacExtensions.h
[Samba.git] / ctdb / common / path.c
blobea3b08f4b2eb04446fc20b1d26246ab884a6da3b
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 datadir[PATH_MAX];
32 char etcdir[PATH_MAX];
33 char rundir[PATH_MAX];
34 char vardir[PATH_MAX];
35 bool test_mode;
36 bool basedir_set;
37 bool datadir_set;
38 bool etcdir_set;
39 bool rundir_set;
40 bool vardir_set;
41 } ctdb_paths = {
42 .datadir = CTDB_DATADIR,
43 .etcdir = CTDB_ETCDIR,
44 .rundir = CTDB_RUNDIR,
45 .vardir = CTDB_VARDIR,
48 static void path_set_basedir(void)
50 const char *t;
52 t = getenv("CTDB_TEST_MODE");
53 if (t == NULL) {
54 goto done;
57 ctdb_paths.test_mode = true;
59 ctdb_paths.basedir = getenv("CTDB_BASE");
60 if (ctdb_paths.basedir == NULL) {
61 D_ERR("Broken CTDB setup, CTDB_BASE not set in test mode\n");
62 abort();
65 done:
66 ctdb_paths.basedir_set = true;
69 static bool path_construct(char *path, const char *subdir)
71 char p[PATH_MAX];
72 int len;
74 if (! ctdb_paths.basedir_set) {
75 path_set_basedir();
78 if (! ctdb_paths.test_mode) {
79 return true;
82 if (subdir == NULL) {
83 len = snprintf(p, sizeof(p), "%s", ctdb_paths.basedir);
84 } else {
85 len = snprintf(p,
86 sizeof(p),
87 "%s/%s",
88 ctdb_paths.basedir,
89 subdir);
92 if ((size_t)len >= sizeof(p)) {
93 return false;
96 strncpy(path, p, PATH_MAX);
97 return true;
100 const char *path_datadir(void)
102 bool ok;
104 if (! ctdb_paths.datadir_set) {
105 ok = path_construct(ctdb_paths.datadir, "share");
106 if (!ok) {
107 D_ERR("Failed to construct DATADIR\n");
108 } else {
109 ctdb_paths.datadir_set = true;
113 return ctdb_paths.datadir;
116 const char *path_etcdir(void)
118 bool ok;
120 if (! ctdb_paths.etcdir_set) {
121 ok = path_construct(ctdb_paths.etcdir, NULL);
122 if (!ok) {
123 D_ERR("Failed to construct ETCDIR\n");
124 } else {
125 ctdb_paths.etcdir_set = true;
129 return ctdb_paths.etcdir;
132 const char *path_rundir(void)
134 bool ok;
136 if (! ctdb_paths.rundir_set) {
137 ok = path_construct(ctdb_paths.rundir, "run");
138 if (!ok) {
139 D_ERR("Failed to construct RUNDIR\n");
140 } else {
141 ctdb_paths.rundir_set = true;
145 return ctdb_paths.rundir;
148 const char *path_vardir(void)
150 bool ok;
152 if (! ctdb_paths.vardir_set) {
153 ok = path_construct(ctdb_paths.vardir, "var");
154 if (!ok) {
155 D_ERR("Failed to construct VARDIR\n");
156 } else {
157 ctdb_paths.vardir_set = true;
161 return ctdb_paths.vardir;
164 char *path_datadir_append(TALLOC_CTX *mem_ctx, const char *path)
166 return talloc_asprintf(mem_ctx, "%s/%s", path_datadir(), path);
169 char *path_etcdir_append(TALLOC_CTX *mem_ctx, const char *path)
171 return talloc_asprintf(mem_ctx, "%s/%s", path_etcdir(), path);
174 char *path_rundir_append(TALLOC_CTX *mem_ctx, const char *path)
176 return talloc_asprintf(mem_ctx, "%s/%s", path_rundir(), path);
179 char *path_vardir_append(TALLOC_CTX *mem_ctx, const char *path)
181 return talloc_asprintf(mem_ctx, "%s/%s", path_vardir(), path);
184 char *path_config(TALLOC_CTX *mem_ctx)
186 return path_etcdir_append(mem_ctx, CTDB_CONFIG_FILE);
189 char *path_socket(TALLOC_CTX *mem_ctx, const char *daemon)
191 if (strcmp(daemon, "ctdbd") == 0) {
192 const char *t = getenv("CTDB_SOCKET");
194 if (t != NULL) {
195 return talloc_strdup(mem_ctx, t);
199 return talloc_asprintf(mem_ctx,
200 "%s/%s.socket",
201 path_rundir(),
202 daemon);
205 char *path_pidfile(TALLOC_CTX *mem_ctx, const char *daemon)
207 return talloc_asprintf(mem_ctx,
208 "%s/%s.pid",
209 path_rundir(),
210 daemon);