ubusd_obj.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. if (type)
  77. type->refcount++;
  78. return obj;
  79. error_free:
  80. free(obj);
  81. return NULL;
  82. }
  83. struct ubus_object *ubusd_create_object(struct ubus_client *cl, struct blob_attr **attr)
  84. {
  85. struct ubus_object *obj;
  86. struct ubus_object_type *type = NULL;
  87. if (attr[UBUS_ATTR_OBJTYPE])
  88. type = ubus_get_obj_type(blob_get_int32(attr[UBUS_ATTR_OBJTYPE]));
  89. else if (attr[UBUS_ATTR_SIGNATURE])
  90. type = ubus_create_obj_type(attr[UBUS_ATTR_SIGNATURE]);
  91. if (!type)
  92. return NULL;
  93. obj = ubusd_create_object_internal(type, 0);
  94. ubus_unref_object_type(type);
  95. if (!obj)
  96. return NULL;
  97. if (attr[UBUS_ATTR_OBJPATH]) {
  98. obj->path.key = strdup(blob_data(attr[UBUS_ATTR_OBJPATH]));
  99. if (!obj->path.key)
  100. goto free;
  101. if (avl_insert(&path, &obj->path) != 0) {
  102. free(obj->path.key);
  103. obj->path.key = NULL;
  104. goto free;
  105. }
  106. }
  107. obj->client = cl;
  108. list_add(&obj->list, &cl->objects);
  109. return obj;
  110. free:
  111. ubusd_free_object(obj);
  112. return NULL;
  113. }
  114. void ubusd_free_object(struct ubus_object *obj)
  115. {
  116. if (obj->path.key) {
  117. avl_delete(&path, &obj->path);
  118. free(obj->path.key);
  119. }
  120. if (!list_empty(&obj->list))
  121. list_del(&obj->list);
  122. ubus_free_id(&objects, &obj->id);
  123. if (obj->type)
  124. ubus_unref_object_type(obj->type);
  125. free(obj);
  126. }
  127. static void __init ubusd_obj_init(void)
  128. {
  129. ubus_init_id_tree(&objects);
  130. ubus_init_id_tree(&obj_types);
  131. ubus_init_string_tree(&path, false);
  132. ubusd_event_init();
  133. }