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/socket.h>
18 #include <sys/param.h>
25 #include <libubox/blob.h>
26 #include <libubox/uloop.h>
27 #include <libubox/usock.h>
28 #include <libubox/list.h>
32 static struct ubus_msg_buf *ubus_msg_ref(struct ubus_msg_buf *ub)
34 if (ub->refcount == ~0)
35 return ubus_msg_new(ub->data, ub->len, false);
41 struct ubus_msg_buf *ubus_msg_new(void *data, int len, bool shared)
43 struct ubus_msg_buf *ub;
44 int buflen = sizeof(*ub);
49 ub = calloc(1, buflen);
60 ub->data = (void *) (ub + 1);
62 memcpy(ub + 1, data, len);
69 void ubus_msg_free(struct ubus_msg_buf *ub)
71 switch (ub->refcount) {
85 static int ubus_msg_writev(int fd, struct ubus_msg_buf *ub, int offset)
87 static struct iovec iov[2];
93 .cmsg_len = sizeof(fd_buf),
94 .cmsg_level = SOL_SOCKET,
95 .cmsg_type = SCM_RIGHTS,
98 struct msghdr msghdr = {
100 .msg_iovlen = ARRAY_SIZE(iov),
101 .msg_control = &fd_buf,
102 .msg_controllen = sizeof(fd_buf),
107 msghdr.msg_control = NULL;
108 msghdr.msg_controllen = 0;
111 if (offset < sizeof(ub->hdr)) {
112 iov[0].iov_base = ((char *) &ub->hdr) + offset;
113 iov[0].iov_len = sizeof(ub->hdr) - offset;
114 iov[1].iov_base = (char *) ub->data;
115 iov[1].iov_len = ub->len;
117 return sendmsg(fd, &msghdr, 0);
119 offset -= sizeof(ub->hdr);
120 return write(fd, ((char *) ub->data) + offset, ub->len - offset);
124 static void ubus_msg_enqueue(struct ubus_client *cl, struct ubus_msg_buf *ub)
126 if (cl->tx_queue[cl->txq_tail])
129 cl->tx_queue[cl->txq_tail] = ubus_msg_ref(ub);
130 cl->txq_tail = (cl->txq_tail + 1) % ARRAY_SIZE(cl->tx_queue);
133 /* takes the msgbuf reference */
134 void ubus_msg_send(struct ubus_client *cl, struct ubus_msg_buf *ub, bool free)
138 if (!cl->tx_queue[cl->txq_cur]) {
139 written = ubus_msg_writev(cl->sock.fd, ub, 0);
140 if (written >= ub->len + sizeof(ub->hdr))
146 cl->txq_ofs = written;
148 /* get an event once we can write to the socket again */
149 uloop_fd_add(&cl->sock, ULOOP_READ | ULOOP_WRITE | ULOOP_EDGE_TRIGGER);
151 ubus_msg_enqueue(cl, ub);
158 static struct ubus_msg_buf *ubus_msg_head(struct ubus_client *cl)
160 return cl->tx_queue[cl->txq_cur];
163 static void ubus_msg_dequeue(struct ubus_client *cl)
165 struct ubus_msg_buf *ub = ubus_msg_head(cl);
172 cl->tx_queue[cl->txq_cur] = NULL;
173 cl->txq_cur = (cl->txq_cur + 1) % ARRAY_SIZE(cl->tx_queue);
176 static void handle_client_disconnect(struct ubus_client *cl)
178 while (ubus_msg_head(cl))
179 ubus_msg_dequeue(cl);
181 ubusd_proto_free_client(cl);
182 if (cl->pending_msg_fd >= 0)
183 close(cl->pending_msg_fd);
184 uloop_fd_delete(&cl->sock);
189 static void client_cb(struct uloop_fd *sock, unsigned int events)
191 struct ubus_client *cl = container_of(sock, struct ubus_client, sock);
192 struct ubus_msg_buf *ub;
193 static struct iovec iov;
199 .cmsg_type = SCM_RIGHTS,
200 .cmsg_level = SOL_SOCKET,
201 .cmsg_len = sizeof(fd_buf),
204 struct msghdr msghdr = {
209 /* first try to tx more pending data */
210 while ((ub = ubus_msg_head(cl))) {
213 written = ubus_msg_writev(sock->fd, ub, cl->txq_ofs);
225 cl->txq_ofs += written;
226 if (cl->txq_ofs < ub->len + sizeof(ub->hdr))
229 ubus_msg_dequeue(cl);
232 /* prevent further ULOOP_WRITE events if we don't have data
234 if (!ubus_msg_head(cl) && (events & ULOOP_WRITE))
235 uloop_fd_add(sock, ULOOP_READ | ULOOP_EDGE_TRIGGER);
238 if (!sock->eof && cl->pending_msg_offset < sizeof(cl->hdrbuf)) {
239 int offset = cl->pending_msg_offset;
244 iov.iov_base = &cl->hdrbuf + offset;
245 iov.iov_len = sizeof(cl->hdrbuf) - offset;
247 if (cl->pending_msg_fd < 0) {
248 msghdr.msg_control = &fd_buf;
249 msghdr.msg_controllen = sizeof(fd_buf);
251 msghdr.msg_control = NULL;
252 msghdr.msg_controllen = 0;
255 bytes = recvmsg(sock->fd, &msghdr, 0);
260 cl->pending_msg_fd = fd_buf.fd;
262 cl->pending_msg_offset += bytes;
263 if (cl->pending_msg_offset < sizeof(cl->hdrbuf))
266 if (blob_pad_len(&cl->hdrbuf.data) > UBUS_MAX_MSGLEN)
269 cl->pending_msg = ubus_msg_new(NULL, blob_raw_len(&cl->hdrbuf.data), false);
270 if (!cl->pending_msg)
273 memcpy(&cl->pending_msg->hdr, &cl->hdrbuf.hdr, sizeof(cl->hdrbuf.hdr));
274 memcpy(cl->pending_msg->data, &cl->hdrbuf.data, sizeof(cl->hdrbuf.data));
277 ub = cl->pending_msg;
279 int offset = cl->pending_msg_offset - sizeof(ub->hdr);
280 int len = blob_raw_len(ub->data) - offset;
284 bytes = read(sock->fd, (char *) ub->data + offset, len);
290 cl->pending_msg_offset += bytes;
295 ub->fd = cl->pending_msg_fd;
296 cl->pending_msg_fd = -1;
297 cl->pending_msg_offset = 0;
298 cl->pending_msg = NULL;
299 ubusd_proto_receive_message(cl, ub);
304 if (!sock->eof || ubus_msg_head(cl))
308 handle_client_disconnect(cl);
311 static bool get_next_connection(int fd)
313 struct ubus_client *cl;
316 client_fd = accept(fd, NULL, 0);
327 cl = ubusd_proto_new_client(client_fd, client_cb);
329 uloop_fd_add(&cl->sock, ULOOP_READ | ULOOP_EDGE_TRIGGER);
336 static void server_cb(struct uloop_fd *fd, unsigned int events)
341 next = get_next_connection(fd->fd);
345 static struct uloop_fd server_fd = {
349 static int usage(const char *progname)
351 fprintf(stderr, "Usage: %s [<options>]\n"
353 " -s <socket>: Set the unix domain socket to listen on\n"
358 int main(int argc, char **argv)
360 const char *ubus_socket = UBUS_UNIX_SOCKET;
364 signal(SIGPIPE, SIG_IGN);
368 while ((ch = getopt(argc, argv, "s:")) != -1) {
371 ubus_socket = optarg;
374 return usage(argv[0]);
380 server_fd.fd = usock(USOCK_UNIX | USOCK_SERVER | USOCK_NONBLOCK, ubus_socket, NULL);
381 if (server_fd.fd < 0) {
386 uloop_fd_add(&server_fd, ULOOP_READ | ULOOP_EDGE_TRIGGER);