a6be1823252cfb294b5128b777ecaf43a3e864d2
[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         if (type)
102                 type->refcount++;
103
104         return obj;
105
106 error_free:
107         free(obj);
108         return NULL;
109 }
110
111 struct ubus_object *ubusd_create_object(struct ubus_client *cl, struct blob_attr **attr)
112 {
113         struct ubus_object *obj;
114         struct ubus_object_type *type = NULL;
115
116         if (attr[UBUS_ATTR_OBJTYPE])
117                 type = ubus_get_obj_type(blob_get_int32(attr[UBUS_ATTR_OBJTYPE]));
118         else if (attr[UBUS_ATTR_SIGNATURE])
119                 type = ubus_create_obj_type(attr[UBUS_ATTR_SIGNATURE]);
120
121         if (!type)
122                 return NULL;
123
124         obj = ubusd_create_object_internal(type, 0);
125         ubus_unref_object_type(type);
126
127         if (!obj)
128                 return NULL;
129
130         if (attr[UBUS_ATTR_OBJPATH]) {
131                 obj->path.key = strdup(blob_data(attr[UBUS_ATTR_OBJPATH]));
132                 if (!obj->path.key)
133                         goto free;
134
135                 if (avl_insert(&path, &obj->path) != 0) {
136                         free(obj->path.key);
137                         obj->path.key = NULL;
138                         goto free;
139                 }
140         }
141
142         obj->client = cl;
143         list_add(&obj->list, &cl->objects);
144         return obj;
145
146 free:
147         ubusd_free_object(obj);
148         return NULL;
149 }
150
151 void ubusd_free_object(struct ubus_object *obj)
152 {
153         if (obj->path.key) {
154                 avl_delete(&path, &obj->path);
155                 free(obj->path.key);
156         }
157         if (!list_empty(&obj->list))
158                 list_del(&obj->list);
159         ubus_free_id(&objects, &obj->id);
160         if (obj->type)
161                 ubus_unref_object_type(obj->type);
162         free(obj);
163 }
164
165 static void __init ubusd_obj_init(void)
166 {
167         ubus_init_id_tree(&objects);
168         ubus_init_id_tree(&obj_types);
169         ubus_init_string_tree(&path, false);
170         ubusd_event_init();
171 }