ubus/ubusd.c

168 lines
3.5 KiB
C
Raw Normal View History

2011-06-17 16:35:11 +02:00
/*
* Copyright (C) 2011-2014 Felix Fietkau <nbd@openwrt.org>
2011-06-17 16:35:11 +02:00
*
* 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
* as published by the Free Software Foundation
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
2010-12-06 03:51:58 +01:00
#include <sys/socket.h>
#ifdef FreeBSD
#include <sys/param.h>
#endif
2010-12-06 03:51:58 +01:00
#include "ubusd.h"
#define USES_EXTERNAL_BUFFER ~0U
2011-02-07 02:41:56 +01:00
static struct ubus_msg_buf *ubus_msg_ref(struct ubus_msg_buf *ub)
2010-12-06 03:51:58 +01:00
{
struct ubus_msg_buf *new_ub;
if (ub->refcount == USES_EXTERNAL_BUFFER) {
new_ub = ubus_msg_new(ub->data, ub->len, false);
if (!new_ub)
return NULL;
memcpy(&new_ub->hdr, &ub->hdr, sizeof(struct ubus_msghdr));
new_ub->fd = ub->fd;
return new_ub;
}
2010-12-06 03:51:58 +01:00
ub->refcount++;
return ub;
}
struct ubus_msg_buf *ubus_msg_new(void *data, int len, bool shared)
{
struct ubus_msg_buf *ub;
int buflen = sizeof(*ub);
if (!shared)
buflen += len;
ub = calloc(1, buflen);
if (!ub)
return NULL;
ub->fd = -1;
2010-12-06 03:51:58 +01:00
if (shared) {
ub->refcount = USES_EXTERNAL_BUFFER;
2010-12-06 03:51:58 +01:00
ub->data = data;
} else {
ub->refcount = 1;
ub->data = (void *) (ub + 1);
if (data)
memcpy(ub + 1, data, len);
}
ub->len = len;
return ub;
}
void ubus_msg_free(struct ubus_msg_buf *ub)
{
switch (ub->refcount) {
case 1:
case USES_EXTERNAL_BUFFER:
if (ub->fd >= 0)
close(ub->fd);
2010-12-06 03:51:58 +01:00
free(ub);
break;
default:
ub->refcount--;
break;
}
}
ssize_t ubus_msg_writev(int fd, struct ubus_msg_buf *ub, size_t offset)
2010-12-06 03:51:58 +01:00
{
static struct iovec iov[2];
static struct {
int fd;
struct cmsghdr h;
} fd_buf = {
.h = {
.cmsg_len = sizeof(fd_buf),
.cmsg_level = SOL_SOCKET,
.cmsg_type = SCM_RIGHTS,
},
};
struct msghdr msghdr = {
.msg_iov = iov,
.msg_iovlen = ARRAY_SIZE(iov),
.msg_control = &fd_buf,
.msg_controllen = sizeof(fd_buf),
};
struct ubus_msghdr hdr;
iron out all extra compiler warnings clang-9 on x86/64 has reported following warnings/errors: libubus-acl.c:123:2: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare] libubus-io.c:108:18: error: comparison of integers of different signs: 'int' and 'size_t' (aka 'unsigned long') [-Werror,-Wsign-compare] libubus-io.c:395:56: error: comparison of integers of different signs: 'ssize_t' (aka 'long') and 'size_t' (aka 'unsigned long') [-Werror,-Wsign-compare] libubus-req.c:441:4: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare] ubusd_acl.c:119:18: error: comparison of integers of different signs: 'int' and 'unsigned long' [-Werror,-Wsign-compare] ubusd_acl.c:152:5: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare] ubusd_acl.c:348:3: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare] ubusd_acl.c:352:3: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare] ubusd_acl.c:357:3: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare] ubusd_acl.c:362:3: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare] ubusd_acl.c:367:3: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare] ubusd_acl.c:447:16: error: comparison of integers of different signs: 'int' and '__size_t' (aka 'unsigned long') [-Werror,-Wsign-compare] ubusd_acl.c:502:18: error: comparison of integers of different signs: 'int' and 'unsigned long' [-Werror,-Wsign-compare] ubusd.c:123:13: error: comparison of integers of different signs: 'int' and 'unsigned long' [-Werror,-Wsign-compare] ubusd.c:170:15: error: comparison of integers of different signs: 'int' and 'unsigned long' [-Werror,-Wsign-compare] ubusd.c:262:43: error: comparison of integers of different signs: 'int' and 'unsigned long' [-Werror,-Wsign-compare] ubusd.c:287:30: error: comparison of integers of different signs: 'int' and 'unsigned long' [-Werror,-Wsign-compare] ubusd_event.c:170:18: error: comparison of integers of different signs: 'int' and 'unsigned long' [-Werror,-Wsign-compare] ubusd_obj.c:71:2: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare] Signed-off-by: Petr Štetiar <ynezz@true.cz>
2019-12-11 10:36:36 +01:00
ssize_t ret;
fd_buf.fd = ub->fd;
if (ub->fd < 0 || offset) {
msghdr.msg_control = NULL;
msghdr.msg_controllen = 0;
}
2010-12-06 03:51:58 +01:00
if (offset < sizeof(ub->hdr)) {
hdr.version = ub->hdr.version;
hdr.type = ub->hdr.type;
hdr.seq = cpu_to_be16(ub->hdr.seq);
hdr.peer = cpu_to_be32(ub->hdr.peer);
iov[0].iov_base = ((char *) &hdr) + offset;
iov[0].iov_len = sizeof(hdr) - offset;
2010-12-06 03:51:58 +01:00
iov[1].iov_base = (char *) ub->data;
iov[1].iov_len = ub->len;
} else {
offset -= sizeof(ub->hdr);
iov[0].iov_base = ((char *) ub->data) + offset;
iov[0].iov_len = ub->len - offset;
msghdr.msg_iovlen = 1;
2010-12-06 03:51:58 +01:00
}
do {
ret = sendmsg(fd, &msghdr, 0);
} while (ret < 0 && errno == EINTR);
return ret;
2010-12-06 03:51:58 +01:00
}
2011-02-05 20:50:08 +01:00
static void ubus_msg_enqueue(struct ubus_client *cl, struct ubus_msg_buf *ub)
{
if (cl->tx_queue[cl->txq_tail])
return;
2011-02-06 01:45:21 +01:00
cl->tx_queue[cl->txq_tail] = ubus_msg_ref(ub);
2011-02-05 20:50:08 +01:00
cl->txq_tail = (cl->txq_tail + 1) % ARRAY_SIZE(cl->tx_queue);
}
2010-12-06 03:51:58 +01:00
/* takes the msgbuf reference */
void ubus_msg_send(struct ubus_client *cl, struct ubus_msg_buf *ub)
2010-12-06 03:51:58 +01:00
{
iron out all extra compiler warnings clang-9 on x86/64 has reported following warnings/errors: libubus-acl.c:123:2: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare] libubus-io.c:108:18: error: comparison of integers of different signs: 'int' and 'size_t' (aka 'unsigned long') [-Werror,-Wsign-compare] libubus-io.c:395:56: error: comparison of integers of different signs: 'ssize_t' (aka 'long') and 'size_t' (aka 'unsigned long') [-Werror,-Wsign-compare] libubus-req.c:441:4: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare] ubusd_acl.c:119:18: error: comparison of integers of different signs: 'int' and 'unsigned long' [-Werror,-Wsign-compare] ubusd_acl.c:152:5: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare] ubusd_acl.c:348:3: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare] ubusd_acl.c:352:3: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare] ubusd_acl.c:357:3: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare] ubusd_acl.c:362:3: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare] ubusd_acl.c:367:3: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare] ubusd_acl.c:447:16: error: comparison of integers of different signs: 'int' and '__size_t' (aka 'unsigned long') [-Werror,-Wsign-compare] ubusd_acl.c:502:18: error: comparison of integers of different signs: 'int' and 'unsigned long' [-Werror,-Wsign-compare] ubusd.c:123:13: error: comparison of integers of different signs: 'int' and 'unsigned long' [-Werror,-Wsign-compare] ubusd.c:170:15: error: comparison of integers of different signs: 'int' and 'unsigned long' [-Werror,-Wsign-compare] ubusd.c:262:43: error: comparison of integers of different signs: 'int' and 'unsigned long' [-Werror,-Wsign-compare] ubusd.c:287:30: error: comparison of integers of different signs: 'int' and 'unsigned long' [-Werror,-Wsign-compare] ubusd_event.c:170:18: error: comparison of integers of different signs: 'int' and 'unsigned long' [-Werror,-Wsign-compare] ubusd_obj.c:71:2: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare] Signed-off-by: Petr Štetiar <ynezz@true.cz>
2019-12-11 10:36:36 +01:00
ssize_t written;
2010-12-06 03:51:58 +01:00
if (ub->hdr.type != UBUS_MSG_MONITOR)
ubusd_monitor_message(cl, ub, true);
2011-02-05 20:50:08 +01:00
if (!cl->tx_queue[cl->txq_cur]) {
written = ubus_msg_writev(cl->sock.fd, ub, 0);
if (written < 0)
written = 0;
2010-12-06 03:51:58 +01:00
iron out all extra compiler warnings clang-9 on x86/64 has reported following warnings/errors: libubus-acl.c:123:2: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare] libubus-io.c:108:18: error: comparison of integers of different signs: 'int' and 'size_t' (aka 'unsigned long') [-Werror,-Wsign-compare] libubus-io.c:395:56: error: comparison of integers of different signs: 'ssize_t' (aka 'long') and 'size_t' (aka 'unsigned long') [-Werror,-Wsign-compare] libubus-req.c:441:4: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare] ubusd_acl.c:119:18: error: comparison of integers of different signs: 'int' and 'unsigned long' [-Werror,-Wsign-compare] ubusd_acl.c:152:5: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare] ubusd_acl.c:348:3: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare] ubusd_acl.c:352:3: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare] ubusd_acl.c:357:3: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare] ubusd_acl.c:362:3: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare] ubusd_acl.c:367:3: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare] ubusd_acl.c:447:16: error: comparison of integers of different signs: 'int' and '__size_t' (aka 'unsigned long') [-Werror,-Wsign-compare] ubusd_acl.c:502:18: error: comparison of integers of different signs: 'int' and 'unsigned long' [-Werror,-Wsign-compare] ubusd.c:123:13: error: comparison of integers of different signs: 'int' and 'unsigned long' [-Werror,-Wsign-compare] ubusd.c:170:15: error: comparison of integers of different signs: 'int' and 'unsigned long' [-Werror,-Wsign-compare] ubusd.c:262:43: error: comparison of integers of different signs: 'int' and 'unsigned long' [-Werror,-Wsign-compare] ubusd.c:287:30: error: comparison of integers of different signs: 'int' and 'unsigned long' [-Werror,-Wsign-compare] ubusd_event.c:170:18: error: comparison of integers of different signs: 'int' and 'unsigned long' [-Werror,-Wsign-compare] ubusd_obj.c:71:2: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'int' [-Werror,-Wsign-compare] Signed-off-by: Petr Štetiar <ynezz@true.cz>
2019-12-11 10:36:36 +01:00
if (written >= (ssize_t) (ub->len + sizeof(ub->hdr)))
return;
2011-02-05 20:50:08 +01:00
cl->txq_ofs = written;
2010-12-06 03:51:58 +01:00
/* get an event once we can write to the socket again */
uloop_fd_add(&cl->sock, ULOOP_READ | ULOOP_WRITE | ULOOP_EDGE_TRIGGER);
}
2011-02-05 20:50:08 +01:00
ubus_msg_enqueue(cl, ub);
}