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