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 struct ubus_msghdr hdr;
115 hdr.version = ub->hdr.version;
116 hdr.type = ub->hdr.type;
117 hdr.seq = cpu_to_be16(ub->hdr.seq);
118 hdr.peer = cpu_to_be32(ub->hdr.peer);
120 iov[0].iov_base = ((char *) &hdr) + offset;
121 iov[0].iov_len = sizeof(hdr) - offset;
122 iov[1].iov_base = (char *) ub->data;
123 iov[1].iov_len = ub->len;
125 return sendmsg(fd, &msghdr, 0);
127 offset -= sizeof(ub->hdr);
128 return write(fd, ((char *) ub->data) + offset, ub->len - offset);
132 static void ubus_msg_enqueue(struct ubus_client *cl, struct ubus_msg_buf *ub)
134 if (cl->tx_queue[cl->txq_tail])
137 cl->tx_queue[cl->txq_tail] = ubus_msg_ref(ub);
138 cl->txq_tail = (cl->txq_tail + 1) % ARRAY_SIZE(cl->tx_queue);
141 /* takes the msgbuf reference */
142 void ubus_msg_send(struct ubus_client *cl, struct ubus_msg_buf *ub, bool free)
146 if (ub->hdr.type != UBUS_MSG_MONITOR)
147 ubusd_monitor_message(cl, ub, true);
149 if (!cl->tx_queue[cl->txq_cur]) {
150 written = ubus_msg_writev(cl->sock.fd, ub, 0);
151 if (written >= ub->len + sizeof(ub->hdr))
157 cl->txq_ofs = written;
159 /* get an event once we can write to the socket again */
160 uloop_fd_add(&cl->sock, ULOOP_READ | ULOOP_WRITE | ULOOP_EDGE_TRIGGER);
162 ubus_msg_enqueue(cl, ub);
169 static struct ubus_msg_buf *ubus_msg_head(struct ubus_client *cl)
171 return cl->tx_queue[cl->txq_cur];
174 static void ubus_msg_dequeue(struct ubus_client *cl)
176 struct ubus_msg_buf *ub = ubus_msg_head(cl);
183 cl->tx_queue[cl->txq_cur] = NULL;
184 cl->txq_cur = (cl->txq_cur + 1) % ARRAY_SIZE(cl->tx_queue);
187 static void handle_client_disconnect(struct ubus_client *cl)
189 while (ubus_msg_head(cl))
190 ubus_msg_dequeue(cl);
192 ubusd_monitor_disconnect(cl);
193 ubusd_proto_free_client(cl);
194 if (cl->pending_msg_fd >= 0)
195 close(cl->pending_msg_fd);
196 uloop_fd_delete(&cl->sock);
201 static void client_cb(struct uloop_fd *sock, unsigned int events)
203 struct ubus_client *cl = container_of(sock, struct ubus_client, sock);
204 struct ubus_msg_buf *ub;
205 static struct iovec iov;
211 .cmsg_type = SCM_RIGHTS,
212 .cmsg_level = SOL_SOCKET,
213 .cmsg_len = sizeof(fd_buf),
216 struct msghdr msghdr = {
221 /* first try to tx more pending data */
222 while ((ub = ubus_msg_head(cl))) {
225 written = ubus_msg_writev(sock->fd, ub, cl->txq_ofs);
237 cl->txq_ofs += written;
238 if (cl->txq_ofs < ub->len + sizeof(ub->hdr))
241 ubus_msg_dequeue(cl);
244 /* prevent further ULOOP_WRITE events if we don't have data
246 if (!ubus_msg_head(cl) && (events & ULOOP_WRITE))
247 uloop_fd_add(sock, ULOOP_READ | ULOOP_EDGE_TRIGGER);
250 if (!sock->eof && cl->pending_msg_offset < sizeof(cl->hdrbuf)) {
251 int offset = cl->pending_msg_offset;
256 iov.iov_base = ((char *) &cl->hdrbuf) + offset;
257 iov.iov_len = sizeof(cl->hdrbuf) - offset;
259 if (cl->pending_msg_fd < 0) {
260 msghdr.msg_control = &fd_buf;
261 msghdr.msg_controllen = sizeof(fd_buf);
263 msghdr.msg_control = NULL;
264 msghdr.msg_controllen = 0;
267 bytes = recvmsg(sock->fd, &msghdr, 0);
272 cl->pending_msg_fd = fd_buf.fd;
274 cl->pending_msg_offset += bytes;
275 if (cl->pending_msg_offset < sizeof(cl->hdrbuf))
278 if (blob_pad_len(&cl->hdrbuf.data) > UBUS_MAX_MSGLEN)
281 cl->pending_msg = ubus_msg_new(NULL, blob_raw_len(&cl->hdrbuf.data), false);
282 if (!cl->pending_msg)
285 cl->hdrbuf.hdr.seq = be16_to_cpu(cl->hdrbuf.hdr.seq);
286 cl->hdrbuf.hdr.peer = be32_to_cpu(cl->hdrbuf.hdr.peer);
288 memcpy(&cl->pending_msg->hdr, &cl->hdrbuf.hdr, sizeof(cl->hdrbuf.hdr));
289 memcpy(cl->pending_msg->data, &cl->hdrbuf.data, sizeof(cl->hdrbuf.data));
292 ub = cl->pending_msg;
294 int offset = cl->pending_msg_offset - sizeof(ub->hdr);
295 int len = blob_raw_len(ub->data) - offset;
299 bytes = read(sock->fd, (char *) ub->data + offset, len);
305 cl->pending_msg_offset += bytes;
310 ub->fd = cl->pending_msg_fd;
311 cl->pending_msg_fd = -1;
312 cl->pending_msg_offset = 0;
313 cl->pending_msg = NULL;
314 ubusd_monitor_message(cl, ub, false);
315 ubusd_proto_receive_message(cl, ub);
320 if (!sock->eof || ubus_msg_head(cl))
324 handle_client_disconnect(cl);
327 static bool get_next_connection(int fd)
329 struct ubus_client *cl;
332 client_fd = accept(fd, NULL, 0);
343 cl = ubusd_proto_new_client(client_fd, client_cb);
345 uloop_fd_add(&cl->sock, ULOOP_READ | ULOOP_EDGE_TRIGGER);
352 static void server_cb(struct uloop_fd *fd, unsigned int events)
357 next = get_next_connection(fd->fd);
361 static struct uloop_fd server_fd = {
365 static int usage(const char *progname)
367 fprintf(stderr, "Usage: %s [<options>]\n"
369 " -A <path>: Set the path to ACL files\n"
370 " -s <socket>: Set the unix domain socket to listen on\n"
375 static void sighup_handler(int sig)
380 int main(int argc, char **argv)
382 const char *ubus_socket = UBUS_UNIX_SOCKET;
386 signal(SIGPIPE, SIG_IGN);
387 signal(SIGHUP, sighup_handler);
389 openlog("ubusd", LOG_PID, LOG_DAEMON);
392 while ((ch = getopt(argc, argv, "A:s:")) != -1) {
395 ubus_socket = optarg;
398 ubusd_acl_dir = optarg;
401 return usage(argv[0]);
407 server_fd.fd = usock(USOCK_UNIX | USOCK_SERVER | USOCK_NONBLOCK, ubus_socket, NULL);
408 if (server_fd.fd < 0) {
413 uloop_fd_add(&server_fd, ULOOP_READ | ULOOP_EDGE_TRIGGER);