trim the wildcard of partial patterns to keep the avl tree sorted properly
[project/ubus.git] / ubusd_event.c
1 #include "ubusd.h"
2
3 static struct avl_tree patterns;
4 static LIST_HEAD(catch_all);
5 static struct ubus_object *event_obj;
6 static int event_seq = 0;
7
8 enum evs_type {
9         EVS_PATTERN,
10         EVS_CATCHALL
11 };
12
13 struct event_source {
14         struct list_head list;
15         struct ubus_object *obj;
16         enum evs_type type;
17         union {
18                 struct {
19                         struct avl_node avl;
20                         bool partial;
21                 } pattern;
22                 struct {
23                         struct list_head list;
24                 } catchall;
25         };
26 };
27
28 static void ubusd_delete_event_source(struct event_source *evs)
29 {
30         list_del(&evs->list);
31         switch (evs->type) {
32         case EVS_PATTERN:
33                 avl_delete(&patterns, &evs->pattern.avl);
34                 break;
35         case EVS_CATCHALL:
36                 list_del(&evs->catchall.list);
37                 break;
38         }
39         free(evs);
40 }
41
42 void ubusd_event_cleanup_object(struct ubus_object *obj)
43 {
44         struct event_source *ev;
45
46         while (!list_empty(&obj->events)) {
47                 ev = list_first_entry(&obj->events, struct event_source, list);
48                 ubusd_delete_event_source(ev);
49         }
50 }
51
52 enum {
53         EVREG_PATTERN,
54         EVREG_OBJECT,
55         EVREG_LAST,
56 };
57
58 static struct blobmsg_policy evr_policy[] = {
59         [EVREG_PATTERN] = { .name = "pattern", .type = BLOBMSG_TYPE_STRING },
60         [EVREG_OBJECT] = { .name = "object", .type = BLOBMSG_TYPE_INT32 },
61 };
62
63
64 static struct event_source *ubusd_alloc_event_source(struct ubus_object *obj, enum evs_type type, int datalen)
65 {
66         struct event_source *evs;
67
68         evs = calloc(1, sizeof(*evs) + datalen);
69         list_add(&evs->list, &obj->events);
70         evs->obj = obj;
71         evs->type = type;
72         return evs;
73 }
74
75 static int ubusd_alloc_catchall(struct ubus_object *obj)
76 {
77         struct event_source *evs;
78
79         evs = ubusd_alloc_event_source(obj, EVS_CATCHALL, 0);
80         list_add(&evs->catchall.list, &catch_all);
81
82         return 0;
83 }
84
85 static int ubusd_alloc_event_pattern(struct ubus_client *cl, struct blob_attr *msg)
86 {
87         struct event_source *ev;
88         struct ubus_object *obj;
89         struct blob_attr *attr[EVREG_LAST];
90         char *pattern;
91         uint32_t id;
92         bool partial = false;
93         int len;
94
95         blobmsg_parse(evr_policy, EVREG_LAST, attr, blob_data(msg), blob_len(msg));
96         if (!attr[EVREG_OBJECT])
97                 return UBUS_STATUS_INVALID_ARGUMENT;
98
99         id = blobmsg_get_u32(attr[EVREG_OBJECT]);
100         if (id < UBUS_SYSTEM_OBJECT_MAX)
101                 return UBUS_STATUS_PERMISSION_DENIED;
102
103         obj = ubusd_find_object(id);
104         if (!obj)
105                 return UBUS_STATUS_NOT_FOUND;
106
107         if (obj->client != cl)
108                 return UBUS_STATUS_PERMISSION_DENIED;
109
110         if (!attr[EVREG_PATTERN])
111                 return ubusd_alloc_catchall(obj);
112
113         pattern = blobmsg_data(attr[EVREG_PATTERN]);
114
115         len = strlen(pattern);
116         if (pattern[len - 1] == '*') {
117                 partial = true;
118                 pattern[len - 1] = 0;
119                 len--;
120         }
121
122         ev = ubusd_alloc_event_source(obj, EVS_PATTERN, len + 1);
123         ev->pattern.partial = partial;
124         ev->pattern.avl.key = (void *) (ev + 1);
125         strcpy(ev->pattern.avl.key, pattern);
126         avl_insert(&patterns, &ev->pattern.avl);
127
128         return 0;
129 }
130
131 enum {
132         EVMSG_ID,
133         EVMSG_DATA,
134         EVMSG_LAST,
135 };
136
137 static struct blobmsg_policy ev_policy[] = {
138         [EVMSG_ID] = { .name = "id", .type = BLOBMSG_TYPE_STRING },
139         [EVMSG_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
140 };
141
142 static void ubusd_send_event_msg(struct ubus_msg_buf **ub, struct ubus_object *obj,
143                                  const char *id, struct blob_attr *msg)
144 {
145         uint32_t *objid_ptr;
146
147         if (*ub) {
148                 objid_ptr = blob_data(blob_data((*ub)->data));
149                 *objid_ptr = htonl(obj->id.id);
150         } else {
151                 blob_buf_init(&b, 0);
152                 blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
153                 blob_put_string(&b, UBUS_ATTR_METHOD, id);
154                 blob_put(&b, UBUS_ATTR_DATA, blobmsg_data(msg), blobmsg_data_len(msg));
155
156                 *ub = ubus_msg_new(b.head, blob_raw_len(b.head), true);
157
158                 (*ub)->hdr.type = UBUS_MSG_INVOKE;
159                 (*ub)->hdr.peer = 0;
160         }
161         (*ub)->hdr.seq = ++event_seq;
162         ubus_msg_send(obj->client, *ub, false);
163 }
164
165 static int ubusd_send_event(struct ubus_client *cl, struct blob_attr *msg)
166 {
167         struct ubus_msg_buf *ub = NULL;
168         struct event_source *ev;
169         struct blob_attr *attr[EVMSG_LAST];
170         const char *id;
171
172         blobmsg_parse(ev_policy, EVMSG_LAST, attr, blob_data(msg), blob_len(msg));
173         if (!attr[EVMSG_ID] || !attr[EVMSG_DATA])
174                 return UBUS_STATUS_INVALID_ARGUMENT;
175
176         id = blobmsg_data(attr[EVMSG_ID]);
177         list_for_each_entry(ev, &catch_all, catchall.list) {
178                 /* do not loop back events */
179                 if (ev->obj->client == cl)
180                         continue;
181
182                 ubusd_send_event_msg(&ub, ev->obj, id, attr[EVMSG_DATA]);
183         }
184
185         if (ub)
186                 ubus_msg_free(ub);
187
188         return 0;
189 }
190
191 static int ubusd_event_recv(struct ubus_client *cl, const char *method, struct blob_attr *msg)
192 {
193         if (!strcmp(method, "register"))
194                 return ubusd_alloc_event_pattern(cl, msg);
195
196         if (!strcmp(method, "send"))
197                 return ubusd_send_event(cl, msg);
198
199         return UBUS_STATUS_INVALID_COMMAND;
200 }
201
202 void ubusd_event_init(void)
203 {
204         ubus_init_string_tree(&patterns, true);
205         event_obj = ubusd_create_object_internal(NULL, UBUS_SYSTEM_OBJECT_EVENT);
206         event_obj->recv_msg = ubusd_event_recv;
207 }
208