#include <sys/types.h>
#include <sys/uio.h>
#include <sys/socket.h>
#include <unistd.h>
#include <fcntl.h>
#include <poll.h>
#include <libubox/usock.h>
#include <libubox/blob.h>
#include <libubox/blobmsg.h>
#include "libubus.h"
#include "libubus-internal.h"
Go to the source code of this file.
|
__hidden struct blob_attr ** | ubus_parse_msg (struct blob_attr *msg, size_t len) |
|
static void | wait_data (int fd, bool write) |
|
static int | writev_retry (int fd, struct iovec *iov, int iov_len, int sock_fd) |
|
int __hidden | ubus_send_msg (struct ubus_context *ctx, uint32_t seq, struct blob_attr *msg, int cmd, uint32_t peer, int fd) |
|
static int | recv_retry (struct ubus_context *ctx, struct iovec *iov, bool wait, int *recv_fd) |
|
bool | ubus_validate_hdr (struct ubus_msghdr *hdr) |
|
static bool | alloc_msg_buf (struct ubus_context *ctx, int len) |
|
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_poll_data (struct ubus_context *ctx, int timeout) |
|
static void | ubus_auto_sub_lookup (struct ubus_context *ctx, struct ubus_object_data *obj, void *priv) |
|
static void | ubus_refresh_auto_subscribe (struct ubus_context *ctx) |
|
static void | ubus_refresh_state (struct ubus_context *ctx) |
|
int | ubus_reconnect (struct ubus_context *ctx, const char *path) |
|
◆ _GNU_SOURCE
◆ STATIC_IOV
#define STATIC_IOV |
( |
|
_var | ) |
{ .iov_base = (char *) &(_var), .iov_len = sizeof(_var) } |
◆ UBUS_MSGBUF_REDUCTION_INTERVAL
#define UBUS_MSGBUF_REDUCTION_INTERVAL 16 |
◆ alloc_msg_buf()
static bool alloc_msg_buf |
( |
struct ubus_context * |
ctx, |
|
|
int |
len |
|
) |
| |
|
static |
Definition at line 240 of file libubus-io.c.
static struct ubus_context * ctx
#define UBUS_MSGBUF_REDUCTION_INTERVAL
int msgbuf_reduction_counter
struct ubus_msghdr_buf msgbuf
#define UBUS_MSG_CHUNK_SIZE
◆ get_next_msg()
static bool get_next_msg |
( |
struct ubus_context * |
ctx, |
|
|
int * |
recv_fd |
|
) |
| |
|
static |
Definition at line 271 of file libubus-io.c.
275 struct blob_attr data;
290 hdrbuf.hdr.seq = be16_to_cpu(hdrbuf.hdr.seq);
291 hdrbuf.hdr.peer = be32_to_cpu(hdrbuf.hdr.peer);
296 len = blob_raw_len(&hdrbuf.data);
300 memcpy(&
ctx->
msgbuf.
hdr, &hdrbuf.hdr,
sizeof(hdrbuf.hdr));
303 iov.iov_base = (
char *)
ctx->
msgbuf.
data +
sizeof(hdrbuf.data);
305 if (iov.iov_len > 0 &&
static int recv_retry(struct ubus_context *ctx, struct iovec *iov, bool wait, int *recv_fd)
static bool alloc_msg_buf(struct ubus_context *ctx, int len)
bool ubus_validate_hdr(struct ubus_msghdr *hdr)
◆ recv_retry()
static int recv_retry |
( |
struct ubus_context * |
ctx, |
|
|
struct iovec * |
iov, |
|
|
bool |
wait, |
|
|
int * |
recv_fd |
|
) |
| |
|
static |
Definition at line 158 of file libubus-io.c.
160 uint8_t fd_buf[CMSG_SPACE(
sizeof(
int))] = { 0 };
161 struct msghdr msghdr = { 0 };
162 struct cmsghdr *cmsg;
170 msghdr.msg_iov = iov,
171 msghdr.msg_iovlen = 1,
172 msghdr.msg_control = fd_buf;
173 msghdr.msg_controllen =
sizeof(fd_buf);
175 cmsg = CMSG_FIRSTHDR(&msghdr);
176 cmsg->cmsg_type = SCM_RIGHTS;
177 cmsg->cmsg_level = SOL_SOCKET;
178 cmsg->cmsg_len = CMSG_LEN(
sizeof(
int));
180 pfd = (
int *) CMSG_DATA(cmsg);
182 while (iov->iov_len > 0) {
184 msghdr.msg_control = fd_buf;
185 msghdr.msg_controllen = cmsg->cmsg_len;
187 msghdr.msg_control = NULL;
188 msghdr.msg_controllen = 0;
192 bytes = recvmsg(fd, &msghdr, 0);
213 iov->iov_len -= bytes;
214 iov->iov_base += bytes;
217 if (iov->iov_len > 0)
static void wait_data(int fd, bool write)
◆ ubus_auto_sub_lookup()
Definition at line 348 of file libubus-io.c.
int ubus_subscribe(struct ubus_context *ctx, struct ubus_subscriber *obj, uint32_t id)
struct list_head auto_subscribers
ubus_new_object_handler_t new_obj_cb
◆ ubus_handle_data()
void __hidden ubus_handle_data |
( |
struct uloop_fd * |
u, |
|
|
unsigned int |
events |
|
) |
| |
Definition at line 312 of file libubus-io.c.
void ubus_process_msg(struct ubus_context *ctx, struct ubus_msghdr_buf *buf, int fd)
static bool get_next_msg(struct ubus_context *ctx, int *recv_fd)
struct uloop_timeout pending_timer
void(* connection_lost)(struct ubus_context *ctx)
◆ ubus_parse_msg()
__hidden struct blob_attr** ubus_parse_msg |
( |
struct blob_attr * |
msg, |
|
|
size_t |
len |
|
) |
| |
Definition at line 46 of file libubus-io.c.
static const struct blob_attr_info ubus_policy[UBUS_ATTR_MAX]
static struct blob_attr * attrbuf[UBUS_ATTR_MAX]
◆ ubus_poll_data()
Definition at line 335 of file libubus-io.c.
337 struct pollfd pfd = {
339 .events = POLLIN | POLLERR,
void __hidden ubus_handle_data(struct uloop_fd *u, unsigned int events)
◆ ubus_reconnect()
int ubus_reconnect |
( |
struct ubus_context * |
ctx, |
|
|
const char * |
path |
|
) |
| |
Definition at line 395 of file libubus-io.c.
399 struct blob_attr data;
401 struct blob_attr *buf;
405 path = UBUS_UNIX_SOCKET;
420 if (read(
ctx->
sock.fd, &hdr,
sizeof(hdr)) !=
sizeof(hdr))
429 buf = calloc(1, blob_raw_len(&hdr.data));
433 memcpy(buf, &hdr.data,
sizeof(hdr.data));
434 if (read(
ctx->
sock.fd, blob_data(buf), blob_len(buf)) != (ssize_t) blob_len(buf))
442 fcntl(
ctx->
sock.fd, F_SETFL, fcntl(
ctx->
sock.fd, F_GETFL) | O_NONBLOCK | O_CLOEXEC);
static void ubus_refresh_state(struct ubus_context *ctx)
@ UBUS_STATUS_CONNECTION_FAILED
@ UBUS_STATUS_UNKNOWN_ERROR
◆ ubus_refresh_auto_subscribe()
static void ubus_refresh_auto_subscribe |
( |
struct ubus_context * |
ctx | ) |
|
|
static |
Definition at line 359 of file libubus-io.c.
static void ubus_auto_sub_lookup(struct ubus_context *ctx, struct ubus_object_data *obj, void *priv)
int ubus_lookup(struct ubus_context *ctx, const char *path, ubus_lookup_handler_t cb, void *priv)
int ubus_register_event_handler(struct ubus_context *ctx, struct ubus_event_handler *ev, const char *pattern)
struct ubus_event_handler auto_subscribe_event_handler
◆ ubus_refresh_state()
Definition at line 371 of file libubus-io.c.
383 objs = alloca(
ctx->
objects.count *
sizeof(*objs));
389 for (n = i, i = 0; i < n; i++)
static void ubus_refresh_auto_subscribe(struct ubus_context *ctx)
int ubus_add_object(struct ubus_context *ctx, struct ubus_object *obj)
struct ubus_object_type * type
◆ ubus_send_msg()
int __hidden ubus_send_msg |
( |
struct ubus_context * |
ctx, |
|
|
uint32_t |
seq, |
|
|
struct blob_attr * |
msg, |
|
|
int |
cmd, |
|
|
uint32_t |
peer, |
|
|
int |
fd |
|
) |
| |
Definition at line 126 of file libubus-io.c.
130 struct iovec iov[2] = {
137 hdr.seq = cpu_to_be16(
seq);
138 hdr.peer = cpu_to_be32(
peer);
141 blob_buf_init(&
b, 0);
145 iov[1].iov_base = (
char *) msg;
146 iov[1].iov_len = blob_raw_len(msg);
static int writev_retry(int fd, struct iovec *iov, int iov_len, int sock_fd)
◆ ubus_validate_hdr()
Definition at line 224 of file libubus-io.c.
226 struct blob_attr *data = (
struct blob_attr *) (hdr + 1);
231 if (blob_raw_len(data) <
sizeof(*data))
234 if (blob_pad_len(data) > UBUS_MAX_MSGLEN)
◆ wait_data()
static void wait_data |
( |
int |
fd, |
|
|
bool |
write |
|
) |
| |
|
static |
Definition at line 52 of file libubus-io.c.
54 struct pollfd pfd = { .fd = fd };
56 pfd.events = write ? POLLOUT : POLLIN;
◆ writev_retry()
static int writev_retry |
( |
int |
fd, |
|
|
struct iovec * |
iov, |
|
|
int |
iov_len, |
|
|
int |
sock_fd |
|
) |
| |
|
static |
Definition at line 60 of file libubus-io.c.
62 uint8_t fd_buf[CMSG_SPACE(
sizeof(
int))] = { 0 };
63 struct msghdr msghdr = { 0 };
69 msghdr.msg_iovlen = iov_len,
70 msghdr.msg_control = fd_buf;
71 msghdr.msg_controllen =
sizeof(fd_buf);
73 cmsg = CMSG_FIRSTHDR(&msghdr);
74 cmsg->cmsg_type = SCM_RIGHTS;
75 cmsg->cmsg_level = SOL_SOCKET;
76 cmsg->cmsg_len = CMSG_LEN(
sizeof(
int));
78 pfd = (
int *) CMSG_DATA(cmsg);
79 msghdr.msg_controllen = cmsg->cmsg_len;
85 msghdr.msg_control = NULL;
86 msghdr.msg_controllen = 0;
91 cur_len = sendmsg(fd, &msghdr, 0);
109 while (cur_len >= (ssize_t) iov->iov_len) {
110 cur_len -= iov->iov_len;
116 iov->iov_base += cur_len;
117 iov->iov_len -= cur_len;
118 msghdr.msg_iov = iov;
119 msghdr.msg_iovlen = iov_len;
◆ attrbuf
◆ ubus_policy