ubusd: allow registration of anonymous objects with type
[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 type->refcount = 1;
62
63 if (!ubus_alloc_id(&obj_types, &type->id, 0))
64 goto error_free;
65
66 INIT_LIST_HEAD(&type->methods);
67
68 blob_for_each_attr(pos, sig, rem) {
69 if (!blobmsg_check_attr(pos, true))
70 goto error_unref;
71
72 if (!ubus_create_obj_method(type, pos))
73 goto error_unref;
74 }
75
76 return type;
77
78 error_unref:
79 ubus_unref_object_type(type);
80 return NULL;
81
82 error_free:
83 free(type);
84 return NULL;
85 }
86
87 static struct ubus_object_type *ubus_get_obj_type(uint32_t obj_id)
88 {
89 struct ubus_object_type *type;
90 struct ubus_id *id;
91
92 id = ubus_find_id(&obj_types, obj_id);
93 if (!id)
94 return NULL;
95
96 type = container_of(id, struct ubus_object_type, id);
97 type->refcount++;
98 return type;
99 }
100
101 struct ubus_object *ubusd_create_object_internal(struct ubus_object_type *type, uint32_t id)
102 {
103 struct ubus_object *obj;
104
105 obj = calloc(1, sizeof(*obj));
106 if (!obj)
107 return NULL;
108
109 if (!ubus_alloc_id(&objects, &obj->id, id))
110 goto error_free;
111
112 obj->type = type;
113 INIT_LIST_HEAD(&obj->list);
114 INIT_LIST_HEAD(&obj->events);
115 if (type)
116 type->refcount++;
117
118 return obj;
119
120 error_free:
121 free(obj);
122 return NULL;
123 }
124
125 struct ubus_object *ubusd_create_object(struct ubus_client *cl, struct blob_attr **attr)
126 {
127 struct ubus_object *obj;
128 struct ubus_object_type *type = NULL;
129
130 if (attr[UBUS_ATTR_OBJTYPE])
131 type = ubus_get_obj_type(blob_get_u32(attr[UBUS_ATTR_OBJTYPE]));
132 else if (attr[UBUS_ATTR_SIGNATURE])
133 type = ubus_create_obj_type(attr[UBUS_ATTR_SIGNATURE]);
134
135 obj = ubusd_create_object_internal(type, 0);
136 if (type)
137 ubus_unref_object_type(type);
138
139 if (!obj)
140 return NULL;
141
142 if (attr[UBUS_ATTR_OBJPATH]) {
143 obj->path.key = strdup(blob_data(attr[UBUS_ATTR_OBJPATH]));
144 if (!obj->path.key)
145 goto free;
146
147 if (avl_insert(&path, &obj->path) != 0) {
148 free((void *) obj->path.key);
149 obj->path.key = NULL;
150 goto free;
151 }
152 ubusd_send_obj_event(obj, true);
153 }
154
155 obj->client = cl;
156 list_add(&obj->list, &cl->objects);
157
158 return obj;
159
160 free:
161 ubusd_free_object(obj);
162 return NULL;
163 }
164
165 void ubusd_free_object(struct ubus_object *obj)
166 {
167 ubusd_event_cleanup_object(obj);
168 if (obj->path.key) {
169 ubusd_send_obj_event(obj, false);
170 avl_delete(&path, &obj->path);
171 free((void *) obj->path.key);
172 }
173 if (!list_empty(&obj->list))
174 list_del(&obj->list);
175 ubus_free_id(&objects, &obj->id);
176 if (obj->type)
177 ubus_unref_object_type(obj->type);
178 free(obj);
179 }
180
181 static void __init ubusd_obj_init(void)
182 {
183 ubus_init_id_tree(&objects);
184 ubus_init_id_tree(&obj_types);
185 ubus_init_string_tree(&path, false);
186 ubusd_event_init();
187 }