pass ubus_msg_buf to callback of internal object
[project/ubus.git] / libubus-obj.c
index bdb2e03..8bc83dc 100644 (file)
@@ -46,7 +46,9 @@ 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;
@@ -89,15 +91,17 @@ 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;
 
@@ -117,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)
@@ -144,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);
 }