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>
26 #include <libubox/blob.h>
27 #include <libubox/uloop.h>
28 #include <libubox/usock.h>
29 #include <libubox/list.h>
33 static struct ubus_msg_buf *ubus_msg_ref(struct ubus_msg_buf *ub)
35 if (ub->refcount == ~0)
36 return ubus_msg_new(ub->data, ub->len, false);
42 struct ubus_msg_buf *ubus_msg_new(void *data, int len, bool shared)
44 struct ubus_msg_buf *ub;
45 int buflen = sizeof(*ub);
50 ub = calloc(1, buflen);
61 ub->data = (void *) (ub + 1);
63 memcpy(ub + 1, data, len);
70 void ubus_msg_free(struct ubus_msg_buf *ub)
72 switch (ub->refcount) {
86 static int ubus_msg_writev(int fd, struct ubus_msg_buf *ub, int offset)
88 static struct iovec iov[2];
94 .cmsg_len = sizeof(fd_buf),
95 .cmsg_level = SOL_SOCKET,
96 .cmsg_type = SCM_RIGHTS,
99 struct msghdr msghdr = {
101 .msg_iovlen = ARRAY_SIZE(iov),
102 .msg_control = &fd_buf,
103 .msg_controllen = sizeof(fd_buf),
108 msghdr.msg_control = NULL;
109 msghdr.msg_controllen = 0;
112 if (offset < sizeof(ub->hdr)) {
113 iov[0].iov_base = ((char *) &ub->hdr) + offset;
114 iov[0].iov_len = sizeof(ub->hdr) - offset;
115 iov[1].iov_base = (char *) ub->data;
116 iov[1].iov_len = ub->len;
118 return sendmsg(fd, &msghdr, 0);
120 offset -= sizeof(ub->hdr);
121 return write(fd, ((char *) ub->data) + offset, ub->len - offset);
125 static void ubus_msg_enqueue(struct ubus_client *cl, struct ubus_msg_buf *ub)
127 if (cl->tx_queue[cl->txq_tail])
130 cl->tx_queue[cl->txq_tail] = ubus_msg_ref(ub);
131 cl->txq_tail = (cl->txq_tail + 1) % ARRAY_SIZE(cl->tx_queue);
134 /* takes the msgbuf reference */
135 void ubus_msg_send(struct ubus_client *cl, struct ubus_msg_buf *ub, bool free)
139 if (!cl->tx_queue[cl->txq_cur]) {
140 written = ubus_msg_writev(cl->sock.fd, ub, 0);
141 if (written >= ub->len + sizeof(ub->hdr))
147 cl->txq_ofs = written;
149 /* get an event once we can write to the socket again */
150 uloop_fd_add(&cl->sock, ULOOP_READ | ULOOP_WRITE | ULOOP_EDGE_TRIGGER);
152 ubus_msg_enqueue(cl, ub);
159 static struct ubus_msg_buf *ubus_msg_head(struct ubus_client *cl)
161 return cl->tx_queue[cl->txq_cur];
164 static void ubus_msg_dequeue(struct ubus_client *cl)
166 struct ubus_msg_buf *ub = ubus_msg_head(cl);
173 cl->tx_queue[cl->txq_cur] = NULL;
174 cl->txq_cur = (cl->txq_cur + 1) % ARRAY_SIZE(cl->tx_queue);
177 static void handle_client_disconnect(struct ubus_client *cl)
179 while (ubus_msg_head(cl))
180 ubus_msg_dequeue(cl);
182 ubusd_proto_free_client(cl);
183 if (cl->pending_msg_fd >= 0)
184 close(cl->pending_msg_fd);
185 uloop_fd_delete(&cl->sock);
190 static void client_cb(struct uloop_fd *sock, unsigned int events)
192 struct ubus_client *cl = container_of(sock, struct ubus_client, sock);
193 struct ubus_msg_buf *ub;
194 static struct iovec iov;
200 .cmsg_type = SCM_RIGHTS,
201 .cmsg_level = SOL_SOCKET,
202 .cmsg_len = sizeof(fd_buf),
205 struct msghdr msghdr = {
210 /* first try to tx more pending data */
211 while ((ub = ubus_msg_head(cl))) {
214 written = ubus_msg_writev(sock->fd, ub, cl->txq_ofs);
226 cl->txq_ofs += written;
227 if (cl->txq_ofs < ub->len + sizeof(ub->hdr))
230 ubus_msg_dequeue(cl);
233 /* prevent further ULOOP_WRITE events if we don't have data
235 if (!ubus_msg_head(cl) && (events & ULOOP_WRITE))
236 uloop_fd_add(sock, ULOOP_READ | ULOOP_EDGE_TRIGGER);
239 if (!sock->eof && cl->pending_msg_offset < sizeof(cl->hdrbuf)) {
240 int offset = cl->pending_msg_offset;
245 iov.iov_base = ((char *) &cl->hdrbuf) + offset;
246 iov.iov_len = sizeof(cl->hdrbuf) - offset;
248 if (cl->pending_msg_fd < 0) {
249 msghdr.msg_control = &fd_buf;
250 msghdr.msg_controllen = sizeof(fd_buf);
252 msghdr.msg_control = NULL;
253 msghdr.msg_controllen = 0;
256 bytes = recvmsg(sock->fd, &msghdr, 0);
261 cl->pending_msg_fd = fd_buf.fd;
263 cl->pending_msg_offset += bytes;
264 if (cl->pending_msg_offset < sizeof(cl->hdrbuf))
267 if (blob_pad_len(&cl->hdrbuf.data) > UBUS_MAX_MSGLEN)
270 cl->pending_msg = ubus_msg_new(NULL, blob_raw_len(&cl->hdrbuf.data), false);
271 if (!cl->pending_msg)
274 memcpy(&cl->pending_msg->hdr, &cl->hdrbuf.hdr, sizeof(cl->hdrbuf.hdr));
275 memcpy(cl->pending_msg->data, &cl->hdrbuf.data, sizeof(cl->hdrbuf.data));
278 ub = cl->pending_msg;
280 int offset = cl->pending_msg_offset - sizeof(ub->hdr);
281 int len = blob_raw_len(ub->data) - offset;
285 bytes = read(sock->fd, (char *) ub->data + offset, len);
291 cl->pending_msg_offset += bytes;
296 ub->fd = cl->pending_msg_fd;
297 cl->pending_msg_fd = -1;
298 cl->pending_msg_offset = 0;
299 cl->pending_msg = NULL;
300 ubusd_proto_receive_message(cl, ub);
305 if (!sock->eof || ubus_msg_head(cl))
309 handle_client_disconnect(cl);
312 static bool get_next_connection(int fd)
314 struct ubus_client *cl;
317 client_fd = accept(fd, NULL, 0);
328 cl = ubusd_proto_new_client(client_fd, client_cb);
330 uloop_fd_add(&cl->sock, ULOOP_READ | ULOOP_EDGE_TRIGGER);
337 static void server_cb(struct uloop_fd *fd, unsigned int events)
342 next = get_next_connection(fd->fd);
346 static struct uloop_fd server_fd = {
350 static int usage(const char *progname)
352 fprintf(stderr, "Usage: %s [<options>]\n"
354 " -s <socket>: Set the unix domain socket to listen on\n"
359 static void sighup_handler(int sig)
364 int main(int argc, char **argv)
366 const char *ubus_socket = UBUS_UNIX_SOCKET;
370 signal(SIGPIPE, SIG_IGN);
371 signal(SIGHUP, sighup_handler);
373 openlog("ubusd", LOG_PID, LOG_DAEMON);
376 while ((ch = getopt(argc, argv, "s:")) != -1) {
379 ubus_socket = optarg;
382 return usage(argv[0]);
388 server_fd.fd = usock(USOCK_UNIX | USOCK_SERVER | USOCK_NONBLOCK, ubus_socket, NULL);
389 if (server_fd.fd < 0) {
394 uloop_fd_add(&server_fd, ULOOP_READ | ULOOP_EDGE_TRIGGER);