ubusd_event: move strmatch_len to ubus_common.h
[project/ubus.git] / ubusd_obj.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 "ubusd.h"
15 #include "ubusd_obj.h"
16
17 struct avl_tree obj_types;
18 struct avl_tree objects;
19 struct avl_tree path;
20
21 static void ubus_unref_object_type(struct ubus_object_type *type)
22 {
23 struct ubus_method *m;
24
25 if (--type->refcount > 0)
26 return;
27
28 while (!list_empty(&type->methods)) {
29 m = list_first_entry(&type->methods, struct ubus_method, list);
30 list_del(&m->list);
31 free(m);
32 }
33
34 ubus_free_id(&obj_types, &type->id);
35 free(type);
36 }
37
38 static bool ubus_create_obj_method(struct ubus_object_type *type, struct blob_attr *attr)
39 {
40 struct ubus_method *m;
41 int bloblen = blob_raw_len(attr);
42
43 m = calloc(1, sizeof(*m) + bloblen);
44 if (!m)
45 return false;
46
47 list_add_tail(&m->list, &type->methods);
48 memcpy(m->data, attr, bloblen);
49 m->name = blobmsg_name(m->data);
50
51 return true;
52 }
53
54 static struct ubus_object_type *ubus_create_obj_type(struct blob_attr *sig)
55 {
56 struct ubus_object_type *type;
57 struct blob_attr *pos;
58 int rem;
59
60 type = calloc(1, sizeof(*type));
61 if (!type)
62 return NULL;
63
64 type->refcount = 1;
65
66 if (!ubus_alloc_id(&obj_types, &type->id, 0))
67 goto error_free;
68
69 INIT_LIST_HEAD(&type->methods);
70
71 blob_for_each_attr(pos, sig, rem) {
72 if (!blobmsg_check_attr(pos, true))
73 goto error_unref;
74
75 if (!ubus_create_obj_method(type, pos))
76 goto error_unref;
77 }
78
79 return type;
80
81 error_unref:
82 ubus_unref_object_type(type);
83 return NULL;
84
85 error_free:
86 free(type);
87 return NULL;
88 }
89
90 static struct ubus_object_type *ubus_get_obj_type(uint32_t obj_id)
91 {
92 struct ubus_object_type *type;
93 struct ubus_id *id;
94
95 id = ubus_find_id(&obj_types, obj_id);
96 if (!id)
97 return NULL;
98
99 type = container_of(id, struct ubus_object_type, id);
100 type->refcount++;
101 return type;
102 }
103
104 struct ubus_object *ubusd_create_object_internal(struct ubus_object_type *type, uint32_t id)
105 {
106 struct ubus_object *obj;
107
108 obj = calloc(1, sizeof(*obj));
109 if (!obj)
110 return NULL;
111
112 if (!ubus_alloc_id(&objects, &obj->id, id))
113 goto error_free;
114
115 obj->type = type;
116 INIT_LIST_HEAD(&obj->list);
117 INIT_LIST_HEAD(&obj->events);
118 INIT_LIST_HEAD(&obj->subscribers);
119 INIT_LIST_HEAD(&obj->target_list);
120 if (type)
121 type->refcount++;
122
123 return obj;
124
125 error_free:
126 free(obj);
127 return NULL;
128 }
129
130 struct ubus_object *ubusd_create_object(struct ubus_client *cl, struct blob_attr **attr)
131 {
132 struct ubus_object *obj;
133 struct ubus_object_type *type = NULL;
134
135 if (attr[UBUS_ATTR_OBJTYPE])
136 type = ubus_get_obj_type(blob_get_u32(attr[UBUS_ATTR_OBJTYPE]));
137 else if (attr[UBUS_ATTR_SIGNATURE])
138 type = ubus_create_obj_type(attr[UBUS_ATTR_SIGNATURE]);
139
140 obj = ubusd_create_object_internal(type, 0);
141 if (type)
142 ubus_unref_object_type(type);
143
144 if (!obj)
145 return NULL;
146
147 if (attr[UBUS_ATTR_OBJPATH]) {
148 if (ubusd_acl_check(cl, blob_data(attr[UBUS_ATTR_OBJPATH]), NULL, UBUS_ACL_PUBLISH))
149 goto free;
150
151 obj->path.key = strdup(blob_data(attr[UBUS_ATTR_OBJPATH]));
152 if (!obj->path.key)
153 goto free;
154
155 if (avl_insert(&path, &obj->path) != 0) {
156 free((void *) obj->path.key);
157 obj->path.key = NULL;
158 goto free;
159 }
160 ubusd_send_obj_event(obj, true);
161 }
162
163 obj->client = cl;
164 list_add(&obj->list, &cl->objects);
165
166 return obj;
167
168 free:
169 ubusd_free_object(obj);
170 return NULL;
171 }
172
173 void ubus_subscribe(struct ubus_object *obj, struct ubus_object *target)
174 {
175 struct ubus_subscription *s;
176 bool first = list_empty(&target->subscribers);
177
178 s = calloc(1, sizeof(*s));
179 if (!s)
180 return;
181
182 s->subscriber = obj;
183 s->target = target;
184 list_add(&s->list, &target->subscribers);
185 list_add(&s->target_list, &obj->target_list);
186
187 if (first)
188 ubus_notify_subscription(target);
189 }
190
191 void ubus_unsubscribe(struct ubus_subscription *s)
192 {
193 struct ubus_object *obj = s->target;
194
195 list_del(&s->list);
196 list_del(&s->target_list);
197 free(s);
198
199 if (list_empty(&obj->subscribers))
200 ubus_notify_subscription(obj);
201 }
202
203 void ubusd_free_object(struct ubus_object *obj)
204 {
205 struct ubus_subscription *s, *tmp;
206
207 list_for_each_entry_safe(s, tmp, &obj->target_list, target_list) {
208 ubus_unsubscribe(s);
209 }
210 list_for_each_entry_safe(s, tmp, &obj->subscribers, list) {
211 ubus_notify_unsubscribe(s);
212 }
213
214 ubusd_event_cleanup_object(obj);
215 if (obj->path.key) {
216 ubusd_send_obj_event(obj, false);
217 avl_delete(&path, &obj->path);
218 free((void *) obj->path.key);
219 }
220 if (!list_empty(&obj->list))
221 list_del(&obj->list);
222 ubus_free_id(&objects, &obj->id);
223 if (obj->type)
224 ubus_unref_object_type(obj->type);
225 free(obj);
226 }
227
228 static void __constructor ubusd_obj_init(void)
229 {
230 ubus_init_id_tree(&objects);
231 ubus_init_id_tree(&obj_types);
232 ubus_init_string_tree(&path, false);
233 ubusd_event_init();
234 ubusd_acl_init();
235 ubusd_monitor_init();
236 }