libubus: rework buffer allocation code
[project/ubus.git] / libubus-io.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/types.h>
15 #include <sys/uio.h>
16 #include <sys/socket.h>
17
18 #include <unistd.h>
19 #include <fcntl.h>
20 #include <poll.h>
21
22 #include <libubox/usock.h>
23 #include <libubox/blob.h>
24 #include <libubox/blobmsg.h>
25
26 #include "libubus.h"
27 #include "libubus-internal.h"
28
29 #define STATIC_IOV(_var) { .iov_base = (char *) &(_var), .iov_len = sizeof(_var) }
30
31 #define UBUS_MSGBUF_REDUCTION_INTERVAL 16
32
33 static const struct blob_attr_info ubus_policy[UBUS_ATTR_MAX] = {
34 [UBUS_ATTR_STATUS] = { .type = BLOB_ATTR_INT32 },
35 [UBUS_ATTR_OBJID] = { .type = BLOB_ATTR_INT32 },
36 [UBUS_ATTR_OBJPATH] = { .type = BLOB_ATTR_STRING },
37 [UBUS_ATTR_METHOD] = { .type = BLOB_ATTR_STRING },
38 [UBUS_ATTR_ACTIVE] = { .type = BLOB_ATTR_INT8 },
39 [UBUS_ATTR_NO_REPLY] = { .type = BLOB_ATTR_INT8 },
40 [UBUS_ATTR_SUBSCRIBERS] = { .type = BLOB_ATTR_NESTED },
41 };
42
43 static struct blob_attr *attrbuf[UBUS_ATTR_MAX];
44
45 __hidden struct blob_attr **ubus_parse_msg(struct blob_attr *msg)
46 {
47 blob_parse(msg, attrbuf, ubus_policy, UBUS_ATTR_MAX);
48 return attrbuf;
49 }
50
51 static void wait_data(int fd, bool write)
52 {
53 struct pollfd pfd = { .fd = fd };
54
55 pfd.events = write ? POLLOUT : POLLIN;
56 poll(&pfd, 1, 0);
57 }
58
59 static int writev_retry(int fd, struct iovec *iov, int iov_len, int sock_fd)
60 {
61 static struct {
62 struct cmsghdr h;
63 int fd;
64 } fd_buf = {
65 .h = {
66 .cmsg_len = sizeof(fd_buf),
67 .cmsg_level = SOL_SOCKET,
68 .cmsg_type = SCM_RIGHTS,
69 }
70 };
71 struct msghdr msghdr = {
72 .msg_iov = iov,
73 .msg_iovlen = iov_len,
74 .msg_control = &fd_buf,
75 .msg_controllen = sizeof(fd_buf),
76 };
77 int len = 0;
78
79 do {
80 int cur_len;
81
82 if (sock_fd < 0) {
83 msghdr.msg_control = NULL;
84 msghdr.msg_controllen = 0;
85 } else {
86 fd_buf.fd = sock_fd;
87 }
88
89 cur_len = sendmsg(fd, &msghdr, 0);
90 if (cur_len < 0) {
91 switch(errno) {
92 case EAGAIN:
93 wait_data(fd, true);
94 break;
95 case EINTR:
96 break;
97 default:
98 return -1;
99 }
100 continue;
101 }
102
103 if (len > 0)
104 sock_fd = -1;
105
106 len += cur_len;
107 while (cur_len >= iov->iov_len) {
108 cur_len -= iov->iov_len;
109 iov_len--;
110 iov++;
111 if (!iov_len)
112 return len;
113 }
114 iov->iov_base += cur_len;
115 iov->iov_len -= cur_len;
116 msghdr.msg_iov = iov;
117 msghdr.msg_iovlen = iov_len;
118 } while (1);
119
120 /* Should never reach here */
121 return -1;
122 }
123
124 int __hidden ubus_send_msg(struct ubus_context *ctx, uint32_t seq,
125 struct blob_attr *msg, int cmd, uint32_t peer, int fd)
126 {
127 struct ubus_msghdr hdr;
128 struct iovec iov[2] = {
129 STATIC_IOV(hdr)
130 };
131 int ret;
132
133 hdr.version = 0;
134 hdr.type = cmd;
135 hdr.seq = seq;
136 hdr.peer = peer;
137
138 if (!msg) {
139 blob_buf_init(&b, 0);
140 msg = b.head;
141 }
142
143 iov[1].iov_base = (char *) msg;
144 iov[1].iov_len = blob_raw_len(msg);
145
146 ret = writev_retry(ctx->sock.fd, iov, ARRAY_SIZE(iov), fd);
147 if (ret < 0)
148 ctx->sock.eof = true;
149
150 if (fd >= 0)
151 close(fd);
152
153 return ret;
154 }
155
156 static int recv_retry(int fd, struct iovec *iov, bool wait, int *recv_fd)
157 {
158 int bytes, total = 0;
159 static struct {
160 struct cmsghdr h;
161 int fd;
162 } fd_buf = {
163 .h = {
164 .cmsg_type = SCM_RIGHTS,
165 .cmsg_level = SOL_SOCKET,
166 .cmsg_len = sizeof(fd_buf),
167 },
168 };
169 struct msghdr msghdr = {
170 .msg_iov = iov,
171 .msg_iovlen = 1,
172 };
173
174 while (iov->iov_len > 0) {
175 if (wait)
176 wait_data(fd, false);
177
178 if (recv_fd) {
179 msghdr.msg_control = &fd_buf;
180 msghdr.msg_controllen = sizeof(fd_buf);
181 } else {
182 msghdr.msg_control = NULL;
183 msghdr.msg_controllen = 0;
184 }
185
186 fd_buf.fd = -1;
187 bytes = recvmsg(fd, &msghdr, 0);
188 if (!bytes)
189 return -1;
190
191 if (bytes < 0) {
192 bytes = 0;
193 if (uloop_cancelled)
194 return 0;
195 if (errno == EINTR)
196 continue;
197
198 if (errno != EAGAIN)
199 return -1;
200 }
201 if (!wait && !bytes)
202 return 0;
203
204 if (recv_fd)
205 *recv_fd = fd_buf.fd;
206
207 recv_fd = NULL;
208
209 wait = true;
210 iov->iov_len -= bytes;
211 iov->iov_base += bytes;
212 total += bytes;
213 }
214
215 return total;
216 }
217
218 static bool ubus_validate_hdr(struct ubus_msghdr *hdr)
219 {
220 struct blob_attr *data = (struct blob_attr *) (hdr + 1);
221
222 if (hdr->version != 0)
223 return false;
224
225 if (blob_raw_len(data) < sizeof(*data))
226 return false;
227
228 if (blob_pad_len(data) > UBUS_MAX_MSGLEN)
229 return false;
230
231 return true;
232 }
233
234 static bool alloc_msg_buf(struct ubus_context *ctx, int len)
235 {
236 void *ptr;
237 int buf_len = ctx->msgbuf_data_len;
238 int rem;
239
240 if (!ctx->msgbuf.data)
241 buf_len = 0;
242
243 rem = (len % UBUS_MSG_CHUNK_SIZE);
244 if (rem > 0)
245 len += UBUS_MSG_CHUNK_SIZE - rem;
246
247 if (len < buf_len &&
248 ++ctx->msgbuf_reduction_counter > UBUS_MSGBUF_REDUCTION_INTERVAL) {
249 ctx->msgbuf_reduction_counter = 0;
250 buf_len = 0;
251 }
252
253 if (len <= buf_len)
254 return true;
255
256 ptr = realloc(ctx->msgbuf.data, len);
257 if (!ptr)
258 return false;
259
260 ctx->msgbuf.data = ptr;
261 return true;
262 }
263
264 static bool get_next_msg(struct ubus_context *ctx, int *recv_fd)
265 {
266 struct {
267 struct ubus_msghdr hdr;
268 struct blob_attr data;
269 } hdrbuf;
270 struct iovec iov = STATIC_IOV(hdrbuf);
271 int len;
272 int r;
273
274 /* receive header + start attribute */
275 r = recv_retry(ctx->sock.fd, &iov, false, recv_fd);
276 if (r <= 0) {
277 if (r < 0)
278 ctx->sock.eof = true;
279
280 return false;
281 }
282
283 if (!ubus_validate_hdr(&hdrbuf.hdr))
284 return false;
285
286 len = blob_raw_len(&hdrbuf.data);
287 if (!alloc_msg_buf(ctx, len))
288 return false;
289
290 memcpy(&ctx->msgbuf.hdr, &hdrbuf.hdr, sizeof(hdrbuf.hdr));
291 memcpy(ctx->msgbuf.data, &hdrbuf.data, sizeof(hdrbuf.data));
292
293 iov.iov_base = (char *)ctx->msgbuf.data + sizeof(hdrbuf.data);
294 iov.iov_len = blob_len(ctx->msgbuf.data);
295 if (iov.iov_len > 0 && !recv_retry(ctx->sock.fd, &iov, true, NULL))
296 return false;
297
298 return true;
299 }
300
301 void __hidden ubus_handle_data(struct uloop_fd *u, unsigned int events)
302 {
303 struct ubus_context *ctx = container_of(u, struct ubus_context, sock);
304 int recv_fd = -1;
305
306 while (get_next_msg(ctx, &recv_fd)) {
307 ubus_process_msg(ctx, &ctx->msgbuf, recv_fd);
308 if (uloop_cancelled)
309 break;
310 }
311
312 if (u->eof)
313 ctx->connection_lost(ctx);
314 }
315
316 void __hidden ubus_poll_data(struct ubus_context *ctx, int timeout)
317 {
318 struct pollfd pfd = {
319 .fd = ctx->sock.fd,
320 .events = POLLIN | POLLERR,
321 };
322
323 poll(&pfd, 1, timeout);
324 ubus_handle_data(&ctx->sock, ULOOP_READ);
325 }
326
327 static void
328 ubus_refresh_state(struct ubus_context *ctx)
329 {
330 struct ubus_object *obj, *tmp;
331 struct ubus_object **objs;
332 int n, i = 0;
333
334 /* clear all type IDs, they need to be registered again */
335 avl_for_each_element(&ctx->objects, obj, avl)
336 if (obj->type)
337 obj->type->id = 0;
338
339 /* push out all objects again */
340 objs = alloca(ctx->objects.count * sizeof(*objs));
341 avl_remove_all_elements(&ctx->objects, obj, avl, tmp) {
342 objs[i++] = obj;
343 obj->id = 0;
344 }
345
346 for (n = i, i = 0; i < n; i++)
347 ubus_add_object(ctx, objs[i]);
348 }
349
350 int ubus_reconnect(struct ubus_context *ctx, const char *path)
351 {
352 struct {
353 struct ubus_msghdr hdr;
354 struct blob_attr data;
355 } hdr;
356 struct blob_attr *buf;
357 int ret = UBUS_STATUS_UNKNOWN_ERROR;
358
359 if (!path)
360 path = UBUS_UNIX_SOCKET;
361
362 if (ctx->sock.fd >= 0) {
363 if (ctx->sock.registered)
364 uloop_fd_delete(&ctx->sock);
365
366 close(ctx->sock.fd);
367 }
368
369 ctx->sock.fd = usock(USOCK_UNIX, path, NULL);
370 if (ctx->sock.fd < 0)
371 return UBUS_STATUS_CONNECTION_FAILED;
372
373 if (read(ctx->sock.fd, &hdr, sizeof(hdr)) != sizeof(hdr))
374 goto out_close;
375
376 if (!ubus_validate_hdr(&hdr.hdr))
377 goto out_close;
378
379 if (hdr.hdr.type != UBUS_MSG_HELLO)
380 goto out_close;
381
382 buf = calloc(1, blob_raw_len(&hdr.data));
383 if (!buf)
384 goto out_close;
385
386 memcpy(buf, &hdr.data, sizeof(hdr.data));
387 if (read(ctx->sock.fd, blob_data(buf), blob_len(buf)) != blob_len(buf))
388 goto out_free;
389
390 ctx->local_id = hdr.hdr.peer;
391 if (!ctx->local_id)
392 goto out_free;
393
394 ret = UBUS_STATUS_OK;
395 fcntl(ctx->sock.fd, F_SETFL, fcntl(ctx->sock.fd, F_GETFL) | O_NONBLOCK | O_CLOEXEC);
396
397 ubus_refresh_state(ctx);
398
399 out_free:
400 free(buf);
401 out_close:
402 if (ret)
403 close(ctx->sock.fd);
404
405 return ret;
406 }