libubus: reduce code duplication and add stack depth protection for unsubscribe/notif...
[project/ubus.git] / libubus.c
1 /*
2 * Copyright (C) 2011-2012 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_data {
42 struct list_head list;
43 int type;
44 struct blob_attr data[];
45 };
46
47 struct ubus_pending_invoke {
48 struct list_head list;
49 struct ubus_msghdr hdr;
50 };
51
52 static void ubus_process_pending_invoke(struct ubus_context *ctx);
53
54 static int ubus_cmp_id(const void *k1, const void *k2, void *ptr)
55 {
56 const uint32_t *id1 = k1, *id2 = k2;
57
58 if (*id1 < *id2)
59 return -1;
60 else
61 return *id1 > *id2;
62 }
63
64 const char *ubus_strerror(int error)
65 {
66 static char err[32];
67
68 if (error < 0 || error >= __UBUS_STATUS_LAST)
69 goto out;
70
71 if (!__ubus_strerror[error])
72 goto out;
73
74 return __ubus_strerror[error];
75
76 out:
77 sprintf(err, "Unknown error: %d", error);
78 return err;
79 }
80
81 static bool ubus_get_status(struct ubus_msghdr *hdr, int *ret)
82 {
83 struct blob_attr **attrbuf = ubus_parse_msg(hdr->data);
84
85 if (!attrbuf[UBUS_ATTR_STATUS])
86 return false;
87
88 *ret = blob_get_u32(attrbuf[UBUS_ATTR_STATUS]);
89 return true;
90 }
91
92 static void req_data_cb(struct ubus_request *req, int type, struct blob_attr *data)
93 {
94 struct blob_attr **attr;
95
96 if (req->raw_data_cb)
97 req->raw_data_cb(req, type, data);
98
99 if (!req->data_cb)
100 return;
101
102 attr = ubus_parse_msg(data);
103 req->data_cb(req, type, attr[UBUS_ATTR_DATA]);
104 }
105
106 static void ubus_process_req_data(struct ubus_request *req)
107 {
108 struct ubus_pending_data *data;
109
110 while (!list_empty(&req->pending)) {
111 data = list_first_entry(&req->pending,
112 struct ubus_pending_data, list);
113 list_del(&data->list);
114 if (!req->cancelled)
115 req_data_cb(req, data->type, data->data);
116 free(data);
117 }
118 }
119
120 static void ubus_req_complete_cb(struct ubus_request *req)
121 {
122 ubus_complete_handler_t cb = req->complete_cb;
123
124 if (!cb)
125 return;
126
127 req->complete_cb = NULL;
128 cb(req, req->status_code);
129 }
130
131 static int ubus_process_req_status(struct ubus_request *req, struct ubus_msghdr *hdr)
132 {
133 int ret = UBUS_STATUS_INVALID_ARGUMENT;
134
135 if (!list_empty(&req->list))
136 list_del_init(&req->list);
137
138 ubus_get_status(hdr, &ret);
139 req->peer = hdr->peer;
140 req->status_msg = true;
141 req->status_code = ret;
142 if (!req->blocked)
143 ubus_req_complete_cb(req);
144
145 return ret;
146 }
147
148 static void ubus_req_data(struct ubus_request *req, struct ubus_msghdr *hdr)
149 {
150 struct ubus_pending_data *data;
151 int len;
152
153 if (!req->blocked) {
154 req->blocked = true;
155 req_data_cb(req, hdr->type, hdr->data);
156 ubus_process_req_data(req);
157 req->blocked = false;
158
159 if (req->status_msg)
160 ubus_req_complete_cb(req);
161
162 return;
163 }
164
165 len = blob_raw_len(hdr->data);
166 data = calloc(1, sizeof(*data) + len);
167 if (!data)
168 return;
169
170 data->type = hdr->type;
171 memcpy(data->data, hdr->data, len);
172 list_add(&data->list, &req->pending);
173 }
174
175 static struct ubus_request *ubus_find_request(struct ubus_context *ctx, uint32_t seq, uint32_t peer)
176 {
177 struct ubus_request *req;
178
179 list_for_each_entry(req, &ctx->requests, list) {
180 if (seq != req->seq || peer != req->peer)
181 continue;
182
183 return req;
184 }
185 return NULL;
186 }
187
188 void ubus_complete_deferred_request(struct ubus_context *ctx, struct ubus_request_data *req, int ret)
189 {
190 blob_buf_init(&b, 0);
191 blob_put_int32(&b, UBUS_ATTR_STATUS, ret);
192 blob_put_int32(&b, UBUS_ATTR_OBJID, req->object);
193 ubus_send_msg(ctx, req->seq, b.head, UBUS_MSG_STATUS, req->peer);
194 }
195
196 void __hidden ubus_process_msg(struct ubus_context *ctx, struct ubus_msghdr *hdr)
197 {
198 struct ubus_pending_invoke *pending;
199 struct ubus_request *req;
200
201 switch(hdr->type) {
202 case UBUS_MSG_STATUS:
203 req = ubus_find_request(ctx, hdr->seq, hdr->peer);
204 if (!req)
205 break;
206
207 ubus_process_req_status(req, hdr);
208 break;
209
210 case UBUS_MSG_DATA:
211 req = ubus_find_request(ctx, hdr->seq, hdr->peer);
212 if (req && (req->data_cb || req->raw_data_cb))
213 ubus_req_data(req, hdr);
214 break;
215
216 case UBUS_MSG_INVOKE:
217 case UBUS_MSG_UNSUBSCRIBE:
218 case UBUS_MSG_NOTIFY:
219 if (ctx->stack_depth > 2) {
220 pending = calloc(1, sizeof(*pending) +
221 blob_raw_len(hdr->data));
222
223 if (!pending)
224 return;
225
226 memcpy(&pending->hdr, hdr, sizeof(*hdr) +
227 blob_raw_len(hdr->data));
228 list_add(&pending->list, &ctx->pending);
229 } else {
230 ubus_process_obj_msg(ctx, hdr);
231 }
232 break;
233 }
234 }
235
236 static void ubus_process_pending_invoke(struct ubus_context *ctx)
237 {
238 struct ubus_pending_invoke *pending, *tmp;
239
240 list_for_each_entry_safe(pending, tmp, &ctx->pending, list) {
241 list_del(&pending->list);
242 ubus_process_msg(ctx, &pending->hdr);
243 free(pending);
244 if (ctx->stack_depth > 2)
245 break;
246 }
247 }
248
249 void ubus_abort_request(struct ubus_context *ctx, struct ubus_request *req)
250 {
251 if (!list_empty(&req->list))
252 return;
253
254 req->cancelled = true;
255 ubus_process_req_data(req);
256 list_del_init(&req->list);
257 }
258
259 void ubus_complete_request_async(struct ubus_context *ctx, struct ubus_request *req)
260 {
261 if (!list_empty(&req->list))
262 return;
263
264 list_add(&req->list, &ctx->requests);
265 }
266
267 static void ubus_sync_req_cb(struct ubus_request *req, int ret)
268 {
269 req->status_msg = true;
270 req->status_code = ret;
271 uloop_end();
272 }
273
274 struct ubus_sync_req_cb {
275 struct uloop_timeout timeout;
276 struct ubus_request *req;
277 };
278
279 static void ubus_sync_req_timeout_cb(struct uloop_timeout *timeout)
280 {
281 struct ubus_sync_req_cb *cb;
282
283 cb = container_of(timeout, struct ubus_sync_req_cb, timeout);
284 ubus_sync_req_cb(cb->req, UBUS_STATUS_TIMEOUT);
285 }
286
287 int ubus_complete_request(struct ubus_context *ctx, struct ubus_request *req,
288 int timeout)
289 {
290 struct ubus_sync_req_cb cb;
291 ubus_complete_handler_t complete_cb = req->complete_cb;
292 bool registered = ctx->sock.registered;
293 int status = UBUS_STATUS_NO_DATA;
294
295 if (!registered) {
296 uloop_init();
297 ubus_add_uloop(ctx);
298 }
299
300 if (timeout) {
301 memset(&cb, 0, sizeof(cb));
302 cb.req = req;
303 cb.timeout.cb = ubus_sync_req_timeout_cb;
304 uloop_timeout_set(&cb.timeout, timeout);
305 }
306
307 ubus_complete_request_async(ctx, req);
308 req->complete_cb = ubus_sync_req_cb;
309
310 ctx->stack_depth++;
311 while (!req->status_msg) {
312 bool cancelled = uloop_cancelled;
313 uloop_cancelled = false;
314 uloop_run();
315 uloop_cancelled = cancelled;
316 }
317 ctx->stack_depth--;
318
319 if (timeout)
320 uloop_timeout_cancel(&cb.timeout);
321
322 if (req->status_msg)
323 status = req->status_code;
324
325 req->complete_cb = complete_cb;
326 if (req->complete_cb)
327 req->complete_cb(req, status);
328
329 if (!registered)
330 uloop_fd_delete(&ctx->sock);
331
332 if (!ctx->stack_depth)
333 ubus_process_pending_invoke(ctx);
334
335 return status;
336 }
337
338 struct ubus_lookup_request {
339 struct ubus_request req;
340 ubus_lookup_handler_t cb;
341 };
342
343 static void ubus_lookup_cb(struct ubus_request *ureq, int type, struct blob_attr *msg)
344 {
345 struct ubus_lookup_request *req;
346 struct ubus_object_data obj;
347 struct blob_attr **attr;
348
349 req = container_of(ureq, struct ubus_lookup_request, req);
350 attr = ubus_parse_msg(msg);
351
352 if (!attr[UBUS_ATTR_OBJID] || !attr[UBUS_ATTR_OBJPATH] ||
353 !attr[UBUS_ATTR_OBJTYPE])
354 return;
355
356 memset(&obj, 0, sizeof(obj));
357 obj.id = blob_get_u32(attr[UBUS_ATTR_OBJID]);
358 obj.path = blob_data(attr[UBUS_ATTR_OBJPATH]);
359 obj.type_id = blob_get_u32(attr[UBUS_ATTR_OBJTYPE]);
360 obj.signature = attr[UBUS_ATTR_SIGNATURE];
361 req->cb(ureq->ctx, &obj, ureq->priv);
362 }
363
364 int __hidden ubus_start_request(struct ubus_context *ctx, struct ubus_request *req,
365 struct blob_attr *msg, int cmd, uint32_t peer)
366 {
367 memset(req, 0, sizeof(*req));
368
369 if (msg && blob_pad_len(msg) > UBUS_MAX_MSGLEN)
370 return -1;
371
372 INIT_LIST_HEAD(&req->list);
373 INIT_LIST_HEAD(&req->pending);
374 req->ctx = ctx;
375 req->peer = peer;
376 req->seq = ++ctx->request_seq;
377 return ubus_send_msg(ctx, req->seq, msg, cmd, peer);
378 }
379
380 int ubus_lookup(struct ubus_context *ctx, const char *path,
381 ubus_lookup_handler_t cb, void *priv)
382 {
383 struct ubus_lookup_request lookup;
384
385 blob_buf_init(&b, 0);
386 if (path)
387 blob_put_string(&b, UBUS_ATTR_OBJPATH, path);
388
389 if (ubus_start_request(ctx, &lookup.req, b.head, UBUS_MSG_LOOKUP, 0) < 0)
390 return UBUS_STATUS_INVALID_ARGUMENT;
391
392 lookup.req.raw_data_cb = ubus_lookup_cb;
393 lookup.req.priv = priv;
394 lookup.cb = cb;
395 return ubus_complete_request(ctx, &lookup.req, 0);
396 }
397
398 static void ubus_lookup_id_cb(struct ubus_request *req, int type, struct blob_attr *msg)
399 {
400 struct blob_attr **attr;
401 uint32_t *id = req->priv;
402
403 attr = ubus_parse_msg(msg);
404
405 if (!attr[UBUS_ATTR_OBJID])
406 return;
407
408 *id = blob_get_u32(attr[UBUS_ATTR_OBJID]);
409 }
410
411 int ubus_lookup_id(struct ubus_context *ctx, const char *path, uint32_t *id)
412 {
413 struct ubus_request req;
414
415 blob_buf_init(&b, 0);
416 if (path)
417 blob_put_string(&b, UBUS_ATTR_OBJPATH, path);
418
419 if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_LOOKUP, 0) < 0)
420 return UBUS_STATUS_INVALID_ARGUMENT;
421
422 req.raw_data_cb = ubus_lookup_id_cb;
423 req.priv = id;
424
425 return ubus_complete_request(ctx, &req, 0);
426 }
427
428 int ubus_send_reply(struct ubus_context *ctx, struct ubus_request_data *req,
429 struct blob_attr *msg)
430 {
431 int ret;
432
433 blob_buf_init(&b, 0);
434 blob_put_int32(&b, UBUS_ATTR_OBJID, req->object);
435 blob_put(&b, UBUS_ATTR_DATA, blob_data(msg), blob_len(msg));
436 ret = ubus_send_msg(ctx, req->seq, b.head, UBUS_MSG_DATA, req->peer);
437 if (ret < 0)
438 return UBUS_STATUS_NO_DATA;
439
440 return 0;
441 }
442
443 int ubus_invoke_async(struct ubus_context *ctx, uint32_t obj, const char *method,
444 struct blob_attr *msg, struct ubus_request *req)
445 {
446 blob_buf_init(&b, 0);
447 blob_put_int32(&b, UBUS_ATTR_OBJID, obj);
448 blob_put_string(&b, UBUS_ATTR_METHOD, method);
449 if (msg)
450 blob_put(&b, UBUS_ATTR_DATA, blob_data(msg), blob_len(msg));
451
452 if (ubus_start_request(ctx, req, b.head, UBUS_MSG_INVOKE, obj) < 0)
453 return UBUS_STATUS_INVALID_ARGUMENT;
454
455 return 0;
456 }
457
458 int ubus_invoke(struct ubus_context *ctx, uint32_t obj, const char *method,
459 struct blob_attr *msg, ubus_data_handler_t cb, void *priv,
460 int timeout)
461 {
462 struct ubus_request req;
463
464 ubus_invoke_async(ctx, obj, method, msg, &req);
465 req.data_cb = cb;
466 req.priv = priv;
467 return ubus_complete_request(ctx, &req, timeout);
468 }
469
470
471
472 static int ubus_event_cb(struct ubus_context *ctx, struct ubus_object *obj,
473 struct ubus_request_data *req,
474 const char *method, struct blob_attr *msg)
475 {
476 struct ubus_event_handler *ev;
477
478 ev = container_of(obj, struct ubus_event_handler, obj);
479 ev->cb(ctx, ev, method, msg);
480 return 0;
481 }
482
483 static const struct ubus_method event_method = {
484 .name = NULL,
485 .handler = ubus_event_cb,
486 };
487
488 int ubus_register_event_handler(struct ubus_context *ctx,
489 struct ubus_event_handler *ev,
490 const char *pattern)
491 {
492 struct ubus_object *obj = &ev->obj;
493 struct blob_buf b2;
494 int ret;
495
496 if (!obj->id) {
497 obj->methods = &event_method;
498 obj->n_methods = 1;
499
500 if (!!obj->name ^ !!obj->type)
501 return UBUS_STATUS_INVALID_ARGUMENT;
502
503 ret = ubus_add_object(ctx, obj);
504 if (ret)
505 return ret;
506 }
507
508 /* use a second buffer, ubus_invoke() overwrites the primary one */
509 memset(&b2, 0, sizeof(b2));
510 blob_buf_init(&b2, 0);
511 blobmsg_add_u32(&b2, "object", obj->id);
512 if (pattern)
513 blobmsg_add_string(&b2, "pattern", pattern);
514
515 return ubus_invoke(ctx, UBUS_SYSTEM_OBJECT_EVENT, "register", b2.head,
516 NULL, NULL, 0);
517 }
518
519 int ubus_send_event(struct ubus_context *ctx, const char *id,
520 struct blob_attr *data)
521 {
522 struct ubus_request req;
523 void *s;
524
525 blob_buf_init(&b, 0);
526 blob_put_int32(&b, UBUS_ATTR_OBJID, UBUS_SYSTEM_OBJECT_EVENT);
527 blob_put_string(&b, UBUS_ATTR_METHOD, "send");
528 s = blob_nest_start(&b, UBUS_ATTR_DATA);
529 blobmsg_add_string(&b, "id", id);
530 blobmsg_add_field(&b, BLOBMSG_TYPE_TABLE, "data", blob_data(data), blob_len(data));
531 blob_nest_end(&b, s);
532
533 if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_INVOKE, UBUS_SYSTEM_OBJECT_EVENT) < 0)
534 return UBUS_STATUS_INVALID_ARGUMENT;
535
536 return ubus_complete_request(ctx, &req, 0);
537 }
538
539 static void ubus_default_connection_lost(struct ubus_context *ctx)
540 {
541 if (ctx->sock.registered)
542 uloop_end();
543 }
544
545 struct ubus_context *ubus_connect(const char *path)
546 {
547 struct ubus_context *ctx;
548
549 ctx = calloc(1, sizeof(*ctx));
550 if (!ctx)
551 return NULL;
552
553 ctx->sock.fd = -1;
554 ctx->sock.cb = ubus_handle_data;
555 ctx->connection_lost = ubus_default_connection_lost;
556
557 INIT_LIST_HEAD(&ctx->requests);
558 INIT_LIST_HEAD(&ctx->pending);
559 avl_init(&ctx->objects, ubus_cmp_id, false, NULL);
560 if (ubus_reconnect(ctx, path)) {
561 free(ctx);
562 ctx = NULL;
563 }
564
565 return ctx;
566 }
567
568 void ubus_free(struct ubus_context *ctx)
569 {
570 close(ctx->sock.fd);
571 free(ctx);
572 }