ubusd_main.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * Copyright (C) 2011-2014 Felix Fietkau <nbd@openwrt.org>
  3. *
  4. * SPDX-License-Identifier: LGPL-2.1-only
  5. */
  6. #include <sys/socket.h>
  7. #include <sys/stat.h>
  8. #ifdef FreeBSD
  9. #include <sys/param.h>
  10. #endif
  11. #include <syslog.h>
  12. #include <libubox/usock.h>
  13. #include "ubusd.h"
  14. static void handle_client_disconnect(struct ubus_client *cl)
  15. {
  16. struct ubus_msg_buf_list *ubl, *ubl2;
  17. list_for_each_entry_safe(ubl, ubl2, &cl->tx_queue, list)
  18. ubus_msg_list_free(ubl);
  19. ubusd_monitor_disconnect(cl);
  20. ubusd_proto_free_client(cl);
  21. if (cl->pending_msg_fd >= 0)
  22. close(cl->pending_msg_fd);
  23. uloop_fd_delete(&cl->sock);
  24. close(cl->sock.fd);
  25. free(cl);
  26. }
  27. static void client_cb(struct uloop_fd *sock, unsigned int events)
  28. {
  29. struct ubus_client *cl = container_of(sock, struct ubus_client, sock);
  30. uint8_t fd_buf[CMSG_SPACE(sizeof(int))] = { 0 };
  31. struct msghdr msghdr = { 0 };
  32. struct ubus_msg_buf *ub;
  33. struct ubus_msg_buf_list *ubl, *ubl2;
  34. static struct iovec iov;
  35. struct cmsghdr *cmsg;
  36. int *pfd;
  37. msghdr.msg_iov = &iov,
  38. msghdr.msg_iovlen = 1,
  39. msghdr.msg_control = fd_buf;
  40. msghdr.msg_controllen = sizeof(fd_buf);
  41. cmsg = CMSG_FIRSTHDR(&msghdr);
  42. cmsg->cmsg_type = SCM_RIGHTS;
  43. cmsg->cmsg_level = SOL_SOCKET;
  44. cmsg->cmsg_len = CMSG_LEN(sizeof(int));
  45. pfd = (int *) CMSG_DATA(cmsg);
  46. msghdr.msg_controllen = cmsg->cmsg_len;
  47. /* first try to tx more pending data */
  48. list_for_each_entry_safe(ubl, ubl2, &cl->tx_queue, list) {
  49. ssize_t written;
  50. ub = ubl->msg;
  51. written = ubus_msg_writev(sock->fd, ub, cl->txq_ofs);
  52. if (written < 0) {
  53. switch(errno) {
  54. case EINTR:
  55. case EAGAIN:
  56. break;
  57. default:
  58. goto disconnect;
  59. }
  60. break;
  61. }
  62. cl->txq_ofs += written;
  63. cl->txq_len -= written;
  64. if (cl->txq_ofs < ub->len + sizeof(ub->hdr))
  65. break;
  66. cl->txq_ofs = 0;
  67. ubus_msg_list_free(ubl);
  68. }
  69. /* prevent further ULOOP_WRITE events if we don't have data
  70. * to send anymore */
  71. if (list_empty(&cl->tx_queue) && (events & ULOOP_WRITE))
  72. uloop_fd_add(sock, ULOOP_READ | ULOOP_EDGE_TRIGGER);
  73. retry:
  74. if (!sock->eof && cl->pending_msg_offset < (int) sizeof(cl->hdrbuf)) {
  75. int offset = cl->pending_msg_offset;
  76. int bytes;
  77. *pfd = -1;
  78. iov.iov_base = ((char *) &cl->hdrbuf) + offset;
  79. iov.iov_len = sizeof(cl->hdrbuf) - offset;
  80. if (cl->pending_msg_fd < 0) {
  81. msghdr.msg_control = fd_buf;
  82. msghdr.msg_controllen = cmsg->cmsg_len;
  83. } else {
  84. msghdr.msg_control = NULL;
  85. msghdr.msg_controllen = 0;
  86. }
  87. bytes = recvmsg(sock->fd, &msghdr, 0);
  88. if (bytes < 0)
  89. goto out;
  90. if (*pfd >= 0)
  91. cl->pending_msg_fd = *pfd;
  92. cl->pending_msg_offset += bytes;
  93. if (cl->pending_msg_offset < (int) sizeof(cl->hdrbuf))
  94. goto out;
  95. if (blob_raw_len(&cl->hdrbuf.data) < sizeof(struct blob_attr))
  96. goto disconnect;
  97. if (blob_pad_len(&cl->hdrbuf.data) > UBUS_MAX_MSGLEN)
  98. goto disconnect;
  99. cl->pending_msg = ubus_msg_new(NULL, blob_raw_len(&cl->hdrbuf.data), false);
  100. if (!cl->pending_msg)
  101. goto disconnect;
  102. cl->hdrbuf.hdr.seq = be16_to_cpu(cl->hdrbuf.hdr.seq);
  103. cl->hdrbuf.hdr.peer = be32_to_cpu(cl->hdrbuf.hdr.peer);
  104. memcpy(&cl->pending_msg->hdr, &cl->hdrbuf.hdr, sizeof(cl->hdrbuf.hdr));
  105. memcpy(cl->pending_msg->data, &cl->hdrbuf.data, sizeof(cl->hdrbuf.data));
  106. }
  107. ub = cl->pending_msg;
  108. if (ub) {
  109. int offset = cl->pending_msg_offset - sizeof(ub->hdr);
  110. int len = blob_raw_len(ub->data) - offset;
  111. int bytes = 0;
  112. if (len > 0) {
  113. bytes = read(sock->fd, (char *) ub->data + offset, len);
  114. if (bytes <= 0)
  115. goto out;
  116. }
  117. if (bytes < len) {
  118. cl->pending_msg_offset += bytes;
  119. goto out;
  120. }
  121. /* accept message */
  122. ub->fd = cl->pending_msg_fd;
  123. cl->pending_msg_fd = -1;
  124. cl->pending_msg_offset = 0;
  125. cl->pending_msg = NULL;
  126. ubusd_monitor_message(cl, ub, false);
  127. ubusd_proto_receive_message(cl, ub);
  128. goto retry;
  129. }
  130. out:
  131. if (!sock->eof || !list_empty(&cl->tx_queue))
  132. return;
  133. disconnect:
  134. handle_client_disconnect(cl);
  135. }
  136. static bool get_next_connection(int fd)
  137. {
  138. struct ubus_client *cl;
  139. int client_fd;
  140. client_fd = accept(fd, NULL, 0);
  141. if (client_fd < 0) {
  142. switch (errno) {
  143. case ECONNABORTED:
  144. case EINTR:
  145. return true;
  146. default:
  147. return false;
  148. }
  149. }
  150. cl = ubusd_proto_new_client(client_fd, client_cb);
  151. if (cl)
  152. uloop_fd_add(&cl->sock, ULOOP_READ | ULOOP_EDGE_TRIGGER);
  153. else
  154. close(client_fd);
  155. return true;
  156. }
  157. static void server_cb(struct uloop_fd *fd, unsigned int events)
  158. {
  159. bool next;
  160. do {
  161. next = get_next_connection(fd->fd);
  162. } while (next);
  163. }
  164. static struct uloop_fd server_fd = {
  165. .cb = server_cb,
  166. };
  167. static int usage(const char *progname)
  168. {
  169. fprintf(stderr, "Usage: %s [<options>]\n"
  170. "Options: \n"
  171. " -A <path>: Set the path to ACL files\n"
  172. " -s <socket>: Set the unix domain socket to listen on\n"
  173. "\n", progname);
  174. return 1;
  175. }
  176. static void sighup_handler(int sig)
  177. {
  178. ubusd_acl_load();
  179. }
  180. int main(int argc, char **argv)
  181. {
  182. const char *ubus_socket = UBUS_UNIX_SOCKET;
  183. int ret = 0;
  184. int ch;
  185. signal(SIGPIPE, SIG_IGN);
  186. signal(SIGHUP, sighup_handler);
  187. openlog("ubusd", LOG_PID, LOG_DAEMON);
  188. uloop_init();
  189. while ((ch = getopt(argc, argv, "A:s:")) != -1) {
  190. switch (ch) {
  191. case 's':
  192. ubus_socket = optarg;
  193. break;
  194. case 'A':
  195. ubusd_acl_dir = optarg;
  196. break;
  197. default:
  198. return usage(argv[0]);
  199. }
  200. }
  201. unlink(ubus_socket);
  202. umask(0111);
  203. server_fd.fd = usock(USOCK_UNIX | USOCK_SERVER | USOCK_NONBLOCK, ubus_socket, NULL);
  204. if (server_fd.fd < 0) {
  205. perror("usock");
  206. ret = -1;
  207. goto out;
  208. }
  209. uloop_fd_add(&server_fd, ULOOP_READ | ULOOP_EDGE_TRIGGER);
  210. ubusd_acl_load();
  211. uloop_run();
  212. unlink(ubus_socket);
  213. out:
  214. uloop_done();
  215. return ret;
  216. }