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