ubusd_main.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 ubus_client_cmd_free(struct ubus_client_cmd *cmd)
  30. {
  31. list_del(&cmd->list);
  32. ubus_msg_free(cmd->msg);
  33. free(cmd);
  34. }
  35. static void ubus_client_cmd_queue_process(struct ubus_client *cl)
  36. {
  37. struct ubus_client_cmd *cmd, *tmp;
  38. list_for_each_entry_safe(cmd, tmp, &cl->cmd_queue, list) {
  39. int ret = ubusd_cmd_lookup(cl, cmd);
  40. /* Stop if the last command caused buffering again */
  41. if (ret == -2)
  42. break;
  43. ubus_client_cmd_free(cmd);
  44. }
  45. }
  46. static void client_cb(struct uloop_fd *sock, unsigned int events)
  47. {
  48. struct ubus_client *cl = container_of(sock, struct ubus_client, sock);
  49. uint8_t fd_buf[CMSG_SPACE(sizeof(int))] = { 0 };
  50. struct msghdr msghdr = { 0 };
  51. struct ubus_msg_buf *ub;
  52. struct ubus_msg_buf_list *ubl, *ubl2;
  53. static struct iovec iov;
  54. struct cmsghdr *cmsg;
  55. int *pfd;
  56. msghdr.msg_iov = &iov,
  57. msghdr.msg_iovlen = 1,
  58. msghdr.msg_control = fd_buf;
  59. msghdr.msg_controllen = sizeof(fd_buf);
  60. cmsg = CMSG_FIRSTHDR(&msghdr);
  61. cmsg->cmsg_type = SCM_RIGHTS;
  62. cmsg->cmsg_level = SOL_SOCKET;
  63. cmsg->cmsg_len = CMSG_LEN(sizeof(int));
  64. pfd = (int *) CMSG_DATA(cmsg);
  65. msghdr.msg_controllen = cmsg->cmsg_len;
  66. /* first try to tx more pending data */
  67. list_for_each_entry_safe(ubl, ubl2, &cl->tx_queue, list) {
  68. ssize_t written;
  69. ub = ubl->msg;
  70. written = ubus_msg_writev(sock->fd, ub, cl->txq_ofs);
  71. if (written < 0) {
  72. switch(errno) {
  73. case EINTR:
  74. case EAGAIN:
  75. break;
  76. default:
  77. goto disconnect;
  78. }
  79. break;
  80. }
  81. cl->txq_ofs += written;
  82. cl->txq_len -= written;
  83. if (cl->txq_ofs < ub->len + sizeof(ub->hdr))
  84. break;
  85. cl->txq_ofs = 0;
  86. ubus_msg_list_free(ubl);
  87. }
  88. if (list_empty(&cl->tx_queue) && (events & ULOOP_WRITE)) {
  89. /* Process queued commands */
  90. ubus_client_cmd_queue_process(cl);
  91. /* prevent further ULOOP_WRITE events if we don't have data
  92. * to send anymore */
  93. if (list_empty(&cl->tx_queue))
  94. uloop_fd_add(sock, ULOOP_READ | ULOOP_EDGE_TRIGGER);
  95. }
  96. retry:
  97. if (!sock->eof && cl->pending_msg_offset < (int) sizeof(cl->hdrbuf)) {
  98. int offset = cl->pending_msg_offset;
  99. int bytes;
  100. *pfd = -1;
  101. iov.iov_base = ((char *) &cl->hdrbuf) + offset;
  102. iov.iov_len = sizeof(cl->hdrbuf) - offset;
  103. if (cl->pending_msg_fd < 0) {
  104. msghdr.msg_control = fd_buf;
  105. msghdr.msg_controllen = cmsg->cmsg_len;
  106. } else {
  107. msghdr.msg_control = NULL;
  108. msghdr.msg_controllen = 0;
  109. }
  110. bytes = recvmsg(sock->fd, &msghdr, 0);
  111. if (bytes < 0)
  112. goto out;
  113. if (*pfd >= 0)
  114. cl->pending_msg_fd = *pfd;
  115. cl->pending_msg_offset += bytes;
  116. if (cl->pending_msg_offset < (int) sizeof(cl->hdrbuf))
  117. goto out;
  118. if (blob_raw_len(&cl->hdrbuf.data) < sizeof(struct blob_attr))
  119. goto disconnect;
  120. if (blob_pad_len(&cl->hdrbuf.data) > UBUS_MAX_MSGLEN)
  121. goto disconnect;
  122. cl->pending_msg = ubus_msg_new(NULL, blob_raw_len(&cl->hdrbuf.data), false);
  123. if (!cl->pending_msg)
  124. goto disconnect;
  125. cl->hdrbuf.hdr.seq = be16_to_cpu(cl->hdrbuf.hdr.seq);
  126. cl->hdrbuf.hdr.peer = be32_to_cpu(cl->hdrbuf.hdr.peer);
  127. memcpy(&cl->pending_msg->hdr, &cl->hdrbuf.hdr, sizeof(cl->hdrbuf.hdr));
  128. memcpy(cl->pending_msg->data, &cl->hdrbuf.data, sizeof(cl->hdrbuf.data));
  129. }
  130. ub = cl->pending_msg;
  131. if (ub) {
  132. int offset = cl->pending_msg_offset - sizeof(ub->hdr);
  133. int len = blob_raw_len(ub->data) - offset;
  134. int bytes = 0;
  135. if (len > 0) {
  136. bytes = read(sock->fd, (char *) ub->data + offset, len);
  137. if (bytes <= 0)
  138. goto out;
  139. }
  140. if (bytes < len) {
  141. cl->pending_msg_offset += bytes;
  142. goto out;
  143. }
  144. /* accept message */
  145. ub->fd = cl->pending_msg_fd;
  146. cl->pending_msg_fd = -1;
  147. cl->pending_msg_offset = 0;
  148. cl->pending_msg = NULL;
  149. ubusd_monitor_message(cl, ub, false);
  150. ubusd_proto_receive_message(cl, ub);
  151. goto retry;
  152. }
  153. out:
  154. if (!sock->eof || !list_empty(&cl->tx_queue))
  155. return;
  156. disconnect:
  157. handle_client_disconnect(cl);
  158. }
  159. static bool get_next_connection(int fd)
  160. {
  161. struct ubus_client *cl;
  162. int client_fd;
  163. client_fd = accept(fd, NULL, 0);
  164. if (client_fd < 0) {
  165. switch (errno) {
  166. case ECONNABORTED:
  167. case EINTR:
  168. return true;
  169. default:
  170. return false;
  171. }
  172. }
  173. cl = ubusd_proto_new_client(client_fd, client_cb);
  174. if (cl)
  175. uloop_fd_add(&cl->sock, ULOOP_READ | ULOOP_EDGE_TRIGGER);
  176. else
  177. close(client_fd);
  178. return true;
  179. }
  180. static void server_cb(struct uloop_fd *fd, unsigned int events)
  181. {
  182. bool next;
  183. do {
  184. next = get_next_connection(fd->fd);
  185. } while (next);
  186. }
  187. static struct uloop_fd server_fd = {
  188. .cb = server_cb,
  189. };
  190. static int usage(const char *progname)
  191. {
  192. fprintf(stderr, "Usage: %s [<options>]\n"
  193. "Options: \n"
  194. " -A <path>: Set the path to ACL files\n"
  195. " -s <socket>: Set the unix domain socket to listen on\n"
  196. "\n", progname);
  197. return 1;
  198. }
  199. static void sighup_handler(int sig)
  200. {
  201. ubusd_acl_load();
  202. }
  203. static void mkdir_sockdir()
  204. {
  205. char *ubus_sock_dir, *tmp;
  206. ubus_sock_dir = strdup(UBUS_UNIX_SOCKET);
  207. tmp = strrchr(ubus_sock_dir, '/');
  208. if (tmp) {
  209. *tmp = '\0';
  210. mkdir(ubus_sock_dir, 0755);
  211. }
  212. free(ubus_sock_dir);
  213. }
  214. #include <libubox/ulog.h>
  215. int main(int argc, char **argv)
  216. {
  217. const char *ubus_socket = UBUS_UNIX_SOCKET;
  218. int ret = 0;
  219. int ch;
  220. signal(SIGPIPE, SIG_IGN);
  221. signal(SIGHUP, sighup_handler);
  222. ulog_open(ULOG_KMSG | ULOG_SYSLOG, LOG_DAEMON, "ubusd");
  223. openlog("ubusd", LOG_PID, LOG_DAEMON);
  224. uloop_init();
  225. while ((ch = getopt(argc, argv, "A:s:")) != -1) {
  226. switch (ch) {
  227. case 's':
  228. ubus_socket = optarg;
  229. break;
  230. case 'A':
  231. ubusd_acl_dir = optarg;
  232. break;
  233. default:
  234. return usage(argv[0]);
  235. }
  236. }
  237. mkdir_sockdir();
  238. unlink(ubus_socket);
  239. umask(0111);
  240. server_fd.fd = usock(USOCK_UNIX | USOCK_SERVER | USOCK_NONBLOCK, ubus_socket, NULL);
  241. if (server_fd.fd < 0) {
  242. perror("usock");
  243. ret = -1;
  244. goto out;
  245. }
  246. uloop_fd_add(&server_fd, ULOOP_READ | ULOOP_EDGE_TRIGGER);
  247. ubusd_acl_load();
  248. uloop_run();
  249. unlink(ubus_socket);
  250. out:
  251. uloop_done();
  252. return ret;
  253. }