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/>.
24 #include "lib/util/debug.h"
26 #include "common/logging.h"
27 #include "common/cmdline.h"
28 #include "common/path.h"
29 #include "common/path_tool.h"
31 struct path_tool_context
{
32 struct cmdline_context
*cmdline
;
35 static int path_tool_config(TALLOC_CTX
*mem_ctx
,
40 struct path_tool_context
*ctx
= talloc_get_type_abort(
41 private_data
, struct path_tool_context
);
44 cmdline_usage(ctx
->cmdline
, "config");
48 printf("%s\n", path_config(mem_ctx
));
53 static int path_tool_pidfile(TALLOC_CTX
*mem_ctx
,
58 struct path_tool_context
*ctx
= talloc_get_type_abort(
59 private_data
, struct path_tool_context
);
63 cmdline_usage(ctx
->cmdline
, "pidfile");
67 p
= path_pidfile(mem_ctx
, argv
[0]);
69 D_ERR("Memory allocation error\n");
78 static int path_tool_socket(TALLOC_CTX
*mem_ctx
,
83 struct path_tool_context
*ctx
= talloc_get_type_abort(
84 private_data
, struct path_tool_context
);
88 cmdline_usage(ctx
->cmdline
, "socket");
92 p
= path_socket(mem_ctx
, argv
[0]);
94 D_ERR("Memory allocation error\n");
103 static int path_tool_datadir(TALLOC_CTX
*mem_ctx
,
108 struct path_tool_context
*ctx
= talloc_get_type_abort(
109 private_data
, struct path_tool_context
);
112 cmdline_usage(ctx
->cmdline
, "datadir");
116 printf("%s\n", path_datadir());
121 static int path_tool_datadir_append(TALLOC_CTX
*mem_ctx
,
126 struct path_tool_context
*ctx
= talloc_get_type_abort(
127 private_data
, struct path_tool_context
);
131 cmdline_usage(ctx
->cmdline
, "datadir append");
135 p
= path_datadir_append(mem_ctx
, argv
[0]);
137 D_ERR("Memory allocation error\n");
146 static int path_tool_etcdir(TALLOC_CTX
*mem_ctx
,
151 struct path_tool_context
*ctx
= talloc_get_type_abort(
152 private_data
, struct path_tool_context
);
155 cmdline_usage(ctx
->cmdline
, "etcdir");
159 printf("%s\n", path_etcdir());
164 static int path_tool_etcdir_append(TALLOC_CTX
*mem_ctx
,
169 struct path_tool_context
*ctx
= talloc_get_type_abort(
170 private_data
, struct path_tool_context
);
174 cmdline_usage(ctx
->cmdline
, "etcdir append");
178 p
= path_etcdir_append(mem_ctx
, argv
[0]);
180 D_ERR("Memory allocation error\n");
189 static int path_tool_rundir(TALLOC_CTX
*mem_ctx
,
194 struct path_tool_context
*ctx
= talloc_get_type_abort(
195 private_data
, struct path_tool_context
);
198 cmdline_usage(ctx
->cmdline
, "rundir");
202 printf("%s\n", path_rundir());
207 static int path_tool_rundir_append(TALLOC_CTX
*mem_ctx
,
212 struct path_tool_context
*ctx
= talloc_get_type_abort(
213 private_data
, struct path_tool_context
);
217 cmdline_usage(ctx
->cmdline
, "rundir append");
221 p
= path_rundir_append(mem_ctx
, argv
[0]);
223 D_ERR("Memory allocation error\n");
232 static int path_tool_vardir(TALLOC_CTX
*mem_ctx
,
237 struct path_tool_context
*ctx
= talloc_get_type_abort(
238 private_data
, struct path_tool_context
);
241 cmdline_usage(ctx
->cmdline
, "vardir");
245 printf("%s\n", path_vardir());
250 static int path_tool_vardir_append(TALLOC_CTX
*mem_ctx
,
255 struct path_tool_context
*ctx
= talloc_get_type_abort(
256 private_data
, struct path_tool_context
);
260 cmdline_usage(ctx
->cmdline
, "vardir append");
264 p
= path_vardir_append(mem_ctx
, argv
[0]);
266 D_ERR("Memory allocation error\n");
275 struct cmdline_command path_commands
[] = {
276 { "config", path_tool_config
,
277 "Get path of CTDB config file", NULL
},
278 { "pidfile", path_tool_pidfile
,
279 "Get path of CTDB daemon pidfile", "<daemon>" },
280 { "socket", path_tool_socket
,
281 "Get path of CTDB daemon socket", "<daemon>" },
282 { "datadir append", path_tool_datadir_append
,
283 "Get path relative to CTDB DATADIR", "<path>" },
284 { "datadir", path_tool_datadir
,
285 "Get path of CTDB DATADIR", NULL
},
286 { "etcdir append", path_tool_etcdir_append
,
287 "Get path relative to CTDB ETCDIR", "<path>" },
288 { "etcdir", path_tool_etcdir
,
289 "Get path of CTDB ETCDIR", NULL
},
290 { "rundir append", path_tool_rundir_append
,
291 "Get path relative to CTDB RUNDIR", "<path>" },
292 { "rundir", path_tool_rundir
,
293 "Get path of CTDB RUNDIR", NULL
},
294 { "vardir append", path_tool_vardir_append
,
295 "Get path relative to CTDB VARDIR", "<path>" },
296 { "vardir", path_tool_vardir
,
297 "Get path of CTDB VARDIR", NULL
},
301 int path_tool_init(TALLOC_CTX
*mem_ctx
,
303 struct poptOption
*options
,
307 struct path_tool_context
**result
)
309 struct path_tool_context
*ctx
;
312 ctx
= talloc_zero(mem_ctx
, struct path_tool_context
);
314 D_ERR("Memory allocation error\n");
318 ret
= cmdline_init(ctx
,
325 D_ERR("Failed to initialize cmdline, ret=%d\n", ret
);
330 ret
= cmdline_parse(ctx
->cmdline
, argc
, argv
, parse_options
);
332 cmdline_usage(ctx
->cmdline
, NULL
);
341 int path_tool_run(struct path_tool_context
*ctx
, int *result
)
343 return cmdline_run(ctx
->cmdline
, ctx
, result
);
346 #ifdef CTDB_PATH_TOOL
348 int main(int argc
, const char **argv
)
351 struct path_tool_context
*ctx
;
354 mem_ctx
= talloc_new(NULL
);
355 if (mem_ctx
== NULL
) {
356 fprintf(stderr
, "Memory allocation error\n");
360 ret
= path_tool_init(mem_ctx
,
368 talloc_free(mem_ctx
);
372 setup_logging("ctdb-path", DEBUG_STDERR
);
373 debuglevel_set(DEBUG_ERR
);
375 ret
= path_tool_run(ctx
, &result
);
380 talloc_free(mem_ctx
);
384 #endif /* CTDB_PATH_TOOL */