libubus: expose ubus_connect_ctx() in public API
[project/ubus.git] / libubus-obj.c
index c35e075..8bc83dc 100644 (file)
@@ -27,7 +27,8 @@ ubus_process_unsubscribe(struct ubus_context *ctx, struct ubus_msghdr *hdr,
                return;
 
        s = container_of(obj, struct ubus_subscriber, obj);
-       s->remove_cb(ctx, s, blob_get_u32(attrbuf[UBUS_ATTR_TARGET]));
+       if (s->remove_cb)
+               s->remove_cb(ctx, s, blob_get_u32(attrbuf[UBUS_ATTR_TARGET]));
 }
 
 static void
@@ -45,9 +46,12 @@ static void
 ubus_process_invoke(struct ubus_context *ctx, struct ubus_msghdr *hdr,
                    struct ubus_object *obj, struct blob_attr **attrbuf)
 {
-       struct ubus_request_data req = {};
+       struct ubus_request_data req = {
+               .fd = -1,
+       };
        int method;
        int ret;
+       bool no_reply = false;
 
        if (!obj) {
                ret = UBUS_STATUS_NOT_FOUND;
@@ -59,6 +63,9 @@ ubus_process_invoke(struct ubus_context *ctx, struct ubus_msghdr *hdr,
                goto send;
        }
 
+       if (attrbuf[UBUS_ATTR_NO_REPLY])
+               no_reply = blob_get_int8(attrbuf[UBUS_ATTR_NO_REPLY]);
+
        req.peer = hdr->peer;
        req.seq = hdr->seq;
        req.object = obj->id;
@@ -77,22 +84,24 @@ found:
        ret = obj->methods[method].handler(ctx, obj, &req,
                                           blob_data(attrbuf[UBUS_ATTR_METHOD]),
                                           attrbuf[UBUS_ATTR_DATA]);
-       if (req.deferred)
+       if (req.deferred || no_reply)
                return;
 
 send:
        ubus_complete_deferred_request(ctx, &req, ret);
 }
 
-void __hidden ubus_process_obj_msg(struct ubus_context *ctx, struct ubus_msghdr *hdr)
+void __hidden ubus_process_obj_msg(struct ubus_context *ctx, struct ubus_msghdr_buf *buf)
 {
        void (*cb)(struct ubus_context *, struct ubus_msghdr *,
                   struct ubus_object *, struct blob_attr **);
+       struct ubus_msghdr *hdr = &buf->hdr;
        struct blob_attr **attrbuf;
        struct ubus_object *obj;
        uint32_t objid;
+       void *prev_data = NULL;
 
-       attrbuf = ubus_parse_msg(hdr->data);
+       attrbuf = ubus_parse_msg(buf->data);
        if (!attrbuf[UBUS_ATTR_OBJID])
                return;
 
@@ -112,7 +121,20 @@ void __hidden ubus_process_obj_msg(struct ubus_context *ctx, struct ubus_msghdr
        default:
                return;
        }
+
+       if (buf == &ctx->msgbuf) {
+               prev_data = buf->data;
+               buf->data = NULL;
+       }
+
        cb(ctx, hdr, obj, attrbuf);
+
+       if (prev_data) {
+               if (buf->data)
+                       free(prev_data);
+               else
+                       buf->data = prev_data;
+       }
 }
 
 static void ubus_add_object_cb(struct ubus_request *req, int type, struct blob_attr *msg)
@@ -139,8 +161,12 @@ static void ubus_push_method_data(const struct ubus_method *m)
 
        mtbl = blobmsg_open_table(&b, m->name);
 
-       for (i = 0; i < m->n_policy; i++)
+       for (i = 0; i < m->n_policy; i++) {
+               if (m->mask && !(m->mask & (1 << i)))
+                       continue;
+
                blobmsg_add_u32(&b, m->policy[i].name, m->policy[i].type);
+       }
 
        blobmsg_close_table(&b, mtbl);
 }