ubusd.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * Copyright (C) 2011-2014 Felix Fietkau <nbd@openwrt.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License version 2.1
  6. * as published by the Free Software Foundation
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <sys/socket.h>
  14. #include <sys/stat.h>
  15. #include <sys/uio.h>
  16. #ifdef FreeBSD
  17. #include <sys/param.h>
  18. #endif
  19. #include <syslog.h>
  20. #include <signal.h>
  21. #include <stdio.h>
  22. #include <unistd.h>
  23. #include <fcntl.h>
  24. #include <libubox/blob.h>
  25. #include <libubox/uloop.h>
  26. #include <libubox/usock.h>
  27. #include <libubox/list.h>
  28. #include "ubusd.h"
  29. static struct ubus_msg_buf *ubus_msg_ref(struct ubus_msg_buf *ub)
  30. {
  31. if (ub->refcount == ~0)
  32. return ubus_msg_new(ub->data, ub->len, false);
  33. ub->refcount++;
  34. return ub;
  35. }
  36. struct ubus_msg_buf *ubus_msg_new(void *data, int len, bool shared)
  37. {
  38. struct ubus_msg_buf *ub;
  39. int buflen = sizeof(*ub);
  40. if (!shared)
  41. buflen += len;
  42. ub = calloc(1, buflen);
  43. if (!ub)
  44. return NULL;
  45. ub->fd = -1;
  46. if (shared) {
  47. ub->refcount = ~0;
  48. ub->data = data;
  49. } else {
  50. ub->refcount = 1;
  51. ub->data = (void *) (ub + 1);
  52. if (data)
  53. memcpy(ub + 1, data, len);
  54. }
  55. ub->len = len;
  56. return ub;
  57. }
  58. void ubus_msg_free(struct ubus_msg_buf *ub)
  59. {
  60. switch (ub->refcount) {
  61. case 1:
  62. case ~0:
  63. if (ub->fd >= 0)
  64. close(ub->fd);
  65. free(ub);
  66. break;
  67. default:
  68. ub->refcount--;
  69. break;
  70. }
  71. }
  72. static int ubus_msg_writev(int fd, struct ubus_msg_buf *ub, int offset)
  73. {
  74. static struct iovec iov[2];
  75. static struct {
  76. struct cmsghdr h;
  77. int fd;
  78. } fd_buf = {
  79. .h = {
  80. .cmsg_len = sizeof(fd_buf),
  81. .cmsg_level = SOL_SOCKET,
  82. .cmsg_type = SCM_RIGHTS,
  83. },
  84. };
  85. struct msghdr msghdr = {
  86. .msg_iov = iov,
  87. .msg_iovlen = ARRAY_SIZE(iov),
  88. .msg_control = &fd_buf,
  89. .msg_controllen = sizeof(fd_buf),
  90. };
  91. fd_buf.fd = ub->fd;
  92. if (ub->fd < 0) {
  93. msghdr.msg_control = NULL;
  94. msghdr.msg_controllen = 0;
  95. }
  96. if (offset < sizeof(ub->hdr)) {
  97. iov[0].iov_base = ((char *) &ub->hdr) + offset;
  98. iov[0].iov_len = sizeof(ub->hdr) - offset;
  99. iov[1].iov_base = (char *) ub->data;
  100. iov[1].iov_len = ub->len;
  101. return sendmsg(fd, &msghdr, 0);
  102. } else {
  103. offset -= sizeof(ub->hdr);
  104. return write(fd, ((char *) ub->data) + offset, ub->len - offset);
  105. }
  106. }
  107. static void ubus_msg_enqueue(struct ubus_client *cl, struct ubus_msg_buf *ub)
  108. {
  109. if (cl->tx_queue[cl->txq_tail])
  110. return;
  111. cl->tx_queue[cl->txq_tail] = ubus_msg_ref(ub);
  112. cl->txq_tail = (cl->txq_tail + 1) % ARRAY_SIZE(cl->tx_queue);
  113. }
  114. /* takes the msgbuf reference */
  115. void ubus_msg_send(struct ubus_client *cl, struct ubus_msg_buf *ub, bool free)
  116. {
  117. int written;
  118. if (ub->hdr.type != UBUS_MSG_MONITOR)
  119. ubusd_monitor_message(cl, ub, true);
  120. if (!cl->tx_queue[cl->txq_cur]) {
  121. written = ubus_msg_writev(cl->sock.fd, ub, 0);
  122. if (written >= ub->len + sizeof(ub->hdr))
  123. goto out;
  124. if (written < 0)
  125. written = 0;
  126. cl->txq_ofs = written;
  127. /* get an event once we can write to the socket again */
  128. uloop_fd_add(&cl->sock, ULOOP_READ | ULOOP_WRITE | ULOOP_EDGE_TRIGGER);
  129. }
  130. ubus_msg_enqueue(cl, ub);
  131. out:
  132. if (free)
  133. ubus_msg_free(ub);
  134. }
  135. static struct ubus_msg_buf *ubus_msg_head(struct ubus_client *cl)
  136. {
  137. return cl->tx_queue[cl->txq_cur];
  138. }
  139. static void ubus_msg_dequeue(struct ubus_client *cl)
  140. {
  141. struct ubus_msg_buf *ub = ubus_msg_head(cl);
  142. if (!ub)
  143. return;
  144. ubus_msg_free(ub);
  145. cl->txq_ofs = 0;
  146. cl->tx_queue[cl->txq_cur] = NULL;
  147. cl->txq_cur = (cl->txq_cur + 1) % ARRAY_SIZE(cl->tx_queue);
  148. }
  149. static void handle_client_disconnect(struct ubus_client *cl)
  150. {
  151. while (ubus_msg_head(cl))
  152. ubus_msg_dequeue(cl);
  153. ubusd_monitor_disconnect(cl);
  154. ubusd_proto_free_client(cl);
  155. if (cl->pending_msg_fd >= 0)
  156. close(cl->pending_msg_fd);
  157. uloop_fd_delete(&cl->sock);
  158. close(cl->sock.fd);
  159. free(cl);
  160. }
  161. static void client_cb(struct uloop_fd *sock, unsigned int events)
  162. {
  163. struct ubus_client *cl = container_of(sock, struct ubus_client, sock);
  164. struct ubus_msg_buf *ub;
  165. static struct iovec iov;
  166. static struct {
  167. struct cmsghdr h;
  168. int fd;
  169. } fd_buf = {
  170. .h = {
  171. .cmsg_type = SCM_RIGHTS,
  172. .cmsg_level = SOL_SOCKET,
  173. .cmsg_len = sizeof(fd_buf),
  174. }
  175. };
  176. struct msghdr msghdr = {
  177. .msg_iov = &iov,
  178. .msg_iovlen = 1,
  179. };
  180. /* first try to tx more pending data */
  181. while ((ub = ubus_msg_head(cl))) {
  182. int written;
  183. written = ubus_msg_writev(sock->fd, ub, cl->txq_ofs);
  184. if (written < 0) {
  185. switch(errno) {
  186. case EINTR:
  187. case EAGAIN:
  188. break;
  189. default:
  190. goto disconnect;
  191. }
  192. break;
  193. }
  194. cl->txq_ofs += written;
  195. if (cl->txq_ofs < ub->len + sizeof(ub->hdr))
  196. break;
  197. ubus_msg_dequeue(cl);
  198. }
  199. /* prevent further ULOOP_WRITE events if we don't have data
  200. * to send anymore */
  201. if (!ubus_msg_head(cl) && (events & ULOOP_WRITE))
  202. uloop_fd_add(sock, ULOOP_READ | ULOOP_EDGE_TRIGGER);
  203. retry:
  204. if (!sock->eof && cl->pending_msg_offset < sizeof(cl->hdrbuf)) {
  205. int offset = cl->pending_msg_offset;
  206. int bytes;
  207. fd_buf.fd = -1;
  208. iov.iov_base = ((char *) &cl->hdrbuf) + offset;
  209. iov.iov_len = sizeof(cl->hdrbuf) - offset;
  210. if (cl->pending_msg_fd < 0) {
  211. msghdr.msg_control = &fd_buf;
  212. msghdr.msg_controllen = sizeof(fd_buf);
  213. } else {
  214. msghdr.msg_control = NULL;
  215. msghdr.msg_controllen = 0;
  216. }
  217. bytes = recvmsg(sock->fd, &msghdr, 0);
  218. if (bytes < 0)
  219. goto out;
  220. if (fd_buf.fd >= 0)
  221. cl->pending_msg_fd = fd_buf.fd;
  222. cl->pending_msg_offset += bytes;
  223. if (cl->pending_msg_offset < sizeof(cl->hdrbuf))
  224. goto out;
  225. if (blob_pad_len(&cl->hdrbuf.data) > UBUS_MAX_MSGLEN)
  226. goto disconnect;
  227. cl->pending_msg = ubus_msg_new(NULL, blob_raw_len(&cl->hdrbuf.data), false);
  228. if (!cl->pending_msg)
  229. goto disconnect;
  230. memcpy(&cl->pending_msg->hdr, &cl->hdrbuf.hdr, sizeof(cl->hdrbuf.hdr));
  231. memcpy(cl->pending_msg->data, &cl->hdrbuf.data, sizeof(cl->hdrbuf.data));
  232. }
  233. ub = cl->pending_msg;
  234. if (ub) {
  235. int offset = cl->pending_msg_offset - sizeof(ub->hdr);
  236. int len = blob_raw_len(ub->data) - offset;
  237. int bytes = 0;
  238. if (len > 0) {
  239. bytes = read(sock->fd, (char *) ub->data + offset, len);
  240. if (bytes <= 0)
  241. goto out;
  242. }
  243. if (bytes < len) {
  244. cl->pending_msg_offset += bytes;
  245. goto out;
  246. }
  247. /* accept message */
  248. ub->fd = cl->pending_msg_fd;
  249. cl->pending_msg_fd = -1;
  250. cl->pending_msg_offset = 0;
  251. cl->pending_msg = NULL;
  252. ubusd_monitor_message(cl, ub, false);
  253. ubusd_proto_receive_message(cl, ub);
  254. goto retry;
  255. }
  256. out:
  257. if (!sock->eof || ubus_msg_head(cl))
  258. return;
  259. disconnect:
  260. handle_client_disconnect(cl);
  261. }
  262. static bool get_next_connection(int fd)
  263. {
  264. struct ubus_client *cl;
  265. int client_fd;
  266. client_fd = accept(fd, NULL, 0);
  267. if (client_fd < 0) {
  268. switch (errno) {
  269. case ECONNABORTED:
  270. case EINTR:
  271. return true;
  272. default:
  273. return false;
  274. }
  275. }
  276. cl = ubusd_proto_new_client(client_fd, client_cb);
  277. if (cl)
  278. uloop_fd_add(&cl->sock, ULOOP_READ | ULOOP_EDGE_TRIGGER);
  279. else
  280. close(client_fd);
  281. return true;
  282. }
  283. static void server_cb(struct uloop_fd *fd, unsigned int events)
  284. {
  285. bool next;
  286. do {
  287. next = get_next_connection(fd->fd);
  288. } while (next);
  289. }
  290. static struct uloop_fd server_fd = {
  291. .cb = server_cb,
  292. };
  293. static int usage(const char *progname)
  294. {
  295. fprintf(stderr, "Usage: %s [<options>]\n"
  296. "Options: \n"
  297. " -s <socket>: Set the unix domain socket to listen on\n"
  298. "\n", progname);
  299. return 1;
  300. }
  301. static void sighup_handler(int sig)
  302. {
  303. ubusd_acl_load();
  304. }
  305. int main(int argc, char **argv)
  306. {
  307. const char *ubus_socket = UBUS_UNIX_SOCKET;
  308. int ret = 0;
  309. int ch;
  310. signal(SIGPIPE, SIG_IGN);
  311. signal(SIGHUP, sighup_handler);
  312. openlog("ubusd", LOG_PID, LOG_DAEMON);
  313. uloop_init();
  314. while ((ch = getopt(argc, argv, "s:")) != -1) {
  315. switch (ch) {
  316. case 's':
  317. ubus_socket = optarg;
  318. break;
  319. default:
  320. return usage(argv[0]);
  321. }
  322. }
  323. unlink(ubus_socket);
  324. umask(0111);
  325. server_fd.fd = usock(USOCK_UNIX | USOCK_SERVER | USOCK_NONBLOCK, ubus_socket, NULL);
  326. if (server_fd.fd < 0) {
  327. perror("usock");
  328. ret = -1;
  329. goto out;
  330. }
  331. uloop_fd_add(&server_fd, ULOOP_READ | ULOOP_EDGE_TRIGGER);
  332. ubusd_acl_load();
  333. uloop_run();
  334. unlink(ubus_socket);
  335. out:
  336. uloop_done();
  337. return ret;
  338. }