2 * Copyright (C) 2011-2014 Felix Fietkau <nbd@openwrt.org>
4 * SPDX-License-Identifier: LGPL-2.1-only
7 #include <sys/socket.h>
11 #include <sys/param.h>
16 #include <libubox/usock.h>
20 static void handle_client_disconnect(struct ubus_client
*cl
)
22 struct ubus_msg_buf_list
*ubl
, *ubl2
;
23 list_for_each_entry_safe(ubl
, ubl2
, &cl
->tx_queue
, list
)
24 ubus_msg_list_free(ubl
);
26 ubusd_monitor_disconnect(cl
);
27 ubusd_proto_free_client(cl
);
28 if (cl
->pending_msg_fd
>= 0)
29 close(cl
->pending_msg_fd
);
30 uloop_fd_delete(&cl
->sock
);
35 static void client_cb(struct uloop_fd
*sock
, unsigned int events
)
37 struct ubus_client
*cl
= container_of(sock
, struct ubus_client
, sock
);
38 uint8_t fd_buf
[CMSG_SPACE(sizeof(int))] = { 0 };
39 struct msghdr msghdr
= { 0 };
40 struct ubus_msg_buf
*ub
;
41 struct ubus_msg_buf_list
*ubl
, *ubl2
;
42 static struct iovec iov
;
46 msghdr
.msg_iov
= &iov
,
47 msghdr
.msg_iovlen
= 1,
48 msghdr
.msg_control
= fd_buf
;
49 msghdr
.msg_controllen
= sizeof(fd_buf
);
51 cmsg
= CMSG_FIRSTHDR(&msghdr
);
52 cmsg
->cmsg_type
= SCM_RIGHTS
;
53 cmsg
->cmsg_level
= SOL_SOCKET
;
54 cmsg
->cmsg_len
= CMSG_LEN(sizeof(int));
56 pfd
= (int *) CMSG_DATA(cmsg
);
57 msghdr
.msg_controllen
= cmsg
->cmsg_len
;
59 /* first try to tx more pending data */
60 list_for_each_entry_safe(ubl
, ubl2
, &cl
->tx_queue
, list
) {
64 written
= ubus_msg_writev(sock
->fd
, ub
, cl
->txq_ofs
);
76 cl
->txq_ofs
+= written
;
77 cl
->txq_len
-= written
;
78 if (cl
->txq_ofs
< ub
->len
+ sizeof(ub
->hdr
))
82 ubus_msg_list_free(ubl
);
85 /* prevent further ULOOP_WRITE events if we don't have data
87 if (list_empty(&cl
->tx_queue
) && (events
& ULOOP_WRITE
))
88 uloop_fd_add(sock
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);
91 if (!sock
->eof
&& cl
->pending_msg_offset
< (int) sizeof(cl
->hdrbuf
)) {
92 int offset
= cl
->pending_msg_offset
;
97 iov
.iov_base
= ((char *) &cl
->hdrbuf
) + offset
;
98 iov
.iov_len
= sizeof(cl
->hdrbuf
) - offset
;
100 if (cl
->pending_msg_fd
< 0) {
101 msghdr
.msg_control
= fd_buf
;
102 msghdr
.msg_controllen
= cmsg
->cmsg_len
;
104 msghdr
.msg_control
= NULL
;
105 msghdr
.msg_controllen
= 0;
108 bytes
= recvmsg(sock
->fd
, &msghdr
, 0);
113 cl
->pending_msg_fd
= *pfd
;
115 cl
->pending_msg_offset
+= bytes
;
116 if (cl
->pending_msg_offset
< (int) sizeof(cl
->hdrbuf
))
119 if (blob_raw_len(&cl
->hdrbuf
.data
) < sizeof(struct blob_attr
))
121 if (blob_pad_len(&cl
->hdrbuf
.data
) > UBUS_MAX_MSGLEN
)
124 cl
->pending_msg
= ubus_msg_new(NULL
, blob_raw_len(&cl
->hdrbuf
.data
), false);
125 if (!cl
->pending_msg
)
128 cl
->hdrbuf
.hdr
.seq
= be16_to_cpu(cl
->hdrbuf
.hdr
.seq
);
129 cl
->hdrbuf
.hdr
.peer
= be32_to_cpu(cl
->hdrbuf
.hdr
.peer
);
131 memcpy(&cl
->pending_msg
->hdr
, &cl
->hdrbuf
.hdr
, sizeof(cl
->hdrbuf
.hdr
));
132 memcpy(cl
->pending_msg
->data
, &cl
->hdrbuf
.data
, sizeof(cl
->hdrbuf
.data
));
135 ub
= cl
->pending_msg
;
137 int offset
= cl
->pending_msg_offset
- sizeof(ub
->hdr
);
138 int len
= blob_raw_len(ub
->data
) - offset
;
142 bytes
= read(sock
->fd
, (char *) ub
->data
+ offset
, len
);
148 cl
->pending_msg_offset
+= bytes
;
153 ub
->fd
= cl
->pending_msg_fd
;
154 cl
->pending_msg_fd
= -1;
155 cl
->pending_msg_offset
= 0;
156 cl
->pending_msg
= NULL
;
157 ubusd_monitor_message(cl
, ub
, false);
158 ubusd_proto_receive_message(cl
, ub
);
163 if (!sock
->eof
|| !list_empty(&cl
->tx_queue
))
167 handle_client_disconnect(cl
);
170 static bool get_next_connection(int fd
)
172 struct ubus_client
*cl
;
175 client_fd
= accept(fd
, NULL
, 0);
186 cl
= ubusd_proto_new_client(client_fd
, client_cb
);
188 uloop_fd_add(&cl
->sock
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);
195 static void server_cb(struct uloop_fd
*fd
, unsigned int events
)
200 next
= get_next_connection(fd
->fd
);
204 static struct uloop_fd server_fd
= {
208 static int usage(const char *progname
)
210 fprintf(stderr
, "Usage: %s [<options>]\n"
212 " -A <path>: Set the path to ACL files\n"
213 " -s <socket>: Set the unix domain socket to listen on\n"
218 static void sighup_handler(int sig
)
223 static void mkdir_sockdir()
225 char *ubus_sock_dir
, *tmp
;
227 ubus_sock_dir
= strdup(UBUS_UNIX_SOCKET
);
228 tmp
= strrchr(ubus_sock_dir
, '/');
231 mkdir(ubus_sock_dir
, 0755);
236 int main(int argc
, char **argv
)
238 const char *ubus_socket
= UBUS_UNIX_SOCKET
;
242 signal(SIGPIPE
, SIG_IGN
);
243 signal(SIGHUP
, sighup_handler
);
245 openlog("ubusd", LOG_PID
, LOG_DAEMON
);
248 while ((ch
= getopt(argc
, argv
, "A:s:")) != -1) {
251 ubus_socket
= optarg
;
254 ubusd_acl_dir
= optarg
;
257 return usage(argv
[0]);
264 server_fd
.fd
= usock(USOCK_UNIX
| USOCK_SERVER
| USOCK_NONBLOCK
, ubus_socket
, NULL
);
265 if (server_fd
.fd
< 0) {
270 uloop_fd_add(&server_fd
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);