libubus-req: add data_cb callback handling for ubus notifications
[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 close(req.req_fd);
99 if (req.deferred || no_reply)
100 return;
101
102 send:
103 ubus_complete_deferred_request(ctx, &req, ret);
104 }
105
106
107 void __hidden ubus_process_obj_msg(struct ubus_context *ctx, struct ubus_msghdr_buf *buf, int fd)
108 {
109 void (*cb)(struct ubus_context *, struct ubus_msghdr *,
110 struct ubus_object *, struct blob_attr **, int fd);
111 struct ubus_msghdr *hdr = &buf->hdr;
112 struct blob_attr **attrbuf;
113 struct ubus_object *obj;
114 uint32_t objid;
115 void *prev_data = NULL;
116 attrbuf = ubus_parse_msg(buf->data);
117 if (!attrbuf[UBUS_ATTR_OBJID])
118 return;
119
120 objid = blob_get_u32(attrbuf[UBUS_ATTR_OBJID]);
121 obj = avl_find_element(&ctx->objects, &objid, obj, avl);
122
123 switch (hdr->type) {
124 case UBUS_MSG_INVOKE:
125 cb = ubus_process_invoke;
126 break;
127 case UBUS_MSG_UNSUBSCRIBE:
128 cb = ubus_process_unsubscribe;
129 break;
130 case UBUS_MSG_NOTIFY:
131 cb = ubus_process_notify;
132 break;
133 default:
134 return;
135 }
136
137 if (buf == &ctx->msgbuf) {
138 prev_data = buf->data;
139 buf->data = NULL;
140 }
141
142 cb(ctx, hdr, obj, attrbuf, fd);
143
144 if (prev_data) {
145 if (buf->data)
146 free(prev_data);
147 else
148 buf->data = prev_data;
149 }
150 }
151
152 static void ubus_add_object_cb(struct ubus_request *req, int type, struct blob_attr *msg)
153 {
154 struct ubus_object *obj = req->priv;
155 struct blob_attr **attrbuf = ubus_parse_msg(msg);
156
157 if (!attrbuf[UBUS_ATTR_OBJID])
158 return;
159
160 obj->id = blob_get_u32(attrbuf[UBUS_ATTR_OBJID]);
161
162 if (attrbuf[UBUS_ATTR_OBJTYPE])
163 obj->type->id = blob_get_u32(attrbuf[UBUS_ATTR_OBJTYPE]);
164
165 obj->avl.key = &obj->id;
166 avl_insert(&req->ctx->objects, &obj->avl);
167 }
168
169 static void ubus_push_method_data(const struct ubus_method *m)
170 {
171 void *mtbl;
172 int i;
173
174 mtbl = blobmsg_open_table(&b, m->name);
175
176 for (i = 0; i < m->n_policy; i++) {
177 if (m->mask && !(m->mask & (1 << i)))
178 continue;
179
180 blobmsg_add_u32(&b, m->policy[i].name, m->policy[i].type);
181 }
182
183 blobmsg_close_table(&b, mtbl);
184 }
185
186 static bool ubus_push_object_type(const struct ubus_object_type *type)
187 {
188 void *s;
189 int i;
190
191 s = blob_nest_start(&b, UBUS_ATTR_SIGNATURE);
192
193 for (i = 0; i < type->n_methods; i++)
194 ubus_push_method_data(&type->methods[i]);
195
196 blob_nest_end(&b, s);
197
198 return true;
199 }
200
201 int ubus_add_object(struct ubus_context *ctx, struct ubus_object *obj)
202 {
203 struct ubus_request req;
204 int ret;
205
206 blob_buf_init(&b, 0);
207
208 if (obj->name && obj->type) {
209 blob_put_string(&b, UBUS_ATTR_OBJPATH, obj->name);
210
211 if (obj->type->id)
212 blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id);
213 else if (!ubus_push_object_type(obj->type))
214 return UBUS_STATUS_INVALID_ARGUMENT;
215 }
216
217 if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_ADD_OBJECT, 0) < 0)
218 return UBUS_STATUS_INVALID_ARGUMENT;
219
220 req.raw_data_cb = ubus_add_object_cb;
221 req.priv = obj;
222 ret = ubus_complete_request(ctx, &req, 0);
223 if (ret)
224 return ret;
225
226 if (!obj->id)
227 return UBUS_STATUS_NO_DATA;
228
229 return 0;
230 }
231
232 static void ubus_remove_object_cb(struct ubus_request *req, int type, struct blob_attr *msg)
233 {
234 struct ubus_object *obj = req->priv;
235 struct blob_attr **attrbuf = ubus_parse_msg(msg);
236
237 if (!attrbuf[UBUS_ATTR_OBJID])
238 return;
239
240 obj->id = 0;
241
242 if (attrbuf[UBUS_ATTR_OBJTYPE] && obj->type)
243 obj->type->id = 0;
244
245 avl_delete(&req->ctx->objects, &obj->avl);
246 }
247
248 int ubus_remove_object(struct ubus_context *ctx, struct ubus_object *obj)
249 {
250 struct ubus_request req;
251 int ret;
252
253 blob_buf_init(&b, 0);
254 blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id);
255
256 if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_REMOVE_OBJECT, 0) < 0)
257 return UBUS_STATUS_INVALID_ARGUMENT;
258
259 req.raw_data_cb = ubus_remove_object_cb;
260 req.priv = obj;
261 ret = ubus_complete_request(ctx, &req, 0);
262 if (ret)
263 return ret;
264
265 if (obj->id)
266 return UBUS_STATUS_NO_DATA;
267
268 return 0;
269 }