• source navigation  • diff markup  • identifier search  • freetext search  • 

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

This page was automatically generated by LXR 0.3.1.  •  OpenWrt