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