ubusd_obj.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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))
  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(struct ubus_client *cl, struct blob_attr **attr)
  67. {
  68. struct ubus_object *obj;
  69. struct ubus_object_type *type = NULL;
  70. if (attr[UBUS_ATTR_OBJTYPE])
  71. type = ubus_get_obj_type(blob_get_int32(attr[UBUS_ATTR_OBJTYPE]));
  72. else if (attr[UBUS_ATTR_SIGNATURE])
  73. type = ubus_create_obj_type(attr[UBUS_ATTR_SIGNATURE]);
  74. if (!type)
  75. return NULL;
  76. obj = calloc(1, sizeof(*obj));
  77. if (!ubus_alloc_id(&objects, &obj->id))
  78. goto error_free;
  79. if (attr[UBUS_ATTR_OBJPATH]) {
  80. obj->path.key = strdup(blob_data(attr[UBUS_ATTR_OBJPATH]));
  81. if (avl_insert(&path, &obj->path) != 0)
  82. goto error_del_id;
  83. }
  84. obj->type = type;
  85. obj->client = cl;
  86. list_add(&obj->list, &cl->objects);
  87. return obj;
  88. error_del_id:
  89. free(obj->path.key);
  90. ubus_free_id(&objects, &obj->id);
  91. error_free:
  92. ubus_unref_object_type(type);
  93. free(obj);
  94. return NULL;
  95. }
  96. void ubusd_free_object(struct ubus_object *obj)
  97. {
  98. if (obj->path.key) {
  99. avl_delete(&path, &obj->path);
  100. free(obj->path.key);
  101. }
  102. list_del(&obj->list);
  103. ubus_free_id(&objects, &obj->id);
  104. ubus_unref_object_type(obj->type);
  105. free(obj);
  106. }
  107. static int ubus_cmp_path(const void *k1, const void *k2, void *ptr)
  108. {
  109. return strcmp(k1, k2);
  110. }
  111. static void __init ubusd_obj_init(void)
  112. {
  113. ubus_init_id_tree(&objects);
  114. ubus_init_id_tree(&obj_types);
  115. avl_init(&path, ubus_cmp_path, false, NULL);
  116. }