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