fix initial object event sequence number
[project/ubus.git] / ubusd_event.c
index a5ff09e..223ca4d 100644 (file)
@@ -1,10 +1,11 @@
+#include <arpa/inet.h>
 #include "ubusd.h"
 
 static struct avl_tree patterns;
 static LIST_HEAD(catch_all);
 static struct ubus_object *event_obj;
 static int event_seq = 0;
-static int obj_event_seq = 0;
+static int obj_event_seq = 1;
 
 enum evs_type {
        EVS_PATTERN,
@@ -129,20 +130,11 @@ static int ubusd_alloc_event_pattern(struct ubus_client *cl, struct blob_attr *m
        return 0;
 }
 
-enum {
-       EVMSG_ID,
-       EVMSG_DATA,
-       EVMSG_LAST,
-};
-
-static struct blobmsg_policy ev_policy[] = {
-       [EVMSG_ID] = { .name = "id", .type = BLOBMSG_TYPE_STRING },
-       [EVMSG_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
-};
+typedef struct ubus_msg_buf *(*event_fill_cb)(void *priv, const char *id);
 
 static void ubusd_send_event_msg(struct ubus_msg_buf **ub, struct ubus_client *cl,
                                 struct ubus_object *obj, const char *id,
-                                struct blob_attr *msg)
+                                event_fill_cb fill_cb, void *cb_priv)
 {
        uint32_t *objid_ptr;
 
@@ -156,20 +148,15 @@ static void ubusd_send_event_msg(struct ubus_msg_buf **ub, struct ubus_client *c
 
        obj->event_seen = obj_event_seq;
 
-       if (*ub) {
-               objid_ptr = blob_data(blob_data((*ub)->data));
-               *objid_ptr = htonl(obj->id.id);
-       } else {
-               blob_buf_init(&b, 0);
-               blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
-               blob_put_string(&b, UBUS_ATTR_METHOD, id);
-               blob_put(&b, UBUS_ATTR_DATA, blobmsg_data(msg), blobmsg_data_len(msg));
-
-               *ub = ubus_msg_new(b.head, blob_raw_len(b.head), true);
-
+       if (!*ub) {
+               *ub = fill_cb(cb_priv, id);
                (*ub)->hdr.type = UBUS_MSG_INVOKE;
                (*ub)->hdr.peer = 0;
        }
+
+       objid_ptr = blob_data(blob_data((*ub)->data));
+       *objid_ptr = htonl(obj->id.id);
+
        (*ub)->hdr.seq = ++event_seq;
        ubus_msg_send(obj->client, *ub, false);
 }
@@ -183,24 +170,15 @@ bool strmatch_len(const char *s1, const char *s2, int *len)
        return false;
 }
 
-static int ubusd_send_event(struct ubus_client *cl, struct blob_attr *msg)
+static int ubusd_send_event(struct ubus_client *cl, const char *id,
+                           event_fill_cb fill_cb, void *cb_priv)
 {
        struct ubus_msg_buf *ub = NULL;
        struct event_source *ev;
-       struct blob_attr *attr[EVMSG_LAST];
-       const char *id;
        int match_len = 0;
-       void *data;
-
-       blobmsg_parse(ev_policy, EVMSG_LAST, attr, blob_data(msg), blob_len(msg));
-       if (!attr[EVMSG_ID] || !attr[EVMSG_DATA])
-               return UBUS_STATUS_INVALID_ARGUMENT;
-
-       id = blobmsg_data(attr[EVMSG_ID]);
-       data = attr[EVMSG_DATA];
 
        list_for_each_entry(ev, &catch_all, catchall.list)
-               ubusd_send_event_msg(&ub, cl, ev->obj, id, data);
+               ubusd_send_event_msg(&ub, cl, ev->obj, id, fill_cb, cb_priv);
 
        obj_event_seq++;
 
@@ -228,7 +206,7 @@ static int ubusd_send_event(struct ubus_client *cl, struct blob_attr *msg)
                                continue;
                }
 
-               ubusd_send_event_msg(&ub, cl, ev->obj, id, data);
+               ubusd_send_event_msg(&ub, cl, ev->obj, id, fill_cb, cb_priv);
        }
 
        if (ub)
@@ -237,17 +215,84 @@ static int ubusd_send_event(struct ubus_client *cl, struct blob_attr *msg)
        return 0;
 }
 
+enum {
+       EVMSG_ID,
+       EVMSG_DATA,
+       EVMSG_LAST,
+};
+
+static struct blobmsg_policy ev_policy[] = {
+       [EVMSG_ID] = { .name = "id", .type = BLOBMSG_TYPE_STRING },
+       [EVMSG_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
+};
+
+static struct ubus_msg_buf *
+ubusd_create_event_from_msg(void *priv, const char *id)
+{
+       struct blob_attr *msg = priv;
+
+       blob_buf_init(&b, 0);
+       blob_put_int32(&b, UBUS_ATTR_OBJID, 0);
+       blob_put_string(&b, UBUS_ATTR_METHOD, id);
+       blob_put(&b, UBUS_ATTR_DATA, blobmsg_data(msg), blobmsg_data_len(msg));
+
+       return ubus_msg_new(b.head, blob_raw_len(b.head), true);
+}
+
+static int ubusd_forward_event(struct ubus_client *cl, struct blob_attr *msg)
+{
+       struct blob_attr *data;
+       struct blob_attr *attr[EVMSG_LAST];
+       const char *id;
+
+       blobmsg_parse(ev_policy, EVMSG_LAST, attr, blob_data(msg), blob_len(msg));
+       if (!attr[EVMSG_ID] || !attr[EVMSG_DATA])
+               return UBUS_STATUS_INVALID_ARGUMENT;
+
+       id = blobmsg_data(attr[EVMSG_ID]);
+       data = attr[EVMSG_DATA];
+
+       if (!strncmp(id, "ubus.", 5))
+               return UBUS_STATUS_PERMISSION_DENIED;
+
+       return ubusd_send_event(cl, id, ubusd_create_event_from_msg, data);
+}
+
 static int ubusd_event_recv(struct ubus_client *cl, const char *method, struct blob_attr *msg)
 {
        if (!strcmp(method, "register"))
                return ubusd_alloc_event_pattern(cl, msg);
 
        if (!strcmp(method, "send"))
-               return ubusd_send_event(cl, msg);
+               return ubusd_forward_event(cl, msg);
 
        return UBUS_STATUS_INVALID_COMMAND;
 }
 
+static struct ubus_msg_buf *
+ubusd_create_object_event_msg(void *priv, const char *id)
+{
+       struct ubus_object *obj = priv;
+       void *s;
+
+       blob_buf_init(&b, 0);
+       blob_put_int32(&b, UBUS_ATTR_OBJID, 0);
+       blob_put_string(&b, UBUS_ATTR_METHOD, id);
+       s = blob_nest_start(&b, UBUS_ATTR_DATA);
+       blobmsg_add_u32(&b, "id", obj->id.id);
+       blobmsg_add_string(&b, "path", obj->path.key);
+       blob_nest_end(&b, s);
+
+       return ubus_msg_new(b.head, blob_raw_len(b.head), true);
+}
+
+void ubusd_send_obj_event(struct ubus_object *obj, bool add)
+{
+       const char *id = add ? "ubus.object.add" : "ubus.object.remove";
+
+       ubusd_send_event(NULL, id, ubusd_create_object_event_msg, obj);
+}
+
 void ubusd_event_init(void)
 {
        ubus_init_string_tree(&patterns, true);