ubusd_proto.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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 <arpa/inet.h>
  14. #include <unistd.h>
  15. #include "ubusd.h"
  16. struct blob_buf b;
  17. static struct avl_tree clients;
  18. static struct blob_attr *attrbuf[UBUS_ATTR_MAX];
  19. typedef int (*ubus_cmd_cb)(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr);
  20. static const struct blob_attr_info ubus_policy[UBUS_ATTR_MAX] = {
  21. [UBUS_ATTR_SIGNATURE] = { .type = BLOB_ATTR_NESTED },
  22. [UBUS_ATTR_OBJTYPE] = { .type = BLOB_ATTR_INT32 },
  23. [UBUS_ATTR_OBJPATH] = { .type = BLOB_ATTR_STRING },
  24. [UBUS_ATTR_OBJID] = { .type = BLOB_ATTR_INT32 },
  25. [UBUS_ATTR_STATUS] = { .type = BLOB_ATTR_INT32 },
  26. [UBUS_ATTR_METHOD] = { .type = BLOB_ATTR_STRING },
  27. [UBUS_ATTR_USER] = { .type = BLOB_ATTR_STRING },
  28. [UBUS_ATTR_GROUP] = { .type = BLOB_ATTR_STRING },
  29. };
  30. struct blob_attr **ubus_parse_msg(struct blob_attr *msg, size_t len)
  31. {
  32. blob_parse_untrusted(msg, len, attrbuf, ubus_policy, UBUS_ATTR_MAX);
  33. return attrbuf;
  34. }
  35. static void ubus_msg_close_fd(struct ubus_msg_buf *ub)
  36. {
  37. if (ub->fd < 0)
  38. return;
  39. close(ub->fd);
  40. ub->fd = -1;
  41. }
  42. static void ubus_msg_init(struct ubus_msg_buf *ub, uint8_t type, uint16_t seq, uint32_t peer)
  43. {
  44. ub->hdr.version = 0;
  45. ub->hdr.type = type;
  46. ub->hdr.seq = seq;
  47. ub->hdr.peer = peer;
  48. }
  49. static struct ubus_msg_buf *ubus_msg_from_blob(bool shared)
  50. {
  51. return ubus_msg_new(b.head, blob_raw_len(b.head), shared);
  52. }
  53. static struct ubus_msg_buf *ubus_reply_from_blob(struct ubus_msg_buf *ub, bool shared)
  54. {
  55. struct ubus_msg_buf *new;
  56. new = ubus_msg_from_blob(shared);
  57. if (!new)
  58. return NULL;
  59. ubus_msg_init(new, UBUS_MSG_DATA, ub->hdr.seq, ub->hdr.peer);
  60. return new;
  61. }
  62. void
  63. ubus_proto_send_msg_from_blob(struct ubus_client *cl, struct ubus_msg_buf *ub,
  64. uint8_t type)
  65. {
  66. /* keep the fd to be passed if it is UBUS_MSG_INVOKE */
  67. int fd = ub->fd;
  68. ub = ubus_reply_from_blob(ub, true);
  69. if (!ub)
  70. return;
  71. ub->hdr.type = type;
  72. ub->fd = fd;
  73. ubus_msg_send(cl, ub);
  74. ubus_msg_free(ub);
  75. }
  76. static bool ubusd_send_hello(struct ubus_client *cl)
  77. {
  78. struct ubus_msg_buf *ub;
  79. blob_buf_init(&b, 0);
  80. ub = ubus_msg_from_blob(true);
  81. if (!ub)
  82. return false;
  83. ubus_msg_init(ub, UBUS_MSG_HELLO, 0, cl->id.id);
  84. ubus_msg_send(cl, ub);
  85. ubus_msg_free(ub);
  86. return true;
  87. }
  88. static int ubusd_send_pong(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
  89. {
  90. ub->hdr.type = UBUS_MSG_DATA;
  91. ubus_msg_send(cl, ub);
  92. return 0;
  93. }
  94. static int ubusd_handle_remove_object(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
  95. {
  96. struct ubus_object *obj;
  97. if (!attr[UBUS_ATTR_OBJID])
  98. return UBUS_STATUS_INVALID_ARGUMENT;
  99. obj = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_OBJID]));
  100. if (!obj)
  101. return UBUS_STATUS_NOT_FOUND;
  102. if (obj->client != cl)
  103. return UBUS_STATUS_PERMISSION_DENIED;
  104. blob_buf_init(&b, 0);
  105. blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
  106. /* check if we're removing the object type as well */
  107. if (obj->type && obj->type->refcount == 1)
  108. blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id.id);
  109. ubus_proto_send_msg_from_blob(cl, ub, UBUS_MSG_DATA);
  110. ubusd_free_object(obj);
  111. return 0;
  112. }
  113. static int ubusd_handle_add_object(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
  114. {
  115. struct ubus_object *obj;
  116. obj = ubusd_create_object(cl, attr);
  117. if (!obj)
  118. return UBUS_STATUS_INVALID_ARGUMENT;
  119. blob_buf_init(&b, 0);
  120. blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
  121. if (attr[UBUS_ATTR_SIGNATURE] && obj->type)
  122. blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id.id);
  123. ubus_proto_send_msg_from_blob(cl, ub, UBUS_MSG_DATA);
  124. return 0;
  125. }
  126. static void ubusd_send_obj(struct ubus_client *cl, struct ubus_msg_buf *ub, struct ubus_object *obj)
  127. {
  128. struct ubus_method *m;
  129. int all_cnt = 0, cnt = 0;
  130. void *s;
  131. if (!obj->type)
  132. return;
  133. blob_buf_init(&b, 0);
  134. blob_put_string(&b, UBUS_ATTR_OBJPATH, obj->path.key);
  135. blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
  136. blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id.id);
  137. s = blob_nest_start(&b, UBUS_ATTR_SIGNATURE);
  138. list_for_each_entry(m, &obj->type->methods, list) {
  139. all_cnt++;
  140. if (!ubusd_acl_check(cl, obj->path.key, blobmsg_name(m->data), UBUS_ACL_ACCESS)) {
  141. blobmsg_add_blob(&b, m->data);
  142. cnt++;
  143. }
  144. }
  145. blob_nest_end(&b, s);
  146. if (cnt || !all_cnt)
  147. ubus_proto_send_msg_from_blob(cl, ub, UBUS_MSG_DATA);
  148. }
  149. static int ubus_client_cmd_queue_add(struct ubus_client *cl,
  150. struct ubus_msg_buf *msg,
  151. struct ubus_object *obj)
  152. {
  153. struct ubus_client_cmd *cmd = malloc(sizeof(*cmd));
  154. if (cmd) {
  155. cmd->msg = msg;
  156. cmd->obj = obj;
  157. list_add_tail(&cmd->list, &cl->cmd_queue);
  158. return -2;
  159. }
  160. return UBUS_STATUS_UNKNOWN_ERROR;
  161. }
  162. static int __ubusd_handle_lookup(struct ubus_client *cl,
  163. struct ubus_msg_buf *ub,
  164. struct blob_attr **attr,
  165. struct ubus_client_cmd *cmd)
  166. {
  167. struct ubus_object *obj = NULL;
  168. char *objpath;
  169. bool found = false;
  170. int len;
  171. if (!attr[UBUS_ATTR_OBJPATH]) {
  172. if (cmd)
  173. obj = cmd->obj;
  174. /* Start from beginning or continue from the last object */
  175. if (obj == NULL)
  176. obj = avl_first_element(&path, obj, path);
  177. avl_for_element_range(obj, avl_last_element(&path, obj, path), obj, path) {
  178. /* Keep sending objects until buffering starts */
  179. if (list_empty(&cl->tx_queue)) {
  180. ubusd_send_obj(cl, ub, obj);
  181. } else {
  182. /* Queue command and continue on the next call */
  183. int ret;
  184. if (cmd == NULL) {
  185. ret = ubus_client_cmd_queue_add(cl, ub, obj);
  186. } else {
  187. cmd->obj = obj;
  188. ret = -2;
  189. }
  190. return ret;
  191. }
  192. }
  193. return 0;
  194. }
  195. objpath = blob_data(attr[UBUS_ATTR_OBJPATH]);
  196. len = strlen(objpath);
  197. if (objpath[len - 1] != '*') {
  198. obj = avl_find_element(&path, objpath, obj, path);
  199. if (!obj)
  200. return UBUS_STATUS_NOT_FOUND;
  201. ubusd_send_obj(cl, ub, obj);
  202. return 0;
  203. }
  204. objpath[--len] = 0;
  205. obj = avl_find_ge_element(&path, objpath, obj, path);
  206. if (!obj)
  207. return UBUS_STATUS_NOT_FOUND;
  208. while (!strncmp(objpath, obj->path.key, len)) {
  209. found = true;
  210. ubusd_send_obj(cl, ub, obj);
  211. if (obj == avl_last_element(&path, obj, path))
  212. break;
  213. obj = avl_next_element(obj, path);
  214. }
  215. if (!found)
  216. return UBUS_STATUS_NOT_FOUND;
  217. return 0;
  218. }
  219. static int ubusd_handle_lookup(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
  220. {
  221. int rc;
  222. if (list_empty(&cl->tx_queue))
  223. rc = __ubusd_handle_lookup(cl, ub, attr, NULL);
  224. else
  225. rc = ubus_client_cmd_queue_add(cl, ub, NULL);
  226. return rc;
  227. }
  228. int ubusd_cmd_lookup(struct ubus_client *cl, struct ubus_client_cmd *cmd)
  229. {
  230. struct ubus_msg_buf *ub = cmd->msg;
  231. struct blob_attr **attr;
  232. int ret;
  233. attr = ubus_parse_msg(ub->data, blob_raw_len(ub->data));
  234. ret = __ubusd_handle_lookup(cl, ub, attr, cmd);
  235. if (ret != -2) {
  236. struct ubus_msg_buf *retmsg = cl->retmsg;
  237. int *retmsg_data = blob_data(blob_data(retmsg->data));
  238. retmsg->hdr.seq = ub->hdr.seq;
  239. retmsg->hdr.peer = ub->hdr.peer;
  240. *retmsg_data = htonl(ret);
  241. ubus_msg_send(cl, retmsg);
  242. }
  243. return ret;
  244. }
  245. static void
  246. ubusd_forward_invoke(struct ubus_client *cl, struct ubus_object *obj,
  247. const char *method, struct ubus_msg_buf *ub,
  248. struct blob_attr *data)
  249. {
  250. blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
  251. blob_put_string(&b, UBUS_ATTR_METHOD, method);
  252. if (cl->user)
  253. blob_put_string(&b, UBUS_ATTR_USER, cl->user);
  254. if (cl->group)
  255. blob_put_string(&b, UBUS_ATTR_GROUP, cl->group);
  256. if (data)
  257. blob_put(&b, UBUS_ATTR_DATA, blob_data(data), blob_len(data));
  258. ubus_proto_send_msg_from_blob(obj->client, ub, UBUS_MSG_INVOKE);
  259. }
  260. static int ubusd_handle_invoke(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
  261. {
  262. struct ubus_object *obj = NULL;
  263. struct ubus_id *id;
  264. const char *method;
  265. if (!attr[UBUS_ATTR_METHOD] || !attr[UBUS_ATTR_OBJID])
  266. return UBUS_STATUS_INVALID_ARGUMENT;
  267. id = ubus_find_id(&objects, blob_get_u32(attr[UBUS_ATTR_OBJID]));
  268. if (!id)
  269. return UBUS_STATUS_NOT_FOUND;
  270. obj = container_of(id, struct ubus_object, id);
  271. method = blob_data(attr[UBUS_ATTR_METHOD]);
  272. if (ubusd_acl_check(cl, obj->path.key, method, UBUS_ACL_ACCESS))
  273. return UBUS_STATUS_PERMISSION_DENIED;
  274. if (!obj->client)
  275. return obj->recv_msg(cl, ub, method, attr[UBUS_ATTR_DATA]);
  276. ub->hdr.peer = cl->id.id;
  277. blob_buf_init(&b, 0);
  278. ubusd_forward_invoke(cl, obj, method, ub, attr[UBUS_ATTR_DATA]);
  279. return -1;
  280. }
  281. static int ubusd_handle_notify(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
  282. {
  283. struct ubus_object *obj = NULL;
  284. struct ubus_subscription *s;
  285. struct ubus_id *id;
  286. const char *method;
  287. bool no_reply = false;
  288. void *c;
  289. if (!attr[UBUS_ATTR_METHOD] || !attr[UBUS_ATTR_OBJID])
  290. return UBUS_STATUS_INVALID_ARGUMENT;
  291. if (attr[UBUS_ATTR_NO_REPLY])
  292. no_reply = blob_get_int8(attr[UBUS_ATTR_NO_REPLY]);
  293. id = ubus_find_id(&objects, blob_get_u32(attr[UBUS_ATTR_OBJID]));
  294. if (!id)
  295. return UBUS_STATUS_NOT_FOUND;
  296. obj = container_of(id, struct ubus_object, id);
  297. if (obj->client != cl)
  298. return UBUS_STATUS_PERMISSION_DENIED;
  299. if (!no_reply) {
  300. blob_buf_init(&b, 0);
  301. blob_put_int32(&b, UBUS_ATTR_OBJID, id->id);
  302. c = blob_nest_start(&b, UBUS_ATTR_SUBSCRIBERS);
  303. list_for_each_entry(s, &obj->subscribers, list) {
  304. blob_put_int32(&b, 0, s->subscriber->id.id);
  305. }
  306. blob_nest_end(&b, c);
  307. blob_put_int32(&b, UBUS_ATTR_STATUS, 0);
  308. ubus_proto_send_msg_from_blob(cl, ub, UBUS_MSG_STATUS);
  309. }
  310. ub->hdr.peer = cl->id.id;
  311. method = blob_data(attr[UBUS_ATTR_METHOD]);
  312. list_for_each_entry(s, &obj->subscribers, list) {
  313. blob_buf_init(&b, 0);
  314. if (no_reply)
  315. blob_put_int8(&b, UBUS_ATTR_NO_REPLY, 1);
  316. ubusd_forward_invoke(cl, s->subscriber, method, ub, attr[UBUS_ATTR_DATA]);
  317. }
  318. return -1;
  319. }
  320. static struct ubus_client *ubusd_get_client_by_id(uint32_t id)
  321. {
  322. struct ubus_id *clid;
  323. clid = ubus_find_id(&clients, id);
  324. if (!clid)
  325. return NULL;
  326. return container_of(clid, struct ubus_client, id);
  327. }
  328. static int ubusd_handle_response(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
  329. {
  330. struct ubus_object *obj;
  331. if (!attr[UBUS_ATTR_OBJID] ||
  332. (ub->hdr.type == UBUS_MSG_STATUS && !attr[UBUS_ATTR_STATUS]) ||
  333. (ub->hdr.type == UBUS_MSG_DATA && !attr[UBUS_ATTR_DATA]))
  334. goto out;
  335. obj = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_OBJID]));
  336. if (!obj)
  337. goto out;
  338. if (cl != obj->client)
  339. goto out;
  340. cl = ubusd_get_client_by_id(ub->hdr.peer);
  341. if (!cl)
  342. goto out;
  343. ub->hdr.peer = blob_get_u32(attr[UBUS_ATTR_OBJID]);
  344. ubus_msg_send(cl, ub);
  345. out:
  346. return -1;
  347. }
  348. static int ubusd_handle_add_watch(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
  349. {
  350. struct ubus_object *obj, *target;
  351. if (!attr[UBUS_ATTR_OBJID] || !attr[UBUS_ATTR_TARGET])
  352. return UBUS_STATUS_INVALID_ARGUMENT;
  353. obj = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_OBJID]));
  354. if (!obj)
  355. return UBUS_STATUS_NOT_FOUND;
  356. if (cl != obj->client)
  357. return UBUS_STATUS_INVALID_ARGUMENT;
  358. target = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_TARGET]));
  359. if (!target || !target->client)
  360. return UBUS_STATUS_NOT_FOUND;
  361. if (cl == target->client)
  362. return UBUS_STATUS_INVALID_ARGUMENT;
  363. if (!target->path.key) {
  364. if (strcmp(target->client->user, cl->user) && strcmp(target->client->group, cl->group))
  365. return UBUS_STATUS_NOT_FOUND;
  366. } else if (ubusd_acl_check(cl, target->path.key, NULL, UBUS_ACL_SUBSCRIBE)) {
  367. return UBUS_STATUS_NOT_FOUND;
  368. }
  369. ubus_subscribe(obj, target);
  370. return 0;
  371. }
  372. static int ubusd_handle_remove_watch(struct ubus_client *cl, struct ubus_msg_buf *ub, struct blob_attr **attr)
  373. {
  374. struct ubus_object *obj;
  375. struct ubus_subscription *s;
  376. uint32_t id;
  377. if (!attr[UBUS_ATTR_OBJID] || !attr[UBUS_ATTR_TARGET])
  378. return UBUS_STATUS_INVALID_ARGUMENT;
  379. obj = ubusd_find_object(blob_get_u32(attr[UBUS_ATTR_OBJID]));
  380. if (!obj)
  381. return UBUS_STATUS_NOT_FOUND;
  382. if (cl != obj->client)
  383. return UBUS_STATUS_INVALID_ARGUMENT;
  384. id = blob_get_u32(attr[UBUS_ATTR_TARGET]);
  385. list_for_each_entry(s, &obj->target_list, target_list) {
  386. if (s->target->id.id != id)
  387. continue;
  388. ubus_unsubscribe(s);
  389. return 0;
  390. }
  391. return UBUS_STATUS_NOT_FOUND;
  392. }
  393. static const ubus_cmd_cb handlers[__UBUS_MSG_LAST] = {
  394. [UBUS_MSG_PING] = ubusd_send_pong,
  395. [UBUS_MSG_ADD_OBJECT] = ubusd_handle_add_object,
  396. [UBUS_MSG_REMOVE_OBJECT] = ubusd_handle_remove_object,
  397. [UBUS_MSG_LOOKUP] = ubusd_handle_lookup,
  398. [UBUS_MSG_INVOKE] = ubusd_handle_invoke,
  399. [UBUS_MSG_STATUS] = ubusd_handle_response,
  400. [UBUS_MSG_DATA] = ubusd_handle_response,
  401. [UBUS_MSG_SUBSCRIBE] = ubusd_handle_add_watch,
  402. [UBUS_MSG_UNSUBSCRIBE] = ubusd_handle_remove_watch,
  403. [UBUS_MSG_NOTIFY] = ubusd_handle_notify,
  404. };
  405. void ubusd_proto_receive_message(struct ubus_client *cl, struct ubus_msg_buf *ub)
  406. {
  407. ubus_cmd_cb cb = NULL;
  408. int ret;
  409. struct ubus_msg_buf *retmsg = cl->retmsg;
  410. int *retmsg_data = blob_data(blob_data(retmsg->data));
  411. retmsg->hdr.seq = ub->hdr.seq;
  412. retmsg->hdr.peer = ub->hdr.peer;
  413. if (ub->hdr.type < __UBUS_MSG_LAST)
  414. cb = handlers[ub->hdr.type];
  415. if (ub->hdr.type != UBUS_MSG_STATUS && ub->hdr.type != UBUS_MSG_INVOKE)
  416. ubus_msg_close_fd(ub);
  417. /* Note: no callback should free the `ub` buffer
  418. that's always done right after the callback finishes */
  419. if (cb)
  420. ret = cb(cl, ub, ubus_parse_msg(ub->data, blob_raw_len(ub->data)));
  421. else
  422. ret = UBUS_STATUS_INVALID_COMMAND;
  423. /* Command has not been completed yet and got queued */
  424. if (ret == -2)
  425. return;
  426. ubus_msg_free(ub);
  427. if (ret == -1)
  428. return;
  429. *retmsg_data = htonl(ret);
  430. ubus_msg_send(cl, retmsg);
  431. }
  432. static int ubusd_proto_init_retmsg(struct ubus_client *cl)
  433. {
  434. struct blob_buf *b = &cl->b;
  435. blob_buf_init(&cl->b, 0);
  436. blob_put_int32(&cl->b, UBUS_ATTR_STATUS, 0);
  437. /* we make the 'retmsg' buffer shared with the blob_buf b, to reduce mem duplication */
  438. cl->retmsg = ubus_msg_new(b->head, blob_raw_len(b->head), true);
  439. if (!cl->retmsg)
  440. return -1;
  441. cl->retmsg->hdr.type = UBUS_MSG_STATUS;
  442. return 0;
  443. }
  444. struct ubus_client *ubusd_proto_new_client(int fd, uloop_fd_handler cb)
  445. {
  446. struct ubus_client *cl;
  447. cl = calloc(1, sizeof(*cl));
  448. if (!cl)
  449. return NULL;
  450. if (ubusd_acl_init_client(cl, fd))
  451. goto free;
  452. INIT_LIST_HEAD(&cl->objects);
  453. INIT_LIST_HEAD(&cl->cmd_queue);
  454. INIT_LIST_HEAD(&cl->tx_queue);
  455. cl->sock.fd = fd;
  456. cl->sock.cb = cb;
  457. cl->pending_msg_fd = -1;
  458. if (!ubus_alloc_id(&clients, &cl->id, 0))
  459. goto free;
  460. if (ubusd_proto_init_retmsg(cl))
  461. goto free;
  462. if (!ubusd_send_hello(cl))
  463. goto delete;
  464. return cl;
  465. delete:
  466. ubus_free_id(&clients, &cl->id);
  467. free:
  468. free(cl);
  469. return NULL;
  470. }
  471. void ubusd_proto_free_client(struct ubus_client *cl)
  472. {
  473. struct ubus_object *obj, *tmp;
  474. list_for_each_entry_safe(obj, tmp, &cl->objects, list) {
  475. ubusd_free_object(obj);
  476. }
  477. ubus_msg_free(cl->retmsg);
  478. blob_buf_free(&cl->b);
  479. ubusd_acl_free_client(cl);
  480. ubus_free_id(&clients, &cl->id);
  481. }
  482. void ubus_notify_subscription(struct ubus_object *obj)
  483. {
  484. bool active = !list_empty(&obj->subscribers);
  485. struct ubus_msg_buf *ub;
  486. blob_buf_init(&b, 0);
  487. blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
  488. blob_put_int8(&b, UBUS_ATTR_ACTIVE, active);
  489. ub = ubus_msg_from_blob(false);
  490. if (!ub)
  491. return;
  492. ubus_msg_init(ub, UBUS_MSG_NOTIFY, ++obj->invoke_seq, 0);
  493. ubus_msg_send(obj->client, ub);
  494. ubus_msg_free(ub);
  495. }
  496. void ubus_notify_unsubscribe(struct ubus_subscription *s)
  497. {
  498. struct ubus_msg_buf *ub;
  499. blob_buf_init(&b, 0);
  500. blob_put_int32(&b, UBUS_ATTR_OBJID, s->subscriber->id.id);
  501. blob_put_int32(&b, UBUS_ATTR_TARGET, s->target->id.id);
  502. ub = ubus_msg_from_blob(false);
  503. if (ub != NULL) {
  504. ubus_msg_init(ub, UBUS_MSG_UNSUBSCRIBE, ++s->subscriber->invoke_seq, 0);
  505. ubus_msg_send(s->subscriber->client, ub);
  506. ubus_msg_free(ub);
  507. }
  508. ubus_unsubscribe(s);
  509. }
  510. static void __constructor ubusd_proto_init(void)
  511. {
  512. ubus_init_id_tree(&clients);
  513. }