2
0

ubusd.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. #include <sys/socket.h>
  2. #include <sys/uio.h>
  3. #include <signal.h>
  4. #include <stdio.h>
  5. #include <unistd.h>
  6. #include <fcntl.h>
  7. #include <libubox/blob.h>
  8. #include <libubox/uloop.h>
  9. #include <libubox/usock.h>
  10. #include <libubox/list.h>
  11. #include "ubusd.h"
  12. static struct avl_tree clients;
  13. static struct ubus_msg_buf *ubus_msg_unshare(struct ubus_msg_buf *ub)
  14. {
  15. ub = realloc(ub, sizeof(*ub) + ub->len);
  16. if (!ub)
  17. return NULL;
  18. ub->refcount = 1;
  19. memcpy(ub + 1, ub->data, ub->len);
  20. ub->data = (void *) (ub + 1);
  21. return ub;
  22. }
  23. struct ubus_msg_buf *ubus_msg_ref(struct ubus_msg_buf *ub)
  24. {
  25. if (ub->refcount == ~0)
  26. return ubus_msg_unshare(ub);
  27. ub->refcount++;
  28. return ub;
  29. }
  30. struct ubus_msg_buf *ubus_msg_new(void *data, int len, bool shared)
  31. {
  32. struct ubus_msg_buf *ub;
  33. int buflen = sizeof(*ub);
  34. if (!shared)
  35. buflen += len;
  36. ub = calloc(1, buflen);
  37. if (!ub)
  38. return NULL;
  39. if (shared) {
  40. ub->refcount = ~0;
  41. ub->data = data;
  42. } else {
  43. ub->refcount = 1;
  44. ub->data = (void *) (ub + 1);
  45. if (data)
  46. memcpy(ub + 1, data, len);
  47. }
  48. ub->len = len;
  49. return ub;
  50. }
  51. void ubus_msg_free(struct ubus_msg_buf *ub)
  52. {
  53. switch (ub->refcount) {
  54. case 1:
  55. case ~0:
  56. free(ub);
  57. break;
  58. default:
  59. ub->refcount--;
  60. break;
  61. }
  62. }
  63. static int ubus_msg_writev(int fd, struct ubus_msg_buf *ub, int offset)
  64. {
  65. struct iovec iov[2];
  66. if (offset < sizeof(ub->hdr)) {
  67. iov[0].iov_base = ((char *) &ub->hdr) + offset;
  68. iov[0].iov_len = sizeof(ub->hdr) - offset;
  69. iov[1].iov_base = (char *) ub->data;
  70. iov[1].iov_len = ub->len;
  71. return writev(fd, iov, 2);
  72. } else {
  73. offset -= sizeof(ub->hdr);
  74. return write(fd, ((char *) ub->data) + offset, ub->len - offset);
  75. }
  76. }
  77. /* takes the msgbuf reference */
  78. void ubus_msg_send(struct ubus_client *cl, struct ubus_msg_buf *ub)
  79. {
  80. int written;
  81. if (cl->buf_head)
  82. goto queue;
  83. written = ubus_msg_writev(cl->sock.fd, ub, 0);
  84. if (written > 0 && written < ub->len + sizeof(ub->hdr)) {
  85. cl->buf_head_ofs = written;
  86. /* get an event once we can write to the socket again */
  87. uloop_fd_add(&cl->sock, ULOOP_READ | ULOOP_WRITE | ULOOP_EDGE_TRIGGER);
  88. goto queue;
  89. }
  90. ubus_msg_free(ub);
  91. return;
  92. queue:
  93. ub = ubus_msg_unshare(ub);
  94. ub->next = NULL;
  95. *cl->buf_tail = ub;
  96. cl->buf_tail = &ub->next;
  97. }
  98. static void handle_client_disconnect(struct ubus_client *cl)
  99. {
  100. struct ubus_object *obj;
  101. while (!list_empty(&cl->objects)) {
  102. obj = list_first_entry(&cl->objects, struct ubus_object, list);
  103. ubusd_free_object(obj);
  104. }
  105. ubus_free_id(&clients, &cl->id);
  106. uloop_fd_delete(&cl->sock);
  107. close(cl->sock.fd);
  108. free(cl);
  109. }
  110. static void client_cb(struct uloop_fd *sock, unsigned int events)
  111. {
  112. struct ubus_client *cl = container_of(sock, struct ubus_client, sock);
  113. struct ubus_msg_buf *ub;
  114. /* first try to tx more pending data */
  115. while (cl->buf_head) {
  116. struct ubus_msg_buf *ub = cl->buf_head;
  117. int written;
  118. written = ubus_msg_writev(sock->fd, ub, cl->buf_head_ofs);
  119. if (written < 0) {
  120. switch(errno) {
  121. case EINTR:
  122. case EAGAIN:
  123. break;
  124. default:
  125. goto disconnect;
  126. }
  127. break;
  128. }
  129. if (written == 0)
  130. break;
  131. cl->buf_head_ofs += written;
  132. if (cl->buf_head_ofs < ub->len + sizeof(ub->hdr))
  133. break;
  134. cl->buf_head_ofs = 0;
  135. cl->buf_head = ub->next;
  136. if (!cl->buf_head)
  137. cl->buf_tail = &cl->buf_head;
  138. }
  139. /* prevent further ULOOP_WRITE events if we don't have data
  140. * to send anymore */
  141. if (!cl->buf_head && (events & ULOOP_WRITE))
  142. uloop_fd_add(sock, ULOOP_READ | ULOOP_EDGE_TRIGGER);
  143. retry:
  144. if (!sock->eof && cl->pending_msg_offset < sizeof(cl->hdrbuf)) {
  145. int offset = cl->pending_msg_offset;
  146. int bytes;
  147. bytes = read(sock->fd, (char *)&cl->hdrbuf + offset, sizeof(cl->hdrbuf) - offset);
  148. if (bytes < 0)
  149. goto out;
  150. cl->pending_msg_offset += bytes;
  151. if (cl->pending_msg_offset < sizeof(cl->hdrbuf))
  152. goto out;
  153. if (blob_len(&cl->hdrbuf.data) + sizeof(cl->hdrbuf) > UBUS_MAX_MSGLEN)
  154. goto disconnect;
  155. cl->pending_msg = ubus_msg_new(NULL, blob_raw_len(&cl->hdrbuf.data), false);
  156. if (!cl->pending_msg)
  157. goto disconnect;
  158. memcpy(&cl->pending_msg->hdr, &cl->hdrbuf.hdr, sizeof(cl->hdrbuf.hdr));
  159. memcpy(cl->pending_msg->data, &cl->hdrbuf.data, sizeof(cl->hdrbuf.data));
  160. }
  161. ub = cl->pending_msg;
  162. if (ub) {
  163. int offset = cl->pending_msg_offset - sizeof(ub->hdr);
  164. int len = blob_raw_len(ub->data) - offset;
  165. int bytes = 0;
  166. if (len > 0) {
  167. bytes = read(sock->fd, (char *) ub->data + offset, len);
  168. if (bytes <= 0)
  169. goto out;
  170. }
  171. if (bytes < len) {
  172. cl->pending_msg_offset += bytes;
  173. goto out;
  174. }
  175. /* accept message */
  176. cl->pending_msg_offset = 0;
  177. cl->pending_msg = NULL;
  178. ubusd_receive_message(cl, ub);
  179. goto retry;
  180. }
  181. out:
  182. if (!sock->eof || cl->buf_head)
  183. return;
  184. disconnect:
  185. handle_client_disconnect(cl);
  186. }
  187. struct ubus_client *ubusd_get_client_by_id(uint32_t id)
  188. {
  189. struct ubus_id *clid;
  190. clid = ubus_find_id(&clients, id);
  191. if (!clid)
  192. return NULL;
  193. return container_of(clid, struct ubus_client, id);
  194. }
  195. static bool get_next_connection(int fd)
  196. {
  197. struct ubus_client *cl;
  198. int client_fd;
  199. client_fd = accept(fd, NULL, 0);
  200. if (client_fd < 0) {
  201. switch (errno) {
  202. case ECONNABORTED:
  203. case EINTR:
  204. return true;
  205. default:
  206. return false;
  207. }
  208. }
  209. cl = calloc(1, sizeof(*cl));
  210. cl->sock.fd = client_fd;
  211. INIT_LIST_HEAD(&cl->objects);
  212. if (!ubus_alloc_id(&clients, &cl->id))
  213. goto error;
  214. cl->sock.cb = client_cb;
  215. uloop_fd_add(&cl->sock, ULOOP_READ | ULOOP_EDGE_TRIGGER);
  216. if (!ubusd_send_hello(cl))
  217. goto error_free;
  218. return true;
  219. error_free:
  220. ubus_free_id(&clients, &cl->id);
  221. error:
  222. close(cl->sock.fd);
  223. free(cl);
  224. return true;
  225. }
  226. static void server_cb(struct uloop_fd *fd, unsigned int events)
  227. {
  228. bool next;
  229. do {
  230. next = get_next_connection(fd->fd);
  231. } while (next);
  232. }
  233. static struct uloop_fd server_fd = {
  234. .cb = server_cb,
  235. };
  236. int main(int argc, char **argv)
  237. {
  238. int ret = 0;
  239. signal(SIGPIPE, SIG_IGN);
  240. ubus_init_id_tree(&clients);
  241. uloop_init();
  242. unlink(UBUS_UNIX_SOCKET);
  243. server_fd.fd = usock(USOCK_UNIX | USOCK_SERVER | USOCK_NONBLOCK, UBUS_UNIX_SOCKET, NULL);
  244. if (server_fd.fd < 0) {
  245. perror("usock");
  246. ret = -1;
  247. goto out;
  248. }
  249. uloop_fd_add(&server_fd, ULOOP_READ | ULOOP_EDGE_TRIGGER);
  250. uloop_run();
  251. out:
  252. uloop_done();
  253. return ret;
  254. }