3 #include <sys/socket.h>
6 #include <libubox/blob.h>
7 #include <libubox/blobmsg.h>
8 #include <libubox/usock.h>
16 #define DPRINTF(_format, ...) fprintf(stderr, "ubus: " _format, ## __VA_ARGS__)
18 #define DPRINTF(...) do {} while(0)
21 #define STATIC_IOV(_var) { .iov_base = (char *) &(_var), .iov_len = sizeof(_var) }
23 const char *__ubus_strerror
[__UBUS_STATUS_LAST
] = {
24 [UBUS_STATUS_OK
] = "Success",
25 [UBUS_STATUS_INVALID_COMMAND
] = "Invalid command",
26 [UBUS_STATUS_INVALID_ARGUMENT
] = "Invalid argument",
27 [UBUS_STATUS_NOT_FOUND
] = "Not found",
28 [UBUS_STATUS_NO_DATA
] = "No response",
31 static struct blob_buf b
;
33 static const struct blob_attr_info ubus_policy
[UBUS_ATTR_MAX
] = {
34 [UBUS_ATTR_STATUS
] = { .type
= BLOB_ATTR_INT32
},
35 [UBUS_ATTR_OBJID
] = { .type
= BLOB_ATTR_INT32
},
36 [UBUS_ATTR_OBJPATH
] = { .type
= BLOB_ATTR_STRING
},
38 static struct blob_attr
*attrbuf
[UBUS_ATTR_MAX
];
40 struct ubus_pending_data
{
41 struct list_head list
;
43 struct blob_attr data
[];
46 struct blob_attr
**ubus_parse_msg(struct blob_attr
*msg
)
48 blob_parse(msg
, attrbuf
, ubus_policy
, UBUS_ATTR_MAX
);
52 const char *ubus_strerror(int error
)
56 if (error
< 0 || error
>= __UBUS_STATUS_LAST
)
59 if (!__ubus_strerror
[error
])
62 return __ubus_strerror
[error
];
65 sprintf(err
, "Unknown error: %d", error
);
69 int ubus_start_request(struct ubus_context
*ctx
, struct ubus_request
*req
,
70 struct blob_attr
*msg
, int cmd
, uint32_t peer
)
72 struct ubus_msghdr hdr
;
73 struct iovec iov
[2] = {
77 memset(req
, 0, sizeof(*req
));
80 hdr
.seq
= ++ctx
->request_seq
;
91 iov
[1].iov_base
= (char *) msg
;
92 iov
[1].iov_len
= blob_raw_len(msg
);
93 INIT_LIST_HEAD(&req
->list
);
94 INIT_LIST_HEAD(&req
->pending
);
96 return writev(ctx
->sock
.fd
, iov
, 2);
99 static bool recv_retry(int fd
, struct iovec
*iov
, bool wait
)
103 while (iov
->iov_len
> 0) {
104 bytes
= read(fd
, iov
->iov_base
, iov
->iov_len
);
110 if (errno
!= EAGAIN
) {
119 iov
->iov_len
-= bytes
;
120 iov
->iov_base
+= bytes
;
126 static bool ubus_validate_hdr(struct ubus_msghdr
*hdr
)
128 if (hdr
->version
!= 0)
131 if (blob_raw_len(hdr
->data
) < sizeof(*hdr
->data
))
134 if (blob_raw_len(hdr
->data
) + sizeof(*hdr
) > UBUS_MAX_MSGLEN
)
140 static bool get_next_msg(struct ubus_context
*ctx
, bool wait
)
142 struct iovec iov
= STATIC_IOV(ctx
->msgbuf
.hdr
);
144 /* receive header + start attribute */
145 iov
.iov_len
+= sizeof(struct blob_attr
);
146 if (!recv_retry(ctx
->sock
.fd
, &iov
, wait
))
149 iov
.iov_len
= blob_len(ctx
->msgbuf
.hdr
.data
);
150 if (iov
.iov_len
> 0 && !recv_retry(ctx
->sock
.fd
, &iov
, true))
153 return ubus_validate_hdr(&ctx
->msgbuf
.hdr
);
156 static bool ubus_get_status(struct ubus_msghdr
*hdr
, int *ret
)
158 ubus_parse_msg(hdr
->data
);
160 if (!attrbuf
[UBUS_ATTR_STATUS
])
163 *ret
= blob_get_int32(attrbuf
[UBUS_ATTR_STATUS
]);
167 static void ubus_process_req_data(struct ubus_request
*req
)
169 struct ubus_pending_data
*data
;
171 while (!list_empty(&req
->pending
)) {
172 data
= list_first_entry(&req
->pending
,
173 struct ubus_pending_data
, list
);
174 list_del(&data
->list
);
175 req
->data_cb(req
, data
->type
, data
->data
);
180 static void ubus_req_complete_cb(struct ubus_request
*req
)
182 ubus_complete_handler_t cb
= req
->complete_cb
;
187 req
->complete_cb
= NULL
;
188 cb(req
, req
->status_code
);
191 static int ubus_process_req_status(struct ubus_request
*req
, struct ubus_msghdr
*hdr
)
193 int ret
= UBUS_STATUS_INVALID_ARGUMENT
;
195 if (!list_empty(&req
->list
))
196 list_del(&req
->list
);
198 ubus_get_status(hdr
, &ret
);
199 req
->peer
= hdr
->peer
;
200 req
->status_msg
= true;
201 req
->status_code
= ret
;
203 ubus_req_complete_cb(req
);
208 static void ubus_req_data(struct ubus_request
*req
, struct ubus_msghdr
*hdr
)
210 struct ubus_pending_data
*data
;
215 req
->data_cb(req
, hdr
->type
, hdr
->data
);
216 ubus_process_req_data(req
);
217 req
->blocked
= false;
220 ubus_req_complete_cb(req
);
225 len
= blob_raw_len(hdr
->data
);
226 data
= calloc(1, sizeof(*data
) + len
);
230 data
->type
= hdr
->type
;
231 memcpy(data
->data
, hdr
->data
, len
);
232 list_add(&data
->list
, &req
->pending
);
235 static void ubus_process_msg(struct ubus_context
*ctx
, struct ubus_msghdr
*hdr
)
237 struct ubus_request
*req
;
239 list_for_each_entry(req
, &ctx
->requests
, list
) {
240 if (hdr
->seq
!= req
->seq
|| hdr
->peer
!= req
->peer
)
244 case UBUS_MSG_STATUS
:
245 ubus_process_req_status(req
, hdr
);
249 ubus_req_data(req
, hdr
);
252 DPRINTF("unknown message type: %d\n", hdr
->type
);
258 void ubus_abort_request(struct ubus_context
*ctx
, struct ubus_request
*req
)
260 if (!list_empty(&req
->list
))
263 list_del(&req
->list
);
266 void ubus_complete_request_async(struct ubus_context
*ctx
, struct ubus_request
*req
)
268 if (!list_empty(&req
->list
))
271 list_add(&req
->list
, &ctx
->requests
);
274 static void ubus_handle_data(struct uloop_fd
*u
, unsigned int events
)
276 struct ubus_context
*ctx
= container_of(u
, struct ubus_context
, sock
);
277 struct ubus_msghdr
*hdr
= &ctx
->msgbuf
.hdr
;
279 while (get_next_msg(ctx
, false))
280 ubus_process_msg(ctx
, hdr
);
283 int ubus_complete_request(struct ubus_context
*ctx
, struct ubus_request
*req
)
285 struct ubus_msghdr
*hdr
= &ctx
->msgbuf
.hdr
;
287 if (!list_empty(&req
->list
))
288 list_del(&req
->list
);
292 return req
->status_code
;
294 if (!get_next_msg(ctx
, true))
295 return UBUS_STATUS_NO_DATA
;
297 if (hdr
->seq
!= req
->seq
|| hdr
->peer
!= req
->peer
)
301 case UBUS_MSG_STATUS
:
302 return ubus_process_req_status(req
, hdr
);
305 ubus_req_data(req
, hdr
);
308 DPRINTF("unknown message type: %d\n", hdr
->type
);
313 ubus_process_msg(ctx
, hdr
);
317 void ubus_invoke_path_async(struct ubus_context
*ctx
, const char *path
, const char *method
,
318 struct blob_attr
*msg
, struct ubus_request
*req
)
320 blob_buf_init(&b
, 0);
321 blob_put_string(&b
, UBUS_ATTR_OBJPATH
, path
);
322 blob_put_string(&b
, UBUS_ATTR_METHOD
, method
);
323 blob_put(&b
, UBUS_ATTR_DATA
, blob_data(msg
), blob_len(msg
));
325 ubus_start_request(ctx
, req
, b
.head
, UBUS_MSG_INVOKE
, 0);
328 int ubus_invoke_path(struct ubus_context
*ctx
, const char *path
, const char *method
,
329 struct blob_attr
*msg
, ubus_data_handler_t cb
, void *priv
)
331 struct ubus_request req
;
333 ubus_invoke_path_async(ctx
, path
, method
, msg
, &req
);
336 return ubus_complete_request(ctx
, &req
);
339 void ubus_invoke_async(struct ubus_context
*ctx
, uint32_t obj
, const char *method
,
340 struct blob_attr
*msg
, struct ubus_request
*req
)
342 blob_buf_init(&b
, 0);
343 blob_put_int32(&b
, UBUS_ATTR_OBJID
, obj
);
344 blob_put_string(&b
, UBUS_ATTR_METHOD
, method
);
345 blob_put(&b
, UBUS_ATTR_DATA
, blob_data(msg
), blob_len(msg
));
347 ubus_start_request(ctx
, req
, b
.head
, UBUS_MSG_INVOKE
, 0);
350 int ubus_invoke(struct ubus_context
*ctx
, uint32_t obj
, const char *method
,
351 struct blob_attr
*msg
, ubus_data_handler_t cb
, void *priv
)
353 struct ubus_request req
;
355 ubus_invoke_async(ctx
, obj
, method
, msg
, &req
);
358 return ubus_complete_request(ctx
, &req
);
361 static void ubus_publish_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
363 struct ubus_object
*obj
= req
->priv
;
367 if (!attrbuf
[UBUS_ATTR_OBJID
])
370 obj
->id
= blob_get_int32(attrbuf
[UBUS_ATTR_OBJID
]);
372 if (attrbuf
[UBUS_ATTR_OBJTYPE
])
373 obj
->type
->id
= blob_get_int32(attrbuf
[UBUS_ATTR_OBJTYPE
]);
376 static bool ubus_push_table_data(const struct ubus_signature
**sig
, int *rem
, bool array
)
378 const struct ubus_signature
*cur
;
386 case UBUS_SIGNATURE_END
:
388 case BLOBMSG_TYPE_INT32
:
389 case BLOBMSG_TYPE_STRING
:
390 blobmsg_add_u32(&b
, cur
->name
, cur
->type
);
392 case BLOBMSG_TYPE_TABLE
:
393 case BLOBMSG_TYPE_ARRAY
:
394 nest_type
= cur
->type
== BLOBMSG_TYPE_ARRAY
;
395 nest
= blobmsg_open_nested(&b
, cur
->name
, nest_type
);
396 if (!ubus_push_table_data(sig
, rem
, nest_type
))
398 blobmsg_close_table(&b
, nest
);
409 static bool ubus_push_object_type(struct ubus_object_type
*type
)
412 int rem
= type
->n_signature
;
413 const struct ubus_signature
*sig
= type
->signature
;
415 s
= blob_nest_start(&b
, UBUS_ATTR_SIGNATURE
);
417 if (sig
->type
!= UBUS_SIGNATURE_METHOD
)
420 m
= blobmsg_open_table(&b
, sig
->name
);
424 if (!ubus_push_table_data(&sig
, &rem
, false))
427 blobmsg_close_table(&b
, m
);
429 blob_nest_end(&b
, s
);
434 int ubus_publish(struct ubus_context
*ctx
, struct ubus_object
*obj
)
436 struct ubus_request req
;
439 if (obj
->id
|| !obj
->name
|| !obj
->type
)
440 return UBUS_STATUS_INVALID_ARGUMENT
;
442 blob_buf_init(&b
, 0);
443 blob_put_string(&b
, UBUS_ATTR_OBJPATH
, obj
->name
);
445 blob_put_int32(&b
, UBUS_ATTR_OBJID
, obj
->parent
->id
);
448 blob_put_int32(&b
, UBUS_ATTR_OBJTYPE
, obj
->type
->id
);
449 else if (!ubus_push_object_type(obj
->type
))
450 return UBUS_STATUS_INVALID_ARGUMENT
;
452 ubus_start_request(ctx
, &req
, b
.head
, UBUS_MSG_PUBLISH
, 0);
453 req
.data_cb
= ubus_publish_cb
;
455 ret
= ubus_complete_request(ctx
, &req
);
460 return UBUS_STATUS_NO_DATA
;
465 struct ubus_context
*ubus_connect(const char *path
)
467 struct ubus_context
*ctx
;
469 struct ubus_msghdr hdr
;
470 struct blob_attr data
;
472 struct blob_attr
*buf
;
475 path
= UBUS_UNIX_SOCKET
;
477 ctx
= calloc(1, sizeof(*ctx
));
481 ctx
->sock
.fd
= usock(USOCK_UNIX
, path
, NULL
);
482 if (ctx
->sock
.fd
< 0) {
483 DPRINTF("Failed to connect to server\n");
486 ctx
->sock
.cb
= ubus_handle_data
;
488 if (read(ctx
->sock
.fd
, &hdr
, sizeof(hdr
)) != sizeof(hdr
)) {
489 DPRINTF("Failed to read initial message data\n");
493 if (!ubus_validate_hdr(&hdr
.hdr
)) {
494 DPRINTF("Failed to validate initial message header\n");
498 if (hdr
.hdr
.type
!= UBUS_MSG_HELLO
) {
499 DPRINTF("Unexpected initial message\n");
503 buf
= calloc(1, blob_raw_len(&hdr
.data
));
507 memcpy(buf
, &hdr
.data
, sizeof(hdr
.data
));
508 if (read(ctx
->sock
.fd
, blob_data(buf
), blob_len(buf
)) != blob_len(buf
)) {
509 DPRINTF("Failed to retrieve initial message data\n");
513 ctx
->local_id
= hdr
.hdr
.peer
;
516 if (!ctx
->local_id
) {
517 DPRINTF("Failed to get local peer id\n");
533 void ubus_free(struct ubus_context
*ctx
)