add functions for internal object allocation
[project/ubus.git] / ubusd_obj.c
1 #include "ubusd.h"
2 #include "ubusd_obj.h"
3
4 struct avl_tree obj_types;
5 struct avl_tree objects;
6 struct avl_tree path;
7
8 static void ubus_unref_object_type(struct ubus_object_type *type)
9 {
10 struct ubus_method *m;
11
12 if (--type->refcount > 0)
13 return;
14
15 while (!list_empty(&type->methods)) {
16 m = list_first_entry(&type->methods, struct ubus_method, list);
17 list_del(&m->list);
18 free(m);
19 }
20
21 ubus_free_id(&obj_types, &type->id);
22 free(type);
23 }
24
25 static bool ubus_create_obj_method(struct ubus_object_type *type, struct blob_attr *attr)
26 {
27 struct ubus_method *m;
28 int bloblen = blob_raw_len(attr);
29
30 m = calloc(1, sizeof(*m) + bloblen);
31 if (!m)
32 return false;
33
34 list_add(&m->list, &type->methods);
35 memcpy(m->data, attr, bloblen);
36 m->name = blobmsg_name(m->data);
37
38 return true;
39 }
40
41 static struct ubus_object_type *ubus_create_obj_type(struct blob_attr *sig)
42 {
43 struct ubus_object_type *type;
44 struct blob_attr *pos;
45 int rem;
46
47 type = calloc(1, sizeof(*type));
48 type->refcount = 1;
49
50 if (!ubus_alloc_id(&obj_types, &type->id, 0))
51 goto error_free;
52
53 INIT_LIST_HEAD(&type->methods);
54
55 blob_for_each_attr(pos, sig, rem) {
56 if (!blobmsg_check_attr(pos, true))
57 goto error_unref;
58
59 if (!ubus_create_obj_method(type, pos))
60 goto error_unref;
61 }
62
63 return type;
64
65 error_unref:
66 ubus_unref_object_type(type);
67 return NULL;
68
69 error_free:
70 free(type);
71 return NULL;
72 }
73
74 static struct ubus_object_type *ubus_get_obj_type(uint32_t obj_id)
75 {
76 struct ubus_object_type *type;
77 struct ubus_id *id;
78
79 id = ubus_find_id(&obj_types, obj_id);
80 if (!id)
81 return NULL;
82
83 type = container_of(id, struct ubus_object_type, id);
84 type->refcount++;
85 return type;
86 }
87
88 struct ubus_object *ubusd_create_object_internal(struct ubus_object_type *type, uint32_t id)
89 {
90 struct ubus_object *obj;
91
92 obj = calloc(1, sizeof(*obj));
93 if (!obj)
94 return NULL;
95
96 if (!ubus_alloc_id(&objects, &obj->id, id))
97 goto error_free;
98
99 obj->type = type;
100 INIT_LIST_HEAD(&obj->list);
101 type->refcount++;
102
103 return obj;
104
105 error_free:
106 free(obj);
107 return NULL;
108 }
109
110 struct ubus_object *ubusd_create_object(struct ubus_client *cl, struct blob_attr **attr)
111 {
112 struct ubus_object *obj;
113 struct ubus_object_type *type = NULL;
114
115 if (attr[UBUS_ATTR_OBJTYPE])
116 type = ubus_get_obj_type(blob_get_int32(attr[UBUS_ATTR_OBJTYPE]));
117 else if (attr[UBUS_ATTR_SIGNATURE])
118 type = ubus_create_obj_type(attr[UBUS_ATTR_SIGNATURE]);
119
120 if (!type)
121 return NULL;
122
123 obj = ubusd_create_object_internal(type, 0);
124 ubus_unref_object_type(type);
125
126 if (!obj)
127 return NULL;
128
129 if (attr[UBUS_ATTR_OBJPATH]) {
130 obj->path.key = strdup(blob_data(attr[UBUS_ATTR_OBJPATH]));
131 if (!obj->path.key)
132 goto free;
133
134 if (avl_insert(&path, &obj->path) != 0) {
135 free(obj->path.key);
136 obj->path.key = NULL;
137 goto free;
138 }
139 }
140
141 obj->client = cl;
142 list_add(&obj->list, &cl->objects);
143 return obj;
144
145 free:
146 ubusd_free_object(obj);
147 return NULL;
148 }
149
150 void ubusd_free_object(struct ubus_object *obj)
151 {
152 if (obj->path.key) {
153 avl_delete(&path, &obj->path);
154 free(obj->path.key);
155 }
156 if (!list_empty(&obj->list))
157 list_del(&obj->list);
158 ubus_free_id(&objects, &obj->id);
159 ubus_unref_object_type(obj->type);
160 free(obj);
161 }
162
163 static int ubus_cmp_path(const void *k1, const void *k2, void *ptr)
164 {
165 return strcmp(k1, k2);
166 }
167
168 static void __init ubusd_obj_init(void)
169 {
170 ubus_init_id_tree(&objects);
171 ubus_init_id_tree(&obj_types);
172 avl_init(&path, ubus_cmp_path, false, NULL);
173 }