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