2 * Copyright (C) 2011 Felix Fietkau <nbd@openwrt.org>
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
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.
14 #include <arpa/inet.h>
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;
23 struct list_head list
;
24 struct ubus_object
*obj
;
29 static void ubusd_delete_event_source(struct event_source
*evs
)
32 avl_delete(&patterns
, &evs
->avl
);
36 void ubusd_event_cleanup_object(struct ubus_object
*obj
)
38 struct event_source
*ev
, *tmp
;
40 list_for_each_entry_safe(ev
, tmp
, &obj
->events
, list
) {
41 ubusd_delete_event_source(ev
);
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
},
56 static int ubusd_alloc_event_pattern(struct ubus_client
*cl
, struct blob_attr
*msg
)
58 struct event_source
*ev
;
59 struct ubus_object
*obj
;
60 struct blob_attr
*attr
[EVREG_LAST
];
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
;
70 id
= blobmsg_get_u32(attr
[EVREG_OBJECT
]);
71 if (id
< UBUS_SYSTEM_OBJECT_MAX
)
72 return UBUS_STATUS_PERMISSION_DENIED
;
74 obj
= ubusd_find_object(id
);
76 return UBUS_STATUS_NOT_FOUND
;
78 if (obj
->client
!= cl
)
79 return UBUS_STATUS_PERMISSION_DENIED
;
81 pattern
= blobmsg_data(attr
[EVREG_PATTERN
]);
83 len
= strlen(pattern
);
84 if (pattern
[len
- 1] == '*') {
90 if (pattern
[0] && ubusd_acl_check(cl
, pattern
, NULL
, UBUS_ACL_LISTEN
))
91 return UBUS_STATUS_PERMISSION_DENIED
;
93 ev
= calloc(1, sizeof(*ev
) + len
+ 1);
95 return UBUS_STATUS_NO_DATA
;
97 list_add(&ev
->list
, &obj
->events
);
99 ev
->partial
= partial
;
100 name
= (char *) (ev
+ 1);
101 strcpy(name
, pattern
);
103 avl_insert(&patterns
, &ev
->avl
);
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
)
114 /* do not loop back events */
115 if (obj
->client
== cl
)
118 /* do not send duplicate events */
119 if (obj
->event_seen
== obj_event_seq
)
122 obj
->event_seen
= obj_event_seq
;
125 *ub
= fill_cb(cb_priv
, id
);
126 (*ub
)->hdr
.type
= UBUS_MSG_INVOKE
;
130 objid_ptr
= blob_data(blob_data((*ub
)->data
));
131 *objid_ptr
= htonl(obj
->id
.id
);
133 (*ub
)->hdr
.seq
= ++event_seq
;
134 ubus_msg_send(obj
->client
, *ub
);
137 int ubusd_send_event(struct ubus_client
*cl
, const char *id
,
138 event_fill_cb fill_cb
, void *cb_priv
)
140 struct ubus_msg_buf
*ub
= NULL
;
141 struct event_source
*ev
;
144 if (ubusd_acl_check(cl
, id
, NULL
, UBUS_ACL_SEND
))
145 return UBUS_STATUS_PERMISSION_DENIED
;
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.
154 avl_for_each_element(&patterns
, ev
, avl
) {
155 const char *key
= ev
->avl
.key
;
159 full_match
= ubus_strmatch_len(id
, key
, &cur_match_len
);
160 if (cur_match_len
< match_len
)
163 match_len
= cur_match_len
;
169 if (match_len
!= (int) strlen(key
))
173 ubusd_send_event_msg(&ub
, cl
, ev
->obj
, id
, fill_cb
, cb_priv
);
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
},
193 static struct ubus_msg_buf
*
194 ubusd_create_event_from_msg(void *priv
, const char *id
)
196 struct blob_attr
*msg
= priv
;
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
));
203 return ubus_msg_new(b
.head
, blob_raw_len(b
.head
), true);
206 static int ubusd_forward_event(struct ubus_client
*cl
, struct blob_attr
*msg
)
208 struct blob_attr
*data
;
209 struct blob_attr
*attr
[EVMSG_LAST
];
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
;
216 id
= blobmsg_data(attr
[EVMSG_ID
]);
217 data
= attr
[EVMSG_DATA
];
219 if (!strncmp(id
, "ubus.", 5))
220 return UBUS_STATUS_PERMISSION_DENIED
;
222 return ubusd_send_event(cl
, id
, ubusd_create_event_from_msg
, data
);
225 static int ubusd_event_recv(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
, const char *method
, struct blob_attr
*msg
)
227 if (!strcmp(method
, "register"))
228 return ubusd_alloc_event_pattern(cl
, msg
);
230 if (!strcmp(method
, "send"))
231 return ubusd_forward_event(cl
, msg
);
233 return UBUS_STATUS_INVALID_COMMAND
;
236 static struct ubus_msg_buf
*
237 ubusd_create_object_event_msg(void *priv
, const char *id
)
239 struct ubus_object
*obj
= priv
;
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
);
250 return ubus_msg_new(b
.head
, blob_raw_len(b
.head
), true);
253 void ubusd_send_obj_event(struct ubus_object
*obj
, bool add
)
255 const char *id
= add
? "ubus.object.add" : "ubus.object.remove";
257 ubusd_send_event(NULL
, id
, ubusd_create_object_event_msg
, obj
);
260 void ubusd_event_init(void)
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
;