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