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
)
56 if (msg
&& blob_pad_len(msg
) > UBUS_MAX_MSGLEN
)
59 INIT_LIST_HEAD(&req
->list
);
60 INIT_LIST_HEAD(&req
->pending
);
63 req
->seq
= ++ctx
->request_seq
;
65 return ubus_send_msg(ctx
, req
->seq
, msg
, cmd
, peer
, req
->fd
);
68 int __hidden
ubus_start_request(struct ubus_context
*ctx
, struct ubus_request
*req
,
69 struct blob_attr
*msg
, int cmd
, uint32_t peer
)
71 memset(req
, 0, sizeof(*req
));
75 return __ubus_start_request(ctx
, req
, msg
, cmd
, peer
);
79 void ubus_abort_request(struct ubus_context
*ctx
, struct ubus_request
*req
)
81 if (list_empty(&req
->list
))
84 req
->cancelled
= true;
85 __ubus_process_req_data(req
);
86 list_del_init(&req
->list
);
89 void ubus_complete_request_async(struct ubus_context
*ctx
, struct ubus_request
*req
)
91 if (!list_empty(&req
->list
))
94 list_add(&req
->list
, &ctx
->requests
);
98 ubus_req_complete_cb(struct ubus_request
*req
)
100 ubus_complete_handler_t cb
= req
->complete_cb
;
105 req
->complete_cb
= NULL
;
106 cb(req
, req
->status_code
);
110 ubus_set_req_status(struct ubus_request
*req
, int ret
)
112 if (!list_empty(&req
->list
))
113 list_del_init(&req
->list
);
115 req
->status_msg
= true;
116 req
->status_code
= ret
;
118 ubus_req_complete_cb(req
);
121 static void ubus_sync_req_cb(struct ubus_request
*req
, int ret
)
123 req
->status_msg
= true;
124 req
->status_code
= ret
;
125 req
->ctx
->cancel_poll
= true;
128 static int64_t get_time_msec(void)
133 clock_gettime(CLOCK_MONOTONIC
, &ts
);
134 val
= (int64_t) ts
.tv_sec
* 1000LL;
135 val
+= ts
.tv_nsec
/ 1000000LL;
139 int ubus_complete_request(struct ubus_context
*ctx
, struct ubus_request
*req
,
142 ubus_complete_handler_t complete_cb
= req
->complete_cb
;
143 int status
= UBUS_STATUS_NO_DATA
;
144 int64_t timeout
= 0, time_end
= 0;
147 time_end
= get_time_msec() + req_timeout
;
149 ubus_complete_request_async(ctx
, req
);
150 req
->complete_cb
= ubus_sync_req_cb
;
153 while (!req
->status_msg
) {
155 timeout
= time_end
- get_time_msec();
157 ubus_set_req_status(req
, UBUS_STATUS_TIMEOUT
);
162 ubus_poll_data(ctx
, (unsigned int) timeout
);
165 ubus_set_req_status(req
, UBUS_STATUS_CONNECTION_FAILED
);
166 ctx
->cancel_poll
= true;
172 if (ctx
->stack_depth
)
173 ctx
->cancel_poll
= true;
176 status
= req
->status_code
;
178 req
->complete_cb
= complete_cb
;
179 if (req
->complete_cb
)
180 req
->complete_cb(req
, status
);
182 if (!ctx
->stack_depth
&& !ctx
->sock
.registered
)
183 ctx
->pending_timer
.cb(&ctx
->pending_timer
);
188 void ubus_complete_deferred_request(struct ubus_context
*ctx
, struct ubus_request_data
*req
, int ret
)
190 blob_buf_init(&b
, 0);
191 blob_put_int32(&b
, UBUS_ATTR_STATUS
, ret
);
192 blob_put_int32(&b
, UBUS_ATTR_OBJID
, req
->object
);
193 ubus_send_msg(ctx
, req
->seq
, b
.head
, UBUS_MSG_STATUS
, req
->peer
, req
->fd
);
196 int ubus_send_reply(struct ubus_context
*ctx
, struct ubus_request_data
*req
,
197 struct blob_attr
*msg
)
201 blob_buf_init(&b
, 0);
202 blob_put_int32(&b
, UBUS_ATTR_OBJID
, req
->object
);
203 blob_put(&b
, UBUS_ATTR_DATA
, blob_data(msg
), blob_len(msg
));
204 ret
= ubus_send_msg(ctx
, req
->seq
, b
.head
, UBUS_MSG_DATA
, req
->peer
, -1);
206 return UBUS_STATUS_NO_DATA
;
211 int ubus_invoke_async_fd(struct ubus_context
*ctx
, uint32_t obj
,
212 const char *method
, struct blob_attr
*msg
,
213 struct ubus_request
*req
, int fd
)
215 blob_buf_init(&b
, 0);
216 blob_put_int32(&b
, UBUS_ATTR_OBJID
, obj
);
217 blob_put_string(&b
, UBUS_ATTR_METHOD
, method
);
219 blob_put(&b
, UBUS_ATTR_DATA
, blob_data(msg
), blob_len(msg
));
221 memset(req
, 0, sizeof(*req
));
223 if (__ubus_start_request(ctx
, req
, b
.head
, UBUS_MSG_INVOKE
, obj
) < 0)
224 return UBUS_STATUS_INVALID_ARGUMENT
;
228 int ubus_invoke_fd(struct ubus_context
*ctx
, uint32_t obj
, const char *method
,
229 struct blob_attr
*msg
, ubus_data_handler_t cb
, void *priv
,
232 struct ubus_request req
;
235 rc
= ubus_invoke_async_fd(ctx
, obj
, method
, msg
, &req
, fd
);
241 return ubus_complete_request(ctx
, &req
, timeout
);
245 ubus_notify_complete_cb(struct ubus_request
*req
, int ret
)
247 struct ubus_notify_request
*nreq
;
249 nreq
= container_of(req
, struct ubus_notify_request
, req
);
250 if (!nreq
->complete_cb
)
253 nreq
->complete_cb(nreq
, 0, 0);
257 __ubus_notify_async(struct ubus_context
*ctx
, struct ubus_object
*obj
,
258 const char *type
, struct blob_attr
*msg
,
259 struct ubus_notify_request
*req
, bool reply
)
261 memset(req
, 0, sizeof(*req
));
263 blob_buf_init(&b
, 0);
264 blob_put_int32(&b
, UBUS_ATTR_OBJID
, obj
->id
);
265 blob_put_string(&b
, UBUS_ATTR_METHOD
, type
);
268 blob_put_int8(&b
, UBUS_ATTR_NO_REPLY
, true);
271 blob_put(&b
, UBUS_ATTR_DATA
, blob_data(msg
), blob_len(msg
));
273 if (ubus_start_request(ctx
, &req
->req
, b
.head
, UBUS_MSG_NOTIFY
, obj
->id
) < 0)
274 return UBUS_STATUS_INVALID_ARGUMENT
;
276 /* wait for status message from ubusd first */
277 req
->req
.notify
= true;
279 req
->id
[0] = obj
->id
;
280 req
->req
.complete_cb
= ubus_notify_complete_cb
;
285 int ubus_notify_async(struct ubus_context
*ctx
, struct ubus_object
*obj
,
286 const char *type
, struct blob_attr
*msg
,
287 struct ubus_notify_request
*req
)
289 return __ubus_notify_async(ctx
, obj
, type
, msg
, req
, true);
292 int ubus_notify(struct ubus_context
*ctx
, struct ubus_object
*obj
,
293 const char *type
, struct blob_attr
*msg
, int timeout
)
295 struct ubus_notify_request req
;
298 ret
= __ubus_notify_async(ctx
, obj
, type
, msg
, &req
, timeout
>= 0);
303 ubus_abort_request(ctx
, &req
.req
);
307 return ubus_complete_request(ctx
, &req
.req
, timeout
);
310 static bool ubus_get_status(struct ubus_msghdr_buf
*buf
, int *ret
)
312 struct blob_attr
**attrbuf
= ubus_parse_msg(buf
->data
);
314 if (!attrbuf
[UBUS_ATTR_STATUS
])
317 *ret
= blob_get_u32(attrbuf
[UBUS_ATTR_STATUS
]);
322 ubus_process_req_status(struct ubus_request
*req
, struct ubus_msghdr_buf
*buf
)
324 int ret
= UBUS_STATUS_INVALID_ARGUMENT
;
326 ubus_get_status(buf
, &ret
);
327 req
->peer
= buf
->hdr
.peer
;
328 ubus_set_req_status(req
, ret
);
334 ubus_process_req_data(struct ubus_request
*req
, struct ubus_msghdr_buf
*buf
)
336 struct ubus_pending_data
*data
;
341 req_data_cb(req
, buf
->hdr
.type
, buf
->data
);
342 __ubus_process_req_data(req
);
343 req
->blocked
= false;
346 ubus_req_complete_cb(req
);
351 len
= blob_raw_len(buf
->data
);
352 data
= calloc(1, sizeof(*data
) + len
);
356 data
->type
= buf
->hdr
.type
;
357 memcpy(data
->data
, buf
->data
, len
);
358 list_add(&data
->list
, &req
->pending
);
362 ubus_find_notify_id(struct ubus_notify_request
*n
, uint32_t objid
)
364 uint32_t pending
= n
->pending
;
367 for (i
= 0; pending
; i
++, pending
>>= 1) {
371 if (n
->id
[i
] == objid
)
378 static struct ubus_request
*
379 ubus_find_request(struct ubus_context
*ctx
, uint32_t seq
, uint32_t peer
, int *id
)
381 struct ubus_request
*req
;
383 list_for_each_entry(req
, &ctx
->requests
, list
) {
384 struct ubus_notify_request
*nreq
;
385 nreq
= container_of(req
, struct ubus_notify_request
, req
);
394 *id
= ubus_find_notify_id(nreq
, peer
);
397 } else if (peer
!= req
->peer
)
405 static void ubus_process_notify_status(struct ubus_request
*req
, int id
, struct ubus_msghdr_buf
*buf
)
407 struct ubus_notify_request
*nreq
;
408 struct blob_attr
**tb
;
409 struct blob_attr
*cur
;
413 nreq
= container_of(req
, struct ubus_notify_request
, req
);
414 nreq
->pending
&= ~(1 << id
);
417 /* first id: ubusd's status message with a list of ids */
418 tb
= ubus_parse_msg(buf
->data
);
419 if (tb
[UBUS_ATTR_SUBSCRIBERS
]) {
420 blob_for_each_attr(cur
, tb
[UBUS_ATTR_SUBSCRIBERS
], rem
) {
421 if (!blob_check_type(blob_data(cur
), blob_len(cur
), BLOB_ATTR_INT32
))
424 nreq
->pending
|= (1 << idx
);
425 nreq
->id
[idx
] = blob_get_int32(cur
);
428 if (idx
== UBUS_MAX_NOTIFY_PEERS
+ 1)
433 ubus_get_status(buf
, &ret
);
435 nreq
->status_cb(nreq
, id
, ret
);
439 ubus_set_req_status(req
, 0);
442 void __hidden
ubus_process_req_msg(struct ubus_context
*ctx
, struct ubus_msghdr_buf
*buf
, int fd
)
444 struct ubus_msghdr
*hdr
= &buf
->hdr
;
445 struct ubus_request
*req
;
449 case UBUS_MSG_STATUS
:
450 req
= ubus_find_request(ctx
, hdr
->seq
, hdr
->peer
, &id
);
462 ubus_process_notify_status(req
, id
, buf
);
464 ubus_process_req_status(req
, buf
);
468 req
= ubus_find_request(ctx
, hdr
->seq
, hdr
->peer
, &id
);
469 if (req
&& (req
->data_cb
|| req
->raw_data_cb
))
470 ubus_process_req_data(req
, buf
);
475 int __ubus_monitor(struct ubus_context
*ctx
, const char *type
)
477 blob_buf_init(&b
, 0);
478 return ubus_invoke(ctx
, UBUS_SYSTEM_OBJECT_MONITOR
, type
, b
.head
, NULL
, NULL
, 1000);