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