fix invalid close() call
[project/ubus.git] / libubus-obj.c
1 /*
2 * Copyright (C) 2011-2012 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 <unistd.h>
15 #include "libubus.h"
16 #include "libubus-internal.h"
17
18 static void
19 ubus_process_unsubscribe(struct ubus_context *ctx, struct ubus_msghdr *hdr,
20 struct ubus_object *obj, struct blob_attr **attrbuf, int fd)
21 {
22 struct ubus_subscriber *s;
23
24 if (!obj || !attrbuf[UBUS_ATTR_TARGET])
25 return;
26
27 if (obj->methods != &watch_method)
28 return;
29
30 s = container_of(obj, struct ubus_subscriber, obj);
31 if (s->remove_cb)
32 s->remove_cb(ctx, s, blob_get_u32(attrbuf[UBUS_ATTR_TARGET]));
33
34 close(fd);
35 }
36
37 static void
38 ubus_process_notify(struct ubus_context *ctx, struct ubus_msghdr *hdr,
39 struct ubus_object *obj, struct blob_attr **attrbuf, int fd)
40 {
41 if (!obj || !attrbuf[UBUS_ATTR_ACTIVE])
42 return;
43
44 obj->has_subscribers = blob_get_u8(attrbuf[UBUS_ATTR_ACTIVE]);
45 if (obj->subscribe_cb)
46 obj->subscribe_cb(ctx, obj);
47
48 close(fd);
49 }
50 static void
51 ubus_process_invoke(struct ubus_context *ctx, struct ubus_msghdr *hdr,
52 struct ubus_object *obj, struct blob_attr **attrbuf, int fd)
53 {
54 struct ubus_request_data req = {
55 .fd = -1,
56 .req_fd = fd,
57 };
58
59 int method;
60 int ret;
61 bool no_reply = false;
62
63 if (!obj) {
64 ret = UBUS_STATUS_NOT_FOUND;
65 goto send;
66 }
67
68 if (!attrbuf[UBUS_ATTR_METHOD]) {
69 ret = UBUS_STATUS_INVALID_ARGUMENT;
70 goto send;
71 }
72
73 if (attrbuf[UBUS_ATTR_NO_REPLY])
74 no_reply = blob_get_int8(attrbuf[UBUS_ATTR_NO_REPLY]);
75
76 req.peer = hdr->peer;
77 req.seq = hdr->seq;
78 req.object = obj->id;
79 if (attrbuf[UBUS_ATTR_USER] && attrbuf[UBUS_ATTR_GROUP]) {
80 req.acl.user = blobmsg_get_string(attrbuf[UBUS_ATTR_USER]);
81 req.acl.group = blobmsg_get_string(attrbuf[UBUS_ATTR_GROUP]);
82 req.acl.object = obj->name;
83 }
84 for (method = 0; method < obj->n_methods; method++)
85 if (!obj->methods[method].name ||
86 !strcmp(obj->methods[method].name,
87 blob_data(attrbuf[UBUS_ATTR_METHOD])))
88 goto found;
89
90 /* not found */
91 ret = UBUS_STATUS_METHOD_NOT_FOUND;
92 goto send;
93
94 found:
95 ret = obj->methods[method].handler(ctx, obj, &req,
96 blob_data(attrbuf[UBUS_ATTR_METHOD]),
97 attrbuf[UBUS_ATTR_DATA]);
98 if (req.req_fd >= 0)
99 close(req.req_fd);
100 if (req.deferred || no_reply)
101 return;
102
103 send:
104 ubus_complete_deferred_request(ctx, &req, ret);
105 }
106
107
108 void __hidden ubus_process_obj_msg(struct ubus_context *ctx, struct ubus_msghdr_buf *buf, int fd)
109 {
110 void (*cb)(struct ubus_context *, struct ubus_msghdr *,
111 struct ubus_object *, struct blob_attr **, int fd);
112 struct ubus_msghdr *hdr = &buf->hdr;
113 struct blob_attr **attrbuf;
114 struct ubus_object *obj;
115 uint32_t objid;
116 void *prev_data = NULL;
117 attrbuf = ubus_parse_msg(buf->data);
118 if (!attrbuf[UBUS_ATTR_OBJID])
119 return;
120
121 objid = blob_get_u32(attrbuf[UBUS_ATTR_OBJID]);
122 obj = avl_find_element(&ctx->objects, &objid, obj, avl);
123
124 switch (hdr->type) {
125 case UBUS_MSG_INVOKE:
126 cb = ubus_process_invoke;
127 break;
128 case UBUS_MSG_UNSUBSCRIBE:
129 cb = ubus_process_unsubscribe;
130 break;
131 case UBUS_MSG_NOTIFY:
132 cb = ubus_process_notify;
133 break;
134 default:
135 return;
136 }
137
138 if (buf == &ctx->msgbuf) {
139 prev_data = buf->data;
140 buf->data = NULL;
141 }
142
143 cb(ctx, hdr, obj, attrbuf, fd);
144
145 if (prev_data) {
146 if (buf->data)
147 free(prev_data);
148 else
149 buf->data = prev_data;
150 }
151 }
152
153 static void ubus_add_object_cb(struct ubus_request *req, int type, struct blob_attr *msg)
154 {
155 struct ubus_object *obj = req->priv;
156 struct blob_attr **attrbuf = ubus_parse_msg(msg);
157
158 if (!attrbuf[UBUS_ATTR_OBJID])
159 return;
160
161 obj->id = blob_get_u32(attrbuf[UBUS_ATTR_OBJID]);
162
163 if (attrbuf[UBUS_ATTR_OBJTYPE])
164 obj->type->id = blob_get_u32(attrbuf[UBUS_ATTR_OBJTYPE]);
165
166 obj->avl.key = &obj->id;
167 avl_insert(&req->ctx->objects, &obj->avl);
168 }
169
170 static void ubus_push_method_data(const struct ubus_method *m)
171 {
172 void *mtbl;
173 int i;
174
175 mtbl = blobmsg_open_table(&b, m->name);
176
177 for (i = 0; i < m->n_policy; i++) {
178 if (m->mask && !(m->mask & (1 << i)))
179 continue;
180
181 blobmsg_add_u32(&b, m->policy[i].name, m->policy[i].type);
182 }
183
184 blobmsg_close_table(&b, mtbl);
185 }
186
187 static bool ubus_push_object_type(const struct ubus_object_type *type)
188 {
189 void *s;
190 int i;
191
192 s = blob_nest_start(&b, UBUS_ATTR_SIGNATURE);
193
194 for (i = 0; i < type->n_methods; i++)
195 ubus_push_method_data(&type->methods[i]);
196
197 blob_nest_end(&b, s);
198
199 return true;
200 }
201
202 int ubus_add_object(struct ubus_context *ctx, struct ubus_object *obj)
203 {
204 struct ubus_request req;
205 int ret;
206
207 blob_buf_init(&b, 0);
208
209 if (obj->name && obj->type) {
210 blob_put_string(&b, UBUS_ATTR_OBJPATH, obj->name);
211
212 if (obj->type->id)
213 blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id);
214 else if (!ubus_push_object_type(obj->type))
215 return UBUS_STATUS_INVALID_ARGUMENT;
216 }
217
218 if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_ADD_OBJECT, 0) < 0)
219 return UBUS_STATUS_INVALID_ARGUMENT;
220
221 req.raw_data_cb = ubus_add_object_cb;
222 req.priv = obj;
223 ret = ubus_complete_request(ctx, &req, 0);
224 if (ret)
225 return ret;
226
227 if (!obj->id)
228 return UBUS_STATUS_NO_DATA;
229
230 return 0;
231 }
232
233 static void ubus_remove_object_cb(struct ubus_request *req, int type, struct blob_attr *msg)
234 {
235 struct ubus_object *obj = req->priv;
236 struct blob_attr **attrbuf = ubus_parse_msg(msg);
237
238 if (!attrbuf[UBUS_ATTR_OBJID])
239 return;
240
241 avl_delete(&req->ctx->objects, &obj->avl);
242
243 obj->id = 0;
244
245 if (attrbuf[UBUS_ATTR_OBJTYPE] && obj->type)
246 obj->type->id = 0;
247 }
248
249 int ubus_remove_object(struct ubus_context *ctx, struct ubus_object *obj)
250 {
251 struct ubus_request req;
252 int ret;
253
254 blob_buf_init(&b, 0);
255 blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id);
256
257 if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_REMOVE_OBJECT, 0) < 0)
258 return UBUS_STATUS_INVALID_ARGUMENT;
259
260 req.raw_data_cb = ubus_remove_object_cb;
261 req.priv = obj;
262 ret = ubus_complete_request(ctx, &req, 0);
263 if (ret)
264 return ret;
265
266 if (obj->id)
267 return UBUS_STATUS_NO_DATA;
268
269 return 0;
270 }