1 #include <sys/socket.h>
8 #include <libubox/blob.h>
9 #include <libubox/uloop.h>
10 #include <libubox/usock.h>
11 #include <libubox/list.h>
15 static struct ubus_msg_buf
*ubus_msg_unshare(struct ubus_msg_buf
*ub
)
17 ub
= realloc(ub
, sizeof(*ub
) + ub
->len
);
22 memcpy(ub
+ 1, ub
->data
, ub
->len
);
23 ub
->data
= (void *) (ub
+ 1);
27 static struct ubus_msg_buf
*ubus_msg_ref(struct ubus_msg_buf
*ub
)
29 if (ub
->refcount
== ~0)
30 return ubus_msg_unshare(ub
);
36 struct ubus_msg_buf
*ubus_msg_new(void *data
, int len
, bool shared
)
38 struct ubus_msg_buf
*ub
;
39 int buflen
= sizeof(*ub
);
44 ub
= calloc(1, buflen
);
53 ub
->data
= (void *) (ub
+ 1);
55 memcpy(ub
+ 1, data
, len
);
62 void ubus_msg_free(struct ubus_msg_buf
*ub
)
64 switch (ub
->refcount
) {
75 static int ubus_msg_writev(int fd
, struct ubus_msg_buf
*ub
, int offset
)
79 if (offset
< sizeof(ub
->hdr
)) {
80 iov
[0].iov_base
= ((char *) &ub
->hdr
) + offset
;
81 iov
[0].iov_len
= sizeof(ub
->hdr
) - offset
;
82 iov
[1].iov_base
= (char *) ub
->data
;
83 iov
[1].iov_len
= ub
->len
;
84 return writev(fd
, iov
, 2);
86 offset
-= sizeof(ub
->hdr
);
87 return write(fd
, ((char *) ub
->data
) + offset
, ub
->len
- offset
);
91 static void ubus_msg_enqueue(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
)
93 if (cl
->tx_queue
[cl
->txq_tail
])
96 cl
->tx_queue
[cl
->txq_tail
] = ubus_msg_ref(ub
);
97 cl
->txq_tail
= (cl
->txq_tail
+ 1) % ARRAY_SIZE(cl
->tx_queue
);
100 /* takes the msgbuf reference */
101 void ubus_msg_send(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
, bool free
)
105 if (!cl
->tx_queue
[cl
->txq_cur
]) {
106 written
= ubus_msg_writev(cl
->sock
.fd
, ub
, 0);
107 if (written
>= ub
->len
+ sizeof(ub
->hdr
))
113 cl
->txq_ofs
= written
;
115 /* get an event once we can write to the socket again */
116 uloop_fd_add(&cl
->sock
, ULOOP_READ
| ULOOP_WRITE
| ULOOP_EDGE_TRIGGER
);
118 ubus_msg_enqueue(cl
, ub
);
125 static struct ubus_msg_buf
*ubus_msg_head(struct ubus_client
*cl
)
127 return cl
->tx_queue
[cl
->txq_cur
];
130 static void ubus_msg_dequeue(struct ubus_client
*cl
)
132 struct ubus_msg_buf
*ub
= ubus_msg_head(cl
);
139 cl
->tx_queue
[cl
->txq_cur
] = NULL
;
140 cl
->txq_cur
= (cl
->txq_cur
+ 1) % ARRAY_SIZE(cl
->tx_queue
);
143 static void handle_client_disconnect(struct ubus_client
*cl
)
145 while (ubus_msg_head(cl
))
146 ubus_msg_dequeue(cl
);
148 ubusd_proto_free_client(cl
);
149 uloop_fd_delete(&cl
->sock
);
154 static void client_cb(struct uloop_fd
*sock
, unsigned int events
)
156 struct ubus_client
*cl
= container_of(sock
, struct ubus_client
, sock
);
157 struct ubus_msg_buf
*ub
;
159 /* first try to tx more pending data */
160 while ((ub
= ubus_msg_head(cl
))) {
163 written
= ubus_msg_writev(sock
->fd
, ub
, cl
->txq_ofs
);
175 cl
->txq_ofs
+= written
;
176 if (cl
->txq_ofs
< ub
->len
+ sizeof(ub
->hdr
))
179 ubus_msg_dequeue(cl
);
182 /* prevent further ULOOP_WRITE events if we don't have data
184 if (!ubus_msg_head(cl
) && (events
& ULOOP_WRITE
))
185 uloop_fd_add(sock
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);
188 if (!sock
->eof
&& cl
->pending_msg_offset
< sizeof(cl
->hdrbuf
)) {
189 int offset
= cl
->pending_msg_offset
;
192 bytes
= read(sock
->fd
, (char *)&cl
->hdrbuf
+ offset
, sizeof(cl
->hdrbuf
) - offset
);
196 cl
->pending_msg_offset
+= bytes
;
197 if (cl
->pending_msg_offset
< sizeof(cl
->hdrbuf
))
200 if (blob_pad_len(&cl
->hdrbuf
.data
) > UBUS_MAX_MSGLEN
)
203 cl
->pending_msg
= ubus_msg_new(NULL
, blob_raw_len(&cl
->hdrbuf
.data
), false);
204 if (!cl
->pending_msg
)
207 memcpy(&cl
->pending_msg
->hdr
, &cl
->hdrbuf
.hdr
, sizeof(cl
->hdrbuf
.hdr
));
208 memcpy(cl
->pending_msg
->data
, &cl
->hdrbuf
.data
, sizeof(cl
->hdrbuf
.data
));
211 ub
= cl
->pending_msg
;
213 int offset
= cl
->pending_msg_offset
- sizeof(ub
->hdr
);
214 int len
= blob_raw_len(ub
->data
) - offset
;
218 bytes
= read(sock
->fd
, (char *) ub
->data
+ offset
, len
);
224 cl
->pending_msg_offset
+= bytes
;
229 cl
->pending_msg_offset
= 0;
230 cl
->pending_msg
= NULL
;
231 ubusd_proto_receive_message(cl
, ub
);
236 if (!sock
->eof
|| ubus_msg_head(cl
))
240 handle_client_disconnect(cl
);
243 static bool get_next_connection(int fd
)
245 struct ubus_client
*cl
;
248 client_fd
= accept(fd
, NULL
, 0);
259 cl
= ubusd_proto_new_client(client_fd
, client_cb
);
261 uloop_fd_add(&cl
->sock
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);
268 static void server_cb(struct uloop_fd
*fd
, unsigned int events
)
273 next
= get_next_connection(fd
->fd
);
277 static struct uloop_fd server_fd
= {
281 static int usage(const char *progname
)
283 fprintf(stderr
, "Usage: %s [<options>]\n"
285 " -s <socket>: Set the unix domain socket to listen on\n"
290 int main(int argc
, char **argv
)
292 const char *ubus_socket
= UBUS_UNIX_SOCKET
;
296 signal(SIGPIPE
, SIG_IGN
);
300 while ((ch
= getopt(argc
, argv
, "s:")) != -1) {
303 ubus_socket
= optarg
;
306 return usage(argv
[0]);
311 server_fd
.fd
= usock(USOCK_UNIX
| USOCK_SERVER
| USOCK_NONBLOCK
, ubus_socket
, NULL
);
312 if (server_fd
.fd
< 0) {
317 uloop_fd_add(&server_fd
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);