Initial import
[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))
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(struct ubus_client *cl, struct blob_attr **attr)
89 {
90 struct ubus_object *obj;
91 struct ubus_object_type *type = NULL;
92
93 if (attr[UBUS_ATTR_OBJTYPE])
94 type = ubus_get_obj_type(blob_get_int32(attr[UBUS_ATTR_OBJTYPE]));
95 else if (attr[UBUS_ATTR_SIGNATURE])
96 type = ubus_create_obj_type(attr[UBUS_ATTR_SIGNATURE]);
97
98 if (!type)
99 return NULL;
100
101 obj = calloc(1, sizeof(*obj));
102 if (!ubus_alloc_id(&objects, &obj->id))
103 goto error_free;
104
105 if (attr[UBUS_ATTR_OBJPATH]) {
106 obj->path.key = strdup(blob_data(attr[UBUS_ATTR_OBJPATH]));
107 if (avl_insert(&path, &obj->path) != 0)
108 goto error_del_id;
109 }
110
111 obj->type = type;
112 list_add(&obj->list, &cl->objects);
113
114 return obj;
115
116 error_del_id:
117 free(obj->path.key);
118 ubus_free_id(&objects, &obj->id);
119 error_free:
120 ubus_unref_object_type(type);
121 free(obj);
122 return NULL;
123 }
124
125 void ubusd_free_object(struct ubus_object *obj)
126 {
127 if (obj->path.key) {
128 avl_delete(&path, &obj->path);
129 free(obj->path.key);
130 }
131 list_del(&obj->list);
132 ubus_free_id(&objects, &obj->id);
133 ubus_unref_object_type(obj->type);
134 free(obj);
135 }
136
137 static int ubus_cmp_path(const void *k1, const void *k2, void *ptr)
138 {
139 return strcmp(k1, k2);
140 }
141
142 static void __init ubusd_obj_init(void)
143 {
144 ubus_init_id_tree(&objects);
145 ubus_init_id_tree(&obj_types);
146 avl_init(&path, ubus_cmp_path, false, NULL);
147 }