smbd: squash check_path_syntax() variants
[Samba.git] / ctdb / common / path_tool.c
blob44d29b6b00fd1af1551c941301c47be2a557b982
1 /*
2 path tool
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"
22 #include <talloc.h>
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,
36 int argc,
37 const char **argv,
38 void *private_data)
40 struct path_tool_context *ctx = talloc_get_type_abort(
41 private_data, struct path_tool_context);
43 if (argc != 0) {
44 cmdline_usage(ctx->cmdline, "config");
45 return EINVAL;
48 printf("%s\n", path_config(mem_ctx));
50 return 0;
53 static int path_tool_pidfile(TALLOC_CTX *mem_ctx,
54 int argc,
55 const char **argv,
56 void *private_data)
58 struct path_tool_context *ctx = talloc_get_type_abort(
59 private_data, struct path_tool_context);
60 char *p;
62 if (argc != 1) {
63 cmdline_usage(ctx->cmdline, "pidfile");
64 return EINVAL;
67 p = path_pidfile(mem_ctx, argv[0]);
68 if (p == NULL) {
69 D_ERR("Memory allocation error\n");
70 return 1;
73 printf("%s\n", p);
75 return 0;
78 static int path_tool_socket(TALLOC_CTX *mem_ctx,
79 int argc,
80 const char **argv,
81 void *private_data)
83 struct path_tool_context *ctx = talloc_get_type_abort(
84 private_data, struct path_tool_context);
85 char *p;
87 if (argc != 1) {
88 cmdline_usage(ctx->cmdline, "socket");
89 return EINVAL;
92 p = path_socket(mem_ctx, argv[0]);
93 if (p == NULL) {
94 D_ERR("Memory allocation error\n");
95 return 1;
98 printf("%s\n", p);
100 return 0;
103 static int path_tool_datadir(TALLOC_CTX *mem_ctx,
104 int argc,
105 const char **argv,
106 void *private_data)
108 struct path_tool_context *ctx = talloc_get_type_abort(
109 private_data, struct path_tool_context);
111 if (argc != 0) {
112 cmdline_usage(ctx->cmdline, "datadir");
113 return EINVAL;
116 printf("%s\n", path_datadir());
118 return 0;
121 static int path_tool_datadir_append(TALLOC_CTX *mem_ctx,
122 int argc,
123 const char **argv,
124 void *private_data)
126 struct path_tool_context *ctx = talloc_get_type_abort(
127 private_data, struct path_tool_context);
128 char *p;
130 if (argc != 1) {
131 cmdline_usage(ctx->cmdline, "datadir append");
132 return EINVAL;
135 p = path_datadir_append(mem_ctx, argv[0]);
136 if (p == NULL) {
137 D_ERR("Memory allocation error\n");
138 return 1;
141 printf("%s\n", p);
143 return 0;
146 static int path_tool_etcdir(TALLOC_CTX *mem_ctx,
147 int argc,
148 const char **argv,
149 void *private_data)
151 struct path_tool_context *ctx = talloc_get_type_abort(
152 private_data, struct path_tool_context);
154 if (argc != 0) {
155 cmdline_usage(ctx->cmdline, "etcdir");
156 return EINVAL;
159 printf("%s\n", path_etcdir());
161 return 0;
164 static int path_tool_etcdir_append(TALLOC_CTX *mem_ctx,
165 int argc,
166 const char **argv,
167 void *private_data)
169 struct path_tool_context *ctx = talloc_get_type_abort(
170 private_data, struct path_tool_context);
171 char *p;
173 if (argc != 1) {
174 cmdline_usage(ctx->cmdline, "etcdir append");
175 return EINVAL;
178 p = path_etcdir_append(mem_ctx, argv[0]);
179 if (p == NULL) {
180 D_ERR("Memory allocation error\n");
181 return 1;
184 printf("%s\n", p);
186 return 0;
189 static int path_tool_rundir(TALLOC_CTX *mem_ctx,
190 int argc,
191 const char **argv,
192 void *private_data)
194 struct path_tool_context *ctx = talloc_get_type_abort(
195 private_data, struct path_tool_context);
197 if (argc != 0) {
198 cmdline_usage(ctx->cmdline, "rundir");
199 return EINVAL;
202 printf("%s\n", path_rundir());
204 return 0;
207 static int path_tool_rundir_append(TALLOC_CTX *mem_ctx,
208 int argc,
209 const char **argv,
210 void *private_data)
212 struct path_tool_context *ctx = talloc_get_type_abort(
213 private_data, struct path_tool_context);
214 char *p;
216 if (argc != 1) {
217 cmdline_usage(ctx->cmdline, "rundir append");
218 return EINVAL;
221 p = path_rundir_append(mem_ctx, argv[0]);
222 if (p == NULL) {
223 D_ERR("Memory allocation error\n");
224 return 1;
227 printf("%s\n", p);
229 return 0;
232 static int path_tool_vardir(TALLOC_CTX *mem_ctx,
233 int argc,
234 const char **argv,
235 void *private_data)
237 struct path_tool_context *ctx = talloc_get_type_abort(
238 private_data, struct path_tool_context);
240 if (argc != 0) {
241 cmdline_usage(ctx->cmdline, "vardir");
242 return EINVAL;
245 printf("%s\n", path_vardir());
247 return 0;
250 static int path_tool_vardir_append(TALLOC_CTX *mem_ctx,
251 int argc,
252 const char **argv,
253 void *private_data)
255 struct path_tool_context *ctx = talloc_get_type_abort(
256 private_data, struct path_tool_context);
257 char *p;
259 if (argc != 1) {
260 cmdline_usage(ctx->cmdline, "vardir append");
261 return EINVAL;
264 p = path_vardir_append(mem_ctx, argv[0]);
265 if (p == NULL) {
266 D_ERR("Memory allocation error\n");
267 return 1;
270 printf("%s\n", p);
272 return 0;
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 },
298 CMDLINE_TABLEEND
301 int path_tool_init(TALLOC_CTX *mem_ctx,
302 const char *prog,
303 struct poptOption *options,
304 int argc,
305 const char **argv,
306 bool parse_options,
307 struct path_tool_context **result)
309 struct path_tool_context *ctx;
310 int ret;
312 ctx = talloc_zero(mem_ctx, struct path_tool_context);
313 if (ctx == NULL) {
314 D_ERR("Memory allocation error\n");
315 return ENOMEM;
318 ret = cmdline_init(ctx,
319 prog,
320 options,
321 NULL,
322 path_commands,
323 &ctx->cmdline);
324 if (ret != 0) {
325 D_ERR("Failed to initialize cmdline, ret=%d\n", ret);
326 talloc_free(ctx);
327 return ret;
330 ret = cmdline_parse(ctx->cmdline, argc, argv, parse_options);
331 if (ret != 0) {
332 cmdline_usage(ctx->cmdline, NULL);
333 talloc_free(ctx);
334 return ret;
337 *result = ctx;
338 return 0;
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)
350 TALLOC_CTX *mem_ctx;
351 struct path_tool_context *ctx;
352 int ret, result;
354 mem_ctx = talloc_new(NULL);
355 if (mem_ctx == NULL) {
356 fprintf(stderr, "Memory allocation error\n");
357 exit(1);
360 ret = path_tool_init(mem_ctx,
361 "ctdb-path",
362 NULL,
363 argc,
364 argv,
365 true,
366 &ctx);
367 if (ret != 0) {
368 talloc_free(mem_ctx);
369 exit(1);
372 setup_logging("ctdb-path", DEBUG_STDERR);
373 debuglevel_set(DEBUG_ERR);
375 ret = path_tool_run(ctx, &result);
376 if (ret != 0) {
377 result = 1;
380 talloc_free(mem_ctx);
381 exit(result);
384 #endif /* CTDB_PATH_TOOL */