libubus-io.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. #define _GNU_SOURCE
  14. #include <sys/types.h>
  15. #include <sys/uio.h>
  16. #include <sys/socket.h>
  17. #include <unistd.h>
  18. #include <fcntl.h>
  19. #include <poll.h>
  20. #include <libubox/usock.h>
  21. #include <libubox/blob.h>
  22. #include <libubox/blobmsg.h>
  23. #include "libubus.h"
  24. #include "libubus-internal.h"
  25. #define STATIC_IOV(_var) { .iov_base = (char *) &(_var), .iov_len = sizeof(_var) }
  26. #define UBUS_MSGBUF_REDUCTION_INTERVAL 16
  27. static const struct blob_attr_info ubus_policy[UBUS_ATTR_MAX] = {
  28. [UBUS_ATTR_STATUS] = { .type = BLOB_ATTR_INT32 },
  29. [UBUS_ATTR_OBJID] = { .type = BLOB_ATTR_INT32 },
  30. [UBUS_ATTR_OBJPATH] = { .type = BLOB_ATTR_STRING },
  31. [UBUS_ATTR_METHOD] = { .type = BLOB_ATTR_STRING },
  32. [UBUS_ATTR_ACTIVE] = { .type = BLOB_ATTR_INT8 },
  33. [UBUS_ATTR_NO_REPLY] = { .type = BLOB_ATTR_INT8 },
  34. [UBUS_ATTR_SUBSCRIBERS] = { .type = BLOB_ATTR_NESTED },
  35. };
  36. static struct blob_attr *attrbuf[UBUS_ATTR_MAX];
  37. __hidden struct blob_attr **ubus_parse_msg(struct blob_attr *msg)
  38. {
  39. blob_parse(msg, attrbuf, ubus_policy, UBUS_ATTR_MAX);
  40. return attrbuf;
  41. }
  42. static void wait_data(int fd, bool write)
  43. {
  44. struct pollfd pfd = { .fd = fd };
  45. pfd.events = write ? POLLOUT : POLLIN;
  46. poll(&pfd, 1, -1);
  47. }
  48. static int writev_retry(int fd, struct iovec *iov, int iov_len, int sock_fd)
  49. {
  50. static struct {
  51. struct cmsghdr h;
  52. int fd;
  53. } fd_buf = {
  54. .h = {
  55. .cmsg_len = sizeof(fd_buf),
  56. .cmsg_level = SOL_SOCKET,
  57. .cmsg_type = SCM_RIGHTS,
  58. }
  59. };
  60. struct msghdr msghdr = {
  61. .msg_iov = iov,
  62. .msg_iovlen = iov_len,
  63. .msg_control = &fd_buf,
  64. .msg_controllen = sizeof(fd_buf),
  65. };
  66. int len = 0;
  67. do {
  68. int cur_len;
  69. if (sock_fd < 0) {
  70. msghdr.msg_control = NULL;
  71. msghdr.msg_controllen = 0;
  72. } else {
  73. fd_buf.fd = sock_fd;
  74. }
  75. cur_len = sendmsg(fd, &msghdr, 0);
  76. if (cur_len < 0) {
  77. switch(errno) {
  78. case EAGAIN:
  79. wait_data(fd, true);
  80. break;
  81. case EINTR:
  82. break;
  83. default:
  84. return -1;
  85. }
  86. continue;
  87. }
  88. if (len > 0)
  89. sock_fd = -1;
  90. len += cur_len;
  91. while (cur_len >= iov->iov_len) {
  92. cur_len -= iov->iov_len;
  93. iov_len--;
  94. iov++;
  95. if (!iov_len)
  96. return len;
  97. }
  98. iov->iov_base += cur_len;
  99. iov->iov_len -= cur_len;
  100. msghdr.msg_iov = iov;
  101. msghdr.msg_iovlen = iov_len;
  102. } while (1);
  103. /* Should never reach here */
  104. return -1;
  105. }
  106. int __hidden ubus_send_msg(struct ubus_context *ctx, uint32_t seq,
  107. struct blob_attr *msg, int cmd, uint32_t peer, int fd)
  108. {
  109. struct ubus_msghdr hdr;
  110. struct iovec iov[2] = {
  111. STATIC_IOV(hdr)
  112. };
  113. int ret;
  114. hdr.version = 0;
  115. hdr.type = cmd;
  116. hdr.seq = cpu_to_be16(seq);
  117. hdr.peer = cpu_to_be32(peer);
  118. if (!msg) {
  119. blob_buf_init(&b, 0);
  120. msg = b.head;
  121. }
  122. iov[1].iov_base = (char *) msg;
  123. iov[1].iov_len = blob_raw_len(msg);
  124. ret = writev_retry(ctx->sock.fd, iov, ARRAY_SIZE(iov), fd);
  125. if (ret < 0)
  126. ctx->sock.eof = true;
  127. if (fd >= 0)
  128. close(fd);
  129. return ret;
  130. }
  131. static int recv_retry(struct ubus_context *ctx, struct iovec *iov, bool wait, int *recv_fd)
  132. {
  133. int bytes, total = 0;
  134. int fd = ctx->sock.fd;
  135. static struct {
  136. struct cmsghdr h;
  137. int fd;
  138. } fd_buf = {
  139. .h = {
  140. .cmsg_type = SCM_RIGHTS,
  141. .cmsg_level = SOL_SOCKET,
  142. .cmsg_len = sizeof(fd_buf),
  143. },
  144. };
  145. struct msghdr msghdr = {
  146. .msg_iov = iov,
  147. .msg_iovlen = 1,
  148. };
  149. while (iov->iov_len > 0) {
  150. if (recv_fd) {
  151. msghdr.msg_control = &fd_buf;
  152. msghdr.msg_controllen = sizeof(fd_buf);
  153. } else {
  154. msghdr.msg_control = NULL;
  155. msghdr.msg_controllen = 0;
  156. }
  157. fd_buf.fd = -1;
  158. bytes = recvmsg(fd, &msghdr, 0);
  159. if (!bytes)
  160. return -1;
  161. if (bytes < 0) {
  162. bytes = 0;
  163. if (errno == EINTR)
  164. continue;
  165. if (errno != EAGAIN)
  166. return -1;
  167. }
  168. if (!wait && !bytes)
  169. return 0;
  170. if (recv_fd)
  171. *recv_fd = fd_buf.fd;
  172. recv_fd = NULL;
  173. wait = true;
  174. iov->iov_len -= bytes;
  175. iov->iov_base += bytes;
  176. total += bytes;
  177. if (iov->iov_len > 0)
  178. wait_data(fd, false);
  179. }
  180. return total;
  181. }
  182. static bool ubus_validate_hdr(struct ubus_msghdr *hdr)
  183. {
  184. struct blob_attr *data = (struct blob_attr *) (hdr + 1);
  185. if (hdr->version != 0)
  186. return false;
  187. if (blob_raw_len(data) < sizeof(*data))
  188. return false;
  189. if (blob_pad_len(data) > UBUS_MAX_MSGLEN)
  190. return false;
  191. return true;
  192. }
  193. static bool alloc_msg_buf(struct ubus_context *ctx, int len)
  194. {
  195. void *ptr;
  196. int buf_len = ctx->msgbuf_data_len;
  197. int rem;
  198. if (!ctx->msgbuf.data)
  199. buf_len = 0;
  200. rem = (len % UBUS_MSG_CHUNK_SIZE);
  201. if (rem > 0)
  202. len += UBUS_MSG_CHUNK_SIZE - rem;
  203. if (len < buf_len &&
  204. ++ctx->msgbuf_reduction_counter > UBUS_MSGBUF_REDUCTION_INTERVAL) {
  205. ctx->msgbuf_reduction_counter = 0;
  206. buf_len = 0;
  207. }
  208. if (len <= buf_len)
  209. return true;
  210. ptr = realloc(ctx->msgbuf.data, len);
  211. if (!ptr)
  212. return false;
  213. ctx->msgbuf.data = ptr;
  214. ctx->msgbuf_data_len = len;
  215. return true;
  216. }
  217. static bool get_next_msg(struct ubus_context *ctx, int *recv_fd)
  218. {
  219. struct {
  220. struct ubus_msghdr hdr;
  221. struct blob_attr data;
  222. } hdrbuf;
  223. struct iovec iov = STATIC_IOV(hdrbuf);
  224. int len;
  225. int r;
  226. /* receive header + start attribute */
  227. r = recv_retry(ctx, &iov, false, recv_fd);
  228. if (r <= 0) {
  229. if (r < 0)
  230. ctx->sock.eof = true;
  231. return false;
  232. }
  233. hdrbuf.hdr.seq = be16_to_cpu(hdrbuf.hdr.seq);
  234. hdrbuf.hdr.peer = be32_to_cpu(hdrbuf.hdr.peer);
  235. if (!ubus_validate_hdr(&hdrbuf.hdr))
  236. return false;
  237. len = blob_raw_len(&hdrbuf.data);
  238. if (!alloc_msg_buf(ctx, len))
  239. return false;
  240. memcpy(&ctx->msgbuf.hdr, &hdrbuf.hdr, sizeof(hdrbuf.hdr));
  241. memcpy(ctx->msgbuf.data, &hdrbuf.data, sizeof(hdrbuf.data));
  242. iov.iov_base = (char *)ctx->msgbuf.data + sizeof(hdrbuf.data);
  243. iov.iov_len = blob_len(ctx->msgbuf.data);
  244. if (iov.iov_len > 0 &&
  245. recv_retry(ctx, &iov, true, NULL) <= 0)
  246. return false;
  247. return true;
  248. }
  249. void __hidden ubus_handle_data(struct uloop_fd *u, unsigned int events)
  250. {
  251. struct ubus_context *ctx = container_of(u, struct ubus_context, sock);
  252. int recv_fd = -1;
  253. while (get_next_msg(ctx, &recv_fd)) {
  254. ubus_process_msg(ctx, &ctx->msgbuf, recv_fd);
  255. if (uloop_cancelling() || ctx->cancel_poll)
  256. break;
  257. }
  258. if (u->eof)
  259. ctx->connection_lost(ctx);
  260. }
  261. void __hidden ubus_poll_data(struct ubus_context *ctx, int timeout)
  262. {
  263. struct pollfd pfd = {
  264. .fd = ctx->sock.fd,
  265. .events = POLLIN | POLLERR,
  266. };
  267. ctx->cancel_poll = false;
  268. poll(&pfd, 1, timeout ? timeout : -1);
  269. ubus_handle_data(&ctx->sock, ULOOP_READ);
  270. }
  271. static void
  272. ubus_refresh_state(struct ubus_context *ctx)
  273. {
  274. struct ubus_object *obj, *tmp;
  275. struct ubus_object **objs;
  276. int n, i = 0;
  277. /* clear all type IDs, they need to be registered again */
  278. avl_for_each_element(&ctx->objects, obj, avl)
  279. if (obj->type)
  280. obj->type->id = 0;
  281. /* push out all objects again */
  282. objs = alloca(ctx->objects.count * sizeof(*objs));
  283. avl_remove_all_elements(&ctx->objects, obj, avl, tmp) {
  284. objs[i++] = obj;
  285. obj->id = 0;
  286. }
  287. for (n = i, i = 0; i < n; i++)
  288. ubus_add_object(ctx, objs[i]);
  289. }
  290. int ubus_reconnect(struct ubus_context *ctx, const char *path)
  291. {
  292. struct {
  293. struct ubus_msghdr hdr;
  294. struct blob_attr data;
  295. } hdr;
  296. struct blob_attr *buf;
  297. int ret = UBUS_STATUS_UNKNOWN_ERROR;
  298. if (!path)
  299. path = UBUS_UNIX_SOCKET;
  300. if (ctx->sock.fd >= 0) {
  301. if (ctx->sock.registered)
  302. uloop_fd_delete(&ctx->sock);
  303. close(ctx->sock.fd);
  304. }
  305. ctx->sock.eof = false;
  306. ctx->sock.error = false;
  307. ctx->sock.fd = usock(USOCK_UNIX, path, NULL);
  308. if (ctx->sock.fd < 0)
  309. return UBUS_STATUS_CONNECTION_FAILED;
  310. if (read(ctx->sock.fd, &hdr, sizeof(hdr)) != sizeof(hdr))
  311. goto out_close;
  312. if (!ubus_validate_hdr(&hdr.hdr))
  313. goto out_close;
  314. if (hdr.hdr.type != UBUS_MSG_HELLO)
  315. goto out_close;
  316. buf = calloc(1, blob_raw_len(&hdr.data));
  317. if (!buf)
  318. goto out_close;
  319. memcpy(buf, &hdr.data, sizeof(hdr.data));
  320. if (read(ctx->sock.fd, blob_data(buf), blob_len(buf)) != blob_len(buf))
  321. goto out_free;
  322. ctx->local_id = hdr.hdr.peer;
  323. if (!ctx->local_id)
  324. goto out_free;
  325. ret = UBUS_STATUS_OK;
  326. fcntl(ctx->sock.fd, F_SETFL, fcntl(ctx->sock.fd, F_GETFL) | O_NONBLOCK | O_CLOEXEC);
  327. ubus_refresh_state(ctx);
  328. out_free:
  329. free(buf);
  330. out_close:
  331. if (ret)
  332. close(ctx->sock.fd);
  333. return ret;
  334. }