2 * Copyright (C) 2011-2014 Felix Fietkau <nbd@openwrt.org>
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
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.
16 #include "libubus-internal.h"
18 struct ubus_pending_data {
19 struct list_head list;
21 struct blob_attr data[];
24 static void req_data_cb(struct ubus_request *req, int type, struct blob_attr *data)
26 struct blob_attr **attr;
29 req->raw_data_cb(req, type, data);
34 attr = ubus_parse_msg(data);
35 if (!attr[UBUS_ATTR_DATA])
38 req->data_cb(req, type, attr[UBUS_ATTR_DATA]);
41 static void __ubus_process_req_data(struct ubus_request *req)
43 struct ubus_pending_data *data;
45 while (!list_empty(&req->pending)) {
46 data = list_first_entry(&req->pending,
47 struct ubus_pending_data, list);
48 list_del(&data->list);
50 req_data_cb(req, data->type, data->data);
55 int __hidden __ubus_start_request(struct ubus_context *ctx, struct ubus_request *req,
56 struct blob_attr *msg, int cmd, uint32_t peer)
59 if (msg && blob_pad_len(msg) > UBUS_MAX_MSGLEN)
62 INIT_LIST_HEAD(&req->list);
63 INIT_LIST_HEAD(&req->pending);
66 req->seq = ++ctx->request_seq;
68 return ubus_send_msg(ctx, req->seq, msg, cmd, peer, req->fd);
71 int __hidden ubus_start_request(struct ubus_context *ctx, struct ubus_request *req,
72 struct blob_attr *msg, int cmd, uint32_t peer)
74 memset(req, 0, sizeof(*req));
78 return __ubus_start_request(ctx, req, msg, cmd, peer);
82 void ubus_abort_request(struct ubus_context *ctx, struct ubus_request *req)
84 if (list_empty(&req->list))
87 req->cancelled = true;
88 __ubus_process_req_data(req);
89 list_del_init(&req->list);
92 void ubus_complete_request_async(struct ubus_context *ctx, struct ubus_request *req)
94 if (!list_empty(&req->list))
97 list_add(&req->list, &ctx->requests);
101 ubus_req_complete_cb(struct ubus_request *req)
103 ubus_complete_handler_t cb = req->complete_cb;
108 req->complete_cb = NULL;
109 cb(req, req->status_code);
113 ubus_set_req_status(struct ubus_request *req, int ret)
115 if (!list_empty(&req->list))
116 list_del_init(&req->list);
118 req->status_msg = true;
119 req->status_code = ret;
121 ubus_req_complete_cb(req);
124 static void ubus_sync_req_cb(struct ubus_request *req, int ret)
126 req->status_msg = true;
127 req->status_code = ret;
128 req->ctx->cancel_poll = true;
131 static int64_t get_time_msec(void)
136 clock_gettime(CLOCK_MONOTONIC, &ts);
137 val = (int64_t) ts.tv_sec * 1000LL;
138 val += ts.tv_nsec / 1000000LL;
142 int ubus_complete_request(struct ubus_context *ctx, struct ubus_request *req,
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;
150 time_end = get_time_msec() + req_timeout;
152 ubus_complete_request_async(ctx, req);
153 req->complete_cb = ubus_sync_req_cb;
156 while (!req->status_msg) {
158 timeout = time_end - get_time_msec();
160 ubus_set_req_status(req, UBUS_STATUS_TIMEOUT);
165 ubus_poll_data(ctx, (unsigned int) timeout);
168 ubus_set_req_status(req, UBUS_STATUS_CONNECTION_FAILED);
169 ctx->cancel_poll = true;
175 if (ctx->stack_depth)
176 ctx->cancel_poll = true;
179 status = req->status_code;
181 req->complete_cb = complete_cb;
182 if (req->complete_cb)
183 req->complete_cb(req, status);
185 if (!ctx->stack_depth && !ctx->sock.registered)
186 ctx->pending_timer.cb(&ctx->pending_timer);
191 void ubus_complete_deferred_request(struct ubus_context *ctx, struct ubus_request_data *req, int ret)
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);
199 static void ubus_put_data(struct blob_buf *buf, struct blob_attr *msg)
202 blob_put(buf, UBUS_ATTR_DATA, blob_data(msg), blob_len(msg));
204 blob_put(buf, UBUS_ATTR_DATA, NULL, 0);
207 int ubus_send_reply(struct ubus_context *ctx, struct ubus_request_data *req,
208 struct blob_attr *msg)
212 blob_buf_init(&b, 0);
213 blob_put_int32(&b, UBUS_ATTR_OBJID, req->object);
214 ubus_put_data(&b, msg);
215 ret = ubus_send_msg(ctx, req->seq, b.head, UBUS_MSG_DATA, req->peer, -1);
217 return UBUS_STATUS_NO_DATA;
222 int ubus_invoke_async_fd(struct ubus_context *ctx, uint32_t obj,
223 const char *method, struct blob_attr *msg,
224 struct ubus_request *req, int fd)
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 ubus_put_data(&b, msg);
231 memset(req, 0, sizeof(*req));
233 if (__ubus_start_request(ctx, req, b.head, UBUS_MSG_INVOKE, obj) < 0)
234 return UBUS_STATUS_INVALID_ARGUMENT;
238 int ubus_invoke_fd(struct ubus_context *ctx, uint32_t obj, const char *method,
239 struct blob_attr *msg, ubus_data_handler_t cb, void *priv,
242 struct ubus_request req;
245 rc = ubus_invoke_async_fd(ctx, obj, method, msg, &req, fd);
251 return ubus_complete_request(ctx, &req, timeout);
255 ubus_notify_complete_cb(struct ubus_request *req, int ret)
257 struct ubus_notify_request *nreq;
259 nreq = container_of(req, struct ubus_notify_request, req);
260 if (!nreq->complete_cb)
263 nreq->complete_cb(nreq, 0, 0);
267 ubus_notify_data_cb(struct ubus_request *req, int type, struct blob_attr *msg)
269 struct ubus_notify_request *nreq;
271 nreq = container_of(req, struct ubus_notify_request, req);
275 nreq->data_cb(nreq, type, msg);
279 __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, bool reply)
283 memset(req, 0, sizeof(*req));
285 blob_buf_init(&b, 0);
286 blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id);
287 blob_put_string(&b, UBUS_ATTR_METHOD, type);
288 ubus_put_data(&b, msg);
291 blob_put_int8(&b, UBUS_ATTR_NO_REPLY, true);
293 if (ubus_start_request(ctx, &req->req, b.head, UBUS_MSG_NOTIFY, obj->id) < 0)
294 return UBUS_STATUS_INVALID_ARGUMENT;
296 /* wait for status message from ubusd first */
297 req->req.notify = true;
299 req->id[0] = obj->id;
300 req->req.complete_cb = ubus_notify_complete_cb;
301 req->req.data_cb = ubus_notify_data_cb;
306 int ubus_notify_async(struct ubus_context *ctx, struct ubus_object *obj,
307 const char *type, struct blob_attr *msg,
308 struct ubus_notify_request *req)
310 return __ubus_notify_async(ctx, obj, type, msg, req, true);
313 int ubus_notify(struct ubus_context *ctx, struct ubus_object *obj,
314 const char *type, struct blob_attr *msg, int timeout)
316 struct ubus_notify_request req;
319 ret = __ubus_notify_async(ctx, obj, type, msg, &req, timeout >= 0);
324 ubus_abort_request(ctx, &req.req);
328 return ubus_complete_request(ctx, &req.req, timeout);
331 static bool ubus_get_status(struct ubus_msghdr_buf *buf, int *ret)
333 struct blob_attr **attrbuf = ubus_parse_msg(buf->data);
335 if (!attrbuf[UBUS_ATTR_STATUS])
338 *ret = blob_get_u32(attrbuf[UBUS_ATTR_STATUS]);
343 ubus_process_req_status(struct ubus_request *req, struct ubus_msghdr_buf *buf)
345 int ret = UBUS_STATUS_INVALID_ARGUMENT;
347 ubus_get_status(buf, &ret);
348 req->peer = buf->hdr.peer;
349 ubus_set_req_status(req, ret);
355 ubus_process_req_data(struct ubus_request *req, struct ubus_msghdr_buf *buf)
357 struct ubus_pending_data *data;
362 req_data_cb(req, buf->hdr.type, buf->data);
363 __ubus_process_req_data(req);
364 req->blocked = false;
367 ubus_req_complete_cb(req);
372 len = blob_raw_len(buf->data);
373 data = calloc(1, sizeof(*data) + len);
377 data->type = buf->hdr.type;
378 memcpy(data->data, buf->data, len);
379 list_add(&data->list, &req->pending);
383 ubus_find_notify_id(struct ubus_notify_request *n, uint32_t objid)
385 uint32_t pending = n->pending;
388 for (i = 0; pending; i++, pending >>= 1) {
392 if (n->id[i] == objid)
399 static struct ubus_request *
400 ubus_find_request(struct ubus_context *ctx, uint32_t seq, uint32_t peer, int *id)
402 struct ubus_request *req;
404 list_for_each_entry(req, &ctx->requests, list) {
405 struct ubus_notify_request *nreq;
406 nreq = container_of(req, struct ubus_notify_request, req);
415 *id = ubus_find_notify_id(nreq, peer);
418 } else if (peer != req->peer)
426 static void ubus_process_notify_status(struct ubus_request *req, int id, struct ubus_msghdr_buf *buf)
428 struct ubus_notify_request *nreq;
429 struct blob_attr **tb;
430 struct blob_attr *cur;
435 nreq = container_of(req, struct ubus_notify_request, req);
436 nreq->pending &= ~(1 << id);
439 /* first id: ubusd's status message with a list of ids */
440 tb = ubus_parse_msg(buf->data);
441 if (tb[UBUS_ATTR_SUBSCRIBERS]) {
442 blob_for_each_attr(cur, tb[UBUS_ATTR_SUBSCRIBERS], rem) {
443 if (!blob_check_type(blob_data(cur), blob_len(cur), BLOB_ATTR_INT32))
446 nreq->pending |= (1 << idx);
447 nreq->id[idx] = blob_get_int32(cur);
450 if (idx == UBUS_MAX_NOTIFY_PEERS + 1)
455 ubus_get_status(buf, &ret);
457 nreq->status_cb(nreq, id, ret);
461 ubus_set_req_status(req, 0);
464 void __hidden ubus_process_req_msg(struct ubus_context *ctx, struct ubus_msghdr_buf *buf, int fd)
466 struct ubus_msghdr *hdr = &buf->hdr;
467 struct ubus_request *req;
471 case UBUS_MSG_STATUS:
472 req = ubus_find_request(ctx, hdr->seq, hdr->peer, &id);
484 ubus_process_notify_status(req, id, buf);
486 ubus_process_req_status(req, buf);
490 req = ubus_find_request(ctx, hdr->seq, hdr->peer, &id);
491 if (req && (req->data_cb || req->raw_data_cb))
492 ubus_process_req_data(req, buf);
497 int __ubus_monitor(struct ubus_context *ctx, const char *type)
499 blob_buf_init(&b, 0);
500 return ubus_invoke(ctx, UBUS_SYSTEM_OBJECT_MONITOR, type, b.head, NULL, NULL, 1000);