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