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 struct avl_tree clients
;
17 static struct ubus_msg_buf
*ubus_msg_unshare(struct ubus_msg_buf
*ub
)
19 ub
= realloc(ub
, sizeof(*ub
) + ub
->len
);
24 memcpy(ub
+ 1, ub
->data
, ub
->len
);
25 ub
->data
= (void *) (ub
+ 1);
29 struct ubus_msg_buf
*ubus_msg_ref(struct ubus_msg_buf
*ub
)
31 if (ub
->refcount
== ~0)
32 return ubus_msg_unshare(ub
);
38 struct ubus_msg_buf
*ubus_msg_new(void *data
, int len
, bool shared
)
40 struct ubus_msg_buf
*ub
;
41 int buflen
= sizeof(*ub
);
46 ub
= calloc(1, buflen
);
55 ub
->data
= (void *) (ub
+ 1);
57 memcpy(ub
+ 1, data
, len
);
64 void ubus_msg_free(struct ubus_msg_buf
*ub
)
66 switch (ub
->refcount
) {
77 static int ubus_msg_writev(int fd
, struct ubus_msg_buf
*ub
, int offset
)
81 if (offset
< sizeof(ub
->hdr
)) {
82 iov
[0].iov_base
= ((char *) &ub
->hdr
) + offset
;
83 iov
[0].iov_len
= sizeof(ub
->hdr
) - offset
;
84 iov
[1].iov_base
= (char *) ub
->data
;
85 iov
[1].iov_len
= ub
->len
;
86 return writev(fd
, iov
, 2);
88 offset
-= sizeof(ub
->hdr
);
89 return write(fd
, ((char *) ub
->data
) + offset
, ub
->len
- offset
);
93 static void ubus_msg_enqueue(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
)
95 if (cl
->tx_queue
[cl
->txq_tail
])
98 cl
->tx_queue
[cl
->txq_tail
] = ubus_msg_ref(ub
);
99 cl
->txq_tail
= (cl
->txq_tail
+ 1) % ARRAY_SIZE(cl
->tx_queue
);
102 /* takes the msgbuf reference */
103 void ubus_msg_send(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
, bool free
)
107 if (!cl
->tx_queue
[cl
->txq_cur
]) {
108 written
= ubus_msg_writev(cl
->sock
.fd
, ub
, 0);
109 if (written
>= ub
->len
+ sizeof(ub
->hdr
))
115 cl
->txq_ofs
= written
;
117 /* get an event once we can write to the socket again */
118 uloop_fd_add(&cl
->sock
, ULOOP_READ
| ULOOP_WRITE
| ULOOP_EDGE_TRIGGER
);
120 ubus_msg_enqueue(cl
, ub
);
127 static struct ubus_msg_buf
*ubus_msg_head(struct ubus_client
*cl
)
129 return cl
->tx_queue
[cl
->txq_cur
];
132 static void ubus_msg_dequeue(struct ubus_client
*cl
)
134 struct ubus_msg_buf
*ub
= ubus_msg_head(cl
);
141 cl
->tx_queue
[cl
->txq_cur
] = NULL
;
142 cl
->txq_cur
= (cl
->txq_cur
+ 1) % ARRAY_SIZE(cl
->tx_queue
);
145 static void handle_client_disconnect(struct ubus_client
*cl
)
147 struct ubus_object
*obj
;
149 while (!list_empty(&cl
->objects
)) {
150 obj
= list_first_entry(&cl
->objects
, struct ubus_object
, list
);
151 ubusd_free_object(obj
);
154 while (ubus_msg_head(cl
))
155 ubus_msg_dequeue(cl
);
157 ubus_free_id(&clients
, &cl
->id
);
158 uloop_fd_delete(&cl
->sock
);
163 static void client_cb(struct uloop_fd
*sock
, unsigned int events
)
165 struct ubus_client
*cl
= container_of(sock
, struct ubus_client
, sock
);
166 struct ubus_msg_buf
*ub
;
168 /* first try to tx more pending data */
169 while ((ub
= ubus_msg_head(cl
))) {
172 written
= ubus_msg_writev(sock
->fd
, ub
, cl
->txq_ofs
);
184 cl
->txq_ofs
+= written
;
185 if (cl
->txq_ofs
< ub
->len
+ sizeof(ub
->hdr
))
188 ubus_msg_dequeue(cl
);
191 /* prevent further ULOOP_WRITE events if we don't have data
193 if (!ubus_msg_head(cl
) && (events
& ULOOP_WRITE
))
194 uloop_fd_add(sock
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);
197 if (!sock
->eof
&& cl
->pending_msg_offset
< sizeof(cl
->hdrbuf
)) {
198 int offset
= cl
->pending_msg_offset
;
201 bytes
= read(sock
->fd
, (char *)&cl
->hdrbuf
+ offset
, sizeof(cl
->hdrbuf
) - offset
);
205 cl
->pending_msg_offset
+= bytes
;
206 if (cl
->pending_msg_offset
< sizeof(cl
->hdrbuf
))
209 if (blob_pad_len(&cl
->hdrbuf
.data
) > UBUS_MAX_MSGLEN
)
212 cl
->pending_msg
= ubus_msg_new(NULL
, blob_raw_len(&cl
->hdrbuf
.data
), false);
213 if (!cl
->pending_msg
)
216 memcpy(&cl
->pending_msg
->hdr
, &cl
->hdrbuf
.hdr
, sizeof(cl
->hdrbuf
.hdr
));
217 memcpy(cl
->pending_msg
->data
, &cl
->hdrbuf
.data
, sizeof(cl
->hdrbuf
.data
));
220 ub
= cl
->pending_msg
;
222 int offset
= cl
->pending_msg_offset
- sizeof(ub
->hdr
);
223 int len
= blob_raw_len(ub
->data
) - offset
;
227 bytes
= read(sock
->fd
, (char *) ub
->data
+ offset
, len
);
233 cl
->pending_msg_offset
+= bytes
;
238 cl
->pending_msg_offset
= 0;
239 cl
->pending_msg
= NULL
;
240 ubusd_receive_message(cl
, ub
);
245 if (!sock
->eof
|| ubus_msg_head(cl
))
249 handle_client_disconnect(cl
);
252 static bool get_next_connection(int fd
)
254 struct ubus_client
*cl
;
257 client_fd
= accept(fd
, NULL
, 0);
268 cl
= calloc(1, sizeof(*cl
));
269 cl
->sock
.fd
= client_fd
;
271 INIT_LIST_HEAD(&cl
->objects
);
272 if (!ubus_alloc_id(&clients
, &cl
->id
, 0))
275 cl
->sock
.cb
= client_cb
;
276 uloop_fd_add(&cl
->sock
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);
277 if (!ubusd_send_hello(cl
))
283 ubus_free_id(&clients
, &cl
->id
);
290 static void server_cb(struct uloop_fd
*fd
, unsigned int events
)
295 next
= get_next_connection(fd
->fd
);
299 static struct uloop_fd server_fd
= {
303 static int usage(const char *progname
)
305 fprintf(stderr
, "Usage: %s [<options>]\n"
307 " -s <socket>: Set the unix domain socket to listen on\n"
312 int main(int argc
, char **argv
)
314 const char *ubus_socket
= UBUS_UNIX_SOCKET
;
318 signal(SIGPIPE
, SIG_IGN
);
320 ubus_init_id_tree(&clients
);
324 while ((ch
= getopt(argc
, argv
, "s:")) != -1) {
327 ubus_socket
= optarg
;
330 return usage(argv
[0]);
335 server_fd
.fd
= usock(USOCK_UNIX
| USOCK_SERVER
| USOCK_NONBLOCK
, ubus_socket
, NULL
);
336 if (server_fd
.fd
< 0) {
341 uloop_fd_add(&server_fd
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);