add copyright/license information
[project/ubus.git] / ubusd_id.c
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
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <fcntl.h>
18
19 #include "ubusmsg.h"
20 #include "ubusd_id.h"
21
22 static int random_fd = -1;
23
24 static int ubus_cmp_id(const void *k1, const void *k2, void *ptr)
25 {
26 const uint32_t *id1 = k1, *id2 = k2;
27
28 if (*id1 < *id2)
29 return -1;
30 else
31 return *id1 > *id2;
32 }
33
34 static int ubus_cmp_str(const void *k1, const void *k2, void *ptr)
35 {
36 return strcmp(k1, k2);
37 }
38
39 void ubus_init_string_tree(struct avl_tree *tree, bool dup)
40 {
41 avl_init(tree, ubus_cmp_str, dup, NULL);
42 }
43
44 void ubus_init_id_tree(struct avl_tree *tree)
45 {
46 if (random_fd < 0) {
47 random_fd = open("/dev/urandom", O_RDONLY);
48 if (random_fd < 0) {
49 perror("open");
50 exit(1);
51 }
52 }
53
54 avl_init(tree, ubus_cmp_id, false, NULL);
55 }
56
57 bool ubus_alloc_id(struct avl_tree *tree, struct ubus_id *id, uint32_t val)
58 {
59 id->avl.key = &id->id;
60 if (val) {
61 id->id = val;
62 return avl_insert(tree, &id->avl) == 0;
63 }
64
65 do {
66 if (read(random_fd, &id->id, sizeof(id->id)) != sizeof(id->id))
67 return false;
68
69 if (id->id < UBUS_SYSTEM_OBJECT_MAX)
70 continue;
71 } while (avl_insert(tree, &id->avl) != 0);
72
73 return true;
74 }
75