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/>.
21 #include "system/filesys.h"
23 #include "lib/util/debug.h"
25 #include "common/path.h"
27 #define CTDB_CONFIG_FILE "ctdb.conf"
31 char datadir
[PATH_MAX
];
32 char etcdir
[PATH_MAX
];
33 char rundir
[PATH_MAX
];
34 char vardir
[PATH_MAX
];
42 .datadir
= CTDB_DATADIR
,
43 .etcdir
= CTDB_ETCDIR
,
44 .rundir
= CTDB_RUNDIR
,
45 .vardir
= CTDB_VARDIR
,
48 static void path_set_basedir(void)
52 t
= getenv("CTDB_TEST_MODE");
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");
66 ctdb_paths
.basedir_set
= true;
69 static bool path_construct(char *path
, const char *subdir
)
74 if (! ctdb_paths
.basedir_set
) {
78 if (! ctdb_paths
.test_mode
) {
83 len
= snprintf(p
, sizeof(p
), "%s", ctdb_paths
.basedir
);
92 if ((size_t)len
>= sizeof(p
)) {
96 strncpy(path
, p
, PATH_MAX
);
100 const char *path_datadir(void)
104 if (! ctdb_paths
.datadir_set
) {
105 ok
= path_construct(ctdb_paths
.datadir
, "share");
107 D_ERR("Failed to construct DATADIR\n");
109 ctdb_paths
.datadir_set
= true;
113 return ctdb_paths
.datadir
;
116 const char *path_etcdir(void)
120 if (! ctdb_paths
.etcdir_set
) {
121 ok
= path_construct(ctdb_paths
.etcdir
, NULL
);
123 D_ERR("Failed to construct ETCDIR\n");
125 ctdb_paths
.etcdir_set
= true;
129 return ctdb_paths
.etcdir
;
132 const char *path_rundir(void)
136 if (! ctdb_paths
.rundir_set
) {
137 ok
= path_construct(ctdb_paths
.rundir
, "run");
139 D_ERR("Failed to construct RUNDIR\n");
141 ctdb_paths
.rundir_set
= true;
145 return ctdb_paths
.rundir
;
148 const char *path_vardir(void)
152 if (! ctdb_paths
.vardir_set
) {
153 ok
= path_construct(ctdb_paths
.vardir
, "var");
155 D_ERR("Failed to construct VARDIR\n");
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");
195 return talloc_strdup(mem_ctx
, t
);
199 return talloc_asprintf(mem_ctx
,
205 char *path_pidfile(TALLOC_CTX
*mem_ctx
, const char *daemon
)
207 return talloc_asprintf(mem_ctx
,