remove an unused-but-set variable
[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         if (!!type ^ !!attr[UBUS_ATTR_OBJPATH])
136                 return NULL;
137
138         obj = ubusd_create_object_internal(type, 0);
139         if (type)
140                 ubus_unref_object_type(type);
141
142         if (!obj)
143                 return NULL;
144
145         if (attr[UBUS_ATTR_OBJPATH]) {
146                 obj->path.key = strdup(blob_data(attr[UBUS_ATTR_OBJPATH]));
147                 if (!obj->path.key)
148                         goto free;
149
150                 if (avl_insert(&path, &obj->path) != 0) {
151                         free((void *) obj->path.key);
152                         obj->path.key = NULL;
153                         goto free;
154                 }
155                 ubusd_send_obj_event(obj, true);
156         }
157
158         obj->client = cl;
159         list_add(&obj->list, &cl->objects);
160
161         return obj;
162
163 free:
164         ubusd_free_object(obj);
165         return NULL;
166 }
167
168 void ubusd_free_object(struct ubus_object *obj)
169 {
170         ubusd_event_cleanup_object(obj);
171         if (obj->path.key) {
172                 ubusd_send_obj_event(obj, false);
173                 avl_delete(&path, &obj->path);
174                 free((void *) obj->path.key);
175         }
176         if (!list_empty(&obj->list))
177                 list_del(&obj->list);
178         ubus_free_id(&objects, &obj->id);
179         if (obj->type)
180                 ubus_unref_object_type(obj->type);
181         free(obj);
182 }
183
184 static void __init ubusd_obj_init(void)
185 {
186         ubus_init_id_tree(&objects);
187         ubus_init_id_tree(&obj_types);
188         ubus_init_string_tree(&path, false);
189         ubusd_event_init();
190 }