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
;
40 while (!list_empty(&obj
->events
)) {
41 ev
= list_first_entry(&obj
->events
, struct event_source
, list
);
42 ubusd_delete_event_source(ev
);
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
},
57 static int ubusd_alloc_event_pattern(struct ubus_client
*cl
, struct blob_attr
*msg
)
59 struct event_source
*ev
;
60 struct ubus_object
*obj
;
61 struct blob_attr
*attr
[EVREG_LAST
];
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
;
71 id
= blobmsg_get_u32(attr
[EVREG_OBJECT
]);
72 if (id
< UBUS_SYSTEM_OBJECT_MAX
)
73 return UBUS_STATUS_PERMISSION_DENIED
;
75 obj
= ubusd_find_object(id
);
77 return UBUS_STATUS_NOT_FOUND
;
79 if (obj
->client
!= cl
)
80 return UBUS_STATUS_PERMISSION_DENIED
;
82 pattern
= blobmsg_data(attr
[EVREG_PATTERN
]);
84 len
= strlen(pattern
);
85 if (pattern
[len
- 1] == '*') {
91 if (pattern
[0] && ubusd_acl_check(cl
, pattern
, NULL
, UBUS_ACL_LISTEN
))
92 return UBUS_STATUS_PERMISSION_DENIED
;
94 ev
= calloc(1, sizeof(*ev
) + len
+ 1);
96 return UBUS_STATUS_NO_DATA
;
98 list_add(&ev
->list
, &obj
->events
);
100 ev
->partial
= partial
;
101 name
= (char *) (ev
+ 1);
102 strcpy(name
, pattern
);
104 avl_insert(&patterns
, &ev
->avl
);
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
)
115 /* do not loop back events */
116 if (obj
->client
== cl
)
119 /* do not send duplicate events */
120 if (obj
->event_seen
== obj_event_seq
)
123 obj
->event_seen
= obj_event_seq
;
126 *ub
= fill_cb(cb_priv
, id
);
127 (*ub
)->hdr
.type
= UBUS_MSG_INVOKE
;
131 objid_ptr
= blob_data(blob_data((*ub
)->data
));
132 *objid_ptr
= htonl(obj
->id
.id
);
134 (*ub
)->hdr
.seq
= ++event_seq
;
135 ubus_msg_send(obj
->client
, *ub
);
138 int ubusd_send_event(struct ubus_client
*cl
, const char *id
,
139 event_fill_cb fill_cb
, void *cb_priv
)
141 struct ubus_msg_buf
*ub
= NULL
;
142 struct event_source
*ev
;
145 if (ubusd_acl_check(cl
, id
, NULL
, UBUS_ACL_SEND
))
146 return UBUS_STATUS_PERMISSION_DENIED
;
151 * Since this tree is sorted alphabetically, we can only expect to find
152 * matching entries as long as the number of matching characters
153 * between the pattern string and our string is monotonically increasing.
155 avl_for_each_element(&patterns
, ev
, avl
) {
156 const char *key
= ev
->avl
.key
;
160 full_match
= ubus_strmatch_len(id
, key
, &cur_match_len
);
161 if (cur_match_len
< match_len
)
164 match_len
= cur_match_len
;
170 if (match_len
!= strlen(key
))
174 ubusd_send_event_msg(&ub
, cl
, ev
->obj
, id
, fill_cb
, cb_priv
);
189 static struct blobmsg_policy ev_policy
[] = {
190 [EVMSG_ID
] = { .name
= "id", .type
= BLOBMSG_TYPE_STRING
},
191 [EVMSG_DATA
] = { .name
= "data", .type
= BLOBMSG_TYPE_TABLE
},
194 static struct ubus_msg_buf
*
195 ubusd_create_event_from_msg(void *priv
, const char *id
)
197 struct blob_attr
*msg
= priv
;
199 blob_buf_init(&b
, 0);
200 blob_put_int32(&b
, UBUS_ATTR_OBJID
, 0);
201 blob_put_string(&b
, UBUS_ATTR_METHOD
, id
);
202 blob_put(&b
, UBUS_ATTR_DATA
, blobmsg_data(msg
), blobmsg_data_len(msg
));
204 return ubus_msg_new(b
.head
, blob_raw_len(b
.head
), true);
207 static int ubusd_forward_event(struct ubus_client
*cl
, struct blob_attr
*msg
)
209 struct blob_attr
*data
;
210 struct blob_attr
*attr
[EVMSG_LAST
];
213 blobmsg_parse(ev_policy
, EVMSG_LAST
, attr
, blob_data(msg
), blob_len(msg
));
214 if (!attr
[EVMSG_ID
] || !attr
[EVMSG_DATA
])
215 return UBUS_STATUS_INVALID_ARGUMENT
;
217 id
= blobmsg_data(attr
[EVMSG_ID
]);
218 data
= attr
[EVMSG_DATA
];
220 if (!strncmp(id
, "ubus.", 5))
221 return UBUS_STATUS_PERMISSION_DENIED
;
223 return ubusd_send_event(cl
, id
, ubusd_create_event_from_msg
, data
);
226 static int ubusd_event_recv(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
, const char *method
, struct blob_attr
*msg
)
228 if (!strcmp(method
, "register"))
229 return ubusd_alloc_event_pattern(cl
, msg
);
231 if (!strcmp(method
, "send"))
232 return ubusd_forward_event(cl
, msg
);
234 return UBUS_STATUS_INVALID_COMMAND
;
237 static struct ubus_msg_buf
*
238 ubusd_create_object_event_msg(void *priv
, const char *id
)
240 struct ubus_object
*obj
= priv
;
243 blob_buf_init(&b
, 0);
244 blob_put_int32(&b
, UBUS_ATTR_OBJID
, 0);
245 blob_put_string(&b
, UBUS_ATTR_METHOD
, id
);
246 s
= blob_nest_start(&b
, UBUS_ATTR_DATA
);
247 blobmsg_add_u32(&b
, "id", obj
->id
.id
);
248 blobmsg_add_string(&b
, "path", obj
->path
.key
);
249 blob_nest_end(&b
, s
);
251 return ubus_msg_new(b
.head
, blob_raw_len(b
.head
), true);
254 void ubusd_send_obj_event(struct ubus_object
*obj
, bool add
)
256 const char *id
= add
? "ubus.object.add" : "ubus.object.remove";
258 ubusd_send_event(NULL
, id
, ubusd_create_object_event_msg
, obj
);
261 void ubusd_event_init(void)
263 ubus_init_string_tree(&patterns
, true);
264 event_obj
= ubusd_create_object_internal(NULL
, UBUS_SYSTEM_OBJECT_EVENT
);
265 if (event_obj
!= NULL
)
266 event_obj
->recv_msg
= ubusd_event_recv
;