libubus: remove ubus_msghdr_data() by passing in the right data structure pointer

Signed-off-by: Felix Fietkau <nbd@openwrt.org>
This commit is contained in:
Felix Fietkau 2014-09-15 15:29:29 +02:00
parent 7c25c119a5
commit 3e45a782b2
5 changed files with 35 additions and 44 deletions

View file

@ -17,22 +17,15 @@
extern struct blob_buf b; extern struct blob_buf b;
extern const struct ubus_method watch_method; extern const struct ubus_method watch_method;
static inline struct blob_attr *
ubus_msghdr_data(struct ubus_msghdr *hdr)
{
struct ubus_msghdr_buf *hdrbuf = container_of(hdr, struct ubus_msghdr_buf, hdr);
return hdrbuf->data;
}
struct blob_attr **ubus_parse_msg(struct blob_attr *msg); struct blob_attr **ubus_parse_msg(struct blob_attr *msg);
void ubus_handle_data(struct uloop_fd *u, unsigned int events); void ubus_handle_data(struct uloop_fd *u, unsigned int events);
int ubus_send_msg(struct ubus_context *ctx, uint32_t seq, int ubus_send_msg(struct ubus_context *ctx, uint32_t seq,
struct blob_attr *msg, int cmd, uint32_t peer, int fd); struct blob_attr *msg, int cmd, uint32_t peer, int fd);
void ubus_process_msg(struct ubus_context *ctx, struct ubus_msghdr *hdr, int fd); void ubus_process_msg(struct ubus_context *ctx, struct ubus_msghdr_buf *buf, int fd);
int __hidden ubus_start_request(struct ubus_context *ctx, struct ubus_request *req, int __hidden ubus_start_request(struct ubus_context *ctx, struct ubus_request *req,
struct blob_attr *msg, int cmd, uint32_t peer); struct blob_attr *msg, int cmd, uint32_t peer);
void ubus_process_obj_msg(struct ubus_context*ctx, struct ubus_msghdr *hdr); void ubus_process_obj_msg(struct ubus_context *ctx, struct ubus_msghdr_buf *buf);
void ubus_process_req_msg(struct ubus_context *ctx, struct ubus_msghdr *hdr, int fd); void ubus_process_req_msg(struct ubus_context *ctx, struct ubus_msghdr_buf *buf, int fd);
void __hidden ubus_poll_data(struct ubus_context *ctx, int timeout); void __hidden ubus_poll_data(struct ubus_context *ctx, int timeout);

View file

@ -283,11 +283,10 @@ static bool get_next_msg(struct ubus_context *ctx, int *recv_fd)
void __hidden ubus_handle_data(struct uloop_fd *u, unsigned int events) void __hidden ubus_handle_data(struct uloop_fd *u, unsigned int events)
{ {
struct ubus_context *ctx = container_of(u, struct ubus_context, sock); struct ubus_context *ctx = container_of(u, struct ubus_context, sock);
struct ubus_msghdr *hdr = &ctx->msgbuf.hdr;
int recv_fd = -1; int recv_fd = -1;
while (get_next_msg(ctx, &recv_fd)) { while (get_next_msg(ctx, &recv_fd)) {
ubus_process_msg(ctx, hdr, recv_fd); ubus_process_msg(ctx, &ctx->msgbuf, recv_fd);
if (uloop_cancelled) if (uloop_cancelled)
break; break;
} }

View file

@ -91,15 +91,16 @@ send:
ubus_complete_deferred_request(ctx, &req, ret); ubus_complete_deferred_request(ctx, &req, ret);
} }
void __hidden ubus_process_obj_msg(struct ubus_context *ctx, struct ubus_msghdr *hdr) void __hidden ubus_process_obj_msg(struct ubus_context *ctx, struct ubus_msghdr_buf *buf)
{ {
void (*cb)(struct ubus_context *, struct ubus_msghdr *, void (*cb)(struct ubus_context *, struct ubus_msghdr *,
struct ubus_object *, struct blob_attr **); struct ubus_object *, struct blob_attr **);
struct ubus_msghdr *hdr = &buf->hdr;
struct blob_attr **attrbuf; struct blob_attr **attrbuf;
struct ubus_object *obj; struct ubus_object *obj;
uint32_t objid; uint32_t objid;
attrbuf = ubus_parse_msg(ubus_msghdr_data(hdr)); attrbuf = ubus_parse_msg(buf->data);
if (!attrbuf[UBUS_ATTR_OBJID]) if (!attrbuf[UBUS_ATTR_OBJID])
return; return;

View file

@ -301,9 +301,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]) if (!attrbuf[UBUS_ATTR_STATUS])
return false; return false;
@ -313,27 +313,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); ubus_get_status(buf, &ret);
req->peer = hdr->peer; req->peer = buf->hdr.peer;
ubus_set_req_status(req, ret); ubus_set_req_status(req, ret);
return ret; return ret;
} }
static void 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; struct ubus_pending_data *data;
int len; int len;
if (!req->blocked) { if (!req->blocked) {
req->blocked = true; 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); __ubus_process_req_data(req);
req->blocked = false; req->blocked = false;
@ -343,13 +342,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); data = calloc(1, sizeof(*data) + len);
if (!data) if (!data)
return; return;
data->type = hdr->type; data->type = buf->hdr.type;
memcpy(data->data, msg_data, len); memcpy(data->data, buf->data, len);
list_add(&data->list, &req->pending); list_add(&data->list, &req->pending);
} }
@ -397,7 +396,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 ubus_notify_request *nreq;
struct blob_attr **tb; struct blob_attr **tb;
@ -410,7 +409,7 @@ static void ubus_process_notify_status(struct ubus_request *req, int id, struct
if (!id) { if (!id) {
/* first id: ubusd's status message with a list of ids */ /* 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]) { if (tb[UBUS_ATTR_SUBSCRIBERS]) {
blob_for_each_attr(cur, tb[UBUS_ATTR_SUBSCRIBERS], rem) { blob_for_each_attr(cur, tb[UBUS_ATTR_SUBSCRIBERS], rem) {
if (!blob_check_type(blob_data(cur), blob_len(cur), BLOB_ATTR_INT32)) if (!blob_check_type(blob_data(cur), blob_len(cur), BLOB_ATTR_INT32))
@ -425,7 +424,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) if (nreq->status_cb)
nreq->status_cb(nreq, id, ret); nreq->status_cb(nreq, id, ret);
} }
@ -434,8 +433,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, int fd) 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; struct ubus_request *req;
int id = -1; int id = -1;
@ -453,15 +453,15 @@ void __hidden ubus_process_req_msg(struct ubus_context *ctx, struct ubus_msghdr
} }
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; break;
case UBUS_MSG_DATA: case UBUS_MSG_DATA:
req = ubus_find_request(ctx, hdr->seq, hdr->peer, &id); req = ubus_find_request(ctx, hdr->seq, hdr->peer, &id);
if (req && (req->data_cb || req->raw_data_cb)) if (req && (req->data_cb || req->raw_data_cb))
ubus_process_req_data(req, hdr); ubus_process_req_data(req, buf);
break; break;
} }
} }

View file

@ -71,41 +71,39 @@ out:
} }
static void static void
ubus_queue_msg(struct ubus_context *ctx, struct ubus_msghdr *hdr) ubus_queue_msg(struct ubus_context *ctx, struct ubus_msghdr_buf *buf)
{ {
struct ubus_pending_msg *pending; struct ubus_pending_msg *pending;
void *data; void *data;
pending = calloc_a(sizeof(*pending), pending = calloc_a(sizeof(*pending), &data, blob_raw_len(buf->data));
&data, blob_raw_len(ubus_msghdr_data(hdr)));
pending->hdr.data = data; pending->hdr.data = data;
memcpy(&pending->hdr.hdr, hdr, sizeof(*hdr)); memcpy(&pending->hdr.hdr, &buf->hdr, sizeof(buf->hdr));
memcpy(data, ubus_msghdr_data(hdr), blob_raw_len(ubus_msghdr_data(hdr))); memcpy(data, buf->data, blob_raw_len(buf->data));
list_add(&pending->list, &ctx->pending); list_add(&pending->list, &ctx->pending);
if (ctx->sock.registered) if (ctx->sock.registered)
uloop_timeout_set(&ctx->pending_timer, 1); uloop_timeout_set(&ctx->pending_timer, 1);
} }
void __hidden void __hidden
ubus_process_msg(struct ubus_context *ctx, struct ubus_msghdr *hdr, int fd) ubus_process_msg(struct ubus_context *ctx, struct ubus_msghdr_buf *buf, int fd)
{ {
switch(buf->hdr.type) {
switch(hdr->type) {
case UBUS_MSG_STATUS: case UBUS_MSG_STATUS:
case UBUS_MSG_DATA: case UBUS_MSG_DATA:
ubus_process_req_msg(ctx, hdr, fd); ubus_process_req_msg(ctx, buf, fd);
break; break;
case UBUS_MSG_INVOKE: case UBUS_MSG_INVOKE:
case UBUS_MSG_UNSUBSCRIBE: case UBUS_MSG_UNSUBSCRIBE:
case UBUS_MSG_NOTIFY: case UBUS_MSG_NOTIFY:
if (ctx->stack_depth) { if (ctx->stack_depth) {
ubus_queue_msg(ctx, hdr); ubus_queue_msg(ctx, buf);
break; break;
} }
ubus_process_obj_msg(ctx, hdr); ubus_process_obj_msg(ctx, buf);
break; break;
} }
} }
@ -118,7 +116,7 @@ static void ubus_process_pending_msg(struct uloop_timeout *timeout)
while (!ctx->stack_depth && !list_empty(&ctx->pending)) { while (!ctx->stack_depth && !list_empty(&ctx->pending)) {
pending = list_first_entry(&ctx->pending, struct ubus_pending_msg, list); pending = list_first_entry(&ctx->pending, struct ubus_pending_msg, list);
list_del(&pending->list); list_del(&pending->list);
ubus_process_msg(ctx, &pending->hdr.hdr, -1); ubus_process_msg(ctx, &pending->hdr, -1);
free(pending); free(pending);
} }
} }