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 req->data_cb(req, type, attr[UBUS_ATTR_DATA]);
38 static void __ubus_process_req_data(struct ubus_request *req)
40 struct ubus_pending_data *data;
42 while (!list_empty(&req->pending)) {
43 data = list_first_entry(&req->pending,
44 struct ubus_pending_data, list);
45 list_del(&data->list);
47 req_data_cb(req, data->type, data->data);
52 int __hidden ubus_start_request(struct ubus_context *ctx, struct ubus_request *req,
53 struct blob_attr *msg, int cmd, uint32_t peer)
55 memset(req, 0, sizeof(*req));
57 if (msg && blob_pad_len(msg) > UBUS_MAX_MSGLEN)
60 INIT_LIST_HEAD(&req->list);
61 INIT_LIST_HEAD(&req->pending);
64 req->seq = ++ctx->request_seq;
65 return ubus_send_msg(ctx, req->seq, msg, cmd, peer, -1);
68 void ubus_abort_request(struct ubus_context *ctx, struct ubus_request *req)
70 if (list_empty(&req->list))
73 req->cancelled = true;
74 __ubus_process_req_data(req);
75 list_del_init(&req->list);
78 void ubus_complete_request_async(struct ubus_context *ctx, struct ubus_request *req)
80 if (!list_empty(&req->list))
83 list_add(&req->list, &ctx->requests);
87 ubus_req_complete_cb(struct ubus_request *req)
89 ubus_complete_handler_t cb = req->complete_cb;
94 req->complete_cb = NULL;
95 cb(req, req->status_code);
99 ubus_set_req_status(struct ubus_request *req, int ret)
101 if (!list_empty(&req->list))
102 list_del_init(&req->list);
104 req->status_msg = true;
105 req->status_code = ret;
107 ubus_req_complete_cb(req);
110 static void ubus_sync_req_cb(struct ubus_request *req, int ret)
112 req->status_msg = true;
113 req->status_code = ret;
117 static int64_t get_time_msec(void)
122 clock_gettime(CLOCK_MONOTONIC, &ts);
123 val = (int64_t) ts.tv_sec * 1000LL;
124 val += ts.tv_nsec / 1000000LL;
128 int ubus_complete_request(struct ubus_context *ctx, struct ubus_request *req,
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;
142 time_end = get_time_msec() + req_timeout;
144 ubus_complete_request_async(ctx, req);
145 req->complete_cb = ubus_sync_req_cb;
148 while (!req->status_msg) {
149 bool cancelled = uloop_cancelled;
151 uloop_cancelled = false;
153 timeout = time_end - get_time_msec();
155 ubus_set_req_status(req, UBUS_STATUS_TIMEOUT);
159 ubus_poll_data(ctx, (unsigned int) timeout);
161 uloop_cancelled = cancelled;
164 if (ctx->stack_depth)
165 uloop_cancelled = true;
168 status = req->status_code;
170 req->complete_cb = complete_cb;
171 if (req->complete_cb)
172 req->complete_cb(req, status);
175 uloop_fd_delete(&ctx->sock);
177 if (ctx->stack_depth)
178 ctx->pending_timer.cb(&ctx->pending_timer);
184 void ubus_complete_deferred_request(struct ubus_context *ctx, struct ubus_request_data *req, int ret)
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);
192 int ubus_send_reply(struct ubus_context *ctx, struct ubus_request_data *req,
193 struct blob_attr *msg)
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);
202 return UBUS_STATUS_NO_DATA;
207 int ubus_invoke_async(struct ubus_context *ctx, uint32_t obj, const char *method,
208 struct blob_attr *msg, struct ubus_request *req)
210 blob_buf_init(&b, 0);
211 blob_put_int32(&b, UBUS_ATTR_OBJID, obj);
212 blob_put_string(&b, UBUS_ATTR_METHOD, method);
214 blob_put(&b, UBUS_ATTR_DATA, blob_data(msg), blob_len(msg));
216 if (ubus_start_request(ctx, req, b.head, UBUS_MSG_INVOKE, obj) < 0)
217 return UBUS_STATUS_INVALID_ARGUMENT;
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,
226 struct ubus_request req;
228 ubus_invoke_async(ctx, obj, method, msg, &req);
231 return ubus_complete_request(ctx, &req, timeout);
235 ubus_notify_complete_cb(struct ubus_request *req, int ret)
237 struct ubus_notify_request *nreq;
239 nreq = container_of(req, struct ubus_notify_request, req);
240 if (!nreq->complete_cb)
243 nreq->complete_cb(nreq, 0, 0);
247 __ubus_notify_async(struct ubus_context *ctx, struct ubus_object *obj,
248 const char *type, struct blob_attr *msg,
249 struct ubus_notify_request *req, bool reply)
251 memset(req, 0, sizeof(*req));
253 blob_buf_init(&b, 0);
254 blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id);
255 blob_put_string(&b, UBUS_ATTR_METHOD, type);
258 blob_put_int8(&b, UBUS_ATTR_NO_REPLY, true);
261 blob_put(&b, UBUS_ATTR_DATA, blob_data(msg), blob_len(msg));
263 if (ubus_start_request(ctx, &req->req, b.head, UBUS_MSG_NOTIFY, obj->id) < 0)
264 return UBUS_STATUS_INVALID_ARGUMENT;
266 /* wait for status message from ubusd first */
267 req->req.notify = true;
269 req->id[0] = obj->id;
270 req->req.complete_cb = ubus_notify_complete_cb;
275 int ubus_notify_async(struct ubus_context *ctx, struct ubus_object *obj,
276 const char *type, struct blob_attr *msg,
277 struct ubus_notify_request *req)
279 return __ubus_notify_async(ctx, obj, type, msg, req, true);
282 int ubus_notify(struct ubus_context *ctx, struct ubus_object *obj,
283 const char *type, struct blob_attr *msg, int timeout)
285 struct ubus_notify_request req;
288 ret = __ubus_notify_async(ctx, obj, type, msg, &req, timeout >= 0);
293 ubus_abort_request(ctx, &req.req);
297 return ubus_complete_request(ctx, &req.req, timeout);
300 static bool ubus_get_status(struct ubus_msghdr *hdr, int *ret)
302 struct blob_attr **attrbuf = ubus_parse_msg(ubus_msghdr_data(hdr));
304 if (!attrbuf[UBUS_ATTR_STATUS])
307 *ret = blob_get_u32(attrbuf[UBUS_ATTR_STATUS]);
312 ubus_process_req_status(struct ubus_request *req, struct ubus_msghdr *hdr)
314 int ret = UBUS_STATUS_INVALID_ARGUMENT;
316 ubus_get_status(hdr, &ret);
317 req->peer = hdr->peer;
318 ubus_set_req_status(req, ret);
324 ubus_process_req_data(struct ubus_request *req, struct ubus_msghdr *hdr)
326 struct blob_attr *msg_data = ubus_msghdr_data(hdr);
327 struct ubus_pending_data *data;
332 req_data_cb(req, hdr->type, msg_data);
333 __ubus_process_req_data(req);
334 req->blocked = false;
337 ubus_req_complete_cb(req);
342 len = blob_raw_len(msg_data);
343 data = calloc(1, sizeof(*data) + len);
347 data->type = hdr->type;
348 memcpy(data->data, msg_data, len);
349 list_add(&data->list, &req->pending);
353 ubus_find_notify_id(struct ubus_notify_request *n, uint32_t objid)
355 uint32_t pending = n->pending;
358 for (i = 0; pending; i++, pending >>= 1) {
362 if (n->id[i] == objid)
369 static struct ubus_request *
370 ubus_find_request(struct ubus_context *ctx, uint32_t seq, uint32_t peer, int *id)
372 struct ubus_request *req;
374 list_for_each_entry(req, &ctx->requests, list) {
375 struct ubus_notify_request *nreq;
376 nreq = container_of(req, struct ubus_notify_request, req);
385 *id = ubus_find_notify_id(nreq, peer);
388 } else if (peer != req->peer)
396 static void ubus_process_notify_status(struct ubus_request *req, int id, struct ubus_msghdr *hdr)
398 struct ubus_notify_request *nreq;
399 struct blob_attr **tb;
400 struct blob_attr *cur;
404 nreq = container_of(req, struct ubus_notify_request, req);
405 nreq->pending &= ~(1 << id);
408 /* first id: ubusd's status message with a list of ids */
409 tb = ubus_parse_msg(ubus_msghdr_data(hdr));
410 if (tb[UBUS_ATTR_SUBSCRIBERS]) {
411 blob_for_each_attr(cur, tb[UBUS_ATTR_SUBSCRIBERS], rem) {
412 if (!blob_check_type(blob_data(cur), blob_len(cur), BLOB_ATTR_INT32))
415 nreq->pending |= (1 << idx);
416 nreq->id[idx] = blob_get_int32(cur);
419 if (idx == UBUS_MAX_NOTIFY_PEERS + 1)
424 ubus_get_status(hdr, &ret);
426 nreq->status_cb(nreq, id, ret);
430 ubus_set_req_status(req, 0);
433 void __hidden ubus_process_req_msg(struct ubus_context *ctx, struct ubus_msghdr *hdr, int fd)
435 struct ubus_request *req;
439 case UBUS_MSG_STATUS:
440 req = ubus_find_request(ctx, hdr->seq, hdr->peer, &id);
452 ubus_process_notify_status(req, id, hdr);
454 ubus_process_req_status(req, hdr);
458 req = ubus_find_request(ctx, hdr->seq, hdr->peer, &id);
459 if (req && (req->data_cb || req->raw_data_cb))
460 ubus_process_req_data(req, hdr);