cmake: Fix find_library for ubusd and examples/server
[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 struct ubus_msghdr hdr;
114
115 hdr.version = ub->hdr.version;
116 hdr.type = ub->hdr.type;
117 hdr.seq = cpu_to_be16(ub->hdr.seq);
118 hdr.peer = cpu_to_be32(ub->hdr.peer);
119
120 iov[0].iov_base = ((char *) &hdr) + offset;
121 iov[0].iov_len = sizeof(hdr) - offset;
122 iov[1].iov_base = (char *) ub->data;
123 iov[1].iov_len = ub->len;
124
125 return sendmsg(fd, &msghdr, 0);
126 } else {
127 offset -= sizeof(ub->hdr);
128 return write(fd, ((char *) ub->data) + offset, ub->len - offset);
129 }
130 }
131
132 static void ubus_msg_enqueue(struct ubus_client *cl, struct ubus_msg_buf *ub)
133 {
134 if (cl->tx_queue[cl->txq_tail])
135 return;
136
137 cl->tx_queue[cl->txq_tail] = ubus_msg_ref(ub);
138 cl->txq_tail = (cl->txq_tail + 1) % ARRAY_SIZE(cl->tx_queue);
139 }
140
141 /* takes the msgbuf reference */
142 void ubus_msg_send(struct ubus_client *cl, struct ubus_msg_buf *ub, bool free)
143 {
144 int written;
145
146 if (ub->hdr.type != UBUS_MSG_MONITOR)
147 ubusd_monitor_message(cl, ub, true);
148
149 if (!cl->tx_queue[cl->txq_cur]) {
150 written = ubus_msg_writev(cl->sock.fd, ub, 0);
151 if (written >= ub->len + sizeof(ub->hdr))
152 goto out;
153
154 if (written < 0)
155 written = 0;
156
157 cl->txq_ofs = written;
158
159 /* get an event once we can write to the socket again */
160 uloop_fd_add(&cl->sock, ULOOP_READ | ULOOP_WRITE | ULOOP_EDGE_TRIGGER);
161 }
162 ubus_msg_enqueue(cl, ub);
163
164 out:
165 if (free)
166 ubus_msg_free(ub);
167 }
168
169 static struct ubus_msg_buf *ubus_msg_head(struct ubus_client *cl)
170 {
171 return cl->tx_queue[cl->txq_cur];
172 }
173
174 static void ubus_msg_dequeue(struct ubus_client *cl)
175 {
176 struct ubus_msg_buf *ub = ubus_msg_head(cl);
177
178 if (!ub)
179 return;
180
181 ubus_msg_free(ub);
182 cl->txq_ofs = 0;
183 cl->tx_queue[cl->txq_cur] = NULL;
184 cl->txq_cur = (cl->txq_cur + 1) % ARRAY_SIZE(cl->tx_queue);
185 }
186
187 static void handle_client_disconnect(struct ubus_client *cl)
188 {
189 while (ubus_msg_head(cl))
190 ubus_msg_dequeue(cl);
191
192 ubusd_monitor_disconnect(cl);
193 ubusd_proto_free_client(cl);
194 if (cl->pending_msg_fd >= 0)
195 close(cl->pending_msg_fd);
196 uloop_fd_delete(&cl->sock);
197 close(cl->sock.fd);
198 free(cl);
199 }
200
201 static void client_cb(struct uloop_fd *sock, unsigned int events)
202 {
203 struct ubus_client *cl = container_of(sock, struct ubus_client, sock);
204 struct ubus_msg_buf *ub;
205 static struct iovec iov;
206 static struct {
207 struct cmsghdr h;
208 int fd;
209 } fd_buf = {
210 .h = {
211 .cmsg_type = SCM_RIGHTS,
212 .cmsg_level = SOL_SOCKET,
213 .cmsg_len = sizeof(fd_buf),
214 }
215 };
216 struct msghdr msghdr = {
217 .msg_iov = &iov,
218 .msg_iovlen = 1,
219 };
220
221 /* first try to tx more pending data */
222 while ((ub = ubus_msg_head(cl))) {
223 int written;
224
225 written = ubus_msg_writev(sock->fd, ub, cl->txq_ofs);
226 if (written < 0) {
227 switch(errno) {
228 case EINTR:
229 case EAGAIN:
230 break;
231 default:
232 goto disconnect;
233 }
234 break;
235 }
236
237 cl->txq_ofs += written;
238 if (cl->txq_ofs < ub->len + sizeof(ub->hdr))
239 break;
240
241 ubus_msg_dequeue(cl);
242 }
243
244 /* prevent further ULOOP_WRITE events if we don't have data
245 * to send anymore */
246 if (!ubus_msg_head(cl) && (events & ULOOP_WRITE))
247 uloop_fd_add(sock, ULOOP_READ | ULOOP_EDGE_TRIGGER);
248
249 retry:
250 if (!sock->eof && cl->pending_msg_offset < sizeof(cl->hdrbuf)) {
251 int offset = cl->pending_msg_offset;
252 int bytes;
253
254 fd_buf.fd = -1;
255
256 iov.iov_base = ((char *) &cl->hdrbuf) + offset;
257 iov.iov_len = sizeof(cl->hdrbuf) - offset;
258
259 if (cl->pending_msg_fd < 0) {
260 msghdr.msg_control = &fd_buf;
261 msghdr.msg_controllen = sizeof(fd_buf);
262 } else {
263 msghdr.msg_control = NULL;
264 msghdr.msg_controllen = 0;
265 }
266
267 bytes = recvmsg(sock->fd, &msghdr, 0);
268 if (bytes < 0)
269 goto out;
270
271 if (fd_buf.fd >= 0)
272 cl->pending_msg_fd = fd_buf.fd;
273
274 cl->pending_msg_offset += bytes;
275 if (cl->pending_msg_offset < sizeof(cl->hdrbuf))
276 goto out;
277
278 if (blob_pad_len(&cl->hdrbuf.data) > UBUS_MAX_MSGLEN)
279 goto disconnect;
280
281 cl->pending_msg = ubus_msg_new(NULL, blob_raw_len(&cl->hdrbuf.data), false);
282 if (!cl->pending_msg)
283 goto disconnect;
284
285 cl->hdrbuf.hdr.seq = be16_to_cpu(cl->hdrbuf.hdr.seq);
286 cl->hdrbuf.hdr.peer = be32_to_cpu(cl->hdrbuf.hdr.peer);
287
288 memcpy(&cl->pending_msg->hdr, &cl->hdrbuf.hdr, sizeof(cl->hdrbuf.hdr));
289 memcpy(cl->pending_msg->data, &cl->hdrbuf.data, sizeof(cl->hdrbuf.data));
290 }
291
292 ub = cl->pending_msg;
293 if (ub) {
294 int offset = cl->pending_msg_offset - sizeof(ub->hdr);
295 int len = blob_raw_len(ub->data) - offset;
296 int bytes = 0;
297
298 if (len > 0) {
299 bytes = read(sock->fd, (char *) ub->data + offset, len);
300 if (bytes <= 0)
301 goto out;
302 }
303
304 if (bytes < len) {
305 cl->pending_msg_offset += bytes;
306 goto out;
307 }
308
309 /* accept message */
310 ub->fd = cl->pending_msg_fd;
311 cl->pending_msg_fd = -1;
312 cl->pending_msg_offset = 0;
313 cl->pending_msg = NULL;
314 ubusd_monitor_message(cl, ub, false);
315 ubusd_proto_receive_message(cl, ub);
316 goto retry;
317 }
318
319 out:
320 if (!sock->eof || ubus_msg_head(cl))
321 return;
322
323 disconnect:
324 handle_client_disconnect(cl);
325 }
326
327 static bool get_next_connection(int fd)
328 {
329 struct ubus_client *cl;
330 int client_fd;
331
332 client_fd = accept(fd, NULL, 0);
333 if (client_fd < 0) {
334 switch (errno) {
335 case ECONNABORTED:
336 case EINTR:
337 return true;
338 default:
339 return false;
340 }
341 }
342
343 cl = ubusd_proto_new_client(client_fd, client_cb);
344 if (cl)
345 uloop_fd_add(&cl->sock, ULOOP_READ | ULOOP_EDGE_TRIGGER);
346 else
347 close(client_fd);
348
349 return true;
350 }
351
352 static void server_cb(struct uloop_fd *fd, unsigned int events)
353 {
354 bool next;
355
356 do {
357 next = get_next_connection(fd->fd);
358 } while (next);
359 }
360
361 static struct uloop_fd server_fd = {
362 .cb = server_cb,
363 };
364
365 static int usage(const char *progname)
366 {
367 fprintf(stderr, "Usage: %s [<options>]\n"
368 "Options: \n"
369 " -A <path>: Set the path to ACL files\n"
370 " -s <socket>: Set the unix domain socket to listen on\n"
371 "\n", progname);
372 return 1;
373 }
374
375 static void sighup_handler(int sig)
376 {
377 ubusd_acl_load();
378 }
379
380 int main(int argc, char **argv)
381 {
382 const char *ubus_socket = UBUS_UNIX_SOCKET;
383 int ret = 0;
384 int ch;
385
386 signal(SIGPIPE, SIG_IGN);
387 signal(SIGHUP, sighup_handler);
388
389 openlog("ubusd", LOG_PID, LOG_DAEMON);
390 uloop_init();
391
392 while ((ch = getopt(argc, argv, "A:s:")) != -1) {
393 switch (ch) {
394 case 's':
395 ubus_socket = optarg;
396 break;
397 case 'A':
398 ubusd_acl_dir = optarg;
399 break;
400 default:
401 return usage(argv[0]);
402 }
403 }
404
405 unlink(ubus_socket);
406 umask(0111);
407 server_fd.fd = usock(USOCK_UNIX | USOCK_SERVER | USOCK_NONBLOCK, ubus_socket, NULL);
408 if (server_fd.fd < 0) {
409 perror("usock");
410 ret = -1;
411 goto out;
412 }
413 uloop_fd_add(&server_fd, ULOOP_READ | ULOOP_EDGE_TRIGGER);
414 ubusd_acl_load();
415
416 uloop_run();
417 unlink(ubus_socket);
418
419 out:
420 uloop_done();
421 return ret;
422 }