Ubus
OpenWrt system message/RPC bus.
libubus.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2011-2014 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 <sys/types.h>
15 #include <sys/socket.h>
16 #include <unistd.h>
17 
18 #include <libubox/blob.h>
19 #include <libubox/blobmsg.h>
20 
21 #include "libubus.h"
22 #include "libubus-internal.h"
23 #include "ubusmsg.h"
24 
26  [UBUS_STATUS_OK] = "Success",
27  [UBUS_STATUS_INVALID_COMMAND] = "Invalid command",
28  [UBUS_STATUS_INVALID_ARGUMENT] = "Invalid argument",
29  [UBUS_STATUS_METHOD_NOT_FOUND] = "Method not found",
30  [UBUS_STATUS_NOT_FOUND] = "Not found",
31  [UBUS_STATUS_NO_DATA] = "No response",
32  [UBUS_STATUS_PERMISSION_DENIED] = "Permission denied",
33  [UBUS_STATUS_TIMEOUT] = "Request timed out",
34  [UBUS_STATUS_NOT_SUPPORTED] = "Operation not supported",
35  [UBUS_STATUS_UNKNOWN_ERROR] = "Unknown error",
36  [UBUS_STATUS_CONNECTION_FAILED] = "Connection failed",
37  [UBUS_STATUS_NO_MEMORY] = "Out of memory",
38  [UBUS_STATUS_PARSE_ERROR] = "Parsing message data failed",
39  [UBUS_STATUS_SYSTEM_ERROR] = "System error",
40 };
41 
42 struct blob_buf b __hidden = {};
43 
45  struct list_head list;
46  struct ubus_msghdr_buf hdr;
47 };
48 
49 static int ubus_cmp_id(const void *k1, const void *k2, void *ptr)
50 {
51  const uint32_t *id1 = k1, *id2 = k2;
52 
53  if (*id1 < *id2)
54  return -1;
55  else
56  return *id1 > *id2;
57 }
58 
59 const char *ubus_strerror(int error)
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 }
75 
76 static void
78 {
79  struct ubus_pending_msg *pending;
80  void *data;
81 
82  pending = calloc_a(sizeof(*pending), &data, blob_raw_len(buf->data));
83 
84  pending->hdr.data = data;
85  memcpy(&pending->hdr.hdr, &buf->hdr, sizeof(buf->hdr));
86  memcpy(data, buf->data, blob_raw_len(buf->data));
87  list_add_tail(&pending->list, &ctx->pending);
88  if (ctx->sock.registered)
89  uloop_timeout_set(&ctx->pending_timer, 1);
90 }
91 
92 void __hidden
93 ubus_process_msg(struct ubus_context *ctx, struct ubus_msghdr_buf *buf, int fd)
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 }
119 
120 static void ubus_process_pending_msg(struct uloop_timeout *timeout)
121 {
122  struct ubus_context *ctx = container_of(timeout, struct ubus_context, pending_timer);
123  struct ubus_pending_msg *pending;
124 
125  while (!list_empty(&ctx->pending)) {
126  if (ctx->stack_depth)
127  break;
128 
129  pending = list_first_entry(&ctx->pending, struct ubus_pending_msg, list);
130  list_del(&pending->list);
131  ubus_process_msg(ctx, &pending->hdr, -1);
132  free(pending);
133  }
134 }
135 
137  struct ubus_request req;
139 };
140 
141 static void ubus_lookup_cb(struct ubus_request *ureq, int type, struct blob_attr *msg)
142 {
143  struct ubus_lookup_request *req;
144  struct ubus_object_data obj = {};
145  struct blob_attr **attr;
146 
147  req = container_of(ureq, struct ubus_lookup_request, req);
148  attr = ubus_parse_msg(msg, blob_raw_len(msg));
149 
150  if (!attr[UBUS_ATTR_OBJID] || !attr[UBUS_ATTR_OBJPATH] ||
151  !attr[UBUS_ATTR_OBJTYPE])
152  return;
153 
154  obj.id = blob_get_u32(attr[UBUS_ATTR_OBJID]);
155  obj.path = blob_data(attr[UBUS_ATTR_OBJPATH]);
156  obj.type_id = blob_get_u32(attr[UBUS_ATTR_OBJTYPE]);
157  obj.signature = attr[UBUS_ATTR_SIGNATURE];
158  req->cb(ureq->ctx, &obj, ureq->priv);
159 }
160 
161 int ubus_lookup(struct ubus_context *ctx, const char *path,
162  ubus_lookup_handler_t cb, void *priv)
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 }
178 
179 static void ubus_lookup_id_cb(struct ubus_request *req, int type, struct blob_attr *msg)
180 {
181  struct blob_attr **attr;
182  uint32_t *id = req->priv;
183 
184  attr = ubus_parse_msg(msg, blob_raw_len(msg));
185 
186  if (!attr[UBUS_ATTR_OBJID])
187  return;
188 
189  *id = blob_get_u32(attr[UBUS_ATTR_OBJID]);
190 }
191 
192 int ubus_lookup_id(struct ubus_context *ctx, const char *path, uint32_t *id)
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 
204  req.priv = id;
205 
206  return ubus_complete_request(ctx, &req, 0);
207 }
208 
209 static int ubus_event_cb(struct ubus_context *ctx, struct ubus_object *obj,
210  struct ubus_request_data *req,
211  const char *method, struct blob_attr *msg)
212 {
213  struct ubus_event_handler *ev;
214 
215  ev = container_of(obj, struct ubus_event_handler, obj);
216  ev->cb(ctx, ev, method, msg);
217  return 0;
218 }
219 
220 static const struct ubus_method event_method = {
221  .name = NULL,
222  .handler = ubus_event_cb,
223 };
224 
226  struct ubus_event_handler *ev,
227  const char *pattern)
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 }
257 
258 int ubus_send_event(struct ubus_context *ctx, const char *id,
259  struct blob_attr *data)
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 }
277 
279 {
280  if (ctx->sock.registered)
281  uloop_end();
282 }
283 
284 int ubus_connect_ctx(struct ubus_context *ctx, const char *path)
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 }
311 
312 static void ubus_auto_reconnect_cb(struct uloop_timeout *timeout)
313 {
314  struct ubus_auto_conn *conn = container_of(timeout, struct ubus_auto_conn, timer);
315 
316  if (!ubus_reconnect(&conn->ctx, conn->path))
317  ubus_add_uloop(&conn->ctx);
318  else
319  uloop_timeout_set(timeout, 1000);
320 }
321 
323 {
324  struct ubus_auto_conn *conn = container_of(ctx, struct ubus_auto_conn, ctx);
325 
326  conn->timer.cb = ubus_auto_reconnect_cb;
327  uloop_timeout_set(&conn->timer, 1000);
328 }
329 
330 static void ubus_auto_connect_cb(struct uloop_timeout *timeout)
331 {
332  struct ubus_auto_conn *conn = container_of(timeout, struct ubus_auto_conn, timer);
333 
334  if (ubus_connect_ctx(&conn->ctx, conn->path)) {
335  uloop_timeout_set(timeout, 1000);
336  fprintf(stderr, "failed to connect to ubus\n");
337  return;
338  }
340  if (conn->cb)
341  conn->cb(&conn->ctx);
342  ubus_add_uloop(&conn->ctx);
343 }
344 
346 {
347  conn->timer.cb = ubus_auto_connect_cb;
348  ubus_auto_connect_cb(&conn->timer);
349 }
350 
351 struct ubus_context *ubus_connect(const char *path)
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 }
366 
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 }
377 
379 {
381  free(ctx);
382 }
static int timeout
Definition: cli.c:21
static struct blob_buf b
Definition: cli.c:19
int(* cb)(struct ubus_context *ctx, int argc, char **argv)
Definition: cli.c:581
static struct ubus_context * ctx
Definition: client.c:22
void ubus_process_obj_msg(struct ubus_context *ctx, struct ubus_msghdr_buf *buf, int fd)
Definition: libubus-obj.c:115
void ubus_handle_data(struct uloop_fd *u, unsigned int events)
Definition: libubus-io.c:312
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
void ubus_process_req_msg(struct ubus_context *ctx, struct ubus_msghdr_buf *buf, int fd)
Definition: libubus-req.c:462
struct blob_attr ** ubus_parse_msg(struct blob_attr *msg, size_t len)
Definition: libubus-io.c:46
int ubus_reconnect(struct ubus_context *ctx, const char *path)
Definition: libubus-io.c:395
int ubus_add_object(struct ubus_context *ctx, struct ubus_object *obj)
Definition: libubus-obj.c:209
int ubus_complete_request(struct ubus_context *ctx, struct ubus_request *req, int req_timeout)
Definition: libubus-req.c:140
struct ubus_context * ubus_connect(const char *path)
Definition: libubus.c:351
static int ubus_cmp_id(const void *k1, const void *k2, void *ptr)
Definition: libubus.c:49
void ubus_auto_connect(struct ubus_auto_conn *conn)
Definition: libubus.c:345
int ubus_lookup(struct ubus_context *ctx, const char *path, ubus_lookup_handler_t cb, void *priv)
Definition: libubus.c:161
static void ubus_auto_reconnect_cb(struct uloop_timeout *timeout)
Definition: libubus.c:312
static const struct ubus_method event_method
Definition: libubus.c:220
static void ubus_auto_connect_cb(struct uloop_timeout *timeout)
Definition: libubus.c:330
struct blob_buf b __hidden
Definition: libubus.c:42
int ubus_send_event(struct ubus_context *ctx, const char *id, struct blob_attr *data)
Definition: libubus.c:258
static void ubus_lookup_cb(struct ubus_request *ureq, int type, struct blob_attr *msg)
Definition: libubus.c:141
void ubus_free(struct ubus_context *ctx)
Definition: libubus.c:378
static void ubus_queue_msg(struct ubus_context *ctx, struct ubus_msghdr_buf *buf)
Definition: libubus.c:77
void ubus_shutdown(struct ubus_context *ctx)
Definition: libubus.c:367
int ubus_connect_ctx(struct ubus_context *ctx, const char *path)
Definition: libubus.c:284
int ubus_lookup_id(struct ubus_context *ctx, const char *path, uint32_t *id)
Definition: libubus.c:192
const char * ubus_strerror(int error)
Definition: libubus.c:59
void __hidden ubus_process_msg(struct ubus_context *ctx, struct ubus_msghdr_buf *buf, int fd)
Definition: libubus.c:93
static void ubus_auto_disconnect_cb(struct ubus_context *ctx)
Definition: libubus.c:322
static void ubus_default_connection_lost(struct ubus_context *ctx)
Definition: libubus.c:278
static int ubus_event_cb(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg)
Definition: libubus.c:209
const char * __ubus_strerror[__UBUS_STATUS_LAST]
Definition: libubus.c:25
int ubus_register_event_handler(struct ubus_context *ctx, struct ubus_event_handler *ev, const char *pattern)
Definition: libubus.c:225
static void ubus_lookup_id_cb(struct ubus_request *req, int type, struct blob_attr *msg)
Definition: libubus.c:179
static void ubus_process_pending_msg(struct uloop_timeout *timeout)
Definition: libubus.c:120
static void ubus_add_uloop(struct ubus_context *ctx)
Definition: libubus.h:268
void(* ubus_lookup_handler_t)(struct ubus_context *ctx, struct ubus_object_data *obj, void *priv)
Definition: libubus.h:46
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
const char * path
Definition: libubus.h:245
struct uloop_timeout timer
Definition: libubus.h:244
ubus_connect_handler_t cb
Definition: libubus.h:246
struct ubus_context ctx
Definition: libubus.h:243
int stack_depth
Definition: libubus.h:168
uint32_t msgbuf_data_len
Definition: libubus.h:174
struct uloop_timeout pending_timer
Definition: libubus.h:163
struct list_head requests
Definition: libubus.h:158
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
void(* monitor_cb)(struct ubus_context *ctx, uint32_t seq, struct blob_attr *data)
Definition: libubus.h:171
struct list_head auto_subscribers
Definition: libubus.h:177
struct list_head pending
Definition: libubus.h:160
struct uloop_fd sock
Definition: libubus.h:162
ubus_event_handler_t cb
Definition: libubus.h:154
struct ubus_object obj
Definition: libubus.h:152
struct ubus_request req
Definition: libubus.c:137
ubus_lookup_handler_t cb
Definition: libubus.c:138
const char * name
Definition: libubus.h:109
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
uint32_t id
Definition: libubus.h:182
struct blob_attr * signature
Definition: libubus.h:185
const char * path
Definition: libubus.h:184
uint32_t type_id
Definition: libubus.h:183
uint32_t id
Definition: libubus.h:130
struct ubus_object_type * type
Definition: libubus.h:133
const struct ubus_method * methods
Definition: libubus.h:138
int n_methods
Definition: libubus.h:139
const char * name
Definition: libubus.h:129
struct list_head list
Definition: libubus.c:45
struct ubus_msghdr_buf hdr
Definition: libubus.c:46
ubus_data_handler_t raw_data_cb
Definition: libubus.h:220
void * priv
Definition: libubus.h:228
struct ubus_context * ctx
Definition: libubus.h:227
struct avl_tree path
Definition: ubusd_obj.c:19
#define UBUS_MSG_CHUNK_SIZE
Definition: ubusmsg.h:22
uint8_t type
Definition: ubusmsg.h:1
#define UBUS_SYSTEM_OBJECT_EVENT
Definition: ubusmsg.h:24
@ UBUS_MSG_INVOKE
Definition: ubusmsg.h:53
@ UBUS_MSG_DATA
Definition: ubusmsg.h:44
@ UBUS_MSG_STATUS
Definition: ubusmsg.h:41
@ UBUS_MSG_LOOKUP
Definition: ubusmsg.h:50
@ UBUS_MSG_NOTIFY
Definition: ubusmsg.h:71
@ UBUS_MSG_MONITOR
Definition: ubusmsg.h:73
@ UBUS_MSG_UNSUBSCRIBE
Definition: ubusmsg.h:64
@ UBUS_STATUS_INVALID_ARGUMENT
Definition: ubusmsg.h:121
@ UBUS_STATUS_NO_DATA
Definition: ubusmsg.h:124
@ UBUS_STATUS_TIMEOUT
Definition: ubusmsg.h:126
@ UBUS_STATUS_METHOD_NOT_FOUND
Definition: ubusmsg.h:122
@ UBUS_STATUS_NOT_FOUND
Definition: ubusmsg.h:123
@ UBUS_STATUS_INVALID_COMMAND
Definition: ubusmsg.h:120
@ UBUS_STATUS_CONNECTION_FAILED
Definition: ubusmsg.h:129
@ UBUS_STATUS_NO_MEMORY
Definition: ubusmsg.h:130
@ __UBUS_STATUS_LAST
Definition: ubusmsg.h:133
@ UBUS_STATUS_PERMISSION_DENIED
Definition: ubusmsg.h:125
@ UBUS_STATUS_OK
Definition: ubusmsg.h:119
@ UBUS_STATUS_UNKNOWN_ERROR
Definition: ubusmsg.h:128
@ UBUS_STATUS_SYSTEM_ERROR
Definition: ubusmsg.h:132
@ UBUS_STATUS_PARSE_ERROR
Definition: ubusmsg.h:131
@ UBUS_STATUS_NOT_SUPPORTED
Definition: ubusmsg.h:127
@ UBUS_ATTR_OBJTYPE
Definition: ubusmsg.h:88
@ UBUS_ATTR_OBJPATH
Definition: ubusmsg.h:84
@ UBUS_ATTR_METHOD
Definition: ubusmsg.h:86
@ UBUS_ATTR_OBJID
Definition: ubusmsg.h:85
@ UBUS_ATTR_SIGNATURE
Definition: ubusmsg.h:89
@ UBUS_ATTR_DATA
Definition: ubusmsg.h:91