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