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;
15 struct list_head list;
16 struct ubus_object *obj;
24 struct list_head list;
29 static void ubusd_delete_event_source(struct event_source *evs)
34 avl_delete(&patterns, &evs->pattern.avl);
37 list_del(&evs->catchall.list);
43 void ubusd_event_cleanup_object(struct ubus_object *obj)
45 struct event_source *ev;
47 while (!list_empty(&obj->events)) {
48 ev = list_first_entry(&obj->events, struct event_source, list);
49 ubusd_delete_event_source(ev);
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 },
65 static struct event_source *ubusd_alloc_event_source(struct ubus_object *obj, enum evs_type type, int datalen)
67 struct event_source *evs;
69 evs = calloc(1, sizeof(*evs) + datalen);
70 list_add(&evs->list, &obj->events);
76 static int ubusd_alloc_catchall(struct ubus_object *obj)
78 struct event_source *evs;
80 evs = ubusd_alloc_event_source(obj, EVS_CATCHALL, 0);
81 list_add(&evs->catchall.list, &catch_all);
86 static int ubusd_alloc_event_pattern(struct ubus_client *cl, struct blob_attr *msg)
88 struct event_source *ev;
89 struct ubus_object *obj;
90 struct blob_attr *attr[EVREG_LAST];
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;
100 id = blobmsg_get_u32(attr[EVREG_OBJECT]);
101 if (id < UBUS_SYSTEM_OBJECT_MAX)
102 return UBUS_STATUS_PERMISSION_DENIED;
104 obj = ubusd_find_object(id);
106 return UBUS_STATUS_NOT_FOUND;
108 if (obj->client != cl)
109 return UBUS_STATUS_PERMISSION_DENIED;
111 if (!attr[EVREG_PATTERN])
112 return ubusd_alloc_catchall(obj);
114 pattern = blobmsg_data(attr[EVREG_PATTERN]);
116 len = strlen(pattern);
117 if (pattern[len - 1] == '*') {
119 pattern[len - 1] = 0;
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);
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 },
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)
149 /* do not loop back events */
150 if (obj->client == cl)
153 /* do not send duplicate events */
154 if (obj->event_seen == obj_event_seq)
157 obj->event_seen = obj_event_seq;
160 objid_ptr = blob_data(blob_data((*ub)->data));
161 *objid_ptr = htonl(obj->id.id);
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));
168 *ub = ubus_msg_new(b.head, blob_raw_len(b.head), true);
170 (*ub)->hdr.type = UBUS_MSG_INVOKE;
173 (*ub)->hdr.seq = ++event_seq;
174 ubus_msg_send(obj->client, *ub, false);
177 bool strmatch_len(const char *s1, const char *s2, int *len)
179 for (*len = 0; s1[*len] == s2[*len]; (*len)++)
186 static int ubusd_send_event(struct ubus_client *cl, struct blob_attr *msg)
188 struct ubus_msg_buf *ub = NULL;
189 struct event_source *ev;
190 struct blob_attr *attr[EVMSG_LAST];
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;
199 id = blobmsg_data(attr[EVMSG_ID]);
200 data = attr[EVMSG_DATA];
202 list_for_each_entry(ev, &catch_all, catchall.list)
203 ubusd_send_event_msg(&ub, cl, ev->obj, id, data);
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.
212 avl_for_each_element(&patterns, ev, pattern.avl) {
213 const char *key = ev->pattern.avl.key;
217 full_match = strmatch_len(id, key, &cur_match_len);
218 if (cur_match_len < match_len)
221 match_len = cur_match_len;
224 if (!ev->pattern.partial)
227 if (match_len != strlen(key))
231 ubusd_send_event_msg(&ub, cl, ev->obj, id, data);
240 static int ubusd_event_recv(struct ubus_client *cl, const char *method, struct blob_attr *msg)
242 if (!strcmp(method, "register"))
243 return ubusd_alloc_event_pattern(cl, msg);
245 if (!strcmp(method, "send"))
246 return ubusd_send_event(cl, msg);
248 return UBUS_STATUS_INVALID_COMMAND;
251 void ubusd_event_init(void)
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;