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