2 * Copyright (C) 2011 Felix Fietkau <nbd@openwrt.org>
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
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.
14 #include <sys/socket.h>
21 #include <libubox/blob.h>
22 #include <libubox/uloop.h>
23 #include <libubox/usock.h>
24 #include <libubox/list.h>
28 static struct ubus_msg_buf
*ubus_msg_unshare(struct ubus_msg_buf
*ub
)
30 ub
= realloc(ub
, sizeof(*ub
) + ub
->len
);
35 memcpy(ub
+ 1, ub
->data
, ub
->len
);
36 ub
->data
= (void *) (ub
+ 1);
40 static struct ubus_msg_buf
*ubus_msg_ref(struct ubus_msg_buf
*ub
)
42 if (ub
->refcount
== ~0)
43 return ubus_msg_unshare(ub
);
49 struct ubus_msg_buf
*ubus_msg_new(void *data
, int len
, bool shared
)
51 struct ubus_msg_buf
*ub
;
52 int buflen
= sizeof(*ub
);
57 ub
= calloc(1, buflen
);
66 ub
->data
= (void *) (ub
+ 1);
68 memcpy(ub
+ 1, data
, len
);
75 void ubus_msg_free(struct ubus_msg_buf
*ub
)
77 switch (ub
->refcount
) {
88 static int ubus_msg_writev(int fd
, struct ubus_msg_buf
*ub
, int offset
)
92 if (offset
< sizeof(ub
->hdr
)) {
93 iov
[0].iov_base
= ((char *) &ub
->hdr
) + offset
;
94 iov
[0].iov_len
= sizeof(ub
->hdr
) - offset
;
95 iov
[1].iov_base
= (char *) ub
->data
;
96 iov
[1].iov_len
= ub
->len
;
97 return writev(fd
, iov
, 2);
99 offset
-= sizeof(ub
->hdr
);
100 return write(fd
, ((char *) ub
->data
) + offset
, ub
->len
- offset
);
104 static void ubus_msg_enqueue(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
)
106 if (cl
->tx_queue
[cl
->txq_tail
])
109 cl
->tx_queue
[cl
->txq_tail
] = ubus_msg_ref(ub
);
110 cl
->txq_tail
= (cl
->txq_tail
+ 1) % ARRAY_SIZE(cl
->tx_queue
);
113 /* takes the msgbuf reference */
114 void ubus_msg_send(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
, bool free
)
118 if (!cl
->tx_queue
[cl
->txq_cur
]) {
119 written
= ubus_msg_writev(cl
->sock
.fd
, ub
, 0);
120 if (written
>= ub
->len
+ sizeof(ub
->hdr
))
126 cl
->txq_ofs
= written
;
128 /* get an event once we can write to the socket again */
129 uloop_fd_add(&cl
->sock
, ULOOP_READ
| ULOOP_WRITE
| ULOOP_EDGE_TRIGGER
);
131 ubus_msg_enqueue(cl
, ub
);
138 static struct ubus_msg_buf
*ubus_msg_head(struct ubus_client
*cl
)
140 return cl
->tx_queue
[cl
->txq_cur
];
143 static void ubus_msg_dequeue(struct ubus_client
*cl
)
145 struct ubus_msg_buf
*ub
= ubus_msg_head(cl
);
152 cl
->tx_queue
[cl
->txq_cur
] = NULL
;
153 cl
->txq_cur
= (cl
->txq_cur
+ 1) % ARRAY_SIZE(cl
->tx_queue
);
156 static void handle_client_disconnect(struct ubus_client
*cl
)
158 while (ubus_msg_head(cl
))
159 ubus_msg_dequeue(cl
);
161 ubusd_proto_free_client(cl
);
162 uloop_fd_delete(&cl
->sock
);
167 static void client_cb(struct uloop_fd
*sock
, unsigned int events
)
169 struct ubus_client
*cl
= container_of(sock
, struct ubus_client
, sock
);
170 struct ubus_msg_buf
*ub
;
172 /* first try to tx more pending data */
173 while ((ub
= ubus_msg_head(cl
))) {
176 written
= ubus_msg_writev(sock
->fd
, ub
, cl
->txq_ofs
);
188 cl
->txq_ofs
+= written
;
189 if (cl
->txq_ofs
< ub
->len
+ sizeof(ub
->hdr
))
192 ubus_msg_dequeue(cl
);
195 /* prevent further ULOOP_WRITE events if we don't have data
197 if (!ubus_msg_head(cl
) && (events
& ULOOP_WRITE
))
198 uloop_fd_add(sock
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);
201 if (!sock
->eof
&& cl
->pending_msg_offset
< sizeof(cl
->hdrbuf
)) {
202 int offset
= cl
->pending_msg_offset
;
205 bytes
= read(sock
->fd
, (char *)&cl
->hdrbuf
+ offset
, sizeof(cl
->hdrbuf
) - offset
);
209 cl
->pending_msg_offset
+= bytes
;
210 if (cl
->pending_msg_offset
< sizeof(cl
->hdrbuf
))
213 if (blob_pad_len(&cl
->hdrbuf
.data
) > UBUS_MAX_MSGLEN
)
216 cl
->pending_msg
= ubus_msg_new(NULL
, blob_raw_len(&cl
->hdrbuf
.data
), false);
217 if (!cl
->pending_msg
)
220 memcpy(&cl
->pending_msg
->hdr
, &cl
->hdrbuf
.hdr
, sizeof(cl
->hdrbuf
.hdr
));
221 memcpy(cl
->pending_msg
->data
, &cl
->hdrbuf
.data
, sizeof(cl
->hdrbuf
.data
));
224 ub
= cl
->pending_msg
;
226 int offset
= cl
->pending_msg_offset
- sizeof(ub
->hdr
);
227 int len
= blob_raw_len(ub
->data
) - offset
;
231 bytes
= read(sock
->fd
, (char *) ub
->data
+ offset
, len
);
237 cl
->pending_msg_offset
+= bytes
;
242 cl
->pending_msg_offset
= 0;
243 cl
->pending_msg
= NULL
;
244 ubusd_proto_receive_message(cl
, ub
);
249 if (!sock
->eof
|| ubus_msg_head(cl
))
253 handle_client_disconnect(cl
);
256 static bool get_next_connection(int fd
)
258 struct ubus_client
*cl
;
261 client_fd
= accept(fd
, NULL
, 0);
272 cl
= ubusd_proto_new_client(client_fd
, client_cb
);
274 uloop_fd_add(&cl
->sock
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);
281 static void server_cb(struct uloop_fd
*fd
, unsigned int events
)
286 next
= get_next_connection(fd
->fd
);
290 static struct uloop_fd server_fd
= {
294 static int usage(const char *progname
)
296 fprintf(stderr
, "Usage: %s [<options>]\n"
298 " -s <socket>: Set the unix domain socket to listen on\n"
303 int main(int argc
, char **argv
)
305 const char *ubus_socket
= UBUS_UNIX_SOCKET
;
309 signal(SIGPIPE
, SIG_IGN
);
313 while ((ch
= getopt(argc
, argv
, "s:")) != -1) {
316 ubus_socket
= optarg
;
319 return usage(argv
[0]);
324 server_fd
.fd
= usock(USOCK_UNIX
| USOCK_SERVER
| USOCK_NONBLOCK
, ubus_socket
, NULL
);
325 if (server_fd
.fd
< 0) {
330 uloop_fd_add(&server_fd
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);