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 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 /* takes the msgbuf reference */
94 void ubus_msg_send(struct ubus_client
*cl
, struct ubus_msg_buf
*ub
)
101 written
= ubus_msg_writev(cl
->sock
.fd
, ub
, 0);
102 if (written
> 0 && written
< ub
->len
+ sizeof(ub
->hdr
)) {
103 cl
->buf_head_ofs
= written
;
105 /* get an event once we can write to the socket again */
106 uloop_fd_add(&cl
->sock
, ULOOP_READ
| ULOOP_WRITE
| ULOOP_EDGE_TRIGGER
);
114 ub
= ubus_msg_unshare(ub
);
117 cl
->buf_tail
= &ub
->next
;
120 static void handle_client_disconnect(struct ubus_client
*cl
)
122 struct ubus_object
*obj
;
124 while (!list_empty(&cl
->objects
)) {
125 obj
= list_first_entry(&cl
->objects
, struct ubus_object
, list
);
126 ubusd_free_object(obj
);
129 ubus_free_id(&clients
, &cl
->id
);
130 uloop_fd_delete(&cl
->sock
);
135 static void client_cb(struct uloop_fd
*sock
, unsigned int events
)
137 struct ubus_client
*cl
= container_of(sock
, struct ubus_client
, sock
);
138 struct ubus_msg_buf
*ub
;
140 /* first try to tx more pending data */
141 while (cl
->buf_head
) {
142 struct ubus_msg_buf
*ub
= cl
->buf_head
;
145 written
= ubus_msg_writev(sock
->fd
, ub
, cl
->buf_head_ofs
);
159 cl
->buf_head_ofs
+= written
;
160 if (cl
->buf_head_ofs
< ub
->len
+ sizeof(ub
->hdr
))
163 cl
->buf_head_ofs
= 0;
164 cl
->buf_head
= ub
->next
;
166 cl
->buf_tail
= &cl
->buf_head
;
169 /* prevent further ULOOP_WRITE events if we don't have data
171 if (!cl
->buf_head
&& (events
& ULOOP_WRITE
))
172 uloop_fd_add(sock
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);
175 if (!sock
->eof
&& cl
->pending_msg_offset
< sizeof(cl
->hdrbuf
)) {
176 int offset
= cl
->pending_msg_offset
;
179 bytes
= read(sock
->fd
, (char *)&cl
->hdrbuf
+ offset
, sizeof(cl
->hdrbuf
) - offset
);
183 cl
->pending_msg_offset
+= bytes
;
184 if (cl
->pending_msg_offset
< sizeof(cl
->hdrbuf
))
187 if (blob_len(&cl
->hdrbuf
.data
) + sizeof(cl
->hdrbuf
) > UBUS_MAX_MSGLEN
)
190 cl
->pending_msg
= ubus_msg_new(NULL
, blob_raw_len(&cl
->hdrbuf
.data
), false);
191 if (!cl
->pending_msg
)
194 memcpy(&cl
->pending_msg
->hdr
, &cl
->hdrbuf
.hdr
, sizeof(cl
->hdrbuf
.hdr
));
195 memcpy(cl
->pending_msg
->data
, &cl
->hdrbuf
.data
, sizeof(cl
->hdrbuf
.data
));
198 ub
= cl
->pending_msg
;
200 int offset
= cl
->pending_msg_offset
- sizeof(ub
->hdr
);
201 int len
= blob_raw_len(ub
->data
) - offset
;
205 bytes
= read(sock
->fd
, (char *) ub
->data
+ offset
, len
);
211 cl
->pending_msg_offset
+= bytes
;
216 cl
->pending_msg_offset
= 0;
217 cl
->pending_msg
= NULL
;
218 ubusd_receive_message(cl
, ub
);
223 if (!sock
->eof
|| cl
->buf_head
)
227 handle_client_disconnect(cl
);
230 struct ubus_client
*ubusd_get_client_by_id(uint32_t id
)
232 struct ubus_id
*clid
;
234 clid
= ubus_find_id(&clients
, id
);
238 return container_of(clid
, struct ubus_client
, id
);
241 static bool get_next_connection(int fd
)
243 struct ubus_client
*cl
;
246 client_fd
= accept(fd
, NULL
, 0);
257 cl
= calloc(1, sizeof(*cl
));
258 cl
->sock
.fd
= client_fd
;
260 INIT_LIST_HEAD(&cl
->objects
);
261 if (!ubus_alloc_id(&clients
, &cl
->id
, 0))
264 cl
->sock
.cb
= client_cb
;
265 uloop_fd_add(&cl
->sock
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);
266 if (!ubusd_send_hello(cl
))
272 ubus_free_id(&clients
, &cl
->id
);
279 static void server_cb(struct uloop_fd
*fd
, unsigned int events
)
284 next
= get_next_connection(fd
->fd
);
288 static struct uloop_fd server_fd
= {
292 int main(int argc
, char **argv
)
296 signal(SIGPIPE
, SIG_IGN
);
298 ubus_init_id_tree(&clients
);
302 unlink(UBUS_UNIX_SOCKET
);
303 server_fd
.fd
= usock(USOCK_UNIX
| USOCK_SERVER
| USOCK_NONBLOCK
, UBUS_UNIX_SOCKET
, NULL
);
304 if (server_fd
.fd
< 0) {
309 uloop_fd_add(&server_fd
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);