cli: document usage of "subscribe" command
[project/ubus.git] / libubus.c
index e1e68d0218d1c32807fa5daf30e698e3bf00e916..91f317c59867e57005618c3a94d835ff8d1d81fb 100644 (file)
--- a/libubus.c
+++ b/libubus.c
@@ -103,7 +103,11 @@ ubus_process_msg(struct ubus_context *ctx, struct ubus_msghdr_buf *buf, int fd)
                        break;
                }
 
-               ubus_process_obj_msg(ctx, buf);
+               ubus_process_obj_msg(ctx, buf, fd);
+               break;
+       case UBUS_MSG_MONITOR:
+               if (ctx->monitor_cb)
+                       ctx->monitor_cb(ctx, buf->hdr.seq, buf->data);
                break;
        }
 }
@@ -111,10 +115,12 @@ ubus_process_msg(struct ubus_context *ctx, struct ubus_msghdr_buf *buf, int fd)
 static void ubus_process_pending_msg(struct uloop_timeout *timeout)
 {
        struct ubus_context *ctx = container_of(timeout, struct ubus_context, pending_timer);
-       struct ubus_pending_msg *pending;
+       struct ubus_pending_msg *pending, *tmp;
+
+       list_for_each_entry_safe(pending, tmp, &ctx->pending, list) {
+               if (ctx->stack_depth)
+                       break;
 
-       while (!ctx->stack_depth && !list_empty(&ctx->pending)) {
-               pending = list_first_entry(&ctx->pending, struct ubus_pending_msg, list);
                list_del(&pending->list);
                ubus_process_msg(ctx, &pending->hdr, -1);
                free(pending);
@@ -129,17 +135,16 @@ struct ubus_lookup_request {
 static void ubus_lookup_cb(struct ubus_request *ureq, int type, struct blob_attr *msg)
 {
        struct ubus_lookup_request *req;
-       struct ubus_object_data obj;
+       struct ubus_object_data obj = {};
        struct blob_attr **attr;
 
        req = container_of(ureq, struct ubus_lookup_request, req);
-       attr = ubus_parse_msg(msg);
+       attr = ubus_parse_msg(msg, blob_raw_len(msg));
 
        if (!attr[UBUS_ATTR_OBJID] || !attr[UBUS_ATTR_OBJPATH] ||
            !attr[UBUS_ATTR_OBJTYPE])
                return;
 
-       memset(&obj, 0, sizeof(obj));
        obj.id = blob_get_u32(attr[UBUS_ATTR_OBJID]);
        obj.path = blob_data(attr[UBUS_ATTR_OBJPATH]);
        obj.type_id = blob_get_u32(attr[UBUS_ATTR_OBJTYPE]);
@@ -170,7 +175,7 @@ static void ubus_lookup_id_cb(struct ubus_request *req, int type, struct blob_at
        struct blob_attr **attr;
        uint32_t *id = req->priv;
 
-       attr = ubus_parse_msg(msg);
+       attr = ubus_parse_msg(msg, blob_raw_len(msg));
 
        if (!attr[UBUS_ATTR_OBJID])
                return;
@@ -216,7 +221,7 @@ int ubus_register_event_handler(struct ubus_context *ctx,
                                const char *pattern)
 {
        struct ubus_object *obj = &ev->obj;
-       struct blob_buf b2;
+       struct blob_buf b2 = {};
        int ret;
 
        if (!obj->id) {
@@ -232,7 +237,6 @@ int ubus_register_event_handler(struct ubus_context *ctx,
        }
 
        /* 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)
@@ -273,6 +277,7 @@ static void ubus_default_connection_lost(struct ubus_context *ctx)
 
 int ubus_connect_ctx(struct ubus_context *ctx, const char *path)
 {
+       uloop_init();
        memset(ctx, 0, sizeof(*ctx));
 
        ctx->sock.fd = -1;
@@ -280,7 +285,7 @@ int ubus_connect_ctx(struct ubus_context *ctx, const char *path)
        ctx->connection_lost = ubus_default_connection_lost;
        ctx->pending_timer.cb = ubus_process_pending_msg;
 
-       ctx->msgbuf.data = calloc(UBUS_MSG_CHUNK_SIZE, sizeof(char));
+       ctx->msgbuf.data = calloc(1, UBUS_MSG_CHUNK_SIZE);
        if (!ctx->msgbuf.data)
                return -1;
        ctx->msgbuf_data_len = UBUS_MSG_CHUNK_SIZE;
@@ -290,6 +295,7 @@ int ubus_connect_ctx(struct ubus_context *ctx, const char *path)
        avl_init(&ctx->objects, ubus_cmp_id, false, NULL);
        if (ubus_reconnect(ctx, path)) {
                free(ctx->msgbuf.data);
+               ctx->msgbuf.data = NULL;
                return -1;
        }
 
@@ -354,7 +360,10 @@ struct ubus_context *ubus_connect(const char *path)
 void ubus_shutdown(struct ubus_context *ctx)
 {
        blob_buf_free(&b);
+       if (!ctx)
+               return;
        close(ctx->sock.fd);
+       uloop_timeout_cancel(&ctx->pending_timer);
        free(ctx->msgbuf.data);
 }