2 * Copyright (C) 2011-2014 Felix Fietkau <nbd@openwrt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2.1
6 * as published by the Free Software Foundation
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <sys/socket.h>
18 #include <sys/param.h>
26 #include <libubox/blob.h>
27 #include <libubox/uloop.h>
28 #include <libubox/usock.h>
29 #include <libubox/list.h>
33 static struct ubus_msg_buf
*ubus_msg_ref(struct ubus_msg_buf
*ub
)
35 struct ubus_msg_buf
*new_ub
;
36 if (ub
->refcount
== ~0) {
37 new_ub
= ubus_msg_new(ub
->data
, ub
->len
, false);
40 memcpy(&new_ub
->hdr
, &ub
->hdr
, sizeof(struct ubus_msghdr
));
49 struct ubus_msg_buf
*ubus_msg_new(void *data
, int len
, bool shared
)
51 struct ubus_msg_buf
*ub
;
52 int buflen
= sizeof(*ub
);
57 ub
= calloc(1, buflen
);
68 ub
->data
= (void *) (ub
+ 1);
70 memcpy(ub
+ 1, data
, len
);
77 void ubus_msg_free(struct ubus_msg_buf
*ub
)
79 switch (ub
->refcount
) {
93 static int ubus_msg_writev(int fd
, struct ubus_msg_buf
*ub
, int offset
)
95 static struct iovec iov
[2];
101 .cmsg_len
= sizeof(fd_buf
),
102 .cmsg_level
= SOL_SOCKET
,
103 .cmsg_type
= SCM_RIGHTS
,
106 struct msghdr msghdr
= {
108 .msg_iovlen
= ARRAY_SIZE(iov
),
109 .msg_control
= &fd_buf
,
110 .msg_controllen
= sizeof(fd_buf
),
115 msghdr
.msg_control
= NULL
;
116 msghdr
.msg_controllen
= 0;
119 if (offset
< sizeof(ub
->hdr
)) {
120 struct ubus_msghdr hdr
;
122 hdr
.version
= ub
->hdr
.version
;
123 hdr
.type
= ub
->hdr
.type
;
124 hdr
.seq
= cpu_to_be16(ub
->hdr
.seq
);
125 hdr
.peer
= cpu_to_be32(ub
->hdr
.peer
);
127 iov
[0].iov_base
= ((char *) &hdr
) + offset
;
128 iov
[0].iov_len
= sizeof(hdr
) - offset
;
129 iov
[1].iov_base
= (char *) ub
->data
;
130 iov
[1].iov_len
= ub
->len
;
132 return sendmsg(fd
, &msghdr
, 0);
134 offset
-= sizeof(ub
->hdr
);
135 return write(fd
, ((char *) ub
->data
) + offset
, ub
->len
- offset
);
139 static void ubus_msg_enqueue(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
)
141 if (cl
->tx_queue
[cl
->txq_tail
])
144 cl
->tx_queue
[cl
->txq_tail
] = ubus_msg_ref(ub
);
145 cl
->txq_tail
= (cl
->txq_tail
+ 1) % ARRAY_SIZE(cl
->tx_queue
);
148 /* takes the msgbuf reference */
149 void ubus_msg_send(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
)
153 if (ub
->hdr
.type
!= UBUS_MSG_MONITOR
)
154 ubusd_monitor_message(cl
, ub
, true);
156 if (!cl
->tx_queue
[cl
->txq_cur
]) {
157 written
= ubus_msg_writev(cl
->sock
.fd
, ub
, 0);
162 if (written
>= ub
->len
+ sizeof(ub
->hdr
))
165 cl
->txq_ofs
= written
;
167 /* get an event once we can write to the socket again */
168 uloop_fd_add(&cl
->sock
, ULOOP_READ
| ULOOP_WRITE
| ULOOP_EDGE_TRIGGER
);
170 ubus_msg_enqueue(cl
, ub
);
173 static struct ubus_msg_buf
*ubus_msg_head(struct ubus_client
*cl
)
175 return cl
->tx_queue
[cl
->txq_cur
];
178 static void ubus_msg_dequeue(struct ubus_client
*cl
)
180 struct ubus_msg_buf
*ub
= ubus_msg_head(cl
);
187 cl
->tx_queue
[cl
->txq_cur
] = NULL
;
188 cl
->txq_cur
= (cl
->txq_cur
+ 1) % ARRAY_SIZE(cl
->tx_queue
);
191 static void handle_client_disconnect(struct ubus_client
*cl
)
193 while (ubus_msg_head(cl
))
194 ubus_msg_dequeue(cl
);
196 ubusd_monitor_disconnect(cl
);
197 ubusd_proto_free_client(cl
);
198 if (cl
->pending_msg_fd
>= 0)
199 close(cl
->pending_msg_fd
);
200 uloop_fd_delete(&cl
->sock
);
205 static void client_cb(struct uloop_fd
*sock
, unsigned int events
)
207 struct ubus_client
*cl
= container_of(sock
, struct ubus_client
, sock
);
208 struct ubus_msg_buf
*ub
;
209 static struct iovec iov
;
215 .cmsg_type
= SCM_RIGHTS
,
216 .cmsg_level
= SOL_SOCKET
,
217 .cmsg_len
= sizeof(fd_buf
),
220 struct msghdr msghdr
= {
225 /* first try to tx more pending data */
226 while ((ub
= ubus_msg_head(cl
))) {
229 written
= ubus_msg_writev(sock
->fd
, ub
, cl
->txq_ofs
);
241 cl
->txq_ofs
+= written
;
242 if (cl
->txq_ofs
< ub
->len
+ sizeof(ub
->hdr
))
245 ubus_msg_dequeue(cl
);
248 /* prevent further ULOOP_WRITE events if we don't have data
250 if (!ubus_msg_head(cl
) && (events
& ULOOP_WRITE
))
251 uloop_fd_add(sock
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);
254 if (!sock
->eof
&& cl
->pending_msg_offset
< sizeof(cl
->hdrbuf
)) {
255 int offset
= cl
->pending_msg_offset
;
260 iov
.iov_base
= ((char *) &cl
->hdrbuf
) + offset
;
261 iov
.iov_len
= sizeof(cl
->hdrbuf
) - offset
;
263 if (cl
->pending_msg_fd
< 0) {
264 msghdr
.msg_control
= &fd_buf
;
265 msghdr
.msg_controllen
= sizeof(fd_buf
);
267 msghdr
.msg_control
= NULL
;
268 msghdr
.msg_controllen
= 0;
271 bytes
= recvmsg(sock
->fd
, &msghdr
, 0);
276 cl
->pending_msg_fd
= fd_buf
.fd
;
278 cl
->pending_msg_offset
+= bytes
;
279 if (cl
->pending_msg_offset
< sizeof(cl
->hdrbuf
))
282 if (blob_pad_len(&cl
->hdrbuf
.data
) > UBUS_MAX_MSGLEN
)
285 cl
->pending_msg
= ubus_msg_new(NULL
, blob_raw_len(&cl
->hdrbuf
.data
), false);
286 if (!cl
->pending_msg
)
289 cl
->hdrbuf
.hdr
.seq
= be16_to_cpu(cl
->hdrbuf
.hdr
.seq
);
290 cl
->hdrbuf
.hdr
.peer
= be32_to_cpu(cl
->hdrbuf
.hdr
.peer
);
292 memcpy(&cl
->pending_msg
->hdr
, &cl
->hdrbuf
.hdr
, sizeof(cl
->hdrbuf
.hdr
));
293 memcpy(cl
->pending_msg
->data
, &cl
->hdrbuf
.data
, sizeof(cl
->hdrbuf
.data
));
296 ub
= cl
->pending_msg
;
298 int offset
= cl
->pending_msg_offset
- sizeof(ub
->hdr
);
299 int len
= blob_raw_len(ub
->data
) - offset
;
303 bytes
= read(sock
->fd
, (char *) ub
->data
+ offset
, len
);
309 cl
->pending_msg_offset
+= bytes
;
314 ub
->fd
= cl
->pending_msg_fd
;
315 cl
->pending_msg_fd
= -1;
316 cl
->pending_msg_offset
= 0;
317 cl
->pending_msg
= NULL
;
318 ubusd_monitor_message(cl
, ub
, false);
319 ubusd_proto_receive_message(cl
, ub
);
324 if (!sock
->eof
|| ubus_msg_head(cl
))
328 handle_client_disconnect(cl
);
331 static bool get_next_connection(int fd
)
333 struct ubus_client
*cl
;
336 client_fd
= accept(fd
, NULL
, 0);
347 cl
= ubusd_proto_new_client(client_fd
, client_cb
);
349 uloop_fd_add(&cl
->sock
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);
356 static void server_cb(struct uloop_fd
*fd
, unsigned int events
)
361 next
= get_next_connection(fd
->fd
);
365 static struct uloop_fd server_fd
= {
369 static int usage(const char *progname
)
371 fprintf(stderr
, "Usage: %s [<options>]\n"
373 " -A <path>: Set the path to ACL files\n"
374 " -s <socket>: Set the unix domain socket to listen on\n"
379 static void sighup_handler(int sig
)
384 int main(int argc
, char **argv
)
386 const char *ubus_socket
= UBUS_UNIX_SOCKET
;
390 signal(SIGPIPE
, SIG_IGN
);
391 signal(SIGHUP
, sighup_handler
);
393 openlog("ubusd", LOG_PID
, LOG_DAEMON
);
396 while ((ch
= getopt(argc
, argv
, "A:s:")) != -1) {
399 ubus_socket
= optarg
;
402 ubusd_acl_dir
= optarg
;
405 return usage(argv
[0]);
411 server_fd
.fd
= usock(USOCK_UNIX
| USOCK_SERVER
| USOCK_NONBLOCK
, ubus_socket
, NULL
);
412 if (server_fd
.fd
< 0) {
417 uloop_fd_add(&server_fd
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);