add Lua bindings for ubus events
[project/ubus.git] / libubus-io.c
index 4ebb8fb..19e3c2f 100644 (file)
@@ -33,6 +33,9 @@ static const struct blob_attr_info ubus_policy[UBUS_ATTR_MAX] = {
        [UBUS_ATTR_OBJID] = { .type = BLOB_ATTR_INT32 },
        [UBUS_ATTR_OBJPATH] = { .type = BLOB_ATTR_STRING },
        [UBUS_ATTR_METHOD] = { .type = BLOB_ATTR_STRING },
+       [UBUS_ATTR_ACTIVE] = { .type = BLOB_ATTR_INT8 },
+       [UBUS_ATTR_NO_REPLY] = { .type = BLOB_ATTR_INT8 },
+       [UBUS_ATTR_SUBSCRIBERS] = { .type = BLOB_ATTR_NESTED },
 };
 
 static struct blob_attr *attrbuf[UBUS_ATTR_MAX];
@@ -74,11 +77,14 @@ static int writev_retry(int fd, struct iovec *iov, int iov_len)
                        cur_len -= iov->iov_len;
                        iov_len--;
                        iov++;
-                       if (!cur_len || !iov_len)
+                       if (!iov_len)
                                return len;
                }
                iov->iov_len -= cur_len;
        } while (1);
+
+       /* Should never reach here */
+       return -1;
 }
 
 int __hidden ubus_send_msg(struct ubus_context *ctx, uint32_t seq,
@@ -88,6 +94,7 @@ int __hidden ubus_send_msg(struct ubus_context *ctx, uint32_t seq,
        struct iovec iov[2] = {
                STATIC_IOV(hdr)
        };
+       int ret;
 
        hdr.version = 0;
        hdr.type = cmd;
@@ -102,48 +109,58 @@ int __hidden ubus_send_msg(struct ubus_context *ctx, uint32_t seq,
        iov[1].iov_base = (char *) msg;
        iov[1].iov_len = blob_raw_len(msg);
 
-       return writev_retry(ctx->sock.fd, iov, ARRAY_SIZE(iov));
+       ret = writev_retry(ctx->sock.fd, iov, ARRAY_SIZE(iov));
+       if (ret < 0)
+               ctx->sock.eof = true;
+
+       return ret;
 }
 
-static bool recv_retry(int fd, struct iovec *iov, bool wait)
+static int recv_retry(int fd, struct iovec *iov, bool wait)
 {
-       int bytes;
+       int bytes, total = 0;
 
        while (iov->iov_len > 0) {
                if (wait)
                        wait_data(fd, false);
 
                bytes = read(fd, iov->iov_base, iov->iov_len);
+               if (!bytes)
+                       return -1;
+
                if (bytes < 0) {
                        bytes = 0;
                        if (uloop_cancelled)
-                               return false;
+                               return 0;
                        if (errno == EINTR)
                                continue;
 
                        if (errno != EAGAIN)
-                               return false;
+                               return -1;
                }
                if (!wait && !bytes)
-                       return false;
+                       return 0;
 
                wait = true;
                iov->iov_len -= bytes;
                iov->iov_base += bytes;
+               total += bytes;
        }
 
-       return true;
+       return total;
 }
 
 static bool ubus_validate_hdr(struct ubus_msghdr *hdr)
 {
+       struct blob_attr *data = ubus_msghdr_data(hdr);
+
        if (hdr->version != 0)
                return false;
 
-       if (blob_raw_len(hdr->data) < sizeof(*hdr->data))
+       if (blob_raw_len(data) < sizeof(*data))
                return false;
 
-       if (blob_pad_len(hdr->data) > UBUS_MAX_MSGLEN)
+       if (blob_pad_len(data) > UBUS_MAX_MSGLEN)
                return false;
 
        return true;
@@ -152,13 +169,19 @@ static bool ubus_validate_hdr(struct ubus_msghdr *hdr)
 static bool get_next_msg(struct ubus_context *ctx)
 {
        struct iovec iov = STATIC_IOV(ctx->msgbuf.hdr);
+       int r;
 
        /* receive header + start attribute */
        iov.iov_len += sizeof(struct blob_attr);
-       if (!recv_retry(ctx->sock.fd, &iov, false))
+       r = recv_retry(ctx->sock.fd, &iov, false);
+       if (r <= 0) {
+               if (r < 0)
+                       ctx->sock.eof = true;
+
                return false;
+       }
 
-       iov.iov_len = blob_len(ctx->msgbuf.hdr.data);
+       iov.iov_len = blob_len(ubus_msghdr_data(&ctx->msgbuf.hdr));
        if (iov.iov_len > 0 && !recv_retry(ctx->sock.fd, &iov, true))
                return false;
 
@@ -184,17 +207,23 @@ static void
 ubus_refresh_state(struct ubus_context *ctx)
 {
        struct ubus_object *obj, *tmp;
+       struct ubus_object **objs;
+       int n, i = 0;
 
        /* clear all type IDs, they need to be registered again */
        avl_for_each_element(&ctx->objects, obj, avl)
-               obj->type->id = 0;
+               if (obj->type)
+                       obj->type->id = 0;
 
        /* push out all objects again */
-       avl_for_each_element_safe(&ctx->objects, obj, avl, tmp) {
+       objs = alloca(ctx->objects.count * sizeof(*objs));
+       avl_remove_all_elements(&ctx->objects, obj, avl, tmp) {
+               objs[i++] = obj;
                obj->id = 0;
-               avl_delete(&ctx->objects, &obj->avl);
-               ubus_add_object(ctx, obj);
        }
+
+       for (n = i, i = 0; i < n; i++)
+               ubus_add_object(ctx, objs[i]);
 }
 
 int ubus_reconnect(struct ubus_context *ctx, const char *path)