libubus: pull the variable length data array out of struct ubus_msghdr to fix builds...
[project/ubus.git] / libubus-io.c
1 /*
2 * Copyright (C) 2011-2012 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 static const struct blob_attr_info ubus_policy[UBUS_ATTR_MAX] = {
32 [UBUS_ATTR_STATUS] = { .type = BLOB_ATTR_INT32 },
33 [UBUS_ATTR_OBJID] = { .type = BLOB_ATTR_INT32 },
34 [UBUS_ATTR_OBJPATH] = { .type = BLOB_ATTR_STRING },
35 [UBUS_ATTR_METHOD] = { .type = BLOB_ATTR_STRING },
36 [UBUS_ATTR_ACTIVE] = { .type = BLOB_ATTR_INT8 },
37 [UBUS_ATTR_NO_REPLY] = { .type = BLOB_ATTR_INT8 },
38 [UBUS_ATTR_SUBSCRIBERS] = { .type = BLOB_ATTR_NESTED },
39 };
40
41 static struct blob_attr *attrbuf[UBUS_ATTR_MAX];
42
43 __hidden struct blob_attr **ubus_parse_msg(struct blob_attr *msg)
44 {
45 blob_parse(msg, attrbuf, ubus_policy, UBUS_ATTR_MAX);
46 return attrbuf;
47 }
48
49 static void wait_data(int fd, bool write)
50 {
51 struct pollfd pfd = { .fd = fd };
52
53 pfd.events = write ? POLLOUT : POLLIN;
54 poll(&pfd, 1, 0);
55 }
56
57 static int writev_retry(int fd, struct iovec *iov, int iov_len)
58 {
59 int len = 0;
60
61 do {
62 int cur_len = writev(fd, iov, iov_len);
63 if (cur_len < 0) {
64 switch(errno) {
65 case EAGAIN:
66 wait_data(fd, true);
67 break;
68 case EINTR:
69 break;
70 default:
71 return -1;
72 }
73 continue;
74 }
75 len += cur_len;
76 while (cur_len >= iov->iov_len) {
77 cur_len -= iov->iov_len;
78 iov_len--;
79 iov++;
80 if (!iov_len)
81 return len;
82 }
83 iov->iov_len -= cur_len;
84 } while (1);
85
86 /* Should never reach here */
87 return -1;
88 }
89
90 int __hidden ubus_send_msg(struct ubus_context *ctx, uint32_t seq,
91 struct blob_attr *msg, int cmd, uint32_t peer)
92 {
93 struct ubus_msghdr hdr;
94 struct iovec iov[2] = {
95 STATIC_IOV(hdr)
96 };
97 int ret;
98
99 hdr.version = 0;
100 hdr.type = cmd;
101 hdr.seq = seq;
102 hdr.peer = peer;
103
104 if (!msg) {
105 blob_buf_init(&b, 0);
106 msg = b.head;
107 }
108
109 iov[1].iov_base = (char *) msg;
110 iov[1].iov_len = blob_raw_len(msg);
111
112 ret = writev_retry(ctx->sock.fd, iov, ARRAY_SIZE(iov));
113 if (ret < 0)
114 ctx->sock.eof = true;
115
116 return ret;
117 }
118
119 static int recv_retry(int fd, struct iovec *iov, bool wait)
120 {
121 int bytes, total = 0;
122
123 while (iov->iov_len > 0) {
124 if (wait)
125 wait_data(fd, false);
126
127 bytes = read(fd, iov->iov_base, iov->iov_len);
128 if (!bytes)
129 return -1;
130
131 if (bytes < 0) {
132 bytes = 0;
133 if (uloop_cancelled)
134 return 0;
135 if (errno == EINTR)
136 continue;
137
138 if (errno != EAGAIN)
139 return -1;
140 }
141 if (!wait && !bytes)
142 return 0;
143
144 wait = true;
145 iov->iov_len -= bytes;
146 iov->iov_base += bytes;
147 total += bytes;
148 }
149
150 return total;
151 }
152
153 static bool ubus_validate_hdr(struct ubus_msghdr *hdr)
154 {
155 struct blob_attr *data = ubus_msghdr_data(hdr);
156
157 if (hdr->version != 0)
158 return false;
159
160 if (blob_raw_len(data) < sizeof(*data))
161 return false;
162
163 if (blob_pad_len(data) > UBUS_MAX_MSGLEN)
164 return false;
165
166 return true;
167 }
168
169 static bool get_next_msg(struct ubus_context *ctx)
170 {
171 struct iovec iov = STATIC_IOV(ctx->msgbuf.hdr);
172 int r;
173
174 /* receive header + start attribute */
175 iov.iov_len += sizeof(struct blob_attr);
176 r = recv_retry(ctx->sock.fd, &iov, false);
177 if (r <= 0) {
178 if (r < 0)
179 ctx->sock.eof = true;
180
181 return false;
182 }
183
184 iov.iov_len = blob_len(ubus_msghdr_data(&ctx->msgbuf.hdr));
185 if (iov.iov_len > 0 && !recv_retry(ctx->sock.fd, &iov, true))
186 return false;
187
188 return ubus_validate_hdr(&ctx->msgbuf.hdr);
189 }
190
191 void __hidden ubus_handle_data(struct uloop_fd *u, unsigned int events)
192 {
193 struct ubus_context *ctx = container_of(u, struct ubus_context, sock);
194 struct ubus_msghdr *hdr = &ctx->msgbuf.hdr;
195
196 while (get_next_msg(ctx)) {
197 ubus_process_msg(ctx, hdr);
198 if (uloop_cancelled)
199 break;
200 }
201
202 if (u->eof)
203 ctx->connection_lost(ctx);
204 }
205
206 static void
207 ubus_refresh_state(struct ubus_context *ctx)
208 {
209 struct ubus_object *obj, *tmp;
210 struct ubus_object **objs;
211 int n, i = 0;
212
213 /* clear all type IDs, they need to be registered again */
214 avl_for_each_element(&ctx->objects, obj, avl)
215 if (obj->type)
216 obj->type->id = 0;
217
218 /* push out all objects again */
219 objs = alloca(ctx->objects.count * sizeof(*objs));
220 avl_remove_all_elements(&ctx->objects, obj, avl, tmp) {
221 objs[i++] = obj;
222 obj->id = 0;
223 }
224
225 for (n = i, i = 0; i < n; i++)
226 ubus_add_object(ctx, objs[i]);
227 }
228
229 int ubus_reconnect(struct ubus_context *ctx, const char *path)
230 {
231 struct {
232 struct ubus_msghdr hdr;
233 struct blob_attr data;
234 } hdr;
235 struct blob_attr *buf;
236 int ret = UBUS_STATUS_UNKNOWN_ERROR;
237
238 if (!path)
239 path = UBUS_UNIX_SOCKET;
240
241 if (ctx->sock.fd >= 0) {
242 if (ctx->sock.registered)
243 uloop_fd_delete(&ctx->sock);
244
245 close(ctx->sock.fd);
246 }
247
248 ctx->sock.fd = usock(USOCK_UNIX, path, NULL);
249 if (ctx->sock.fd < 0)
250 return UBUS_STATUS_CONNECTION_FAILED;
251
252 if (read(ctx->sock.fd, &hdr, sizeof(hdr)) != sizeof(hdr))
253 goto out_close;
254
255 if (!ubus_validate_hdr(&hdr.hdr))
256 goto out_close;
257
258 if (hdr.hdr.type != UBUS_MSG_HELLO)
259 goto out_close;
260
261 buf = calloc(1, blob_raw_len(&hdr.data));
262 if (!buf)
263 goto out_close;
264
265 memcpy(buf, &hdr.data, sizeof(hdr.data));
266 if (read(ctx->sock.fd, blob_data(buf), blob_len(buf)) != blob_len(buf))
267 goto out_free;
268
269 ctx->local_id = hdr.hdr.peer;
270 if (!ctx->local_id)
271 goto out_free;
272
273 ret = UBUS_STATUS_OK;
274 fcntl(ctx->sock.fd, F_SETFL, fcntl(ctx->sock.fd, F_GETFL) | O_NONBLOCK);
275
276 ubus_refresh_state(ctx);
277
278 out_free:
279 free(buf);
280 out_close:
281 if (ret)
282 close(ctx->sock.fd);
283
284 return ret;
285 }