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 <systemd/sd-daemon.h>
29 #include <libubox/blob.h>
30 #include <libubox/uloop.h>
31 #include <libubox/usock.h>
32 #include <libubox/list.h>
36 static struct ubus_msg_buf *ubus_msg_ref(struct ubus_msg_buf *ub)
38 if (ub->refcount == ~0)
39 return ubus_msg_new(ub->data, ub->len, false);
45 struct ubus_msg_buf *ubus_msg_new(void *data, int len, bool shared)
47 struct ubus_msg_buf *ub;
48 int buflen = sizeof(*ub);
53 ub = calloc(1, buflen);
64 ub->data = (void *) (ub + 1);
66 memcpy(ub + 1, data, len);
73 void ubus_msg_free(struct ubus_msg_buf *ub)
75 switch (ub->refcount) {
89 static int ubus_msg_writev(int fd, struct ubus_msg_buf *ub, int offset)
91 static struct iovec iov[2];
97 .cmsg_len = sizeof(fd_buf),
98 .cmsg_level = SOL_SOCKET,
99 .cmsg_type = SCM_RIGHTS,
102 struct msghdr msghdr = {
104 .msg_iovlen = ARRAY_SIZE(iov),
105 .msg_control = &fd_buf,
106 .msg_controllen = sizeof(fd_buf),
111 msghdr.msg_control = NULL;
112 msghdr.msg_controllen = 0;
115 if (offset < sizeof(ub->hdr)) {
116 struct ubus_msghdr hdr;
118 hdr.version = ub->hdr.version;
119 hdr.type = ub->hdr.type;
120 hdr.seq = cpu_to_be16(ub->hdr.seq);
121 hdr.peer = cpu_to_be32(ub->hdr.peer);
123 iov[0].iov_base = ((char *) &hdr) + offset;
124 iov[0].iov_len = sizeof(hdr) - offset;
125 iov[1].iov_base = (char *) ub->data;
126 iov[1].iov_len = ub->len;
128 return sendmsg(fd, &msghdr, 0);
130 offset -= sizeof(ub->hdr);
131 return write(fd, ((char *) ub->data) + offset, ub->len - offset);
135 static void ubus_msg_enqueue(struct ubus_client *cl, struct ubus_msg_buf *ub)
137 if (cl->tx_queue[cl->txq_tail])
140 cl->tx_queue[cl->txq_tail] = ubus_msg_ref(ub);
141 cl->txq_tail = (cl->txq_tail + 1) % ARRAY_SIZE(cl->tx_queue);
144 /* takes the msgbuf reference */
145 void ubus_msg_send(struct ubus_client *cl, struct ubus_msg_buf *ub, bool free)
149 if (ub->hdr.type != UBUS_MSG_MONITOR)
150 ubusd_monitor_message(cl, ub, true);
152 if (!cl->tx_queue[cl->txq_cur]) {
153 written = ubus_msg_writev(cl->sock.fd, ub, 0);
154 if (written >= ub->len + sizeof(ub->hdr))
160 cl->txq_ofs = written;
162 /* get an event once we can write to the socket again */
163 uloop_fd_add(&cl->sock, ULOOP_READ | ULOOP_WRITE | ULOOP_EDGE_TRIGGER);
165 ubus_msg_enqueue(cl, ub);
172 static struct ubus_msg_buf *ubus_msg_head(struct ubus_client *cl)
174 return cl->tx_queue[cl->txq_cur];
177 static void ubus_msg_dequeue(struct ubus_client *cl)
179 struct ubus_msg_buf *ub = ubus_msg_head(cl);
186 cl->tx_queue[cl->txq_cur] = NULL;
187 cl->txq_cur = (cl->txq_cur + 1) % ARRAY_SIZE(cl->tx_queue);
190 static void handle_client_disconnect(struct ubus_client *cl)
192 while (ubus_msg_head(cl))
193 ubus_msg_dequeue(cl);
195 ubusd_monitor_disconnect(cl);
196 ubusd_proto_free_client(cl);
197 if (cl->pending_msg_fd >= 0)
198 close(cl->pending_msg_fd);
199 uloop_fd_delete(&cl->sock);
204 static void client_cb(struct uloop_fd *sock, unsigned int events)
206 struct ubus_client *cl = container_of(sock, struct ubus_client, sock);
207 struct ubus_msg_buf *ub;
208 static struct iovec iov;
214 .cmsg_type = SCM_RIGHTS,
215 .cmsg_level = SOL_SOCKET,
216 .cmsg_len = sizeof(fd_buf),
219 struct msghdr msghdr = {
224 /* first try to tx more pending data */
225 while ((ub = ubus_msg_head(cl))) {
228 written = ubus_msg_writev(sock->fd, ub, cl->txq_ofs);
240 cl->txq_ofs += written;
241 if (cl->txq_ofs < ub->len + sizeof(ub->hdr))
244 ubus_msg_dequeue(cl);
247 /* prevent further ULOOP_WRITE events if we don't have data
249 if (!ubus_msg_head(cl) && (events & ULOOP_WRITE))
250 uloop_fd_add(sock, ULOOP_READ | ULOOP_EDGE_TRIGGER);
253 if (!sock->eof && cl->pending_msg_offset < sizeof(cl->hdrbuf)) {
254 int offset = cl->pending_msg_offset;
259 iov.iov_base = ((char *) &cl->hdrbuf) + offset;
260 iov.iov_len = sizeof(cl->hdrbuf) - offset;
262 if (cl->pending_msg_fd < 0) {
263 msghdr.msg_control = &fd_buf;
264 msghdr.msg_controllen = sizeof(fd_buf);
266 msghdr.msg_control = NULL;
267 msghdr.msg_controllen = 0;
270 bytes = recvmsg(sock->fd, &msghdr, 0);
275 cl->pending_msg_fd = fd_buf.fd;
277 cl->pending_msg_offset += bytes;
278 if (cl->pending_msg_offset < sizeof(cl->hdrbuf))
281 if (blob_pad_len(&cl->hdrbuf.data) > UBUS_MAX_MSGLEN)
284 cl->pending_msg = ubus_msg_new(NULL, blob_raw_len(&cl->hdrbuf.data), false);
285 if (!cl->pending_msg)
288 cl->hdrbuf.hdr.seq = be16_to_cpu(cl->hdrbuf.hdr.seq);
289 cl->hdrbuf.hdr.peer = be32_to_cpu(cl->hdrbuf.hdr.peer);
291 memcpy(&cl->pending_msg->hdr, &cl->hdrbuf.hdr, sizeof(cl->hdrbuf.hdr));
292 memcpy(cl->pending_msg->data, &cl->hdrbuf.data, sizeof(cl->hdrbuf.data));
295 ub = cl->pending_msg;
297 int offset = cl->pending_msg_offset - sizeof(ub->hdr);
298 int len = blob_raw_len(ub->data) - offset;
302 bytes = read(sock->fd, (char *) ub->data + offset, len);
308 cl->pending_msg_offset += bytes;
313 ub->fd = cl->pending_msg_fd;
314 cl->pending_msg_fd = -1;
315 cl->pending_msg_offset = 0;
316 cl->pending_msg = NULL;
317 ubusd_monitor_message(cl, ub, false);
318 ubusd_proto_receive_message(cl, ub);
323 if (!sock->eof || ubus_msg_head(cl))
327 handle_client_disconnect(cl);
330 static bool get_next_connection(int fd)
332 struct ubus_client *cl;
335 client_fd = accept(fd, NULL, 0);
346 cl = ubusd_proto_new_client(client_fd, client_cb);
348 uloop_fd_add(&cl->sock, ULOOP_READ | ULOOP_EDGE_TRIGGER);
355 static void server_cb(struct uloop_fd *fd, unsigned int events)
360 next = get_next_connection(fd->fd);
364 static struct uloop_fd server_fd = {
368 static int usage(const char *progname)
370 fprintf(stderr, "Usage: %s [<options>]\n"
372 " -A <path>: Set the path to ACL files\n"
373 " -s <socket>: Set the unix domain socket to listen on\n"
378 static void sighup_handler(int sig)
383 int main(int argc, char **argv)
385 const char *ubus_socket = UBUS_UNIX_SOCKET;
386 bool remove_socket = true;
389 #ifdef ENABLE_SYSTEMD
393 signal(SIGPIPE, SIG_IGN);
394 signal(SIGHUP, sighup_handler);
396 openlog("ubusd", LOG_PID, LOG_DAEMON);
399 while ((ch = getopt(argc, argv, "A:s:")) != -1) {
402 ubus_socket = optarg;
405 ubusd_acl_dir = optarg;
408 return usage(argv[0]);
412 #ifdef ENABLE_SYSTEMD
413 n_fds = sd_listen_fds(1);
415 fprintf(stderr, "Too many file descriptors received.\n");
418 } else if (n_fds == 1) {
419 server_fd.fd = SD_LISTEN_FDS_START + 0;
420 fcntl(server_fd.fd, F_SETFD, fcntl(server_fd.fd, F_GETFD) | FD_CLOEXEC);
421 fcntl(server_fd.fd, F_SETFL, fcntl(server_fd.fd, F_GETFL) | O_NONBLOCK);
423 remove_socket = false;
429 server_fd.fd = usock(USOCK_UNIX | USOCK_SERVER | USOCK_NONBLOCK, ubus_socket, NULL);
430 if (server_fd.fd < 0) {
436 uloop_fd_add(&server_fd, ULOOP_READ | ULOOP_EDGE_TRIGGER);