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 ubus_client_cmd_free(struct ubus_client_cmd
*cmd
)
38 ubus_msg_free(cmd
->msg
);
42 static void ubus_client_cmd_queue_process(struct ubus_client
*cl
)
44 struct ubus_client_cmd
*cmd
, *tmp
;
46 list_for_each_entry_safe(cmd
, tmp
, &cl
->cmd_queue
, list
) {
47 int ret
= ubusd_cmd_lookup(cl
, cmd
);
49 /* Stop if the last command caused buffering again */
53 ubus_client_cmd_free(cmd
);
57 static void client_cb(struct uloop_fd
*sock
, unsigned int events
)
59 struct ubus_client
*cl
= container_of(sock
, struct ubus_client
, sock
);
60 uint8_t fd_buf
[CMSG_SPACE(sizeof(int))] = { 0 };
61 struct msghdr msghdr
= { 0 };
62 struct ubus_msg_buf
*ub
;
63 struct ubus_msg_buf_list
*ubl
, *ubl2
;
64 static struct iovec iov
;
68 msghdr
.msg_iov
= &iov
,
69 msghdr
.msg_iovlen
= 1,
70 msghdr
.msg_control
= fd_buf
;
71 msghdr
.msg_controllen
= sizeof(fd_buf
);
73 cmsg
= CMSG_FIRSTHDR(&msghdr
);
74 cmsg
->cmsg_type
= SCM_RIGHTS
;
75 cmsg
->cmsg_level
= SOL_SOCKET
;
76 cmsg
->cmsg_len
= CMSG_LEN(sizeof(int));
78 pfd
= (int *) CMSG_DATA(cmsg
);
79 msghdr
.msg_controllen
= cmsg
->cmsg_len
;
81 /* first try to tx more pending data */
82 list_for_each_entry_safe(ubl
, ubl2
, &cl
->tx_queue
, list
) {
86 written
= ubus_msg_writev(sock
->fd
, ub
, cl
->txq_ofs
);
98 cl
->txq_ofs
+= written
;
99 cl
->txq_len
-= written
;
100 if (cl
->txq_ofs
< ub
->len
+ sizeof(ub
->hdr
))
104 ubus_msg_list_free(ubl
);
107 if (list_empty(&cl
->tx_queue
) && (events
& ULOOP_WRITE
)) {
108 /* Process queued commands */
109 ubus_client_cmd_queue_process(cl
);
111 /* prevent further ULOOP_WRITE events if we don't have data
113 if (list_empty(&cl
->tx_queue
))
114 uloop_fd_add(sock
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);
118 if (!sock
->eof
&& cl
->pending_msg_offset
< (int) sizeof(cl
->hdrbuf
)) {
119 int offset
= cl
->pending_msg_offset
;
124 iov
.iov_base
= ((char *) &cl
->hdrbuf
) + offset
;
125 iov
.iov_len
= sizeof(cl
->hdrbuf
) - offset
;
127 if (cl
->pending_msg_fd
< 0) {
128 msghdr
.msg_control
= fd_buf
;
129 msghdr
.msg_controllen
= cmsg
->cmsg_len
;
131 msghdr
.msg_control
= NULL
;
132 msghdr
.msg_controllen
= 0;
135 bytes
= recvmsg(sock
->fd
, &msghdr
, 0);
140 cl
->pending_msg_fd
= *pfd
;
142 cl
->pending_msg_offset
+= bytes
;
143 if (cl
->pending_msg_offset
< (int) sizeof(cl
->hdrbuf
))
146 if (blob_raw_len(&cl
->hdrbuf
.data
) < sizeof(struct blob_attr
))
148 if (blob_pad_len(&cl
->hdrbuf
.data
) > UBUS_MAX_MSGLEN
)
151 cl
->pending_msg
= ubus_msg_new(NULL
, blob_raw_len(&cl
->hdrbuf
.data
), false);
152 if (!cl
->pending_msg
)
155 cl
->hdrbuf
.hdr
.seq
= be16_to_cpu(cl
->hdrbuf
.hdr
.seq
);
156 cl
->hdrbuf
.hdr
.peer
= be32_to_cpu(cl
->hdrbuf
.hdr
.peer
);
158 memcpy(&cl
->pending_msg
->hdr
, &cl
->hdrbuf
.hdr
, sizeof(cl
->hdrbuf
.hdr
));
159 memcpy(cl
->pending_msg
->data
, &cl
->hdrbuf
.data
, sizeof(cl
->hdrbuf
.data
));
162 ub
= cl
->pending_msg
;
164 int offset
= cl
->pending_msg_offset
- sizeof(ub
->hdr
);
165 int len
= blob_raw_len(ub
->data
) - offset
;
169 bytes
= read(sock
->fd
, (char *) ub
->data
+ offset
, len
);
175 cl
->pending_msg_offset
+= bytes
;
180 ub
->fd
= cl
->pending_msg_fd
;
181 cl
->pending_msg_fd
= -1;
182 cl
->pending_msg_offset
= 0;
183 cl
->pending_msg
= NULL
;
184 ubusd_monitor_message(cl
, ub
, false);
185 ubusd_proto_receive_message(cl
, ub
);
190 if (!sock
->eof
|| !list_empty(&cl
->tx_queue
))
194 handle_client_disconnect(cl
);
197 static bool get_next_connection(int fd
)
199 struct ubus_client
*cl
;
202 client_fd
= accept(fd
, NULL
, 0);
213 cl
= ubusd_proto_new_client(client_fd
, client_cb
);
215 uloop_fd_add(&cl
->sock
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);
222 static void server_cb(struct uloop_fd
*fd
, unsigned int events
)
227 next
= get_next_connection(fd
->fd
);
231 static struct uloop_fd server_fd
= {
235 static int usage(const char *progname
)
237 fprintf(stderr
, "Usage: %s [<options>]\n"
239 " -A <path>: Set the path to ACL files\n"
240 " -s <socket>: Set the unix domain socket to listen on\n"
245 static void sighup_handler(int sig
)
250 static void mkdir_sockdir()
252 char *ubus_sock_dir
, *tmp
;
254 ubus_sock_dir
= strdup(UBUS_UNIX_SOCKET
);
255 tmp
= strrchr(ubus_sock_dir
, '/');
258 mkdir(ubus_sock_dir
, 0755);
263 #include <libubox/ulog.h>
265 int main(int argc
, char **argv
)
267 const char *ubus_socket
= UBUS_UNIX_SOCKET
;
271 signal(SIGPIPE
, SIG_IGN
);
272 signal(SIGHUP
, sighup_handler
);
274 ulog_open(ULOG_KMSG
| ULOG_SYSLOG
, LOG_DAEMON
, "ubusd");
275 openlog("ubusd", LOG_PID
, LOG_DAEMON
);
278 while ((ch
= getopt(argc
, argv
, "A:s:")) != -1) {
281 ubus_socket
= optarg
;
284 ubusd_acl_dir
= optarg
;
287 return usage(argv
[0]);
294 server_fd
.fd
= usock(USOCK_UNIX
| USOCK_SERVER
| USOCK_NONBLOCK
, ubus_socket
, NULL
);
295 if (server_fd
.fd
< 0) {
300 uloop_fd_add(&server_fd
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);