3 #include <sys/socket.h>
6 #include <libubox/blob.h>
7 #include <libubox/blobmsg.h>
8 #include <libubox/usock.h>
13 #define STATIC_IOV(_var) { .iov_base = (char *) &(_var), .iov_len = sizeof(_var) }
15 const char *__ubus_strerror
[__UBUS_STATUS_LAST
] = {
16 [UBUS_STATUS_OK
] = "Success",
17 [UBUS_STATUS_INVALID_COMMAND
] = "Invalid command",
18 [UBUS_STATUS_INVALID_ARGUMENT
] = "Invalid argument",
19 [UBUS_STATUS_METHOD_NOT_FOUND
] = "Method not found",
20 [UBUS_STATUS_NOT_FOUND
] = "Not found",
21 [UBUS_STATUS_NO_DATA
] = "No response",
22 [UBUS_STATUS_PERMISSION_DENIED
] = "Permission denied",
25 static struct blob_buf b
;
27 static const struct blob_attr_info ubus_policy
[UBUS_ATTR_MAX
] = {
28 [UBUS_ATTR_STATUS
] = { .type
= BLOB_ATTR_INT32
},
29 [UBUS_ATTR_OBJID
] = { .type
= BLOB_ATTR_INT32
},
30 [UBUS_ATTR_OBJPATH
] = { .type
= BLOB_ATTR_STRING
},
31 [UBUS_ATTR_METHOD
] = { .type
= BLOB_ATTR_STRING
},
33 static struct blob_attr
*attrbuf
[UBUS_ATTR_MAX
];
35 struct ubus_pending_data
{
36 struct list_head list
;
38 struct blob_attr data
[];
41 static int ubus_cmp_id(const void *k1
, const void *k2
, void *ptr
)
43 const uint32_t *id1
= k1
, *id2
= k2
;
51 static struct blob_attr
**ubus_parse_msg(struct blob_attr
*msg
)
53 blob_parse(msg
, attrbuf
, ubus_policy
, UBUS_ATTR_MAX
);
57 const char *ubus_strerror(int error
)
61 if (error
< 0 || error
>= __UBUS_STATUS_LAST
)
64 if (!__ubus_strerror
[error
])
67 return __ubus_strerror
[error
];
70 sprintf(err
, "Unknown error: %d", error
);
74 static int ubus_send_msg(struct ubus_context
*ctx
, uint32_t seq
,
75 struct blob_attr
*msg
, int cmd
, uint32_t peer
)
77 struct ubus_msghdr hdr
;
78 struct iovec iov
[2] = {
92 iov
[1].iov_base
= (char *) msg
;
93 iov
[1].iov_len
= blob_raw_len(msg
);
95 return writev(ctx
->sock
.fd
, iov
, 2);
98 static int ubus_start_request(struct ubus_context
*ctx
, struct ubus_request
*req
,
99 struct blob_attr
*msg
, int cmd
, uint32_t peer
)
101 memset(req
, 0, sizeof(*req
));
103 INIT_LIST_HEAD(&req
->list
);
104 INIT_LIST_HEAD(&req
->pending
);
107 req
->seq
= ++ctx
->request_seq
;
108 return ubus_send_msg(ctx
, req
->seq
, msg
, cmd
, peer
);
111 static bool recv_retry(int fd
, struct iovec
*iov
, bool wait
)
115 while (iov
->iov_len
> 0) {
116 bytes
= read(fd
, iov
->iov_base
, iov
->iov_len
);
124 if (errno
!= EAGAIN
) {
133 iov
->iov_len
-= bytes
;
134 iov
->iov_base
+= bytes
;
140 static bool ubus_validate_hdr(struct ubus_msghdr
*hdr
)
142 if (hdr
->version
!= 0)
145 if (blob_raw_len(hdr
->data
) < sizeof(*hdr
->data
))
148 if (blob_pad_len(hdr
->data
) > UBUS_MAX_MSGLEN
)
154 static bool get_next_msg(struct ubus_context
*ctx
, bool wait
)
156 struct iovec iov
= STATIC_IOV(ctx
->msgbuf
.hdr
);
158 /* receive header + start attribute */
159 iov
.iov_len
+= sizeof(struct blob_attr
);
160 if (!recv_retry(ctx
->sock
.fd
, &iov
, wait
))
163 iov
.iov_len
= blob_len(ctx
->msgbuf
.hdr
.data
);
164 if (iov
.iov_len
> 0 && !recv_retry(ctx
->sock
.fd
, &iov
, true))
167 return ubus_validate_hdr(&ctx
->msgbuf
.hdr
);
170 static bool ubus_get_status(struct ubus_msghdr
*hdr
, int *ret
)
172 ubus_parse_msg(hdr
->data
);
174 if (!attrbuf
[UBUS_ATTR_STATUS
])
177 *ret
= blob_get_u32(attrbuf
[UBUS_ATTR_STATUS
]);
181 static void req_data_cb(struct ubus_request
*req
, int type
, struct blob_attr
*data
)
183 struct blob_attr
**attr
;
185 if (req
->raw_data_cb
)
186 req
->raw_data_cb(req
, type
, data
);
191 attr
= ubus_parse_msg(data
);
192 req
->data_cb(req
, type
, attr
[UBUS_ATTR_DATA
]);
195 static void ubus_process_req_data(struct ubus_request
*req
)
197 struct ubus_pending_data
*data
;
199 while (!list_empty(&req
->pending
)) {
200 data
= list_first_entry(&req
->pending
,
201 struct ubus_pending_data
, list
);
202 list_del(&data
->list
);
204 req_data_cb(req
, data
->type
, data
->data
);
209 static void ubus_req_complete_cb(struct ubus_request
*req
)
211 ubus_complete_handler_t cb
= req
->complete_cb
;
216 req
->complete_cb
= NULL
;
217 cb(req
, req
->status_code
);
220 static int ubus_process_req_status(struct ubus_request
*req
, struct ubus_msghdr
*hdr
)
222 int ret
= UBUS_STATUS_INVALID_ARGUMENT
;
224 if (!list_empty(&req
->list
))
225 list_del(&req
->list
);
227 ubus_get_status(hdr
, &ret
);
228 req
->peer
= hdr
->peer
;
229 req
->status_msg
= true;
230 req
->status_code
= ret
;
232 ubus_req_complete_cb(req
);
237 static void ubus_req_data(struct ubus_request
*req
, struct ubus_msghdr
*hdr
)
239 struct ubus_pending_data
*data
;
244 req_data_cb(req
, hdr
->type
, hdr
->data
);
245 ubus_process_req_data(req
);
246 req
->blocked
= false;
249 ubus_req_complete_cb(req
);
254 len
= blob_raw_len(hdr
->data
);
255 data
= calloc(1, sizeof(*data
) + len
);
259 data
->type
= hdr
->type
;
260 memcpy(data
->data
, hdr
->data
, len
);
261 list_add(&data
->list
, &req
->pending
);
264 static struct ubus_request
*ubus_find_request(struct ubus_context
*ctx
, uint32_t seq
, uint32_t peer
)
266 struct ubus_request
*req
;
268 list_for_each_entry(req
, &ctx
->requests
, list
) {
269 if (seq
!= req
->seq
|| peer
!= req
->peer
)
277 static void ubus_process_invoke(struct ubus_context
*ctx
, struct ubus_msghdr
*hdr
)
279 struct ubus_request_data req
;
280 struct ubus_object
*obj
;
285 ubus_parse_msg(hdr
->data
);
287 if (!attrbuf
[UBUS_ATTR_OBJID
])
290 objid
= blob_get_u32(attrbuf
[UBUS_ATTR_OBJID
]);
292 if (!attrbuf
[UBUS_ATTR_METHOD
]) {
293 ret
= UBUS_STATUS_INVALID_ARGUMENT
;
297 obj
= avl_find_element(&ctx
->objects
, &objid
, obj
, avl
);
299 ret
= UBUS_STATUS_NOT_FOUND
;
303 for (method
= 0; method
< obj
->n_methods
; method
++)
304 if (!obj
->methods
[method
].name
||
305 !strcmp(obj
->methods
[method
].name
,
306 blob_data(attrbuf
[UBUS_ATTR_METHOD
])))
310 ret
= UBUS_STATUS_METHOD_NOT_FOUND
;
315 req
.peer
= hdr
->peer
;
317 ret
= obj
->methods
[method
].handler(ctx
, obj
, &req
,
318 blob_data(attrbuf
[UBUS_ATTR_METHOD
]),
319 attrbuf
[UBUS_ATTR_DATA
]);
322 blob_buf_init(&b
, 0);
323 blob_put_int32(&b
, UBUS_ATTR_STATUS
, ret
);
324 blob_put_int32(&b
, UBUS_ATTR_OBJID
, objid
);
325 ubus_send_msg(ctx
, hdr
->seq
, b
.head
, UBUS_MSG_STATUS
, hdr
->peer
);
328 static void ubus_process_msg(struct ubus_context
*ctx
, struct ubus_msghdr
*hdr
)
330 struct ubus_request
*req
;
333 case UBUS_MSG_STATUS
:
334 req
= ubus_find_request(ctx
, hdr
->seq
, hdr
->peer
);
338 ubus_process_req_status(req
, hdr
);
342 req
= ubus_find_request(ctx
, hdr
->seq
, hdr
->peer
);
343 if (req
&& (req
->data_cb
|| req
->raw_data_cb
))
344 ubus_req_data(req
, hdr
);
347 case UBUS_MSG_INVOKE
:
348 ubus_process_invoke(ctx
, hdr
);
353 void ubus_abort_request(struct ubus_context
*ctx
, struct ubus_request
*req
)
355 if (!list_empty(&req
->list
))
358 req
->cancelled
= true;
359 ubus_process_req_data(req
);
360 list_del(&req
->list
);
363 void ubus_complete_request_async(struct ubus_context
*ctx
, struct ubus_request
*req
)
365 if (!list_empty(&req
->list
))
368 list_add(&req
->list
, &ctx
->requests
);
371 static void ubus_handle_data(struct uloop_fd
*u
, unsigned int events
)
373 struct ubus_context
*ctx
= container_of(u
, struct ubus_context
, sock
);
374 struct ubus_msghdr
*hdr
= &ctx
->msgbuf
.hdr
;
376 while (get_next_msg(ctx
, false))
377 ubus_process_msg(ctx
, hdr
);
380 ctx
->connection_lost(ctx
);
383 int ubus_complete_request(struct ubus_context
*ctx
, struct ubus_request
*req
)
385 struct ubus_msghdr
*hdr
= &ctx
->msgbuf
.hdr
;
387 if (!list_empty(&req
->list
))
388 list_del(&req
->list
);
392 return req
->status_code
;
395 return UBUS_STATUS_NO_DATA
;
397 if (!get_next_msg(ctx
, true))
398 return UBUS_STATUS_NO_DATA
;
400 if (hdr
->seq
!= req
->seq
|| hdr
->peer
!= req
->peer
)
404 case UBUS_MSG_STATUS
:
405 return ubus_process_req_status(req
, hdr
);
407 if (req
->data_cb
|| req
->raw_data_cb
)
408 ubus_req_data(req
, hdr
);
415 ubus_process_msg(ctx
, hdr
);
419 struct ubus_lookup_request
{
420 struct ubus_request req
;
421 ubus_lookup_handler_t cb
;
424 static void ubus_lookup_cb(struct ubus_request
*ureq
, int type
, struct blob_attr
*msg
)
426 struct ubus_lookup_request
*req
;
427 struct ubus_object_data obj
;
428 struct blob_attr
**attr
;
430 req
= container_of(ureq
, struct ubus_lookup_request
, req
);
431 attr
= ubus_parse_msg(msg
);
433 if (!attr
[UBUS_ATTR_OBJID
] || !attr
[UBUS_ATTR_OBJPATH
] ||
434 !attr
[UBUS_ATTR_OBJTYPE
])
437 memset(&obj
, 0, sizeof(obj
));
438 obj
.id
= blob_get_u32(attr
[UBUS_ATTR_OBJID
]);
439 obj
.path
= blob_data(attr
[UBUS_ATTR_OBJPATH
]);
440 obj
.type_id
= blob_get_u32(attr
[UBUS_ATTR_OBJTYPE
]);
441 obj
.signature
= attr
[UBUS_ATTR_SIGNATURE
];
442 req
->cb(ureq
->ctx
, &obj
, ureq
->priv
);
445 int ubus_lookup(struct ubus_context
*ctx
, const char *path
,
446 ubus_lookup_handler_t cb
, void *priv
)
448 struct ubus_lookup_request lookup
;
450 blob_buf_init(&b
, 0);
452 blob_put_string(&b
, UBUS_ATTR_OBJPATH
, path
);
453 ubus_start_request(ctx
, &lookup
.req
, b
.head
, UBUS_MSG_LOOKUP
, 0);
454 lookup
.req
.raw_data_cb
= ubus_lookup_cb
;
455 lookup
.req
.priv
= priv
;
457 return ubus_complete_request(ctx
, &lookup
.req
);
460 static void ubus_lookup_id_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
462 struct blob_attr
**attr
;
463 uint32_t *id
= req
->priv
;
465 attr
= ubus_parse_msg(msg
);
467 if (!attr
[UBUS_ATTR_OBJID
])
470 *id
= blob_get_u32(attr
[UBUS_ATTR_OBJID
]);
473 int ubus_lookup_id(struct ubus_context
*ctx
, const char *path
, uint32_t *id
)
475 struct ubus_request req
;
477 blob_buf_init(&b
, 0);
479 blob_put_string(&b
, UBUS_ATTR_OBJPATH
, path
);
480 ubus_start_request(ctx
, &req
, b
.head
, UBUS_MSG_LOOKUP
, 0);
481 req
.raw_data_cb
= ubus_lookup_id_cb
;
484 return ubus_complete_request(ctx
, &req
);
487 int ubus_send_reply(struct ubus_context
*ctx
, struct ubus_request_data
*req
,
488 struct blob_attr
*msg
)
492 blob_buf_init(&b
, 0);
493 blob_put_int32(&b
, UBUS_ATTR_OBJID
, req
->object
);
494 blob_put(&b
, UBUS_ATTR_DATA
, blob_data(msg
), blob_len(msg
));
495 ret
= ubus_send_msg(ctx
, req
->seq
, b
.head
, UBUS_MSG_DATA
, req
->peer
);
497 return UBUS_STATUS_NO_DATA
;
502 void ubus_invoke_async(struct ubus_context
*ctx
, uint32_t obj
, const char *method
,
503 struct blob_attr
*msg
, struct ubus_request
*req
)
505 blob_buf_init(&b
, 0);
506 blob_put_int32(&b
, UBUS_ATTR_OBJID
, obj
);
507 blob_put_string(&b
, UBUS_ATTR_METHOD
, method
);
509 blob_put(&b
, UBUS_ATTR_DATA
, blob_data(msg
), blob_len(msg
));
511 ubus_start_request(ctx
, req
, b
.head
, UBUS_MSG_INVOKE
, obj
);
514 int ubus_invoke(struct ubus_context
*ctx
, uint32_t obj
, const char *method
,
515 struct blob_attr
*msg
, ubus_data_handler_t cb
, void *priv
)
517 struct ubus_request req
;
519 ubus_invoke_async(ctx
, obj
, method
, msg
, &req
);
522 return ubus_complete_request(ctx
, &req
);
525 static void ubus_add_object_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
527 struct ubus_object
*obj
= req
->priv
;
531 if (!attrbuf
[UBUS_ATTR_OBJID
])
534 obj
->id
= blob_get_u32(attrbuf
[UBUS_ATTR_OBJID
]);
536 if (attrbuf
[UBUS_ATTR_OBJTYPE
])
537 obj
->type
->id
= blob_get_u32(attrbuf
[UBUS_ATTR_OBJTYPE
]);
539 obj
->avl
.key
= &obj
->id
;
540 avl_insert(&req
->ctx
->objects
, &obj
->avl
);
543 static bool ubus_push_table_data(const struct ubus_signature
**sig
, int *rem
, bool array
)
545 const struct ubus_signature
*cur
;
553 case UBUS_SIGNATURE_END
:
555 case BLOBMSG_TYPE_INT32
:
556 case BLOBMSG_TYPE_STRING
:
557 blobmsg_add_u32(&b
, cur
->name
, cur
->type
);
559 case BLOBMSG_TYPE_TABLE
:
560 case BLOBMSG_TYPE_ARRAY
:
561 nest_type
= cur
->type
== BLOBMSG_TYPE_ARRAY
;
562 nest
= blobmsg_open_nested(&b
, cur
->name
, nest_type
);
563 if (!ubus_push_table_data(sig
, rem
, nest_type
))
565 blobmsg_close_table(&b
, nest
);
576 static bool ubus_push_object_type(struct ubus_object_type
*type
)
579 int rem
= type
->n_signature
;
580 const struct ubus_signature
*sig
= type
->signature
;
582 s
= blob_nest_start(&b
, UBUS_ATTR_SIGNATURE
);
584 if (sig
->type
!= UBUS_SIGNATURE_METHOD
)
587 m
= blobmsg_open_table(&b
, sig
->name
);
591 if (!ubus_push_table_data(&sig
, &rem
, false))
594 blobmsg_close_table(&b
, m
);
596 blob_nest_end(&b
, s
);
601 static int __ubus_add_object(struct ubus_context
*ctx
, struct ubus_object
*obj
)
603 struct ubus_request req
;
606 blob_buf_init(&b
, 0);
608 if (obj
->name
&& obj
->type
) {
609 blob_put_string(&b
, UBUS_ATTR_OBJPATH
, obj
->name
);
612 blob_put_int32(&b
, UBUS_ATTR_OBJTYPE
, obj
->type
->id
);
613 else if (!ubus_push_object_type(obj
->type
))
614 return UBUS_STATUS_INVALID_ARGUMENT
;
617 ubus_start_request(ctx
, &req
, b
.head
, UBUS_MSG_ADD_OBJECT
, 0);
618 req
.raw_data_cb
= ubus_add_object_cb
;
620 ret
= ubus_complete_request(ctx
, &req
);
625 return UBUS_STATUS_NO_DATA
;
630 int ubus_add_object(struct ubus_context
*ctx
, struct ubus_object
*obj
)
632 if (!obj
->name
|| !obj
->type
)
633 return UBUS_STATUS_INVALID_ARGUMENT
;
635 return __ubus_add_object(ctx
, obj
);
638 static void ubus_remove_object_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
640 struct ubus_object
*obj
= req
->priv
;
644 if (!attrbuf
[UBUS_ATTR_OBJID
])
649 if (attrbuf
[UBUS_ATTR_OBJTYPE
] && obj
->type
)
652 avl_delete(&req
->ctx
->objects
, &obj
->avl
);
655 int ubus_remove_object(struct ubus_context
*ctx
, struct ubus_object
*obj
)
657 struct ubus_request req
;
660 blob_buf_init(&b
, 0);
661 blob_put_int32(&b
, UBUS_ATTR_OBJID
, obj
->id
);
662 ubus_start_request(ctx
, &req
, b
.head
, UBUS_MSG_REMOVE_OBJECT
, 0);
663 req
.raw_data_cb
= ubus_remove_object_cb
;
665 ret
= ubus_complete_request(ctx
, &req
);
670 return UBUS_STATUS_NO_DATA
;
675 static int ubus_event_cb(struct ubus_context
*ctx
, struct ubus_object
*obj
,
676 struct ubus_request_data
*req
,
677 const char *method
, struct blob_attr
*msg
)
679 struct ubus_event_handler
*ev
;
681 ev
= container_of(obj
, struct ubus_event_handler
, obj
);
682 ev
->cb(ctx
, ev
, method
, msg
);
686 static const struct ubus_method event_method
= {
688 .handler
= ubus_event_cb
,
691 int ubus_register_event_handler(struct ubus_context
*ctx
,
692 struct ubus_event_handler
*ev
,
695 struct ubus_object
*obj
= &ev
->obj
;
700 obj
->methods
= &event_method
;
703 if (!!obj
->name
^ !!obj
->type
)
704 return UBUS_STATUS_INVALID_ARGUMENT
;
706 ret
= __ubus_add_object(ctx
, obj
);
711 /* use a second buffer, ubus_invoke() overwrites the primary one */
712 memset(&b2
, 0, sizeof(b2
));
713 blob_buf_init(&b2
, 0);
714 blobmsg_add_u32(&b2
, "object", obj
->id
);
716 blobmsg_add_string(&b2
, "pattern", pattern
);
718 ret
= ubus_invoke(ctx
, UBUS_SYSTEM_OBJECT_EVENT
, "register", b2
.head
,
724 int ubus_send_event(struct ubus_context
*ctx
, const char *id
,
725 struct blob_attr
*data
)
727 struct ubus_request req
;
730 blob_buf_init(&b
, 0);
731 blob_put_int32(&b
, UBUS_ATTR_OBJID
, UBUS_SYSTEM_OBJECT_EVENT
);
732 blob_put_string(&b
, UBUS_ATTR_METHOD
, "send");
733 s
= blob_nest_start(&b
, UBUS_ATTR_DATA
);
734 blobmsg_add_string(&b
, "id", id
);
735 blobmsg_add_field(&b
, BLOBMSG_TYPE_TABLE
, "data", blob_data(data
), blob_len(data
));
736 blob_nest_end(&b
, s
);
738 ubus_start_request(ctx
, &req
, b
.head
, UBUS_MSG_INVOKE
, UBUS_SYSTEM_OBJECT_EVENT
);
739 return ubus_complete_request(ctx
, &req
);
742 static void ubus_default_connection_lost(struct ubus_context
*ctx
)
744 if (ctx
->sock
.registered
)
748 struct ubus_context
*ubus_connect(const char *path
)
750 struct ubus_context
*ctx
;
752 struct ubus_msghdr hdr
;
753 struct blob_attr data
;
755 struct blob_attr
*buf
;
758 path
= UBUS_UNIX_SOCKET
;
760 ctx
= calloc(1, sizeof(*ctx
));
764 ctx
->sock
.fd
= usock(USOCK_UNIX
, path
, NULL
);
765 if (ctx
->sock
.fd
< 0)
768 ctx
->sock
.cb
= ubus_handle_data
;
770 if (read(ctx
->sock
.fd
, &hdr
, sizeof(hdr
)) != sizeof(hdr
))
773 if (!ubus_validate_hdr(&hdr
.hdr
))
776 if (hdr
.hdr
.type
!= UBUS_MSG_HELLO
)
779 buf
= calloc(1, blob_raw_len(&hdr
.data
));
783 memcpy(buf
, &hdr
.data
, sizeof(hdr
.data
));
784 if (read(ctx
->sock
.fd
, blob_data(buf
), blob_len(buf
)) != blob_len(buf
))
787 ctx
->local_id
= hdr
.hdr
.peer
;
790 ctx
->connection_lost
= ubus_default_connection_lost
;
792 INIT_LIST_HEAD(&ctx
->requests
);
793 avl_init(&ctx
->objects
, ubus_cmp_id
, false, NULL
);
810 void ubus_free(struct ubus_context
*ctx
)