2 * Copyright (C) 2011 Felix Fietkau <nbd@openwrt.org>
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
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.
16 #include <libubox/blobmsg_json.h>
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",
40 static const char *format_type(void *priv
, struct blob_attr
*attr
)
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\"",
49 const char *type
= NULL
;
52 if (blob_id(attr
) != BLOBMSG_TYPE_INT32
)
55 typeid = blobmsg_get_u32(attr
);
56 if (typeid < ARRAY_SIZE(attr_types
))
57 type
= attr_types
[typeid];
59 type
= "\"(unknown)\"";
64 static void receive_list_result(struct ubus_context
*ctx
, struct ubus_object_data
*obj
, void *priv
)
66 struct blob_attr
*cur
;
70 if (simple_output
|| !verbose
) {
71 printf("%s\n", obj
->path
);
75 printf("'%s' @%08x\n", obj
->path
, obj
->id
);
80 blob_for_each_attr(cur
, obj
->signature
, rem
) {
81 s
= blobmsg_format_json_with_cb(cur
, false, format_type
, NULL
, -1);
87 static void receive_call_result_data(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
93 str
= blobmsg_format_json_indent(msg
, true, simple_output
? -1 : 0);
98 static void print_event(const char *type
, struct blob_attr
*msg
)
102 str
= blobmsg_format_json(msg
, true);
103 printf("{ \"%s\": %s }\n", type
, str
);
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
)
112 print_event(method
, msg
);
116 static void receive_event(struct ubus_context
*ctx
, struct ubus_event_handler
*ev
,
117 const char *type
, struct blob_attr
*msg
)
119 print_event(type
, msg
);
122 static int ubus_cli_error(char *cmd
, int argc
, char **argv
, int err
)
126 if (!simple_output
&& !isatty(fileno(stderr
))) {
127 fprintf(stderr
, "Command failed: ubus %s ", cmd
);
128 for (i
= 0; i
< argc
; i
++) {
129 fprintf(stderr
, "%s ", argv
[i
]);
131 fprintf(stderr
, "(%s)\n", ubus_strerror(err
));
139 static int ubus_cli_list(struct ubus_context
*ctx
, int argc
, char **argv
)
141 const char *path
= NULL
;
149 return ubus_lookup(ctx
, path
, receive_list_result
, NULL
);
152 static int ubus_cli_call(struct ubus_context
*ctx
, int argc
, char **argv
)
157 if (argc
< 2 || argc
> 3)
160 blob_buf_init(&b
, 0);
161 if (argc
== 3 && !blobmsg_add_json_from_string(&b
, argv
[2])) {
162 return ubus_cli_error("call", argc
, argv
, UBUS_STATUS_PARSE_ERROR
);
165 ret
= ubus_lookup_id(ctx
, argv
[0], &id
);
169 ret
= ubus_invoke(ctx
, id
, argv
[1], b
.head
, receive_call_result_data
, NULL
, timeout
* 1000);
171 return ubus_cli_error("call", argc
, argv
, ret
);
176 struct cli_listen_data
{
177 struct uloop_timeout timeout
;
181 static void ubus_cli_listen_timeout(struct uloop_timeout
*timeout
)
183 struct cli_listen_data
*data
= container_of(timeout
, struct cli_listen_data
, timeout
);
184 data
->timed_out
= true;
188 static void do_listen(struct ubus_context
*ctx
, struct cli_listen_data
*data
)
190 memset(data
, 0, sizeof(*data
));
191 data
->timeout
.cb
= ubus_cli_listen_timeout
;
195 uloop_timeout_set(&data
->timeout
, listen_timeout
* 1000);
200 static int ubus_cli_listen(struct ubus_context
*ctx
, int argc
, char **argv
)
202 struct ubus_event_handler ev
= {
205 struct cli_listen_data data
;
217 ret
= ubus_register_event_handler(ctx
, &ev
, event
);
231 fprintf(stderr
, "Error while registering for event '%s': %s\n",
232 event
, ubus_strerror(ret
));
236 do_listen(ctx
, &data
);
241 static int ubus_cli_subscribe(struct ubus_context
*ctx
, int argc
, char **argv
)
243 struct ubus_subscriber sub
= {
244 .cb
= receive_request
,
246 struct cli_listen_data data
;
254 fprintf(stderr
, "You need to specify an object to subscribe to\n");
258 ret
= ubus_register_subscriber(ctx
, &sub
);
259 for (; !ret
&& argc
> 0; argc
--, argv
++) {
262 ret
= ubus_lookup_id(ctx
, argv
[0], &id
);
266 ret
= ubus_subscribe(ctx
, &sub
, id
);
271 fprintf(stderr
, "Error while registering for event '%s': %s\n",
272 event
, ubus_strerror(ret
));
276 do_listen(ctx
, &data
);
282 static int ubus_cli_send(struct ubus_context
*ctx
, int argc
, char **argv
)
284 if (argc
< 1 || argc
> 2)
287 blob_buf_init(&b
, 0);
289 if (argc
== 2 && !blobmsg_add_json_from_string(&b
, argv
[1])) {
290 return UBUS_STATUS_PARSE_ERROR
;
293 return ubus_send_event(ctx
, argv
[0], b
.head
);
296 struct cli_wait_data
{
297 struct uloop_timeout timeout
;
298 struct ubus_event_handler ev
;
303 static void wait_check_object(struct cli_wait_data
*data
, const char *path
)
307 for (i
= 0; i
< data
->n_pending
; i
++) {
308 if (strcmp(path
, data
->pending
[i
]) != 0)
312 if (i
== data
->n_pending
)
315 memmove(&data
->pending
[i
], &data
->pending
[i
+ 1],
316 (data
->n_pending
- i
) * sizeof(*data
->pending
));
320 if (!data
->n_pending
)
324 static void wait_event_cb(struct ubus_context
*ctx
, struct ubus_event_handler
*ev
,
325 const char *type
, struct blob_attr
*msg
)
327 static const struct blobmsg_policy policy
= {
328 "path", BLOBMSG_TYPE_STRING
330 struct cli_wait_data
*data
= container_of(ev
, struct cli_wait_data
, ev
);
331 struct blob_attr
*attr
;
334 if (strcmp(type
, "ubus.object.add") != 0)
337 blobmsg_parse(&policy
, 1, &attr
, blob_data(msg
), blob_len(msg
));
341 path
= blobmsg_data(attr
);
342 wait_check_object(data
, path
);
345 static void wait_list_cb(struct ubus_context
*ctx
, struct ubus_object_data
*obj
, void *priv
)
347 struct cli_wait_data
*data
= priv
;
349 wait_check_object(data
, obj
->path
);
353 static void wait_timeout(struct uloop_timeout
*timeout
)
358 static int ubus_cli_wait_for(struct ubus_context
*ctx
, int argc
, char **argv
)
360 struct cli_wait_data data
= {
361 .timeout
.cb
= wait_timeout
,
362 .ev
.cb
= wait_event_cb
,
374 ret
= ubus_register_event_handler(ctx
, &data
.ev
, "ubus.object.add");
381 ret
= ubus_lookup(ctx
, NULL
, wait_list_cb
, &data
);
388 uloop_timeout_set(&data
.timeout
, timeout
* 1000);
393 return UBUS_STATUS_TIMEOUT
;
399 ubus_cli_msg_type(uint32_t type
)
401 const char *ret
= NULL
;
402 static char unk_type
[16];
405 if (type
< ARRAY_SIZE(monitor_types
))
406 ret
= monitor_types
[type
];
409 snprintf(unk_type
, sizeof(unk_type
), "%d", type
);
417 ubus_cli_get_monitor_data(struct blob_attr
*data
)
419 static const struct blob_attr_info policy
[UBUS_ATTR_MAX
] = {
420 [UBUS_ATTR_STATUS
] = { .type
= BLOB_ATTR_INT32
},
421 [UBUS_ATTR_OBJPATH
] = { .type
= BLOB_ATTR_STRING
},
422 [UBUS_ATTR_OBJID
] = { .type
= BLOB_ATTR_INT32
},
423 [UBUS_ATTR_METHOD
] = { .type
= BLOB_ATTR_STRING
},
424 [UBUS_ATTR_OBJTYPE
] = { .type
= BLOB_ATTR_INT32
},
425 [UBUS_ATTR_SIGNATURE
] = { .type
= BLOB_ATTR_NESTED
},
426 [UBUS_ATTR_DATA
] = { .type
= BLOB_ATTR_NESTED
},
427 [UBUS_ATTR_ACTIVE
] = { .type
= BLOB_ATTR_INT8
},
428 [UBUS_ATTR_NO_REPLY
] = { .type
= BLOB_ATTR_INT8
},
429 [UBUS_ATTR_USER
] = { .type
= BLOB_ATTR_STRING
},
430 [UBUS_ATTR_GROUP
] = { .type
= BLOB_ATTR_STRING
},
432 static const char * const names
[UBUS_ATTR_MAX
] = {
433 [UBUS_ATTR_STATUS
] = "status",
434 [UBUS_ATTR_OBJPATH
] = "objpath",
435 [UBUS_ATTR_OBJID
] = "objid",
436 [UBUS_ATTR_METHOD
] = "method",
437 [UBUS_ATTR_OBJTYPE
] = "objtype",
438 [UBUS_ATTR_SIGNATURE
] = "signature",
439 [UBUS_ATTR_DATA
] = "data",
440 [UBUS_ATTR_ACTIVE
] = "active",
441 [UBUS_ATTR_NO_REPLY
] = "no_reply",
442 [UBUS_ATTR_USER
] = "user",
443 [UBUS_ATTR_GROUP
] = "group",
445 struct blob_attr
*tb
[UBUS_ATTR_MAX
];
448 blob_buf_init(&b
, 0);
449 blob_parse(data
, tb
, policy
, UBUS_ATTR_MAX
);
451 for (i
= 0; i
< UBUS_ATTR_MAX
; i
++) {
452 const char *n
= names
[i
];
453 struct blob_attr
*v
= tb
[i
];
458 switch(policy
[i
].type
) {
459 case BLOB_ATTR_INT32
:
460 blobmsg_add_u32(&b
, n
, blob_get_int32(v
));
462 case BLOB_ATTR_STRING
:
463 blobmsg_add_string(&b
, n
, blob_data(v
));
466 blobmsg_add_u8(&b
, n
, !!blob_get_int8(v
));
468 case BLOB_ATTR_NESTED
:
469 blobmsg_add_field(&b
, BLOBMSG_TYPE_TABLE
, n
, blobmsg_data(v
), blobmsg_data_len(v
));
474 return blobmsg_format_json(b
.head
, true);
478 ubus_cli_monitor_cb(struct ubus_context
*ctx
, uint32_t seq
, struct blob_attr
*msg
)
480 static const struct blob_attr_info policy
[UBUS_MONITOR_MAX
] = {
481 [UBUS_MONITOR_CLIENT
] = { .type
= BLOB_ATTR_INT32
},
482 [UBUS_MONITOR_PEER
] = { .type
= BLOB_ATTR_INT32
},
483 [UBUS_MONITOR_SEND
] = { .type
= BLOB_ATTR_INT8
},
484 [UBUS_MONITOR_TYPE
] = { .type
= BLOB_ATTR_INT32
},
485 [UBUS_MONITOR_DATA
] = { .type
= BLOB_ATTR_NESTED
},
487 struct blob_attr
*tb
[UBUS_MONITOR_MAX
];
488 uint32_t client
, peer
, type
;
492 blob_parse_untrusted(msg
, blob_raw_len(msg
), tb
, policy
, UBUS_MONITOR_MAX
);
494 if (!tb
[UBUS_MONITOR_CLIENT
] ||
495 !tb
[UBUS_MONITOR_PEER
] ||
496 !tb
[UBUS_MONITOR_SEND
] ||
497 !tb
[UBUS_MONITOR_TYPE
] ||
498 !tb
[UBUS_MONITOR_DATA
]) {
499 printf("Invalid monitor msg\n");
503 send
= blob_get_int32(tb
[UBUS_MONITOR_SEND
]);
504 client
= blob_get_int32(tb
[UBUS_MONITOR_CLIENT
]);
505 peer
= blob_get_int32(tb
[UBUS_MONITOR_PEER
]);
506 type
= blob_get_int32(tb
[UBUS_MONITOR_TYPE
]);
508 if (monitor_mask
&& type
< 32 && !(monitor_mask
& (1 << type
)))
511 if (monitor_dir
>= 0 && send
!= monitor_dir
)
514 data
= ubus_cli_get_monitor_data(tb
[UBUS_MONITOR_DATA
]);
515 printf("%s %08x #%08x %14s: %s\n", send
? "->" : "<-", client
, peer
, ubus_cli_msg_type(type
), data
);
520 static int ubus_cli_monitor(struct ubus_context
*ctx
, int argc
, char **argv
)
526 ctx
->monitor_cb
= ubus_cli_monitor_cb
;
527 ret
= ubus_monitor_start(ctx
);
534 ubus_monitor_stop(ctx
);
538 static int add_monitor_type(const char *type
)
542 for (i
= 0; i
< ARRAY_SIZE(monitor_types
); i
++) {
543 if (!monitor_types
[i
] || strcmp(monitor_types
[i
], type
) != 0)
546 monitor_mask
|= 1 << i
;
553 static int usage(const char *prog
)
556 "Usage: %s [<options>] <command> [arguments...]\n"
558 " -s <socket>: Set the unix domain socket to connect to\n"
559 " -t <timeout>: Set the timeout (in seconds) for a command to complete\n"
560 " -S: Use simplified output (for scripts)\n"
561 " -v: More verbose output\n"
562 " -m <type>: (for monitor): include a specific message type\n"
563 " (can be used more than once)\n"
564 " -M <r|t> (for monitor): only capture received or transmitted traffic\n"
567 " - list [<path>] List objects\n"
568 " - call <path> <method> [<message>] Call an object method\n"
569 " - subscribe <path> [<path>...] Subscribe to object(s) notifications\n"
570 " - listen [<path>...] Listen for events\n"
571 " - send <type> [<message>] Send an event\n"
572 " - wait_for <object> [<object>...] Wait for multiple objects to appear on ubus\n"
573 " - monitor Monitor ubus traffic\n"
581 int (*cb
)(struct ubus_context
*ctx
, int argc
, char **argv
);
583 { "list", ubus_cli_list
},
584 { "call", ubus_cli_call
},
585 { "listen", ubus_cli_listen
},
586 { "subscribe", ubus_cli_subscribe
},
587 { "send", ubus_cli_send
},
588 { "wait_for", ubus_cli_wait_for
},
589 { "monitor", ubus_cli_monitor
},
592 int main(int argc
, char **argv
)
594 const char *progname
, *ubus_socket
= NULL
;
595 struct ubus_context
*ctx
;
603 while ((ch
= getopt(argc
, argv
, "m:M:vs:t:S")) != -1) {
606 ubus_socket
= optarg
;
609 listen_timeout
= atoi(optarg
);
610 timeout
= atoi(optarg
);
613 simple_output
= true;
619 if (add_monitor_type(optarg
))
620 return usage(progname
);
631 return usage(progname
);
635 return usage(progname
);
644 return usage(progname
);
646 ctx
= ubus_connect(ubus_socket
);
649 fprintf(stderr
, "Failed to connect to ubus\n");
657 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
658 if (strcmp(commands
[i
].name
, cmd
) != 0)
661 ret
= commands
[i
].cb(ctx
, argc
, argv
);
665 if (ret
> 0 && !simple_output
)
666 fprintf(stderr
, "Command failed: %s\n", ubus_strerror(ret
));