logread: ensure that the len < sizeof(struct blob_attr) check runs before trying...
[project/ubox.git] / log / logd.c
index 8cebeb8..27d3cac 100644 (file)
@@ -55,11 +55,6 @@ client_close(struct ustream *s)
        free(cl);
 }
 
-static void
-client_notify_write(struct ustream *s, int bytes)
-{
-}
-
 static void client_notify_state(struct ustream *s)
 {
        client_close(s);
@@ -75,6 +70,7 @@ read_log(struct ubus_context *ctx, struct ubus_object *obj,
        struct log_head *l;
        int count = 0;
        int fds[2];
+       int ret;
 
        if (msg) {
                blobmsg_parse(&read_policy, 1, &tb, blob_data(msg), blob_len(msg));
@@ -88,7 +84,6 @@ read_log(struct ubus_context *ctx, struct ubus_object *obj,
        }
        ubus_request_set_fd(ctx, req, fds[0]);
        cl = calloc(1, sizeof(*cl));
-       cl->s.stream.notify_write = client_notify_write;
        cl->s.stream.notify_state = client_notify_state;
        cl->fd = fds[1];
        ustream_fd_init(&cl->s, cl->fd);
@@ -102,7 +97,9 @@ read_log(struct ubus_context *ctx, struct ubus_object *obj,
                blobmsg_add_u32(&b, "source", l->source);
                blobmsg_add_u64(&b, "time", l->ts.tv_sec * 1000LL);
                l = log_list(count, l);
-               if (ustream_write(&cl->s.stream, (void *) b.head, blob_len(b.head) + sizeof(struct blob_attr), false) <= 0)
+               ret = ustream_write(&cl->s.stream, (void *) b.head, blob_len(b.head) + sizeof(struct blob_attr), false);
+               blob_buf_free(&b);
+               if (ret < 0)
                        break;
        }
        return 0;
@@ -150,15 +147,17 @@ ubus_notify_log(struct log_head *l)
        if (list_empty(&clients))
                return;
 
-       list_for_each_entry(c, &clients, list) {
-               blob_buf_init(&b, 0);
-               blobmsg_add_string(&b, "msg", l->data);
-               blobmsg_add_u32(&b, "id", l->id);
-               blobmsg_add_u32(&b, "priority", l->priority);
-               blobmsg_add_u32(&b, "source", l->source);
-               blobmsg_add_u64(&b, "time", (((__u64) l->ts.tv_sec) * 1000) + (l->ts.tv_nsec / 1000000));
+       blob_buf_init(&b, 0);
+       blobmsg_add_string(&b, "msg", l->data);
+       blobmsg_add_u32(&b, "id", l->id);
+       blobmsg_add_u32(&b, "priority", l->priority);
+       blobmsg_add_u32(&b, "source", l->source);
+       blobmsg_add_u64(&b, "time", (((__u64) l->ts.tv_sec) * 1000) + (l->ts.tv_nsec / 1000000));
+
+       list_for_each_entry(c, &clients, list)
                ustream_write(&c->s.stream, (void *) b.head, blob_len(b.head) + sizeof(struct blob_attr), false);
-       }
+
+       blob_buf_free(&b);
 }
 
 static void