ubusd_obj.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright (C) 2011 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 "ubusd.h"
  14. #include "ubusd_obj.h"
  15. struct avl_tree obj_types;
  16. struct avl_tree objects;
  17. struct avl_tree path;
  18. static void ubus_unref_object_type(struct ubus_object_type *type)
  19. {
  20. struct ubus_method *m, *tmp;
  21. if (--type->refcount > 0)
  22. return;
  23. list_for_each_entry_safe(m, tmp, &type->methods, list) {
  24. list_del(&m->list);
  25. free(m);
  26. }
  27. ubus_free_id(&obj_types, &type->id);
  28. free(type);
  29. }
  30. static bool ubus_create_obj_method(struct ubus_object_type *type, struct blob_attr *attr)
  31. {
  32. struct ubus_method *m;
  33. int bloblen = blob_raw_len(attr);
  34. m = calloc(1, sizeof(*m) + bloblen);
  35. if (!m)
  36. return false;
  37. list_add_tail(&m->list, &type->methods);
  38. memcpy(m->data, attr, bloblen);
  39. m->name = blobmsg_name(m->data);
  40. return true;
  41. }
  42. static struct ubus_object_type *ubus_create_obj_type(struct blob_attr *sig)
  43. {
  44. struct ubus_object_type *type;
  45. struct blob_attr *pos;
  46. size_t rem;
  47. type = calloc(1, sizeof(*type));
  48. if (!type)
  49. return NULL;
  50. type->refcount = 1;
  51. if (!ubus_alloc_id(&obj_types, &type->id, 0))
  52. goto error_free;
  53. INIT_LIST_HEAD(&type->methods);
  54. blob_for_each_attr(pos, sig, rem) {
  55. if (!blobmsg_check_attr(pos, true))
  56. goto error_unref;
  57. if (!ubus_create_obj_method(type, pos))
  58. goto error_unref;
  59. }
  60. return type;
  61. error_unref:
  62. ubus_unref_object_type(type);
  63. return NULL;
  64. error_free:
  65. free(type);
  66. return NULL;
  67. }
  68. static struct ubus_object_type *ubus_get_obj_type(uint32_t obj_id)
  69. {
  70. struct ubus_object_type *type;
  71. struct ubus_id *id;
  72. id = ubus_find_id(&obj_types, obj_id);
  73. if (!id)
  74. return NULL;
  75. type = container_of(id, struct ubus_object_type, id);
  76. type->refcount++;
  77. return type;
  78. }
  79. struct ubus_object *ubusd_create_object_internal(struct ubus_object_type *type, uint32_t id)
  80. {
  81. struct ubus_object *obj;
  82. obj = calloc(1, sizeof(*obj));
  83. if (!obj)
  84. return NULL;
  85. if (!ubus_alloc_id(&objects, &obj->id, id))
  86. goto error_free;
  87. obj->type = type;
  88. INIT_LIST_HEAD(&obj->list);
  89. INIT_LIST_HEAD(&obj->events);
  90. INIT_LIST_HEAD(&obj->subscribers);
  91. INIT_LIST_HEAD(&obj->target_list);
  92. if (type)
  93. type->refcount++;
  94. return obj;
  95. error_free:
  96. free(obj);
  97. return NULL;
  98. }
  99. struct ubus_object *ubusd_create_object(struct ubus_client *cl, struct blob_attr **attr)
  100. {
  101. struct ubus_object *obj;
  102. struct ubus_object_type *type = NULL;
  103. if (attr[UBUS_ATTR_OBJTYPE])
  104. type = ubus_get_obj_type(blob_get_u32(attr[UBUS_ATTR_OBJTYPE]));
  105. else if (attr[UBUS_ATTR_SIGNATURE])
  106. type = ubus_create_obj_type(attr[UBUS_ATTR_SIGNATURE]);
  107. obj = ubusd_create_object_internal(type, 0);
  108. if (type)
  109. ubus_unref_object_type(type);
  110. if (!obj)
  111. return NULL;
  112. if (attr[UBUS_ATTR_OBJPATH]) {
  113. if (ubusd_acl_check(cl, blob_data(attr[UBUS_ATTR_OBJPATH]), NULL, UBUS_ACL_PUBLISH))
  114. goto free;
  115. obj->path.key = strdup(blob_data(attr[UBUS_ATTR_OBJPATH]));
  116. if (!obj->path.key)
  117. goto free;
  118. if (avl_insert(&path, &obj->path) != 0) {
  119. free((void *) obj->path.key);
  120. obj->path.key = NULL;
  121. goto free;
  122. }
  123. ubusd_send_obj_event(obj, true);
  124. }
  125. obj->client = cl;
  126. list_add(&obj->list, &cl->objects);
  127. return obj;
  128. free:
  129. ubusd_free_object(obj);
  130. return NULL;
  131. }
  132. void ubus_subscribe(struct ubus_object *obj, struct ubus_object *target)
  133. {
  134. struct ubus_subscription *s;
  135. bool first = list_empty(&target->subscribers);
  136. s = calloc(1, sizeof(*s));
  137. if (!s)
  138. return;
  139. s->subscriber = obj;
  140. s->target = target;
  141. list_add(&s->list, &target->subscribers);
  142. list_add(&s->target_list, &obj->target_list);
  143. if (first)
  144. ubus_notify_subscription(target);
  145. }
  146. void ubus_unsubscribe(struct ubus_subscription *s)
  147. {
  148. struct ubus_object *obj = s->target;
  149. list_del(&s->list);
  150. list_del(&s->target_list);
  151. free(s);
  152. if (list_empty(&obj->subscribers))
  153. ubus_notify_subscription(obj);
  154. }
  155. void ubusd_free_object(struct ubus_object *obj)
  156. {
  157. struct ubus_subscription *s, *tmp;
  158. list_for_each_entry_safe(s, tmp, &obj->target_list, target_list) {
  159. ubus_unsubscribe(s);
  160. }
  161. list_for_each_entry_safe(s, tmp, &obj->subscribers, list) {
  162. ubus_notify_unsubscribe(s);
  163. }
  164. ubusd_event_cleanup_object(obj);
  165. if (obj->path.key) {
  166. ubusd_send_obj_event(obj, false);
  167. avl_delete(&path, &obj->path);
  168. free((void *) obj->path.key);
  169. }
  170. if (!list_empty(&obj->list))
  171. list_del(&obj->list);
  172. ubus_free_id(&objects, &obj->id);
  173. if (obj->type)
  174. ubus_unref_object_type(obj->type);
  175. free(obj);
  176. }
  177. static void __constructor ubusd_obj_init(void)
  178. {
  179. ubus_init_id_tree(&objects);
  180. ubus_init_id_tree(&obj_types);
  181. ubus_init_string_tree(&path, false);
  182. ubusd_event_init();
  183. ubusd_acl_init();
  184. ubusd_monitor_init();
  185. }