1 #include <sys/socket.h>
8 #include <libubox/blob.h>
9 #include <libubox/uloop.h>
10 #include <libubox/usock.h>
11 #include <libubox/list.h>
15 static struct avl_tree clients;
17 static struct ubus_msg_buf *ubus_msg_unshare(struct ubus_msg_buf *ub)
19 ub = realloc(ub, sizeof(*ub) + ub->len);
24 memcpy(ub + 1, ub->data, ub->len);
25 ub->data = (void *) (ub + 1);
29 struct ubus_msg_buf *ubus_msg_ref(struct ubus_msg_buf *ub)
31 if (ub->refcount == ~0)
32 return ubus_msg_unshare(ub);
38 struct ubus_msg_buf *ubus_msg_new(void *data, int len, bool shared)
40 struct ubus_msg_buf *ub;
41 int buflen = sizeof(*ub);
46 ub = calloc(1, buflen);
55 ub->data = (void *) (ub + 1);
57 memcpy(ub + 1, data, len);
64 void ubus_msg_free(struct ubus_msg_buf *ub)
66 switch (ub->refcount) {
77 static int ubus_msg_writev(int fd, struct ubus_msg_buf *ub, int offset)
81 if (offset < sizeof(ub->hdr)) {
82 iov[0].iov_base = ((char *) &ub->hdr) + offset;
83 iov[0].iov_len = sizeof(ub->hdr) - offset;
84 iov[1].iov_base = (char *) ub->data;
85 iov[1].iov_len = ub->len;
86 return writev(fd, iov, 2);
88 offset -= sizeof(ub->hdr);
89 return write(fd, ((char *) ub->data) + offset, ub->len - offset);
93 static void ubus_msg_enqueue(struct ubus_client *cl, struct ubus_msg_buf *ub)
95 if (cl->tx_queue[cl->txq_tail])
98 cl->tx_queue[cl->txq_tail] = ub;
99 cl->txq_tail = (cl->txq_tail + 1) % ARRAY_SIZE(cl->tx_queue);
102 /* takes the msgbuf reference */
103 void ubus_msg_send(struct ubus_client *cl, struct ubus_msg_buf *ub, bool free)
107 if (!cl->tx_queue[cl->txq_cur]) {
108 written = ubus_msg_writev(cl->sock.fd, ub, 0);
109 if (written >= ub->len + sizeof(ub->hdr))
115 cl->txq_ofs = written;
117 /* get an event once we can write to the socket again */
118 uloop_fd_add(&cl->sock, ULOOP_READ | ULOOP_WRITE | ULOOP_EDGE_TRIGGER);
120 ubus_msg_enqueue(cl, ub);
127 static struct ubus_msg_buf *ubus_msg_head(struct ubus_client *cl)
129 return cl->tx_queue[cl->txq_cur];
132 static void ubus_msg_dequeue(struct ubus_client *cl)
134 struct ubus_msg_buf *ub = ubus_msg_head(cl);
141 cl->tx_queue[cl->txq_cur] = NULL;
142 cl->txq_cur = (cl->txq_cur + 1) % ARRAY_SIZE(cl->tx_queue);
145 static void handle_client_disconnect(struct ubus_client *cl)
147 struct ubus_object *obj;
149 while (!list_empty(&cl->objects)) {
150 obj = list_first_entry(&cl->objects, struct ubus_object, list);
151 ubusd_free_object(obj);
154 while (ubus_msg_head(cl))
155 ubus_msg_dequeue(cl);
157 ubus_free_id(&clients, &cl->id);
158 uloop_fd_delete(&cl->sock);
163 static void client_cb(struct uloop_fd *sock, unsigned int events)
165 struct ubus_client *cl = container_of(sock, struct ubus_client, sock);
166 struct ubus_msg_buf *ub;
168 /* first try to tx more pending data */
169 while ((ub = ubus_msg_head(cl))) {
172 written = ubus_msg_writev(sock->fd, ub, cl->txq_ofs);
186 cl->txq_ofs += written;
187 if (cl->txq_ofs < ub->len + sizeof(ub->hdr))
190 ubus_msg_dequeue(cl);
193 /* prevent further ULOOP_WRITE events if we don't have data
195 if (!ubus_msg_head(cl) && (events & ULOOP_WRITE))
196 uloop_fd_add(sock, ULOOP_READ | ULOOP_EDGE_TRIGGER);
199 if (!sock->eof && cl->pending_msg_offset < sizeof(cl->hdrbuf)) {
200 int offset = cl->pending_msg_offset;
203 bytes = read(sock->fd, (char *)&cl->hdrbuf + offset, sizeof(cl->hdrbuf) - offset);
207 cl->pending_msg_offset += bytes;
208 if (cl->pending_msg_offset < sizeof(cl->hdrbuf))
211 if (blob_len(&cl->hdrbuf.data) + sizeof(cl->hdrbuf) > UBUS_MAX_MSGLEN)
214 cl->pending_msg = ubus_msg_new(NULL, blob_raw_len(&cl->hdrbuf.data), false);
215 if (!cl->pending_msg)
218 memcpy(&cl->pending_msg->hdr, &cl->hdrbuf.hdr, sizeof(cl->hdrbuf.hdr));
219 memcpy(cl->pending_msg->data, &cl->hdrbuf.data, sizeof(cl->hdrbuf.data));
222 ub = cl->pending_msg;
224 int offset = cl->pending_msg_offset - sizeof(ub->hdr);
225 int len = blob_raw_len(ub->data) - offset;
229 bytes = read(sock->fd, (char *) ub->data + offset, len);
235 cl->pending_msg_offset += bytes;
240 cl->pending_msg_offset = 0;
241 cl->pending_msg = NULL;
242 ubusd_receive_message(cl, ub);
247 if (!sock->eof || ubus_msg_head(cl))
251 handle_client_disconnect(cl);
254 struct ubus_client *ubusd_get_client_by_id(uint32_t id)
256 struct ubus_id *clid;
258 clid = ubus_find_id(&clients, id);
262 return container_of(clid, struct ubus_client, id);
265 static bool get_next_connection(int fd)
267 struct ubus_client *cl;
270 client_fd = accept(fd, NULL, 0);
281 cl = calloc(1, sizeof(*cl));
282 cl->sock.fd = client_fd;
284 INIT_LIST_HEAD(&cl->objects);
285 if (!ubus_alloc_id(&clients, &cl->id, 0))
288 cl->sock.cb = client_cb;
289 uloop_fd_add(&cl->sock, ULOOP_READ | ULOOP_EDGE_TRIGGER);
290 if (!ubusd_send_hello(cl))
296 ubus_free_id(&clients, &cl->id);
303 static void server_cb(struct uloop_fd *fd, unsigned int events)
308 next = get_next_connection(fd->fd);
312 static struct uloop_fd server_fd = {
316 int main(int argc, char **argv)
320 signal(SIGPIPE, SIG_IGN);
322 ubus_init_id_tree(&clients);
326 unlink(UBUS_UNIX_SOCKET);
327 server_fd.fd = usock(USOCK_UNIX | USOCK_SERVER | USOCK_NONBLOCK, UBUS_UNIX_SOCKET, NULL);
328 if (server_fd.fd < 0) {
333 uloop_fd_add(&server_fd, ULOOP_READ | ULOOP_EDGE_TRIGGER);