X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fubus.git;a=blobdiff_plain;f=libubus.c;h=590a0fa2ec5dbdf6e008545c0b19bc4f421e640d;hp=3050509a781ca975e702ace3e5e42b21b5c379e0;hb=95062c1ef74695d138bbc4b9efc5910b1436bd9c;hpb=d2f1a01d9f6b49d7353dc848018c176400091741 diff --git a/libubus.c b/libubus.c index 3050509..590a0fa 100644 --- a/libubus.c +++ b/libubus.c @@ -1,7 +1,22 @@ +/* + * Copyright (C) 2011 Felix Fietkau + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 2.1 + * as published by the Free Software Foundation + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + #include #include #include +#include #include +#include #include #include @@ -10,14 +25,6 @@ #include "libubus.h" #include "ubusmsg.h" -#define DEBUG 1 - -#ifdef DEBUG -#define DPRINTF(_format, ...) fprintf(stderr, "ubus: " _format, ## __VA_ARGS__) -#else -#define DPRINTF(...) do {} while(0) -#endif - #define STATIC_IOV(_var) { .iov_base = (char *) &(_var), .iov_len = sizeof(_var) } const char *__ubus_strerror[__UBUS_STATUS_LAST] = { @@ -28,6 +35,9 @@ const char *__ubus_strerror[__UBUS_STATUS_LAST] = { [UBUS_STATUS_NOT_FOUND] = "Not found", [UBUS_STATUS_NO_DATA] = "No response", [UBUS_STATUS_PERMISSION_DENIED] = "Permission denied", + [UBUS_STATUS_TIMEOUT] = "Request timed out", + [UBUS_STATUS_NOT_SUPPORTED] = "Operation not supported", + [UBUS_STATUS_UNKNOWN_ERROR] = "Unknown error", }; static struct blob_buf b; @@ -46,6 +56,13 @@ struct ubus_pending_data { struct blob_attr data[]; }; +struct ubus_pending_invoke { + struct list_head list; + struct ubus_msghdr hdr; +}; + +static void ubus_process_pending_invoke(struct ubus_context *ctx); + static int ubus_cmp_id(const void *k1, const void *k2, void *ptr) { const uint32_t *id1 = k1, *id2 = k2; @@ -79,6 +96,44 @@ out: return err; } +static void wait_data(int fd, bool write) +{ + struct pollfd pfd = { .fd = fd }; + + pfd.events = write ? POLLOUT : POLLIN; + poll(&pfd, 1, 0); +} + +static int writev_retry(int fd, struct iovec *iov, int iov_len) +{ + int len = 0; + + do { + int cur_len = writev(fd, iov, iov_len); + if (cur_len < 0) { + switch(errno) { + case EAGAIN: + wait_data(fd, true); + break; + case EINTR: + break; + default: + return -1; + } + continue; + } + len += cur_len; + while (cur_len >= iov->iov_len) { + cur_len -= iov->iov_len; + iov_len--; + iov++; + if (!cur_len || !iov_len) + return len; + } + iov->iov_len -= cur_len; + } while (1); +} + static int ubus_send_msg(struct ubus_context *ctx, uint32_t seq, struct blob_attr *msg, int cmd, uint32_t peer) { @@ -100,7 +155,7 @@ static int ubus_send_msg(struct ubus_context *ctx, uint32_t seq, iov[1].iov_base = (char *) msg; iov[1].iov_len = blob_raw_len(msg); - return writev(ctx->sock.fd, iov, 2); + return writev_retry(ctx->sock.fd, iov, ARRAY_SIZE(iov)); } static int ubus_start_request(struct ubus_context *ctx, struct ubus_request *req, @@ -108,6 +163,9 @@ static int ubus_start_request(struct ubus_context *ctx, struct ubus_request *req { memset(req, 0, sizeof(*req)); + if (msg && blob_pad_len(msg) > UBUS_MAX_MSGLEN) + return -1; + INIT_LIST_HEAD(&req->list); INIT_LIST_HEAD(&req->pending); req->ctx = ctx; @@ -121,6 +179,9 @@ static bool recv_retry(int fd, struct iovec *iov, bool wait) int bytes; while (iov->iov_len > 0) { + if (wait) + wait_data(fd, false); + bytes = read(fd, iov->iov_base, iov->iov_len); if (bytes < 0) { bytes = 0; @@ -129,10 +190,8 @@ static bool recv_retry(int fd, struct iovec *iov, bool wait) if (errno == EINTR) continue; - if (errno != EAGAIN) { - perror("read"); + if (errno != EAGAIN) return false; - } } if (!wait && !bytes) return false; @@ -153,19 +212,19 @@ static bool ubus_validate_hdr(struct ubus_msghdr *hdr) if (blob_raw_len(hdr->data) < sizeof(*hdr->data)) return false; - if (blob_raw_len(hdr->data) + sizeof(*hdr) > UBUS_MAX_MSGLEN) + if (blob_pad_len(hdr->data) > UBUS_MAX_MSGLEN) return false; return true; } -static bool get_next_msg(struct ubus_context *ctx, bool wait) +static bool get_next_msg(struct ubus_context *ctx) { struct iovec iov = STATIC_IOV(ctx->msgbuf.hdr); /* receive header + start attribute */ iov.iov_len += sizeof(struct blob_attr); - if (!recv_retry(ctx->sock.fd, &iov, wait)) + if (!recv_retry(ctx->sock.fd, &iov, false)) return false; iov.iov_len = blob_len(ctx->msgbuf.hdr.data); @@ -230,7 +289,7 @@ static int ubus_process_req_status(struct ubus_request *req, struct ubus_msghdr int ret = UBUS_STATUS_INVALID_ARGUMENT; if (!list_empty(&req->list)) - list_del(&req->list); + list_del_init(&req->list); ubus_get_status(hdr, &ret); req->peer = hdr->peer; @@ -290,6 +349,8 @@ static void ubus_process_invoke(struct ubus_context *ctx, struct ubus_msghdr *hd int method; int ret = 0; + req.peer = hdr->peer; + req.seq = hdr->seq; ubus_parse_msg(hdr->data); if (!attrbuf[UBUS_ATTR_OBJID]) @@ -320,21 +381,20 @@ static void ubus_process_invoke(struct ubus_context *ctx, struct ubus_msghdr *hd found: req.object = objid; - req.peer = hdr->peer; - req.seq = hdr->seq; ret = obj->methods[method].handler(ctx, obj, &req, - obj->methods[method].name, + blob_data(attrbuf[UBUS_ATTR_METHOD]), attrbuf[UBUS_ATTR_DATA]); send: blob_buf_init(&b, 0); blob_put_int32(&b, UBUS_ATTR_STATUS, ret); blob_put_int32(&b, UBUS_ATTR_OBJID, objid); - ubus_send_msg(ctx, hdr->seq, b.head, UBUS_MSG_STATUS, hdr->peer); + ubus_send_msg(ctx, req.seq, b.head, UBUS_MSG_STATUS, req.peer); } static void ubus_process_msg(struct ubus_context *ctx, struct ubus_msghdr *hdr) { + struct ubus_pending_invoke *pending; struct ubus_request *req; switch(hdr->type) { @@ -353,14 +413,36 @@ static void ubus_process_msg(struct ubus_context *ctx, struct ubus_msghdr *hdr) break; case UBUS_MSG_INVOKE: - ubus_process_invoke(ctx, hdr); - break; - default: - DPRINTF("unknown message type: %d\n", hdr->type); + if (ctx->stack_depth > 2) { + pending = calloc(1, sizeof(*pending) + + blob_raw_len(hdr->data)); + + if (!pending) + return; + + memcpy(&pending->hdr, hdr, sizeof(*hdr) + + blob_raw_len(hdr->data)); + list_add(&pending->list, &ctx->pending); + } else { + ubus_process_invoke(ctx, hdr); + } break; } } +static void ubus_process_pending_invoke(struct ubus_context *ctx) +{ + struct ubus_pending_invoke *pending, *tmp; + + list_for_each_entry_safe(pending, tmp, &ctx->pending, list) { + list_del(&pending->list); + ubus_process_msg(ctx, &pending->hdr); + free(pending); + if (ctx->stack_depth > 2) + break; + } +} + void ubus_abort_request(struct ubus_context *ctx, struct ubus_request *req) { if (!list_empty(&req->list)) @@ -368,7 +450,7 @@ void ubus_abort_request(struct ubus_context *ctx, struct ubus_request *req) req->cancelled = true; ubus_process_req_data(req); - list_del(&req->list); + list_del_init(&req->list); } void ubus_complete_request_async(struct ubus_context *ctx, struct ubus_request *req) @@ -384,47 +466,85 @@ static void ubus_handle_data(struct uloop_fd *u, unsigned int events) struct ubus_context *ctx = container_of(u, struct ubus_context, sock); struct ubus_msghdr *hdr = &ctx->msgbuf.hdr; - while (get_next_msg(ctx, false)) + while (get_next_msg(ctx)) { ubus_process_msg(ctx, hdr); + if (uloop_cancelled) + break; + } if (u->eof) ctx->connection_lost(ctx); } -int ubus_complete_request(struct ubus_context *ctx, struct ubus_request *req) +static void ubus_sync_req_cb(struct ubus_request *req, int ret) { - struct ubus_msghdr *hdr = &ctx->msgbuf.hdr; + req->status_msg = true; + req->status_code = ret; + uloop_end(); +} - if (!list_empty(&req->list)) - list_del(&req->list); +struct ubus_sync_req_cb { + struct uloop_timeout timeout; + struct ubus_request *req; +}; - while (1) { - if (req->status_msg) - return req->status_code; +static void ubus_sync_req_timeout_cb(struct uloop_timeout *timeout) +{ + struct ubus_sync_req_cb *cb; - if (req->cancelled) - return UBUS_STATUS_NO_DATA; + cb = container_of(timeout, struct ubus_sync_req_cb, timeout); + ubus_sync_req_cb(cb->req, UBUS_STATUS_TIMEOUT); +} - if (!get_next_msg(ctx, true)) - return UBUS_STATUS_NO_DATA; +int ubus_complete_request(struct ubus_context *ctx, struct ubus_request *req, + int timeout) +{ + struct ubus_sync_req_cb cb; + ubus_complete_handler_t complete_cb = req->complete_cb; + bool registered = ctx->sock.registered; + int status = UBUS_STATUS_NO_DATA; - if (hdr->seq != req->seq || hdr->peer != req->peer) - goto skip; + if (!registered) { + uloop_init(); + ubus_add_uloop(ctx); + } - switch(hdr->type) { - case UBUS_MSG_STATUS: - return ubus_process_req_status(req, hdr); - case UBUS_MSG_DATA: - if (req->data_cb || req->raw_data_cb) - ubus_req_data(req, hdr); - continue; - default: - goto skip; - } + if (timeout) { + memset(&cb, 0, sizeof(cb)); + cb.req = req; + cb.timeout.cb = ubus_sync_req_timeout_cb; + uloop_timeout_set(&cb.timeout, timeout); + } -skip: - ubus_process_msg(ctx, hdr); + ubus_complete_request_async(ctx, req); + req->complete_cb = ubus_sync_req_cb; + + ctx->stack_depth++; + while (!req->status_msg) { + bool cancelled = uloop_cancelled; + uloop_cancelled = false; + uloop_run(); + uloop_cancelled = cancelled; } + ctx->stack_depth--; + + if (timeout) + uloop_timeout_cancel(&cb.timeout); + + if (req->status_msg) + status = req->status_code; + + req->complete_cb = complete_cb; + if (req->complete_cb) + req->complete_cb(req, status); + + if (!registered) + uloop_fd_delete(&ctx->sock); + + if (!ctx->stack_depth) + ubus_process_pending_invoke(ctx); + + return status; } struct ubus_lookup_request { @@ -461,11 +581,14 @@ int ubus_lookup(struct ubus_context *ctx, const char *path, blob_buf_init(&b, 0); if (path) blob_put_string(&b, UBUS_ATTR_OBJPATH, path); - ubus_start_request(ctx, &lookup.req, b.head, UBUS_MSG_LOOKUP, 0); + + if (ubus_start_request(ctx, &lookup.req, b.head, UBUS_MSG_LOOKUP, 0) < 0) + return UBUS_STATUS_INVALID_ARGUMENT; + lookup.req.raw_data_cb = ubus_lookup_cb; lookup.req.priv = priv; lookup.cb = cb; - return ubus_complete_request(ctx, &lookup.req); + return ubus_complete_request(ctx, &lookup.req, 0); } static void ubus_lookup_id_cb(struct ubus_request *req, int type, struct blob_attr *msg) @@ -488,11 +611,14 @@ int ubus_lookup_id(struct ubus_context *ctx, const char *path, uint32_t *id) blob_buf_init(&b, 0); if (path) blob_put_string(&b, UBUS_ATTR_OBJPATH, path); - ubus_start_request(ctx, &req, b.head, UBUS_MSG_LOOKUP, 0); + + if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_LOOKUP, 0) < 0) + return UBUS_STATUS_INVALID_ARGUMENT; + req.raw_data_cb = ubus_lookup_id_cb; req.priv = id; - return ubus_complete_request(ctx, &req); + return ubus_complete_request(ctx, &req, 0); } int ubus_send_reply(struct ubus_context *ctx, struct ubus_request_data *req, @@ -510,7 +636,7 @@ int ubus_send_reply(struct ubus_context *ctx, struct ubus_request_data *req, return 0; } -void ubus_invoke_async(struct ubus_context *ctx, uint32_t obj, const char *method, +int ubus_invoke_async(struct ubus_context *ctx, uint32_t obj, const char *method, struct blob_attr *msg, struct ubus_request *req) { blob_buf_init(&b, 0); @@ -519,18 +645,22 @@ void ubus_invoke_async(struct ubus_context *ctx, uint32_t obj, const char *metho if (msg) blob_put(&b, UBUS_ATTR_DATA, blob_data(msg), blob_len(msg)); - ubus_start_request(ctx, req, b.head, UBUS_MSG_INVOKE, obj); + if (ubus_start_request(ctx, req, b.head, UBUS_MSG_INVOKE, obj) < 0) + return UBUS_STATUS_INVALID_ARGUMENT; + + return 0; } int ubus_invoke(struct ubus_context *ctx, uint32_t obj, const char *method, - struct blob_attr *msg, ubus_data_handler_t cb, void *priv) + struct blob_attr *msg, ubus_data_handler_t cb, void *priv, + int timeout) { struct ubus_request req; ubus_invoke_async(ctx, obj, method, msg, &req); req.data_cb = cb; req.priv = priv; - return ubus_complete_request(ctx, &req); + return ubus_complete_request(ctx, &req, timeout); } static void ubus_add_object_cb(struct ubus_request *req, int type, struct blob_attr *msg) @@ -551,65 +681,35 @@ static void ubus_add_object_cb(struct ubus_request *req, int type, struct blob_a avl_insert(&req->ctx->objects, &obj->avl); } -static bool ubus_push_table_data(const struct ubus_signature **sig, int *rem, bool array) +static void ubus_push_method_data(const struct ubus_method *m) { - const struct ubus_signature *cur; - bool nest_type; - void *nest; + void *mtbl; + int i; - while (rem) { - cur = (*sig)++; - (*rem)--; - switch(cur->type) { - case UBUS_SIGNATURE_END: - return !array; - case BLOBMSG_TYPE_INT32: - case BLOBMSG_TYPE_STRING: - blobmsg_add_u32(&b, cur->name, cur->type); - break; - case BLOBMSG_TYPE_TABLE: - case BLOBMSG_TYPE_ARRAY: - nest_type = cur->type == BLOBMSG_TYPE_ARRAY; - nest = blobmsg_open_nested(&b, cur->name, nest_type); - if (!ubus_push_table_data(sig, rem, nest_type)) - return false; - blobmsg_close_table(&b, nest); - break; - default: - return false; - } - if (array) - return true; - } - return false; + mtbl = blobmsg_open_table(&b, m->name); + + for (i = 0; i < m->n_policy; i++) + blobmsg_add_u32(&b, m->policy[i].name, m->policy[i].type); + + blobmsg_close_table(&b, mtbl); } -static bool ubus_push_object_type(struct ubus_object_type *type) +static bool ubus_push_object_type(const struct ubus_object_type *type) { - void *s, *m; - int rem = type->n_signature; - const struct ubus_signature *sig = type->signature; + void *s; + int i; s = blob_nest_start(&b, UBUS_ATTR_SIGNATURE); - while (rem) { - if (sig->type != UBUS_SIGNATURE_METHOD) - return false; - m = blobmsg_open_table(&b, sig->name); - - sig++; - rem--; - if (!ubus_push_table_data(&sig, &rem, false)) - return false; + for (i = 0; i < type->n_methods; i++) + ubus_push_method_data(&type->methods[i]); - blobmsg_close_table(&b, m); - } blob_nest_end(&b, s); return true; } -static int __ubus_add_object(struct ubus_context *ctx, struct ubus_object *obj) +int ubus_add_object(struct ubus_context *ctx, struct ubus_object *obj) { struct ubus_request req; int ret; @@ -625,10 +725,12 @@ static int __ubus_add_object(struct ubus_context *ctx, struct ubus_object *obj) return UBUS_STATUS_INVALID_ARGUMENT; } - ubus_start_request(ctx, &req, b.head, UBUS_MSG_ADD_OBJECT, 0); + if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_ADD_OBJECT, 0) < 0) + return UBUS_STATUS_INVALID_ARGUMENT; + req.raw_data_cb = ubus_add_object_cb; req.priv = obj; - ret = ubus_complete_request(ctx, &req); + ret = ubus_complete_request(ctx, &req, 0); if (ret) return ret; @@ -638,14 +740,6 @@ static int __ubus_add_object(struct ubus_context *ctx, struct ubus_object *obj) return 0; } -int ubus_add_object(struct ubus_context *ctx, struct ubus_object *obj) -{ - if (!obj->name || !obj->type) - return UBUS_STATUS_INVALID_ARGUMENT; - - return __ubus_add_object(ctx, obj); -} - static void ubus_remove_object_cb(struct ubus_request *req, int type, struct blob_attr *msg) { struct ubus_object *obj = req->priv; @@ -670,10 +764,13 @@ int ubus_remove_object(struct ubus_context *ctx, struct ubus_object *obj) blob_buf_init(&b, 0); blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id); - ubus_start_request(ctx, &req, b.head, UBUS_MSG_REMOVE_OBJECT, 0); + + if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_REMOVE_OBJECT, 0) < 0) + return UBUS_STATUS_INVALID_ARGUMENT; + req.raw_data_cb = ubus_remove_object_cb; req.priv = obj; - ret = ubus_complete_request(ctx, &req); + ret = ubus_complete_request(ctx, &req, 0); if (ret) return ret; @@ -714,7 +811,7 @@ int ubus_register_event_handler(struct ubus_context *ctx, if (!!obj->name ^ !!obj->type) return UBUS_STATUS_INVALID_ARGUMENT; - ret = __ubus_add_object(ctx, obj); + ret = ubus_add_object(ctx, obj); if (ret) return ret; } @@ -726,14 +823,106 @@ int ubus_register_event_handler(struct ubus_context *ctx, if (pattern) blobmsg_add_string(&b2, "pattern", pattern); - ret = ubus_invoke(ctx, UBUS_SYSTEM_OBJECT_EVENT, "register", b2.head, - NULL, NULL); + return ubus_invoke(ctx, UBUS_SYSTEM_OBJECT_EVENT, "register", b2.head, + NULL, NULL, 0); +} + +enum { + WATCH_ID, + WATCH_NOTIFY, + __WATCH_MAX +}; + +static const struct blobmsg_policy watch_policy[] = { + [WATCH_ID] = { .name = "id", .type = BLOBMSG_TYPE_INT32 }, + [WATCH_NOTIFY] = { .name = "notify", .type = BLOBMSG_TYPE_STRING }, +}; + + +static int ubus_watch_cb(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, + const char *method, struct blob_attr *msg) +{ + struct ubus_watch_object *w; + struct blob_attr *tb[__WATCH_MAX]; + + blobmsg_parse(watch_policy, ARRAY_SIZE(watch_policy), tb, blob_data(msg), blob_len(msg)); + + if (!tb[WATCH_ID] || !tb[WATCH_NOTIFY]) + return UBUS_STATUS_INVALID_ARGUMENT; + + if (req->peer) + return UBUS_STATUS_INVALID_ARGUMENT; + w = container_of(obj, struct ubus_watch_object, obj); + w->cb(ctx, w, blobmsg_get_u32(tb[WATCH_ID])); return 0; } +static const struct ubus_method watch_method = { + .name = NULL, + .handler = ubus_watch_cb, +}; + +int ubus_register_watch_object(struct ubus_context *ctx, struct ubus_watch_object *w_obj) +{ + struct ubus_object *obj = &w_obj->obj; + + obj->methods = &watch_method; + obj->n_methods = 1; + + return ubus_add_object(ctx, obj); +} + +static int +__ubus_watch_request(struct ubus_context *ctx, struct ubus_object *obj, uint32_t id, const char *method, int type) +{ + struct ubus_request req; + + blob_buf_init(&b, 0); + blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id); + blob_put_int32(&b, UBUS_ATTR_TARGET, id); + if (method) + blob_put_string(&b, UBUS_ATTR_METHOD, method); + + if (ubus_start_request(ctx, &req, b.head, type, 0) < 0) + return UBUS_STATUS_INVALID_ARGUMENT; + + return ubus_complete_request(ctx, &req, 0); + +} + +int ubus_watch_object_add(struct ubus_context *ctx, struct ubus_watch_object *obj, uint32_t id) +{ + return __ubus_watch_request(ctx, &obj->obj, id, "event", UBUS_MSG_ADD_WATCH); +} + +int ubus_watch_object_remove(struct ubus_context *ctx, struct ubus_watch_object *obj, uint32_t id) +{ + return __ubus_watch_request(ctx, &obj->obj, id, NULL, UBUS_MSG_REMOVE_WATCH); +} + +int ubus_send_event(struct ubus_context *ctx, const char *id, + struct blob_attr *data) +{ + struct ubus_request req; + void *s; + + blob_buf_init(&b, 0); + blob_put_int32(&b, UBUS_ATTR_OBJID, UBUS_SYSTEM_OBJECT_EVENT); + blob_put_string(&b, UBUS_ATTR_METHOD, "send"); + s = blob_nest_start(&b, UBUS_ATTR_DATA); + blobmsg_add_string(&b, "id", id); + blobmsg_add_field(&b, BLOBMSG_TYPE_TABLE, "data", blob_data(data), blob_len(data)); + blob_nest_end(&b, s); -void ubus_default_connection_lost(struct ubus_context *ctx) + if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_INVOKE, UBUS_SYSTEM_OBJECT_EVENT) < 0) + return UBUS_STATUS_INVALID_ARGUMENT; + + return ubus_complete_request(ctx, &req, 0); +} + +static void ubus_default_connection_lost(struct ubus_context *ctx) { if (ctx->sock.registered) uloop_end(); @@ -784,11 +973,14 @@ struct ubus_context *ubus_connect(const char *path) ctx->connection_lost = ubus_default_connection_lost; INIT_LIST_HEAD(&ctx->requests); + INIT_LIST_HEAD(&ctx->pending); avl_init(&ctx->objects, ubus_cmp_id, false, NULL); if (!ctx->local_id) goto error_close; + fcntl(ctx->sock.fd, F_SETFL, fcntl(ctx->sock.fd, F_GETFL) | O_NONBLOCK); + return ctx; error_free_buf: