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