projects
/
project
/
ubus.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
ubusd: fix build on non-linux systems without peercred support
[project/ubus.git]
/
libubus-req.c
diff --git
a/libubus-req.c
b/libubus-req.c
index
f89e7a7
..
2197f2a
100644
(file)
--- a/
libubus-req.c
+++ b/
libubus-req.c
@@
-1,5
+1,5
@@
/*
/*
- * Copyright (C) 2011-201
2
Felix Fietkau <nbd@openwrt.org>
+ * Copyright (C) 2011-201
4
Felix Fietkau <nbd@openwrt.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 2.1
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 2.1
@@
-11,6
+11,7
@@
* GNU General Public License for more details.
*/
* GNU General Public License for more details.
*/
+#include <unistd.h>
#include "libubus.h"
#include "libubus-internal.h"
#include "libubus.h"
#include "libubus-internal.h"
@@
-61,7
+62,7
@@
int __hidden ubus_start_request(struct ubus_context *ctx, struct ubus_request *r
req->ctx = ctx;
req->peer = peer;
req->seq = ++ctx->request_seq;
req->ctx = ctx;
req->peer = peer;
req->seq = ++ctx->request_seq;
- return ubus_send_msg(ctx, req->seq, msg, cmd, peer);
+ return ubus_send_msg(ctx, req->seq, msg, cmd, peer
, -1
);
}
void ubus_abort_request(struct ubus_context *ctx, struct ubus_request *req)
}
void ubus_abort_request(struct ubus_context *ctx, struct ubus_request *req)
@@
-113,38
+114,32
@@
static void ubus_sync_req_cb(struct ubus_request *req, int ret)
uloop_end();
}
uloop_end();
}
-struct ubus_sync_req_cb {
- struct uloop_timeout timeout;
- struct ubus_request *req;
-};
-
-static void ubus_sync_req_timeout_cb(struct uloop_timeout *timeout)
+static int64_t get_time_msec(void)
{
{
- struct ubus_sync_req_cb *cb;
+ struct timespec ts;
+ int64_t val;
- cb = container_of(timeout, struct ubus_sync_req_cb, timeout);
- ubus_set_req_status(cb->req, UBUS_STATUS_TIMEOUT);
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ val = (int64_t) ts.tv_sec * 1000LL;
+ val += ts.tv_nsec / 1000000LL;
+ return val;
}
int ubus_complete_request(struct ubus_context *ctx, struct ubus_request *req,
}
int ubus_complete_request(struct ubus_context *ctx, struct ubus_request *req,
- int timeout)
+ int
req_
timeout)
{
{
- struct ubus_sync_req_cb cb;
ubus_complete_handler_t complete_cb = req->complete_cb;
bool registered = ctx->sock.registered;
int status = UBUS_STATUS_NO_DATA;
ubus_complete_handler_t complete_cb = req->complete_cb;
bool registered = ctx->sock.registered;
int status = UBUS_STATUS_NO_DATA;
+ int64_t timeout = 0, time_end = 0;
if (!registered) {
uloop_init();
ubus_add_uloop(ctx);
}
if (!registered) {
uloop_init();
ubus_add_uloop(ctx);
}
- if (timeout) {
- memset(&cb, 0, sizeof(cb));
- cb.req = req;
- cb.timeout.cb = ubus_sync_req_timeout_cb;
- uloop_timeout_set(&cb.timeout, timeout);
- }
+ if (req_timeout)
+ time_end = get_time_msec() + req_timeout;
ubus_complete_request_async(ctx, req);
req->complete_cb = ubus_sync_req_cb;
ubus_complete_request_async(ctx, req);
req->complete_cb = ubus_sync_req_cb;
@@
-152,17
+147,24
@@
int ubus_complete_request(struct ubus_context *ctx, struct ubus_request *req,
ctx->stack_depth++;
while (!req->status_msg) {
bool cancelled = uloop_cancelled;
ctx->stack_depth++;
while (!req->status_msg) {
bool cancelled = uloop_cancelled;
+
uloop_cancelled = false;
uloop_cancelled = false;
- uloop_run();
+ if (req_timeout) {
+ timeout = time_end - get_time_msec();
+ if (timeout <= 0) {
+ ubus_set_req_status(req, UBUS_STATUS_TIMEOUT);
+ uloop_cancelled = cancelled;
+ break;
+ }
+ }
+ ubus_poll_data(ctx, (unsigned int) timeout);
+
uloop_cancelled = cancelled;
}
ctx->stack_depth--;
if (ctx->stack_depth)
uloop_cancelled = true;
uloop_cancelled = cancelled;
}
ctx->stack_depth--;
if (ctx->stack_depth)
uloop_cancelled = true;
- if (timeout)
- uloop_timeout_cancel(&cb.timeout);
-
if (req->status_msg)
status = req->status_code;
if (req->status_msg)
status = req->status_code;
@@
-170,11
+172,12
@@
int ubus_complete_request(struct ubus_context *ctx, struct ubus_request *req,
if (req->complete_cb)
req->complete_cb(req, status);
if (req->complete_cb)
req->complete_cb(req, status);
- if (!registered)
+ if (!registered)
{
uloop_fd_delete(&ctx->sock);
uloop_fd_delete(&ctx->sock);
- if (!ctx->stack_depth)
- ubus_process_pending_msg(ctx);
+ if (!ctx->stack_depth)
+ ctx->pending_timer.cb(&ctx->pending_timer);
+ }
return status;
}
return status;
}
@@
-184,7
+187,7
@@
void ubus_complete_deferred_request(struct ubus_context *ctx, struct ubus_reques
blob_buf_init(&b, 0);
blob_put_int32(&b, UBUS_ATTR_STATUS, ret);
blob_put_int32(&b, UBUS_ATTR_OBJID, req->object);
blob_buf_init(&b, 0);
blob_put_int32(&b, UBUS_ATTR_STATUS, ret);
blob_put_int32(&b, UBUS_ATTR_OBJID, req->object);
- ubus_send_msg(ctx, req->seq, b.head, UBUS_MSG_STATUS, req->peer);
+ ubus_send_msg(ctx, req->seq, b.head, UBUS_MSG_STATUS, req->peer
, req->fd
);
}
int ubus_send_reply(struct ubus_context *ctx, struct ubus_request_data *req,
}
int ubus_send_reply(struct ubus_context *ctx, struct ubus_request_data *req,
@@
-195,7
+198,7
@@
int ubus_send_reply(struct ubus_context *ctx, struct ubus_request_data *req,
blob_buf_init(&b, 0);
blob_put_int32(&b, UBUS_ATTR_OBJID, req->object);
blob_put(&b, UBUS_ATTR_DATA, blob_data(msg), blob_len(msg));
blob_buf_init(&b, 0);
blob_put_int32(&b, UBUS_ATTR_OBJID, req->object);
blob_put(&b, UBUS_ATTR_DATA, blob_data(msg), blob_len(msg));
- ret = ubus_send_msg(ctx, req->seq, b.head, UBUS_MSG_DATA, req->peer);
+ ret = ubus_send_msg(ctx, req->seq, b.head, UBUS_MSG_DATA, req->peer
, -1
);
if (ret < 0)
return UBUS_STATUS_NO_DATA;
if (ret < 0)
return UBUS_STATUS_NO_DATA;
@@
-222,8
+225,12
@@
int ubus_invoke(struct ubus_context *ctx, uint32_t obj, const char *method,
int timeout)
{
struct ubus_request req;
int timeout)
{
struct ubus_request req;
+ int rc;
+
+ rc = ubus_invoke_async(ctx, obj, method, msg, &req);
+ if (rc)
+ return rc;
- ubus_invoke_async(ctx, obj, method, msg, &req);
req.data_cb = cb;
req.priv = priv;
return ubus_complete_request(ctx, &req, timeout);
req.data_cb = cb;
req.priv = priv;
return ubus_complete_request(ctx, &req, timeout);
@@
-295,9
+302,9
@@
int ubus_notify(struct ubus_context *ctx, struct ubus_object *obj,
return ubus_complete_request(ctx, &req.req, timeout);
}
return ubus_complete_request(ctx, &req.req, timeout);
}
-static bool ubus_get_status(struct ubus_msghdr
*hdr
, int *ret)
+static bool ubus_get_status(struct ubus_msghdr
_buf *buf
, int *ret)
{
{
- struct blob_attr **attrbuf = ubus_parse_msg(
ubus_msghdr_data(hdr)
);
+ struct blob_attr **attrbuf = ubus_parse_msg(
buf->data
);
if (!attrbuf[UBUS_ATTR_STATUS])
return false;
if (!attrbuf[UBUS_ATTR_STATUS])
return false;
@@
-307,27
+314,26
@@
static bool ubus_get_status(struct ubus_msghdr *hdr, int *ret)
}
static int
}
static int
-ubus_process_req_status(struct ubus_request *req, struct ubus_msghdr
*hdr
)
+ubus_process_req_status(struct ubus_request *req, struct ubus_msghdr
_buf *buf
)
{
int ret = UBUS_STATUS_INVALID_ARGUMENT;
{
int ret = UBUS_STATUS_INVALID_ARGUMENT;
- ubus_get_status(
hdr
, &ret);
- req->peer =
hdr->
peer;
+ ubus_get_status(
buf
, &ret);
+ req->peer =
buf->hdr.
peer;
ubus_set_req_status(req, ret);
return ret;
}
static void
ubus_set_req_status(req, ret);
return ret;
}
static void
-ubus_process_req_data(struct ubus_request *req, struct ubus_msghdr
*hdr
)
+ubus_process_req_data(struct ubus_request *req, struct ubus_msghdr
_buf *buf
)
{
{
- struct blob_attr *msg_data = ubus_msghdr_data(hdr);
struct ubus_pending_data *data;
int len;
if (!req->blocked) {
req->blocked = true;
struct ubus_pending_data *data;
int len;
if (!req->blocked) {
req->blocked = true;
- req_data_cb(req,
hdr->type, msg_
data);
+ req_data_cb(req,
buf->hdr.type, buf->
data);
__ubus_process_req_data(req);
req->blocked = false;
__ubus_process_req_data(req);
req->blocked = false;
@@
-337,13
+343,13
@@
ubus_process_req_data(struct ubus_request *req, struct ubus_msghdr *hdr)
return;
}
return;
}
- len = blob_raw_len(
msg_
data);
+ len = blob_raw_len(
buf->
data);
data = calloc(1, sizeof(*data) + len);
if (!data)
return;
data = calloc(1, sizeof(*data) + len);
if (!data)
return;
- data->type =
hdr->
type;
- memcpy(data->data,
msg_
data, len);
+ data->type =
buf->hdr.
type;
+ memcpy(data->data,
buf->
data, len);
list_add(&data->list, &req->pending);
}
list_add(&data->list, &req->pending);
}
@@
-391,7
+397,7
@@
ubus_find_request(struct ubus_context *ctx, uint32_t seq, uint32_t peer, int *id
return NULL;
}
return NULL;
}
-static void ubus_process_notify_status(struct ubus_request *req, int id, struct ubus_msghdr
*hdr
)
+static void ubus_process_notify_status(struct ubus_request *req, int id, struct ubus_msghdr
_buf *buf
)
{
struct ubus_notify_request *nreq;
struct blob_attr **tb;
{
struct ubus_notify_request *nreq;
struct blob_attr **tb;
@@
-404,7
+410,7
@@
static void ubus_process_notify_status(struct ubus_request *req, int id, struct
if (!id) {
/* first id: ubusd's status message with a list of ids */
if (!id) {
/* first id: ubusd's status message with a list of ids */
- tb = ubus_parse_msg(
ubus_msghdr_data(hdr)
);
+ tb = ubus_parse_msg(
buf->data
);
if (tb[UBUS_ATTR_SUBSCRIBERS]) {
blob_for_each_attr(cur, tb[UBUS_ATTR_SUBSCRIBERS], rem) {
if (!blob_check_type(blob_data(cur), blob_len(cur), BLOB_ATTR_INT32))
if (tb[UBUS_ATTR_SUBSCRIBERS]) {
blob_for_each_attr(cur, tb[UBUS_ATTR_SUBSCRIBERS], rem) {
if (!blob_check_type(blob_data(cur), blob_len(cur), BLOB_ATTR_INT32))
@@
-419,7
+425,7
@@
static void ubus_process_notify_status(struct ubus_request *req, int id, struct
}
}
} else {
}
}
} else {
- ubus_get_status(
hdr
, &ret);
+ ubus_get_status(
buf
, &ret);
if (nreq->status_cb)
nreq->status_cb(nreq, id, ret);
}
if (nreq->status_cb)
nreq->status_cb(nreq, id, ret);
}
@@
-428,8
+434,9
@@
static void ubus_process_notify_status(struct ubus_request *req, int id, struct
ubus_set_req_status(req, 0);
}
ubus_set_req_status(req, 0);
}
-void __hidden ubus_process_req_msg(struct ubus_context *ctx, struct ubus_msghdr
*hdr
)
+void __hidden ubus_process_req_msg(struct ubus_context *ctx, struct ubus_msghdr
_buf *buf, int fd
)
{
{
+ struct ubus_msghdr *hdr = &buf->hdr;
struct ubus_request *req;
int id = -1;
struct ubus_request *req;
int id = -1;
@@
-439,16
+446,23
@@
void __hidden ubus_process_req_msg(struct ubus_context *ctx, struct ubus_msghdr
if (!req)
break;
if (!req)
break;
+ if (fd >= 0) {
+ if (req->fd_cb)
+ req->fd_cb(req, fd);
+ else
+ close(fd);
+ }
+
if (id >= 0)
if (id >= 0)
- ubus_process_notify_status(req, id,
hdr
);
+ ubus_process_notify_status(req, id,
buf
);
else
else
- ubus_process_req_status(req,
hdr
);
+ ubus_process_req_status(req,
buf
);
break;
case UBUS_MSG_DATA:
req = ubus_find_request(ctx, hdr->seq, hdr->peer, &id);
if (req && (req->data_cb || req->raw_data_cb))
break;
case UBUS_MSG_DATA:
req = ubus_find_request(ctx, hdr->seq, hdr->peer, &id);
if (req && (req->data_cb || req->raw_data_cb))
- ubus_process_req_data(req,
hdr
);
+ ubus_process_req_data(req,
buf
);
break;
}
}
break;
}
}