X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fubus.git;a=blobdiff_plain;f=libubus.c;h=1ebc4214403e705d0f5c3d8d2476e2c3eb023836;hp=c1a1010cc0ba788b734ee74e2c0669f092914872;hb=2378e900abe8038c4fd384259f97bd85e0e8d078;hpb=36b1a80b075203c650a6612f1ef45674210fb7a6 diff --git a/libubus.c b/libubus.c index c1a1010..1ebc421 100644 --- a/libubus.c +++ b/libubus.c @@ -10,14 +10,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] = { @@ -27,6 +19,7 @@ const char *__ubus_strerror[__UBUS_STATUS_LAST] = { [UBUS_STATUS_METHOD_NOT_FOUND] = "Method not found", [UBUS_STATUS_NOT_FOUND] = "Not found", [UBUS_STATUS_NO_DATA] = "No response", + [UBUS_STATUS_PERMISSION_DENIED] = "Permission denied", }; static struct blob_buf b; @@ -55,7 +48,7 @@ static int ubus_cmp_id(const void *k1, const void *k2, void *ptr) return *id1 > *id2; } -struct blob_attr **ubus_parse_msg(struct blob_attr *msg) +static struct blob_attr **ubus_parse_msg(struct blob_attr *msg) { blob_parse(msg, attrbuf, ubus_policy, UBUS_ATTR_MAX); return attrbuf; @@ -107,6 +100,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; @@ -123,6 +119,8 @@ static bool recv_retry(int fd, struct iovec *iov, bool wait) bytes = read(fd, iov->iov_base, iov->iov_len); if (bytes < 0) { bytes = 0; + if (uloop_cancelled) + return false; if (errno == EINTR) continue; @@ -150,7 +148,7 @@ 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; @@ -179,7 +177,7 @@ static bool ubus_get_status(struct ubus_msghdr *hdr, int *ret) if (!attrbuf[UBUS_ATTR_STATUS]) return false; - *ret = blob_get_int32(attrbuf[UBUS_ATTR_STATUS]); + *ret = blob_get_u32(attrbuf[UBUS_ATTR_STATUS]); return true; } @@ -292,7 +290,7 @@ static void ubus_process_invoke(struct ubus_context *ctx, struct ubus_msghdr *hd if (!attrbuf[UBUS_ATTR_OBJID]) return; - objid = blob_get_int32(attrbuf[UBUS_ATTR_OBJID]); + objid = blob_get_u32(attrbuf[UBUS_ATTR_OBJID]); if (!attrbuf[UBUS_ATTR_METHOD]) { ret = UBUS_STATUS_INVALID_ARGUMENT; @@ -306,7 +304,8 @@ static void ubus_process_invoke(struct ubus_context *ctx, struct ubus_msghdr *hd } for (method = 0; method < obj->n_methods; method++) - if (!strcmp(obj->methods[method].name, + if (!obj->methods[method].name || + !strcmp(obj->methods[method].name, blob_data(attrbuf[UBUS_ATTR_METHOD]))) goto found; @@ -319,7 +318,7 @@ found: 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: @@ -351,9 +350,6 @@ static void ubus_process_msg(struct ubus_context *ctx, struct ubus_msghdr *hdr) case UBUS_MSG_INVOKE: ubus_process_invoke(ctx, hdr); break; - default: - DPRINTF("unknown message type: %d\n", hdr->type); - break; } } @@ -442,9 +438,9 @@ static void ubus_lookup_cb(struct ubus_request *ureq, int type, struct blob_attr return; memset(&obj, 0, sizeof(obj)); - obj.id = blob_get_int32(attr[UBUS_ATTR_OBJID]); + obj.id = blob_get_u32(attr[UBUS_ATTR_OBJID]); obj.path = blob_data(attr[UBUS_ATTR_OBJPATH]); - obj.type_id = blob_get_int32(attr[UBUS_ATTR_OBJTYPE]); + obj.type_id = blob_get_u32(attr[UBUS_ATTR_OBJTYPE]); obj.signature = attr[UBUS_ATTR_SIGNATURE]; req->cb(ureq->ctx, &obj, ureq->priv); } @@ -457,7 +453,10 @@ 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; @@ -474,7 +473,7 @@ static void ubus_lookup_id_cb(struct ubus_request *req, int type, struct blob_at if (!attr[UBUS_ATTR_OBJID]) return; - *id = blob_get_int32(attr[UBUS_ATTR_OBJID]); + *id = blob_get_u32(attr[UBUS_ATTR_OBJID]); } int ubus_lookup_id(struct ubus_context *ctx, const char *path, uint32_t *id) @@ -484,7 +483,10 @@ 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; @@ -506,7 +508,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); @@ -515,7 +517,10 @@ 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, @@ -529,7 +534,7 @@ int ubus_invoke(struct ubus_context *ctx, uint32_t obj, const char *method, return ubus_complete_request(ctx, &req); } -static void ubus_publish_cb(struct ubus_request *req, int type, struct blob_attr *msg) +static void ubus_add_object_cb(struct ubus_request *req, int type, struct blob_attr *msg) { struct ubus_object *obj = req->priv; @@ -538,10 +543,10 @@ static void ubus_publish_cb(struct ubus_request *req, int type, struct blob_attr if (!attrbuf[UBUS_ATTR_OBJID]) return; - obj->id = blob_get_int32(attrbuf[UBUS_ATTR_OBJID]); + obj->id = blob_get_u32(attrbuf[UBUS_ATTR_OBJID]); if (attrbuf[UBUS_ATTR_OBJTYPE]) - obj->type->id = blob_get_int32(attrbuf[UBUS_ATTR_OBJTYPE]); + obj->type->id = blob_get_u32(attrbuf[UBUS_ATTR_OBJTYPE]); obj->avl.key = &obj->id; avl_insert(&req->ctx->objects, &obj->avl); @@ -605,36 +610,155 @@ static bool ubus_push_object_type(struct ubus_object_type *type) return true; } -int ubus_publish(struct ubus_context *ctx, struct ubus_object *obj) +static int __ubus_add_object(struct ubus_context *ctx, struct ubus_object *obj) { struct ubus_request req; int ret; - if (obj->id || !obj->name || !obj->type) + blob_buf_init(&b, 0); + + if (obj->name && obj->type) { + blob_put_string(&b, UBUS_ATTR_OBJPATH, obj->name); + + if (obj->type->id) + blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id); + else if (!ubus_push_object_type(obj->type)) + return UBUS_STATUS_INVALID_ARGUMENT; + } + + 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); + if (ret) + return ret; + + if (!obj->id) + return UBUS_STATUS_NO_DATA; + + 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; + + ubus_parse_msg(msg); + + if (!attrbuf[UBUS_ATTR_OBJID]) + return; + + obj->id = 0; + + if (attrbuf[UBUS_ATTR_OBJTYPE] && obj->type) + obj->type->id = 0; + + avl_delete(&req->ctx->objects, &obj->avl); +} + +int ubus_remove_object(struct ubus_context *ctx, struct ubus_object *obj) +{ + struct ubus_request req; + int ret; + blob_buf_init(&b, 0); - blob_put_string(&b, UBUS_ATTR_OBJPATH, obj->name); + blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id); - if (obj->type->id) - blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id); - else if (!ubus_push_object_type(obj->type)) + if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_REMOVE_OBJECT, 0) < 0) return UBUS_STATUS_INVALID_ARGUMENT; - ubus_start_request(ctx, &req, b.head, UBUS_MSG_PUBLISH, 0); - req.raw_data_cb = ubus_publish_cb; + req.raw_data_cb = ubus_remove_object_cb; req.priv = obj; ret = ubus_complete_request(ctx, &req); if (ret) return ret; - if (!obj->id) + if (obj->id) return UBUS_STATUS_NO_DATA; return 0; } -void ubus_default_connection_lost(struct ubus_context *ctx) +static int ubus_event_cb(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, + const char *method, struct blob_attr *msg) +{ + struct ubus_event_handler *ev; + + ev = container_of(obj, struct ubus_event_handler, obj); + ev->cb(ctx, ev, method, msg); + return 0; +} + +static const struct ubus_method event_method = { + .name = NULL, + .handler = ubus_event_cb, +}; + +int ubus_register_event_handler(struct ubus_context *ctx, + struct ubus_event_handler *ev, + const char *pattern) +{ + struct ubus_object *obj = &ev->obj; + struct blob_buf b2; + int ret; + + if (!obj->id) { + obj->methods = &event_method; + obj->n_methods = 1; + + if (!!obj->name ^ !!obj->type) + return UBUS_STATUS_INVALID_ARGUMENT; + + ret = __ubus_add_object(ctx, obj); + if (ret) + return ret; + } + + /* use a second buffer, ubus_invoke() overwrites the primary one */ + memset(&b2, 0, sizeof(b2)); + blob_buf_init(&b2, 0); + blobmsg_add_u32(&b2, "object", obj->id); + if (pattern) + blobmsg_add_string(&b2, "pattern", pattern); + + ret = ubus_invoke(ctx, UBUS_SYSTEM_OBJECT_EVENT, "register", b2.head, + NULL, NULL); + + return 0; +} + +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); + + 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); +} + +static void ubus_default_connection_lost(struct ubus_context *ctx) { if (ctx->sock.registered) uloop_end(); @@ -657,36 +781,27 @@ struct ubus_context *ubus_connect(const char *path) goto error; ctx->sock.fd = usock(USOCK_UNIX, path, NULL); - if (ctx->sock.fd < 0) { - DPRINTF("Failed to connect to server\n"); + if (ctx->sock.fd < 0) goto error_free; - } + ctx->sock.cb = ubus_handle_data; - if (read(ctx->sock.fd, &hdr, sizeof(hdr)) != sizeof(hdr)) { - DPRINTF("Failed to read initial message data\n"); + if (read(ctx->sock.fd, &hdr, sizeof(hdr)) != sizeof(hdr)) goto error_close; - } - if (!ubus_validate_hdr(&hdr.hdr)) { - DPRINTF("Failed to validate initial message header\n"); + if (!ubus_validate_hdr(&hdr.hdr)) goto error_close; - } - if (hdr.hdr.type != UBUS_MSG_HELLO) { - DPRINTF("Unexpected initial message\n"); + if (hdr.hdr.type != UBUS_MSG_HELLO) goto error_close; - } buf = calloc(1, blob_raw_len(&hdr.data)); if (!buf) goto error_close; memcpy(buf, &hdr.data, sizeof(hdr.data)); - if (read(ctx->sock.fd, blob_data(buf), blob_len(buf)) != blob_len(buf)) { - DPRINTF("Failed to retrieve initial message data\n"); + if (read(ctx->sock.fd, blob_data(buf), blob_len(buf)) != blob_len(buf)) goto error_free_buf; - } ctx->local_id = hdr.hdr.peer; free(buf); @@ -696,10 +811,8 @@ struct ubus_context *ubus_connect(const char *path) INIT_LIST_HEAD(&ctx->requests); avl_init(&ctx->objects, ubus_cmp_id, false, NULL); - if (!ctx->local_id) { - DPRINTF("Failed to get local peer id\n"); + if (!ctx->local_id) goto error_close; - } return ctx;