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