libubus.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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/types.h>
  14. #include <sys/socket.h>
  15. #include <unistd.h>
  16. #include <libubox/blob.h>
  17. #include <libubox/blobmsg.h>
  18. #include "libubus.h"
  19. #include "libubus-internal.h"
  20. #include "ubusmsg.h"
  21. const char *__ubus_strerror[__UBUS_STATUS_LAST] = {
  22. [UBUS_STATUS_OK] = "Success",
  23. [UBUS_STATUS_INVALID_COMMAND] = "Invalid command",
  24. [UBUS_STATUS_INVALID_ARGUMENT] = "Invalid argument",
  25. [UBUS_STATUS_METHOD_NOT_FOUND] = "Method not found",
  26. [UBUS_STATUS_NOT_FOUND] = "Not found",
  27. [UBUS_STATUS_NO_DATA] = "No response",
  28. [UBUS_STATUS_PERMISSION_DENIED] = "Permission denied",
  29. [UBUS_STATUS_TIMEOUT] = "Request timed out",
  30. [UBUS_STATUS_NOT_SUPPORTED] = "Operation not supported",
  31. [UBUS_STATUS_UNKNOWN_ERROR] = "Unknown error",
  32. [UBUS_STATUS_CONNECTION_FAILED] = "Connection failed",
  33. [UBUS_STATUS_NO_MEMORY] = "Out of memory",
  34. [UBUS_STATUS_PARSE_ERROR] = "Parsing message data failed",
  35. [UBUS_STATUS_SYSTEM_ERROR] = "System error",
  36. };
  37. struct blob_buf b __hidden = {};
  38. struct ubus_pending_msg {
  39. struct list_head list;
  40. struct ubus_msghdr_buf hdr;
  41. };
  42. static int ubus_cmp_id(const void *k1, const void *k2, void *ptr)
  43. {
  44. const uint32_t *id1 = k1, *id2 = k2;
  45. if (*id1 < *id2)
  46. return -1;
  47. else
  48. return *id1 > *id2;
  49. }
  50. const char *ubus_strerror(int error)
  51. {
  52. static char err[32];
  53. if (error < 0 || error >= __UBUS_STATUS_LAST)
  54. goto out;
  55. if (!__ubus_strerror[error])
  56. goto out;
  57. return __ubus_strerror[error];
  58. out:
  59. sprintf(err, "Unknown error: %d", error);
  60. return err;
  61. }
  62. static void
  63. ubus_queue_msg(struct ubus_context *ctx, struct ubus_msghdr_buf *buf)
  64. {
  65. struct ubus_pending_msg *pending;
  66. void *data;
  67. pending = calloc_a(sizeof(*pending), &data, blob_raw_len(buf->data));
  68. pending->hdr.data = data;
  69. memcpy(&pending->hdr.hdr, &buf->hdr, sizeof(buf->hdr));
  70. memcpy(data, buf->data, blob_raw_len(buf->data));
  71. list_add_tail(&pending->list, &ctx->pending);
  72. if (ctx->sock.registered)
  73. uloop_timeout_set(&ctx->pending_timer, 1);
  74. }
  75. void __hidden
  76. ubus_process_msg(struct ubus_context *ctx, struct ubus_msghdr_buf *buf, int fd)
  77. {
  78. switch(buf->hdr.type) {
  79. case UBUS_MSG_STATUS:
  80. case UBUS_MSG_DATA:
  81. ubus_process_req_msg(ctx, buf, fd);
  82. break;
  83. case UBUS_MSG_INVOKE:
  84. case UBUS_MSG_UNSUBSCRIBE:
  85. case UBUS_MSG_NOTIFY:
  86. if (ctx->stack_depth) {
  87. ubus_queue_msg(ctx, buf);
  88. break;
  89. }
  90. ctx->stack_depth++;
  91. ubus_process_obj_msg(ctx, buf, fd);
  92. ctx->stack_depth--;
  93. break;
  94. case UBUS_MSG_MONITOR:
  95. if (ctx->monitor_cb)
  96. ctx->monitor_cb(ctx, buf->hdr.seq, buf->data);
  97. break;
  98. }
  99. }
  100. static void ubus_process_pending_msg(struct uloop_timeout *timeout)
  101. {
  102. struct ubus_context *ctx = container_of(timeout, struct ubus_context, pending_timer);
  103. struct ubus_pending_msg *pending;
  104. while (!list_empty(&ctx->pending)) {
  105. if (ctx->stack_depth)
  106. break;
  107. pending = list_first_entry(&ctx->pending, struct ubus_pending_msg, list);
  108. list_del(&pending->list);
  109. ubus_process_msg(ctx, &pending->hdr, -1);
  110. free(pending);
  111. }
  112. }
  113. struct ubus_lookup_request {
  114. struct ubus_request req;
  115. ubus_lookup_handler_t cb;
  116. };
  117. static void ubus_lookup_cb(struct ubus_request *ureq, int type, struct blob_attr *msg)
  118. {
  119. struct ubus_lookup_request *req;
  120. struct ubus_object_data obj = {};
  121. struct blob_attr **attr;
  122. req = container_of(ureq, struct ubus_lookup_request, req);
  123. attr = ubus_parse_msg(msg, blob_raw_len(msg));
  124. if (!attr[UBUS_ATTR_OBJID] || !attr[UBUS_ATTR_OBJPATH] ||
  125. !attr[UBUS_ATTR_OBJTYPE])
  126. return;
  127. obj.id = blob_get_u32(attr[UBUS_ATTR_OBJID]);
  128. obj.path = blob_data(attr[UBUS_ATTR_OBJPATH]);
  129. obj.type_id = blob_get_u32(attr[UBUS_ATTR_OBJTYPE]);
  130. obj.signature = attr[UBUS_ATTR_SIGNATURE];
  131. req->cb(ureq->ctx, &obj, ureq->priv);
  132. }
  133. int ubus_lookup(struct ubus_context *ctx, const char *path,
  134. ubus_lookup_handler_t cb, void *priv)
  135. {
  136. struct ubus_lookup_request lookup;
  137. blob_buf_init(&b, 0);
  138. if (path)
  139. blob_put_string(&b, UBUS_ATTR_OBJPATH, path);
  140. if (ubus_start_request(ctx, &lookup.req, b.head, UBUS_MSG_LOOKUP, 0) < 0)
  141. return UBUS_STATUS_INVALID_ARGUMENT;
  142. lookup.req.raw_data_cb = ubus_lookup_cb;
  143. lookup.req.priv = priv;
  144. lookup.cb = cb;
  145. return ubus_complete_request(ctx, &lookup.req, 0);
  146. }
  147. static void ubus_lookup_id_cb(struct ubus_request *req, int type, struct blob_attr *msg)
  148. {
  149. struct blob_attr **attr;
  150. uint32_t *id = req->priv;
  151. attr = ubus_parse_msg(msg, blob_raw_len(msg));
  152. if (!attr[UBUS_ATTR_OBJID])
  153. return;
  154. *id = blob_get_u32(attr[UBUS_ATTR_OBJID]);
  155. }
  156. int ubus_lookup_id(struct ubus_context *ctx, const char *path, uint32_t *id)
  157. {
  158. struct ubus_request req;
  159. blob_buf_init(&b, 0);
  160. if (path)
  161. blob_put_string(&b, UBUS_ATTR_OBJPATH, path);
  162. if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_LOOKUP, 0) < 0)
  163. return UBUS_STATUS_INVALID_ARGUMENT;
  164. req.raw_data_cb = ubus_lookup_id_cb;
  165. req.priv = id;
  166. return ubus_complete_request(ctx, &req, 0);
  167. }
  168. static int ubus_event_cb(struct ubus_context *ctx, struct ubus_object *obj,
  169. struct ubus_request_data *req,
  170. const char *method, struct blob_attr *msg)
  171. {
  172. struct ubus_event_handler *ev;
  173. ev = container_of(obj, struct ubus_event_handler, obj);
  174. ev->cb(ctx, ev, method, msg);
  175. return 0;
  176. }
  177. static const struct ubus_method event_method = {
  178. .name = NULL,
  179. .handler = ubus_event_cb,
  180. };
  181. int ubus_register_event_handler(struct ubus_context *ctx,
  182. struct ubus_event_handler *ev,
  183. const char *pattern)
  184. {
  185. struct ubus_object *obj = &ev->obj;
  186. struct blob_buf b2 = {};
  187. int ret;
  188. if (!obj->id) {
  189. obj->methods = &event_method;
  190. obj->n_methods = 1;
  191. if (!!obj->name ^ !!obj->type)
  192. return UBUS_STATUS_INVALID_ARGUMENT;
  193. ret = ubus_add_object(ctx, obj);
  194. if (ret)
  195. return ret;
  196. }
  197. /* use a second buffer, ubus_invoke() overwrites the primary one */
  198. blob_buf_init(&b2, 0);
  199. blobmsg_add_u32(&b2, "object", obj->id);
  200. if (pattern)
  201. blobmsg_add_string(&b2, "pattern", pattern);
  202. ret = ubus_invoke(ctx, UBUS_SYSTEM_OBJECT_EVENT, "register", b2.head,
  203. NULL, NULL, 0);
  204. blob_buf_free(&b2);
  205. return ret;
  206. }
  207. int ubus_send_event(struct ubus_context *ctx, const char *id,
  208. struct blob_attr *data)
  209. {
  210. struct ubus_request req;
  211. void *s;
  212. blob_buf_init(&b, 0);
  213. blob_put_int32(&b, UBUS_ATTR_OBJID, UBUS_SYSTEM_OBJECT_EVENT);
  214. blob_put_string(&b, UBUS_ATTR_METHOD, "send");
  215. s = blob_nest_start(&b, UBUS_ATTR_DATA);
  216. blobmsg_add_string(&b, "id", id);
  217. blobmsg_add_field(&b, BLOBMSG_TYPE_TABLE, "data", blob_data(data), blob_len(data));
  218. blob_nest_end(&b, s);
  219. if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_INVOKE, UBUS_SYSTEM_OBJECT_EVENT) < 0)
  220. return UBUS_STATUS_INVALID_ARGUMENT;
  221. return ubus_complete_request(ctx, &req, 0);
  222. }
  223. static void ubus_default_connection_lost(struct ubus_context *ctx)
  224. {
  225. if (ctx->sock.registered)
  226. uloop_end();
  227. }
  228. int ubus_connect_ctx(struct ubus_context *ctx, const char *path)
  229. {
  230. uloop_init();
  231. memset(ctx, 0, sizeof(*ctx));
  232. ctx->sock.fd = -1;
  233. ctx->sock.cb = ubus_handle_data;
  234. ctx->connection_lost = ubus_default_connection_lost;
  235. ctx->pending_timer.cb = ubus_process_pending_msg;
  236. ctx->msgbuf.data = calloc(1, UBUS_MSG_CHUNK_SIZE);
  237. if (!ctx->msgbuf.data)
  238. return -1;
  239. ctx->msgbuf_data_len = UBUS_MSG_CHUNK_SIZE;
  240. INIT_LIST_HEAD(&ctx->requests);
  241. INIT_LIST_HEAD(&ctx->pending);
  242. INIT_LIST_HEAD(&ctx->auto_subscribers);
  243. avl_init(&ctx->objects, ubus_cmp_id, false, NULL);
  244. if (ubus_reconnect(ctx, path)) {
  245. free(ctx->msgbuf.data);
  246. ctx->msgbuf.data = NULL;
  247. return -1;
  248. }
  249. return 0;
  250. }
  251. static void ubus_auto_reconnect_cb(struct uloop_timeout *timeout)
  252. {
  253. struct ubus_auto_conn *conn = container_of(timeout, struct ubus_auto_conn, timer);
  254. if (!ubus_reconnect(&conn->ctx, conn->path))
  255. ubus_add_uloop(&conn->ctx);
  256. else
  257. uloop_timeout_set(timeout, 1000);
  258. }
  259. static void ubus_auto_disconnect_cb(struct ubus_context *ctx)
  260. {
  261. struct ubus_auto_conn *conn = container_of(ctx, struct ubus_auto_conn, ctx);
  262. conn->timer.cb = ubus_auto_reconnect_cb;
  263. uloop_timeout_set(&conn->timer, 1000);
  264. }
  265. static void ubus_auto_connect_cb(struct uloop_timeout *timeout)
  266. {
  267. struct ubus_auto_conn *conn = container_of(timeout, struct ubus_auto_conn, timer);
  268. if (ubus_connect_ctx(&conn->ctx, conn->path)) {
  269. uloop_timeout_set(timeout, 1000);
  270. fprintf(stderr, "failed to connect to ubus\n");
  271. return;
  272. }
  273. conn->ctx.connection_lost = ubus_auto_disconnect_cb;
  274. if (conn->cb)
  275. conn->cb(&conn->ctx);
  276. ubus_add_uloop(&conn->ctx);
  277. }
  278. void ubus_auto_connect(struct ubus_auto_conn *conn)
  279. {
  280. conn->timer.cb = ubus_auto_connect_cb;
  281. ubus_auto_connect_cb(&conn->timer);
  282. }
  283. struct ubus_context *ubus_connect(const char *path)
  284. {
  285. struct ubus_context *ctx;
  286. ctx = calloc(1, sizeof(*ctx));
  287. if (!ctx)
  288. return NULL;
  289. if (ubus_connect_ctx(ctx, path)) {
  290. free(ctx);
  291. ctx = NULL;
  292. }
  293. return ctx;
  294. }
  295. void ubus_shutdown(struct ubus_context *ctx)
  296. {
  297. blob_buf_free(&b);
  298. if (!ctx)
  299. return;
  300. uloop_fd_delete(&ctx->sock);
  301. close(ctx->sock.fd);
  302. uloop_timeout_cancel(&ctx->pending_timer);
  303. free(ctx->msgbuf.data);
  304. }
  305. void ubus_free(struct ubus_context *ctx)
  306. {
  307. ubus_shutdown(ctx);
  308. free(ctx);
  309. }