ubusd/libubus-io: fix socket descriptor passing
[project/ubus.git] / ubusd_main.c
1 /*
2 * Copyright (C) 2011-2014 Felix Fietkau <nbd@openwrt.org>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 */
6
7 #include <sys/socket.h>
8 #include <sys/stat.h>
9 #ifdef FreeBSD
10 #include <sys/param.h>
11 #endif
12 #include <syslog.h>
13
14 #include <libubox/usock.h>
15
16 #include "ubusd.h"
17
18 static struct ubus_msg_buf *ubus_msg_head(struct ubus_client *cl)
19 {
20 return cl->tx_queue[cl->txq_cur];
21 }
22
23 static void ubus_msg_dequeue(struct ubus_client *cl)
24 {
25 struct ubus_msg_buf *ub = ubus_msg_head(cl);
26
27 if (!ub)
28 return;
29
30 ubus_msg_free(ub);
31 cl->txq_ofs = 0;
32 cl->tx_queue[cl->txq_cur] = NULL;
33 cl->txq_cur = (cl->txq_cur + 1) % ARRAY_SIZE(cl->tx_queue);
34 }
35
36 static void handle_client_disconnect(struct ubus_client *cl)
37 {
38 while (ubus_msg_head(cl))
39 ubus_msg_dequeue(cl);
40
41 ubusd_monitor_disconnect(cl);
42 ubusd_proto_free_client(cl);
43 if (cl->pending_msg_fd >= 0)
44 close(cl->pending_msg_fd);
45 uloop_fd_delete(&cl->sock);
46 close(cl->sock.fd);
47 free(cl);
48 }
49
50 static void client_cb(struct uloop_fd *sock, unsigned int events)
51 {
52 struct ubus_client *cl = container_of(sock, struct ubus_client, sock);
53 uint8_t fd_buf[CMSG_SPACE(sizeof(int))] = { 0 };
54 struct msghdr msghdr = { 0 };
55 struct ubus_msg_buf *ub;
56 static struct iovec iov;
57 struct cmsghdr *cmsg;
58 int *pfd;
59
60 msghdr.msg_iov = &iov,
61 msghdr.msg_iovlen = 1,
62 msghdr.msg_control = fd_buf;
63 msghdr.msg_controllen = sizeof(fd_buf);
64
65 cmsg = CMSG_FIRSTHDR(&msghdr);
66 cmsg->cmsg_type = SCM_RIGHTS;
67 cmsg->cmsg_level = SOL_SOCKET;
68 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
69
70 pfd = (int *) CMSG_DATA(cmsg);
71 msghdr.msg_controllen = cmsg->cmsg_len;
72
73 /* first try to tx more pending data */
74 while ((ub = ubus_msg_head(cl))) {
75 ssize_t written;
76
77 written = ubus_msg_writev(sock->fd, ub, cl->txq_ofs);
78 if (written < 0) {
79 switch(errno) {
80 case EINTR:
81 case EAGAIN:
82 break;
83 default:
84 goto disconnect;
85 }
86 break;
87 }
88
89 cl->txq_ofs += written;
90 if (cl->txq_ofs < ub->len + sizeof(ub->hdr))
91 break;
92
93 ubus_msg_dequeue(cl);
94 }
95
96 /* prevent further ULOOP_WRITE events if we don't have data
97 * to send anymore */
98 if (!ubus_msg_head(cl) && (events & ULOOP_WRITE))
99 uloop_fd_add(sock, ULOOP_READ | ULOOP_EDGE_TRIGGER);
100
101 retry:
102 if (!sock->eof && cl->pending_msg_offset < (int) sizeof(cl->hdrbuf)) {
103 int offset = cl->pending_msg_offset;
104 int bytes;
105
106 *pfd = -1;
107
108 iov.iov_base = ((char *) &cl->hdrbuf) + offset;
109 iov.iov_len = sizeof(cl->hdrbuf) - offset;
110
111 if (cl->pending_msg_fd < 0) {
112 msghdr.msg_control = fd_buf;
113 msghdr.msg_controllen = cmsg->cmsg_len;
114 } else {
115 msghdr.msg_control = NULL;
116 msghdr.msg_controllen = 0;
117 }
118
119 bytes = recvmsg(sock->fd, &msghdr, 0);
120 if (bytes < 0)
121 goto out;
122
123 if (*pfd >= 0)
124 cl->pending_msg_fd = *pfd;
125
126 cl->pending_msg_offset += bytes;
127 if (cl->pending_msg_offset < (int) sizeof(cl->hdrbuf))
128 goto out;
129
130 if (blob_pad_len(&cl->hdrbuf.data) > UBUS_MAX_MSGLEN)
131 goto disconnect;
132
133 cl->pending_msg = ubus_msg_new(NULL, blob_raw_len(&cl->hdrbuf.data), false);
134 if (!cl->pending_msg)
135 goto disconnect;
136
137 cl->hdrbuf.hdr.seq = be16_to_cpu(cl->hdrbuf.hdr.seq);
138 cl->hdrbuf.hdr.peer = be32_to_cpu(cl->hdrbuf.hdr.peer);
139
140 memcpy(&cl->pending_msg->hdr, &cl->hdrbuf.hdr, sizeof(cl->hdrbuf.hdr));
141 memcpy(cl->pending_msg->data, &cl->hdrbuf.data, sizeof(cl->hdrbuf.data));
142 }
143
144 ub = cl->pending_msg;
145 if (ub) {
146 int offset = cl->pending_msg_offset - sizeof(ub->hdr);
147 int len = blob_raw_len(ub->data) - offset;
148 int bytes = 0;
149
150 if (len > 0) {
151 bytes = read(sock->fd, (char *) ub->data + offset, len);
152 if (bytes <= 0)
153 goto out;
154 }
155
156 if (bytes < len) {
157 cl->pending_msg_offset += bytes;
158 goto out;
159 }
160
161 /* accept message */
162 ub->fd = cl->pending_msg_fd;
163 cl->pending_msg_fd = -1;
164 cl->pending_msg_offset = 0;
165 cl->pending_msg = NULL;
166 ubusd_monitor_message(cl, ub, false);
167 ubusd_proto_receive_message(cl, ub);
168 goto retry;
169 }
170
171 out:
172 if (!sock->eof || ubus_msg_head(cl))
173 return;
174
175 disconnect:
176 handle_client_disconnect(cl);
177 }
178
179 static bool get_next_connection(int fd)
180 {
181 struct ubus_client *cl;
182 int client_fd;
183
184 client_fd = accept(fd, NULL, 0);
185 if (client_fd < 0) {
186 switch (errno) {
187 case ECONNABORTED:
188 case EINTR:
189 return true;
190 default:
191 return false;
192 }
193 }
194
195 cl = ubusd_proto_new_client(client_fd, client_cb);
196 if (cl)
197 uloop_fd_add(&cl->sock, ULOOP_READ | ULOOP_EDGE_TRIGGER);
198 else
199 close(client_fd);
200
201 return true;
202 }
203
204 static void server_cb(struct uloop_fd *fd, unsigned int events)
205 {
206 bool next;
207
208 do {
209 next = get_next_connection(fd->fd);
210 } while (next);
211 }
212
213 static struct uloop_fd server_fd = {
214 .cb = server_cb,
215 };
216
217 static int usage(const char *progname)
218 {
219 fprintf(stderr, "Usage: %s [<options>]\n"
220 "Options: \n"
221 " -A <path>: Set the path to ACL files\n"
222 " -s <socket>: Set the unix domain socket to listen on\n"
223 "\n", progname);
224 return 1;
225 }
226
227 static void sighup_handler(int sig)
228 {
229 ubusd_acl_load();
230 }
231
232 int main(int argc, char **argv)
233 {
234 const char *ubus_socket = UBUS_UNIX_SOCKET;
235 int ret = 0;
236 int ch;
237
238 signal(SIGPIPE, SIG_IGN);
239 signal(SIGHUP, sighup_handler);
240
241 openlog("ubusd", LOG_PID, LOG_DAEMON);
242 uloop_init();
243
244 while ((ch = getopt(argc, argv, "A:s:")) != -1) {
245 switch (ch) {
246 case 's':
247 ubus_socket = optarg;
248 break;
249 case 'A':
250 ubusd_acl_dir = optarg;
251 break;
252 default:
253 return usage(argv[0]);
254 }
255 }
256
257 unlink(ubus_socket);
258 umask(0111);
259 server_fd.fd = usock(USOCK_UNIX | USOCK_SERVER | USOCK_NONBLOCK, ubus_socket, NULL);
260 if (server_fd.fd < 0) {
261 perror("usock");
262 ret = -1;
263 goto out;
264 }
265 uloop_fd_add(&server_fd, ULOOP_READ | ULOOP_EDGE_TRIGGER);
266 ubusd_acl_load();
267
268 uloop_run();
269 unlink(ubus_socket);
270
271 out:
272 uloop_done();
273 return ret;
274 }