cli: use UBUS_STATUS_PARSE_ERROR
[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 listen_timeout;
21 static int timeout = 30;
22 static bool simple_output = false;
23 static int verbose = 0;
24 static int monitor_dir = -1;
25 static uint32_t monitor_mask;
26 static const char * const monitor_types[] = {
27 [UBUS_MSG_HELLO] = "hello",
28 [UBUS_MSG_STATUS] = "status",
29 [UBUS_MSG_DATA] = "data",
30 [UBUS_MSG_PING] = "ping",
31 [UBUS_MSG_LOOKUP] = "lookup",
32 [UBUS_MSG_INVOKE] = "invoke",
33 [UBUS_MSG_ADD_OBJECT] = "add_object",
34 [UBUS_MSG_REMOVE_OBJECT] = "remove_object",
35 [UBUS_MSG_SUBSCRIBE] = "subscribe",
36 [UBUS_MSG_UNSUBSCRIBE] = "unsubscribe",
37 [UBUS_MSG_NOTIFY] = "notify",
38 };
39
40 static const char *format_type(void *priv, struct blob_attr *attr)
41 {
42 static const char * const attr_types[] = {
43 [BLOBMSG_TYPE_INT8] = "\"Boolean\"",
44 [BLOBMSG_TYPE_INT32] = "\"Integer\"",
45 [BLOBMSG_TYPE_STRING] = "\"String\"",
46 [BLOBMSG_TYPE_ARRAY] = "\"Array\"",
47 [BLOBMSG_TYPE_TABLE] = "\"Table\"",
48 };
49 const char *type = NULL;
50 size_t typeid;
51
52 if (blob_id(attr) != BLOBMSG_TYPE_INT32)
53 return NULL;
54
55 typeid = blobmsg_get_u32(attr);
56 if (typeid < ARRAY_SIZE(attr_types))
57 type = attr_types[typeid];
58 if (!type)
59 type = "\"(unknown)\"";
60
61 return type;
62 }
63
64 static void receive_list_result(struct ubus_context *ctx, struct ubus_object_data *obj, void *priv)
65 {
66 struct blob_attr *cur;
67 char *s;
68 size_t rem;
69
70 if (simple_output || !verbose) {
71 printf("%s\n", obj->path);
72 return;
73 }
74
75 printf("'%s' @%08x\n", obj->path, obj->id);
76
77 if (!obj->signature)
78 return;
79
80 blob_for_each_attr(cur, obj->signature, rem) {
81 s = blobmsg_format_json_with_cb(cur, false, format_type, NULL, -1);
82 printf("\t%s\n", s);
83 free(s);
84 }
85 }
86
87 static void receive_call_result_data(struct ubus_request *req, int type, struct blob_attr *msg)
88 {
89 char *str;
90 if (!msg)
91 return;
92
93 str = blobmsg_format_json_indent(msg, true, simple_output ? -1 : 0);
94 printf("%s\n", str);
95 free(str);
96 }
97
98 static void print_event(const char *type, struct blob_attr *msg)
99 {
100 char *str;
101
102 str = blobmsg_format_json(msg, true);
103 printf("{ \"%s\": %s }\n", type, str);
104 fflush(stdout);
105 free(str);
106 }
107
108 static int receive_request(struct ubus_context *ctx, struct ubus_object *obj,
109 struct ubus_request_data *req,
110 const char *method, struct blob_attr *msg)
111 {
112 print_event(method, msg);
113 return 0;
114 }
115
116 static void receive_event(struct ubus_context *ctx, struct ubus_event_handler *ev,
117 const char *type, struct blob_attr *msg)
118 {
119 print_event(type, msg);
120 }
121
122 static int ubus_cli_list(struct ubus_context *ctx, int argc, char **argv)
123 {
124 const char *path = NULL;
125
126 if (argc > 1)
127 return -2;
128
129 if (argc == 1)
130 path = argv[0];
131
132 return ubus_lookup(ctx, path, receive_list_result, NULL);
133 }
134
135 static int ubus_cli_call(struct ubus_context *ctx, int argc, char **argv)
136 {
137 uint32_t id;
138 int ret;
139
140 if (argc < 2 || argc > 3)
141 return -2;
142
143 blob_buf_init(&b, 0);
144 if (argc == 3 && !blobmsg_add_json_from_string(&b, argv[2])) {
145 return UBUS_STATUS_PARSE_ERROR;
146 }
147
148 ret = ubus_lookup_id(ctx, argv[0], &id);
149 if (ret)
150 return ret;
151
152 return ubus_invoke(ctx, id, argv[1], b.head, receive_call_result_data, NULL, timeout * 1000);
153 }
154
155 struct cli_listen_data {
156 struct uloop_timeout timeout;
157 bool timed_out;
158 };
159
160 static void ubus_cli_listen_timeout(struct uloop_timeout *timeout)
161 {
162 struct cli_listen_data *data = container_of(timeout, struct cli_listen_data, timeout);
163 data->timed_out = true;
164 uloop_end();
165 }
166
167 static void do_listen(struct ubus_context *ctx, struct cli_listen_data *data)
168 {
169 memset(data, 0, sizeof(*data));
170 data->timeout.cb = ubus_cli_listen_timeout;
171 uloop_init();
172 ubus_add_uloop(ctx);
173 if (listen_timeout)
174 uloop_timeout_set(&data->timeout, listen_timeout * 1000);
175 uloop_run();
176 uloop_done();
177 }
178
179 static int ubus_cli_listen(struct ubus_context *ctx, int argc, char **argv)
180 {
181 struct ubus_event_handler ev = {
182 .cb = receive_event,
183 };
184 struct cli_listen_data data;
185 const char *event;
186 int ret = 0;
187
188 if (argc > 0) {
189 event = argv[0];
190 } else {
191 event = "*";
192 argc = 1;
193 }
194
195 do {
196 ret = ubus_register_event_handler(ctx, &ev, event);
197 if (ret)
198 break;
199
200 argv++;
201 argc--;
202 if (argc <= 0)
203 break;
204
205 event = argv[0];
206 } while (1);
207
208 if (ret) {
209 if (!simple_output)
210 fprintf(stderr, "Error while registering for event '%s': %s\n",
211 event, ubus_strerror(ret));
212 return -1;
213 }
214
215 do_listen(ctx, &data);
216
217 return 0;
218 }
219
220 static int ubus_cli_subscribe(struct ubus_context *ctx, int argc, char **argv)
221 {
222 struct ubus_subscriber sub = {
223 .cb = receive_request,
224 };
225 struct cli_listen_data data;
226 const char *event;
227 int ret = 0;
228
229 if (argc > 0) {
230 event = argv[0];
231 } else {
232 if (!simple_output)
233 fprintf(stderr, "You need to specify an object to subscribe to\n");
234 return -1;
235 }
236
237 ret = ubus_register_subscriber(ctx, &sub);
238 for (; !ret && argc > 0; argc--, argv++) {
239 uint32_t id;
240
241 ret = ubus_lookup_id(ctx, argv[0], &id);
242 if (ret)
243 break;
244
245 ret = ubus_subscribe(ctx, &sub, id);
246 }
247
248 if (ret) {
249 if (!simple_output)
250 fprintf(stderr, "Error while registering for event '%s': %s\n",
251 event, ubus_strerror(ret));
252 return -1;
253 }
254
255 do_listen(ctx, &data);
256
257 return 0;
258 }
259
260
261 static int ubus_cli_send(struct ubus_context *ctx, int argc, char **argv)
262 {
263 if (argc < 1 || argc > 2)
264 return -2;
265
266 blob_buf_init(&b, 0);
267
268 if (argc == 2 && !blobmsg_add_json_from_string(&b, argv[1])) {
269 return UBUS_STATUS_PARSE_ERROR;
270 }
271
272 return ubus_send_event(ctx, argv[0], b.head);
273 }
274
275 struct cli_wait_data {
276 struct uloop_timeout timeout;
277 struct ubus_event_handler ev;
278 char **pending;
279 int n_pending;
280 };
281
282 static void wait_check_object(struct cli_wait_data *data, const char *path)
283 {
284 int i;
285
286 for (i = 0; i < data->n_pending; i++) {
287 if (strcmp(path, data->pending[i]) != 0)
288 continue;
289
290 data->n_pending--;
291 if (i == data->n_pending)
292 break;
293
294 memmove(&data->pending[i], &data->pending[i + 1],
295 (data->n_pending - i) * sizeof(*data->pending));
296 i--;
297 }
298
299 if (!data->n_pending)
300 uloop_end();
301 }
302
303 static void wait_event_cb(struct ubus_context *ctx, struct ubus_event_handler *ev,
304 const char *type, struct blob_attr *msg)
305 {
306 static const struct blobmsg_policy policy = {
307 "path", BLOBMSG_TYPE_STRING
308 };
309 struct cli_wait_data *data = container_of(ev, struct cli_wait_data, ev);
310 struct blob_attr *attr;
311 const char *path;
312
313 if (strcmp(type, "ubus.object.add") != 0)
314 return;
315
316 blobmsg_parse(&policy, 1, &attr, blob_data(msg), blob_len(msg));
317 if (!attr)
318 return;
319
320 path = blobmsg_data(attr);
321 wait_check_object(data, path);
322 }
323
324 static void wait_list_cb(struct ubus_context *ctx, struct ubus_object_data *obj, void *priv)
325 {
326 struct cli_wait_data *data = priv;
327
328 wait_check_object(data, obj->path);
329 }
330
331
332 static void wait_timeout(struct uloop_timeout *timeout)
333 {
334 uloop_end();
335 }
336
337 static int ubus_cli_wait_for(struct ubus_context *ctx, int argc, char **argv)
338 {
339 struct cli_wait_data data = {
340 .timeout.cb = wait_timeout,
341 .ev.cb = wait_event_cb,
342 .pending = argv,
343 .n_pending = argc,
344 };
345 int ret;
346
347 if (argc < 1)
348 return -2;
349
350 uloop_init();
351 ubus_add_uloop(ctx);
352
353 ret = ubus_register_event_handler(ctx, &data.ev, "ubus.object.add");
354 if (ret)
355 return ret;
356
357 if (!data.n_pending)
358 return ret;
359
360 ret = ubus_lookup(ctx, NULL, wait_list_cb, &data);
361 if (ret)
362 return ret;
363
364 if (!data.n_pending)
365 return ret;
366
367 uloop_timeout_set(&data.timeout, timeout * 1000);
368 uloop_run();
369 uloop_done();
370
371 if (data.n_pending)
372 return UBUS_STATUS_TIMEOUT;
373
374 return ret;
375 }
376
377 static const char *
378 ubus_cli_msg_type(uint32_t type)
379 {
380 const char *ret = NULL;
381 static char unk_type[16];
382
383
384 if (type < ARRAY_SIZE(monitor_types))
385 ret = monitor_types[type];
386
387 if (!ret) {
388 snprintf(unk_type, sizeof(unk_type), "%d", type);
389 ret = unk_type;
390 }
391
392 return ret;
393 }
394
395 static char *
396 ubus_cli_get_monitor_data(struct blob_attr *data)
397 {
398 static const struct blob_attr_info policy[UBUS_ATTR_MAX] = {
399 [UBUS_ATTR_STATUS] = { .type = BLOB_ATTR_INT32 },
400 [UBUS_ATTR_OBJPATH] = { .type = BLOB_ATTR_STRING },
401 [UBUS_ATTR_OBJID] = { .type = BLOB_ATTR_INT32 },
402 [UBUS_ATTR_METHOD] = { .type = BLOB_ATTR_STRING },
403 [UBUS_ATTR_OBJTYPE] = { .type = BLOB_ATTR_INT32 },
404 [UBUS_ATTR_SIGNATURE] = { .type = BLOB_ATTR_NESTED },
405 [UBUS_ATTR_DATA] = { .type = BLOB_ATTR_NESTED },
406 [UBUS_ATTR_ACTIVE] = { .type = BLOB_ATTR_INT8 },
407 [UBUS_ATTR_NO_REPLY] = { .type = BLOB_ATTR_INT8 },
408 [UBUS_ATTR_USER] = { .type = BLOB_ATTR_STRING },
409 [UBUS_ATTR_GROUP] = { .type = BLOB_ATTR_STRING },
410 };
411 static const char * const names[UBUS_ATTR_MAX] = {
412 [UBUS_ATTR_STATUS] = "status",
413 [UBUS_ATTR_OBJPATH] = "objpath",
414 [UBUS_ATTR_OBJID] = "objid",
415 [UBUS_ATTR_METHOD] = "method",
416 [UBUS_ATTR_OBJTYPE] = "objtype",
417 [UBUS_ATTR_SIGNATURE] = "signature",
418 [UBUS_ATTR_DATA] = "data",
419 [UBUS_ATTR_ACTIVE] = "active",
420 [UBUS_ATTR_NO_REPLY] = "no_reply",
421 [UBUS_ATTR_USER] = "user",
422 [UBUS_ATTR_GROUP] = "group",
423 };
424 struct blob_attr *tb[UBUS_ATTR_MAX];
425 int i;
426
427 blob_buf_init(&b, 0);
428 blob_parse(data, tb, policy, UBUS_ATTR_MAX);
429
430 for (i = 0; i < UBUS_ATTR_MAX; i++) {
431 const char *n = names[i];
432 struct blob_attr *v = tb[i];
433
434 if (!tb[i] || !n)
435 continue;
436
437 switch(policy[i].type) {
438 case BLOB_ATTR_INT32:
439 blobmsg_add_u32(&b, n, blob_get_int32(v));
440 break;
441 case BLOB_ATTR_STRING:
442 blobmsg_add_string(&b, n, blob_data(v));
443 break;
444 case BLOB_ATTR_INT8:
445 blobmsg_add_u8(&b, n, !!blob_get_int8(v));
446 break;
447 case BLOB_ATTR_NESTED:
448 blobmsg_add_field(&b, BLOBMSG_TYPE_TABLE, n, blobmsg_data(v), blobmsg_data_len(v));
449 break;
450 }
451 }
452
453 return blobmsg_format_json(b.head, true);
454 }
455
456 static void
457 ubus_cli_monitor_cb(struct ubus_context *ctx, uint32_t seq, struct blob_attr *msg)
458 {
459 static const struct blob_attr_info policy[UBUS_MONITOR_MAX] = {
460 [UBUS_MONITOR_CLIENT] = { .type = BLOB_ATTR_INT32 },
461 [UBUS_MONITOR_PEER] = { .type = BLOB_ATTR_INT32 },
462 [UBUS_MONITOR_SEND] = { .type = BLOB_ATTR_INT8 },
463 [UBUS_MONITOR_TYPE] = { .type = BLOB_ATTR_INT32 },
464 [UBUS_MONITOR_DATA] = { .type = BLOB_ATTR_NESTED },
465 };
466 struct blob_attr *tb[UBUS_MONITOR_MAX];
467 uint32_t client, peer, type;
468 bool send;
469 char *data;
470
471 blob_parse_untrusted(msg, blob_raw_len(msg), tb, policy, UBUS_MONITOR_MAX);
472
473 if (!tb[UBUS_MONITOR_CLIENT] ||
474 !tb[UBUS_MONITOR_PEER] ||
475 !tb[UBUS_MONITOR_SEND] ||
476 !tb[UBUS_MONITOR_TYPE] ||
477 !tb[UBUS_MONITOR_DATA]) {
478 printf("Invalid monitor msg\n");
479 return;
480 }
481
482 send = blob_get_int32(tb[UBUS_MONITOR_SEND]);
483 client = blob_get_int32(tb[UBUS_MONITOR_CLIENT]);
484 peer = blob_get_int32(tb[UBUS_MONITOR_PEER]);
485 type = blob_get_int32(tb[UBUS_MONITOR_TYPE]);
486
487 if (monitor_mask && type < 32 && !(monitor_mask & (1 << type)))
488 return;
489
490 if (monitor_dir >= 0 && send != monitor_dir)
491 return;
492
493 data = ubus_cli_get_monitor_data(tb[UBUS_MONITOR_DATA]);
494 printf("%s %08x #%08x %14s: %s\n", send ? "->" : "<-", client, peer, ubus_cli_msg_type(type), data);
495 free(data);
496 fflush(stdout);
497 }
498
499 static int ubus_cli_monitor(struct ubus_context *ctx, int argc, char **argv)
500 {
501 int ret;
502
503 uloop_init();
504 ubus_add_uloop(ctx);
505 ctx->monitor_cb = ubus_cli_monitor_cb;
506 ret = ubus_monitor_start(ctx);
507 if (ret)
508 return ret;
509
510 uloop_run();
511 uloop_done();
512
513 ubus_monitor_stop(ctx);
514 return 0;
515 }
516
517 static int add_monitor_type(const char *type)
518 {
519 size_t i;
520
521 for (i = 0; i < ARRAY_SIZE(monitor_types); i++) {
522 if (!monitor_types[i] || strcmp(monitor_types[i], type) != 0)
523 continue;
524
525 monitor_mask |= 1 << i;
526 return 0;
527 }
528
529 return -1;
530 }
531
532 static int usage(const char *prog)
533 {
534 fprintf(stderr,
535 "Usage: %s [<options>] <command> [arguments...]\n"
536 "Options:\n"
537 " -s <socket>: Set the unix domain socket to connect to\n"
538 " -t <timeout>: Set the timeout (in seconds) for a command to complete\n"
539 " -S: Use simplified output (for scripts)\n"
540 " -v: More verbose output\n"
541 " -m <type>: (for monitor): include a specific message type\n"
542 " (can be used more than once)\n"
543 " -M <r|t> (for monitor): only capture received or transmitted traffic\n"
544 "\n"
545 "Commands:\n"
546 " - list [<path>] List objects\n"
547 " - call <path> <method> [<message>] Call an object method\n"
548 " - subscribe <path> [<path>...] Subscribe to object(s) notifications\n"
549 " - listen [<path>...] Listen for events\n"
550 " - send <type> [<message>] Send an event\n"
551 " - wait_for <object> [<object>...] Wait for multiple objects to appear on ubus\n"
552 " - monitor Monitor ubus traffic\n"
553 "\n", prog);
554 return 1;
555 }
556
557
558 static struct {
559 const char *name;
560 int (*cb)(struct ubus_context *ctx, int argc, char **argv);
561 } commands[] = {
562 { "list", ubus_cli_list },
563 { "call", ubus_cli_call },
564 { "listen", ubus_cli_listen },
565 { "subscribe", ubus_cli_subscribe },
566 { "send", ubus_cli_send },
567 { "wait_for", ubus_cli_wait_for },
568 { "monitor", ubus_cli_monitor },
569 };
570
571 int main(int argc, char **argv)
572 {
573 const char *progname, *ubus_socket = NULL;
574 struct ubus_context *ctx;
575 int ret = 0;
576 char *cmd;
577 size_t i;
578 int ch;
579
580 progname = argv[0];
581
582 while ((ch = getopt(argc, argv, "m:M:vs:t:S")) != -1) {
583 switch (ch) {
584 case 's':
585 ubus_socket = optarg;
586 break;
587 case 't':
588 listen_timeout = atoi(optarg);
589 timeout = atoi(optarg);
590 break;
591 case 'S':
592 simple_output = true;
593 break;
594 case 'v':
595 verbose++;
596 break;
597 case 'm':
598 if (add_monitor_type(optarg))
599 return usage(progname);
600 break;
601 case 'M':
602 switch (optarg[0]) {
603 case 'r':
604 monitor_dir = 0;
605 break;
606 case 't':
607 monitor_dir = 1;
608 break;
609 default:
610 return usage(progname);
611 }
612 break;
613 default:
614 return usage(progname);
615 }
616 }
617
618 argc -= optind;
619 argv += optind;
620
621 cmd = argv[0];
622 if (argc < 1)
623 return usage(progname);
624
625 ctx = ubus_connect(ubus_socket);
626 if (!ctx) {
627 if (!simple_output)
628 fprintf(stderr, "Failed to connect to ubus\n");
629 return -1;
630 }
631
632 argv++;
633 argc--;
634
635 ret = -2;
636 for (i = 0; i < ARRAY_SIZE(commands); i++) {
637 if (strcmp(commands[i].name, cmd) != 0)
638 continue;
639
640 ret = commands[i].cb(ctx, argc, argv);
641 break;
642 }
643
644 if (ret > 0 && !simple_output)
645 fprintf(stderr, "Command failed: %s\n", ubus_strerror(ret));
646 else if (ret == -2)
647 usage(progname);
648
649 ubus_free(ctx);
650 return ret;
651 }