Ubus
OpenWrt system message/RPC bus.
libubus-internal.h File Reference

Go to the source code of this file.

Functions

struct blob_attr ** ubus_parse_msg (struct blob_attr *msg, size_t len)
 
bool ubus_validate_hdr (struct ubus_msghdr *hdr)
 
void ubus_handle_data (struct uloop_fd *u, unsigned int events)
 
int ubus_send_msg (struct ubus_context *ctx, uint32_t seq, struct blob_attr *msg, int cmd, uint32_t peer, int fd)
 
void ubus_process_msg (struct ubus_context *ctx, struct ubus_msghdr_buf *buf, int fd)
 
int __hidden ubus_start_request (struct ubus_context *ctx, struct ubus_request *req, struct blob_attr *msg, int cmd, uint32_t peer)
 
int __hidden __ubus_start_request (struct ubus_context *ctx, struct ubus_request *req, struct blob_attr *msg, int cmd, uint32_t peer)
 
void ubus_process_obj_msg (struct ubus_context *ctx, struct ubus_msghdr_buf *buf, int fd)
 
void ubus_process_req_msg (struct ubus_context *ctx, struct ubus_msghdr_buf *buf, int fd)
 
void __hidden ubus_poll_data (struct ubus_context *ctx, int timeout)
 

Variables

struct blob_buf b
 
const struct ubus_method watch_method
 

Function Documentation

◆ __ubus_start_request()

int __hidden __ubus_start_request ( struct ubus_context ctx,
struct ubus_request req,
struct blob_attr *  msg,
int  cmd,
uint32_t  peer 
)

Definition at line 53 of file libubus-req.c.

55 {
56 
57  if (msg && blob_pad_len(msg) > UBUS_MAX_MSGLEN)
58  return -1;
59 
60  INIT_LIST_HEAD(&req->list);
61  INIT_LIST_HEAD(&req->pending);
62  req->ctx = ctx;
63  req->peer = peer;
64  req->seq = ++ctx->request_seq;
65 
66  return ubus_send_msg(ctx, req->seq, msg, cmd, peer, req->fd);
67 }
static struct ubus_context * ctx
Definition: client.c:22
int ubus_send_msg(struct ubus_context *ctx, uint32_t seq, struct blob_attr *msg, int cmd, uint32_t peer, int fd)
Definition: libubus-io.c:126
uint16_t request_seq
Definition: libubus.h:166
uint32_t peer
Definition: libubus.h:217
uint16_t seq
Definition: libubus.h:218
struct list_head pending
Definition: libubus.h:210
struct ubus_context * ctx
Definition: libubus.h:227
struct list_head list
Definition: libubus.h:208
uint32_t peer
Definition: ubusmsg.h:3
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ubus_handle_data()

void ubus_handle_data ( struct uloop_fd *  u,
unsigned int  events 
)

Definition at line 312 of file libubus-io.c.

313 {
314  struct ubus_context *ctx = container_of(u, struct ubus_context, sock);
315  int recv_fd = -1;
316 
317  while (1) {
318  if (!ctx->stack_depth)
320 
321  if (!get_next_msg(ctx, &recv_fd))
322  break;
323  ubus_process_msg(ctx, &ctx->msgbuf, recv_fd);
324  if (uloop_cancelling() || ctx->cancel_poll)
325  break;
326  }
327 
328  if (!ctx->stack_depth)
330 
331  if (u->eof)
333 }
void ubus_process_msg(struct ubus_context *ctx, struct ubus_msghdr_buf *buf, int fd)
Definition: libubus.c:93
static bool get_next_msg(struct ubus_context *ctx, int *recv_fd)
Definition: libubus-io.c:271
int stack_depth
Definition: libubus.h:168
struct uloop_timeout pending_timer
Definition: libubus.h:163
struct ubus_msghdr_buf msgbuf
Definition: libubus.h:173
void(* connection_lost)(struct ubus_context *ctx)
Definition: libubus.h:170
bool cancel_poll
Definition: libubus.h:167
struct uloop_fd sock
Definition: libubus.h:162
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ubus_parse_msg()

struct blob_attr** ubus_parse_msg ( struct blob_attr *  msg,
size_t  len 
)

Definition at line 46 of file libubus-io.c.

47 {
48  blob_parse_untrusted(msg, len, attrbuf, ubus_policy, UBUS_ATTR_MAX);
49  return attrbuf;
50 }
static const struct blob_attr_info ubus_policy[UBUS_ATTR_MAX]
Definition: libubus-io.c:34
static struct blob_attr * attrbuf[UBUS_ATTR_MAX]
Definition: libubus-io.c:44
@ UBUS_ATTR_MAX
Definition: ubusmsg.h:103
Here is the caller graph for this function:

◆ ubus_poll_data()

void __hidden ubus_poll_data ( struct ubus_context ctx,
int  timeout 
)

Definition at line 335 of file libubus-io.c.

336 {
337  struct pollfd pfd = {
338  .fd = ctx->sock.fd,
339  .events = POLLIN | POLLERR,
340  };
341 
342  ctx->cancel_poll = false;
343  poll(&pfd, 1, timeout ? timeout : -1);
344  ubus_handle_data(&ctx->sock, ULOOP_READ);
345 }
static int timeout
Definition: cli.c:21
void __hidden ubus_handle_data(struct uloop_fd *u, unsigned int events)
Definition: libubus-io.c:312
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ubus_process_msg()

void ubus_process_msg ( struct ubus_context ctx,
struct ubus_msghdr_buf buf,
int  fd 
)

Definition at line 93 of file libubus.c.

94 {
95  switch(buf->hdr.type) {
96  case UBUS_MSG_STATUS:
97  case UBUS_MSG_DATA:
98  ubus_process_req_msg(ctx, buf, fd);
99  break;
100 
101  case UBUS_MSG_INVOKE:
103  case UBUS_MSG_NOTIFY:
104  if (ctx->stack_depth) {
105  ubus_queue_msg(ctx, buf);
106  break;
107  }
108 
109  ctx->stack_depth++;
110  ubus_process_obj_msg(ctx, buf, fd);
111  ctx->stack_depth--;
112  break;
113  case UBUS_MSG_MONITOR:
114  if (ctx->monitor_cb)
115  ctx->monitor_cb(ctx, buf->hdr.seq, buf->data);
116  break;
117  }
118 }
void ubus_process_obj_msg(struct ubus_context *ctx, struct ubus_msghdr_buf *buf, int fd)
Definition: libubus-obj.c:115
void ubus_process_req_msg(struct ubus_context *ctx, struct ubus_msghdr_buf *buf, int fd)
Definition: libubus-req.c:462
static void ubus_queue_msg(struct ubus_context *ctx, struct ubus_msghdr_buf *buf)
Definition: libubus.c:77
void(* monitor_cb)(struct ubus_context *ctx, uint32_t seq, struct blob_attr *data)
Definition: libubus.h:171
struct ubus_msghdr hdr
Definition: libubus.h:42
struct blob_attr * data
Definition: libubus.h:43
uint8_t type
Definition: ubusmsg.h:31
uint16_t seq
Definition: ubusmsg.h:32
@ UBUS_MSG_INVOKE
Definition: ubusmsg.h:53
@ UBUS_MSG_DATA
Definition: ubusmsg.h:44
@ UBUS_MSG_STATUS
Definition: ubusmsg.h:41
@ UBUS_MSG_NOTIFY
Definition: ubusmsg.h:71
@ UBUS_MSG_MONITOR
Definition: ubusmsg.h:73
@ UBUS_MSG_UNSUBSCRIBE
Definition: ubusmsg.h:64
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ubus_process_obj_msg()

void ubus_process_obj_msg ( struct ubus_context ctx,
struct ubus_msghdr_buf buf,
int  fd 
)

Definition at line 115 of file libubus-obj.c.

116 {
117  void (*cb)(struct ubus_context *, struct ubus_msghdr *,
118  struct ubus_object *, struct blob_attr **, int fd);
119  struct ubus_msghdr *hdr = &buf->hdr;
120  struct blob_attr **attrbuf;
121  struct ubus_object *obj;
122  uint32_t objid;
123  void *prev_data = NULL;
124  attrbuf = ubus_parse_msg(buf->data, blob_raw_len(buf->data));
125  if (!attrbuf[UBUS_ATTR_OBJID])
126  return;
127 
128  objid = blob_get_u32(attrbuf[UBUS_ATTR_OBJID]);
129  obj = avl_find_element(&ctx->objects, &objid, obj, avl);
130 
131  switch (hdr->type) {
132  case UBUS_MSG_INVOKE:
134  break;
137  break;
138  case UBUS_MSG_NOTIFY:
140  break;
141  default:
142  return;
143  }
144 
145  if (buf == &ctx->msgbuf) {
146  prev_data = buf->data;
147  buf->data = NULL;
148  }
149 
150  cb(ctx, hdr, obj, attrbuf, fd);
151 
152  if (prev_data) {
153  if (buf->data)
154  free(prev_data);
155  else
156  buf->data = prev_data;
157  }
158 }
int(* cb)(struct ubus_context *ctx, int argc, char **argv)
Definition: cli.c:581
struct blob_attr ** ubus_parse_msg(struct blob_attr *msg, size_t len)
Definition: libubus-io.c:46
static void ubus_process_notify(struct ubus_context *ctx, struct ubus_msghdr *hdr, struct ubus_object *obj, struct blob_attr **attrbuf, int fd)
Definition: libubus-obj.c:39
static void ubus_process_invoke(struct ubus_context *ctx, struct ubus_msghdr *hdr, struct ubus_object *obj, struct blob_attr **attrbuf, int fd)
Definition: libubus-obj.c:53
static void ubus_process_unsubscribe(struct ubus_context *ctx, struct ubus_msghdr *hdr, struct ubus_object *obj, struct blob_attr **attrbuf, int fd)
Definition: libubus-obj.c:19
struct avl_tree objects
Definition: libubus.h:159
struct avl_node avl
Definition: libubus.h:127
@ UBUS_ATTR_OBJID
Definition: ubusmsg.h:85
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ubus_process_req_msg()

void ubus_process_req_msg ( struct ubus_context ctx,
struct ubus_msghdr_buf buf,
int  fd 
)

Definition at line 462 of file libubus-req.c.

463 {
464  struct ubus_msghdr *hdr = &buf->hdr;
465  struct ubus_request *req;
466  int id = -1;
467 
468  switch(hdr->type) {
469  case UBUS_MSG_STATUS:
470  req = ubus_find_request(ctx, hdr->seq, hdr->peer, &id);
471  if (!req)
472  break;
473 
474  if (fd >= 0) {
475  if (req->fd_cb)
476  req->fd_cb(req, fd);
477  else
478  close(fd);
479  }
480 
481  if (id >= 0)
482  ubus_process_notify_status(req, id, buf);
483  else
484  ubus_process_req_status(req, buf);
485  break;
486 
487  case UBUS_MSG_DATA:
488  req = ubus_find_request(ctx, hdr->seq, hdr->peer, &id);
489  if (req && (req->data_cb || req->raw_data_cb))
490  ubus_process_req_data(req, buf);
491  break;
492  }
493 }
static void ubus_process_req_data(struct ubus_request *req, struct ubus_msghdr_buf *buf)
Definition: libubus-req.c:353
static int ubus_process_req_status(struct ubus_request *req, struct ubus_msghdr_buf *buf)
Definition: libubus-req.c:341
static void ubus_process_notify_status(struct ubus_request *req, int id, struct ubus_msghdr_buf *buf)
Definition: libubus-req.c:424
static struct ubus_request * ubus_find_request(struct ubus_context *ctx, uint32_t seq, uint32_t peer, int *id)
Definition: libubus-req.c:398
uint32_t peer
Definition: ubusmsg.h:33
ubus_data_handler_t raw_data_cb
Definition: libubus.h:220
ubus_data_handler_t data_cb
Definition: libubus.h:221
ubus_fd_handler_t fd_cb
Definition: libubus.h:222
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ubus_send_msg()

int ubus_send_msg ( struct ubus_context ctx,
uint32_t  seq,
struct blob_attr *  msg,
int  cmd,
uint32_t  peer,
int  fd 
)

Definition at line 126 of file libubus-io.c.

128 {
129  struct ubus_msghdr hdr;
130  struct iovec iov[2] = {
131  STATIC_IOV(hdr)
132  };
133  int ret;
134 
135  hdr.version = 0;
136  hdr.type = cmd;
137  hdr.seq = cpu_to_be16(seq);
138  hdr.peer = cpu_to_be32(peer);
139 
140  if (!msg) {
141  blob_buf_init(&b, 0);
142  msg = b.head;
143  }
144 
145  iov[1].iov_base = (char *) msg;
146  iov[1].iov_len = blob_raw_len(msg);
147 
148  ret = writev_retry(ctx->sock.fd, iov, ARRAY_SIZE(iov), fd);
149  if (ret < 0)
150  ctx->sock.eof = true;
151 
152  if (fd >= 0)
153  close(fd);
154 
155  return ret;
156 }
static struct blob_buf b
Definition: cli.c:19
#define STATIC_IOV(_var)
Definition: libubus-io.c:30
static int writev_retry(int fd, struct iovec *iov, int iov_len, int sock_fd)
Definition: libubus-io.c:60
uint16_t seq
Definition: ubusmsg.h:2
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ubus_start_request()

int __hidden ubus_start_request ( struct ubus_context ctx,
struct ubus_request req,
struct blob_attr *  msg,
int  cmd,
uint32_t  peer 
)

Definition at line 69 of file libubus-req.c.

71 {
72  memset(req, 0, sizeof(*req));
73 
74  req->fd = -1;
75 
76  return __ubus_start_request(ctx, req, msg, cmd, peer);
77 }
int __hidden __ubus_start_request(struct ubus_context *ctx, struct ubus_request *req, struct blob_attr *msg, int cmd, uint32_t peer)
Definition: libubus-req.c:53
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ubus_validate_hdr()

bool ubus_validate_hdr ( struct ubus_msghdr hdr)

Definition at line 224 of file libubus-io.c.

225 {
226  struct blob_attr *data = (struct blob_attr *) (hdr + 1);
227 
228  if (hdr->version != 0)
229  return false;
230 
231  if (blob_raw_len(data) < sizeof(*data))
232  return false;
233 
234  if (blob_pad_len(data) > UBUS_MAX_MSGLEN)
235  return false;
236 
237  return true;
238 }
uint8_t version
Definition: ubusmsg.h:30
Here is the caller graph for this function:

Variable Documentation

◆ b

struct blob_buf b
extern

Definition at line 1 of file ubusd_proto.c.

◆ watch_method

const struct ubus_method watch_method
extern