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