ubusd_obj.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #include "ubusd.h"
  2. #include "ubusd_obj.h"
  3. struct avl_tree obj_types;
  4. struct avl_tree objects;
  5. struct avl_tree path;
  6. static void ubus_unref_object_type(struct ubus_object_type *type)
  7. {
  8. struct ubus_method *m;
  9. if (--type->refcount > 0)
  10. return;
  11. while (!list_empty(&type->methods)) {
  12. m = list_first_entry(&type->methods, struct ubus_method, list);
  13. list_del(&m->list);
  14. free(m);
  15. }
  16. ubus_free_id(&obj_types, &type->id);
  17. free(type);
  18. }
  19. static bool ubus_create_obj_method(struct ubus_object_type *type, struct blob_attr *attr)
  20. {
  21. struct ubus_method *m;
  22. int bloblen = blob_raw_len(attr);
  23. m = calloc(1, sizeof(*m) + bloblen);
  24. if (!m)
  25. return false;
  26. list_add(&m->list, &type->methods);
  27. memcpy(m->data, attr, bloblen);
  28. m->name = blobmsg_name(m->data);
  29. return true;
  30. }
  31. static struct ubus_object_type *ubus_create_obj_type(struct blob_attr *sig)
  32. {
  33. struct ubus_object_type *type;
  34. struct blob_attr *pos;
  35. int rem;
  36. type = calloc(1, sizeof(*type));
  37. type->refcount = 1;
  38. if (!ubus_alloc_id(&obj_types, &type->id, 0))
  39. goto error_free;
  40. INIT_LIST_HEAD(&type->methods);
  41. blob_for_each_attr(pos, sig, rem) {
  42. if (!blobmsg_check_attr(pos, true))
  43. goto error_unref;
  44. if (!ubus_create_obj_method(type, pos))
  45. goto error_unref;
  46. }
  47. return type;
  48. error_unref:
  49. ubus_unref_object_type(type);
  50. return NULL;
  51. error_free:
  52. free(type);
  53. return NULL;
  54. }
  55. static struct ubus_object_type *ubus_get_obj_type(uint32_t obj_id)
  56. {
  57. struct ubus_object_type *type;
  58. struct ubus_id *id;
  59. id = ubus_find_id(&obj_types, obj_id);
  60. if (!id)
  61. return NULL;
  62. type = container_of(id, struct ubus_object_type, id);
  63. type->refcount++;
  64. return type;
  65. }
  66. struct ubus_object *ubusd_create_object_internal(struct ubus_object_type *type, uint32_t id)
  67. {
  68. struct ubus_object *obj;
  69. obj = calloc(1, sizeof(*obj));
  70. if (!obj)
  71. return NULL;
  72. if (!ubus_alloc_id(&objects, &obj->id, id))
  73. goto error_free;
  74. obj->type = type;
  75. INIT_LIST_HEAD(&obj->list);
  76. INIT_LIST_HEAD(&obj->events);
  77. if (type)
  78. type->refcount++;
  79. return obj;
  80. error_free:
  81. free(obj);
  82. return NULL;
  83. }
  84. struct ubus_object *ubusd_create_object(struct ubus_client *cl, struct blob_attr **attr)
  85. {
  86. struct ubus_object *obj;
  87. struct ubus_object_type *type = NULL;
  88. if (attr[UBUS_ATTR_OBJTYPE])
  89. type = ubus_get_obj_type(blob_get_int32(attr[UBUS_ATTR_OBJTYPE]));
  90. else if (attr[UBUS_ATTR_SIGNATURE])
  91. type = ubus_create_obj_type(attr[UBUS_ATTR_SIGNATURE]);
  92. if (!!type ^ !!attr[UBUS_ATTR_OBJPATH])
  93. return NULL;
  94. obj = ubusd_create_object_internal(type, 0);
  95. if (type)
  96. ubus_unref_object_type(type);
  97. if (!obj)
  98. return NULL;
  99. if (attr[UBUS_ATTR_OBJPATH]) {
  100. obj->path.key = strdup(blob_data(attr[UBUS_ATTR_OBJPATH]));
  101. if (!obj->path.key)
  102. goto free;
  103. if (avl_insert(&path, &obj->path) != 0) {
  104. free(obj->path.key);
  105. obj->path.key = NULL;
  106. goto free;
  107. }
  108. }
  109. obj->client = cl;
  110. list_add(&obj->list, &cl->objects);
  111. return obj;
  112. free:
  113. ubusd_free_object(obj);
  114. return NULL;
  115. }
  116. void ubusd_free_object(struct ubus_object *obj)
  117. {
  118. ubusd_event_cleanup_object(obj);
  119. if (obj->path.key) {
  120. avl_delete(&path, &obj->path);
  121. free(obj->path.key);
  122. }
  123. if (!list_empty(&obj->list))
  124. list_del(&obj->list);
  125. ubus_free_id(&objects, &obj->id);
  126. if (obj->type)
  127. ubus_unref_object_type(obj->type);
  128. free(obj);
  129. }
  130. static void __init ubusd_obj_init(void)
  131. {
  132. ubus_init_id_tree(&objects);
  133. ubus_init_id_tree(&obj_types);
  134. ubus_init_string_tree(&path, false);
  135. ubusd_event_init();
  136. }