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 etcdir
[PATH_MAX
];
32 char rundir
[PATH_MAX
];
33 char vardir
[PATH_MAX
];
40 .etcdir
= CTDB_ETCDIR
,
41 .rundir
= CTDB_RUNDIR
,
42 .vardir
= CTDB_VARDIR
,
45 static void path_set_basedir(void)
49 t
= getenv("CTDB_TEST_MODE");
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");
63 ctdb_paths
.basedir_set
= true;
66 static bool path_construct(char *path
, const char *subdir
)
71 if (! ctdb_paths
.basedir_set
) {
75 if (! ctdb_paths
.test_mode
) {
80 len
= snprintf(p
, sizeof(p
), "%s", ctdb_paths
.basedir
);
89 if (len
>= sizeof(p
)) {
93 strncpy(path
, p
, PATH_MAX
);
97 const char *path_etcdir(void)
101 if (! ctdb_paths
.etcdir_set
) {
102 ok
= path_construct(ctdb_paths
.etcdir
, NULL
);
104 D_ERR("Failed to construct ETCDIR\n");
106 ctdb_paths
.etcdir_set
= true;
110 return ctdb_paths
.etcdir
;
113 const char *path_rundir(void)
117 if (! ctdb_paths
.rundir_set
) {
118 ok
= path_construct(ctdb_paths
.rundir
, "run");
120 D_ERR("Failed to construct RUNDIR\n");
122 ctdb_paths
.rundir_set
= true;
126 return ctdb_paths
.rundir
;
129 const char *path_vardir(void)
133 if (! ctdb_paths
.rundir_set
) {
134 ok
= path_construct(ctdb_paths
.vardir
, "var");
136 D_ERR("Failed to construct VARDIR\n");
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
,
173 char *path_pidfile(TALLOC_CTX
*mem_ctx
, const char *daemon
)
175 return talloc_asprintf(mem_ctx
,