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