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