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 struct ubus_msg_buf
*ubus_msg_head(struct ubus_client
*cl
)
22 return cl
->tx_queue
[cl
->txq_cur
];
25 static void ubus_msg_dequeue(struct ubus_client
*cl
)
27 struct ubus_msg_buf
*ub
= ubus_msg_head(cl
);
34 cl
->tx_queue
[cl
->txq_cur
] = NULL
;
35 cl
->txq_cur
= (cl
->txq_cur
+ 1) % ARRAY_SIZE(cl
->tx_queue
);
38 static void handle_client_disconnect(struct ubus_client
*cl
)
40 while (ubus_msg_head(cl
))
43 ubusd_monitor_disconnect(cl
);
44 ubusd_proto_free_client(cl
);
45 if (cl
->pending_msg_fd
>= 0)
46 close(cl
->pending_msg_fd
);
47 uloop_fd_delete(&cl
->sock
);
52 static void client_cb(struct uloop_fd
*sock
, unsigned int events
)
54 struct ubus_client
*cl
= container_of(sock
, struct ubus_client
, sock
);
55 uint8_t fd_buf
[CMSG_SPACE(sizeof(int))] = { 0 };
56 struct msghdr msghdr
= { 0 };
57 struct ubus_msg_buf
*ub
;
58 static struct iovec iov
;
62 msghdr
.msg_iov
= &iov
,
63 msghdr
.msg_iovlen
= 1,
64 msghdr
.msg_control
= fd_buf
;
65 msghdr
.msg_controllen
= sizeof(fd_buf
);
67 cmsg
= CMSG_FIRSTHDR(&msghdr
);
68 cmsg
->cmsg_type
= SCM_RIGHTS
;
69 cmsg
->cmsg_level
= SOL_SOCKET
;
70 cmsg
->cmsg_len
= CMSG_LEN(sizeof(int));
72 pfd
= (int *) CMSG_DATA(cmsg
);
73 msghdr
.msg_controllen
= cmsg
->cmsg_len
;
75 /* first try to tx more pending data */
76 while ((ub
= ubus_msg_head(cl
))) {
79 written
= ubus_msg_writev(sock
->fd
, ub
, cl
->txq_ofs
);
91 cl
->txq_ofs
+= written
;
92 if (cl
->txq_ofs
< ub
->len
+ sizeof(ub
->hdr
))
98 /* prevent further ULOOP_WRITE events if we don't have data
100 if (!ubus_msg_head(cl
) && (events
& ULOOP_WRITE
))
101 uloop_fd_add(sock
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);
104 if (!sock
->eof
&& cl
->pending_msg_offset
< (int) sizeof(cl
->hdrbuf
)) {
105 int offset
= cl
->pending_msg_offset
;
110 iov
.iov_base
= ((char *) &cl
->hdrbuf
) + offset
;
111 iov
.iov_len
= sizeof(cl
->hdrbuf
) - offset
;
113 if (cl
->pending_msg_fd
< 0) {
114 msghdr
.msg_control
= fd_buf
;
115 msghdr
.msg_controllen
= cmsg
->cmsg_len
;
117 msghdr
.msg_control
= NULL
;
118 msghdr
.msg_controllen
= 0;
121 bytes
= recvmsg(sock
->fd
, &msghdr
, 0);
126 cl
->pending_msg_fd
= *pfd
;
128 cl
->pending_msg_offset
+= bytes
;
129 if (cl
->pending_msg_offset
< (int) sizeof(cl
->hdrbuf
))
132 if (blob_pad_len(&cl
->hdrbuf
.data
) > UBUS_MAX_MSGLEN
)
135 cl
->pending_msg
= ubus_msg_new(NULL
, blob_raw_len(&cl
->hdrbuf
.data
), false);
136 if (!cl
->pending_msg
)
139 cl
->hdrbuf
.hdr
.seq
= be16_to_cpu(cl
->hdrbuf
.hdr
.seq
);
140 cl
->hdrbuf
.hdr
.peer
= be32_to_cpu(cl
->hdrbuf
.hdr
.peer
);
142 memcpy(&cl
->pending_msg
->hdr
, &cl
->hdrbuf
.hdr
, sizeof(cl
->hdrbuf
.hdr
));
143 memcpy(cl
->pending_msg
->data
, &cl
->hdrbuf
.data
, sizeof(cl
->hdrbuf
.data
));
146 ub
= cl
->pending_msg
;
148 int offset
= cl
->pending_msg_offset
- sizeof(ub
->hdr
);
149 int len
= blob_raw_len(ub
->data
) - offset
;
153 bytes
= read(sock
->fd
, (char *) ub
->data
+ offset
, len
);
159 cl
->pending_msg_offset
+= bytes
;
164 ub
->fd
= cl
->pending_msg_fd
;
165 cl
->pending_msg_fd
= -1;
166 cl
->pending_msg_offset
= 0;
167 cl
->pending_msg
= NULL
;
168 ubusd_monitor_message(cl
, ub
, false);
169 ubusd_proto_receive_message(cl
, ub
);
174 if (!sock
->eof
|| ubus_msg_head(cl
))
178 handle_client_disconnect(cl
);
181 static bool get_next_connection(int fd
)
183 struct ubus_client
*cl
;
186 client_fd
= accept(fd
, NULL
, 0);
197 cl
= ubusd_proto_new_client(client_fd
, client_cb
);
199 uloop_fd_add(&cl
->sock
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);
206 static void server_cb(struct uloop_fd
*fd
, unsigned int events
)
211 next
= get_next_connection(fd
->fd
);
215 static struct uloop_fd server_fd
= {
219 static int usage(const char *progname
)
221 fprintf(stderr
, "Usage: %s [<options>]\n"
223 " -A <path>: Set the path to ACL files\n"
224 " -s <socket>: Set the unix domain socket to listen on\n"
229 static void sighup_handler(int sig
)
234 static void mkdir_sockdir()
236 char *ubus_sock_dir
, *tmp
;
238 ubus_sock_dir
= strdup(UBUS_UNIX_SOCKET
);
239 tmp
= strrchr(ubus_sock_dir
, '/');
242 mkdir(ubus_sock_dir
, 0755);
247 int main(int argc
, char **argv
)
249 const char *ubus_socket
= UBUS_UNIX_SOCKET
;
253 signal(SIGPIPE
, SIG_IGN
);
254 signal(SIGHUP
, sighup_handler
);
256 openlog("ubusd", LOG_PID
, LOG_DAEMON
);
259 while ((ch
= getopt(argc
, argv
, "A:s:")) != -1) {
262 ubus_socket
= optarg
;
265 ubusd_acl_dir
= optarg
;
268 return usage(argv
[0]);
275 server_fd
.fd
= usock(USOCK_UNIX
| USOCK_SERVER
| USOCK_NONBLOCK
, ubus_socket
, NULL
);
276 if (server_fd
.fd
< 0) {
281 uloop_fd_add(&server_fd
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);