Ubus
OpenWrt system message/RPC bus.
ubusd_id.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <libubox/avl-cmp.h>
#include "ubusmsg.h"
#include "ubusd_id.h"

Go to the source code of this file.

Functions

static int ubus_cmp_id (const void *k1, const void *k2, void *ptr)
 
void ubus_init_string_tree (struct avl_tree *tree, bool dup)
 
void ubus_init_id_tree (struct avl_tree *tree)
 
bool ubus_alloc_id (struct avl_tree *tree, struct ubus_id *id, uint32_t val)
 

Variables

static int random_fd = -1
 

Function Documentation

◆ ubus_alloc_id()

bool ubus_alloc_id ( struct avl_tree *  tree,
struct ubus_id id,
uint32_t  val 
)

Definition at line 53 of file ubusd_id.c.

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 }
struct avl_node avl
Definition: ubusd_id.h:21
uint32_t id
Definition: ubusd_id.h:22
static int random_fd
Definition: ubusd_id.c:23
#define UBUS_SYSTEM_OBJECT_MAX
Definition: ubusmsg.h:27
Here is the caller graph for this function:

◆ ubus_cmp_id()

static int ubus_cmp_id ( const void *  k1,
const void *  k2,
void *  ptr 
)
static

Definition at line 25 of file ubusd_id.c.

26 {
27  const uint32_t *id1 = k1, *id2 = k2;
28 
29  if (*id1 < *id2)
30  return -1;
31  else
32  return *id1 > *id2;
33 }
Here is the caller graph for this function:

◆ ubus_init_id_tree()

void ubus_init_id_tree ( struct avl_tree *  tree)

Definition at line 40 of file ubusd_id.c.

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 }
static int ubus_cmp_id(const void *k1, const void *k2, void *ptr)
Definition: ubusd_id.c:25
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ubus_init_string_tree()

void ubus_init_string_tree ( struct avl_tree *  tree,
bool  dup 
)

Definition at line 35 of file ubusd_id.c.

36 {
37  avl_init(tree, avl_strcmp, dup, NULL);
38 }
Here is the caller graph for this function:

Variable Documentation

◆ random_fd

int random_fd = -1
static

Definition at line 23 of file ubusd_id.c.