ubusd.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. static void ubus_msg_enqueue(struct ubus_client *cl, struct ubus_msg_buf *ub)
  78. {
  79. if (cl->tx_queue[cl->txq_tail])
  80. return;
  81. cl->tx_queue[cl->txq_tail] = ubus_msg_ref(ub);
  82. cl->txq_tail = (cl->txq_tail + 1) % ARRAY_SIZE(cl->tx_queue);
  83. }
  84. /* takes the msgbuf reference */
  85. void ubus_msg_send(struct ubus_client *cl, struct ubus_msg_buf *ub, bool free)
  86. {
  87. int written;
  88. if (!cl->tx_queue[cl->txq_cur]) {
  89. written = ubus_msg_writev(cl->sock.fd, ub, 0);
  90. if (written >= ub->len + sizeof(ub->hdr))
  91. goto out;
  92. if (written < 0)
  93. written = 0;
  94. cl->txq_ofs = written;
  95. /* get an event once we can write to the socket again */
  96. uloop_fd_add(&cl->sock, ULOOP_READ | ULOOP_WRITE | ULOOP_EDGE_TRIGGER);
  97. }
  98. ubus_msg_enqueue(cl, ub);
  99. out:
  100. if (free)
  101. ubus_msg_free(ub);
  102. }
  103. static struct ubus_msg_buf *ubus_msg_head(struct ubus_client *cl)
  104. {
  105. return cl->tx_queue[cl->txq_cur];
  106. }
  107. static void ubus_msg_dequeue(struct ubus_client *cl)
  108. {
  109. struct ubus_msg_buf *ub = ubus_msg_head(cl);
  110. if (!ub)
  111. return;
  112. ubus_msg_free(ub);
  113. cl->txq_ofs = 0;
  114. cl->tx_queue[cl->txq_cur] = NULL;
  115. cl->txq_cur = (cl->txq_cur + 1) % ARRAY_SIZE(cl->tx_queue);
  116. }
  117. static void handle_client_disconnect(struct ubus_client *cl)
  118. {
  119. struct ubus_object *obj;
  120. while (!list_empty(&cl->objects)) {
  121. obj = list_first_entry(&cl->objects, struct ubus_object, list);
  122. ubusd_free_object(obj);
  123. }
  124. while (ubus_msg_head(cl))
  125. ubus_msg_dequeue(cl);
  126. ubus_free_id(&clients, &cl->id);
  127. uloop_fd_delete(&cl->sock);
  128. close(cl->sock.fd);
  129. free(cl);
  130. }
  131. static void client_cb(struct uloop_fd *sock, unsigned int events)
  132. {
  133. struct ubus_client *cl = container_of(sock, struct ubus_client, sock);
  134. struct ubus_msg_buf *ub;
  135. /* first try to tx more pending data */
  136. while ((ub = ubus_msg_head(cl))) {
  137. int written;
  138. written = ubus_msg_writev(sock->fd, ub, cl->txq_ofs);
  139. if (written < 0) {
  140. switch(errno) {
  141. case EINTR:
  142. case EAGAIN:
  143. break;
  144. default:
  145. goto disconnect;
  146. }
  147. break;
  148. }
  149. cl->txq_ofs += written;
  150. if (cl->txq_ofs < ub->len + sizeof(ub->hdr))
  151. break;
  152. ubus_msg_dequeue(cl);
  153. }
  154. /* prevent further ULOOP_WRITE events if we don't have data
  155. * to send anymore */
  156. if (!ubus_msg_head(cl) && (events & ULOOP_WRITE))
  157. uloop_fd_add(sock, ULOOP_READ | ULOOP_EDGE_TRIGGER);
  158. retry:
  159. if (!sock->eof && cl->pending_msg_offset < sizeof(cl->hdrbuf)) {
  160. int offset = cl->pending_msg_offset;
  161. int bytes;
  162. bytes = read(sock->fd, (char *)&cl->hdrbuf + offset, sizeof(cl->hdrbuf) - offset);
  163. if (bytes < 0)
  164. goto out;
  165. cl->pending_msg_offset += bytes;
  166. if (cl->pending_msg_offset < sizeof(cl->hdrbuf))
  167. goto out;
  168. if (blob_len(&cl->hdrbuf.data) + sizeof(cl->hdrbuf) > UBUS_MAX_MSGLEN)
  169. goto disconnect;
  170. cl->pending_msg = ubus_msg_new(NULL, blob_raw_len(&cl->hdrbuf.data), false);
  171. if (!cl->pending_msg)
  172. goto disconnect;
  173. memcpy(&cl->pending_msg->hdr, &cl->hdrbuf.hdr, sizeof(cl->hdrbuf.hdr));
  174. memcpy(cl->pending_msg->data, &cl->hdrbuf.data, sizeof(cl->hdrbuf.data));
  175. }
  176. ub = cl->pending_msg;
  177. if (ub) {
  178. int offset = cl->pending_msg_offset - sizeof(ub->hdr);
  179. int len = blob_raw_len(ub->data) - offset;
  180. int bytes = 0;
  181. if (len > 0) {
  182. bytes = read(sock->fd, (char *) ub->data + offset, len);
  183. if (bytes <= 0)
  184. goto out;
  185. }
  186. if (bytes < len) {
  187. cl->pending_msg_offset += bytes;
  188. goto out;
  189. }
  190. /* accept message */
  191. cl->pending_msg_offset = 0;
  192. cl->pending_msg = NULL;
  193. ubusd_receive_message(cl, ub);
  194. goto retry;
  195. }
  196. out:
  197. if (!sock->eof || ubus_msg_head(cl))
  198. return;
  199. disconnect:
  200. handle_client_disconnect(cl);
  201. }
  202. struct ubus_client *ubusd_get_client_by_id(uint32_t id)
  203. {
  204. struct ubus_id *clid;
  205. clid = ubus_find_id(&clients, id);
  206. if (!clid)
  207. return NULL;
  208. return container_of(clid, struct ubus_client, id);
  209. }
  210. static bool get_next_connection(int fd)
  211. {
  212. struct ubus_client *cl;
  213. int client_fd;
  214. client_fd = accept(fd, NULL, 0);
  215. if (client_fd < 0) {
  216. switch (errno) {
  217. case ECONNABORTED:
  218. case EINTR:
  219. return true;
  220. default:
  221. return false;
  222. }
  223. }
  224. cl = calloc(1, sizeof(*cl));
  225. cl->sock.fd = client_fd;
  226. INIT_LIST_HEAD(&cl->objects);
  227. if (!ubus_alloc_id(&clients, &cl->id, 0))
  228. goto error;
  229. cl->sock.cb = client_cb;
  230. uloop_fd_add(&cl->sock, ULOOP_READ | ULOOP_EDGE_TRIGGER);
  231. if (!ubusd_send_hello(cl))
  232. goto error_free;
  233. return true;
  234. error_free:
  235. ubus_free_id(&clients, &cl->id);
  236. error:
  237. close(cl->sock.fd);
  238. free(cl);
  239. return true;
  240. }
  241. static void server_cb(struct uloop_fd *fd, unsigned int events)
  242. {
  243. bool next;
  244. do {
  245. next = get_next_connection(fd->fd);
  246. } while (next);
  247. }
  248. static struct uloop_fd server_fd = {
  249. .cb = server_cb,
  250. };
  251. static int usage(const char *progname)
  252. {
  253. fprintf(stderr, "Usage: %s [<options>]\n"
  254. "Options: \n"
  255. " -s <socket>: Set the unix domain socket to listen on\n"
  256. "\n", progname);
  257. return 1;
  258. }
  259. int main(int argc, char **argv)
  260. {
  261. const char *ubus_socket = UBUS_UNIX_SOCKET;
  262. int ret = 0;
  263. int ch;
  264. signal(SIGPIPE, SIG_IGN);
  265. ubus_init_id_tree(&clients);
  266. uloop_init();
  267. while ((ch = getopt(argc, argv, "s:")) != -1) {
  268. switch (ch) {
  269. case 's':
  270. ubus_socket = optarg;
  271. break;
  272. default:
  273. return usage(argv[0]);
  274. }
  275. }
  276. unlink(ubus_socket);
  277. server_fd.fd = usock(USOCK_UNIX | USOCK_SERVER | USOCK_NONBLOCK, ubus_socket, NULL);
  278. if (server_fd.fd < 0) {
  279. perror("usock");
  280. ret = -1;
  281. goto out;
  282. }
  283. uloop_fd_add(&server_fd, ULOOP_READ | ULOOP_EDGE_TRIGGER);
  284. uloop_run();
  285. unlink(ubus_socket);
  286. out:
  287. uloop_done();
  288. return ret;
  289. }