5180a6f828625a6ca476ff7cb837c414d7d27f23
[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                 uloop_init();
149                 ubus_add_uloop(ctx);
150         }
151
152         if (req_timeout)
153                 time_end = get_time_msec() + req_timeout;
154
155         ubus_complete_request_async(ctx, req);
156         req->complete_cb = ubus_sync_req_cb;
157
158         ctx->stack_depth++;
159         while (!req->status_msg) {
160                 bool cancelled = uloop_cancelled;
161
162                 uloop_cancelled = false;
163                 if (req_timeout) {
164                         timeout = time_end - get_time_msec();
165                         if (timeout <= 0) {
166                                 ubus_set_req_status(req, UBUS_STATUS_TIMEOUT);
167                                 uloop_cancelled = cancelled;
168                                 break;
169                         }
170                 }
171                 ubus_poll_data(ctx, (unsigned int) timeout);
172
173                 uloop_cancelled = cancelled;
174                 if (ctx->sock.eof) {
175                         ubus_set_req_status(req, UBUS_STATUS_CONNECTION_FAILED);
176                         break;
177                 }
178         }
179         ctx->stack_depth--;
180         if (ctx->stack_depth)
181                 uloop_cancelled = true;
182
183         if (req->status_msg)
184                 status = req->status_code;
185
186         req->complete_cb = complete_cb;
187         if (req->complete_cb)
188                 req->complete_cb(req, status);
189
190         if (!registered) {
191                 uloop_fd_delete(&ctx->sock);
192
193                 if (!ctx->stack_depth)
194                         ctx->pending_timer.cb(&ctx->pending_timer);
195         }
196
197         return status;
198 }
199
200 void ubus_complete_deferred_request(struct ubus_context *ctx, struct ubus_request_data *req, int ret)
201 {
202         blob_buf_init(&b, 0);
203         blob_put_int32(&b, UBUS_ATTR_STATUS, ret);
204         blob_put_int32(&b, UBUS_ATTR_OBJID, req->object);
205         ubus_send_msg(ctx, req->seq, b.head, UBUS_MSG_STATUS, req->peer, req->fd);
206 }
207
208 int ubus_send_reply(struct ubus_context *ctx, struct ubus_request_data *req,
209                     struct blob_attr *msg)
210 {
211         int ret;
212
213         blob_buf_init(&b, 0);
214         blob_put_int32(&b, UBUS_ATTR_OBJID, req->object);
215         blob_put(&b, UBUS_ATTR_DATA, blob_data(msg), blob_len(msg));
216         ret = ubus_send_msg(ctx, req->seq, b.head, UBUS_MSG_DATA, req->peer, -1);
217         if (ret < 0)
218                 return UBUS_STATUS_NO_DATA;
219
220         return 0;
221 }
222
223 int ubus_invoke_async(struct ubus_context *ctx, uint32_t obj, const char *method,
224                        struct blob_attr *msg, struct ubus_request *req)
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         if (ubus_start_request(ctx, req, b.head, UBUS_MSG_INVOKE, obj) < 0)
233                 return UBUS_STATUS_INVALID_ARGUMENT;
234
235         return 0;
236 }
237
238
239 int ubus_invoke_async_fd(struct ubus_context *ctx, uint32_t obj, const char *method,
240                        struct blob_attr *msg, struct ubus_request *req, int fd)
241 {
242         blob_buf_init(&b, 0);
243         blob_put_int32(&b, UBUS_ATTR_OBJID, obj);
244         blob_put_string(&b, UBUS_ATTR_METHOD, method);
245         if (msg)
246                 blob_put(&b, UBUS_ATTR_DATA, blob_data(msg), blob_len(msg));
247
248         memset(req, 0, sizeof(*req));
249         req->fd = fd;
250         if (__ubus_start_request(ctx, req, b.head, UBUS_MSG_INVOKE, obj) < 0)
251                 return UBUS_STATUS_INVALID_ARGUMENT;
252         return 0;
253 }
254
255 int ubus_invoke(struct ubus_context *ctx, uint32_t obj, const char *method,
256                 struct blob_attr *msg, ubus_data_handler_t cb, void *priv,
257                 int timeout)
258 {
259         struct ubus_request req;
260         int rc;
261
262         rc = ubus_invoke_async(ctx, obj, method, msg, &req);
263         if (rc)
264                 return rc;
265
266         req.data_cb = cb;
267         req.priv = priv;
268         return ubus_complete_request(ctx, &req, timeout);
269 }
270
271 int ubus_invoke_fd(struct ubus_context *ctx, uint32_t obj, const char *method,
272                 struct blob_attr *msg, ubus_data_handler_t cb, void *priv,
273                 int timeout, int fd)
274 {
275         struct ubus_request req;
276         int rc;
277
278         rc = ubus_invoke_async_fd(ctx, obj, method, msg, &req, fd);
279         if (rc)
280                 return rc;
281
282         req.data_cb = cb;
283         req.priv = priv;
284         return ubus_complete_request(ctx, &req, timeout);
285 }
286
287 static void
288 ubus_notify_complete_cb(struct ubus_request *req, int ret)
289 {
290         struct ubus_notify_request *nreq;
291
292         nreq = container_of(req, struct ubus_notify_request, req);
293         if (!nreq->complete_cb)
294                 return;
295
296         nreq->complete_cb(nreq, 0, 0);
297 }
298
299 static int
300 __ubus_notify_async(struct ubus_context *ctx, struct ubus_object *obj,
301                     const char *type, struct blob_attr *msg,
302                     struct ubus_notify_request *req, bool reply)
303 {
304         memset(req, 0, sizeof(*req));
305
306         blob_buf_init(&b, 0);
307         blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id);
308         blob_put_string(&b, UBUS_ATTR_METHOD, type);
309
310         if (!reply)
311                 blob_put_int8(&b, UBUS_ATTR_NO_REPLY, true);
312
313         if (msg)
314                 blob_put(&b, UBUS_ATTR_DATA, blob_data(msg), blob_len(msg));
315
316         if (ubus_start_request(ctx, &req->req, b.head, UBUS_MSG_NOTIFY, obj->id) < 0)
317                 return UBUS_STATUS_INVALID_ARGUMENT;
318
319         /* wait for status message from ubusd first */
320         req->req.notify = true;
321         req->pending = 1;
322         req->id[0] = obj->id;
323         req->req.complete_cb = ubus_notify_complete_cb;
324
325         return 0;
326 }
327
328 int ubus_notify_async(struct ubus_context *ctx, struct ubus_object *obj,
329                       const char *type, struct blob_attr *msg,
330                       struct ubus_notify_request *req)
331 {
332         return __ubus_notify_async(ctx, obj, type, msg, req, true);
333 }
334
335 int ubus_notify(struct ubus_context *ctx, struct ubus_object *obj,
336                 const char *type, struct blob_attr *msg, int timeout)
337 {
338         struct ubus_notify_request req;
339         int ret;
340
341         ret = __ubus_notify_async(ctx, obj, type, msg, &req, timeout >= 0);
342         if (ret < 0)
343                 return ret;
344
345         if (timeout < 0) {
346                 ubus_abort_request(ctx, &req.req);
347                 return 0;
348         }
349
350         return ubus_complete_request(ctx, &req.req, timeout);
351 }
352
353 static bool ubus_get_status(struct ubus_msghdr_buf *buf, int *ret)
354 {
355         struct blob_attr **attrbuf = ubus_parse_msg(buf->data);
356
357         if (!attrbuf[UBUS_ATTR_STATUS])
358                 return false;
359
360         *ret = blob_get_u32(attrbuf[UBUS_ATTR_STATUS]);
361         return true;
362 }
363
364 static int
365 ubus_process_req_status(struct ubus_request *req, struct ubus_msghdr_buf *buf)
366 {
367         int ret = UBUS_STATUS_INVALID_ARGUMENT;
368
369         ubus_get_status(buf, &ret);
370         req->peer = buf->hdr.peer;
371         ubus_set_req_status(req, ret);
372
373         return ret;
374 }
375
376 static void
377 ubus_process_req_data(struct ubus_request *req, struct ubus_msghdr_buf *buf)
378 {
379         struct ubus_pending_data *data;
380         int len;
381
382         if (!req->blocked) {
383                 req->blocked = true;
384                 req_data_cb(req, buf->hdr.type, buf->data);
385                 __ubus_process_req_data(req);
386                 req->blocked = false;
387
388                 if (req->status_msg)
389                         ubus_req_complete_cb(req);
390
391                 return;
392         }
393
394         len = blob_raw_len(buf->data);
395         data = calloc(1, sizeof(*data) + len);
396         if (!data)
397                 return;
398
399         data->type = buf->hdr.type;
400         memcpy(data->data, buf->data, len);
401         list_add(&data->list, &req->pending);
402 }
403
404 static int
405 ubus_find_notify_id(struct ubus_notify_request *n, uint32_t objid)
406 {
407         uint32_t pending = n->pending;
408         int i;
409
410         for (i = 0; pending; i++, pending >>= 1) {
411                 if (!(pending & 1))
412                         continue;
413
414                 if (n->id[i] == objid)
415                         return i;
416         }
417
418         return -1;
419 }
420
421 static struct ubus_request *
422 ubus_find_request(struct ubus_context *ctx, uint32_t seq, uint32_t peer, int *id)
423 {
424         struct ubus_request *req;
425
426         list_for_each_entry(req, &ctx->requests, list) {
427                 struct ubus_notify_request *nreq;
428                 nreq = container_of(req, struct ubus_notify_request, req);
429
430                 if (seq != req->seq)
431                         continue;
432
433                 if (req->notify) {
434                         if (!nreq->pending)
435                                 continue;
436
437                         *id = ubus_find_notify_id(nreq, peer);
438                         if (*id < 0)
439                                 continue;
440                 } else if (peer != req->peer)
441                         continue;
442
443                 return req;
444         }
445         return NULL;
446 }
447
448 static void ubus_process_notify_status(struct ubus_request *req, int id, struct ubus_msghdr_buf *buf)
449 {
450         struct ubus_notify_request *nreq;
451         struct blob_attr **tb;
452         struct blob_attr *cur;
453         int rem, idx = 1;
454         int ret = 0;
455
456         nreq = container_of(req, struct ubus_notify_request, req);
457         nreq->pending &= ~(1 << id);
458
459         if (!id) {
460                 /* first id: ubusd's status message with a list of ids */
461                 tb = ubus_parse_msg(buf->data);
462                 if (tb[UBUS_ATTR_SUBSCRIBERS]) {
463                         blob_for_each_attr(cur, tb[UBUS_ATTR_SUBSCRIBERS], rem) {
464                                 if (!blob_check_type(blob_data(cur), blob_len(cur), BLOB_ATTR_INT32))
465                                         continue;
466
467                                 nreq->pending |= (1 << idx);
468                                 nreq->id[idx] = blob_get_int32(cur);
469                                 idx++;
470
471                                 if (idx == UBUS_MAX_NOTIFY_PEERS + 1)
472                                         break;
473                         }
474                 }
475         } else {
476                 ubus_get_status(buf, &ret);
477                 if (nreq->status_cb)
478                         nreq->status_cb(nreq, id, ret);
479         }
480
481         if (!nreq->pending)
482                 ubus_set_req_status(req, 0);
483 }
484
485 void __hidden ubus_process_req_msg(struct ubus_context *ctx, struct ubus_msghdr_buf *buf, int fd)
486 {
487         struct ubus_msghdr *hdr = &buf->hdr;
488         struct ubus_request *req;
489         int id = -1;
490
491         switch(hdr->type) {
492         case UBUS_MSG_STATUS:
493                 req = ubus_find_request(ctx, hdr->seq, hdr->peer, &id);
494                 if (!req)
495                         break;
496
497                 if (fd >= 0) {
498                         if (req->fd_cb)
499                                 req->fd_cb(req, fd);
500                         else
501                                 close(fd);
502                 }
503
504                 if (id >= 0)
505                         ubus_process_notify_status(req, id, buf);
506                 else
507                         ubus_process_req_status(req, buf);
508                 break;
509
510         case UBUS_MSG_DATA:
511                 req = ubus_find_request(ctx, hdr->seq, hdr->peer, &id);
512                 if (req && (req->data_cb || req->raw_data_cb))
513                         ubus_process_req_data(req, buf);
514                 break;
515         }
516 }
517
518 int __ubus_monitor(struct ubus_context *ctx, const char *type)
519 {
520         blob_buf_init(&b, 0);
521         return ubus_invoke(ctx, UBUS_SYSTEM_OBJECT_MONITOR, type, b.head, NULL, NULL, 1000);
522 }