3 #include <sys/socket.h>
7 #include <libubox/blob.h>
8 #include <libubox/blobmsg.h>
9 #include <libubox/usock.h>
14 #define STATIC_IOV(_var) { .iov_base = (char *) &(_var), .iov_len = sizeof(_var) }
16 const char *__ubus_strerror[__UBUS_STATUS_LAST] = {
17 [UBUS_STATUS_OK] = "Success",
18 [UBUS_STATUS_INVALID_COMMAND] = "Invalid command",
19 [UBUS_STATUS_INVALID_ARGUMENT] = "Invalid argument",
20 [UBUS_STATUS_METHOD_NOT_FOUND] = "Method not found",
21 [UBUS_STATUS_NOT_FOUND] = "Not found",
22 [UBUS_STATUS_NO_DATA] = "No response",
23 [UBUS_STATUS_PERMISSION_DENIED] = "Permission denied",
24 [UBUS_STATUS_TIMEOUT] = "Request timed out",
27 static struct blob_buf b;
29 static const struct blob_attr_info ubus_policy[UBUS_ATTR_MAX] = {
30 [UBUS_ATTR_STATUS] = { .type = BLOB_ATTR_INT32 },
31 [UBUS_ATTR_OBJID] = { .type = BLOB_ATTR_INT32 },
32 [UBUS_ATTR_OBJPATH] = { .type = BLOB_ATTR_STRING },
33 [UBUS_ATTR_METHOD] = { .type = BLOB_ATTR_STRING },
35 static struct blob_attr *attrbuf[UBUS_ATTR_MAX];
37 struct ubus_pending_data {
38 struct list_head list;
40 struct blob_attr data[];
43 static int ubus_cmp_id(const void *k1, const void *k2, void *ptr)
45 const uint32_t *id1 = k1, *id2 = k2;
53 static struct blob_attr **ubus_parse_msg(struct blob_attr *msg)
55 blob_parse(msg, attrbuf, ubus_policy, UBUS_ATTR_MAX);
59 const char *ubus_strerror(int error)
63 if (error < 0 || error >= __UBUS_STATUS_LAST)
66 if (!__ubus_strerror[error])
69 return __ubus_strerror[error];
72 sprintf(err, "Unknown error: %d", error);
76 static int writev_retry(int fd, struct iovec *iov, int iov_len)
81 int cur_len = writev(fd, iov, iov_len);
85 /* turn off non-blocking mode */
86 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) &
97 while (cur_len >= iov->iov_len) {
98 cur_len -= iov->iov_len;
101 if (!cur_len || !iov_len)
104 iov->iov_len -= cur_len;
108 static int ubus_send_msg(struct ubus_context *ctx, uint32_t seq,
109 struct blob_attr *msg, int cmd, uint32_t peer)
111 struct ubus_msghdr hdr;
112 struct iovec iov[2] = {
122 blob_buf_init(&b, 0);
126 iov[1].iov_base = (char *) msg;
127 iov[1].iov_len = blob_raw_len(msg);
129 return writev_retry(ctx->sock.fd, iov, ARRAY_SIZE(iov));
132 static int ubus_start_request(struct ubus_context *ctx, struct ubus_request *req,
133 struct blob_attr *msg, int cmd, uint32_t peer)
135 memset(req, 0, sizeof(*req));
137 if (msg && blob_pad_len(msg) > UBUS_MAX_MSGLEN)
140 INIT_LIST_HEAD(&req->list);
141 INIT_LIST_HEAD(&req->pending);
144 req->seq = ++ctx->request_seq;
145 return ubus_send_msg(ctx, req->seq, msg, cmd, peer);
148 static bool recv_retry(int fd, struct iovec *iov, bool wait)
152 while (iov->iov_len > 0) {
153 bytes = read(fd, iov->iov_base, iov->iov_len);
168 iov->iov_len -= bytes;
169 iov->iov_base += bytes;
175 static bool ubus_validate_hdr(struct ubus_msghdr *hdr)
177 if (hdr->version != 0)
180 if (blob_raw_len(hdr->data) < sizeof(*hdr->data))
183 if (blob_pad_len(hdr->data) > UBUS_MAX_MSGLEN)
189 static bool get_next_msg(struct ubus_context *ctx)
191 struct iovec iov = STATIC_IOV(ctx->msgbuf.hdr);
193 /* receive header + start attribute */
194 iov.iov_len += sizeof(struct blob_attr);
195 if (!recv_retry(ctx->sock.fd, &iov, false))
198 iov.iov_len = blob_len(ctx->msgbuf.hdr.data);
199 if (iov.iov_len > 0 && !recv_retry(ctx->sock.fd, &iov, true))
202 return ubus_validate_hdr(&ctx->msgbuf.hdr);
205 static bool ubus_get_status(struct ubus_msghdr *hdr, int *ret)
207 ubus_parse_msg(hdr->data);
209 if (!attrbuf[UBUS_ATTR_STATUS])
212 *ret = blob_get_u32(attrbuf[UBUS_ATTR_STATUS]);
216 static void req_data_cb(struct ubus_request *req, int type, struct blob_attr *data)
218 struct blob_attr **attr;
220 if (req->raw_data_cb)
221 req->raw_data_cb(req, type, data);
226 attr = ubus_parse_msg(data);
227 req->data_cb(req, type, attr[UBUS_ATTR_DATA]);
230 static void ubus_process_req_data(struct ubus_request *req)
232 struct ubus_pending_data *data;
234 while (!list_empty(&req->pending)) {
235 data = list_first_entry(&req->pending,
236 struct ubus_pending_data, list);
237 list_del(&data->list);
239 req_data_cb(req, data->type, data->data);
244 static void ubus_req_complete_cb(struct ubus_request *req)
246 ubus_complete_handler_t cb = req->complete_cb;
251 req->complete_cb = NULL;
252 cb(req, req->status_code);
255 static int ubus_process_req_status(struct ubus_request *req, struct ubus_msghdr *hdr)
257 int ret = UBUS_STATUS_INVALID_ARGUMENT;
259 if (!list_empty(&req->list))
260 list_del(&req->list);
262 ubus_get_status(hdr, &ret);
263 req->peer = hdr->peer;
264 req->status_msg = true;
265 req->status_code = ret;
267 ubus_req_complete_cb(req);
272 static void ubus_req_data(struct ubus_request *req, struct ubus_msghdr *hdr)
274 struct ubus_pending_data *data;
279 req_data_cb(req, hdr->type, hdr->data);
280 ubus_process_req_data(req);
281 req->blocked = false;
284 ubus_req_complete_cb(req);
289 len = blob_raw_len(hdr->data);
290 data = calloc(1, sizeof(*data) + len);
294 data->type = hdr->type;
295 memcpy(data->data, hdr->data, len);
296 list_add(&data->list, &req->pending);
299 static struct ubus_request *ubus_find_request(struct ubus_context *ctx, uint32_t seq, uint32_t peer)
301 struct ubus_request *req;
303 list_for_each_entry(req, &ctx->requests, list) {
304 if (seq != req->seq || peer != req->peer)
312 static void ubus_process_invoke(struct ubus_context *ctx, struct ubus_msghdr *hdr)
314 struct ubus_request_data req;
315 struct ubus_object *obj;
320 ubus_parse_msg(hdr->data);
322 if (!attrbuf[UBUS_ATTR_OBJID])
325 objid = blob_get_u32(attrbuf[UBUS_ATTR_OBJID]);
327 if (!attrbuf[UBUS_ATTR_METHOD]) {
328 ret = UBUS_STATUS_INVALID_ARGUMENT;
332 obj = avl_find_element(&ctx->objects, &objid, obj, avl);
334 ret = UBUS_STATUS_NOT_FOUND;
338 for (method = 0; method < obj->n_methods; method++)
339 if (!obj->methods[method].name ||
340 !strcmp(obj->methods[method].name,
341 blob_data(attrbuf[UBUS_ATTR_METHOD])))
345 ret = UBUS_STATUS_METHOD_NOT_FOUND;
350 req.peer = hdr->peer;
352 ret = obj->methods[method].handler(ctx, obj, &req,
353 blob_data(attrbuf[UBUS_ATTR_METHOD]),
354 attrbuf[UBUS_ATTR_DATA]);
357 blob_buf_init(&b, 0);
358 blob_put_int32(&b, UBUS_ATTR_STATUS, ret);
359 blob_put_int32(&b, UBUS_ATTR_OBJID, objid);
360 ubus_send_msg(ctx, hdr->seq, b.head, UBUS_MSG_STATUS, hdr->peer);
363 static void ubus_process_msg(struct ubus_context *ctx, struct ubus_msghdr *hdr)
365 struct ubus_request *req;
368 case UBUS_MSG_STATUS:
369 req = ubus_find_request(ctx, hdr->seq, hdr->peer);
373 ubus_process_req_status(req, hdr);
377 req = ubus_find_request(ctx, hdr->seq, hdr->peer);
378 if (req && (req->data_cb || req->raw_data_cb))
379 ubus_req_data(req, hdr);
382 case UBUS_MSG_INVOKE:
383 ubus_process_invoke(ctx, hdr);
388 void ubus_abort_request(struct ubus_context *ctx, struct ubus_request *req)
390 if (!list_empty(&req->list))
393 req->cancelled = true;
394 ubus_process_req_data(req);
395 list_del(&req->list);
398 void ubus_complete_request_async(struct ubus_context *ctx, struct ubus_request *req)
400 if (!list_empty(&req->list))
403 list_add(&req->list, &ctx->requests);
406 static void ubus_handle_data(struct uloop_fd *u, unsigned int events)
408 struct ubus_context *ctx = container_of(u, struct ubus_context, sock);
409 struct ubus_msghdr *hdr = &ctx->msgbuf.hdr;
411 while (get_next_msg(ctx)) {
412 ubus_process_msg(ctx, hdr);
418 ctx->connection_lost(ctx);
421 static void ubus_sync_req_cb(struct ubus_request *req, int ret)
423 req->status_msg = true;
424 req->status_code = ret;
428 struct ubus_sync_req_cb {
429 struct uloop_timeout timeout;
430 struct ubus_request *req;
433 static void ubus_sync_req_timeout_cb(struct uloop_timeout *timeout)
435 struct ubus_sync_req_cb *cb;
437 cb = container_of(timeout, struct ubus_sync_req_cb, timeout);
438 ubus_sync_req_cb(cb->req, UBUS_STATUS_TIMEOUT);
441 int ubus_complete_request(struct ubus_context *ctx, struct ubus_request *req,
444 struct ubus_sync_req_cb cb;
445 ubus_complete_handler_t complete_cb = req->complete_cb;
446 bool registered = ctx->sock.registered;
447 bool cancelled = uloop_cancelled;
448 int status = UBUS_STATUS_NO_DATA;
456 memset(&cb, 0, sizeof(cb));
458 cb.timeout.cb = ubus_sync_req_timeout_cb;
459 uloop_timeout_set(&cb.timeout, timeout);
462 ubus_complete_request_async(ctx, req);
463 req->complete_cb = ubus_sync_req_cb;
468 uloop_timeout_cancel(&cb.timeout);
471 status = req->status_code;
473 req->complete_cb = complete_cb;
474 if (req->complete_cb)
475 req->complete_cb(req, status);
477 uloop_cancelled = cancelled;
479 uloop_fd_delete(&ctx->sock);
484 struct ubus_lookup_request {
485 struct ubus_request req;
486 ubus_lookup_handler_t cb;
489 static void ubus_lookup_cb(struct ubus_request *ureq, int type, struct blob_attr *msg)
491 struct ubus_lookup_request *req;
492 struct ubus_object_data obj;
493 struct blob_attr **attr;
495 req = container_of(ureq, struct ubus_lookup_request, req);
496 attr = ubus_parse_msg(msg);
498 if (!attr[UBUS_ATTR_OBJID] || !attr[UBUS_ATTR_OBJPATH] ||
499 !attr[UBUS_ATTR_OBJTYPE])
502 memset(&obj, 0, sizeof(obj));
503 obj.id = blob_get_u32(attr[UBUS_ATTR_OBJID]);
504 obj.path = blob_data(attr[UBUS_ATTR_OBJPATH]);
505 obj.type_id = blob_get_u32(attr[UBUS_ATTR_OBJTYPE]);
506 obj.signature = attr[UBUS_ATTR_SIGNATURE];
507 req->cb(ureq->ctx, &obj, ureq->priv);
510 int ubus_lookup(struct ubus_context *ctx, const char *path,
511 ubus_lookup_handler_t cb, void *priv)
513 struct ubus_lookup_request lookup;
515 blob_buf_init(&b, 0);
517 blob_put_string(&b, UBUS_ATTR_OBJPATH, path);
519 if (ubus_start_request(ctx, &lookup.req, b.head, UBUS_MSG_LOOKUP, 0) < 0)
520 return UBUS_STATUS_INVALID_ARGUMENT;
522 lookup.req.raw_data_cb = ubus_lookup_cb;
523 lookup.req.priv = priv;
525 return ubus_complete_request(ctx, &lookup.req, 0);
528 static void ubus_lookup_id_cb(struct ubus_request *req, int type, struct blob_attr *msg)
530 struct blob_attr **attr;
531 uint32_t *id = req->priv;
533 attr = ubus_parse_msg(msg);
535 if (!attr[UBUS_ATTR_OBJID])
538 *id = blob_get_u32(attr[UBUS_ATTR_OBJID]);
541 int ubus_lookup_id(struct ubus_context *ctx, const char *path, uint32_t *id)
543 struct ubus_request req;
545 blob_buf_init(&b, 0);
547 blob_put_string(&b, UBUS_ATTR_OBJPATH, path);
549 if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_LOOKUP, 0) < 0)
550 return UBUS_STATUS_INVALID_ARGUMENT;
552 req.raw_data_cb = ubus_lookup_id_cb;
555 return ubus_complete_request(ctx, &req, 0);
558 int ubus_send_reply(struct ubus_context *ctx, struct ubus_request_data *req,
559 struct blob_attr *msg)
563 blob_buf_init(&b, 0);
564 blob_put_int32(&b, UBUS_ATTR_OBJID, req->object);
565 blob_put(&b, UBUS_ATTR_DATA, blob_data(msg), blob_len(msg));
566 ret = ubus_send_msg(ctx, req->seq, b.head, UBUS_MSG_DATA, req->peer);
568 return UBUS_STATUS_NO_DATA;
573 int ubus_invoke_async(struct ubus_context *ctx, uint32_t obj, const char *method,
574 struct blob_attr *msg, struct ubus_request *req)
576 blob_buf_init(&b, 0);
577 blob_put_int32(&b, UBUS_ATTR_OBJID, obj);
578 blob_put_string(&b, UBUS_ATTR_METHOD, method);
580 blob_put(&b, UBUS_ATTR_DATA, blob_data(msg), blob_len(msg));
582 if (ubus_start_request(ctx, req, b.head, UBUS_MSG_INVOKE, obj) < 0)
583 return UBUS_STATUS_INVALID_ARGUMENT;
588 int ubus_invoke(struct ubus_context *ctx, uint32_t obj, const char *method,
589 struct blob_attr *msg, ubus_data_handler_t cb, void *priv,
592 struct ubus_request req;
594 ubus_invoke_async(ctx, obj, method, msg, &req);
597 return ubus_complete_request(ctx, &req, timeout);
600 static void ubus_add_object_cb(struct ubus_request *req, int type, struct blob_attr *msg)
602 struct ubus_object *obj = req->priv;
606 if (!attrbuf[UBUS_ATTR_OBJID])
609 obj->id = blob_get_u32(attrbuf[UBUS_ATTR_OBJID]);
611 if (attrbuf[UBUS_ATTR_OBJTYPE])
612 obj->type->id = blob_get_u32(attrbuf[UBUS_ATTR_OBJTYPE]);
614 obj->avl.key = &obj->id;
615 avl_insert(&req->ctx->objects, &obj->avl);
618 static void ubus_push_method_data(const struct ubus_method *m)
623 mtbl = blobmsg_open_table(&b, m->name);
625 for (i = 0; i < m->n_policy; i++)
626 blobmsg_add_u32(&b, m->policy[i].name, m->policy[i].type);
628 blobmsg_close_table(&b, mtbl);
631 static bool ubus_push_object_type(const struct ubus_object_type *type)
636 s = blob_nest_start(&b, UBUS_ATTR_SIGNATURE);
638 for (i = 0; i < type->n_methods; i++)
639 ubus_push_method_data(&type->methods[i]);
641 blob_nest_end(&b, s);
646 static int __ubus_add_object(struct ubus_context *ctx, struct ubus_object *obj)
648 struct ubus_request req;
651 blob_buf_init(&b, 0);
653 if (obj->name && obj->type) {
654 blob_put_string(&b, UBUS_ATTR_OBJPATH, obj->name);
657 blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id);
658 else if (!ubus_push_object_type(obj->type))
659 return UBUS_STATUS_INVALID_ARGUMENT;
662 if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_ADD_OBJECT, 0) < 0)
663 return UBUS_STATUS_INVALID_ARGUMENT;
665 req.raw_data_cb = ubus_add_object_cb;
667 ret = ubus_complete_request(ctx, &req, 0);
672 return UBUS_STATUS_NO_DATA;
677 int ubus_add_object(struct ubus_context *ctx, struct ubus_object *obj)
679 if (!obj->name || !obj->type)
680 return UBUS_STATUS_INVALID_ARGUMENT;
682 return __ubus_add_object(ctx, obj);
685 static void ubus_remove_object_cb(struct ubus_request *req, int type, struct blob_attr *msg)
687 struct ubus_object *obj = req->priv;
691 if (!attrbuf[UBUS_ATTR_OBJID])
696 if (attrbuf[UBUS_ATTR_OBJTYPE] && obj->type)
699 avl_delete(&req->ctx->objects, &obj->avl);
702 int ubus_remove_object(struct ubus_context *ctx, struct ubus_object *obj)
704 struct ubus_request req;
707 blob_buf_init(&b, 0);
708 blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id);
710 if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_REMOVE_OBJECT, 0) < 0)
711 return UBUS_STATUS_INVALID_ARGUMENT;
713 req.raw_data_cb = ubus_remove_object_cb;
715 ret = ubus_complete_request(ctx, &req, 0);
720 return UBUS_STATUS_NO_DATA;
725 static int ubus_event_cb(struct ubus_context *ctx, struct ubus_object *obj,
726 struct ubus_request_data *req,
727 const char *method, struct blob_attr *msg)
729 struct ubus_event_handler *ev;
731 ev = container_of(obj, struct ubus_event_handler, obj);
732 ev->cb(ctx, ev, method, msg);
736 static const struct ubus_method event_method = {
738 .handler = ubus_event_cb,
741 int ubus_register_event_handler(struct ubus_context *ctx,
742 struct ubus_event_handler *ev,
745 struct ubus_object *obj = &ev->obj;
750 obj->methods = &event_method;
753 if (!!obj->name ^ !!obj->type)
754 return UBUS_STATUS_INVALID_ARGUMENT;
756 ret = __ubus_add_object(ctx, obj);
761 /* use a second buffer, ubus_invoke() overwrites the primary one */
762 memset(&b2, 0, sizeof(b2));
763 blob_buf_init(&b2, 0);
764 blobmsg_add_u32(&b2, "object", obj->id);
766 blobmsg_add_string(&b2, "pattern", pattern);
768 return ubus_invoke(ctx, UBUS_SYSTEM_OBJECT_EVENT, "register", b2.head,
772 int ubus_send_event(struct ubus_context *ctx, const char *id,
773 struct blob_attr *data)
775 struct ubus_request req;
778 blob_buf_init(&b, 0);
779 blob_put_int32(&b, UBUS_ATTR_OBJID, UBUS_SYSTEM_OBJECT_EVENT);
780 blob_put_string(&b, UBUS_ATTR_METHOD, "send");
781 s = blob_nest_start(&b, UBUS_ATTR_DATA);
782 blobmsg_add_string(&b, "id", id);
783 blobmsg_add_field(&b, BLOBMSG_TYPE_TABLE, "data", blob_data(data), blob_len(data));
784 blob_nest_end(&b, s);
786 if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_INVOKE, UBUS_SYSTEM_OBJECT_EVENT) < 0)
787 return UBUS_STATUS_INVALID_ARGUMENT;
789 return ubus_complete_request(ctx, &req, 0);
792 static void ubus_default_connection_lost(struct ubus_context *ctx)
794 if (ctx->sock.registered)
798 struct ubus_context *ubus_connect(const char *path)
800 struct ubus_context *ctx;
802 struct ubus_msghdr hdr;
803 struct blob_attr data;
805 struct blob_attr *buf;
808 path = UBUS_UNIX_SOCKET;
810 ctx = calloc(1, sizeof(*ctx));
814 ctx->sock.fd = usock(USOCK_UNIX, path, NULL);
815 if (ctx->sock.fd < 0)
818 ctx->sock.cb = ubus_handle_data;
820 if (read(ctx->sock.fd, &hdr, sizeof(hdr)) != sizeof(hdr))
823 if (!ubus_validate_hdr(&hdr.hdr))
826 if (hdr.hdr.type != UBUS_MSG_HELLO)
829 buf = calloc(1, blob_raw_len(&hdr.data));
833 memcpy(buf, &hdr.data, sizeof(hdr.data));
834 if (read(ctx->sock.fd, blob_data(buf), blob_len(buf)) != blob_len(buf))
837 ctx->local_id = hdr.hdr.peer;
840 ctx->connection_lost = ubus_default_connection_lost;
842 INIT_LIST_HEAD(&ctx->requests);
843 avl_init(&ctx->objects, ubus_cmp_id, false, NULL);
860 void ubus_free(struct ubus_context *ctx)