libubus-io.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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, 0);
  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 = seq;
  117. hdr.peer = 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(int fd, struct iovec *iov, bool wait, int *recv_fd)
  132. {
  133. int bytes, total = 0;
  134. static struct {
  135. struct cmsghdr h;
  136. int fd;
  137. } fd_buf = {
  138. .h = {
  139. .cmsg_type = SCM_RIGHTS,
  140. .cmsg_level = SOL_SOCKET,
  141. .cmsg_len = sizeof(fd_buf),
  142. },
  143. };
  144. struct msghdr msghdr = {
  145. .msg_iov = iov,
  146. .msg_iovlen = 1,
  147. };
  148. while (iov->iov_len > 0) {
  149. if (wait)
  150. wait_data(fd, false);
  151. if (recv_fd) {
  152. msghdr.msg_control = &fd_buf;
  153. msghdr.msg_controllen = sizeof(fd_buf);
  154. } else {
  155. msghdr.msg_control = NULL;
  156. msghdr.msg_controllen = 0;
  157. }
  158. fd_buf.fd = -1;
  159. bytes = recvmsg(fd, &msghdr, 0);
  160. if (!bytes)
  161. return -1;
  162. if (bytes < 0) {
  163. bytes = 0;
  164. if (uloop_cancelled)
  165. return 0;
  166. if (errno == EINTR)
  167. continue;
  168. if (errno != EAGAIN)
  169. return -1;
  170. }
  171. if (!wait && !bytes)
  172. return 0;
  173. if (recv_fd)
  174. *recv_fd = fd_buf.fd;
  175. recv_fd = NULL;
  176. wait = true;
  177. iov->iov_len -= bytes;
  178. iov->iov_base += bytes;
  179. total += bytes;
  180. }
  181. return total;
  182. }
  183. static bool ubus_validate_hdr(struct ubus_msghdr *hdr)
  184. {
  185. struct blob_attr *data = (struct blob_attr *) (hdr + 1);
  186. if (hdr->version != 0)
  187. return false;
  188. if (blob_raw_len(data) < sizeof(*data))
  189. return false;
  190. if (blob_pad_len(data) > UBUS_MAX_MSGLEN)
  191. return false;
  192. return true;
  193. }
  194. static bool alloc_msg_buf(struct ubus_context *ctx, int len)
  195. {
  196. void *ptr;
  197. int buf_len = ctx->msgbuf_data_len;
  198. int rem;
  199. if (!ctx->msgbuf.data)
  200. buf_len = 0;
  201. rem = (len % UBUS_MSG_CHUNK_SIZE);
  202. if (rem > 0)
  203. len += UBUS_MSG_CHUNK_SIZE - rem;
  204. if (len < buf_len &&
  205. ++ctx->msgbuf_reduction_counter > UBUS_MSGBUF_REDUCTION_INTERVAL) {
  206. ctx->msgbuf_reduction_counter = 0;
  207. buf_len = 0;
  208. }
  209. if (len <= buf_len)
  210. return true;
  211. ptr = realloc(ctx->msgbuf.data, len);
  212. if (!ptr)
  213. return false;
  214. ctx->msgbuf.data = ptr;
  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->sock.fd, &iov, false, recv_fd);
  228. if (r <= 0) {
  229. if (r < 0)
  230. ctx->sock.eof = true;
  231. return false;
  232. }
  233. if (!ubus_validate_hdr(&hdrbuf.hdr))
  234. return false;
  235. len = blob_raw_len(&hdrbuf.data);
  236. if (!alloc_msg_buf(ctx, len))
  237. return false;
  238. memcpy(&ctx->msgbuf.hdr, &hdrbuf.hdr, sizeof(hdrbuf.hdr));
  239. memcpy(ctx->msgbuf.data, &hdrbuf.data, sizeof(hdrbuf.data));
  240. iov.iov_base = (char *)ctx->msgbuf.data + sizeof(hdrbuf.data);
  241. iov.iov_len = blob_len(ctx->msgbuf.data);
  242. if (iov.iov_len > 0 && !recv_retry(ctx->sock.fd, &iov, true, NULL))
  243. return false;
  244. return true;
  245. }
  246. void __hidden ubus_handle_data(struct uloop_fd *u, unsigned int events)
  247. {
  248. struct ubus_context *ctx = container_of(u, struct ubus_context, sock);
  249. int recv_fd = -1;
  250. while (get_next_msg(ctx, &recv_fd)) {
  251. ubus_process_msg(ctx, &ctx->msgbuf, recv_fd);
  252. if (uloop_cancelled)
  253. break;
  254. }
  255. if (u->eof)
  256. ctx->connection_lost(ctx);
  257. }
  258. void __hidden ubus_poll_data(struct ubus_context *ctx, int timeout)
  259. {
  260. struct pollfd pfd = {
  261. .fd = ctx->sock.fd,
  262. .events = POLLIN | POLLERR,
  263. };
  264. poll(&pfd, 1, timeout);
  265. ubus_handle_data(&ctx->sock, ULOOP_READ);
  266. }
  267. static void
  268. ubus_refresh_state(struct ubus_context *ctx)
  269. {
  270. struct ubus_object *obj, *tmp;
  271. struct ubus_object **objs;
  272. int n, i = 0;
  273. /* clear all type IDs, they need to be registered again */
  274. avl_for_each_element(&ctx->objects, obj, avl)
  275. if (obj->type)
  276. obj->type->id = 0;
  277. /* push out all objects again */
  278. objs = alloca(ctx->objects.count * sizeof(*objs));
  279. avl_remove_all_elements(&ctx->objects, obj, avl, tmp) {
  280. objs[i++] = obj;
  281. obj->id = 0;
  282. }
  283. for (n = i, i = 0; i < n; i++)
  284. ubus_add_object(ctx, objs[i]);
  285. }
  286. int ubus_reconnect(struct ubus_context *ctx, const char *path)
  287. {
  288. struct {
  289. struct ubus_msghdr hdr;
  290. struct blob_attr data;
  291. } hdr;
  292. struct blob_attr *buf;
  293. int ret = UBUS_STATUS_UNKNOWN_ERROR;
  294. if (!path)
  295. path = UBUS_UNIX_SOCKET;
  296. if (ctx->sock.fd >= 0) {
  297. if (ctx->sock.registered)
  298. uloop_fd_delete(&ctx->sock);
  299. close(ctx->sock.fd);
  300. }
  301. ctx->sock.fd = usock(USOCK_UNIX, path, NULL);
  302. if (ctx->sock.fd < 0)
  303. return UBUS_STATUS_CONNECTION_FAILED;
  304. if (read(ctx->sock.fd, &hdr, sizeof(hdr)) != sizeof(hdr))
  305. goto out_close;
  306. if (!ubus_validate_hdr(&hdr.hdr))
  307. goto out_close;
  308. if (hdr.hdr.type != UBUS_MSG_HELLO)
  309. goto out_close;
  310. buf = calloc(1, blob_raw_len(&hdr.data));
  311. if (!buf)
  312. goto out_close;
  313. memcpy(buf, &hdr.data, sizeof(hdr.data));
  314. if (read(ctx->sock.fd, blob_data(buf), blob_len(buf)) != blob_len(buf))
  315. goto out_free;
  316. ctx->local_id = hdr.hdr.peer;
  317. if (!ctx->local_id)
  318. goto out_free;
  319. ret = UBUS_STATUS_OK;
  320. fcntl(ctx->sock.fd, F_SETFL, fcntl(ctx->sock.fd, F_GETFL) | O_NONBLOCK | O_CLOEXEC);
  321. ubus_refresh_state(ctx);
  322. out_free:
  323. free(buf);
  324. out_close:
  325. if (ret)
  326. close(ctx->sock.fd);
  327. return ret;
  328. }