libubus: use calloc_a for queued msgs to reduce the number of allocations
[project/ubus.git] / libubus.c
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
25 const char *__ubus_strerror[__UBUS_STATUS_LAST] = {
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 };
38
39 struct blob_buf b __hidden = {};
40
41 struct ubus_pending_msg {
42 struct list_head list;
43 struct ubus_msghdr_buf hdr;
44 };
45
46 static int ubus_cmp_id(const void *k1, const void *k2, void *ptr)
47 {
48 const uint32_t *id1 = k1, *id2 = k2;
49
50 if (*id1 < *id2)
51 return -1;
52 else
53 return *id1 > *id2;
54 }
55
56 const char *ubus_strerror(int error)
57 {
58 static char err[32];
59
60 if (error < 0 || error >= __UBUS_STATUS_LAST)
61 goto out;
62
63 if (!__ubus_strerror[error])
64 goto out;
65
66 return __ubus_strerror[error];
67
68 out:
69 sprintf(err, "Unknown error: %d", error);
70 return err;
71 }
72
73 static void
74 ubus_queue_msg(struct ubus_context *ctx, struct ubus_msghdr *hdr)
75 {
76 struct ubus_pending_msg *pending;
77 void *data;
78
79 pending = calloc_a(sizeof(*pending),
80 &data, blob_raw_len(ubus_msghdr_data(hdr)));
81
82 pending->hdr.data = data;
83 memcpy(&pending->hdr.hdr, hdr, sizeof(*hdr));
84 memcpy(data, ubus_msghdr_data(hdr), blob_raw_len(ubus_msghdr_data(hdr)));
85 list_add(&pending->list, &ctx->pending);
86 if (ctx->sock.registered)
87 uloop_timeout_set(&ctx->pending_timer, 1);
88 }
89
90 void __hidden
91 ubus_process_msg(struct ubus_context *ctx, struct ubus_msghdr *hdr, int fd)
92 {
93
94 switch(hdr->type) {
95 case UBUS_MSG_STATUS:
96 case UBUS_MSG_DATA:
97 ubus_process_req_msg(ctx, hdr, fd);
98 break;
99
100 case UBUS_MSG_INVOKE:
101 case UBUS_MSG_UNSUBSCRIBE:
102 case UBUS_MSG_NOTIFY:
103 if (ctx->stack_depth) {
104 ubus_queue_msg(ctx, hdr);
105 break;
106 }
107
108 ubus_process_obj_msg(ctx, hdr);
109 break;
110 }
111 }
112
113 static void ubus_process_pending_msg(struct uloop_timeout *timeout)
114 {
115 struct ubus_context *ctx = container_of(timeout, struct ubus_context, pending_timer);
116 struct ubus_pending_msg *pending;
117
118 while (!ctx->stack_depth && !list_empty(&ctx->pending)) {
119 pending = list_first_entry(&ctx->pending, struct ubus_pending_msg, list);
120 list_del(&pending->list);
121 ubus_process_msg(ctx, &pending->hdr.hdr, -1);
122 free(pending);
123 }
124 }
125
126 struct ubus_lookup_request {
127 struct ubus_request req;
128 ubus_lookup_handler_t cb;
129 };
130
131 static void ubus_lookup_cb(struct ubus_request *ureq, int type, struct blob_attr *msg)
132 {
133 struct ubus_lookup_request *req;
134 struct ubus_object_data obj;
135 struct blob_attr **attr;
136
137 req = container_of(ureq, struct ubus_lookup_request, req);
138 attr = ubus_parse_msg(msg);
139
140 if (!attr[UBUS_ATTR_OBJID] || !attr[UBUS_ATTR_OBJPATH] ||
141 !attr[UBUS_ATTR_OBJTYPE])
142 return;
143
144 memset(&obj, 0, sizeof(obj));
145 obj.id = blob_get_u32(attr[UBUS_ATTR_OBJID]);
146 obj.path = blob_data(attr[UBUS_ATTR_OBJPATH]);
147 obj.type_id = blob_get_u32(attr[UBUS_ATTR_OBJTYPE]);
148 obj.signature = attr[UBUS_ATTR_SIGNATURE];
149 req->cb(ureq->ctx, &obj, ureq->priv);
150 }
151
152 int ubus_lookup(struct ubus_context *ctx, const char *path,
153 ubus_lookup_handler_t cb, void *priv)
154 {
155 struct ubus_lookup_request lookup;
156
157 blob_buf_init(&b, 0);
158 if (path)
159 blob_put_string(&b, UBUS_ATTR_OBJPATH, path);
160
161 if (ubus_start_request(ctx, &lookup.req, b.head, UBUS_MSG_LOOKUP, 0) < 0)
162 return UBUS_STATUS_INVALID_ARGUMENT;
163
164 lookup.req.raw_data_cb = ubus_lookup_cb;
165 lookup.req.priv = priv;
166 lookup.cb = cb;
167 return ubus_complete_request(ctx, &lookup.req, 0);
168 }
169
170 static void ubus_lookup_id_cb(struct ubus_request *req, int type, struct blob_attr *msg)
171 {
172 struct blob_attr **attr;
173 uint32_t *id = req->priv;
174
175 attr = ubus_parse_msg(msg);
176
177 if (!attr[UBUS_ATTR_OBJID])
178 return;
179
180 *id = blob_get_u32(attr[UBUS_ATTR_OBJID]);
181 }
182
183 int ubus_lookup_id(struct ubus_context *ctx, const char *path, uint32_t *id)
184 {
185 struct ubus_request req;
186
187 blob_buf_init(&b, 0);
188 if (path)
189 blob_put_string(&b, UBUS_ATTR_OBJPATH, path);
190
191 if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_LOOKUP, 0) < 0)
192 return UBUS_STATUS_INVALID_ARGUMENT;
193
194 req.raw_data_cb = ubus_lookup_id_cb;
195 req.priv = id;
196
197 return ubus_complete_request(ctx, &req, 0);
198 }
199
200 static int ubus_event_cb(struct ubus_context *ctx, struct ubus_object *obj,
201 struct ubus_request_data *req,
202 const char *method, struct blob_attr *msg)
203 {
204 struct ubus_event_handler *ev;
205
206 ev = container_of(obj, struct ubus_event_handler, obj);
207 ev->cb(ctx, ev, method, msg);
208 return 0;
209 }
210
211 static const struct ubus_method event_method = {
212 .name = NULL,
213 .handler = ubus_event_cb,
214 };
215
216 int ubus_register_event_handler(struct ubus_context *ctx,
217 struct ubus_event_handler *ev,
218 const char *pattern)
219 {
220 struct ubus_object *obj = &ev->obj;
221 struct blob_buf b2;
222 int ret;
223
224 if (!obj->id) {
225 obj->methods = &event_method;
226 obj->n_methods = 1;
227
228 if (!!obj->name ^ !!obj->type)
229 return UBUS_STATUS_INVALID_ARGUMENT;
230
231 ret = ubus_add_object(ctx, obj);
232 if (ret)
233 return ret;
234 }
235
236 /* use a second buffer, ubus_invoke() overwrites the primary one */
237 memset(&b2, 0, sizeof(b2));
238 blob_buf_init(&b2, 0);
239 blobmsg_add_u32(&b2, "object", obj->id);
240 if (pattern)
241 blobmsg_add_string(&b2, "pattern", pattern);
242
243 ret = ubus_invoke(ctx, UBUS_SYSTEM_OBJECT_EVENT, "register", b2.head,
244 NULL, NULL, 0);
245 blob_buf_free(&b2);
246
247 return ret;
248 }
249
250 int ubus_send_event(struct ubus_context *ctx, const char *id,
251 struct blob_attr *data)
252 {
253 struct ubus_request req;
254 void *s;
255
256 blob_buf_init(&b, 0);
257 blob_put_int32(&b, UBUS_ATTR_OBJID, UBUS_SYSTEM_OBJECT_EVENT);
258 blob_put_string(&b, UBUS_ATTR_METHOD, "send");
259 s = blob_nest_start(&b, UBUS_ATTR_DATA);
260 blobmsg_add_string(&b, "id", id);
261 blobmsg_add_field(&b, BLOBMSG_TYPE_TABLE, "data", blob_data(data), blob_len(data));
262 blob_nest_end(&b, s);
263
264 if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_INVOKE, UBUS_SYSTEM_OBJECT_EVENT) < 0)
265 return UBUS_STATUS_INVALID_ARGUMENT;
266
267 return ubus_complete_request(ctx, &req, 0);
268 }
269
270 static void ubus_default_connection_lost(struct ubus_context *ctx)
271 {
272 if (ctx->sock.registered)
273 uloop_end();
274 }
275
276 static int _ubus_connect(struct ubus_context *ctx, const char *path)
277 {
278 ctx->sock.fd = -1;
279 ctx->sock.cb = ubus_handle_data;
280 ctx->connection_lost = ubus_default_connection_lost;
281 ctx->pending_timer.cb = ubus_process_pending_msg;
282
283 ctx->msgbuf.data = calloc(UBUS_MSG_CHUNK_SIZE, sizeof(char));
284 if (!ctx->msgbuf.data)
285 return -1;
286 ctx->msgbuf_data_len = UBUS_MSG_CHUNK_SIZE;
287
288 INIT_LIST_HEAD(&ctx->requests);
289 INIT_LIST_HEAD(&ctx->pending);
290 avl_init(&ctx->objects, ubus_cmp_id, false, NULL);
291 if (ubus_reconnect(ctx, path)) {
292 free(ctx->msgbuf.data);
293 return -1;
294 }
295
296 return 0;
297 }
298
299 static void ubus_auto_reconnect_cb(struct uloop_timeout *timeout)
300 {
301 struct ubus_auto_conn *conn = container_of(timeout, struct ubus_auto_conn, timer);
302
303 if (!ubus_reconnect(&conn->ctx, conn->path))
304 ubus_add_uloop(&conn->ctx);
305 else
306 uloop_timeout_set(timeout, 1000);
307 }
308
309 static void ubus_auto_disconnect_cb(struct ubus_context *ctx)
310 {
311 struct ubus_auto_conn *conn = container_of(ctx, struct ubus_auto_conn, ctx);
312
313 conn->timer.cb = ubus_auto_reconnect_cb;
314 uloop_timeout_set(&conn->timer, 1000);
315 }
316
317 static void ubus_auto_connect_cb(struct uloop_timeout *timeout)
318 {
319 struct ubus_auto_conn *conn = container_of(timeout, struct ubus_auto_conn, timer);
320
321 if (_ubus_connect(&conn->ctx, conn->path)) {
322 uloop_timeout_set(timeout, 1000);
323 fprintf(stderr, "failed to connect to ubus\n");
324 return;
325 }
326 conn->ctx.connection_lost = ubus_auto_disconnect_cb;
327 if (conn->cb)
328 conn->cb(&conn->ctx);
329 ubus_add_uloop(&conn->ctx);
330 }
331
332 void ubus_auto_connect(struct ubus_auto_conn *conn)
333 {
334 conn->timer.cb = ubus_auto_connect_cb;
335 ubus_auto_connect_cb(&conn->timer);
336 }
337
338 struct ubus_context *ubus_connect(const char *path)
339 {
340 struct ubus_context *ctx;
341
342 ctx = calloc(1, sizeof(*ctx));
343 if (!ctx)
344 return NULL;
345
346 if (_ubus_connect(ctx, path)) {
347 free(ctx);
348 ctx = NULL;
349 }
350
351 return ctx;
352 }
353
354 void ubus_free(struct ubus_context *ctx)
355 {
356 blob_buf_free(&b);
357 close(ctx->sock.fd);
358 free(ctx->msgbuf.data);
359 free(ctx);
360 }