d3c12f403067246936733627ab11e48253358ab4
2 * Copyright (C) 2011-2014 Felix Fietkau <nbd@openwrt.org>
4 * SPDX-License-Identifier: LGPL-2.1-only
7 #include <sys/socket.h>
10 #include <sys/param.h>
14 #include <libubox/usock.h>
18 static void handle_client_disconnect(struct ubus_client
*cl
)
20 struct ubus_msg_buf_list
*ubl
, *ubl2
;
21 list_for_each_entry_safe(ubl
, ubl2
, &cl
->tx_queue
, list
)
22 ubus_msg_list_free(ubl
);
24 ubusd_monitor_disconnect(cl
);
25 ubusd_proto_free_client(cl
);
26 if (cl
->pending_msg_fd
>= 0)
27 close(cl
->pending_msg_fd
);
28 uloop_fd_delete(&cl
->sock
);
33 static void client_cb(struct uloop_fd
*sock
, unsigned int events
)
35 struct ubus_client
*cl
= container_of(sock
, struct ubus_client
, sock
);
36 uint8_t fd_buf
[CMSG_SPACE(sizeof(int))] = { 0 };
37 struct msghdr msghdr
= { 0 };
38 struct ubus_msg_buf
*ub
;
39 struct ubus_msg_buf_list
*ubl
, *ubl2
;
40 static struct iovec iov
;
44 msghdr
.msg_iov
= &iov
,
45 msghdr
.msg_iovlen
= 1,
46 msghdr
.msg_control
= fd_buf
;
47 msghdr
.msg_controllen
= sizeof(fd_buf
);
49 cmsg
= CMSG_FIRSTHDR(&msghdr
);
50 cmsg
->cmsg_type
= SCM_RIGHTS
;
51 cmsg
->cmsg_level
= SOL_SOCKET
;
52 cmsg
->cmsg_len
= CMSG_LEN(sizeof(int));
54 pfd
= (int *) CMSG_DATA(cmsg
);
55 msghdr
.msg_controllen
= cmsg
->cmsg_len
;
57 /* first try to tx more pending data */
58 list_for_each_entry_safe(ubl
, ubl2
, &cl
->tx_queue
, list
) {
62 written
= ubus_msg_writev(sock
->fd
, ub
, cl
->txq_ofs
);
74 cl
->txq_ofs
+= written
;
75 cl
->txq_len
-= written
;
76 if (cl
->txq_ofs
< ub
->len
+ sizeof(ub
->hdr
))
79 ubus_msg_list_free(ubl
);
82 /* prevent further ULOOP_WRITE events if we don't have data
84 if (list_empty(&cl
->tx_queue
) && (events
& ULOOP_WRITE
))
85 uloop_fd_add(sock
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);
88 if (!sock
->eof
&& cl
->pending_msg_offset
< (int) sizeof(cl
->hdrbuf
)) {
89 int offset
= cl
->pending_msg_offset
;
94 iov
.iov_base
= ((char *) &cl
->hdrbuf
) + offset
;
95 iov
.iov_len
= sizeof(cl
->hdrbuf
) - offset
;
97 if (cl
->pending_msg_fd
< 0) {
98 msghdr
.msg_control
= fd_buf
;
99 msghdr
.msg_controllen
= cmsg
->cmsg_len
;
101 msghdr
.msg_control
= NULL
;
102 msghdr
.msg_controllen
= 0;
105 bytes
= recvmsg(sock
->fd
, &msghdr
, 0);
110 cl
->pending_msg_fd
= *pfd
;
112 cl
->pending_msg_offset
+= bytes
;
113 if (cl
->pending_msg_offset
< (int) sizeof(cl
->hdrbuf
))
116 if (blob_pad_len(&cl
->hdrbuf
.data
) > UBUS_MAX_MSGLEN
)
119 cl
->pending_msg
= ubus_msg_new(NULL
, blob_raw_len(&cl
->hdrbuf
.data
), false);
120 if (!cl
->pending_msg
)
123 cl
->hdrbuf
.hdr
.seq
= be16_to_cpu(cl
->hdrbuf
.hdr
.seq
);
124 cl
->hdrbuf
.hdr
.peer
= be32_to_cpu(cl
->hdrbuf
.hdr
.peer
);
126 memcpy(&cl
->pending_msg
->hdr
, &cl
->hdrbuf
.hdr
, sizeof(cl
->hdrbuf
.hdr
));
127 memcpy(cl
->pending_msg
->data
, &cl
->hdrbuf
.data
, sizeof(cl
->hdrbuf
.data
));
130 ub
= cl
->pending_msg
;
132 int offset
= cl
->pending_msg_offset
- sizeof(ub
->hdr
);
133 int len
= blob_raw_len(ub
->data
) - offset
;
137 bytes
= read(sock
->fd
, (char *) ub
->data
+ offset
, len
);
143 cl
->pending_msg_offset
+= bytes
;
148 ub
->fd
= cl
->pending_msg_fd
;
149 cl
->pending_msg_fd
= -1;
150 cl
->pending_msg_offset
= 0;
151 cl
->pending_msg
= NULL
;
152 ubusd_monitor_message(cl
, ub
, false);
153 ubusd_proto_receive_message(cl
, ub
);
158 if (!sock
->eof
|| !list_empty(&cl
->tx_queue
))
162 handle_client_disconnect(cl
);
165 static bool get_next_connection(int fd
)
167 struct ubus_client
*cl
;
170 client_fd
= accept(fd
, NULL
, 0);
181 cl
= ubusd_proto_new_client(client_fd
, client_cb
);
183 uloop_fd_add(&cl
->sock
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);
190 static void server_cb(struct uloop_fd
*fd
, unsigned int events
)
195 next
= get_next_connection(fd
->fd
);
199 static struct uloop_fd server_fd
= {
203 static int usage(const char *progname
)
205 fprintf(stderr
, "Usage: %s [<options>]\n"
207 " -A <path>: Set the path to ACL files\n"
208 " -s <socket>: Set the unix domain socket to listen on\n"
213 static void sighup_handler(int sig
)
218 int main(int argc
, char **argv
)
220 const char *ubus_socket
= UBUS_UNIX_SOCKET
;
224 signal(SIGPIPE
, SIG_IGN
);
225 signal(SIGHUP
, sighup_handler
);
227 openlog("ubusd", LOG_PID
, LOG_DAEMON
);
230 while ((ch
= getopt(argc
, argv
, "A:s:")) != -1) {
233 ubus_socket
= optarg
;
236 ubusd_acl_dir
= optarg
;
239 return usage(argv
[0]);
245 server_fd
.fd
= usock(USOCK_UNIX
| USOCK_SERVER
| USOCK_NONBLOCK
, ubus_socket
, NULL
);
246 if (server_fd
.fd
< 0) {
251 uloop_fd_add(&server_fd
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);