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