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.
14 #include <sys/types.h>
16 #include <sys/socket.h>
22 #include <libubox/usock.h>
23 #include <libubox/blob.h>
24 #include <libubox/blobmsg.h>
27 #include "libubus-internal.h"
29 #define STATIC_IOV(_var) { .iov_base = (char *) &(_var), .iov_len = sizeof(_var) }
31 #define UBUS_MSGBUF_REDUCTION_INTERVAL 16
33 static const struct blob_attr_info ubus_policy[UBUS_ATTR_MAX] = {
34 [UBUS_ATTR_STATUS] = { .type = BLOB_ATTR_INT32 },
35 [UBUS_ATTR_OBJID] = { .type = BLOB_ATTR_INT32 },
36 [UBUS_ATTR_OBJPATH] = { .type = BLOB_ATTR_STRING },
37 [UBUS_ATTR_METHOD] = { .type = BLOB_ATTR_STRING },
38 [UBUS_ATTR_ACTIVE] = { .type = BLOB_ATTR_INT8 },
39 [UBUS_ATTR_NO_REPLY] = { .type = BLOB_ATTR_INT8 },
40 [UBUS_ATTR_SUBSCRIBERS] = { .type = BLOB_ATTR_NESTED },
43 static struct blob_attr *attrbuf[UBUS_ATTR_MAX];
45 __hidden struct blob_attr **ubus_parse_msg(struct blob_attr *msg)
47 blob_parse(msg, attrbuf, ubus_policy, UBUS_ATTR_MAX);
51 static void wait_data(int fd, bool write)
53 struct pollfd pfd = { .fd = fd };
55 pfd.events = write ? POLLOUT : POLLIN;
59 static int writev_retry(int fd, struct iovec *iov, int iov_len, int sock_fd)
66 .cmsg_len = sizeof(fd_buf),
67 .cmsg_level = SOL_SOCKET,
68 .cmsg_type = SCM_RIGHTS,
71 struct msghdr msghdr = {
73 .msg_iovlen = iov_len,
74 .msg_control = &fd_buf,
75 .msg_controllen = sizeof(fd_buf),
83 msghdr.msg_control = NULL;
84 msghdr.msg_controllen = 0;
89 cur_len = sendmsg(fd, &msghdr, 0);
107 while (cur_len >= iov->iov_len) {
108 cur_len -= iov->iov_len;
114 iov->iov_len -= cur_len;
117 /* Should never reach here */
121 int __hidden ubus_send_msg(struct ubus_context *ctx, uint32_t seq,
122 struct blob_attr *msg, int cmd, uint32_t peer, int fd)
124 struct ubus_msghdr hdr;
125 struct iovec iov[2] = {
136 blob_buf_init(&b, 0);
140 iov[1].iov_base = (char *) msg;
141 iov[1].iov_len = blob_raw_len(msg);
143 ret = writev_retry(ctx->sock.fd, iov, ARRAY_SIZE(iov), fd);
145 ctx->sock.eof = true;
153 static int recv_retry(int fd, struct iovec *iov, bool wait, int *recv_fd)
155 int bytes, total = 0;
161 .cmsg_type = SCM_RIGHTS,
162 .cmsg_level = SOL_SOCKET,
163 .cmsg_len = sizeof(fd_buf),
166 struct msghdr msghdr = {
171 while (iov->iov_len > 0) {
173 wait_data(fd, false);
176 msghdr.msg_control = &fd_buf;
177 msghdr.msg_controllen = sizeof(fd_buf);
179 msghdr.msg_control = NULL;
180 msghdr.msg_controllen = 0;
184 bytes = recvmsg(fd, &msghdr, 0);
202 *recv_fd = fd_buf.fd;
207 iov->iov_len -= bytes;
208 iov->iov_base += bytes;
215 static bool ubus_validate_hdr(struct ubus_msghdr *hdr)
217 struct blob_attr *data = (struct blob_attr *) (hdr + 1);
219 if (hdr->version != 0)
222 if (blob_raw_len(data) < sizeof(*data))
225 if (blob_pad_len(data) > UBUS_MAX_MSGLEN)
231 static bool get_next_msg(struct ubus_context *ctx, int *recv_fd)
234 struct ubus_msghdr hdr;
235 struct blob_attr data;
237 struct iovec iov = STATIC_IOV(hdrbuf);
241 /* receive header + start attribute */
242 r = recv_retry(ctx->sock.fd, &iov, false, recv_fd);
245 ctx->sock.eof = true;
250 if (!ubus_validate_hdr(&hdrbuf.hdr))
253 len = blob_raw_len(&hdrbuf.data);
254 if (len > ctx->msgbuf_data_len) {
255 ctx->msgbuf_reduction_counter = UBUS_MSGBUF_REDUCTION_INTERVAL;
256 } else if (ctx->msgbuf_data_len > UBUS_MSG_CHUNK_SIZE) {
257 if (ctx->msgbuf_reduction_counter > 0) {
259 --ctx->msgbuf_reduction_counter;
261 len = UBUS_MSG_CHUNK_SIZE;
266 ctx->msgbuf.data = realloc(ctx->msgbuf.data, len * sizeof(char));
267 if (ctx->msgbuf.data)
268 ctx->msgbuf_data_len = len;
270 if (!ctx->msgbuf.data)
273 memcpy(&ctx->msgbuf.hdr, &hdrbuf.hdr, sizeof(hdrbuf.hdr));
274 memcpy(ctx->msgbuf.data, &hdrbuf.data, sizeof(hdrbuf.data));
276 iov.iov_base = (char *)ctx->msgbuf.data + sizeof(hdrbuf.data);
277 iov.iov_len = blob_len(ctx->msgbuf.data);
278 if (iov.iov_len > 0 && !recv_retry(ctx->sock.fd, &iov, true, NULL))
284 void __hidden ubus_handle_data(struct uloop_fd *u, unsigned int events)
286 struct ubus_context *ctx = container_of(u, struct ubus_context, sock);
287 struct ubus_msghdr *hdr = &ctx->msgbuf.hdr;
290 while (get_next_msg(ctx, &recv_fd)) {
291 ubus_process_msg(ctx, hdr, recv_fd);
297 ctx->connection_lost(ctx);
300 void __hidden ubus_poll_data(struct ubus_context *ctx, int timeout)
302 struct pollfd pfd = {
304 .events = POLLIN | POLLERR,
307 poll(&pfd, 1, timeout);
308 ubus_handle_data(&ctx->sock, ULOOP_READ);
312 ubus_refresh_state(struct ubus_context *ctx)
314 struct ubus_object *obj, *tmp;
315 struct ubus_object **objs;
318 /* clear all type IDs, they need to be registered again */
319 avl_for_each_element(&ctx->objects, obj, avl)
323 /* push out all objects again */
324 objs = alloca(ctx->objects.count * sizeof(*objs));
325 avl_remove_all_elements(&ctx->objects, obj, avl, tmp) {
330 for (n = i, i = 0; i < n; i++)
331 ubus_add_object(ctx, objs[i]);
334 int ubus_reconnect(struct ubus_context *ctx, const char *path)
337 struct ubus_msghdr hdr;
338 struct blob_attr data;
340 struct blob_attr *buf;
341 int ret = UBUS_STATUS_UNKNOWN_ERROR;
344 path = UBUS_UNIX_SOCKET;
346 if (ctx->sock.fd >= 0) {
347 if (ctx->sock.registered)
348 uloop_fd_delete(&ctx->sock);
353 ctx->sock.fd = usock(USOCK_UNIX, path, NULL);
354 if (ctx->sock.fd < 0)
355 return UBUS_STATUS_CONNECTION_FAILED;
357 if (read(ctx->sock.fd, &hdr, sizeof(hdr)) != sizeof(hdr))
360 if (!ubus_validate_hdr(&hdr.hdr))
363 if (hdr.hdr.type != UBUS_MSG_HELLO)
366 buf = calloc(1, blob_raw_len(&hdr.data));
370 memcpy(buf, &hdr.data, sizeof(hdr.data));
371 if (read(ctx->sock.fd, blob_data(buf), blob_len(buf)) != blob_len(buf))
374 ctx->local_id = hdr.hdr.peer;
378 ret = UBUS_STATUS_OK;
379 fcntl(ctx->sock.fd, F_SETFL, fcntl(ctx->sock.fd, F_GETFL) | O_NONBLOCK);
381 ubus_refresh_state(ctx);