2 * Copyright (C) 2011-2014 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>
18 #include <sys/param.h>
25 #include <libubox/blob.h>
26 #include <libubox/uloop.h>
27 #include <libubox/usock.h>
28 #include <libubox/list.h>
32 static struct ubus_msg_buf
*ubus_msg_unshare(struct ubus_msg_buf
*ub
)
34 ub
= realloc(ub
, sizeof(*ub
) + ub
->len
);
39 memcpy(ub
+ 1, ub
->data
, ub
->len
);
40 ub
->data
= (void *) (ub
+ 1);
44 static struct ubus_msg_buf
*ubus_msg_ref(struct ubus_msg_buf
*ub
)
46 if (ub
->refcount
== ~0)
47 return ubus_msg_unshare(ub
);
53 struct ubus_msg_buf
*ubus_msg_new(void *data
, int len
, bool shared
)
55 struct ubus_msg_buf
*ub
;
56 int buflen
= sizeof(*ub
);
61 ub
= calloc(1, buflen
);
72 ub
->data
= (void *) (ub
+ 1);
74 memcpy(ub
+ 1, data
, len
);
81 void ubus_msg_free(struct ubus_msg_buf
*ub
)
83 switch (ub
->refcount
) {
97 static int ubus_msg_writev(int fd
, struct ubus_msg_buf
*ub
, int offset
)
99 static struct iovec iov
[2];
105 .cmsg_len
= sizeof(fd_buf
),
106 .cmsg_level
= SOL_SOCKET
,
107 .cmsg_type
= SCM_RIGHTS
,
110 struct msghdr msghdr
= {
112 .msg_iovlen
= ARRAY_SIZE(iov
),
113 .msg_control
= &fd_buf
,
114 .msg_controllen
= sizeof(fd_buf
),
119 msghdr
.msg_control
= NULL
;
120 msghdr
.msg_controllen
= 0;
123 if (offset
< sizeof(ub
->hdr
)) {
124 iov
[0].iov_base
= ((char *) &ub
->hdr
) + offset
;
125 iov
[0].iov_len
= sizeof(ub
->hdr
) - offset
;
126 iov
[1].iov_base
= (char *) ub
->data
;
127 iov
[1].iov_len
= ub
->len
;
129 return sendmsg(fd
, &msghdr
, 0);
131 offset
-= sizeof(ub
->hdr
);
132 return write(fd
, ((char *) ub
->data
) + offset
, ub
->len
- offset
);
136 static void ubus_msg_enqueue(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
)
138 if (cl
->tx_queue
[cl
->txq_tail
])
141 cl
->tx_queue
[cl
->txq_tail
] = ubus_msg_ref(ub
);
142 cl
->txq_tail
= (cl
->txq_tail
+ 1) % ARRAY_SIZE(cl
->tx_queue
);
145 /* takes the msgbuf reference */
146 void ubus_msg_send(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
, bool free
)
150 if (!cl
->tx_queue
[cl
->txq_cur
]) {
151 written
= ubus_msg_writev(cl
->sock
.fd
, ub
, 0);
152 if (written
>= ub
->len
+ sizeof(ub
->hdr
))
158 cl
->txq_ofs
= written
;
160 /* get an event once we can write to the socket again */
161 uloop_fd_add(&cl
->sock
, ULOOP_READ
| ULOOP_WRITE
| ULOOP_EDGE_TRIGGER
);
163 ubus_msg_enqueue(cl
, ub
);
170 static struct ubus_msg_buf
*ubus_msg_head(struct ubus_client
*cl
)
172 return cl
->tx_queue
[cl
->txq_cur
];
175 static void ubus_msg_dequeue(struct ubus_client
*cl
)
177 struct ubus_msg_buf
*ub
= ubus_msg_head(cl
);
184 cl
->tx_queue
[cl
->txq_cur
] = NULL
;
185 cl
->txq_cur
= (cl
->txq_cur
+ 1) % ARRAY_SIZE(cl
->tx_queue
);
188 static void handle_client_disconnect(struct ubus_client
*cl
)
190 while (ubus_msg_head(cl
))
191 ubus_msg_dequeue(cl
);
193 ubusd_proto_free_client(cl
);
194 if (cl
->pending_msg_fd
>= 0)
195 close(cl
->pending_msg_fd
);
196 uloop_fd_delete(&cl
->sock
);
201 static void client_cb(struct uloop_fd
*sock
, unsigned int events
)
203 struct ubus_client
*cl
= container_of(sock
, struct ubus_client
, sock
);
204 struct ubus_msg_buf
*ub
;
205 static struct iovec iov
;
211 .cmsg_type
= SCM_RIGHTS
,
212 .cmsg_level
= SOL_SOCKET
,
213 .cmsg_len
= sizeof(fd_buf
),
216 struct msghdr msghdr
= {
221 /* first try to tx more pending data */
222 while ((ub
= ubus_msg_head(cl
))) {
225 written
= ubus_msg_writev(sock
->fd
, ub
, cl
->txq_ofs
);
237 cl
->txq_ofs
+= written
;
238 if (cl
->txq_ofs
< ub
->len
+ sizeof(ub
->hdr
))
241 ubus_msg_dequeue(cl
);
244 /* prevent further ULOOP_WRITE events if we don't have data
246 if (!ubus_msg_head(cl
) && (events
& ULOOP_WRITE
))
247 uloop_fd_add(sock
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);
250 if (!sock
->eof
&& cl
->pending_msg_offset
< sizeof(cl
->hdrbuf
)) {
251 int offset
= cl
->pending_msg_offset
;
256 iov
.iov_base
= &cl
->hdrbuf
+ offset
;
257 iov
.iov_len
= sizeof(cl
->hdrbuf
) - offset
;
259 if (cl
->pending_msg_fd
< 0) {
260 msghdr
.msg_control
= &fd_buf
;
261 msghdr
.msg_controllen
= sizeof(fd_buf
);
263 msghdr
.msg_control
= NULL
;
264 msghdr
.msg_controllen
= 0;
267 bytes
= recvmsg(sock
->fd
, &msghdr
, 0);
272 cl
->pending_msg_fd
= fd_buf
.fd
;
274 cl
->pending_msg_offset
+= bytes
;
275 if (cl
->pending_msg_offset
< sizeof(cl
->hdrbuf
))
278 if (blob_pad_len(&cl
->hdrbuf
.data
) > UBUS_MAX_MSGLEN
)
281 cl
->pending_msg
= ubus_msg_new(NULL
, blob_raw_len(&cl
->hdrbuf
.data
), false);
282 if (!cl
->pending_msg
)
285 memcpy(&cl
->pending_msg
->hdr
, &cl
->hdrbuf
.hdr
, sizeof(cl
->hdrbuf
.hdr
));
286 memcpy(cl
->pending_msg
->data
, &cl
->hdrbuf
.data
, sizeof(cl
->hdrbuf
.data
));
289 ub
= cl
->pending_msg
;
291 int offset
= cl
->pending_msg_offset
- sizeof(ub
->hdr
);
292 int len
= blob_raw_len(ub
->data
) - offset
;
296 bytes
= read(sock
->fd
, (char *) ub
->data
+ offset
, len
);
302 cl
->pending_msg_offset
+= bytes
;
307 ub
->fd
= cl
->pending_msg_fd
;
308 cl
->pending_msg_fd
= -1;
309 cl
->pending_msg_offset
= 0;
310 cl
->pending_msg
= NULL
;
311 ubusd_proto_receive_message(cl
, ub
);
316 if (!sock
->eof
|| ubus_msg_head(cl
))
320 handle_client_disconnect(cl
);
323 static bool get_next_connection(int fd
)
325 struct ubus_client
*cl
;
328 client_fd
= accept(fd
, NULL
, 0);
339 cl
= ubusd_proto_new_client(client_fd
, client_cb
);
341 uloop_fd_add(&cl
->sock
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);
348 static void server_cb(struct uloop_fd
*fd
, unsigned int events
)
353 next
= get_next_connection(fd
->fd
);
357 static struct uloop_fd server_fd
= {
361 static int usage(const char *progname
)
363 fprintf(stderr
, "Usage: %s [<options>]\n"
365 " -s <socket>: Set the unix domain socket to listen on\n"
370 int main(int argc
, char **argv
)
372 const char *ubus_socket
= UBUS_UNIX_SOCKET
;
376 signal(SIGPIPE
, SIG_IGN
);
380 while ((ch
= getopt(argc
, argv
, "s:")) != -1) {
383 ubus_socket
= optarg
;
386 return usage(argv
[0]);
392 server_fd
.fd
= usock(USOCK_UNIX
| USOCK_SERVER
| USOCK_NONBLOCK
, ubus_socket
, NULL
);
393 if (server_fd
.fd
< 0) {
398 uloop_fd_add(&server_fd
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);