A library used by many OpenWrt projects as a planned D-Bus replacement
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

72 lines
1.5 KiB

  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 <stdio.h>
  14. #include <stdlib.h>
  15. #include <unistd.h>
  16. #include <fcntl.h>
  17. #include <libubox/avl-cmp.h>
  18. #include "ubusmsg.h"
  19. #include "ubusd_id.h"
  20. static int random_fd = -1;
  21. static int ubus_cmp_id(const void *k1, const void *k2, void *ptr)
  22. {
  23. const uint32_t *id1 = k1, *id2 = k2;
  24. if (*id1 < *id2)
  25. return -1;
  26. else
  27. return *id1 > *id2;
  28. }
  29. void ubus_init_string_tree(struct avl_tree *tree, bool dup)
  30. {
  31. avl_init(tree, avl_strcmp, dup, NULL);
  32. }
  33. void ubus_init_id_tree(struct avl_tree *tree)
  34. {
  35. if (random_fd < 0) {
  36. random_fd = open("/dev/urandom", O_RDONLY);
  37. if (random_fd < 0) {
  38. perror("open");
  39. exit(1);
  40. }
  41. }
  42. avl_init(tree, ubus_cmp_id, false, NULL);
  43. }
  44. bool ubus_alloc_id(struct avl_tree *tree, struct ubus_id *id, uint32_t val)
  45. {
  46. id->avl.key = &id->id;
  47. if (val) {
  48. id->id = val;
  49. return avl_insert(tree, &id->avl) == 0;
  50. }
  51. do {
  52. if (read(random_fd, &id->id, sizeof(id->id)) != sizeof(id->id))
  53. return false;
  54. if (id->id < UBUS_SYSTEM_OBJECT_MAX)
  55. continue;
  56. } while (avl_insert(tree, &id->avl) != 0);
  57. return true;
  58. }