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 static const struct blob_attr_info ubus_policy[UBUS_ATTR_MAX] = {
32 [UBUS_ATTR_STATUS] = { .type = BLOB_ATTR_INT32 },
33 [UBUS_ATTR_OBJID] = { .type = BLOB_ATTR_INT32 },
34 [UBUS_ATTR_OBJPATH] = { .type = BLOB_ATTR_STRING },
35 [UBUS_ATTR_METHOD] = { .type = BLOB_ATTR_STRING },
36 [UBUS_ATTR_ACTIVE] = { .type = BLOB_ATTR_INT8 },
37 [UBUS_ATTR_NO_REPLY] = { .type = BLOB_ATTR_INT8 },
38 [UBUS_ATTR_SUBSCRIBERS] = { .type = BLOB_ATTR_NESTED },
41 static struct blob_attr *attrbuf[UBUS_ATTR_MAX];
43 __hidden struct blob_attr **ubus_parse_msg(struct blob_attr *msg)
45 blob_parse(msg, attrbuf, ubus_policy, UBUS_ATTR_MAX);
49 static void wait_data(int fd, bool write)
51 struct pollfd pfd = { .fd = fd };
53 pfd.events = write ? POLLOUT : POLLIN;
57 static int writev_retry(int fd, struct iovec *iov, int iov_len, int sock_fd)
64 .cmsg_len = sizeof(fd_buf),
65 .cmsg_level = SOL_SOCKET,
66 .cmsg_type = SCM_RIGHTS,
69 struct msghdr msghdr = {
71 .msg_iovlen = iov_len,
72 .msg_control = &fd_buf,
73 .msg_controllen = sizeof(fd_buf),
81 msghdr.msg_control = NULL;
82 msghdr.msg_controllen = 0;
87 cur_len = sendmsg(fd, &msghdr, 0);
105 while (cur_len >= iov->iov_len) {
106 cur_len -= iov->iov_len;
112 iov->iov_len -= cur_len;
115 /* Should never reach here */
119 int __hidden ubus_send_msg(struct ubus_context *ctx, uint32_t seq,
120 struct blob_attr *msg, int cmd, uint32_t peer, int fd)
122 struct ubus_msghdr hdr;
123 struct iovec iov[2] = {
134 blob_buf_init(&b, 0);
138 iov[1].iov_base = (char *) msg;
139 iov[1].iov_len = blob_raw_len(msg);
141 ret = writev_retry(ctx->sock.fd, iov, ARRAY_SIZE(iov), fd);
143 ctx->sock.eof = true;
151 static int recv_retry(int fd, struct iovec *iov, bool wait, int *recv_fd)
153 int bytes, total = 0;
159 .cmsg_type = SCM_RIGHTS,
160 .cmsg_level = SOL_SOCKET,
161 .cmsg_len = sizeof(fd_buf),
164 struct msghdr msghdr = {
169 while (iov->iov_len > 0) {
171 wait_data(fd, false);
174 msghdr.msg_control = &fd_buf;
175 msghdr.msg_controllen = sizeof(fd_buf);
177 msghdr.msg_control = NULL;
178 msghdr.msg_controllen = 0;
182 bytes = recvmsg(fd, &msghdr, 0);
200 *recv_fd = fd_buf.fd;
205 iov->iov_len -= bytes;
206 iov->iov_base += bytes;
213 static bool ubus_validate_hdr(struct ubus_msghdr *hdr)
215 struct blob_attr *data = ubus_msghdr_data(hdr);
217 if (hdr->version != 0)
220 if (blob_raw_len(data) < sizeof(*data))
223 if (blob_pad_len(data) > UBUS_MAX_MSGLEN)
229 static bool get_next_msg(struct ubus_context *ctx, int *recv_fd)
231 struct iovec iov = STATIC_IOV(ctx->msgbuf.hdr);
234 /* receive header + start attribute */
235 iov.iov_len += sizeof(struct blob_attr);
236 r = recv_retry(ctx->sock.fd, &iov, false, recv_fd);
239 ctx->sock.eof = true;
244 iov.iov_len = blob_len(ubus_msghdr_data(&ctx->msgbuf.hdr));
245 if (iov.iov_len > 0 && !recv_retry(ctx->sock.fd, &iov, true, NULL))
248 return ubus_validate_hdr(&ctx->msgbuf.hdr);
251 void __hidden ubus_handle_data(struct uloop_fd *u, unsigned int events)
253 struct ubus_context *ctx = container_of(u, struct ubus_context, sock);
254 struct ubus_msghdr *hdr = &ctx->msgbuf.hdr;
257 while (get_next_msg(ctx, &recv_fd)) {
258 ubus_process_msg(ctx, hdr, recv_fd);
264 ctx->connection_lost(ctx);
267 void __hidden ubus_poll_data(struct ubus_context *ctx, int timeout)
269 struct pollfd pfd = {
271 .events = POLLIN | POLLERR,
274 poll(&pfd, 1, timeout);
275 ubus_handle_data(&ctx->sock, ULOOP_READ);
279 ubus_refresh_state(struct ubus_context *ctx)
281 struct ubus_object *obj, *tmp;
282 struct ubus_object **objs;
285 /* clear all type IDs, they need to be registered again */
286 avl_for_each_element(&ctx->objects, obj, avl)
290 /* push out all objects again */
291 objs = alloca(ctx->objects.count * sizeof(*objs));
292 avl_remove_all_elements(&ctx->objects, obj, avl, tmp) {
297 for (n = i, i = 0; i < n; i++)
298 ubus_add_object(ctx, objs[i]);
301 int ubus_reconnect(struct ubus_context *ctx, const char *path)
304 struct ubus_msghdr hdr;
305 struct blob_attr data;
307 struct blob_attr *buf;
308 int ret = UBUS_STATUS_UNKNOWN_ERROR;
311 path = UBUS_UNIX_SOCKET;
313 if (ctx->sock.fd >= 0) {
314 if (ctx->sock.registered)
315 uloop_fd_delete(&ctx->sock);
320 ctx->sock.fd = usock(USOCK_UNIX, path, NULL);
321 if (ctx->sock.fd < 0)
322 return UBUS_STATUS_CONNECTION_FAILED;
324 if (read(ctx->sock.fd, &hdr, sizeof(hdr)) != sizeof(hdr))
327 if (!ubus_validate_hdr(&hdr.hdr))
330 if (hdr.hdr.type != UBUS_MSG_HELLO)
333 buf = calloc(1, blob_raw_len(&hdr.data));
337 memcpy(buf, &hdr.data, sizeof(hdr.data));
338 if (read(ctx->sock.fd, blob_data(buf), blob_len(buf)) != blob_len(buf))
341 ctx->local_id = hdr.hdr.peer;
345 ret = UBUS_STATUS_OK;
346 fcntl(ctx->sock.fd, F_SETFL, fcntl(ctx->sock.fd, F_GETFL) | O_NONBLOCK);
348 ubus_refresh_state(ctx);