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