CMakeLists.txt: bump minimum cmake version
[project/ubus.git] / libubus-req.c
index 3eeae40297fc042fc2132acecb16d431a14c4253..ae9d1925ecdf1845f5d536b50240216f9ef56fb4 100644 (file)
@@ -31,17 +31,18 @@ static void req_data_cb(struct ubus_request *req, int type, struct blob_attr *da
        if (!req->data_cb)
                return;
 
-       attr = ubus_parse_msg(data);
+       attr = ubus_parse_msg(data, blob_raw_len(data));
+       if (!attr[UBUS_ATTR_DATA])
+               return;
+
        req->data_cb(req, type, attr[UBUS_ATTR_DATA]);
 }
 
 static void __ubus_process_req_data(struct ubus_request *req)
 {
-       struct ubus_pending_data *data;
+       struct ubus_pending_data *data, *tmp;
 
-       while (!list_empty(&req->pending)) {
-               data = list_first_entry(&req->pending,
-                       struct ubus_pending_data, list);
+       list_for_each_entry_safe(data, tmp, &req->pending, list) {
                list_del(&data->list);
                if (!req->cancelled)
                        req_data_cb(req, data->type, data->data);
@@ -122,7 +123,7 @@ static void ubus_sync_req_cb(struct ubus_request *req, int ret)
 {
        req->status_msg = true;
        req->status_code = ret;
-       uloop_end();
+       req->ctx->cancel_poll = true;
 }
 
 static int64_t get_time_msec(void)
@@ -140,15 +141,9 @@ int ubus_complete_request(struct ubus_context *ctx, struct ubus_request *req,
                          int req_timeout)
 {
        ubus_complete_handler_t complete_cb = req->complete_cb;
-       bool registered = ctx->sock.registered;
        int status = UBUS_STATUS_NO_DATA;
        int64_t timeout = 0, time_end = 0;
 
-       if (!registered) {
-               uloop_init();
-               ubus_add_uloop(ctx);
-       }
-
        if (req_timeout)
                time_end = get_time_msec() + req_timeout;
 
@@ -157,28 +152,26 @@ int ubus_complete_request(struct ubus_context *ctx, struct ubus_request *req,
 
        ctx->stack_depth++;
        while (!req->status_msg) {
-               bool cancelled = uloop_cancelled;
-
-               uloop_cancelled = false;
                if (req_timeout) {
                        timeout = time_end - get_time_msec();
                        if (timeout <= 0) {
                                ubus_set_req_status(req, UBUS_STATUS_TIMEOUT);
-                               uloop_cancelled = cancelled;
                                break;
                        }
                }
+
                ubus_poll_data(ctx, (unsigned int) timeout);
 
-               uloop_cancelled = cancelled;
                if (ctx->sock.eof) {
                        ubus_set_req_status(req, UBUS_STATUS_CONNECTION_FAILED);
+                       ctx->cancel_poll = true;
                        break;
                }
        }
+
        ctx->stack_depth--;
        if (ctx->stack_depth)
-               uloop_cancelled = true;
+               ctx->cancel_poll = true;
 
        if (req->status_msg)
                status = req->status_code;
@@ -187,12 +180,8 @@ int ubus_complete_request(struct ubus_context *ctx, struct ubus_request *req,
        if (req->complete_cb)
                req->complete_cb(req, status);
 
-       if (!registered) {
-               uloop_fd_delete(&ctx->sock);
-
-               if (!ctx->stack_depth)
-                       ctx->pending_timer.cb(&ctx->pending_timer);
-       }
+       if (!ctx->stack_depth && !ctx->sock.registered)
+               ctx->pending_timer.cb(&ctx->pending_timer);
 
        return status;
 }
@@ -205,6 +194,14 @@ void ubus_complete_deferred_request(struct ubus_context *ctx, struct ubus_reques
        ubus_send_msg(ctx, req->seq, b.head, UBUS_MSG_STATUS, req->peer, req->fd);
 }
 
+static void ubus_put_data(struct blob_buf *buf, struct blob_attr *msg)
+{
+       if (msg)
+               blob_put(buf, UBUS_ATTR_DATA, blob_data(msg), blob_len(msg));
+       else
+               blob_put(buf, UBUS_ATTR_DATA, NULL, 0);
+}
+
 int ubus_send_reply(struct ubus_context *ctx, struct ubus_request_data *req,
                    struct blob_attr *msg)
 {
@@ -212,7 +209,7 @@ int ubus_send_reply(struct ubus_context *ctx, struct ubus_request_data *req,
 
        blob_buf_init(&b, 0);
        blob_put_int32(&b, UBUS_ATTR_OBJID, req->object);
-       blob_put(&b, UBUS_ATTR_DATA, blob_data(msg), blob_len(msg));
+       ubus_put_data(&b, msg);
        ret = ubus_send_msg(ctx, req->seq, b.head, UBUS_MSG_DATA, req->peer, -1);
        if (ret < 0)
                return UBUS_STATUS_NO_DATA;
@@ -227,8 +224,7 @@ int ubus_invoke_async_fd(struct ubus_context *ctx, uint32_t obj,
        blob_buf_init(&b, 0);
        blob_put_int32(&b, UBUS_ATTR_OBJID, obj);
        blob_put_string(&b, UBUS_ATTR_METHOD, method);
-       if (msg)
-               blob_put(&b, UBUS_ATTR_DATA, blob_data(msg), blob_len(msg));
+       ubus_put_data(&b, msg);
 
        memset(req, 0, sizeof(*req));
        req->fd = fd;
@@ -265,6 +261,18 @@ ubus_notify_complete_cb(struct ubus_request *req, int ret)
        nreq->complete_cb(nreq, 0, 0);
 }
 
+static void
+ubus_notify_data_cb(struct ubus_request *req, int type, struct blob_attr *msg)
+{
+       struct ubus_notify_request *nreq;
+
+       nreq = container_of(req, struct ubus_notify_request, req);
+       if (!nreq->data_cb)
+               return;
+
+       nreq->data_cb(nreq, type, msg);
+}
+
 static int
 __ubus_notify_async(struct ubus_context *ctx, struct ubus_object *obj,
                    const char *type, struct blob_attr *msg,
@@ -275,13 +283,11 @@ __ubus_notify_async(struct ubus_context *ctx, struct ubus_object *obj,
        blob_buf_init(&b, 0);
        blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id);
        blob_put_string(&b, UBUS_ATTR_METHOD, type);
+       ubus_put_data(&b, msg);
 
        if (!reply)
                blob_put_int8(&b, UBUS_ATTR_NO_REPLY, true);
 
-       if (msg)
-               blob_put(&b, UBUS_ATTR_DATA, blob_data(msg), blob_len(msg));
-
        if (ubus_start_request(ctx, &req->req, b.head, UBUS_MSG_NOTIFY, obj->id) < 0)
                return UBUS_STATUS_INVALID_ARGUMENT;
 
@@ -290,6 +296,7 @@ __ubus_notify_async(struct ubus_context *ctx, struct ubus_object *obj,
        req->pending = 1;
        req->id[0] = obj->id;
        req->req.complete_cb = ubus_notify_complete_cb;
+       req->req.data_cb = ubus_notify_data_cb;
 
        return 0;
 }
@@ -321,7 +328,7 @@ int ubus_notify(struct ubus_context *ctx, struct ubus_object *obj,
 
 static bool ubus_get_status(struct ubus_msghdr_buf *buf, int *ret)
 {
-       struct blob_attr **attrbuf = ubus_parse_msg(buf->data);
+       struct blob_attr **attrbuf = ubus_parse_msg(buf->data, blob_raw_len(buf->data));
 
        if (!attrbuf[UBUS_ATTR_STATUS])
                return false;
@@ -419,7 +426,8 @@ static void ubus_process_notify_status(struct ubus_request *req, int id, struct
        struct ubus_notify_request *nreq;
        struct blob_attr **tb;
        struct blob_attr *cur;
-       int rem, idx = 1;
+       size_t rem;
+       int idx = 1;
        int ret = 0;
 
        nreq = container_of(req, struct ubus_notify_request, req);
@@ -427,7 +435,7 @@ static void ubus_process_notify_status(struct ubus_request *req, int id, struct
 
        if (!id) {
                /* first id: ubusd's status message with a list of ids */
-               tb = ubus_parse_msg(buf->data);
+               tb = ubus_parse_msg(buf->data, blob_raw_len(buf->data));
                if (tb[UBUS_ATTR_SUBSCRIBERS]) {
                        blob_for_each_attr(cur, tb[UBUS_ATTR_SUBSCRIBERS], rem) {
                                if (!blob_check_type(blob_data(cur), blob_len(cur), BLOB_ATTR_INT32))