libubus: reset ctx->sock.eof to fix reconnect issues
[project/ubus.git] / ubusd.c
1 /*
2 * Copyright (C) 2011-2014 Felix Fietkau <nbd@openwrt.org>
3 *
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
7 *
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.
12 */
13
14 #include <sys/socket.h>
15 #include <sys/stat.h>
16 #include <sys/uio.h>
17 #ifdef FreeBSD
18 #include <sys/param.h>
19 #endif
20 #include <syslog.h>
21 #include <signal.h>
22 #include <stdio.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25
26 #include <libubox/blob.h>
27 #include <libubox/uloop.h>
28 #include <libubox/usock.h>
29 #include <libubox/list.h>
30
31 #include "ubusd.h"
32
33 static struct ubus_msg_buf *ubus_msg_ref(struct ubus_msg_buf *ub)
34 {
35 if (ub->refcount == ~0)
36 return ubus_msg_new(ub->data, ub->len, false);
37
38 ub->refcount++;
39 return ub;
40 }
41
42 struct ubus_msg_buf *ubus_msg_new(void *data, int len, bool shared)
43 {
44 struct ubus_msg_buf *ub;
45 int buflen = sizeof(*ub);
46
47 if (!shared)
48 buflen += len;
49
50 ub = calloc(1, buflen);
51 if (!ub)
52 return NULL;
53
54 ub->fd = -1;
55
56 if (shared) {
57 ub->refcount = ~0;
58 ub->data = data;
59 } else {
60 ub->refcount = 1;
61 ub->data = (void *) (ub + 1);
62 if (data)
63 memcpy(ub + 1, data, len);
64 }
65
66 ub->len = len;
67 return ub;
68 }
69
70 void ubus_msg_free(struct ubus_msg_buf *ub)
71 {
72 switch (ub->refcount) {
73 case 1:
74 case ~0:
75 if (ub->fd >= 0)
76 close(ub->fd);
77
78 free(ub);
79 break;
80 default:
81 ub->refcount--;
82 break;
83 }
84 }
85
86 static int ubus_msg_writev(int fd, struct ubus_msg_buf *ub, int offset)
87 {
88 static struct iovec iov[2];
89 static struct {
90 struct cmsghdr h;
91 int fd;
92 } fd_buf = {
93 .h = {
94 .cmsg_len = sizeof(fd_buf),
95 .cmsg_level = SOL_SOCKET,
96 .cmsg_type = SCM_RIGHTS,
97 },
98 };
99 struct msghdr msghdr = {
100 .msg_iov = iov,
101 .msg_iovlen = ARRAY_SIZE(iov),
102 .msg_control = &fd_buf,
103 .msg_controllen = sizeof(fd_buf),
104 };
105
106 fd_buf.fd = ub->fd;
107 if (ub->fd < 0) {
108 msghdr.msg_control = NULL;
109 msghdr.msg_controllen = 0;
110 }
111
112 if (offset < sizeof(ub->hdr)) {
113 struct ubus_msghdr hdr;
114
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);
119
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;
124
125 return sendmsg(fd, &msghdr, 0);
126 } else {
127 offset -= sizeof(ub->hdr);
128 return write(fd, ((char *) ub->data) + offset, ub->len - offset);
129 }
130 }
131
132 static void ubus_msg_enqueue(struct ubus_client *cl, struct ubus_msg_buf *ub)
133 {
134 if (cl->tx_queue[cl->txq_tail])
135 return;
136
137 cl->tx_queue[cl->txq_tail] = ubus_msg_ref(ub);
138 cl->txq_tail = (cl->txq_tail + 1) % ARRAY_SIZE(cl->tx_queue);
139 }
140
141 /* takes the msgbuf reference */
142 void ubus_msg_send(struct ubus_client *cl, struct ubus_msg_buf *ub, bool free)
143 {
144 int written;
145
146 if (ub->hdr.type != UBUS_MSG_MONITOR)
147 ubusd_monitor_message(cl, ub, true);
148
149 if (!cl->tx_queue[cl->txq_cur]) {
150 written = ubus_msg_writev(cl->sock.fd, ub, 0);
151
152 if (written < 0)
153 written = 0;
154
155 if (written >= ub->len + sizeof(ub->hdr))
156 goto out;
157
158 cl->txq_ofs = written;
159
160 /* get an event once we can write to the socket again */
161 uloop_fd_add(&cl->sock, ULOOP_READ | ULOOP_WRITE | ULOOP_EDGE_TRIGGER);
162 }
163 ubus_msg_enqueue(cl, ub);
164
165 out:
166 if (free)
167 ubus_msg_free(ub);
168 }
169
170 static struct ubus_msg_buf *ubus_msg_head(struct ubus_client *cl)
171 {
172 return cl->tx_queue[cl->txq_cur];
173 }
174
175 static void ubus_msg_dequeue(struct ubus_client *cl)
176 {
177 struct ubus_msg_buf *ub = ubus_msg_head(cl);
178
179 if (!ub)
180 return;
181
182 ubus_msg_free(ub);
183 cl->txq_ofs = 0;
184 cl->tx_queue[cl->txq_cur] = NULL;
185 cl->txq_cur = (cl->txq_cur + 1) % ARRAY_SIZE(cl->tx_queue);
186 }
187
188 static void handle_client_disconnect(struct ubus_client *cl)
189 {
190 while (ubus_msg_head(cl))
191 ubus_msg_dequeue(cl);
192
193 ubusd_monitor_disconnect(cl);
194 ubusd_proto_free_client(cl);
195 if (cl->pending_msg_fd >= 0)
196 close(cl->pending_msg_fd);
197 uloop_fd_delete(&cl->sock);
198 close(cl->sock.fd);
199 free(cl);
200 }
201
202 static void client_cb(struct uloop_fd *sock, unsigned int events)
203 {
204 struct ubus_client *cl = container_of(sock, struct ubus_client, sock);
205 struct ubus_msg_buf *ub;
206 static struct iovec iov;
207 static struct {
208 struct cmsghdr h;
209 int fd;
210 } fd_buf = {
211 .h = {
212 .cmsg_type = SCM_RIGHTS,
213 .cmsg_level = SOL_SOCKET,
214 .cmsg_len = sizeof(fd_buf),
215 }
216 };
217 struct msghdr msghdr = {
218 .msg_iov = &iov,
219 .msg_iovlen = 1,
220 };
221
222 /* first try to tx more pending data */
223 while ((ub = ubus_msg_head(cl))) {
224 int written;
225
226 written = ubus_msg_writev(sock->fd, ub, cl->txq_ofs);
227 if (written < 0) {
228 switch(errno) {
229 case EINTR:
230 case EAGAIN:
231 break;
232 default:
233 goto disconnect;
234 }
235 break;
236 }
237
238 cl->txq_ofs += written;
239 if (cl->txq_ofs < ub->len + sizeof(ub->hdr))
240 break;
241
242 ubus_msg_dequeue(cl);
243 }
244
245 /* prevent further ULOOP_WRITE events if we don't have data
246 * to send anymore */
247 if (!ubus_msg_head(cl) && (events & ULOOP_WRITE))
248 uloop_fd_add(sock, ULOOP_READ | ULOOP_EDGE_TRIGGER);
249
250 retry:
251 if (!sock->eof && cl->pending_msg_offset < sizeof(cl->hdrbuf)) {
252 int offset = cl->pending_msg_offset;
253 int bytes;
254
255 fd_buf.fd = -1;
256
257 iov.iov_base = ((char *) &cl->hdrbuf) + offset;
258 iov.iov_len = sizeof(cl->hdrbuf) - offset;
259
260 if (cl->pending_msg_fd < 0) {
261 msghdr.msg_control = &fd_buf;
262 msghdr.msg_controllen = sizeof(fd_buf);
263 } else {
264 msghdr.msg_control = NULL;
265 msghdr.msg_controllen = 0;
266 }
267
268 bytes = recvmsg(sock->fd, &msghdr, 0);
269 if (bytes < 0)
270 goto out;
271
272 if (fd_buf.fd >= 0)
273 cl->pending_msg_fd = fd_buf.fd;
274
275 cl->pending_msg_offset += bytes;
276 if (cl->pending_msg_offset < sizeof(cl->hdrbuf))
277 goto out;
278
279 if (blob_pad_len(&cl->hdrbuf.data) > UBUS_MAX_MSGLEN)
280 goto disconnect;
281
282 cl->pending_msg = ubus_msg_new(NULL, blob_raw_len(&cl->hdrbuf.data), false);
283 if (!cl->pending_msg)
284 goto disconnect;
285
286 cl->hdrbuf.hdr.seq = be16_to_cpu(cl->hdrbuf.hdr.seq);
287 cl->hdrbuf.hdr.peer = be32_to_cpu(cl->hdrbuf.hdr.peer);
288
289 memcpy(&cl->pending_msg->hdr, &cl->hdrbuf.hdr, sizeof(cl->hdrbuf.hdr));
290 memcpy(cl->pending_msg->data, &cl->hdrbuf.data, sizeof(cl->hdrbuf.data));
291 }
292
293 ub = cl->pending_msg;
294 if (ub) {
295 int offset = cl->pending_msg_offset - sizeof(ub->hdr);
296 int len = blob_raw_len(ub->data) - offset;
297 int bytes = 0;
298
299 if (len > 0) {
300 bytes = read(sock->fd, (char *) ub->data + offset, len);
301 if (bytes <= 0)
302 goto out;
303 }
304
305 if (bytes < len) {
306 cl->pending_msg_offset += bytes;
307 goto out;
308 }
309
310 /* accept message */
311 ub->fd = cl->pending_msg_fd;
312 cl->pending_msg_fd = -1;
313 cl->pending_msg_offset = 0;
314 cl->pending_msg = NULL;
315 ubusd_monitor_message(cl, ub, false);
316 ubusd_proto_receive_message(cl, ub);
317 goto retry;
318 }
319
320 out:
321 if (!sock->eof || ubus_msg_head(cl))
322 return;
323
324 disconnect:
325 handle_client_disconnect(cl);
326 }
327
328 static bool get_next_connection(int fd)
329 {
330 struct ubus_client *cl;
331 int client_fd;
332
333 client_fd = accept(fd, NULL, 0);
334 if (client_fd < 0) {
335 switch (errno) {
336 case ECONNABORTED:
337 case EINTR:
338 return true;
339 default:
340 return false;
341 }
342 }
343
344 cl = ubusd_proto_new_client(client_fd, client_cb);
345 if (cl)
346 uloop_fd_add(&cl->sock, ULOOP_READ | ULOOP_EDGE_TRIGGER);
347 else
348 close(client_fd);
349
350 return true;
351 }
352
353 static void server_cb(struct uloop_fd *fd, unsigned int events)
354 {
355 bool next;
356
357 do {
358 next = get_next_connection(fd->fd);
359 } while (next);
360 }
361
362 static struct uloop_fd server_fd = {
363 .cb = server_cb,
364 };
365
366 static int usage(const char *progname)
367 {
368 fprintf(stderr, "Usage: %s [<options>]\n"
369 "Options: \n"
370 " -A <path>: Set the path to ACL files\n"
371 " -s <socket>: Set the unix domain socket to listen on\n"
372 "\n", progname);
373 return 1;
374 }
375
376 static void sighup_handler(int sig)
377 {
378 ubusd_acl_load();
379 }
380
381 int main(int argc, char **argv)
382 {
383 const char *ubus_socket = UBUS_UNIX_SOCKET;
384 int ret = 0;
385 int ch;
386
387 signal(SIGPIPE, SIG_IGN);
388 signal(SIGHUP, sighup_handler);
389
390 openlog("ubusd", LOG_PID, LOG_DAEMON);
391 uloop_init();
392
393 while ((ch = getopt(argc, argv, "A:s:")) != -1) {
394 switch (ch) {
395 case 's':
396 ubus_socket = optarg;
397 break;
398 case 'A':
399 ubusd_acl_dir = optarg;
400 break;
401 default:
402 return usage(argv[0]);
403 }
404 }
405
406 unlink(ubus_socket);
407 umask(0111);
408 server_fd.fd = usock(USOCK_UNIX | USOCK_SERVER | USOCK_NONBLOCK, ubus_socket, NULL);
409 if (server_fd.fd < 0) {
410 perror("usock");
411 ret = -1;
412 goto out;
413 }
414 uloop_fd_add(&server_fd, ULOOP_READ | ULOOP_EDGE_TRIGGER);
415 ubusd_acl_load();
416
417 uloop_run();
418 unlink(ubus_socket);
419
420 out:
421 uloop_done();
422 return ret;
423 }