d44db51f315922362e62f02931cc88f4e08af3cb
[project/ubus.git] / libubus-req.c
1 /*
2 * Copyright (C) 2011-2014 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 struct ubus_pending_data {
19 struct list_head list;
20 int type;
21 struct blob_attr data[];
22 };
23
24 static void req_data_cb(struct ubus_request *req, int type, struct blob_attr *data)
25 {
26 struct blob_attr **attr;
27
28 if (req->raw_data_cb)
29 req->raw_data_cb(req, type, data);
30
31 if (!req->data_cb)
32 return;
33
34 attr = ubus_parse_msg(data);
35 req->data_cb(req, type, attr[UBUS_ATTR_DATA]);
36 }
37
38 static void __ubus_process_req_data(struct ubus_request *req)
39 {
40 struct ubus_pending_data *data;
41
42 while (!list_empty(&req->pending)) {
43 data = list_first_entry(&req->pending,
44 struct ubus_pending_data, list);
45 list_del(&data->list);
46 if (!req->cancelled)
47 req_data_cb(req, data->type, data->data);
48 free(data);
49 }
50 }
51
52 int __hidden ubus_start_request(struct ubus_context *ctx, struct ubus_request *req,
53 struct blob_attr *msg, int cmd, uint32_t peer)
54 {
55 memset(req, 0, sizeof(*req));
56
57 if (msg && blob_pad_len(msg) > UBUS_MAX_MSGLEN)
58 return -1;
59
60 INIT_LIST_HEAD(&req->list);
61 INIT_LIST_HEAD(&req->pending);
62 req->ctx = ctx;
63 req->peer = peer;
64 req->seq = ++ctx->request_seq;
65 return ubus_send_msg(ctx, req->seq, msg, cmd, peer, -1);
66 }
67
68 void ubus_abort_request(struct ubus_context *ctx, struct ubus_request *req)
69 {
70 if (list_empty(&req->list))
71 return;
72
73 req->cancelled = true;
74 __ubus_process_req_data(req);
75 list_del_init(&req->list);
76 }
77
78 void ubus_complete_request_async(struct ubus_context *ctx, struct ubus_request *req)
79 {
80 if (!list_empty(&req->list))
81 return;
82
83 list_add(&req->list, &ctx->requests);
84 }
85
86 static void
87 ubus_req_complete_cb(struct ubus_request *req)
88 {
89 ubus_complete_handler_t cb = req->complete_cb;
90
91 if (!cb)
92 return;
93
94 req->complete_cb = NULL;
95 cb(req, req->status_code);
96 }
97
98 static void
99 ubus_set_req_status(struct ubus_request *req, int ret)
100 {
101 if (!list_empty(&req->list))
102 list_del_init(&req->list);
103
104 req->status_msg = true;
105 req->status_code = ret;
106 if (!req->blocked)
107 ubus_req_complete_cb(req);
108 }
109
110 static void ubus_sync_req_cb(struct ubus_request *req, int ret)
111 {
112 req->status_msg = true;
113 req->status_code = ret;
114 uloop_end();
115 }
116
117 static int64_t get_time_msec(void)
118 {
119 struct timespec ts;
120 int64_t val;
121
122 clock_gettime(CLOCK_MONOTONIC, &ts);
123 val = (int64_t) ts.tv_sec * 1000LL;
124 val += ts.tv_nsec / 1000000LL;
125 return val;
126 }
127
128 int ubus_complete_request(struct ubus_context *ctx, struct ubus_request *req,
129 int req_timeout)
130 {
131 ubus_complete_handler_t complete_cb = req->complete_cb;
132 bool registered = ctx->sock.registered;
133 int status = UBUS_STATUS_NO_DATA;
134 int64_t timeout = 0, time_end = 0;
135
136 if (!registered) {
137 uloop_init();
138 ubus_add_uloop(ctx);
139 }
140
141 if (req_timeout)
142 time_end = get_time_msec() + req_timeout;
143
144 ubus_complete_request_async(ctx, req);
145 req->complete_cb = ubus_sync_req_cb;
146
147 ctx->stack_depth++;
148 while (!req->status_msg) {
149 bool cancelled = uloop_cancelled;
150
151 uloop_cancelled = false;
152 if (req_timeout) {
153 timeout = time_end - get_time_msec();
154 if (timeout <= 0) {
155 ubus_set_req_status(req, UBUS_STATUS_TIMEOUT);
156 uloop_cancelled = cancelled;
157 break;
158 }
159 }
160 ubus_poll_data(ctx, (unsigned int) timeout);
161
162 uloop_cancelled = cancelled;
163 }
164 ctx->stack_depth--;
165 if (ctx->stack_depth)
166 uloop_cancelled = true;
167
168 if (req->status_msg)
169 status = req->status_code;
170
171 req->complete_cb = complete_cb;
172 if (req->complete_cb)
173 req->complete_cb(req, status);
174
175 if (!registered) {
176 uloop_fd_delete(&ctx->sock);
177
178 if (ctx->stack_depth)
179 ctx->pending_timer.cb(&ctx->pending_timer);
180 }
181
182 return status;
183 }
184
185 void ubus_complete_deferred_request(struct ubus_context *ctx, struct ubus_request_data *req, int ret)
186 {
187 blob_buf_init(&b, 0);
188 blob_put_int32(&b, UBUS_ATTR_STATUS, ret);
189 blob_put_int32(&b, UBUS_ATTR_OBJID, req->object);
190 ubus_send_msg(ctx, req->seq, b.head, UBUS_MSG_STATUS, req->peer, req->fd);
191 }
192
193 int ubus_send_reply(struct ubus_context *ctx, struct ubus_request_data *req,
194 struct blob_attr *msg)
195 {
196 int ret;
197
198 blob_buf_init(&b, 0);
199 blob_put_int32(&b, UBUS_ATTR_OBJID, req->object);
200 blob_put(&b, UBUS_ATTR_DATA, blob_data(msg), blob_len(msg));
201 ret = ubus_send_msg(ctx, req->seq, b.head, UBUS_MSG_DATA, req->peer, -1);
202 if (ret < 0)
203 return UBUS_STATUS_NO_DATA;
204
205 return 0;
206 }
207
208 int ubus_invoke_async(struct ubus_context *ctx, uint32_t obj, const char *method,
209 struct blob_attr *msg, struct ubus_request *req)
210 {
211 blob_buf_init(&b, 0);
212 blob_put_int32(&b, UBUS_ATTR_OBJID, obj);
213 blob_put_string(&b, UBUS_ATTR_METHOD, method);
214 if (msg)
215 blob_put(&b, UBUS_ATTR_DATA, blob_data(msg), blob_len(msg));
216
217 if (ubus_start_request(ctx, req, b.head, UBUS_MSG_INVOKE, obj) < 0)
218 return UBUS_STATUS_INVALID_ARGUMENT;
219
220 return 0;
221 }
222
223 int ubus_invoke(struct ubus_context *ctx, uint32_t obj, const char *method,
224 struct blob_attr *msg, ubus_data_handler_t cb, void *priv,
225 int timeout)
226 {
227 struct ubus_request req;
228 int rc;
229
230 rc = ubus_invoke_async(ctx, obj, method, msg, &req);
231 if (rc)
232 return rc;
233
234 req.data_cb = cb;
235 req.priv = priv;
236 return ubus_complete_request(ctx, &req, timeout);
237 }
238
239 static void
240 ubus_notify_complete_cb(struct ubus_request *req, int ret)
241 {
242 struct ubus_notify_request *nreq;
243
244 nreq = container_of(req, struct ubus_notify_request, req);
245 if (!nreq->complete_cb)
246 return;
247
248 nreq->complete_cb(nreq, 0, 0);
249 }
250
251 static int
252 __ubus_notify_async(struct ubus_context *ctx, struct ubus_object *obj,
253 const char *type, struct blob_attr *msg,
254 struct ubus_notify_request *req, bool reply)
255 {
256 memset(req, 0, sizeof(*req));
257
258 blob_buf_init(&b, 0);
259 blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id);
260 blob_put_string(&b, UBUS_ATTR_METHOD, type);
261
262 if (!reply)
263 blob_put_int8(&b, UBUS_ATTR_NO_REPLY, true);
264
265 if (msg)
266 blob_put(&b, UBUS_ATTR_DATA, blob_data(msg), blob_len(msg));
267
268 if (ubus_start_request(ctx, &req->req, b.head, UBUS_MSG_NOTIFY, obj->id) < 0)
269 return UBUS_STATUS_INVALID_ARGUMENT;
270
271 /* wait for status message from ubusd first */
272 req->req.notify = true;
273 req->pending = 1;
274 req->id[0] = obj->id;
275 req->req.complete_cb = ubus_notify_complete_cb;
276
277 return 0;
278 }
279
280 int ubus_notify_async(struct ubus_context *ctx, struct ubus_object *obj,
281 const char *type, struct blob_attr *msg,
282 struct ubus_notify_request *req)
283 {
284 return __ubus_notify_async(ctx, obj, type, msg, req, true);
285 }
286
287 int ubus_notify(struct ubus_context *ctx, struct ubus_object *obj,
288 const char *type, struct blob_attr *msg, int timeout)
289 {
290 struct ubus_notify_request req;
291 int ret;
292
293 ret = __ubus_notify_async(ctx, obj, type, msg, &req, timeout >= 0);
294 if (ret < 0)
295 return ret;
296
297 if (timeout < 0) {
298 ubus_abort_request(ctx, &req.req);
299 return 0;
300 }
301
302 return ubus_complete_request(ctx, &req.req, timeout);
303 }
304
305 static bool ubus_get_status(struct ubus_msghdr_buf *buf, int *ret)
306 {
307 struct blob_attr **attrbuf = ubus_parse_msg(buf->data);
308
309 if (!attrbuf[UBUS_ATTR_STATUS])
310 return false;
311
312 *ret = blob_get_u32(attrbuf[UBUS_ATTR_STATUS]);
313 return true;
314 }
315
316 static int
317 ubus_process_req_status(struct ubus_request *req, struct ubus_msghdr_buf *buf)
318 {
319 int ret = UBUS_STATUS_INVALID_ARGUMENT;
320
321 ubus_get_status(buf, &ret);
322 req->peer = buf->hdr.peer;
323 ubus_set_req_status(req, ret);
324
325 return ret;
326 }
327
328 static void
329 ubus_process_req_data(struct ubus_request *req, struct ubus_msghdr_buf *buf)
330 {
331 struct ubus_pending_data *data;
332 int len;
333
334 if (!req->blocked) {
335 req->blocked = true;
336 req_data_cb(req, buf->hdr.type, buf->data);
337 __ubus_process_req_data(req);
338 req->blocked = false;
339
340 if (req->status_msg)
341 ubus_req_complete_cb(req);
342
343 return;
344 }
345
346 len = blob_raw_len(buf->data);
347 data = calloc(1, sizeof(*data) + len);
348 if (!data)
349 return;
350
351 data->type = buf->hdr.type;
352 memcpy(data->data, buf->data, len);
353 list_add(&data->list, &req->pending);
354 }
355
356 static int
357 ubus_find_notify_id(struct ubus_notify_request *n, uint32_t objid)
358 {
359 uint32_t pending = n->pending;
360 int i;
361
362 for (i = 0; pending; i++, pending >>= 1) {
363 if (!(pending & 1))
364 continue;
365
366 if (n->id[i] == objid)
367 return i;
368 }
369
370 return -1;
371 }
372
373 static struct ubus_request *
374 ubus_find_request(struct ubus_context *ctx, uint32_t seq, uint32_t peer, int *id)
375 {
376 struct ubus_request *req;
377
378 list_for_each_entry(req, &ctx->requests, list) {
379 struct ubus_notify_request *nreq;
380 nreq = container_of(req, struct ubus_notify_request, req);
381
382 if (seq != req->seq)
383 continue;
384
385 if (req->notify) {
386 if (!nreq->pending)
387 continue;
388
389 *id = ubus_find_notify_id(nreq, peer);
390 if (*id < 0)
391 continue;
392 } else if (peer != req->peer)
393 continue;
394
395 return req;
396 }
397 return NULL;
398 }
399
400 static void ubus_process_notify_status(struct ubus_request *req, int id, struct ubus_msghdr_buf *buf)
401 {
402 struct ubus_notify_request *nreq;
403 struct blob_attr **tb;
404 struct blob_attr *cur;
405 int rem, idx = 1;
406 int ret = 0;
407
408 nreq = container_of(req, struct ubus_notify_request, req);
409 nreq->pending &= ~(1 << id);
410
411 if (!id) {
412 /* first id: ubusd's status message with a list of ids */
413 tb = ubus_parse_msg(buf->data);
414 if (tb[UBUS_ATTR_SUBSCRIBERS]) {
415 blob_for_each_attr(cur, tb[UBUS_ATTR_SUBSCRIBERS], rem) {
416 if (!blob_check_type(blob_data(cur), blob_len(cur), BLOB_ATTR_INT32))
417 continue;
418
419 nreq->pending |= (1 << idx);
420 nreq->id[idx] = blob_get_int32(cur);
421 idx++;
422
423 if (idx == UBUS_MAX_NOTIFY_PEERS + 1)
424 break;
425 }
426 }
427 } else {
428 ubus_get_status(buf, &ret);
429 if (nreq->status_cb)
430 nreq->status_cb(nreq, id, ret);
431 }
432
433 if (!nreq->pending)
434 ubus_set_req_status(req, 0);
435 }
436
437 void __hidden ubus_process_req_msg(struct ubus_context *ctx, struct ubus_msghdr_buf *buf, int fd)
438 {
439 struct ubus_msghdr *hdr = &buf->hdr;
440 struct ubus_request *req;
441 int id = -1;
442
443 switch(hdr->type) {
444 case UBUS_MSG_STATUS:
445 req = ubus_find_request(ctx, hdr->seq, hdr->peer, &id);
446 if (!req)
447 break;
448
449 if (fd >= 0) {
450 if (req->fd_cb)
451 req->fd_cb(req, fd);
452 else
453 close(fd);
454 }
455
456 if (id >= 0)
457 ubus_process_notify_status(req, id, buf);
458 else
459 ubus_process_req_status(req, buf);
460 break;
461
462 case UBUS_MSG_DATA:
463 req = ubus_find_request(ctx, hdr->seq, hdr->peer, &id);
464 if (req && (req->data_cb || req->raw_data_cb))
465 ubus_process_req_data(req, buf);
466 break;
467 }
468 }