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