add writev_retry to avoid incomplete writes
[project/ubus.git] / ubusd_event.c
1 #include <arpa/inet.h>
2 #include "ubusd.h"
3
4 static struct avl_tree patterns;
5 static LIST_HEAD(catch_all);
6 static struct ubus_object *event_obj;
7 static int event_seq = 0;
8 static int obj_event_seq = 1;
9
10 enum evs_type {
11         EVS_PATTERN,
12         EVS_CATCHALL
13 };
14
15 struct event_source {
16         struct list_head list;
17         struct ubus_object *obj;
18         enum evs_type type;
19         union {
20                 struct {
21                         struct avl_node avl;
22                         bool partial;
23                 } pattern;
24                 struct {
25                         struct list_head list;
26                 } catchall;
27         };
28 };
29
30 static void ubusd_delete_event_source(struct event_source *evs)
31 {
32         list_del(&evs->list);
33         switch (evs->type) {
34         case EVS_PATTERN:
35                 avl_delete(&patterns, &evs->pattern.avl);
36                 break;
37         case EVS_CATCHALL:
38                 list_del(&evs->catchall.list);
39                 break;
40         }
41         free(evs);
42 }
43
44 void ubusd_event_cleanup_object(struct ubus_object *obj)
45 {
46         struct event_source *ev;
47
48         while (!list_empty(&obj->events)) {
49                 ev = list_first_entry(&obj->events, struct event_source, list);
50                 ubusd_delete_event_source(ev);
51         }
52 }
53
54 enum {
55         EVREG_PATTERN,
56         EVREG_OBJECT,
57         EVREG_LAST,
58 };
59
60 static struct blobmsg_policy evr_policy[] = {
61         [EVREG_PATTERN] = { .name = "pattern", .type = BLOBMSG_TYPE_STRING },
62         [EVREG_OBJECT] = { .name = "object", .type = BLOBMSG_TYPE_INT32 },
63 };
64
65
66 static struct event_source *ubusd_alloc_event_source(struct ubus_object *obj, enum evs_type type, int datalen)
67 {
68         struct event_source *evs;
69
70         evs = calloc(1, sizeof(*evs) + datalen);
71         list_add(&evs->list, &obj->events);
72         evs->obj = obj;
73         evs->type = type;
74         return evs;
75 }
76
77 static int ubusd_alloc_catchall(struct ubus_object *obj)
78 {
79         struct event_source *evs;
80
81         evs = ubusd_alloc_event_source(obj, EVS_CATCHALL, 0);
82         list_add(&evs->catchall.list, &catch_all);
83
84         return 0;
85 }
86
87 static int ubusd_alloc_event_pattern(struct ubus_client *cl, struct blob_attr *msg)
88 {
89         struct event_source *ev;
90         struct ubus_object *obj;
91         struct blob_attr *attr[EVREG_LAST];
92         char *pattern;
93         uint32_t id;
94         bool partial = false;
95         int len;
96
97         blobmsg_parse(evr_policy, EVREG_LAST, attr, blob_data(msg), blob_len(msg));
98         if (!attr[EVREG_OBJECT])
99                 return UBUS_STATUS_INVALID_ARGUMENT;
100
101         id = blobmsg_get_u32(attr[EVREG_OBJECT]);
102         if (id < UBUS_SYSTEM_OBJECT_MAX)
103                 return UBUS_STATUS_PERMISSION_DENIED;
104
105         obj = ubusd_find_object(id);
106         if (!obj)
107                 return UBUS_STATUS_NOT_FOUND;
108
109         if (obj->client != cl)
110                 return UBUS_STATUS_PERMISSION_DENIED;
111
112         if (!attr[EVREG_PATTERN])
113                 return ubusd_alloc_catchall(obj);
114
115         pattern = blobmsg_data(attr[EVREG_PATTERN]);
116
117         len = strlen(pattern);
118         if (pattern[len - 1] == '*') {
119                 partial = true;
120                 pattern[len - 1] = 0;
121                 len--;
122         }
123
124         ev = ubusd_alloc_event_source(obj, EVS_PATTERN, len + 1);
125         ev->pattern.partial = partial;
126         ev->pattern.avl.key = (void *) (ev + 1);
127         strcpy(ev->pattern.avl.key, pattern);
128         avl_insert(&patterns, &ev->pattern.avl);
129
130         return 0;
131 }
132
133 typedef struct ubus_msg_buf *(*event_fill_cb)(void *priv, const char *id);
134
135 static void ubusd_send_event_msg(struct ubus_msg_buf **ub, struct ubus_client *cl,
136                                  struct ubus_object *obj, const char *id,
137                                  event_fill_cb fill_cb, void *cb_priv)
138 {
139         uint32_t *objid_ptr;
140
141         /* do not loop back events */
142         if (obj->client == cl)
143             return;
144
145         /* do not send duplicate events */
146         if (obj->event_seen == obj_event_seq)
147                 return;
148
149         obj->event_seen = obj_event_seq;
150
151         if (!*ub) {
152                 *ub = fill_cb(cb_priv, id);
153                 (*ub)->hdr.type = UBUS_MSG_INVOKE;
154                 (*ub)->hdr.peer = 0;
155         }
156
157         objid_ptr = blob_data(blob_data((*ub)->data));
158         *objid_ptr = htonl(obj->id.id);
159
160         (*ub)->hdr.seq = ++event_seq;
161         ubus_msg_send(obj->client, *ub, false);
162 }
163
164 bool strmatch_len(const char *s1, const char *s2, int *len)
165 {
166         for (*len = 0; s1[*len] == s2[*len]; (*len)++)
167                 if (!s1[*len])
168                         return true;
169
170         return false;
171 }
172
173 static int ubusd_send_event(struct ubus_client *cl, const char *id,
174                             event_fill_cb fill_cb, void *cb_priv)
175 {
176         struct ubus_msg_buf *ub = NULL;
177         struct event_source *ev;
178         int match_len = 0;
179
180         list_for_each_entry(ev, &catch_all, catchall.list)
181                 ubusd_send_event_msg(&ub, cl, ev->obj, id, fill_cb, cb_priv);
182
183         obj_event_seq++;
184
185         /*
186          * Since this tree is sorted alphabetically, we can only expect to find
187          * matching entries as long as the number of matching characters
188          * between the pattern string and our string is monotonically increasing.
189          */
190         avl_for_each_element(&patterns, ev, pattern.avl) {
191                 const char *key = ev->pattern.avl.key;
192                 int cur_match_len;
193                 bool full_match;
194
195                 full_match = strmatch_len(id, key, &cur_match_len);
196                 if (cur_match_len < match_len)
197                         break;
198
199                 match_len = cur_match_len;
200
201                 if (!full_match) {
202                         if (!ev->pattern.partial)
203                                 continue;
204
205                         if (match_len != strlen(key))
206                                 continue;
207                 }
208
209                 ubusd_send_event_msg(&ub, cl, ev->obj, id, fill_cb, cb_priv);
210         }
211
212         if (ub)
213                 ubus_msg_free(ub);
214
215         return 0;
216 }
217
218 enum {
219         EVMSG_ID,
220         EVMSG_DATA,
221         EVMSG_LAST,
222 };
223
224 static struct blobmsg_policy ev_policy[] = {
225         [EVMSG_ID] = { .name = "id", .type = BLOBMSG_TYPE_STRING },
226         [EVMSG_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
227 };
228
229 static struct ubus_msg_buf *
230 ubusd_create_event_from_msg(void *priv, const char *id)
231 {
232         struct blob_attr *msg = priv;
233
234         blob_buf_init(&b, 0);
235         blob_put_int32(&b, UBUS_ATTR_OBJID, 0);
236         blob_put_string(&b, UBUS_ATTR_METHOD, id);
237         blob_put(&b, UBUS_ATTR_DATA, blobmsg_data(msg), blobmsg_data_len(msg));
238
239         return ubus_msg_new(b.head, blob_raw_len(b.head), true);
240 }
241
242 static int ubusd_forward_event(struct ubus_client *cl, struct blob_attr *msg)
243 {
244         struct blob_attr *data;
245         struct blob_attr *attr[EVMSG_LAST];
246         const char *id;
247
248         blobmsg_parse(ev_policy, EVMSG_LAST, attr, blob_data(msg), blob_len(msg));
249         if (!attr[EVMSG_ID] || !attr[EVMSG_DATA])
250                 return UBUS_STATUS_INVALID_ARGUMENT;
251
252         id = blobmsg_data(attr[EVMSG_ID]);
253         data = attr[EVMSG_DATA];
254
255         if (!strncmp(id, "ubus.", 5))
256                 return UBUS_STATUS_PERMISSION_DENIED;
257
258         return ubusd_send_event(cl, id, ubusd_create_event_from_msg, data);
259 }
260
261 static int ubusd_event_recv(struct ubus_client *cl, const char *method, struct blob_attr *msg)
262 {
263         if (!strcmp(method, "register"))
264                 return ubusd_alloc_event_pattern(cl, msg);
265
266         if (!strcmp(method, "send"))
267                 return ubusd_forward_event(cl, msg);
268
269         return UBUS_STATUS_INVALID_COMMAND;
270 }
271
272 static struct ubus_msg_buf *
273 ubusd_create_object_event_msg(void *priv, const char *id)
274 {
275         struct ubus_object *obj = priv;
276         void *s;
277
278         blob_buf_init(&b, 0);
279         blob_put_int32(&b, UBUS_ATTR_OBJID, 0);
280         blob_put_string(&b, UBUS_ATTR_METHOD, id);
281         s = blob_nest_start(&b, UBUS_ATTR_DATA);
282         blobmsg_add_u32(&b, "id", obj->id.id);
283         blobmsg_add_string(&b, "path", obj->path.key);
284         blob_nest_end(&b, s);
285
286         return ubus_msg_new(b.head, blob_raw_len(b.head), true);
287 }
288
289 void ubusd_send_obj_event(struct ubus_object *obj, bool add)
290 {
291         const char *id = add ? "ubus.object.add" : "ubus.object.remove";
292
293         ubusd_send_event(NULL, id, ubusd_create_object_event_msg, obj);
294 }
295
296 void ubusd_event_init(void)
297 {
298         ubus_init_string_tree(&patterns, true);
299         event_obj = ubusd_create_object_internal(NULL, UBUS_SYSTEM_OBJECT_EVENT);
300         event_obj->recv_msg = ubusd_event_recv;
301 }
302