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