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