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