ubus: Fix issues reported by static code analysis tool Klocwork
[project/ubus.git] / cli.c
1 /*
2  * Copyright (C) 2011 Felix Fietkau <nbd@openwrt.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License version 2.1
6  * as published by the Free Software Foundation
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13
14 #include <unistd.h>
15
16 #include <libubox/blobmsg_json.h>
17 #include "libubus.h"
18
19 static struct blob_buf b;
20 static int timeout = 30;
21 static bool simple_output = false;
22 static int verbose = 0;
23
24 static const char *format_type(void *priv, struct blob_attr *attr)
25 {
26         static const char * const attr_types[] = {
27                 [BLOBMSG_TYPE_INT8] = "\"Boolean\"",
28                 [BLOBMSG_TYPE_INT32] = "\"Integer\"",
29                 [BLOBMSG_TYPE_STRING] = "\"String\"",
30                 [BLOBMSG_TYPE_ARRAY] = "\"Array\"",
31                 [BLOBMSG_TYPE_TABLE] = "\"Table\"",
32         };
33         const char *type = NULL;
34         int typeid;
35
36         if (blob_id(attr) != BLOBMSG_TYPE_INT32)
37                 return NULL;
38
39         typeid = blobmsg_get_u32(attr);
40         if (typeid < ARRAY_SIZE(attr_types))
41                 type = attr_types[typeid];
42         if (!type)
43                 type = "\"(unknown)\"";
44
45         return type;
46 }
47
48 static void receive_list_result(struct ubus_context *ctx, struct ubus_object_data *obj, void *priv)
49 {
50         struct blob_attr *cur;
51         char *s;
52         int rem;
53
54         if (simple_output || !verbose) {
55                 printf("%s\n", obj->path);
56                 return;
57         }
58
59         printf("'%s' @%08x\n", obj->path, obj->id);
60
61         if (!obj->signature)
62                 return;
63
64         blob_for_each_attr(cur, obj->signature, rem) {
65                 s = blobmsg_format_json_with_cb(cur, false, format_type, NULL, -1);
66                 printf("\t%s\n", s);
67                 free(s);
68         }
69 }
70
71 static void receive_call_result_data(struct ubus_request *req, int type, struct blob_attr *msg)
72 {
73         char *str;
74         if (!msg)
75                 return;
76
77         str = blobmsg_format_json_indent(msg, true, simple_output ? -1 : 0);
78         printf("%s\n", str);
79         free(str);
80 }
81
82 static void receive_event(struct ubus_context *ctx, struct ubus_event_handler *ev,
83                           const char *type, struct blob_attr *msg)
84 {
85         char *str;
86
87         str = blobmsg_format_json(msg, true);
88         printf("{ \"%s\": %s }\n", type, str);
89         free(str);
90 }
91
92 static int ubus_cli_list(struct ubus_context *ctx, int argc, char **argv)
93 {
94         const char *path = NULL;
95
96         if (argc > 1)
97                 return -2;
98
99         if (argc == 1)
100                 path = argv[0];
101
102         return ubus_lookup(ctx, path, receive_list_result, NULL);
103 }
104
105 static int ubus_cli_call(struct ubus_context *ctx, int argc, char **argv)
106 {
107         uint32_t id;
108         int ret;
109
110         if (argc < 2 || argc > 3)
111                 return -2;
112
113         blob_buf_init(&b, 0);
114         if (argc == 3 && !blobmsg_add_json_from_string(&b, argv[2])) {
115                 if (!simple_output)
116                         fprintf(stderr, "Failed to parse message data\n");
117                 return -1;
118         }
119
120         ret = ubus_lookup_id(ctx, argv[0], &id);
121         if (ret)
122                 return ret;
123
124         return ubus_invoke(ctx, id, argv[1], b.head, receive_call_result_data, NULL, timeout * 1000);
125 }
126
127 static int ubus_cli_listen(struct ubus_context *ctx, int argc, char **argv)
128 {
129         static struct ubus_event_handler listener;
130         const char *event;
131         int ret = 0;
132
133         memset(&listener, 0, sizeof(listener));
134         listener.cb = receive_event;
135
136         if (argc > 0) {
137                 event = argv[0];
138         } else {
139                 event = "*";
140                 argc = 1;
141         }
142
143         do {
144                 ret = ubus_register_event_handler(ctx, &listener, event);
145                 if (ret)
146                         break;
147
148                 argv++;
149                 argc--;
150                 if (argc <= 0)
151                         break;
152
153                 event = argv[0];
154         } while (1);
155
156         if (ret) {
157                 if (!simple_output)
158                         fprintf(stderr, "Error while registering for event '%s': %s\n",
159                                 event, ubus_strerror(ret));
160                 return -1;
161         }
162
163         uloop_init();
164         ubus_add_uloop(ctx);
165         uloop_run();
166         uloop_done();
167
168         return 0;
169 }
170
171 static int ubus_cli_send(struct ubus_context *ctx, int argc, char **argv)
172 {
173         if (argc < 1 || argc > 2)
174                 return -2;
175
176         blob_buf_init(&b, 0);
177
178         if (argc == 2 && !blobmsg_add_json_from_string(&b, argv[1])) {
179                 if (!simple_output)
180                         fprintf(stderr, "Failed to parse message data\n");
181                 return -1;
182         }
183
184         return ubus_send_event(ctx, argv[0], b.head);
185 }
186
187 struct cli_wait_data {
188         struct uloop_timeout timeout;
189         struct ubus_event_handler ev;
190         char **pending;
191         int n_pending;
192 };
193
194 static void wait_check_object(struct cli_wait_data *data, const char *path)
195 {
196         int i;
197
198         for (i = 0; i < data->n_pending; i++) {
199                 if (strcmp(path, data->pending[i]) != 0)
200                         continue;
201
202                 data->n_pending--;
203                 if (i == data->n_pending)
204                         break;
205
206                 memmove(&data->pending[i], &data->pending[i + 1],
207                         (data->n_pending - i) * sizeof(*data->pending));
208                 i--;
209         }
210
211         if (!data->n_pending)
212                 uloop_end();
213 }
214
215 static void wait_event_cb(struct ubus_context *ctx, struct ubus_event_handler *ev,
216                           const char *type, struct blob_attr *msg)
217 {
218         static const struct blobmsg_policy policy = {
219                 "path", BLOBMSG_TYPE_STRING
220         };
221         struct cli_wait_data *data = container_of(ev, struct cli_wait_data, ev);
222         struct blob_attr *attr;
223         const char *path;
224
225         if (strcmp(type, "ubus.object.add") != 0)
226                 return;
227
228         blobmsg_parse(&policy, 1, &attr, blob_data(msg), blob_len(msg));
229         if (!attr)
230                 return;
231
232         path = blobmsg_data(attr);
233         wait_check_object(data, path);
234 }
235
236 static void wait_list_cb(struct ubus_context *ctx, struct ubus_object_data *obj, void *priv)
237 {
238         struct cli_wait_data *data = priv;
239
240         wait_check_object(data, obj->path);
241 }
242
243
244 static void wait_timeout(struct uloop_timeout *timeout)
245 {
246         uloop_end();
247 }
248
249 static int ubus_cli_wait_for(struct ubus_context *ctx, int argc, char **argv)
250 {
251         struct cli_wait_data data = {
252                 .timeout.cb = wait_timeout,
253                 .ev.cb = wait_event_cb,
254                 .pending = argv,
255                 .n_pending = argc,
256         };
257         int ret;
258
259         if (argc < 1)
260                 return -2;
261
262         uloop_init();
263         ubus_add_uloop(ctx);
264
265         ret = ubus_lookup(ctx, NULL, wait_list_cb, &data);
266         if (ret)
267                 return ret;
268
269         if (!data.n_pending)
270                 return ret;
271
272         ret = ubus_register_event_handler(ctx, &data.ev, "ubus.object.add");
273         if (ret)
274                 return ret;
275
276         uloop_timeout_set(&data.timeout, timeout * 1000);
277         uloop_run();
278         uloop_done();
279
280         if (data.n_pending)
281                 return UBUS_STATUS_TIMEOUT;
282
283         return ret;
284 }
285
286
287 static int usage(const char *prog)
288 {
289         fprintf(stderr,
290                 "Usage: %s [<options>] <command> [arguments...]\n"
291                 "Options:\n"
292                 " -s <socket>:          Set the unix domain socket to connect to\n"
293                 " -t <timeout>:         Set the timeout (in seconds) for a command to complete\n"
294                 " -S:                   Use simplified output (for scripts)\n"
295                 " -v:                   More verbose output\n"
296                 "\n"
297                 "Commands:\n"
298                 " - list [<path>]                       List objects\n"
299                 " - call <path> <method> [<message>]    Call an object method\n"
300                 " - listen [<path>...]                  Listen for events\n"
301                 " - send <type> [<message>]             Send an event\n"
302                 " - wait_for <object> [<object>...]     Wait for multiple objects to appear on ubus\n"
303                 "\n", prog);
304         return 1;
305 }
306
307
308 struct {
309         const char *name;
310         int (*cb)(struct ubus_context *ctx, int argc, char **argv);
311 } commands[] = {
312         { "list", ubus_cli_list },
313         { "call", ubus_cli_call },
314         { "listen", ubus_cli_listen },
315         { "send", ubus_cli_send },
316         { "wait_for", ubus_cli_wait_for },
317 };
318
319 int main(int argc, char **argv)
320 {
321         const char *progname, *ubus_socket = NULL;
322         static struct ubus_context *ctx;
323         char *cmd;
324         int ret = 0;
325         int i, ch;
326
327         progname = argv[0];
328
329         while ((ch = getopt(argc, argv, "vs:t:S")) != -1) {
330                 switch (ch) {
331                 case 's':
332                         ubus_socket = optarg;
333                         break;
334                 case 't':
335                         timeout = atoi(optarg);
336                         break;
337                 case 'S':
338                         simple_output = true;
339                         break;
340                 case 'v':
341                         verbose++;
342                         break;
343                 default:
344                         return usage(progname);
345                 }
346         }
347
348         argc -= optind;
349         argv += optind;
350
351         cmd = argv[0];
352         if (argc < 1)
353                 return usage(progname);
354
355         ctx = ubus_connect(ubus_socket);
356         if (!ctx) {
357                 if (!simple_output)
358                         fprintf(stderr, "Failed to connect to ubus\n");
359                 return -1;
360         }
361
362         argv++;
363         argc--;
364
365         ret = -2;
366         for (i = 0; i < ARRAY_SIZE(commands); i++) {
367                 if (strcmp(commands[i].name, cmd) != 0)
368                         continue;
369
370                 ret = commands[i].cb(ctx, argc, argv);
371                 break;
372         }
373
374         if (ret > 0 && !simple_output)
375                 fprintf(stderr, "Command failed: %s\n", ubus_strerror(ret));
376         else if (ret == -2)
377                 usage(progname);
378
379         ubus_free(ctx);
380         return ret;
381 }