libubus-req.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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 <unistd.h>
  14. #include "libubus.h"
  15. #include "libubus-internal.h"
  16. struct ubus_pending_data {
  17. struct list_head list;
  18. int type;
  19. struct blob_attr data[];
  20. };
  21. static void req_data_cb(struct ubus_request *req, int type, struct blob_attr *data)
  22. {
  23. struct blob_attr **attr;
  24. if (req->raw_data_cb)
  25. req->raw_data_cb(req, type, data);
  26. if (!req->data_cb)
  27. return;
  28. attr = ubus_parse_msg(data, blob_raw_len(data));
  29. if (!attr[UBUS_ATTR_DATA])
  30. return;
  31. req->data_cb(req, type, attr[UBUS_ATTR_DATA]);
  32. }
  33. static void __ubus_process_req_data(struct ubus_request *req)
  34. {
  35. struct ubus_pending_data *data, *tmp;
  36. list_for_each_entry_safe(data, tmp, &req->pending, list) {
  37. list_del(&data->list);
  38. if (!req->cancelled)
  39. req_data_cb(req, data->type, data->data);
  40. free(data);
  41. }
  42. }
  43. int __hidden __ubus_start_request(struct ubus_context *ctx, struct ubus_request *req,
  44. struct blob_attr *msg, int cmd, uint32_t peer)
  45. {
  46. if (msg && blob_pad_len(msg) > UBUS_MAX_MSGLEN)
  47. return -1;
  48. INIT_LIST_HEAD(&req->list);
  49. INIT_LIST_HEAD(&req->pending);
  50. req->ctx = ctx;
  51. req->peer = peer;
  52. req->seq = ++ctx->request_seq;
  53. return ubus_send_msg(ctx, req->seq, msg, cmd, peer, req->fd);
  54. }
  55. int __hidden ubus_start_request(struct ubus_context *ctx, struct ubus_request *req,
  56. struct blob_attr *msg, int cmd, uint32_t peer)
  57. {
  58. memset(req, 0, sizeof(*req));
  59. req->fd = -1;
  60. return __ubus_start_request(ctx, req, msg, cmd, peer);
  61. }
  62. void ubus_abort_request(struct ubus_context *ctx, struct ubus_request *req)
  63. {
  64. if (list_empty(&req->list))
  65. return;
  66. req->cancelled = true;
  67. __ubus_process_req_data(req);
  68. list_del_init(&req->list);
  69. }
  70. void ubus_complete_request_async(struct ubus_context *ctx, struct ubus_request *req)
  71. {
  72. if (!list_empty(&req->list))
  73. return;
  74. list_add(&req->list, &ctx->requests);
  75. }
  76. static void
  77. ubus_req_complete_cb(struct ubus_request *req)
  78. {
  79. ubus_complete_handler_t cb = req->complete_cb;
  80. if (!cb)
  81. return;
  82. req->complete_cb = NULL;
  83. cb(req, req->status_code);
  84. }
  85. static void
  86. ubus_set_req_status(struct ubus_request *req, int ret)
  87. {
  88. if (!list_empty(&req->list))
  89. list_del_init(&req->list);
  90. req->status_msg = true;
  91. req->status_code = ret;
  92. if (!req->blocked)
  93. ubus_req_complete_cb(req);
  94. }
  95. static void ubus_sync_req_cb(struct ubus_request *req, int ret)
  96. {
  97. req->status_msg = true;
  98. req->status_code = ret;
  99. req->ctx->cancel_poll = true;
  100. }
  101. static int64_t get_time_msec(void)
  102. {
  103. struct timespec ts;
  104. int64_t val;
  105. clock_gettime(CLOCK_MONOTONIC, &ts);
  106. val = (int64_t) ts.tv_sec * 1000LL;
  107. val += ts.tv_nsec / 1000000LL;
  108. return val;
  109. }
  110. int ubus_complete_request(struct ubus_context *ctx, struct ubus_request *req,
  111. int req_timeout)
  112. {
  113. ubus_complete_handler_t complete_cb = req->complete_cb;
  114. int status = UBUS_STATUS_NO_DATA;
  115. int64_t timeout = 0, time_end = 0;
  116. if (req_timeout)
  117. time_end = get_time_msec() + req_timeout;
  118. ubus_complete_request_async(ctx, req);
  119. req->complete_cb = ubus_sync_req_cb;
  120. ctx->stack_depth++;
  121. while (!req->status_msg) {
  122. if (req_timeout) {
  123. timeout = time_end - get_time_msec();
  124. if (timeout <= 0) {
  125. ubus_set_req_status(req, UBUS_STATUS_TIMEOUT);
  126. break;
  127. }
  128. }
  129. ubus_poll_data(ctx, (unsigned int) timeout);
  130. if (ctx->sock.eof) {
  131. ubus_set_req_status(req, UBUS_STATUS_CONNECTION_FAILED);
  132. ctx->cancel_poll = true;
  133. break;
  134. }
  135. }
  136. ctx->stack_depth--;
  137. if (ctx->stack_depth)
  138. ctx->cancel_poll = true;
  139. if (req->status_msg)
  140. status = req->status_code;
  141. req->complete_cb = complete_cb;
  142. if (req->complete_cb)
  143. req->complete_cb(req, status);
  144. if (!ctx->stack_depth && !ctx->sock.registered)
  145. ctx->pending_timer.cb(&ctx->pending_timer);
  146. return status;
  147. }
  148. void ubus_complete_deferred_request(struct ubus_context *ctx, struct ubus_request_data *req, int ret)
  149. {
  150. blob_buf_init(&b, 0);
  151. blob_put_int32(&b, UBUS_ATTR_STATUS, ret);
  152. blob_put_int32(&b, UBUS_ATTR_OBJID, req->object);
  153. ubus_send_msg(ctx, req->seq, b.head, UBUS_MSG_STATUS, req->peer, req->fd);
  154. }
  155. static void ubus_put_data(struct blob_buf *buf, struct blob_attr *msg)
  156. {
  157. if (msg)
  158. blob_put(buf, UBUS_ATTR_DATA, blob_data(msg), blob_len(msg));
  159. else
  160. blob_put(buf, UBUS_ATTR_DATA, NULL, 0);
  161. }
  162. int ubus_send_reply(struct ubus_context *ctx, struct ubus_request_data *req,
  163. struct blob_attr *msg)
  164. {
  165. int ret;
  166. blob_buf_init(&b, 0);
  167. blob_put_int32(&b, UBUS_ATTR_OBJID, req->object);
  168. ubus_put_data(&b, msg);
  169. ret = ubus_send_msg(ctx, req->seq, b.head, UBUS_MSG_DATA, req->peer, -1);
  170. if (ret < 0)
  171. return UBUS_STATUS_NO_DATA;
  172. return 0;
  173. }
  174. int ubus_invoke_async_fd(struct ubus_context *ctx, uint32_t obj,
  175. const char *method, struct blob_attr *msg,
  176. struct ubus_request *req, int fd)
  177. {
  178. blob_buf_init(&b, 0);
  179. blob_put_int32(&b, UBUS_ATTR_OBJID, obj);
  180. blob_put_string(&b, UBUS_ATTR_METHOD, method);
  181. ubus_put_data(&b, msg);
  182. memset(req, 0, sizeof(*req));
  183. req->fd = fd;
  184. if (__ubus_start_request(ctx, req, b.head, UBUS_MSG_INVOKE, obj) < 0)
  185. return UBUS_STATUS_INVALID_ARGUMENT;
  186. return 0;
  187. }
  188. int ubus_invoke_fd(struct ubus_context *ctx, uint32_t obj, const char *method,
  189. struct blob_attr *msg, ubus_data_handler_t cb, void *priv,
  190. int timeout, int fd)
  191. {
  192. struct ubus_request req;
  193. int rc;
  194. rc = ubus_invoke_async_fd(ctx, obj, method, msg, &req, fd);
  195. if (rc)
  196. return rc;
  197. req.data_cb = cb;
  198. req.priv = priv;
  199. return ubus_complete_request(ctx, &req, timeout);
  200. }
  201. static void
  202. ubus_notify_complete_cb(struct ubus_request *req, int ret)
  203. {
  204. struct ubus_notify_request *nreq;
  205. nreq = container_of(req, struct ubus_notify_request, req);
  206. if (!nreq->complete_cb)
  207. return;
  208. nreq->complete_cb(nreq, 0, 0);
  209. }
  210. static void
  211. ubus_notify_data_cb(struct ubus_request *req, int type, struct blob_attr *msg)
  212. {
  213. struct ubus_notify_request *nreq;
  214. nreq = container_of(req, struct ubus_notify_request, req);
  215. if (!nreq->data_cb)
  216. return;
  217. nreq->data_cb(nreq, type, msg);
  218. }
  219. static int
  220. __ubus_notify_async(struct ubus_context *ctx, struct ubus_object *obj,
  221. const char *type, struct blob_attr *msg,
  222. struct ubus_notify_request *req, bool reply)
  223. {
  224. memset(req, 0, sizeof(*req));
  225. blob_buf_init(&b, 0);
  226. blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id);
  227. blob_put_string(&b, UBUS_ATTR_METHOD, type);
  228. ubus_put_data(&b, msg);
  229. if (!reply)
  230. blob_put_int8(&b, UBUS_ATTR_NO_REPLY, true);
  231. if (ubus_start_request(ctx, &req->req, b.head, UBUS_MSG_NOTIFY, obj->id) < 0)
  232. return UBUS_STATUS_INVALID_ARGUMENT;
  233. /* wait for status message from ubusd first */
  234. req->req.notify = true;
  235. req->pending = 1;
  236. req->id[0] = obj->id;
  237. req->req.complete_cb = ubus_notify_complete_cb;
  238. req->req.data_cb = ubus_notify_data_cb;
  239. return 0;
  240. }
  241. int ubus_notify_async(struct ubus_context *ctx, struct ubus_object *obj,
  242. const char *type, struct blob_attr *msg,
  243. struct ubus_notify_request *req)
  244. {
  245. return __ubus_notify_async(ctx, obj, type, msg, req, true);
  246. }
  247. int ubus_notify(struct ubus_context *ctx, struct ubus_object *obj,
  248. const char *type, struct blob_attr *msg, int timeout)
  249. {
  250. struct ubus_notify_request req;
  251. int ret;
  252. ret = __ubus_notify_async(ctx, obj, type, msg, &req, timeout >= 0);
  253. if (ret < 0)
  254. return ret;
  255. if (timeout < 0) {
  256. ubus_abort_request(ctx, &req.req);
  257. return 0;
  258. }
  259. return ubus_complete_request(ctx, &req.req, timeout);
  260. }
  261. static bool ubus_get_status(struct ubus_msghdr_buf *buf, int *ret)
  262. {
  263. struct blob_attr **attrbuf = ubus_parse_msg(buf->data, blob_raw_len(buf->data));
  264. if (!attrbuf[UBUS_ATTR_STATUS])
  265. return false;
  266. *ret = blob_get_u32(attrbuf[UBUS_ATTR_STATUS]);
  267. return true;
  268. }
  269. static int
  270. ubus_process_req_status(struct ubus_request *req, struct ubus_msghdr_buf *buf)
  271. {
  272. int ret = UBUS_STATUS_INVALID_ARGUMENT;
  273. ubus_get_status(buf, &ret);
  274. req->peer = buf->hdr.peer;
  275. ubus_set_req_status(req, ret);
  276. return ret;
  277. }
  278. static void
  279. ubus_process_req_data(struct ubus_request *req, struct ubus_msghdr_buf *buf)
  280. {
  281. struct ubus_pending_data *data;
  282. int len;
  283. if (!req->blocked) {
  284. req->blocked = true;
  285. req_data_cb(req, buf->hdr.type, buf->data);
  286. __ubus_process_req_data(req);
  287. req->blocked = false;
  288. if (req->status_msg)
  289. ubus_req_complete_cb(req);
  290. return;
  291. }
  292. len = blob_raw_len(buf->data);
  293. data = calloc(1, sizeof(*data) + len);
  294. if (!data)
  295. return;
  296. data->type = buf->hdr.type;
  297. memcpy(data->data, buf->data, len);
  298. list_add(&data->list, &req->pending);
  299. }
  300. static int
  301. ubus_find_notify_id(struct ubus_notify_request *n, uint32_t objid)
  302. {
  303. uint32_t pending = n->pending;
  304. int i;
  305. for (i = 0; pending; i++, pending >>= 1) {
  306. if (!(pending & 1))
  307. continue;
  308. if (n->id[i] == objid)
  309. return i;
  310. }
  311. return -1;
  312. }
  313. static struct ubus_request *
  314. ubus_find_request(struct ubus_context *ctx, uint32_t seq, uint32_t peer, int *id)
  315. {
  316. struct ubus_request *req;
  317. list_for_each_entry(req, &ctx->requests, list) {
  318. struct ubus_notify_request *nreq;
  319. nreq = container_of(req, struct ubus_notify_request, req);
  320. if (seq != req->seq)
  321. continue;
  322. if (req->notify) {
  323. if (!nreq->pending)
  324. continue;
  325. *id = ubus_find_notify_id(nreq, peer);
  326. if (*id < 0)
  327. continue;
  328. } else if (peer != req->peer)
  329. continue;
  330. return req;
  331. }
  332. return NULL;
  333. }
  334. static void ubus_process_notify_status(struct ubus_request *req, int id, struct ubus_msghdr_buf *buf)
  335. {
  336. struct ubus_notify_request *nreq;
  337. struct blob_attr **tb;
  338. struct blob_attr *cur;
  339. size_t rem;
  340. int idx = 1;
  341. int ret = 0;
  342. nreq = container_of(req, struct ubus_notify_request, req);
  343. nreq->pending &= ~(1 << id);
  344. if (!id) {
  345. /* first id: ubusd's status message with a list of ids */
  346. tb = ubus_parse_msg(buf->data, blob_raw_len(buf->data));
  347. if (tb[UBUS_ATTR_SUBSCRIBERS]) {
  348. blob_for_each_attr(cur, tb[UBUS_ATTR_SUBSCRIBERS], rem) {
  349. if (!blob_check_type(blob_data(cur), blob_len(cur), BLOB_ATTR_INT32))
  350. continue;
  351. nreq->pending |= (1 << idx);
  352. nreq->id[idx] = blob_get_int32(cur);
  353. idx++;
  354. if (idx == UBUS_MAX_NOTIFY_PEERS + 1)
  355. break;
  356. }
  357. }
  358. } else {
  359. ubus_get_status(buf, &ret);
  360. if (nreq->status_cb)
  361. nreq->status_cb(nreq, id, ret);
  362. }
  363. if (!nreq->pending)
  364. ubus_set_req_status(req, 0);
  365. }
  366. void __hidden ubus_process_req_msg(struct ubus_context *ctx, struct ubus_msghdr_buf *buf, int fd)
  367. {
  368. struct ubus_msghdr *hdr = &buf->hdr;
  369. struct ubus_request *req;
  370. int id = -1;
  371. switch(hdr->type) {
  372. case UBUS_MSG_STATUS:
  373. req = ubus_find_request(ctx, hdr->seq, hdr->peer, &id);
  374. if (!req)
  375. break;
  376. if (fd >= 0) {
  377. if (req->fd_cb)
  378. req->fd_cb(req, fd);
  379. else
  380. close(fd);
  381. }
  382. if (id >= 0)
  383. ubus_process_notify_status(req, id, buf);
  384. else
  385. ubus_process_req_status(req, buf);
  386. break;
  387. case UBUS_MSG_DATA:
  388. req = ubus_find_request(ctx, hdr->seq, hdr->peer, &id);
  389. if (req && (req->data_cb || req->raw_data_cb))
  390. ubus_process_req_data(req, buf);
  391. break;
  392. }
  393. }
  394. int __ubus_monitor(struct ubus_context *ctx, const char *type)
  395. {
  396. blob_buf_init(&b, 0);
  397. return ubus_invoke(ctx, UBUS_SYSTEM_OBJECT_MONITOR, type, b.head, NULL, NULL, 1000);
  398. }