add functionality for registering anonymous objects as event listeners
[project/ubus.git] / ubusd_event.c
1 #include "ubusd.h"
2
3 static struct avl_tree patterns;
4 static LIST_HEAD(catch_all);
5 static struct ubus_object *event_obj;
6
7 enum evs_type {
8 EVS_PATTERN,
9 EVS_CATCHALL
10 };
11
12 struct event_source {
13 struct list_head list;
14 struct ubus_object *obj;
15 enum evs_type type;
16 union {
17 struct {
18 struct avl_node avl;
19 } pattern;
20 struct {
21 struct list_head list;
22 } catchall;
23 };
24 };
25
26 struct event_pattern {
27 struct event_source evs;
28 struct list_head list;
29 };
30
31 struct event_catchall {
32 struct event_source evs;
33
34 struct list_head list;
35 struct ubus_object *obj;
36 };
37
38 static void ubusd_delete_event_source(struct event_source *evs)
39 {
40 list_del(&evs->list);
41 switch (evs->type) {
42 case EVS_PATTERN:
43 avl_delete(&patterns, &evs->pattern.avl);
44 break;
45 case EVS_CATCHALL:
46 list_del(&evs->catchall.list);
47 break;
48 }
49 free(evs);
50 }
51
52 void ubusd_event_cleanup_object(struct ubus_object *obj)
53 {
54 struct event_source *ev;
55
56 while (!list_empty(&obj->events)) {
57 ev = list_first_entry(&obj->events, struct event_source, list);
58 ubusd_delete_event_source(ev);
59 }
60 }
61
62 enum {
63 EVMSG_PATTERN,
64 EVMSG_OBJECT,
65 EVMSG_LAST,
66 };
67
68 static struct blobmsg_policy ev_policy[] = {
69 [EVMSG_PATTERN] = { .name = "pattern", .type = BLOBMSG_TYPE_STRING },
70 [EVMSG_OBJECT] = { .name = "object", .type = BLOBMSG_TYPE_INT32 },
71 };
72
73
74 static struct event_source *ubusd_alloc_event_source(struct ubus_object *obj, enum evs_type type, int datalen)
75 {
76 struct event_source *evs;
77
78 evs = calloc(1, sizeof(*evs) + datalen);
79 list_add(&evs->list, &obj->events);
80 evs->obj = obj;
81 evs->type = type;
82 return evs;
83 }
84
85 static int ubusd_alloc_catchall(struct ubus_object *obj)
86 {
87 struct event_source *evs;
88
89 evs = ubusd_alloc_event_source(obj, EVS_CATCHALL, 0);
90 list_add(&evs->catchall.list, &catch_all);
91
92 return 0;
93 }
94
95 static int ubusd_alloc_event_pattern(struct ubus_client *cl, struct blob_attr *msg)
96 {
97 struct event_source *ev;
98 struct ubus_object *obj;
99 struct blob_attr *attr[EVMSG_LAST];
100 const char *pattern;
101 uint32_t id;
102
103 blobmsg_parse(ev_policy, EVMSG_LAST, attr, blob_data(msg), blob_len(msg));
104 if (!attr[EVMSG_OBJECT])
105 return UBUS_STATUS_INVALID_ARGUMENT;
106
107 id = blobmsg_get_u32(attr[EVMSG_OBJECT]);
108 if (id < UBUS_SYSTEM_OBJECT_MAX)
109 return UBUS_STATUS_PERMISSION_DENIED;
110
111 obj = ubusd_find_object(id);
112 if (!obj)
113 return UBUS_STATUS_NOT_FOUND;
114
115 if (obj->client != cl)
116 return UBUS_STATUS_PERMISSION_DENIED;
117
118 if (!attr[EVMSG_PATTERN])
119 return ubusd_alloc_catchall(obj);
120
121 pattern = blobmsg_data(attr[EVMSG_PATTERN]);
122 ev = ubusd_alloc_event_source(obj, EVS_PATTERN, strlen(pattern) + 1);
123 ev->pattern.avl.key = (void *) (ev + 1);
124 strcpy(ev->pattern.avl.key, pattern);
125 avl_insert(&patterns, &ev->pattern.avl);
126
127 return 0;
128 }
129
130 static int ubusd_event_recv(struct ubus_client *cl, const char *method, struct blob_attr *msg)
131 {
132 if (!strcmp(method, "register"))
133 return ubusd_alloc_event_pattern(cl, msg);
134
135 return UBUS_STATUS_INVALID_COMMAND;
136 }
137
138 void ubusd_event_init(void)
139 {
140 ubus_init_string_tree(&patterns, true);
141 event_obj = ubusd_create_object_internal(NULL, UBUS_SYSTEM_OBJECT_EVENT);
142 event_obj->recv_msg = ubusd_event_recv;
143 }
144