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.
15 #include <sys/types.h>
17 #include <sys/socket.h>
23 #include <libubox/usock.h>
24 #include <libubox/blob.h>
25 #include <libubox/blobmsg.h>
28 #include "libubus-internal.h"
30 #define STATIC_IOV(_var) { .iov_base = (char *) &(_var), .iov_len = sizeof(_var) }
32 #define UBUS_MSGBUF_REDUCTION_INTERVAL 16
34 static const struct blob_attr_info ubus_policy[UBUS_ATTR_MAX] = {
35 [UBUS_ATTR_STATUS] = { .type = BLOB_ATTR_INT32 },
36 [UBUS_ATTR_OBJID] = { .type = BLOB_ATTR_INT32 },
37 [UBUS_ATTR_OBJPATH] = { .type = BLOB_ATTR_STRING },
38 [UBUS_ATTR_METHOD] = { .type = BLOB_ATTR_STRING },
39 [UBUS_ATTR_ACTIVE] = { .type = BLOB_ATTR_INT8 },
40 [UBUS_ATTR_NO_REPLY] = { .type = BLOB_ATTR_INT8 },
41 [UBUS_ATTR_SUBSCRIBERS] = { .type = BLOB_ATTR_NESTED },
44 static struct blob_attr *attrbuf[UBUS_ATTR_MAX];
46 __hidden struct blob_attr **ubus_parse_msg(struct blob_attr *msg)
48 blob_parse(msg, attrbuf, ubus_policy, UBUS_ATTR_MAX);
52 static void wait_data(int fd, bool write)
54 struct pollfd pfd = { .fd = fd };
56 pfd.events = write ? POLLOUT : POLLIN;
60 static int writev_retry(int fd, struct iovec *iov, int iov_len, int sock_fd)
67 .cmsg_len = sizeof(fd_buf),
68 .cmsg_level = SOL_SOCKET,
69 .cmsg_type = SCM_RIGHTS,
72 struct msghdr msghdr = {
74 .msg_iovlen = iov_len,
75 .msg_control = &fd_buf,
76 .msg_controllen = sizeof(fd_buf),
84 msghdr.msg_control = NULL;
85 msghdr.msg_controllen = 0;
90 cur_len = sendmsg(fd, &msghdr, 0);
108 while (cur_len >= iov->iov_len) {
109 cur_len -= iov->iov_len;
115 iov->iov_base += cur_len;
116 iov->iov_len -= cur_len;
117 msghdr.msg_iov = iov;
118 msghdr.msg_iovlen = iov_len;
121 /* Should never reach here */
125 int __hidden ubus_send_msg(struct ubus_context *ctx, uint32_t seq,
126 struct blob_attr *msg, int cmd, uint32_t peer, int fd)
128 struct ubus_msghdr hdr;
129 struct iovec iov[2] = {
140 blob_buf_init(&b, 0);
144 iov[1].iov_base = (char *) msg;
145 iov[1].iov_len = blob_raw_len(msg);
147 ret = writev_retry(ctx->sock.fd, iov, ARRAY_SIZE(iov), fd);
149 ctx->sock.eof = true;
157 static int recv_retry(int fd, struct iovec *iov, bool wait, int *recv_fd)
159 int bytes, total = 0;
165 .cmsg_type = SCM_RIGHTS,
166 .cmsg_level = SOL_SOCKET,
167 .cmsg_len = sizeof(fd_buf),
170 struct msghdr msghdr = {
175 while (iov->iov_len > 0) {
177 wait_data(fd, false);
180 msghdr.msg_control = &fd_buf;
181 msghdr.msg_controllen = sizeof(fd_buf);
183 msghdr.msg_control = NULL;
184 msghdr.msg_controllen = 0;
188 bytes = recvmsg(fd, &msghdr, 0);
206 *recv_fd = fd_buf.fd;
211 iov->iov_len -= bytes;
212 iov->iov_base += bytes;
219 static bool ubus_validate_hdr(struct ubus_msghdr *hdr)
221 struct blob_attr *data = (struct blob_attr *) (hdr + 1);
223 if (hdr->version != 0)
226 if (blob_raw_len(data) < sizeof(*data))
229 if (blob_pad_len(data) > UBUS_MAX_MSGLEN)
235 static bool alloc_msg_buf(struct ubus_context *ctx, int len)
238 int buf_len = ctx->msgbuf_data_len;
241 if (!ctx->msgbuf.data)
244 rem = (len % UBUS_MSG_CHUNK_SIZE);
246 len += UBUS_MSG_CHUNK_SIZE - rem;
249 ++ctx->msgbuf_reduction_counter > UBUS_MSGBUF_REDUCTION_INTERVAL) {
250 ctx->msgbuf_reduction_counter = 0;
257 ptr = realloc(ctx->msgbuf.data, len);
261 ctx->msgbuf.data = ptr;
265 static bool get_next_msg(struct ubus_context *ctx, int *recv_fd)
268 struct ubus_msghdr hdr;
269 struct blob_attr data;
271 struct iovec iov = STATIC_IOV(hdrbuf);
275 /* receive header + start attribute */
276 r = recv_retry(ctx->sock.fd, &iov, false, recv_fd);
279 ctx->sock.eof = true;
284 if (!ubus_validate_hdr(&hdrbuf.hdr))
287 len = blob_raw_len(&hdrbuf.data);
288 if (!alloc_msg_buf(ctx, len))
291 memcpy(&ctx->msgbuf.hdr, &hdrbuf.hdr, sizeof(hdrbuf.hdr));
292 memcpy(ctx->msgbuf.data, &hdrbuf.data, sizeof(hdrbuf.data));
294 iov.iov_base = (char *)ctx->msgbuf.data + sizeof(hdrbuf.data);
295 iov.iov_len = blob_len(ctx->msgbuf.data);
296 if (iov.iov_len > 0 && !recv_retry(ctx->sock.fd, &iov, true, NULL))
302 void __hidden ubus_handle_data(struct uloop_fd *u, unsigned int events)
304 struct ubus_context *ctx = container_of(u, struct ubus_context, sock);
307 while (get_next_msg(ctx, &recv_fd)) {
308 ubus_process_msg(ctx, &ctx->msgbuf, recv_fd);
314 ctx->connection_lost(ctx);
317 void __hidden ubus_poll_data(struct ubus_context *ctx, int timeout)
319 struct pollfd pfd = {
321 .events = POLLIN | POLLERR,
324 poll(&pfd, 1, timeout ? timeout : -1);
325 ubus_handle_data(&ctx->sock, ULOOP_READ);
329 ubus_refresh_state(struct ubus_context *ctx)
331 struct ubus_object *obj, *tmp;
332 struct ubus_object **objs;
335 /* clear all type IDs, they need to be registered again */
336 avl_for_each_element(&ctx->objects, obj, avl)
340 /* push out all objects again */
341 objs = alloca(ctx->objects.count * sizeof(*objs));
342 avl_remove_all_elements(&ctx->objects, obj, avl, tmp) {
347 for (n = i, i = 0; i < n; i++)
348 ubus_add_object(ctx, objs[i]);
351 int ubus_reconnect(struct ubus_context *ctx, const char *path)
354 struct ubus_msghdr hdr;
355 struct blob_attr data;
357 struct blob_attr *buf;
358 int ret = UBUS_STATUS_UNKNOWN_ERROR;
361 path = UBUS_UNIX_SOCKET;
363 if (ctx->sock.fd >= 0) {
364 if (ctx->sock.registered)
365 uloop_fd_delete(&ctx->sock);
370 ctx->sock.fd = usock(USOCK_UNIX, path, NULL);
371 if (ctx->sock.fd < 0)
372 return UBUS_STATUS_CONNECTION_FAILED;
374 if (read(ctx->sock.fd, &hdr, sizeof(hdr)) != sizeof(hdr))
377 if (!ubus_validate_hdr(&hdr.hdr))
380 if (hdr.hdr.type != UBUS_MSG_HELLO)
383 buf = calloc(1, blob_raw_len(&hdr.data));
387 memcpy(buf, &hdr.data, sizeof(hdr.data));
388 if (read(ctx->sock.fd, blob_data(buf), blob_len(buf)) != blob_len(buf))
391 ctx->local_id = hdr.hdr.peer;
395 ret = UBUS_STATUS_OK;
396 fcntl(ctx->sock.fd, F_SETFL, fcntl(ctx->sock.fd, F_GETFL) | O_NONBLOCK | O_CLOEXEC);
398 ubus_refresh_state(ctx);