libubus-req.c 11 KB

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