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 struct ubus_msg_buf
*ub
;
54 static struct iovec iov
;
60 .cmsg_type
= SCM_RIGHTS
,
61 .cmsg_level
= SOL_SOCKET
,
62 .cmsg_len
= sizeof(fd_buf
),
65 struct msghdr msghdr
= {
70 /* first try to tx more pending data */
71 while ((ub
= ubus_msg_head(cl
))) {
74 written
= ubus_msg_writev(sock
->fd
, ub
, cl
->txq_ofs
);
86 cl
->txq_ofs
+= written
;
87 if (cl
->txq_ofs
< ub
->len
+ sizeof(ub
->hdr
))
93 /* prevent further ULOOP_WRITE events if we don't have data
95 if (!ubus_msg_head(cl
) && (events
& ULOOP_WRITE
))
96 uloop_fd_add(sock
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);
99 if (!sock
->eof
&& cl
->pending_msg_offset
< (int) sizeof(cl
->hdrbuf
)) {
100 int offset
= cl
->pending_msg_offset
;
105 iov
.iov_base
= ((char *) &cl
->hdrbuf
) + offset
;
106 iov
.iov_len
= sizeof(cl
->hdrbuf
) - offset
;
108 if (cl
->pending_msg_fd
< 0) {
109 msghdr
.msg_control
= &fd_buf
;
110 msghdr
.msg_controllen
= sizeof(fd_buf
);
112 msghdr
.msg_control
= NULL
;
113 msghdr
.msg_controllen
= 0;
116 bytes
= recvmsg(sock
->fd
, &msghdr
, 0);
121 cl
->pending_msg_fd
= fd_buf
.fd
;
123 cl
->pending_msg_offset
+= bytes
;
124 if (cl
->pending_msg_offset
< (int) sizeof(cl
->hdrbuf
))
127 if (blob_pad_len(&cl
->hdrbuf
.data
) > UBUS_MAX_MSGLEN
)
130 cl
->pending_msg
= ubus_msg_new(NULL
, blob_raw_len(&cl
->hdrbuf
.data
), false);
131 if (!cl
->pending_msg
)
134 cl
->hdrbuf
.hdr
.seq
= be16_to_cpu(cl
->hdrbuf
.hdr
.seq
);
135 cl
->hdrbuf
.hdr
.peer
= be32_to_cpu(cl
->hdrbuf
.hdr
.peer
);
137 memcpy(&cl
->pending_msg
->hdr
, &cl
->hdrbuf
.hdr
, sizeof(cl
->hdrbuf
.hdr
));
138 memcpy(cl
->pending_msg
->data
, &cl
->hdrbuf
.data
, sizeof(cl
->hdrbuf
.data
));
141 ub
= cl
->pending_msg
;
143 int offset
= cl
->pending_msg_offset
- sizeof(ub
->hdr
);
144 int len
= blob_raw_len(ub
->data
) - offset
;
148 bytes
= read(sock
->fd
, (char *) ub
->data
+ offset
, len
);
154 cl
->pending_msg_offset
+= bytes
;
159 ub
->fd
= cl
->pending_msg_fd
;
160 cl
->pending_msg_fd
= -1;
161 cl
->pending_msg_offset
= 0;
162 cl
->pending_msg
= NULL
;
163 ubusd_monitor_message(cl
, ub
, false);
164 ubusd_proto_receive_message(cl
, ub
);
169 if (!sock
->eof
|| ubus_msg_head(cl
))
173 handle_client_disconnect(cl
);
176 static bool get_next_connection(int fd
)
178 struct ubus_client
*cl
;
181 client_fd
= accept(fd
, NULL
, 0);
192 cl
= ubusd_proto_new_client(client_fd
, client_cb
);
194 uloop_fd_add(&cl
->sock
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);
201 static void server_cb(struct uloop_fd
*fd
, unsigned int events
)
206 next
= get_next_connection(fd
->fd
);
210 static struct uloop_fd server_fd
= {
214 static int usage(const char *progname
)
216 fprintf(stderr
, "Usage: %s [<options>]\n"
218 " -A <path>: Set the path to ACL files\n"
219 " -s <socket>: Set the unix domain socket to listen on\n"
224 static void sighup_handler(int sig
)
229 int main(int argc
, char **argv
)
231 const char *ubus_socket
= UBUS_UNIX_SOCKET
;
235 signal(SIGPIPE
, SIG_IGN
);
236 signal(SIGHUP
, sighup_handler
);
238 openlog("ubusd", LOG_PID
, LOG_DAEMON
);
241 while ((ch
= getopt(argc
, argv
, "A:s:")) != -1) {
244 ubus_socket
= optarg
;
247 ubusd_acl_dir
= optarg
;
250 return usage(argv
[0]);
256 server_fd
.fd
= usock(USOCK_UNIX
| USOCK_SERVER
| USOCK_NONBLOCK
, ubus_socket
, NULL
);
257 if (server_fd
.fd
< 0) {
262 uloop_fd_add(&server_fd
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);