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 struct ubus_msg_buf
*ubus_msg_head(struct ubus_client
*cl
)
20 return cl
->tx_queue
[cl
->txq_cur
];
23 static void ubus_msg_dequeue(struct ubus_client
*cl
)
25 struct ubus_msg_buf
*ub
= ubus_msg_head(cl
);
32 cl
->tx_queue
[cl
->txq_cur
] = NULL
;
33 cl
->txq_cur
= (cl
->txq_cur
+ 1) % ARRAY_SIZE(cl
->tx_queue
);
36 static void handle_client_disconnect(struct ubus_client
*cl
)
38 while (ubus_msg_head(cl
))
41 ubusd_monitor_disconnect(cl
);
42 ubusd_proto_free_client(cl
);
43 if (cl
->pending_msg_fd
>= 0)
44 close(cl
->pending_msg_fd
);
45 uloop_fd_delete(&cl
->sock
);
50 static void client_cb(struct uloop_fd
*sock
, unsigned int events
)
52 struct ubus_client
*cl
= container_of(sock
, struct ubus_client
, sock
);
53 uint8_t fd_buf
[CMSG_SPACE(sizeof(int))] = { 0 };
54 struct msghdr msghdr
= { 0 };
55 struct ubus_msg_buf
*ub
;
56 static struct iovec iov
;
60 msghdr
.msg_iov
= &iov
,
61 msghdr
.msg_iovlen
= 1,
62 msghdr
.msg_control
= fd_buf
;
63 msghdr
.msg_controllen
= sizeof(fd_buf
);
65 cmsg
= CMSG_FIRSTHDR(&msghdr
);
66 cmsg
->cmsg_type
= SCM_RIGHTS
;
67 cmsg
->cmsg_level
= SOL_SOCKET
;
68 cmsg
->cmsg_len
= CMSG_LEN(sizeof(int));
70 pfd
= (int *) CMSG_DATA(cmsg
);
71 msghdr
.msg_controllen
= cmsg
->cmsg_len
;
73 /* first try to tx more pending data */
74 while ((ub
= ubus_msg_head(cl
))) {
77 written
= ubus_msg_writev(sock
->fd
, ub
, cl
->txq_ofs
);
89 cl
->txq_ofs
+= written
;
90 if (cl
->txq_ofs
< ub
->len
+ sizeof(ub
->hdr
))
96 /* prevent further ULOOP_WRITE events if we don't have data
98 if (!ubus_msg_head(cl
) && (events
& ULOOP_WRITE
))
99 uloop_fd_add(sock
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);
102 if (!sock
->eof
&& cl
->pending_msg_offset
< (int) sizeof(cl
->hdrbuf
)) {
103 int offset
= cl
->pending_msg_offset
;
108 iov
.iov_base
= ((char *) &cl
->hdrbuf
) + offset
;
109 iov
.iov_len
= sizeof(cl
->hdrbuf
) - offset
;
111 if (cl
->pending_msg_fd
< 0) {
112 msghdr
.msg_control
= fd_buf
;
113 msghdr
.msg_controllen
= cmsg
->cmsg_len
;
115 msghdr
.msg_control
= NULL
;
116 msghdr
.msg_controllen
= 0;
119 bytes
= recvmsg(sock
->fd
, &msghdr
, 0);
124 cl
->pending_msg_fd
= *pfd
;
126 cl
->pending_msg_offset
+= bytes
;
127 if (cl
->pending_msg_offset
< (int) sizeof(cl
->hdrbuf
))
130 if (blob_pad_len(&cl
->hdrbuf
.data
) > UBUS_MAX_MSGLEN
)
133 cl
->pending_msg
= ubus_msg_new(NULL
, blob_raw_len(&cl
->hdrbuf
.data
), false);
134 if (!cl
->pending_msg
)
137 cl
->hdrbuf
.hdr
.seq
= be16_to_cpu(cl
->hdrbuf
.hdr
.seq
);
138 cl
->hdrbuf
.hdr
.peer
= be32_to_cpu(cl
->hdrbuf
.hdr
.peer
);
140 memcpy(&cl
->pending_msg
->hdr
, &cl
->hdrbuf
.hdr
, sizeof(cl
->hdrbuf
.hdr
));
141 memcpy(cl
->pending_msg
->data
, &cl
->hdrbuf
.data
, sizeof(cl
->hdrbuf
.data
));
144 ub
= cl
->pending_msg
;
146 int offset
= cl
->pending_msg_offset
- sizeof(ub
->hdr
);
147 int len
= blob_raw_len(ub
->data
) - offset
;
151 bytes
= read(sock
->fd
, (char *) ub
->data
+ offset
, len
);
157 cl
->pending_msg_offset
+= bytes
;
162 ub
->fd
= cl
->pending_msg_fd
;
163 cl
->pending_msg_fd
= -1;
164 cl
->pending_msg_offset
= 0;
165 cl
->pending_msg
= NULL
;
166 ubusd_monitor_message(cl
, ub
, false);
167 ubusd_proto_receive_message(cl
, ub
);
172 if (!sock
->eof
|| ubus_msg_head(cl
))
176 handle_client_disconnect(cl
);
179 static bool get_next_connection(int fd
)
181 struct ubus_client
*cl
;
184 client_fd
= accept(fd
, NULL
, 0);
195 cl
= ubusd_proto_new_client(client_fd
, client_cb
);
197 uloop_fd_add(&cl
->sock
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);
204 static void server_cb(struct uloop_fd
*fd
, unsigned int events
)
209 next
= get_next_connection(fd
->fd
);
213 static struct uloop_fd server_fd
= {
217 static int usage(const char *progname
)
219 fprintf(stderr
, "Usage: %s [<options>]\n"
221 " -A <path>: Set the path to ACL files\n"
222 " -s <socket>: Set the unix domain socket to listen on\n"
227 static void sighup_handler(int sig
)
232 int main(int argc
, char **argv
)
234 const char *ubus_socket
= UBUS_UNIX_SOCKET
;
238 signal(SIGPIPE
, SIG_IGN
);
239 signal(SIGHUP
, sighup_handler
);
241 openlog("ubusd", LOG_PID
, LOG_DAEMON
);
244 while ((ch
= getopt(argc
, argv
, "A:s:")) != -1) {
247 ubus_socket
= optarg
;
250 ubusd_acl_dir
= optarg
;
253 return usage(argv
[0]);
259 server_fd
.fd
= usock(USOCK_UNIX
| USOCK_SERVER
| USOCK_NONBLOCK
, ubus_socket
, NULL
);
260 if (server_fd
.fd
< 0) {
265 uloop_fd_add(&server_fd
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);