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_etcdir(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, "etcdir");
116 printf("%s\n", path_etcdir());
121 static int path_tool_etcdir_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, "etcdir append");
135 p = path_etcdir_append(mem_ctx, argv[0]);
137 D_ERR("Memory allocation error\n");
146 static int path_tool_rundir(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, "rundir");
159 printf("%s\n", path_rundir());
164 static int path_tool_rundir_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, "rundir append");
178 p = path_rundir_append(mem_ctx, argv[0]);
180 D_ERR("Memory allocation error\n");
189 static int path_tool_vardir(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, "vardir");
202 printf("%s\n", path_vardir());
207 static int path_tool_vardir_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, "vardir append");
221 p = path_vardir_append(mem_ctx, argv[0]);
223 D_ERR("Memory allocation error\n");
232 struct cmdline_command path_commands[] = {
233 { "config", path_tool_config,
234 "Get path of CTDB config file", NULL },
235 { "pidfile", path_tool_pidfile,
236 "Get path of CTDB daemon pidfile", "<daemon>" },
237 { "socket", path_tool_socket,
238 "Get path of CTDB daemon socket", "<daemon>" },
239 { "etcdir append", path_tool_etcdir_append,
240 "Get path relative to CTDB ETCDIR", "<path>" },
241 { "etcdir", path_tool_etcdir,
242 "Get path of CTDB ETCDIR", NULL },
243 { "rundir append", path_tool_rundir_append,
244 "Get path relative to CTDB RUNDIR", "<path>" },
245 { "rundir", path_tool_rundir,
246 "Get path of CTDB RUNDIR", NULL },
247 { "vardir append", path_tool_vardir_append,
248 "Get path relative to CTDB VARDIR", "<path>" },
249 { "vardir", path_tool_vardir,
250 "Get path of CTDB VARDIR", NULL },
254 int path_tool_init(TALLOC_CTX *mem_ctx,
256 struct poptOption *options,
260 struct path_tool_context **result)
262 struct path_tool_context *ctx;
265 ctx = talloc_zero(mem_ctx, struct path_tool_context);
267 D_ERR("Memory allocation error\n");
271 ret = cmdline_init(ctx, prog, options, path_commands, &ctx->cmdline);
273 D_ERR("Failed to initialize cmdline, ret=%d\n", ret);
278 ret = cmdline_parse(ctx->cmdline, argc, argv, parse_options);
280 cmdline_usage(ctx->cmdline, NULL);
289 int path_tool_run(struct path_tool_context *ctx, int *result)
291 return cmdline_run(ctx->cmdline, ctx, result);
294 #ifdef CTDB_PATH_TOOL
296 int main(int argc, const char **argv)
299 struct path_tool_context *ctx;
302 mem_ctx = talloc_new(NULL);
303 if (mem_ctx == NULL) {
304 fprintf(stderr, "Memory allocation error\n");
308 ret = path_tool_init(mem_ctx,
316 talloc_free(mem_ctx);
320 setup_logging("ctdb-path", DEBUG_STDERR);
321 DEBUGLEVEL = DEBUG_ERR;
323 ret = path_tool_run(ctx, &result);
328 talloc_free(mem_ctx);
332 #endif /* CTDB_PATH_TOOL */