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>
22 #include <libubox/blob.h>
23 #include <libubox/uloop.h>
24 #include <libubox/usock.h>
25 #include <libubox/list.h>
29 static struct ubus_msg_buf
*ubus_msg_unshare(struct ubus_msg_buf
*ub
)
31 ub
= realloc(ub
, sizeof(*ub
) + ub
->len
);
36 memcpy(ub
+ 1, ub
->data
, ub
->len
);
37 ub
->data
= (void *) (ub
+ 1);
41 static struct ubus_msg_buf
*ubus_msg_ref(struct ubus_msg_buf
*ub
)
43 if (ub
->refcount
== ~0)
44 return ubus_msg_unshare(ub
);
50 struct ubus_msg_buf
*ubus_msg_new(void *data
, int len
, bool shared
)
52 struct ubus_msg_buf
*ub
;
53 int buflen
= sizeof(*ub
);
58 ub
= calloc(1, buflen
);
67 ub
->data
= (void *) (ub
+ 1);
69 memcpy(ub
+ 1, data
, len
);
76 void ubus_msg_free(struct ubus_msg_buf
*ub
)
78 switch (ub
->refcount
) {
89 static int ubus_msg_writev(int fd
, struct ubus_msg_buf
*ub
, int offset
)
93 if (offset
< sizeof(ub
->hdr
)) {
94 iov
[0].iov_base
= ((char *) &ub
->hdr
) + offset
;
95 iov
[0].iov_len
= sizeof(ub
->hdr
) - offset
;
96 iov
[1].iov_base
= (char *) ub
->data
;
97 iov
[1].iov_len
= ub
->len
;
98 return writev(fd
, iov
, 2);
100 offset
-= sizeof(ub
->hdr
);
101 return write(fd
, ((char *) ub
->data
) + offset
, ub
->len
- offset
);
105 static void ubus_msg_enqueue(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
)
107 if (cl
->tx_queue
[cl
->txq_tail
])
110 cl
->tx_queue
[cl
->txq_tail
] = ubus_msg_ref(ub
);
111 cl
->txq_tail
= (cl
->txq_tail
+ 1) % ARRAY_SIZE(cl
->tx_queue
);
114 /* takes the msgbuf reference */
115 void ubus_msg_send(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
, bool free
)
119 if (!cl
->tx_queue
[cl
->txq_cur
]) {
120 written
= ubus_msg_writev(cl
->sock
.fd
, ub
, 0);
121 if (written
>= ub
->len
+ sizeof(ub
->hdr
))
127 cl
->txq_ofs
= written
;
129 /* get an event once we can write to the socket again */
130 uloop_fd_add(&cl
->sock
, ULOOP_READ
| ULOOP_WRITE
| ULOOP_EDGE_TRIGGER
);
132 ubus_msg_enqueue(cl
, ub
);
139 static struct ubus_msg_buf
*ubus_msg_head(struct ubus_client
*cl
)
141 return cl
->tx_queue
[cl
->txq_cur
];
144 static void ubus_msg_dequeue(struct ubus_client
*cl
)
146 struct ubus_msg_buf
*ub
= ubus_msg_head(cl
);
153 cl
->tx_queue
[cl
->txq_cur
] = NULL
;
154 cl
->txq_cur
= (cl
->txq_cur
+ 1) % ARRAY_SIZE(cl
->tx_queue
);
157 static void handle_client_disconnect(struct ubus_client
*cl
)
159 while (ubus_msg_head(cl
))
160 ubus_msg_dequeue(cl
);
162 ubusd_proto_free_client(cl
);
163 uloop_fd_delete(&cl
->sock
);
168 static void client_cb(struct uloop_fd
*sock
, unsigned int events
)
170 struct ubus_client
*cl
= container_of(sock
, struct ubus_client
, sock
);
171 struct ubus_msg_buf
*ub
;
173 /* first try to tx more pending data */
174 while ((ub
= ubus_msg_head(cl
))) {
177 written
= ubus_msg_writev(sock
->fd
, ub
, cl
->txq_ofs
);
189 cl
->txq_ofs
+= written
;
190 if (cl
->txq_ofs
< ub
->len
+ sizeof(ub
->hdr
))
193 ubus_msg_dequeue(cl
);
196 /* prevent further ULOOP_WRITE events if we don't have data
198 if (!ubus_msg_head(cl
) && (events
& ULOOP_WRITE
))
199 uloop_fd_add(sock
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);
202 if (!sock
->eof
&& cl
->pending_msg_offset
< sizeof(cl
->hdrbuf
)) {
203 int offset
= cl
->pending_msg_offset
;
206 bytes
= read(sock
->fd
, (char *)&cl
->hdrbuf
+ offset
, sizeof(cl
->hdrbuf
) - offset
);
210 cl
->pending_msg_offset
+= bytes
;
211 if (cl
->pending_msg_offset
< sizeof(cl
->hdrbuf
))
214 if (blob_pad_len(&cl
->hdrbuf
.data
) > UBUS_MAX_MSGLEN
)
217 cl
->pending_msg
= ubus_msg_new(NULL
, blob_raw_len(&cl
->hdrbuf
.data
), false);
218 if (!cl
->pending_msg
)
221 memcpy(&cl
->pending_msg
->hdr
, &cl
->hdrbuf
.hdr
, sizeof(cl
->hdrbuf
.hdr
));
222 memcpy(cl
->pending_msg
->data
, &cl
->hdrbuf
.data
, sizeof(cl
->hdrbuf
.data
));
225 ub
= cl
->pending_msg
;
227 int offset
= cl
->pending_msg_offset
- sizeof(ub
->hdr
);
228 int len
= blob_raw_len(ub
->data
) - offset
;
232 bytes
= read(sock
->fd
, (char *) ub
->data
+ offset
, len
);
238 cl
->pending_msg_offset
+= bytes
;
243 cl
->pending_msg_offset
= 0;
244 cl
->pending_msg
= NULL
;
245 ubusd_proto_receive_message(cl
, ub
);
250 if (!sock
->eof
|| ubus_msg_head(cl
))
254 handle_client_disconnect(cl
);
257 static bool get_next_connection(int fd
)
259 struct ubus_client
*cl
;
262 client_fd
= accept(fd
, NULL
, 0);
273 cl
= ubusd_proto_new_client(client_fd
, client_cb
);
275 uloop_fd_add(&cl
->sock
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);
282 static void server_cb(struct uloop_fd
*fd
, unsigned int events
)
287 next
= get_next_connection(fd
->fd
);
291 static struct uloop_fd server_fd
= {
295 static int usage(const char *progname
)
297 fprintf(stderr
, "Usage: %s [<options>]\n"
299 " -s <socket>: Set the unix domain socket to listen on\n"
304 int main(int argc
, char **argv
)
306 const char *ubus_socket
= UBUS_UNIX_SOCKET
;
310 signal(SIGPIPE
, SIG_IGN
);
314 while ((ch
= getopt(argc
, argv
, "s:")) != -1) {
317 ubus_socket
= optarg
;
320 return usage(argv
[0]);
326 server_fd
.fd
= usock(USOCK_UNIX
| USOCK_SERVER
| USOCK_NONBLOCK
, ubus_socket
, NULL
);
327 if (server_fd
.fd
< 0) {
332 uloop_fd_add(&server_fd
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);