ctdb-common: Add path tool
[metze/samba/wip.git] / ctdb / common / path_tool.c
1 /*
2    path tool
3
4    Copyright (C) Amitay Isaacs  2018
5
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.
10
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.
15
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/>.
18 */
19
20 #include "replace.h"
21
22 #include <talloc.h>
23
24 #include "lib/util/debug.h"
25
26 #include "common/logging.h"
27 #include "common/cmdline.h"
28 #include "common/path.h"
29 #include "common/path_tool.h"
30
31 struct path_tool_context {
32         struct cmdline_context *cmdline;
33 };
34
35 static int path_tool_config(TALLOC_CTX *mem_ctx,
36                             int argc,
37                             const char **argv,
38                             void *private_data)
39 {
40         struct path_tool_context *ctx = talloc_get_type_abort(
41                 private_data, struct path_tool_context);
42
43         if (argc != 0) {
44                 cmdline_usage(ctx->cmdline, "config");
45                 return EINVAL;
46         }
47
48         printf("%s\n", path_config(mem_ctx));
49
50         return 0;
51 }
52
53 static int path_tool_pidfile(TALLOC_CTX *mem_ctx,
54                              int argc,
55                              const char **argv,
56                              void *private_data)
57 {
58         struct path_tool_context *ctx = talloc_get_type_abort(
59                 private_data, struct path_tool_context);
60         char *p;
61
62         if (argc != 1) {
63                 cmdline_usage(ctx->cmdline, "pidfile");
64                 return EINVAL;
65         }
66
67         p = path_pidfile(mem_ctx, argv[0]);
68         if (p == NULL) {
69                 D_ERR("Memory allocation error\n");
70                 return 1;
71         }
72
73         printf("%s\n", p);
74
75         return 0;
76 }
77
78 static int path_tool_socket(TALLOC_CTX *mem_ctx,
79                              int argc,
80                              const char **argv,
81                              void *private_data)
82 {
83         struct path_tool_context *ctx = talloc_get_type_abort(
84                 private_data, struct path_tool_context);
85         char *p;
86
87         if (argc != 1) {
88                 cmdline_usage(ctx->cmdline, "socket");
89                 return EINVAL;
90         }
91
92         p = path_socket(mem_ctx, argv[0]);
93         if (p == NULL) {
94                 D_ERR("Memory allocation error\n");
95                 return 1;
96         }
97
98         printf("%s\n", p);
99
100         return 0;
101 }
102
103 static int path_tool_etcdir(TALLOC_CTX *mem_ctx,
104                             int argc,
105                             const char **argv,
106                             void *private_data)
107 {
108         struct path_tool_context *ctx = talloc_get_type_abort(
109                 private_data, struct path_tool_context);
110
111         if (argc != 0) {
112                 cmdline_usage(ctx->cmdline, "etcdir");
113                 return EINVAL;
114         }
115
116         printf("%s\n", path_etcdir());
117
118         return 0;
119 }
120
121 static int path_tool_etcdir_append(TALLOC_CTX *mem_ctx,
122                                    int argc,
123                                    const char **argv,
124                                    void *private_data)
125 {
126         struct path_tool_context *ctx = talloc_get_type_abort(
127                 private_data, struct path_tool_context);
128         char *p;
129
130         if (argc != 1) {
131                 cmdline_usage(ctx->cmdline, "etcdir append");
132                 return EINVAL;
133         }
134
135         p = path_etcdir_append(mem_ctx, argv[0]);
136         if (p == NULL) {
137                 D_ERR("Memory allocation error\n");
138                 return 1;
139         }
140
141         printf("%s\n", p);
142
143         return 0;
144 }
145
146 static int path_tool_rundir(TALLOC_CTX *mem_ctx,
147                             int argc,
148                             const char **argv,
149                             void *private_data)
150 {
151         struct path_tool_context *ctx = talloc_get_type_abort(
152                 private_data, struct path_tool_context);
153
154         if (argc != 0) {
155                 cmdline_usage(ctx->cmdline, "rundir");
156                 return EINVAL;
157         }
158
159         printf("%s\n", path_rundir());
160
161         return 0;
162 }
163
164 static int path_tool_rundir_append(TALLOC_CTX *mem_ctx,
165                                    int argc,
166                                    const char **argv,
167                                    void *private_data)
168 {
169         struct path_tool_context *ctx = talloc_get_type_abort(
170                 private_data, struct path_tool_context);
171         char *p;
172
173         if (argc != 1) {
174                 cmdline_usage(ctx->cmdline, "rundir append");
175                 return EINVAL;
176         }
177
178         p = path_rundir_append(mem_ctx, argv[0]);
179         if (p == NULL) {
180                 D_ERR("Memory allocation error\n");
181                 return 1;
182         }
183
184         printf("%s\n", p);
185
186         return 0;
187 }
188
189 static int path_tool_vardir(TALLOC_CTX *mem_ctx,
190                             int argc,
191                             const char **argv,
192                             void *private_data)
193 {
194         struct path_tool_context *ctx = talloc_get_type_abort(
195                 private_data, struct path_tool_context);
196
197         if (argc != 0) {
198                 cmdline_usage(ctx->cmdline, "vardir");
199                 return EINVAL;
200         }
201
202         printf("%s\n", path_vardir());
203
204         return 0;
205 }
206
207 static int path_tool_vardir_append(TALLOC_CTX *mem_ctx,
208                                    int argc,
209                                    const char **argv,
210                                    void *private_data)
211 {
212         struct path_tool_context *ctx = talloc_get_type_abort(
213                 private_data, struct path_tool_context);
214         char *p;
215
216         if (argc != 1) {
217                 cmdline_usage(ctx->cmdline, "vardir append");
218                 return EINVAL;
219         }
220
221         p = path_vardir_append(mem_ctx, argv[0]);
222         if (p == NULL) {
223                 D_ERR("Memory allocation error\n");
224                 return 1;
225         }
226
227         printf("%s\n", p);
228
229         return 0;
230 }
231
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 },
251         CMDLINE_TABLEEND
252 };
253
254 int path_tool_init(TALLOC_CTX *mem_ctx,
255                    const char *prog,
256                    struct poptOption *options,
257                    int argc,
258                    const char **argv,
259                    bool parse_options,
260                    struct path_tool_context **result)
261 {
262         struct path_tool_context *ctx;
263         int ret;
264
265         ctx = talloc_zero(mem_ctx, struct path_tool_context);
266         if (ctx == NULL) {
267                 D_ERR("Memory allocation error\n");
268                 return ENOMEM;
269         }
270
271         ret = cmdline_init(ctx, prog, options, path_commands, &ctx->cmdline);
272         if (ret != 0) {
273                 D_ERR("Failed to initialize cmdline, ret=%d\n", ret);
274                 talloc_free(ctx);
275                 return ret;
276         }
277
278         ret = cmdline_parse(ctx->cmdline, argc, argv, parse_options);
279         if (ret != 0) {
280                 cmdline_usage(ctx->cmdline, NULL);
281                 talloc_free(ctx);
282                 return ret;
283         }
284
285         *result = ctx;
286         return 0;
287 }
288
289 int path_tool_run(struct path_tool_context *ctx, int *result)
290 {
291         return cmdline_run(ctx->cmdline, ctx, result);
292 }
293
294 #ifdef CTDB_PATH_TOOL
295
296 int main(int argc, const char **argv)
297 {
298         TALLOC_CTX *mem_ctx;
299         struct path_tool_context *ctx;
300         int ret, result;
301
302         mem_ctx = talloc_new(NULL);
303         if (mem_ctx == NULL) {
304                 fprintf(stderr, "Memory allocation error\n");
305                 exit(1);
306         }
307
308         ret = path_tool_init(mem_ctx,
309                              "ctdb-path",
310                              NULL,
311                              argc,
312                              argv,
313                              true,
314                              &ctx);
315         if (ret != 0) {
316                 talloc_free(mem_ctx);
317                 exit(1);
318         }
319
320         setup_logging("ctdb-path", DEBUG_STDERR);
321         DEBUGLEVEL = DEBUG_ERR;
322
323         ret = path_tool_run(ctx, &result);
324         if (ret != 0) {
325                 result = 1;
326         }
327
328         talloc_free(mem_ctx);
329         exit(result);
330 }
331
332 #endif /* CTDB_PATH_TOOL */