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>
26 #include <libubox/blob.h>
27 #include <libubox/uloop.h>
28 #include <libubox/usock.h>
29 #include <libubox/list.h>
33 #define USES_EXTERNAL_BUFFER ~0U
35 static struct ubus_msg_buf
*ubus_msg_ref(struct ubus_msg_buf
*ub
)
37 struct ubus_msg_buf
*new_ub
;
38 if (ub
->refcount
== USES_EXTERNAL_BUFFER
) {
39 new_ub
= ubus_msg_new(ub
->data
, ub
->len
, false);
42 memcpy(&new_ub
->hdr
, &ub
->hdr
, sizeof(struct ubus_msghdr
));
51 struct ubus_msg_buf
*ubus_msg_new(void *data
, int len
, bool shared
)
53 struct ubus_msg_buf
*ub
;
54 int buflen
= sizeof(*ub
);
59 ub
= calloc(1, buflen
);
66 ub
->refcount
= USES_EXTERNAL_BUFFER
;
70 ub
->data
= (void *) (ub
+ 1);
72 memcpy(ub
+ 1, data
, len
);
79 void ubus_msg_free(struct ubus_msg_buf
*ub
)
81 switch (ub
->refcount
) {
83 case USES_EXTERNAL_BUFFER
:
95 static ssize_t
ubus_msg_writev(int fd
, struct ubus_msg_buf
*ub
, size_t offset
)
97 static struct iovec iov
[2];
103 .cmsg_len
= sizeof(fd_buf
),
104 .cmsg_level
= SOL_SOCKET
,
105 .cmsg_type
= SCM_RIGHTS
,
108 struct msghdr msghdr
= {
110 .msg_iovlen
= ARRAY_SIZE(iov
),
111 .msg_control
= &fd_buf
,
112 .msg_controllen
= sizeof(fd_buf
),
114 struct ubus_msghdr hdr
;
118 if (ub
->fd
< 0 || offset
) {
119 msghdr
.msg_control
= NULL
;
120 msghdr
.msg_controllen
= 0;
123 if (offset
< sizeof(ub
->hdr
)) {
124 hdr
.version
= ub
->hdr
.version
;
125 hdr
.type
= ub
->hdr
.type
;
126 hdr
.seq
= cpu_to_be16(ub
->hdr
.seq
);
127 hdr
.peer
= cpu_to_be32(ub
->hdr
.peer
);
129 iov
[0].iov_base
= ((char *) &hdr
) + offset
;
130 iov
[0].iov_len
= sizeof(hdr
) - offset
;
131 iov
[1].iov_base
= (char *) ub
->data
;
132 iov
[1].iov_len
= ub
->len
;
134 offset
-= sizeof(ub
->hdr
);
135 iov
[0].iov_base
= ((char *) ub
->data
) + offset
;
136 iov
[0].iov_len
= ub
->len
- offset
;
137 msghdr
.msg_iovlen
= 1;
141 ret
= sendmsg(fd
, &msghdr
, 0);
142 } while (ret
< 0 && errno
== EINTR
);
147 static void ubus_msg_enqueue(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
)
149 if (cl
->tx_queue
[cl
->txq_tail
])
152 cl
->tx_queue
[cl
->txq_tail
] = ubus_msg_ref(ub
);
153 cl
->txq_tail
= (cl
->txq_tail
+ 1) % ARRAY_SIZE(cl
->tx_queue
);
156 /* takes the msgbuf reference */
157 void ubus_msg_send(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
)
161 if (ub
->hdr
.type
!= UBUS_MSG_MONITOR
)
162 ubusd_monitor_message(cl
, ub
, true);
164 if (!cl
->tx_queue
[cl
->txq_cur
]) {
165 written
= ubus_msg_writev(cl
->sock
.fd
, ub
, 0);
170 if (written
>= (ssize_t
) (ub
->len
+ sizeof(ub
->hdr
)))
173 cl
->txq_ofs
= written
;
175 /* get an event once we can write to the socket again */
176 uloop_fd_add(&cl
->sock
, ULOOP_READ
| ULOOP_WRITE
| ULOOP_EDGE_TRIGGER
);
178 ubus_msg_enqueue(cl
, ub
);
181 static struct ubus_msg_buf
*ubus_msg_head(struct ubus_client
*cl
)
183 return cl
->tx_queue
[cl
->txq_cur
];
186 static void ubus_msg_dequeue(struct ubus_client
*cl
)
188 struct ubus_msg_buf
*ub
= ubus_msg_head(cl
);
195 cl
->tx_queue
[cl
->txq_cur
] = NULL
;
196 cl
->txq_cur
= (cl
->txq_cur
+ 1) % ARRAY_SIZE(cl
->tx_queue
);
199 static void handle_client_disconnect(struct ubus_client
*cl
)
201 while (ubus_msg_head(cl
))
202 ubus_msg_dequeue(cl
);
204 ubusd_monitor_disconnect(cl
);
205 ubusd_proto_free_client(cl
);
206 if (cl
->pending_msg_fd
>= 0)
207 close(cl
->pending_msg_fd
);
208 uloop_fd_delete(&cl
->sock
);
213 static void client_cb(struct uloop_fd
*sock
, unsigned int events
)
215 struct ubus_client
*cl
= container_of(sock
, struct ubus_client
, sock
);
216 struct ubus_msg_buf
*ub
;
217 static struct iovec iov
;
223 .cmsg_type
= SCM_RIGHTS
,
224 .cmsg_level
= SOL_SOCKET
,
225 .cmsg_len
= sizeof(fd_buf
),
228 struct msghdr msghdr
= {
233 /* first try to tx more pending data */
234 while ((ub
= ubus_msg_head(cl
))) {
237 written
= ubus_msg_writev(sock
->fd
, ub
, cl
->txq_ofs
);
249 cl
->txq_ofs
+= written
;
250 if (cl
->txq_ofs
< ub
->len
+ sizeof(ub
->hdr
))
253 ubus_msg_dequeue(cl
);
256 /* prevent further ULOOP_WRITE events if we don't have data
258 if (!ubus_msg_head(cl
) && (events
& ULOOP_WRITE
))
259 uloop_fd_add(sock
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);
262 if (!sock
->eof
&& cl
->pending_msg_offset
< (int) sizeof(cl
->hdrbuf
)) {
263 int offset
= cl
->pending_msg_offset
;
268 iov
.iov_base
= ((char *) &cl
->hdrbuf
) + offset
;
269 iov
.iov_len
= sizeof(cl
->hdrbuf
) - offset
;
271 if (cl
->pending_msg_fd
< 0) {
272 msghdr
.msg_control
= &fd_buf
;
273 msghdr
.msg_controllen
= sizeof(fd_buf
);
275 msghdr
.msg_control
= NULL
;
276 msghdr
.msg_controllen
= 0;
279 bytes
= recvmsg(sock
->fd
, &msghdr
, 0);
284 cl
->pending_msg_fd
= fd_buf
.fd
;
286 cl
->pending_msg_offset
+= bytes
;
287 if (cl
->pending_msg_offset
< (int) sizeof(cl
->hdrbuf
))
290 if (blob_pad_len(&cl
->hdrbuf
.data
) > UBUS_MAX_MSGLEN
)
293 cl
->pending_msg
= ubus_msg_new(NULL
, blob_raw_len(&cl
->hdrbuf
.data
), false);
294 if (!cl
->pending_msg
)
297 cl
->hdrbuf
.hdr
.seq
= be16_to_cpu(cl
->hdrbuf
.hdr
.seq
);
298 cl
->hdrbuf
.hdr
.peer
= be32_to_cpu(cl
->hdrbuf
.hdr
.peer
);
300 memcpy(&cl
->pending_msg
->hdr
, &cl
->hdrbuf
.hdr
, sizeof(cl
->hdrbuf
.hdr
));
301 memcpy(cl
->pending_msg
->data
, &cl
->hdrbuf
.data
, sizeof(cl
->hdrbuf
.data
));
304 ub
= cl
->pending_msg
;
306 int offset
= cl
->pending_msg_offset
- sizeof(ub
->hdr
);
307 int len
= blob_raw_len(ub
->data
) - offset
;
311 bytes
= read(sock
->fd
, (char *) ub
->data
+ offset
, len
);
317 cl
->pending_msg_offset
+= bytes
;
322 ub
->fd
= cl
->pending_msg_fd
;
323 cl
->pending_msg_fd
= -1;
324 cl
->pending_msg_offset
= 0;
325 cl
->pending_msg
= NULL
;
326 ubusd_monitor_message(cl
, ub
, false);
327 ubusd_proto_receive_message(cl
, ub
);
332 if (!sock
->eof
|| ubus_msg_head(cl
))
336 handle_client_disconnect(cl
);
339 static bool get_next_connection(int fd
)
341 struct ubus_client
*cl
;
344 client_fd
= accept(fd
, NULL
, 0);
355 cl
= ubusd_proto_new_client(client_fd
, client_cb
);
357 uloop_fd_add(&cl
->sock
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);
364 static void server_cb(struct uloop_fd
*fd
, unsigned int events
)
369 next
= get_next_connection(fd
->fd
);
373 static struct uloop_fd server_fd
= {
377 static int usage(const char *progname
)
379 fprintf(stderr
, "Usage: %s [<options>]\n"
381 " -A <path>: Set the path to ACL files\n"
382 " -s <socket>: Set the unix domain socket to listen on\n"
387 static void sighup_handler(int sig
)
392 int main(int argc
, char **argv
)
394 const char *ubus_socket
= UBUS_UNIX_SOCKET
;
398 signal(SIGPIPE
, SIG_IGN
);
399 signal(SIGHUP
, sighup_handler
);
401 openlog("ubusd", LOG_PID
, LOG_DAEMON
);
404 while ((ch
= getopt(argc
, argv
, "A:s:")) != -1) {
407 ubus_socket
= optarg
;
410 ubusd_acl_dir
= optarg
;
413 return usage(argv
[0]);
419 server_fd
.fd
= usock(USOCK_UNIX
| USOCK_SERVER
| USOCK_NONBLOCK
, ubus_socket
, NULL
);
420 if (server_fd
.fd
< 0) {
425 uloop_fd_add(&server_fd
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);