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