ubusd: fix offset calculation (based on patch by Yang Chao)
[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                 iov[0].iov_base = ((char *) &ub->hdr) + offset;
114                 iov[0].iov_len = sizeof(ub->hdr) - offset;
115                 iov[1].iov_base = (char *) ub->data;
116                 iov[1].iov_len = ub->len;
117
118                 return sendmsg(fd, &msghdr, 0);
119         } else {
120                 offset -= sizeof(ub->hdr);
121                 return write(fd, ((char *) ub->data) + offset, ub->len - offset);
122         }
123 }
124
125 static void ubus_msg_enqueue(struct ubus_client *cl, struct ubus_msg_buf *ub)
126 {
127         if (cl->tx_queue[cl->txq_tail])
128                 return;
129
130         cl->tx_queue[cl->txq_tail] = ubus_msg_ref(ub);
131         cl->txq_tail = (cl->txq_tail + 1) % ARRAY_SIZE(cl->tx_queue);
132 }
133
134 /* takes the msgbuf reference */
135 void ubus_msg_send(struct ubus_client *cl, struct ubus_msg_buf *ub, bool free)
136 {
137         int written;
138
139         if (!cl->tx_queue[cl->txq_cur]) {
140                 written = ubus_msg_writev(cl->sock.fd, ub, 0);
141                 if (written >= ub->len + sizeof(ub->hdr))
142                         goto out;
143
144                 if (written < 0)
145                         written = 0;
146
147                 cl->txq_ofs = written;
148
149                 /* get an event once we can write to the socket again */
150                 uloop_fd_add(&cl->sock, ULOOP_READ | ULOOP_WRITE | ULOOP_EDGE_TRIGGER);
151         }
152         ubus_msg_enqueue(cl, ub);
153
154 out:
155         if (free)
156                 ubus_msg_free(ub);
157 }
158
159 static struct ubus_msg_buf *ubus_msg_head(struct ubus_client *cl)
160 {
161         return cl->tx_queue[cl->txq_cur];
162 }
163
164 static void ubus_msg_dequeue(struct ubus_client *cl)
165 {
166         struct ubus_msg_buf *ub = ubus_msg_head(cl);
167
168         if (!ub)
169                 return;
170
171         ubus_msg_free(ub);
172         cl->txq_ofs = 0;
173         cl->tx_queue[cl->txq_cur] = NULL;
174         cl->txq_cur = (cl->txq_cur + 1) % ARRAY_SIZE(cl->tx_queue);
175 }
176
177 static void handle_client_disconnect(struct ubus_client *cl)
178 {
179         while (ubus_msg_head(cl))
180                 ubus_msg_dequeue(cl);
181
182         ubusd_proto_free_client(cl);
183         if (cl->pending_msg_fd >= 0)
184                 close(cl->pending_msg_fd);
185         uloop_fd_delete(&cl->sock);
186         close(cl->sock.fd);
187         free(cl);
188 }
189
190 static void client_cb(struct uloop_fd *sock, unsigned int events)
191 {
192         struct ubus_client *cl = container_of(sock, struct ubus_client, sock);
193         struct ubus_msg_buf *ub;
194         static struct iovec iov;
195         static struct {
196                 struct cmsghdr h;
197                 int fd;
198         } fd_buf = {
199                 .h = {
200                         .cmsg_type = SCM_RIGHTS,
201                         .cmsg_level = SOL_SOCKET,
202                         .cmsg_len = sizeof(fd_buf),
203                 }
204         };
205         struct msghdr msghdr = {
206                 .msg_iov = &iov,
207                 .msg_iovlen = 1,
208         };
209
210         /* first try to tx more pending data */
211         while ((ub = ubus_msg_head(cl))) {
212                 int written;
213
214                 written = ubus_msg_writev(sock->fd, ub, cl->txq_ofs);
215                 if (written < 0) {
216                         switch(errno) {
217                         case EINTR:
218                         case EAGAIN:
219                                 break;
220                         default:
221                                 goto disconnect;
222                         }
223                         break;
224                 }
225
226                 cl->txq_ofs += written;
227                 if (cl->txq_ofs < ub->len + sizeof(ub->hdr))
228                         break;
229
230                 ubus_msg_dequeue(cl);
231         }
232
233         /* prevent further ULOOP_WRITE events if we don't have data
234          * to send anymore */
235         if (!ubus_msg_head(cl) && (events & ULOOP_WRITE))
236                 uloop_fd_add(sock, ULOOP_READ | ULOOP_EDGE_TRIGGER);
237
238 retry:
239         if (!sock->eof && cl->pending_msg_offset < sizeof(cl->hdrbuf)) {
240                 int offset = cl->pending_msg_offset;
241                 int bytes;
242
243                 fd_buf.fd = -1;
244
245                 iov.iov_base = ((char *) &cl->hdrbuf) + offset;
246                 iov.iov_len = sizeof(cl->hdrbuf) - offset;
247
248                 if (cl->pending_msg_fd < 0) {
249                         msghdr.msg_control = &fd_buf;
250                         msghdr.msg_controllen = sizeof(fd_buf);
251                 } else {
252                         msghdr.msg_control = NULL;
253                         msghdr.msg_controllen = 0;
254                 }
255
256                 bytes = recvmsg(sock->fd, &msghdr, 0);
257                 if (bytes < 0)
258                         goto out;
259
260                 if (fd_buf.fd >= 0)
261                         cl->pending_msg_fd = fd_buf.fd;
262
263                 cl->pending_msg_offset += bytes;
264                 if (cl->pending_msg_offset < sizeof(cl->hdrbuf))
265                         goto out;
266
267                 if (blob_pad_len(&cl->hdrbuf.data) > UBUS_MAX_MSGLEN)
268                         goto disconnect;
269
270                 cl->pending_msg = ubus_msg_new(NULL, blob_raw_len(&cl->hdrbuf.data), false);
271                 if (!cl->pending_msg)
272                         goto disconnect;
273
274                 memcpy(&cl->pending_msg->hdr, &cl->hdrbuf.hdr, sizeof(cl->hdrbuf.hdr));
275                 memcpy(cl->pending_msg->data, &cl->hdrbuf.data, sizeof(cl->hdrbuf.data));
276         }
277
278         ub = cl->pending_msg;
279         if (ub) {
280                 int offset = cl->pending_msg_offset - sizeof(ub->hdr);
281                 int len = blob_raw_len(ub->data) - offset;
282                 int bytes = 0;
283
284                 if (len > 0) {
285                         bytes = read(sock->fd, (char *) ub->data + offset, len);
286                         if (bytes <= 0)
287                                 goto out;
288                 }
289
290                 if (bytes < len) {
291                         cl->pending_msg_offset += bytes;
292                         goto out;
293                 }
294
295                 /* accept message */
296                 ub->fd = cl->pending_msg_fd;
297                 cl->pending_msg_fd = -1;
298                 cl->pending_msg_offset = 0;
299                 cl->pending_msg = NULL;
300                 ubusd_proto_receive_message(cl, ub);
301                 goto retry;
302         }
303
304 out:
305         if (!sock->eof || ubus_msg_head(cl))
306                 return;
307
308 disconnect:
309         handle_client_disconnect(cl);
310 }
311
312 static bool get_next_connection(int fd)
313 {
314         struct ubus_client *cl;
315         int client_fd;
316
317         client_fd = accept(fd, NULL, 0);
318         if (client_fd < 0) {
319                 switch (errno) {
320                 case ECONNABORTED:
321                 case EINTR:
322                         return true;
323                 default:
324                         return false;
325                 }
326         }
327
328         cl = ubusd_proto_new_client(client_fd, client_cb);
329         if (cl)
330                 uloop_fd_add(&cl->sock, ULOOP_READ | ULOOP_EDGE_TRIGGER);
331         else
332                 close(client_fd);
333
334         return true;
335 }
336
337 static void server_cb(struct uloop_fd *fd, unsigned int events)
338 {
339         bool next;
340
341         do {
342                 next = get_next_connection(fd->fd);
343         } while (next);
344 }
345
346 static struct uloop_fd server_fd = {
347         .cb = server_cb,
348 };
349
350 static int usage(const char *progname)
351 {
352         fprintf(stderr, "Usage: %s [<options>]\n"
353                 "Options: \n"
354                 "  -s <socket>:         Set the unix domain socket to listen on\n"
355                 "\n", progname);
356         return 1;
357 }
358
359 static void sighup_handler(int sig)
360 {
361         ubusd_acl_load();
362 }
363
364 int main(int argc, char **argv)
365 {
366         const char *ubus_socket = UBUS_UNIX_SOCKET;
367         int ret = 0;
368         int ch;
369
370         signal(SIGPIPE, SIG_IGN);
371         signal(SIGHUP, sighup_handler);
372
373         openlog("ubusd", LOG_PID, LOG_DAEMON);
374         uloop_init();
375
376         while ((ch = getopt(argc, argv, "s:")) != -1) {
377                 switch (ch) {
378                 case 's':
379                         ubus_socket = optarg;
380                         break;
381                 default:
382                         return usage(argv[0]);
383                 }
384         }
385
386         unlink(ubus_socket);
387         umask(0111);
388         server_fd.fd = usock(USOCK_UNIX | USOCK_SERVER | USOCK_NONBLOCK, ubus_socket, NULL);
389         if (server_fd.fd < 0) {
390                 perror("usock");
391                 ret = -1;
392                 goto out;
393         }
394         uloop_fd_add(&server_fd, ULOOP_READ | ULOOP_EDGE_TRIGGER);
395         ubusd_acl_load();
396
397         uloop_run();
398         unlink(ubus_socket);
399
400 out:
401         uloop_done();
402         return ret;
403 }