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;
10 struct list_head list;
11 struct ubus_object *obj;
16 static void ubusd_delete_event_source(struct event_source *evs)
19 avl_delete(&patterns, &evs->avl);
23 void ubusd_event_cleanup_object(struct ubus_object *obj)
25 struct event_source *ev;
27 while (!list_empty(&obj->events)) {
28 ev = list_first_entry(&obj->events, struct event_source, list);
29 ubusd_delete_event_source(ev);
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 },
44 static int ubusd_alloc_event_pattern(struct ubus_client *cl, struct blob_attr *msg)
46 struct event_source *ev;
47 struct ubus_object *obj;
48 struct blob_attr *attr[EVREG_LAST];
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;
58 id = blobmsg_get_u32(attr[EVREG_OBJECT]);
59 if (id < UBUS_SYSTEM_OBJECT_MAX)
60 return UBUS_STATUS_PERMISSION_DENIED;
62 obj = ubusd_find_object(id);
64 return UBUS_STATUS_NOT_FOUND;
66 if (obj->client != cl)
67 return UBUS_STATUS_PERMISSION_DENIED;
69 pattern = blobmsg_data(attr[EVREG_PATTERN]);
71 len = strlen(pattern);
72 if (pattern[len - 1] == '*') {
78 ev = calloc(1, sizeof(*ev) + len + 1);
80 return UBUS_STATUS_NO_DATA;
82 list_add(&ev->list, &obj->events);
84 ev->partial = partial;
85 ev->avl.key = (void *) (ev + 1);
86 strcpy(ev->avl.key, pattern);
87 avl_insert(&patterns, &ev->avl);
92 typedef struct ubus_msg_buf *(*event_fill_cb)(void *priv, const char *id);
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)
100 /* do not loop back events */
101 if (obj->client == cl)
104 /* do not send duplicate events */
105 if (obj->event_seen == obj_event_seq)
108 obj->event_seen = obj_event_seq;
111 *ub = fill_cb(cb_priv, id);
112 (*ub)->hdr.type = UBUS_MSG_INVOKE;
116 objid_ptr = blob_data(blob_data((*ub)->data));
117 *objid_ptr = htonl(obj->id.id);
119 (*ub)->hdr.seq = ++event_seq;
120 ubus_msg_send(obj->client, *ub, false);
123 bool strmatch_len(const char *s1, const char *s2, int *len)
125 for (*len = 0; s1[*len] == s2[*len]; (*len)++)
132 static int ubusd_send_event(struct ubus_client *cl, const char *id,
133 event_fill_cb fill_cb, void *cb_priv)
135 struct ubus_msg_buf *ub = NULL;
136 struct event_source *ev;
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.
146 avl_for_each_element(&patterns, ev, avl) {
147 const char *key = ev->avl.key;
151 full_match = strmatch_len(id, key, &cur_match_len);
152 if (cur_match_len < match_len)
155 match_len = cur_match_len;
161 if (match_len != strlen(key))
165 ubusd_send_event_msg(&ub, cl, ev->obj, id, fill_cb, cb_priv);
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 },
185 static struct ubus_msg_buf *
186 ubusd_create_event_from_msg(void *priv, const char *id)
188 struct blob_attr *msg = priv;
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));
195 return ubus_msg_new(b.head, blob_raw_len(b.head), true);
198 static int ubusd_forward_event(struct ubus_client *cl, struct blob_attr *msg)
200 struct blob_attr *data;
201 struct blob_attr *attr[EVMSG_LAST];
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;
208 id = blobmsg_data(attr[EVMSG_ID]);
209 data = attr[EVMSG_DATA];
211 if (!strncmp(id, "ubus.", 5))
212 return UBUS_STATUS_PERMISSION_DENIED;
214 return ubusd_send_event(cl, id, ubusd_create_event_from_msg, data);
217 static int ubusd_event_recv(struct ubus_client *cl, const char *method, struct blob_attr *msg)
219 if (!strcmp(method, "register"))
220 return ubusd_alloc_event_pattern(cl, msg);
222 if (!strcmp(method, "send"))
223 return ubusd_forward_event(cl, msg);
225 return UBUS_STATUS_INVALID_COMMAND;
228 static struct ubus_msg_buf *
229 ubusd_create_object_event_msg(void *priv, const char *id)
231 struct ubus_object *obj = priv;
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);
242 return ubus_msg_new(b.head, blob_raw_len(b.head), true);
245 void ubusd_send_obj_event(struct ubus_object *obj, bool add)
247 const char *id = add ? "ubus.object.add" : "ubus.object.remove";
249 ubusd_send_event(NULL, id, ubusd_create_object_event_msg, obj);
252 void ubusd_event_init(void)
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;