libubus.c 8.9 KB

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