ubusd_id.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include "ubusmsg.h"
  6. #include "ubusd_id.h"
  7. static int random_fd = -1;
  8. static int ubus_cmp_id(const void *k1, const void *k2, void *ptr)
  9. {
  10. const uint32_t *id1 = k1, *id2 = k2;
  11. if (*id1 < *id2)
  12. return -1;
  13. else
  14. return *id1 > *id2;
  15. }
  16. static int ubus_cmp_str(const void *k1, const void *k2, void *ptr)
  17. {
  18. return strcmp(k1, k2);
  19. }
  20. void ubus_init_string_tree(struct avl_tree *tree, bool dup)
  21. {
  22. avl_init(tree, ubus_cmp_str, dup, NULL);
  23. }
  24. void ubus_init_id_tree(struct avl_tree *tree)
  25. {
  26. if (random_fd < 0) {
  27. random_fd = open("/dev/urandom", O_RDONLY);
  28. if (random_fd < 0) {
  29. perror("open");
  30. exit(1);
  31. }
  32. }
  33. avl_init(tree, ubus_cmp_id, false, NULL);
  34. }
  35. bool ubus_alloc_id(struct avl_tree *tree, struct ubus_id *id, uint32_t val)
  36. {
  37. id->avl.key = &id->id;
  38. if (val) {
  39. id->id = val;
  40. return avl_insert(tree, &id->avl) == 0;
  41. }
  42. do {
  43. if (read(random_fd, &id->id, sizeof(id->id)) != sizeof(id->id))
  44. return false;
  45. if (id->id < UBUS_SYSTEM_OBJECT_MAX)
  46. continue;
  47. } while (avl_insert(tree, &id->avl) != 0);
  48. return true;
  49. }