Ubus
OpenWrt system message/RPC bus.
ubusd_event.c File Reference
#include <arpa/inet.h>
#include "ubusd.h"

Go to the source code of this file.

Data Structures

struct  event_source
 

Enumerations

enum  { EVREG_PATTERN , EVREG_OBJECT , EVREG_LAST }
 
enum  { EVMSG_ID , EVMSG_DATA , EVMSG_LAST }
 

Functions

static void ubusd_delete_event_source (struct event_source *evs)
 
void ubusd_event_cleanup_object (struct ubus_object *obj)
 
static int ubusd_alloc_event_pattern (struct ubus_client *cl, struct blob_attr *msg)
 
static void ubusd_send_event_msg (struct ubus_msg_buf **ub, struct ubus_client *cl, struct ubus_object *obj, const char *id, event_fill_cb fill_cb, void *cb_priv)
 
int ubusd_send_event (struct ubus_client *cl, const char *id, event_fill_cb fill_cb, void *cb_priv)
 
static struct ubus_msg_bufubusd_create_event_from_msg (void *priv, const char *id)
 
static int ubusd_forward_event (struct ubus_client *cl, struct blob_attr *msg)
 
static int ubusd_event_recv (struct ubus_client *cl, struct ubus_msg_buf *ub, const char *method, struct blob_attr *msg)
 
static struct ubus_msg_bufubusd_create_object_event_msg (void *priv, const char *id)
 
void ubusd_send_obj_event (struct ubus_object *obj, bool add)
 
void ubusd_event_init (void)
 

Variables

static struct avl_tree patterns
 
static struct ubus_objectevent_obj
 
static int event_seq = 0
 
static int obj_event_seq = 1
 
static struct blobmsg_policy evr_policy []
 
static struct blobmsg_policy ev_policy []
 

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
EVREG_PATTERN 
EVREG_OBJECT 
EVREG_LAST 

Definition at line 45 of file ubusd_event.c.

45  {
48  EVREG_LAST,
49 };
@ EVREG_LAST
Definition: ubusd_event.c:48
@ EVREG_OBJECT
Definition: ubusd_event.c:47
@ EVREG_PATTERN
Definition: ubusd_event.c:46

◆ anonymous enum

anonymous enum
Enumerator
EVMSG_ID 
EVMSG_DATA 
EVMSG_LAST 

Definition at line 185 of file ubusd_event.c.

185  {
186  EVMSG_ID,
187  EVMSG_DATA,
188  EVMSG_LAST,
189 };
@ EVMSG_ID
Definition: ubusd_event.c:186
@ EVMSG_LAST
Definition: ubusd_event.c:188
@ EVMSG_DATA
Definition: ubusd_event.c:187

Function Documentation

◆ ubusd_alloc_event_pattern()

static int ubusd_alloc_event_pattern ( struct ubus_client cl,
struct blob_attr *  msg 
)
static

Definition at line 56 of file ubusd_event.c.

57 {
58  struct event_source *ev;
59  struct ubus_object *obj;
60  struct blob_attr *attr[EVREG_LAST];
61  char *pattern, *name;
62  uint32_t id;
63  bool partial = false;
64  int len;
65 
66  if (!msg)
68 
69  blobmsg_parse(evr_policy, EVREG_LAST, attr, blob_data(msg), blob_len(msg));
70  if (!attr[EVREG_OBJECT] || !attr[EVREG_PATTERN])
72 
73  id = blobmsg_get_u32(attr[EVREG_OBJECT]);
74  if (id < UBUS_SYSTEM_OBJECT_MAX)
76 
77  obj = ubusd_find_object(id);
78  if (!obj)
79  return UBUS_STATUS_NOT_FOUND;
80 
81  if (obj->client != cl)
83 
84  pattern = blobmsg_data(attr[EVREG_PATTERN]);
85 
86  len = strlen(pattern);
87  if (pattern[len - 1] == '*') {
88  partial = true;
89  pattern[len - 1] = 0;
90  len--;
91  }
92 
93  if (pattern[0] && ubusd_acl_check(cl, pattern, NULL, UBUS_ACL_LISTEN))
95 
96  ev = calloc(1, sizeof(*ev) + len + 1);
97  if (!ev)
98  return UBUS_STATUS_NO_DATA;
99 
100  list_add(&ev->list, &obj->events);
101  ev->obj = obj;
102  ev->partial = partial;
103  name = (char *) (ev + 1);
104  strcpy(name, pattern);
105  ev->avl.key = name;
106  avl_insert(&patterns, &ev->avl);
107 
108  return 0;
109 }
const char * name
Definition: cli.c:580
struct avl_node avl
Definition: ubusd_event.c:25
struct ubus_object * obj
Definition: ubusd_event.c:24
struct list_head list
Definition: ubusd_event.c:23
struct ubus_client * client
Definition: ubusd_obj.h:54
struct list_head events
Definition: ubusd_obj.h:47
int ubusd_acl_check(struct ubus_client *cl, const char *obj, const char *method, enum ubusd_acl_type type)
Definition: ubusd_acl.c:90
@ UBUS_ACL_LISTEN
Definition: ubusd_acl.h:21
static struct blobmsg_policy evr_policy[]
Definition: ubusd_event.c:51
static struct avl_tree patterns
Definition: ubusd_event.c:17
static struct ubus_object * ubusd_find_object(uint32_t objid)
Definition: ubusd_obj.h:66
@ UBUS_STATUS_INVALID_ARGUMENT
Definition: ubusmsg.h:121
@ UBUS_STATUS_NO_DATA
Definition: ubusmsg.h:124
@ UBUS_STATUS_NOT_FOUND
Definition: ubusmsg.h:123
@ UBUS_STATUS_PERMISSION_DENIED
Definition: ubusmsg.h:125
#define UBUS_SYSTEM_OBJECT_MAX
Definition: ubusmsg.h:27
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ubusd_create_event_from_msg()

static struct ubus_msg_buf* ubusd_create_event_from_msg ( void *  priv,
const char *  id 
)
static

Definition at line 197 of file ubusd_event.c.

198 {
199  struct blob_attr *msg = priv;
200 
201  blob_buf_init(&b, 0);
202  blob_put_int32(&b, UBUS_ATTR_OBJID, 0);
203  blob_put_string(&b, UBUS_ATTR_METHOD, id);
204  blob_put(&b, UBUS_ATTR_DATA, blobmsg_data(msg), blobmsg_data_len(msg));
205 
206  return ubus_msg_new(b.head, blob_raw_len(b.head), true);
207 }
static struct blob_buf b
Definition: cli.c:19
struct ubus_msg_buf * ubus_msg_new(void *data, int len, bool shared)
Definition: ubusd.c:39
@ UBUS_ATTR_METHOD
Definition: ubusmsg.h:86
@ UBUS_ATTR_OBJID
Definition: ubusmsg.h:85
@ UBUS_ATTR_DATA
Definition: ubusmsg.h:91
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ubusd_create_object_event_msg()

static struct ubus_msg_buf* ubusd_create_object_event_msg ( void *  priv,
const char *  id 
)
static

Definition at line 243 of file ubusd_event.c.

244 {
245  struct ubus_object *obj = priv;
246  void *s;
247 
248  blob_buf_init(&b, 0);
249  blob_put_int32(&b, UBUS_ATTR_OBJID, 0);
250  blob_put_string(&b, UBUS_ATTR_METHOD, id);
251  s = blob_nest_start(&b, UBUS_ATTR_DATA);
252  blobmsg_add_u32(&b, "id", obj->id.id);
253  blobmsg_add_string(&b, "path", obj->path.key);
254  blob_nest_end(&b, s);
255 
256  return ubus_msg_new(b.head, blob_raw_len(b.head), true);
257 }
uint32_t id
Definition: libubus.h:130
const char * path
Definition: libubus.h:132
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ubusd_delete_event_source()

static void ubusd_delete_event_source ( struct event_source evs)
static

Definition at line 29 of file ubusd_event.c.

30 {
31  list_del(&evs->list);
32  avl_delete(&patterns, &evs->avl);
33  free(evs);
34 }

◆ ubusd_event_cleanup_object()

void ubusd_event_cleanup_object ( struct ubus_object obj)

Definition at line 36 of file ubusd_event.c.

37 {
38  struct event_source *ev, *tmp;
39 
40  list_for_each_entry_safe(ev, tmp, &obj->events, list) {
42  }
43 }
static void ubusd_delete_event_source(struct event_source *evs)
Definition: ubusd_event.c:29
Here is the caller graph for this function:

◆ ubusd_event_init()

void ubusd_event_init ( void  )

Definition at line 266 of file ubusd_event.c.

267 {
270  if (event_obj != NULL)
272 }
int(* recv_msg)(struct ubus_client *client, struct ubus_msg_buf *ub, const char *method, struct blob_attr *msg)
Definition: ubusd_obj.h:55
static struct ubus_object * event_obj
Definition: ubusd_event.c:18
static int ubusd_event_recv(struct ubus_client *cl, struct ubus_msg_buf *ub, const char *method, struct blob_attr *msg)
Definition: ubusd_event.c:231
void ubus_init_string_tree(struct avl_tree *tree, bool dup)
Definition: ubusd_id.c:35
struct ubus_object * ubusd_create_object_internal(struct ubus_object_type *type, uint32_t id)
Definition: ubusd_obj.c:103
#define UBUS_SYSTEM_OBJECT_EVENT
Definition: ubusmsg.h:24
Here is the caller graph for this function:

◆ ubusd_event_recv()

static int ubusd_event_recv ( struct ubus_client cl,
struct ubus_msg_buf ub,
const char *  method,
struct blob_attr *  msg 
)
static

Definition at line 231 of file ubusd_event.c.

232 {
233  if (!strcmp(method, "register"))
234  return ubusd_alloc_event_pattern(cl, msg);
235 
236  if (!strcmp(method, "send"))
237  return ubusd_forward_event(cl, msg);
238 
240 }
static int ubusd_forward_event(struct ubus_client *cl, struct blob_attr *msg)
Definition: ubusd_event.c:209
static int ubusd_alloc_event_pattern(struct ubus_client *cl, struct blob_attr *msg)
Definition: ubusd_event.c:56
@ UBUS_STATUS_INVALID_COMMAND
Definition: ubusmsg.h:120
Here is the call graph for this function:

◆ ubusd_forward_event()

static int ubusd_forward_event ( struct ubus_client cl,
struct blob_attr *  msg 
)
static

Definition at line 209 of file ubusd_event.c.

210 {
211  struct blob_attr *data;
212  struct blob_attr *attr[EVMSG_LAST];
213  const char *id;
214 
215  if (!msg)
217 
218  blobmsg_parse(ev_policy, EVMSG_LAST, attr, blob_data(msg), blob_len(msg));
219  if (!attr[EVMSG_ID] || !attr[EVMSG_DATA])
221 
222  id = blobmsg_data(attr[EVMSG_ID]);
223  data = attr[EVMSG_DATA];
224 
225  if (!strncmp(id, "ubus.", 5))
227 
228  return ubusd_send_event(cl, id, ubusd_create_event_from_msg, data);
229 }
static struct ubus_msg_buf * ubusd_create_event_from_msg(void *priv, const char *id)
Definition: ubusd_event.c:197
int ubusd_send_event(struct ubus_client *cl, const char *id, event_fill_cb fill_cb, void *cb_priv)
Definition: ubusd_event.c:140
static struct blobmsg_policy ev_policy[]
Definition: ubusd_event.c:191
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ubusd_send_event()

int ubusd_send_event ( struct ubus_client cl,
const char *  id,
event_fill_cb  fill_cb,
void *  cb_priv 
)

Definition at line 140 of file ubusd_event.c.

142 {
143  struct ubus_msg_buf *ub = NULL;
144  struct event_source *ev;
145  int match_len = 0;
146 
147  if (ubusd_acl_check(cl, id, NULL, UBUS_ACL_SEND))
149 
150  obj_event_seq++;
151 
152  /*
153  * Since this tree is sorted alphabetically, we can only expect to find
154  * matching entries as long as the number of matching characters
155  * between the pattern string and our string is monotonically increasing.
156  */
157  avl_for_each_element(&patterns, ev, avl) {
158  const char *key = ev->avl.key;
159  int cur_match_len;
160  bool full_match;
161 
162  full_match = ubus_strmatch_len(id, key, &cur_match_len);
163  if (cur_match_len < match_len)
164  break;
165 
166  match_len = cur_match_len;
167 
168  if (!full_match) {
169  if (!ev->partial)
170  continue;
171 
172  if (match_len != (int) strlen(key))
173  continue;
174  }
175 
176  ubusd_send_event_msg(&ub, cl, ev->obj, id, fill_cb, cb_priv);
177  }
178 
179  if (ub)
180  ubus_msg_free(ub);
181 
182  return 0;
183 }
static bool ubus_strmatch_len(const char *s1, const char *s2, int *len)
Definition: ubus_common.h:22
void ubus_msg_free(struct ubus_msg_buf *ub)
Definition: ubusd.c:67
@ UBUS_ACL_SEND
Definition: ubusd_acl.h:22
static int obj_event_seq
Definition: ubusd_event.c:20
static void ubusd_send_event_msg(struct ubus_msg_buf **ub, struct ubus_client *cl, struct ubus_object *obj, const char *id, event_fill_cb fill_cb, void *cb_priv)
Definition: ubusd_event.c:111
Here is the caller graph for this function:

◆ ubusd_send_event_msg()

static void ubusd_send_event_msg ( struct ubus_msg_buf **  ub,
struct ubus_client cl,
struct ubus_object obj,
const char *  id,
event_fill_cb  fill_cb,
void *  cb_priv 
)
static

Definition at line 111 of file ubusd_event.c.

114 {
115  uint32_t *objid_ptr;
116 
117  /* do not loop back events */
118  if (obj->client == cl)
119  return;
120 
121  /* do not send duplicate events */
122  if (obj->event_seen == obj_event_seq)
123  return;
124 
125  obj->event_seen = obj_event_seq;
126 
127  if (!*ub) {
128  *ub = fill_cb(cb_priv, id);
129  (*ub)->hdr.type = UBUS_MSG_INVOKE;
130  (*ub)->hdr.peer = 0;
131  }
132 
133  objid_ptr = blob_data(blob_data((*ub)->data));
134  *objid_ptr = htonl(obj->id.id);
135 
136  (*ub)->hdr.seq = ++event_seq;
137  ubus_msg_send(obj->client, *ub);
138 }
int event_seen
Definition: ubusd_obj.h:58
void ubus_msg_send(struct ubus_client *cl, struct ubus_msg_buf *ub)
Definition: ubusd.c:162
static int event_seq
Definition: ubusd_event.c:19
@ UBUS_MSG_INVOKE
Definition: ubusmsg.h:53
Here is the call graph for this function:

◆ ubusd_send_obj_event()

void ubusd_send_obj_event ( struct ubus_object obj,
bool  add 
)

Definition at line 259 of file ubusd_event.c.

260 {
261  const char *id = add ? "ubus.object.add" : "ubus.object.remove";
262 
264 }
static struct ubus_msg_buf * ubusd_create_object_event_msg(void *priv, const char *id)
Definition: ubusd_event.c:243
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ ev_policy

struct blobmsg_policy ev_policy[]
static
Initial value:
= {
[EVMSG_ID] = { .name = "id", .type = BLOBMSG_TYPE_STRING },
[EVMSG_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
}

Definition at line 140 of file ubusd_event.c.

◆ event_obj

struct ubus_object* event_obj
static

Definition at line 18 of file ubusd_event.c.

◆ event_seq

int event_seq = 0
static

Definition at line 19 of file ubusd_event.c.

◆ evr_policy

struct blobmsg_policy evr_policy[]
static
Initial value:
= {
[EVREG_PATTERN] = { .name = "pattern", .type = BLOBMSG_TYPE_STRING },
[EVREG_OBJECT] = { .name = "object", .type = BLOBMSG_TYPE_INT32 },
}

Definition at line 36 of file ubusd_event.c.

◆ obj_event_seq

int obj_event_seq = 1
static

Definition at line 20 of file ubusd_event.c.

◆ patterns

struct avl_tree patterns
static

Definition at line 1 of file ubusd_event.c.