Ubus
OpenWrt system message/RPC bus.
libubus.h File Reference
#include <libubox/avl.h>
#include <libubox/list.h>
#include <libubox/blobmsg.h>
#include <libubox/uloop.h>
#include <stdint.h>
#include "ubusmsg.h"
#include "ubus_common.h"

Go to the source code of this file.

Data Structures

struct  ubus_msghdr_buf
 
struct  ubus_method
 
struct  ubus_object_type
 
struct  ubus_object
 
struct  ubus_subscriber
 
struct  ubus_event_handler
 
struct  ubus_context
 
struct  ubus_object_data
 
struct  ubus_acl_key
 
struct  ubus_request_data
 
struct  ubus_request
 
struct  ubus_notify_request
 
struct  ubus_auto_conn
 
struct  acl_object
 

Macros

#define UBUS_MAX_NOTIFY_PEERS   16
 
#define UBUS_OBJECT_TYPE(_name, _methods)
 
#define __UBUS_METHOD_NOARG(_name, _handler, _tags)
 
#define __UBUS_METHOD(_name, _handler, _policy, _tags)
 
#define UBUS_METHOD(_name, _handler, _policy)    { __UBUS_METHOD(_name, _handler, _policy, 0) }
 
#define UBUS_METHOD_TAG(_name, _handler, _policy, _tags)    { __UBUS_METHOD(_name, _handler, _policy, _tags) }
 
#define UBUS_METHOD_MASK(_name, _handler, _policy, _mask)
 
#define UBUS_METHOD_NOARG(_name, _handler)    { __UBUS_METHOD_NOARG(_name, _handler, 0) }
 
#define UBUS_METHOD_TAG_NOARG(_name, _handler, _tags)    { __UBUS_METHOD_NOARG(_name, _handler, _tags) }
 
#define UBUS_TAG_STATUS   BIT(0)
 
#define UBUS_TAG_ADMIN   BIT(1)
 
#define UBUS_TAG_PRIVATE   BIT(2)
 
#define acl_for_each(o, m)
 

Typedefs

typedef void(* ubus_lookup_handler_t) (struct ubus_context *ctx, struct ubus_object_data *obj, void *priv)
 
typedef int(* ubus_handler_t) (struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg)
 
typedef void(* ubus_state_handler_t) (struct ubus_context *ctx, struct ubus_object *obj)
 
typedef void(* ubus_remove_handler_t) (struct ubus_context *ctx, struct ubus_subscriber *obj, uint32_t id)
 
typedef void(* ubus_event_handler_t) (struct ubus_context *ctx, struct ubus_event_handler *ev, const char *type, struct blob_attr *msg)
 
typedef void(* ubus_data_handler_t) (struct ubus_request *req, int type, struct blob_attr *msg)
 
typedef void(* ubus_fd_handler_t) (struct ubus_request *req, int fd)
 
typedef void(* ubus_complete_handler_t) (struct ubus_request *req, int ret)
 
typedef void(* ubus_notify_complete_handler_t) (struct ubus_notify_request *req, int idx, int ret)
 
typedef void(* ubus_notify_data_handler_t) (struct ubus_notify_request *req, int type, struct blob_attr *msg)
 
typedef void(* ubus_connect_handler_t) (struct ubus_context *ctx)
 
typedef bool(* ubus_new_object_handler_t) (struct ubus_context *ctx, struct ubus_subscriber *sub, const char *path)
 

Functions

struct ubus_contextubus_connect (const char *path)
 
int ubus_connect_ctx (struct ubus_context *ctx, const char *path)
 
void ubus_auto_connect (struct ubus_auto_conn *conn)
 
int ubus_reconnect (struct ubus_context *ctx, const char *path)
 
void ubus_free (struct ubus_context *ctx)
 
void ubus_shutdown (struct ubus_context *ctx)
 
static void ubus_auto_shutdown (struct ubus_auto_conn *conn)
 
const char * ubus_strerror (int error)
 
static void ubus_add_uloop (struct ubus_context *ctx)
 
static void ubus_handle_event (struct ubus_context *ctx)
 
int ubus_complete_request (struct ubus_context *ctx, struct ubus_request *req, int timeout)
 
void ubus_complete_request_async (struct ubus_context *ctx, struct ubus_request *req)
 
void ubus_abort_request (struct ubus_context *ctx, struct ubus_request *req)
 
int ubus_lookup (struct ubus_context *ctx, const char *path, ubus_lookup_handler_t cb, void *priv)
 
int ubus_lookup_id (struct ubus_context *ctx, const char *path, uint32_t *id)
 
int ubus_add_object (struct ubus_context *ctx, struct ubus_object *obj)
 
int ubus_remove_object (struct ubus_context *ctx, struct ubus_object *obj)
 
int ubus_register_subscriber (struct ubus_context *ctx, struct ubus_subscriber *obj)
 
static int ubus_unregister_subscriber (struct ubus_context *ctx, struct ubus_subscriber *obj)
 
int ubus_subscribe (struct ubus_context *ctx, struct ubus_subscriber *obj, uint32_t id)
 
int ubus_unsubscribe (struct ubus_context *ctx, struct ubus_subscriber *obj, uint32_t id)
 
int __ubus_monitor (struct ubus_context *ctx, const char *type)
 
static int ubus_monitor_start (struct ubus_context *ctx)
 
static int ubus_monitor_stop (struct ubus_context *ctx)
 
int ubus_register_acl (struct ubus_context *ctx)
 
int ubus_invoke_fd (struct ubus_context *ctx, uint32_t obj, const char *method, struct blob_attr *msg, ubus_data_handler_t cb, void *priv, int timeout, int fd)
 
static int ubus_invoke (struct ubus_context *ctx, uint32_t obj, const char *method, struct blob_attr *msg, ubus_data_handler_t cb, void *priv, int timeout)
 
int ubus_invoke_async_fd (struct ubus_context *ctx, uint32_t obj, const char *method, struct blob_attr *msg, struct ubus_request *req, int fd)
 
static int ubus_invoke_async (struct ubus_context *ctx, uint32_t obj, const char *method, struct blob_attr *msg, struct ubus_request *req)
 
int ubus_send_reply (struct ubus_context *ctx, struct ubus_request_data *req, struct blob_attr *msg)
 
static void ubus_defer_request (struct ubus_context *ctx, struct ubus_request_data *req, struct ubus_request_data *new_req)
 
static void ubus_request_set_fd (struct ubus_context *ctx, struct ubus_request_data *req, int fd)
 
static int ubus_request_get_caller_fd (struct ubus_request_data *req)
 
void ubus_complete_deferred_request (struct ubus_context *ctx, struct ubus_request_data *req, int ret)
 
int ubus_notify (struct ubus_context *ctx, struct ubus_object *obj, const char *type, struct blob_attr *msg, int timeout)
 
int ubus_notify_async (struct ubus_context *ctx, struct ubus_object *obj, const char *type, struct blob_attr *msg, struct ubus_notify_request *req)
 
int ubus_send_event (struct ubus_context *ctx, const char *id, struct blob_attr *data)
 
int ubus_register_event_handler (struct ubus_context *ctx, struct ubus_event_handler *ev, const char *pattern)
 
static int ubus_unregister_event_handler (struct ubus_context *ctx, struct ubus_event_handler *ev)
 

Variables

struct avl_tree acl_objects
 

Macro Definition Documentation

◆ __UBUS_METHOD

#define __UBUS_METHOD (   _name,
  _handler,
  _policy,
  _tags 
)
Value:
__UBUS_METHOD_NOARG(_name, _handler, _tags), \
.policy = _policy, \
.n_policy = ARRAY_SIZE(_policy)
#define __UBUS_METHOD_NOARG(_name, _handler, _tags)
Definition: libubus.h:76

Definition at line 81 of file libubus.h.

◆ __UBUS_METHOD_NOARG

#define __UBUS_METHOD_NOARG (   _name,
  _handler,
  _tags 
)
Value:
.name = _name, \
.handler = _handler, \
.tags = _tags

Definition at line 76 of file libubus.h.

◆ acl_for_each

#define acl_for_each (   o,
 
)
Value:
if ((m)->object && (m)->user && (m)->group) \
avl_for_element_range(avl_find_ge_element(&acl_objects, m, o, avl), avl_find_le_element(&acl_objects, m, o, avl), o, avl)
struct avl_tree acl_objects

Definition at line 343 of file libubus.h.

◆ UBUS_MAX_NOTIFY_PEERS

#define UBUS_MAX_NOTIFY_PEERS   16

Definition at line 29 of file libubus.h.

◆ UBUS_METHOD

#define UBUS_METHOD (   _name,
  _handler,
  _policy 
)     { __UBUS_METHOD(_name, _handler, _policy, 0) }

Definition at line 86 of file libubus.h.

◆ UBUS_METHOD_MASK

#define UBUS_METHOD_MASK (   _name,
  _handler,
  _policy,
  _mask 
)
Value:
{ \
__UBUS_METHOD(_name, _handler, _policy, 0),\
.mask = _mask \
}

Definition at line 92 of file libubus.h.

◆ UBUS_METHOD_NOARG

#define UBUS_METHOD_NOARG (   _name,
  _handler 
)     { __UBUS_METHOD_NOARG(_name, _handler, 0) }

Definition at line 98 of file libubus.h.

◆ UBUS_METHOD_TAG

#define UBUS_METHOD_TAG (   _name,
  _handler,
  _policy,
  _tags 
)     { __UBUS_METHOD(_name, _handler, _policy, _tags) }

Definition at line 89 of file libubus.h.

◆ UBUS_METHOD_TAG_NOARG

#define UBUS_METHOD_TAG_NOARG (   _name,
  _handler,
  _tags 
)     { __UBUS_METHOD_NOARG(_name, _handler, _tags) }

Definition at line 101 of file libubus.h.

◆ UBUS_OBJECT_TYPE

#define UBUS_OBJECT_TYPE (   _name,
  _methods 
)
Value:
{ \
.name = _name, \
.id = 0, \
.n_methods = ARRAY_SIZE(_methods), \
.methods = _methods \
}

Definition at line 68 of file libubus.h.

◆ UBUS_TAG_ADMIN

#define UBUS_TAG_ADMIN   BIT(1)

Definition at line 105 of file libubus.h.

◆ UBUS_TAG_PRIVATE

#define UBUS_TAG_PRIVATE   BIT(2)

Definition at line 106 of file libubus.h.

◆ UBUS_TAG_STATUS

#define UBUS_TAG_STATUS   BIT(0)

Definition at line 104 of file libubus.h.

Typedef Documentation

◆ ubus_complete_handler_t

typedef void(* ubus_complete_handler_t) (struct ubus_request *req, int ret)

Definition at line 60 of file libubus.h.

◆ ubus_connect_handler_t

typedef void(* ubus_connect_handler_t) (struct ubus_context *ctx)

Definition at line 65 of file libubus.h.

◆ ubus_data_handler_t

typedef void(* ubus_data_handler_t) (struct ubus_request *req, int type, struct blob_attr *msg)

Definition at line 57 of file libubus.h.

◆ ubus_event_handler_t

typedef void(* ubus_event_handler_t) (struct ubus_context *ctx, struct ubus_event_handler *ev, const char *type, struct blob_attr *msg)

Definition at line 55 of file libubus.h.

◆ ubus_fd_handler_t

typedef void(* ubus_fd_handler_t) (struct ubus_request *req, int fd)

Definition at line 59 of file libubus.h.

◆ ubus_handler_t

typedef int(* ubus_handler_t) (struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg)

Definition at line 49 of file libubus.h.

◆ ubus_lookup_handler_t

typedef void(* ubus_lookup_handler_t) (struct ubus_context *ctx, struct ubus_object_data *obj, void *priv)

Definition at line 46 of file libubus.h.

◆ ubus_new_object_handler_t

typedef bool(* ubus_new_object_handler_t) (struct ubus_context *ctx, struct ubus_subscriber *sub, const char *path)

Definition at line 66 of file libubus.h.

◆ ubus_notify_complete_handler_t

typedef void(* ubus_notify_complete_handler_t) (struct ubus_notify_request *req, int idx, int ret)

Definition at line 61 of file libubus.h.

◆ ubus_notify_data_handler_t

typedef void(* ubus_notify_data_handler_t) (struct ubus_notify_request *req, int type, struct blob_attr *msg)

Definition at line 63 of file libubus.h.

◆ ubus_remove_handler_t

typedef void(* ubus_remove_handler_t) (struct ubus_context *ctx, struct ubus_subscriber *obj, uint32_t id)

Definition at line 53 of file libubus.h.

◆ ubus_state_handler_t

typedef void(* ubus_state_handler_t) (struct ubus_context *ctx, struct ubus_object *obj)

Definition at line 52 of file libubus.h.

Function Documentation

◆ __ubus_monitor()

int __ubus_monitor ( struct ubus_context ctx,
const char *  type 
)

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

496 {
497  blob_buf_init(&b, 0);
498  return ubus_invoke(ctx, UBUS_SYSTEM_OBJECT_MONITOR, type, b.head, NULL, NULL, 1000);
499 }
static struct blob_buf b
Definition: cli.c:19
static struct ubus_context * ctx
Definition: client.c:22
static int ubus_invoke(struct ubus_context *ctx, uint32_t obj, const char *method, struct blob_attr *msg, ubus_data_handler_t cb, void *priv, int timeout)
Definition: libubus.h:354
uint8_t type
Definition: ubusmsg.h:1
#define UBUS_SYSTEM_OBJECT_MONITOR
Definition: ubusmsg.h:26
Here is the caller graph for this function:

◆ ubus_abort_request()

void ubus_abort_request ( struct ubus_context ctx,
struct ubus_request req 
)

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

81 {
82  if (list_empty(&req->list))
83  return;
84 
85  req->cancelled = true;
87  list_del_init(&req->list);
88 }
static void __ubus_process_req_data(struct ubus_request *req)
Definition: libubus-req.c:41
bool cancelled
Definition: libubus.h:214
struct list_head list
Definition: libubus.h:208
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ubus_add_object()

int ubus_add_object ( struct ubus_context ctx,
struct ubus_object obj 
)

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

210 {
211  struct ubus_request req;
212  int ret;
213 
214  blob_buf_init(&b, 0);
215 
216  if (obj->name && obj->type) {
217  blob_put_string(&b, UBUS_ATTR_OBJPATH, obj->name);
218 
219  if (obj->type->id)
220  blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id);
221  else if (!ubus_push_object_type(obj->type))
223  }
224 
225  if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_ADD_OBJECT, 0) < 0)
227 
228  req.raw_data_cb = ubus_add_object_cb;
229  req.priv = obj;
230  ret = ubus_complete_request(ctx, &req, 0);
231  if (ret)
232  return ret;
233 
234  if (!obj->id)
235  return UBUS_STATUS_NO_DATA;
236 
237  return 0;
238 }
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:69
static void ubus_add_object_cb(struct ubus_request *req, int type, struct blob_attr *msg)
Definition: libubus-obj.c:160
static bool ubus_push_object_type(const struct ubus_object_type *type)
Definition: libubus-obj.c:194
int ubus_complete_request(struct ubus_context *ctx, struct ubus_request *req, int req_timeout)
Definition: libubus-req.c:140
uint32_t id
Definition: libubus.h:120
uint32_t id
Definition: libubus.h:130
struct ubus_object_type * type
Definition: libubus.h:133
const char * name
Definition: libubus.h:129
@ UBUS_MSG_ADD_OBJECT
Definition: ubusmsg.h:55
@ UBUS_STATUS_INVALID_ARGUMENT
Definition: ubusmsg.h:121
@ UBUS_STATUS_NO_DATA
Definition: ubusmsg.h:124
@ UBUS_ATTR_OBJTYPE
Definition: ubusmsg.h:88
@ UBUS_ATTR_OBJPATH
Definition: ubusmsg.h:84
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ubus_add_uloop()

static void ubus_add_uloop ( struct ubus_context ctx)
inlinestatic

Definition at line 268 of file libubus.h.

269 {
270  uloop_fd_add(&ctx->sock, ULOOP_BLOCKING | ULOOP_READ);
271 }
struct uloop_fd sock
Definition: libubus.h:162
Here is the caller graph for this function:

◆ ubus_auto_connect()

void ubus_auto_connect ( struct ubus_auto_conn conn)

Definition at line 345 of file libubus.c.

346 {
347  conn->timer.cb = ubus_auto_connect_cb;
348  ubus_auto_connect_cb(&conn->timer);
349 }
static void ubus_auto_connect_cb(struct uloop_timeout *timeout)
Definition: libubus.c:330
struct uloop_timeout timer
Definition: libubus.h:244
Here is the call graph for this function:

◆ ubus_auto_shutdown()

static void ubus_auto_shutdown ( struct ubus_auto_conn conn)
inlinestatic

Definition at line 260 of file libubus.h.

261 {
262  uloop_timeout_cancel(&conn->timer);
263  ubus_shutdown(&conn->ctx);
264 }
void ubus_shutdown(struct ubus_context *ctx)
Definition: libubus.c:367
struct ubus_context ctx
Definition: libubus.h:243
Here is the call graph for this function:

◆ ubus_complete_deferred_request()

void ubus_complete_deferred_request ( struct ubus_context ctx,
struct ubus_request_data req,
int  ret 
)

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

190 {
191  blob_buf_init(&b, 0);
192  blob_put_int32(&b, UBUS_ATTR_STATUS, ret);
193  blob_put_int32(&b, UBUS_ATTR_OBJID, req->object);
194  ubus_send_msg(ctx, req->seq, b.head, UBUS_MSG_STATUS, req->peer, req->fd);
195 }
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 seq
Definition: libubus.h:197
uint32_t object
Definition: libubus.h:195
uint32_t peer
Definition: libubus.h:196
@ UBUS_MSG_STATUS
Definition: ubusmsg.h:41
@ UBUS_ATTR_STATUS
Definition: ubusmsg.h:82
@ UBUS_ATTR_OBJID
Definition: ubusmsg.h:85
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ubus_complete_request()

int ubus_complete_request ( struct ubus_context ctx,
struct ubus_request req,
int  timeout 
)

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

142 {
143  ubus_complete_handler_t complete_cb = req->complete_cb;
144  int status = UBUS_STATUS_NO_DATA;
145  int64_t timeout = 0, time_end = 0;
146 
147  if (req_timeout)
148  time_end = get_time_msec() + req_timeout;
149 
152 
153  ctx->stack_depth++;
154  while (!req->status_msg) {
155  if (req_timeout) {
156  timeout = time_end - get_time_msec();
157  if (timeout <= 0) {
159  break;
160  }
161  }
162 
163  ubus_poll_data(ctx, (unsigned int) timeout);
164 
165  if (ctx->sock.eof) {
167  ctx->cancel_poll = true;
168  break;
169  }
170  }
171 
172  ctx->stack_depth--;
173  if (ctx->stack_depth)
174  ctx->cancel_poll = true;
175 
176  if (req->status_msg)
177  status = req->status_code;
178 
179  req->complete_cb = complete_cb;
180  if (req->complete_cb)
181  req->complete_cb(req, status);
182 
183  if (!ctx->stack_depth && !ctx->sock.registered)
185 
186  return status;
187 }
static int timeout
Definition: cli.c:21
void __hidden ubus_poll_data(struct ubus_context *ctx, int timeout)
Definition: libubus-io.c:335
static int64_t get_time_msec(void)
Definition: libubus-req.c:129
static void ubus_set_req_status(struct ubus_request *req, int ret)
Definition: libubus-req.c:111
static void ubus_sync_req_cb(struct ubus_request *req, int ret)
Definition: libubus-req.c:122
void ubus_complete_request_async(struct ubus_context *ctx, struct ubus_request *req)
Definition: libubus-req.c:90
void(* ubus_complete_handler_t)(struct ubus_request *req, int ret)
Definition: libubus.h:60
int stack_depth
Definition: libubus.h:168
struct uloop_timeout pending_timer
Definition: libubus.h:163
bool cancel_poll
Definition: libubus.h:167
ubus_complete_handler_t complete_cb
Definition: libubus.h:223
int status_code
Definition: libubus.h:211
bool status_msg
Definition: libubus.h:212
@ UBUS_STATUS_TIMEOUT
Definition: ubusmsg.h:126
@ UBUS_STATUS_CONNECTION_FAILED
Definition: ubusmsg.h:129
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ubus_complete_request_async()

void ubus_complete_request_async ( struct ubus_context ctx,
struct ubus_request req 
)

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

91 {
92  if (!list_empty(&req->list))
93  return;
94 
95  list_add(&req->list, &ctx->requests);
96 }
struct list_head requests
Definition: libubus.h:158
Here is the caller graph for this function:

◆ ubus_connect()

struct ubus_context* ubus_connect ( const char *  path)

Definition at line 351 of file libubus.c.

352 {
353  struct ubus_context *ctx;
354 
355  ctx = calloc(1, sizeof(*ctx));
356  if (!ctx)
357  return NULL;
358 
359  if (ubus_connect_ctx(ctx, path)) {
360  free(ctx);
361  ctx = NULL;
362  }
363 
364  return ctx;
365 }
int ubus_connect_ctx(struct ubus_context *ctx, const char *path)
Definition: libubus.c:284
struct avl_tree path
Definition: ubusd_obj.c:19
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ubus_connect_ctx()

int ubus_connect_ctx ( struct ubus_context ctx,
const char *  path 
)

Definition at line 284 of file libubus.c.

285 {
286  uloop_init();
287  memset(ctx, 0, sizeof(*ctx));
288 
289  ctx->sock.fd = -1;
290  ctx->sock.cb = ubus_handle_data;
293 
294  ctx->msgbuf.data = calloc(1, UBUS_MSG_CHUNK_SIZE);
295  if (!ctx->msgbuf.data)
296  return -1;
298 
299  INIT_LIST_HEAD(&ctx->requests);
300  INIT_LIST_HEAD(&ctx->pending);
301  INIT_LIST_HEAD(&ctx->auto_subscribers);
302  avl_init(&ctx->objects, ubus_cmp_id, false, NULL);
303  if (ubus_reconnect(ctx, path)) {
304  free(ctx->msgbuf.data);
305  ctx->msgbuf.data = NULL;
306  return -1;
307  }
308 
309  return 0;
310 }
void ubus_handle_data(struct uloop_fd *u, unsigned int events)
Definition: libubus-io.c:312
int ubus_reconnect(struct ubus_context *ctx, const char *path)
Definition: libubus-io.c:395
static int ubus_cmp_id(const void *k1, const void *k2, void *ptr)
Definition: libubus.c:49
static void ubus_default_connection_lost(struct ubus_context *ctx)
Definition: libubus.c:278
static void ubus_process_pending_msg(struct uloop_timeout *timeout)
Definition: libubus.c:120
uint32_t msgbuf_data_len
Definition: libubus.h:174
struct ubus_msghdr_buf msgbuf
Definition: libubus.h:173
void(* connection_lost)(struct ubus_context *ctx)
Definition: libubus.h:170
struct avl_tree objects
Definition: libubus.h:159
struct list_head auto_subscribers
Definition: libubus.h:177
struct list_head pending
Definition: libubus.h:160
struct blob_attr * data
Definition: libubus.h:43
#define UBUS_MSG_CHUNK_SIZE
Definition: ubusmsg.h:22
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ubus_defer_request()

static void ubus_defer_request ( struct ubus_context ctx,
struct ubus_request_data req,
struct ubus_request_data new_req 
)
inlinestatic

Definition at line 375 of file libubus.h.

378 {
379  (void) ctx;
380  memcpy(new_req, req, sizeof(*req));
381  req->deferred = true;
382 }
Here is the caller graph for this function:

◆ ubus_free()

void ubus_free ( struct ubus_context ctx)

Definition at line 378 of file libubus.c.

379 {
381  free(ctx);
382 }
void ubus_shutdown(struct ubus_context *ctx)
Definition: libubus.c:367
Here is the caller graph for this function:

◆ ubus_handle_event()

static void ubus_handle_event ( struct ubus_context ctx)
inlinestatic

Definition at line 274 of file libubus.h.

275 {
276  ctx->sock.cb(&ctx->sock, ULOOP_READ);
277 }

◆ ubus_invoke()

static int ubus_invoke ( struct ubus_context ctx,
uint32_t  obj,
const char *  method,
struct blob_attr *  msg,
ubus_data_handler_t  cb,
void *  priv,
int  timeout 
)
inlinestatic

Definition at line 354 of file libubus.h.

357 {
358  return ubus_invoke_fd(ctx, obj, method, msg, cb, priv, timeout, -1);
359 }
int(* cb)(struct ubus_context *ctx, int argc, char **argv)
Definition: cli.c:581
int ubus_invoke_fd(struct ubus_context *ctx, uint32_t obj, const char *method, struct blob_attr *msg, ubus_data_handler_t cb, void *priv, int timeout, int fd)
Definition: libubus-req.c:236
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ubus_invoke_async()

static int ubus_invoke_async ( struct ubus_context ctx,
uint32_t  obj,
const char *  method,
struct blob_attr *  msg,
struct ubus_request req 
)
inlinestatic

Definition at line 365 of file libubus.h.

367 {
368  return ubus_invoke_async_fd(ctx, obj, method, msg, req, -1);
369 }
int ubus_invoke_async_fd(struct ubus_context *ctx, uint32_t obj, const char *method, struct blob_attr *msg, struct ubus_request *req, int fd)
Definition: libubus-req.c:220
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ubus_invoke_async_fd()

int ubus_invoke_async_fd ( struct ubus_context ctx,
uint32_t  obj,
const char *  method,
struct blob_attr *  msg,
struct ubus_request req,
int  fd 
)

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

223 {
224  blob_buf_init(&b, 0);
225  blob_put_int32(&b, UBUS_ATTR_OBJID, obj);
226  blob_put_string(&b, UBUS_ATTR_METHOD, method);
227  ubus_put_data(&b, msg);
228 
229  memset(req, 0, sizeof(*req));
230  req->fd = fd;
231  if (__ubus_start_request(ctx, req, b.head, UBUS_MSG_INVOKE, obj) < 0)
233  return 0;
234 }
static void ubus_put_data(struct blob_buf *buf, struct blob_attr *msg)
Definition: libubus-req.c:197
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
@ UBUS_MSG_INVOKE
Definition: ubusmsg.h:53
@ UBUS_ATTR_METHOD
Definition: ubusmsg.h:86
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ubus_invoke_fd()

int ubus_invoke_fd ( struct ubus_context ctx,
uint32_t  obj,
const char *  method,
struct blob_attr *  msg,
ubus_data_handler_t  cb,
void *  priv,
int  timeout,
int  fd 
)

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

239 {
240  struct ubus_request req;
241  int rc;
242 
243  rc = ubus_invoke_async_fd(ctx, obj, method, msg, &req, fd);
244  if (rc)
245  return rc;
246 
247  req.data_cb = cb;
248  req.priv = priv;
249  return ubus_complete_request(ctx, &req, timeout);
250 }
int ubus_invoke_async_fd(struct ubus_context *ctx, uint32_t obj, const char *method, struct blob_attr *msg, struct ubus_request *req, int fd)
Definition: libubus-req.c:220
void * priv
Definition: libubus.h:228
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ubus_lookup()

int ubus_lookup ( struct ubus_context ctx,
const char *  path,
ubus_lookup_handler_t  cb,
void *  priv 
)

Definition at line 161 of file libubus.c.

163 {
164  struct ubus_lookup_request lookup;
165 
166  blob_buf_init(&b, 0);
167  if (path)
168  blob_put_string(&b, UBUS_ATTR_OBJPATH, path);
169 
170  if (ubus_start_request(ctx, &lookup.req, b.head, UBUS_MSG_LOOKUP, 0) < 0)
172 
173  lookup.req.raw_data_cb = ubus_lookup_cb;
174  lookup.req.priv = priv;
175  lookup.cb = cb;
176  return ubus_complete_request(ctx, &lookup.req, 0);
177 }
static void ubus_lookup_cb(struct ubus_request *ureq, int type, struct blob_attr *msg)
Definition: libubus.c:141
@ UBUS_MSG_LOOKUP
Definition: ubusmsg.h:50
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ubus_lookup_id()

int ubus_lookup_id ( struct ubus_context ctx,
const char *  path,
uint32_t *  id 
)

Definition at line 192 of file libubus.c.

193 {
194  struct ubus_request req;
195 
196  blob_buf_init(&b, 0);
197  if (path)
198  blob_put_string(&b, UBUS_ATTR_OBJPATH, path);
199 
200  if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_LOOKUP, 0) < 0)
202 
203  req.raw_data_cb = ubus_lookup_id_cb;
204  req.priv = id;
205 
206  return ubus_complete_request(ctx, &req, 0);
207 }
static void ubus_lookup_id_cb(struct ubus_request *req, int type, struct blob_attr *msg)
Definition: libubus.c:179
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ubus_monitor_start()

static int ubus_monitor_start ( struct ubus_context ctx)
inlinestatic

Definition at line 321 of file libubus.h.

322 {
323  return __ubus_monitor(ctx, "add");
324 }
int __ubus_monitor(struct ubus_context *ctx, const char *type)
Definition: libubus-req.c:495
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ubus_monitor_stop()

static int ubus_monitor_stop ( struct ubus_context ctx)
inlinestatic

Definition at line 326 of file libubus.h.

327 {
328  return __ubus_monitor(ctx, "remove");
329 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ubus_notify()

int ubus_notify ( struct ubus_context ctx,
struct ubus_object obj,
const char *  type,
struct blob_attr *  msg,
int  timeout 
)

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

313 {
314  struct ubus_notify_request req;
315  int ret;
316 
317  ret = __ubus_notify_async(ctx, obj, type, msg, &req, timeout >= 0);
318  if (ret < 0)
319  return ret;
320 
321  if (timeout < 0) {
322  ubus_abort_request(ctx, &req.req);
323  return 0;
324  }
325 
326  return ubus_complete_request(ctx, &req.req, timeout);
327 }
void ubus_abort_request(struct ubus_context *ctx, struct ubus_request *req)
Definition: libubus-req.c:80
static int __ubus_notify_async(struct ubus_context *ctx, struct ubus_object *obj, const char *type, struct blob_attr *msg, struct ubus_notify_request *req, bool reply)
Definition: libubus-req.c:277
struct ubus_request req
Definition: libubus.h:232
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ubus_notify_async()

int ubus_notify_async ( struct ubus_context ctx,
struct ubus_object obj,
const char *  type,
struct blob_attr *  msg,
struct ubus_notify_request req 
)

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

307 {
308  return __ubus_notify_async(ctx, obj, type, msg, req, true);
309 }
Here is the call graph for this function:

◆ ubus_reconnect()

int ubus_reconnect ( struct ubus_context ctx,
const char *  path 
)

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

396 {
397  struct {
398  struct ubus_msghdr hdr;
399  struct blob_attr data;
400  } hdr;
401  struct blob_attr *buf;
402  int ret = UBUS_STATUS_UNKNOWN_ERROR;
403 
404  if (!path)
405  path = UBUS_UNIX_SOCKET;
406 
407  if (ctx->sock.fd >= 0) {
408  if (ctx->sock.registered)
409  uloop_fd_delete(&ctx->sock);
410 
411  close(ctx->sock.fd);
412  }
413 
414  ctx->sock.eof = false;
415  ctx->sock.error = false;
416  ctx->sock.fd = usock(USOCK_UNIX, path, NULL);
417  if (ctx->sock.fd < 0)
419 
420  if (read(ctx->sock.fd, &hdr, sizeof(hdr)) != sizeof(hdr))
421  goto out_close;
422 
423  if (!ubus_validate_hdr(&hdr.hdr))
424  goto out_close;
425 
426  if (hdr.hdr.type != UBUS_MSG_HELLO)
427  goto out_close;
428 
429  buf = calloc(1, blob_raw_len(&hdr.data));
430  if (!buf)
431  goto out_close;
432 
433  memcpy(buf, &hdr.data, sizeof(hdr.data));
434  if (read(ctx->sock.fd, blob_data(buf), blob_len(buf)) != (ssize_t) blob_len(buf))
435  goto out_free;
436 
437  ctx->local_id = hdr.hdr.peer;
438  if (!ctx->local_id)
439  goto out_free;
440 
441  ret = UBUS_STATUS_OK;
442  fcntl(ctx->sock.fd, F_SETFL, fcntl(ctx->sock.fd, F_GETFL) | O_NONBLOCK | O_CLOEXEC);
443 
445 
446 out_free:
447  free(buf);
448 out_close:
449  if (ret)
450  close(ctx->sock.fd);
451 
452  return ret;
453 }
static void ubus_refresh_state(struct ubus_context *ctx)
Definition: libubus-io.c:371
bool ubus_validate_hdr(struct ubus_msghdr *hdr)
Definition: libubus-io.c:224
uint32_t local_id
Definition: libubus.h:165
@ UBUS_MSG_HELLO
Definition: ubusmsg.h:38
@ UBUS_STATUS_OK
Definition: ubusmsg.h:119
@ UBUS_STATUS_UNKNOWN_ERROR
Definition: ubusmsg.h:128
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ubus_register_acl()

int ubus_register_acl ( struct ubus_context ctx)

Definition at line 142 of file libubus-acl.c.

143 {
144  int ret;
145 
147 
148  ret = ubus_register_event_handler(ctx, &acl_event, "ubus.acl.sequence");
149  if (!ret)
150  acl_query(ctx);
151 
152  return ret;
153 }
static struct ubus_event_handler acl_event
Definition: libubus-acl.c:22
static void acl_subscribe_cb(struct ubus_context *ctx, struct ubus_event_handler *ev, const char *type, struct blob_attr *msg)
Definition: libubus-acl.c:134
static void acl_query(struct ubus_context *ctx)
Definition: libubus-acl.c:127
int ubus_register_event_handler(struct ubus_context *ctx, struct ubus_event_handler *ev, const char *pattern)
Definition: libubus.c:225
ubus_event_handler_t cb
Definition: libubus.h:154
Here is the call graph for this function:

◆ ubus_register_event_handler()

int ubus_register_event_handler ( struct ubus_context ctx,
struct ubus_event_handler ev,
const char *  pattern 
)

Definition at line 225 of file libubus.c.

228 {
229  struct ubus_object *obj = &ev->obj;
230  struct blob_buf b2 = {};
231  int ret;
232 
233  if (!obj->id) {
234  obj->methods = &event_method;
235  obj->n_methods = 1;
236 
237  if (!!obj->name ^ !!obj->type)
239 
240  ret = ubus_add_object(ctx, obj);
241  if (ret)
242  return ret;
243  }
244 
245  /* use a second buffer, ubus_invoke() overwrites the primary one */
246  blob_buf_init(&b2, 0);
247  blobmsg_add_u32(&b2, "object", obj->id);
248  if (pattern)
249  blobmsg_add_string(&b2, "pattern", pattern);
250 
251  ret = ubus_invoke(ctx, UBUS_SYSTEM_OBJECT_EVENT, "register", b2.head,
252  NULL, NULL, 0);
253  blob_buf_free(&b2);
254 
255  return ret;
256 }
int ubus_add_object(struct ubus_context *ctx, struct ubus_object *obj)
Definition: libubus-obj.c:209
static const struct ubus_method event_method
Definition: libubus.c:220
struct ubus_object obj
Definition: libubus.h:152
const struct ubus_method * methods
Definition: libubus.h:138
int n_methods
Definition: libubus.h:139
#define UBUS_SYSTEM_OBJECT_EVENT
Definition: ubusmsg.h:24
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ubus_register_subscriber()

int ubus_register_subscriber ( struct ubus_context ctx,
struct ubus_subscriber obj 
)

Definition at line 77 of file libubus-sub.c.

78 {
79  struct ubus_object *obj = &s->obj;
80  int ret;
81 
82  INIT_LIST_HEAD(&s->list);
83  obj->methods = &watch_method;
84  obj->n_methods = 1;
85 
86  ret = ubus_add_object(ctx, obj);
87  if (ret)
88  return ret;
89 
90  if (s->new_obj_cb) {
92  list_add(&s->list, &ctx->auto_subscribers);
94  if (!ev->obj.id)
95  ubus_register_event_handler(ctx, ev, "ubus.object.add");
97  }
98 
99  return 0;
100 }
const struct ubus_method watch_method
static void ubus_auto_sub_lookup(struct ubus_context *ctx, struct ubus_object_data *obj, void *priv)
Definition: libubus-sub.c:68
static void ubus_auto_sub_event_handler_cb(struct ubus_context *ctx, struct ubus_event_handler *ev, const char *type, struct blob_attr *msg)
Definition: libubus-sub.c:35
int ubus_lookup(struct ubus_context *ctx, const char *path, ubus_lookup_handler_t cb, void *priv)
Definition: libubus.c:161
struct ubus_event_handler auto_subscribe_event_handler
Definition: libubus.h:178
struct ubus_object obj
Definition: libubus.h:144
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ubus_remove_object()

int ubus_remove_object ( struct ubus_context ctx,
struct ubus_object obj 
)

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

257 {
258  struct ubus_request req;
259  int ret;
260 
261  blob_buf_init(&b, 0);
262  blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id);
263 
264  if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_REMOVE_OBJECT, 0) < 0)
266 
267  req.raw_data_cb = ubus_remove_object_cb;
268  req.priv = obj;
269  ret = ubus_complete_request(ctx, &req, 0);
270  if (ret)
271  return ret;
272 
273  if (obj->id)
274  return UBUS_STATUS_NO_DATA;
275 
276  return 0;
277 }
static void ubus_remove_object_cb(struct ubus_request *req, int type, struct blob_attr *msg)
Definition: libubus-obj.c:240
@ UBUS_MSG_REMOVE_OBJECT
Definition: ubusmsg.h:56
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ubus_request_get_caller_fd()

static int ubus_request_get_caller_fd ( struct ubus_request_data req)
inlinestatic

Definition at line 391 of file libubus.h.

392 {
393  int fd = req->req_fd;
394  req->req_fd = -1;
395 
396  return fd;
397 }

◆ ubus_request_set_fd()

static void ubus_request_set_fd ( struct ubus_context ctx,
struct ubus_request_data req,
int  fd 
)
inlinestatic

Definition at line 384 of file libubus.h.

386 {
387  (void) ctx;
388  req->fd = fd;
389 }
Here is the caller graph for this function:

◆ ubus_send_event()

int ubus_send_event ( struct ubus_context ctx,
const char *  id,
struct blob_attr *  data 
)

Definition at line 258 of file libubus.c.

260 {
261  struct ubus_request req;
262  void *s;
263 
264  blob_buf_init(&b, 0);
265  blob_put_int32(&b, UBUS_ATTR_OBJID, UBUS_SYSTEM_OBJECT_EVENT);
266  blob_put_string(&b, UBUS_ATTR_METHOD, "send");
267  s = blob_nest_start(&b, UBUS_ATTR_DATA);
268  blobmsg_add_string(&b, "id", id);
269  blobmsg_add_field(&b, BLOBMSG_TYPE_TABLE, "data", blob_data(data), blob_len(data));
270  blob_nest_end(&b, s);
271 
274 
275  return ubus_complete_request(ctx, &req, 0);
276 }
@ UBUS_ATTR_DATA
Definition: ubusmsg.h:91
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ubus_send_reply()

int ubus_send_reply ( struct ubus_context ctx,
struct ubus_request_data req,
struct blob_attr *  msg 
)

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

207 {
208  int ret;
209 
210  blob_buf_init(&b, 0);
211  blob_put_int32(&b, UBUS_ATTR_OBJID, req->object);
212  ubus_put_data(&b, msg);
213  ret = ubus_send_msg(ctx, req->seq, b.head, UBUS_MSG_DATA, req->peer, -1);
214  if (ret < 0)
215  return UBUS_STATUS_NO_DATA;
216 
217  return 0;
218 }
@ UBUS_MSG_DATA
Definition: ubusmsg.h:44
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ubus_shutdown()

void ubus_shutdown ( struct ubus_context ctx)

Definition at line 367 of file libubus.c.

368 {
369  blob_buf_free(&b);
370  if (!ctx)
371  return;
372  uloop_fd_delete(&ctx->sock);
373  close(ctx->sock.fd);
374  uloop_timeout_cancel(&ctx->pending_timer);
375  free(ctx->msgbuf.data);
376 }
Here is the caller graph for this function:

◆ ubus_strerror()

const char* ubus_strerror ( int  error)

Definition at line 59 of file libubus.c.

60 {
61  static char err[32];
62 
63  if (error < 0 || error >= __UBUS_STATUS_LAST)
64  goto out;
65 
66  if (!__ubus_strerror[error])
67  goto out;
68 
69  return __ubus_strerror[error];
70 
71 out:
72  sprintf(err, "Unknown error: %d", error);
73  return err;
74 }
const char * __ubus_strerror[__UBUS_STATUS_LAST]
Definition: libubus.c:25
@ __UBUS_STATUS_LAST
Definition: ubusmsg.h:133
Here is the caller graph for this function:

◆ ubus_subscribe()

int ubus_subscribe ( struct ubus_context ctx,
struct ubus_subscriber obj,
uint32_t  id 
)

Definition at line 118 of file libubus-sub.c.

119 {
121 }
static int __ubus_subscribe_request(struct ubus_context *ctx, struct ubus_object *obj, uint32_t id, int type)
Definition: libubus-sub.c:103
@ UBUS_MSG_SUBSCRIBE
Definition: ubusmsg.h:63
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ubus_unregister_event_handler()

static int ubus_unregister_event_handler ( struct ubus_context ctx,
struct ubus_event_handler ev 
)
inlinestatic

Definition at line 423 of file libubus.h.

425 {
426  return ubus_remove_object(ctx, &ev->obj);
427 }
int ubus_remove_object(struct ubus_context *ctx, struct ubus_object *obj)
Definition: libubus-obj.c:256

◆ ubus_unregister_subscriber()

static int ubus_unregister_subscriber ( struct ubus_context ctx,
struct ubus_subscriber obj 
)
inlinestatic

Definition at line 309 of file libubus.h.

310 {
311  if (!list_empty(&obj->list))
312  list_del_init(&obj->list);
313  return ubus_remove_object(ctx, &obj->obj);
314 }
struct list_head list
Definition: libubus.h:143
Here is the call graph for this function:

◆ ubus_unsubscribe()

int ubus_unsubscribe ( struct ubus_context ctx,
struct ubus_subscriber obj,
uint32_t  id 
)

Definition at line 123 of file libubus-sub.c.

124 {
126 }
@ UBUS_MSG_UNSUBSCRIBE
Definition: ubusmsg.h:64
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ acl_objects

struct avl_tree acl_objects
extern