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
, *tmp
;
45 list_for_each_entry_safe(data
, tmp
, &req
->pending
, list
) {
46 list_del(&data
->list
);
48 req_data_cb(req
, data
->type
, data
->data
);
53 int __hidden
__ubus_start_request(struct ubus_context
*ctx
, struct ubus_request
*req
,
54 struct blob_attr
*msg
, int cmd
, uint32_t peer
)
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
;
66 return ubus_send_msg(ctx
, req
->seq
, msg
, cmd
, peer
, req
->fd
);
69 int __hidden
ubus_start_request(struct ubus_context
*ctx
, struct ubus_request
*req
,
70 struct blob_attr
*msg
, int cmd
, uint32_t peer
)
72 memset(req
, 0, sizeof(*req
));
76 return __ubus_start_request(ctx
, req
, msg
, cmd
, peer
);
80 void ubus_abort_request(struct ubus_context
*ctx
, struct ubus_request
*req
)
82 if (list_empty(&req
->list
))
85 req
->cancelled
= true;
86 __ubus_process_req_data(req
);
87 list_del_init(&req
->list
);
90 void ubus_complete_request_async(struct ubus_context
*ctx
, struct ubus_request
*req
)
92 if (!list_empty(&req
->list
))
95 list_add(&req
->list
, &ctx
->requests
);
99 ubus_req_complete_cb(struct ubus_request
*req
)
101 ubus_complete_handler_t cb
= req
->complete_cb
;
106 req
->complete_cb
= NULL
;
107 cb(req
, req
->status_code
);
111 ubus_set_req_status(struct ubus_request
*req
, int ret
)
113 if (!list_empty(&req
->list
))
114 list_del_init(&req
->list
);
116 req
->status_msg
= true;
117 req
->status_code
= ret
;
119 ubus_req_complete_cb(req
);
122 static void ubus_sync_req_cb(struct ubus_request
*req
, int ret
)
124 req
->status_msg
= true;
125 req
->status_code
= ret
;
126 req
->ctx
->cancel_poll
= true;
129 static int64_t get_time_msec(void)
134 clock_gettime(CLOCK_MONOTONIC
, &ts
);
135 val
= (int64_t) ts
.tv_sec
* 1000LL;
136 val
+= ts
.tv_nsec
/ 1000000LL;
140 int ubus_complete_request(struct ubus_context
*ctx
, struct ubus_request
*req
,
143 ubus_complete_handler_t complete_cb
= req
->complete_cb
;
144 int status
= UBUS_STATUS_NO_DATA
;
145 int64_t timeout
= 0, time_end
= 0;
148 time_end
= get_time_msec() + req_timeout
;
150 ubus_complete_request_async(ctx
, req
);
151 req
->complete_cb
= ubus_sync_req_cb
;
154 while (!req
->status_msg
) {
156 timeout
= time_end
- get_time_msec();
158 ubus_set_req_status(req
, UBUS_STATUS_TIMEOUT
);
163 ubus_poll_data(ctx
, (unsigned int) timeout
);
166 ubus_set_req_status(req
, UBUS_STATUS_CONNECTION_FAILED
);
167 ctx
->cancel_poll
= true;
173 if (ctx
->stack_depth
)
174 ctx
->cancel_poll
= true;
177 status
= req
->status_code
;
179 req
->complete_cb
= complete_cb
;
180 if (req
->complete_cb
)
181 req
->complete_cb(req
, status
);
183 if (!ctx
->stack_depth
&& !ctx
->sock
.registered
)
184 ctx
->pending_timer
.cb(&ctx
->pending_timer
);
189 void ubus_complete_deferred_request(struct ubus_context
*ctx
, struct ubus_request_data
*req
, int ret
)
191 blob_buf_init(&b
, 0);
192 blob_put_int32(&b
, UBUS_ATTR_STATUS
, ret
);
193 blob_put_int32(&b
, UBUS_ATTR_OBJID
, req
->object
);
194 ubus_send_msg(ctx
, req
->seq
, b
.head
, UBUS_MSG_STATUS
, req
->peer
, req
->fd
);
197 static void ubus_put_data(struct blob_buf
*buf
, struct blob_attr
*msg
)
200 blob_put(buf
, UBUS_ATTR_DATA
, blob_data(msg
), blob_len(msg
));
202 blob_put(buf
, UBUS_ATTR_DATA
, NULL
, 0);
205 int ubus_send_reply(struct ubus_context
*ctx
, struct ubus_request_data
*req
,
206 struct blob_attr
*msg
)
210 blob_buf_init(&b
, 0);
211 blob_put_int32(&b
, UBUS_ATTR_OBJID
, req
->object
);
212 ubus_put_data(&b
, msg
);
213 ret
= ubus_send_msg(ctx
, req
->seq
, b
.head
, UBUS_MSG_DATA
, req
->peer
, -1);
215 return UBUS_STATUS_NO_DATA
;
220 int ubus_invoke_async_fd(struct ubus_context
*ctx
, uint32_t obj
,
221 const char *method
, struct blob_attr
*msg
,
222 struct ubus_request
*req
, int fd
)
224 blob_buf_init(&b
, 0);
225 blob_put_int32(&b
, UBUS_ATTR_OBJID
, obj
);
226 blob_put_string(&b
, UBUS_ATTR_METHOD
, method
);
227 ubus_put_data(&b
, msg
);
229 memset(req
, 0, sizeof(*req
));
231 if (__ubus_start_request(ctx
, req
, b
.head
, UBUS_MSG_INVOKE
, obj
) < 0)
232 return UBUS_STATUS_INVALID_ARGUMENT
;
236 int ubus_invoke_fd(struct ubus_context
*ctx
, uint32_t obj
, const char *method
,
237 struct blob_attr
*msg
, ubus_data_handler_t cb
, void *priv
,
240 struct ubus_request req
;
243 rc
= ubus_invoke_async_fd(ctx
, obj
, method
, msg
, &req
, fd
);
249 return ubus_complete_request(ctx
, &req
, timeout
);
253 ubus_notify_complete_cb(struct ubus_request
*req
, int ret
)
255 struct ubus_notify_request
*nreq
;
257 nreq
= container_of(req
, struct ubus_notify_request
, req
);
258 if (!nreq
->complete_cb
)
261 nreq
->complete_cb(nreq
, 0, 0);
265 ubus_notify_data_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
267 struct ubus_notify_request
*nreq
;
269 nreq
= container_of(req
, struct ubus_notify_request
, req
);
273 nreq
->data_cb(nreq
, type
, msg
);
277 __ubus_notify_async(struct ubus_context
*ctx
, struct ubus_object
*obj
,
278 const char *type
, struct blob_attr
*msg
,
279 struct ubus_notify_request
*req
, bool reply
)
281 memset(req
, 0, sizeof(*req
));
283 blob_buf_init(&b
, 0);
284 blob_put_int32(&b
, UBUS_ATTR_OBJID
, obj
->id
);
285 blob_put_string(&b
, UBUS_ATTR_METHOD
, type
);
286 ubus_put_data(&b
, msg
);
289 blob_put_int8(&b
, UBUS_ATTR_NO_REPLY
, true);
291 if (ubus_start_request(ctx
, &req
->req
, b
.head
, UBUS_MSG_NOTIFY
, obj
->id
) < 0)
292 return UBUS_STATUS_INVALID_ARGUMENT
;
294 /* wait for status message from ubusd first */
295 req
->req
.notify
= true;
297 req
->id
[0] = obj
->id
;
298 req
->req
.complete_cb
= ubus_notify_complete_cb
;
299 req
->req
.data_cb
= ubus_notify_data_cb
;
304 int ubus_notify_async(struct ubus_context
*ctx
, struct ubus_object
*obj
,
305 const char *type
, struct blob_attr
*msg
,
306 struct ubus_notify_request
*req
)
308 return __ubus_notify_async(ctx
, obj
, type
, msg
, req
, true);
311 int ubus_notify(struct ubus_context
*ctx
, struct ubus_object
*obj
,
312 const char *type
, struct blob_attr
*msg
, int timeout
)
314 struct ubus_notify_request req
;
317 ret
= __ubus_notify_async(ctx
, obj
, type
, msg
, &req
, timeout
>= 0);
322 ubus_abort_request(ctx
, &req
.req
);
326 return ubus_complete_request(ctx
, &req
.req
, timeout
);
329 static bool ubus_get_status(struct ubus_msghdr_buf
*buf
, int *ret
)
331 struct blob_attr
**attrbuf
= ubus_parse_msg(buf
->data
);
333 if (!attrbuf
[UBUS_ATTR_STATUS
])
336 *ret
= blob_get_u32(attrbuf
[UBUS_ATTR_STATUS
]);
341 ubus_process_req_status(struct ubus_request
*req
, struct ubus_msghdr_buf
*buf
)
343 int ret
= UBUS_STATUS_INVALID_ARGUMENT
;
345 ubus_get_status(buf
, &ret
);
346 req
->peer
= buf
->hdr
.peer
;
347 ubus_set_req_status(req
, ret
);
353 ubus_process_req_data(struct ubus_request
*req
, struct ubus_msghdr_buf
*buf
)
355 struct ubus_pending_data
*data
;
360 req_data_cb(req
, buf
->hdr
.type
, buf
->data
);
361 __ubus_process_req_data(req
);
362 req
->blocked
= false;
365 ubus_req_complete_cb(req
);
370 len
= blob_raw_len(buf
->data
);
371 data
= calloc(1, sizeof(*data
) + len
);
375 data
->type
= buf
->hdr
.type
;
376 memcpy(data
->data
, buf
->data
, len
);
377 list_add(&data
->list
, &req
->pending
);
381 ubus_find_notify_id(struct ubus_notify_request
*n
, uint32_t objid
)
383 uint32_t pending
= n
->pending
;
386 for (i
= 0; pending
; i
++, pending
>>= 1) {
390 if (n
->id
[i
] == objid
)
397 static struct ubus_request
*
398 ubus_find_request(struct ubus_context
*ctx
, uint32_t seq
, uint32_t peer
, int *id
)
400 struct ubus_request
*req
;
402 list_for_each_entry(req
, &ctx
->requests
, list
) {
403 struct ubus_notify_request
*nreq
;
404 nreq
= container_of(req
, struct ubus_notify_request
, req
);
413 *id
= ubus_find_notify_id(nreq
, peer
);
416 } else if (peer
!= req
->peer
)
424 static void ubus_process_notify_status(struct ubus_request
*req
, int id
, struct ubus_msghdr_buf
*buf
)
426 struct ubus_notify_request
*nreq
;
427 struct blob_attr
**tb
;
428 struct blob_attr
*cur
;
433 nreq
= container_of(req
, struct ubus_notify_request
, req
);
434 nreq
->pending
&= ~(1 << id
);
437 /* first id: ubusd's status message with a list of ids */
438 tb
= ubus_parse_msg(buf
->data
);
439 if (tb
[UBUS_ATTR_SUBSCRIBERS
]) {
440 blob_for_each_attr(cur
, tb
[UBUS_ATTR_SUBSCRIBERS
], rem
) {
441 if (!blob_check_type(blob_data(cur
), blob_len(cur
), BLOB_ATTR_INT32
))
444 nreq
->pending
|= (1 << idx
);
445 nreq
->id
[idx
] = blob_get_int32(cur
);
448 if (idx
== UBUS_MAX_NOTIFY_PEERS
+ 1)
453 ubus_get_status(buf
, &ret
);
455 nreq
->status_cb(nreq
, id
, ret
);
459 ubus_set_req_status(req
, 0);
462 void __hidden
ubus_process_req_msg(struct ubus_context
*ctx
, struct ubus_msghdr_buf
*buf
, int fd
)
464 struct ubus_msghdr
*hdr
= &buf
->hdr
;
465 struct ubus_request
*req
;
469 case UBUS_MSG_STATUS
:
470 req
= ubus_find_request(ctx
, hdr
->seq
, hdr
->peer
, &id
);
482 ubus_process_notify_status(req
, id
, buf
);
484 ubus_process_req_status(req
, buf
);
488 req
= ubus_find_request(ctx
, hdr
->seq
, hdr
->peer
, &id
);
489 if (req
&& (req
->data_cb
|| req
->raw_data_cb
))
490 ubus_process_req_data(req
, buf
);
495 int __ubus_monitor(struct ubus_context
*ctx
, const char *type
)
497 blob_buf_init(&b
, 0);
498 return ubus_invoke(ctx
, UBUS_SYSTEM_OBJECT_MONITOR
, type
, b
.head
, NULL
, NULL
, 1000);