ubusd_main.c 5.6 KB

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