add an error code for "operation not supported"
[project/ubus.git] / libubus.c
1 /*
2 * Copyright (C) 2011 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/uio.h>
16 #include <sys/socket.h>
17 #include <fcntl.h>
18 #include <unistd.h>
19
20 #include <libubox/blob.h>
21 #include <libubox/blobmsg.h>
22 #include <libubox/usock.h>
23
24 #include "libubus.h"
25 #include "ubusmsg.h"
26
27 #define STATIC_IOV(_var) { .iov_base = (char *) &(_var), .iov_len = sizeof(_var) }
28
29 const char *__ubus_strerror[__UBUS_STATUS_LAST] = {
30 [UBUS_STATUS_OK] = "Success",
31 [UBUS_STATUS_INVALID_COMMAND] = "Invalid command",
32 [UBUS_STATUS_INVALID_ARGUMENT] = "Invalid argument",
33 [UBUS_STATUS_METHOD_NOT_FOUND] = "Method not found",
34 [UBUS_STATUS_NOT_FOUND] = "Not found",
35 [UBUS_STATUS_NO_DATA] = "No response",
36 [UBUS_STATUS_PERMISSION_DENIED] = "Permission denied",
37 [UBUS_STATUS_TIMEOUT] = "Request timed out",
38 [UBUS_STATUS_NOT_SUPPORTED] = "Operation not supported",
39 };
40
41 static struct blob_buf b;
42
43 static const struct blob_attr_info ubus_policy[UBUS_ATTR_MAX] = {
44 [UBUS_ATTR_STATUS] = { .type = BLOB_ATTR_INT32 },
45 [UBUS_ATTR_OBJID] = { .type = BLOB_ATTR_INT32 },
46 [UBUS_ATTR_OBJPATH] = { .type = BLOB_ATTR_STRING },
47 [UBUS_ATTR_METHOD] = { .type = BLOB_ATTR_STRING },
48 };
49 static struct blob_attr *attrbuf[UBUS_ATTR_MAX];
50
51 struct ubus_pending_data {
52 struct list_head list;
53 int type;
54 struct blob_attr data[];
55 };
56
57 static int ubus_cmp_id(const void *k1, const void *k2, void *ptr)
58 {
59 const uint32_t *id1 = k1, *id2 = k2;
60
61 if (*id1 < *id2)
62 return -1;
63 else
64 return *id1 > *id2;
65 }
66
67 static struct blob_attr **ubus_parse_msg(struct blob_attr *msg)
68 {
69 blob_parse(msg, attrbuf, ubus_policy, UBUS_ATTR_MAX);
70 return attrbuf;
71 }
72
73 const char *ubus_strerror(int error)
74 {
75 static char err[32];
76
77 if (error < 0 || error >= __UBUS_STATUS_LAST)
78 goto out;
79
80 if (!__ubus_strerror[error])
81 goto out;
82
83 return __ubus_strerror[error];
84
85 out:
86 sprintf(err, "Unknown error: %d", error);
87 return err;
88 }
89
90 static int writev_retry(int fd, struct iovec *iov, int iov_len)
91 {
92 int len = 0;
93
94 do {
95 int cur_len = writev(fd, iov, iov_len);
96 if (cur_len < 0) {
97 switch(errno) {
98 case EAGAIN:
99 /* turn off non-blocking mode */
100 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) &
101 ~O_NONBLOCK);
102 break;
103 case EINTR:
104 break;
105 default:
106 return -1;
107 }
108 continue;
109 }
110 len += cur_len;
111 while (cur_len >= iov->iov_len) {
112 cur_len -= iov->iov_len;
113 iov_len--;
114 iov++;
115 if (!cur_len || !iov_len)
116 return len;
117 }
118 iov->iov_len -= cur_len;
119 } while (1);
120 }
121
122 static int ubus_send_msg(struct ubus_context *ctx, uint32_t seq,
123 struct blob_attr *msg, int cmd, uint32_t peer)
124 {
125 struct ubus_msghdr hdr;
126 struct iovec iov[2] = {
127 STATIC_IOV(hdr)
128 };
129
130 hdr.version = 0;
131 hdr.type = cmd;
132 hdr.seq = seq;
133 hdr.peer = peer;
134
135 if (!msg) {
136 blob_buf_init(&b, 0);
137 msg = b.head;
138 }
139
140 iov[1].iov_base = (char *) msg;
141 iov[1].iov_len = blob_raw_len(msg);
142
143 return writev_retry(ctx->sock.fd, iov, ARRAY_SIZE(iov));
144 }
145
146 static int ubus_start_request(struct ubus_context *ctx, struct ubus_request *req,
147 struct blob_attr *msg, int cmd, uint32_t peer)
148 {
149 memset(req, 0, sizeof(*req));
150
151 if (msg && blob_pad_len(msg) > UBUS_MAX_MSGLEN)
152 return -1;
153
154 INIT_LIST_HEAD(&req->list);
155 INIT_LIST_HEAD(&req->pending);
156 req->ctx = ctx;
157 req->peer = peer;
158 req->seq = ++ctx->request_seq;
159 return ubus_send_msg(ctx, req->seq, msg, cmd, peer);
160 }
161
162 static bool recv_retry(int fd, struct iovec *iov, bool wait)
163 {
164 int bytes;
165
166 while (iov->iov_len > 0) {
167 bytes = read(fd, iov->iov_base, iov->iov_len);
168 if (bytes < 0) {
169 bytes = 0;
170 if (uloop_cancelled)
171 return false;
172 if (errno == EINTR)
173 continue;
174
175 if (errno != EAGAIN)
176 return false;
177 }
178 if (!wait && !bytes)
179 return false;
180
181 wait = true;
182 iov->iov_len -= bytes;
183 iov->iov_base += bytes;
184 }
185
186 return true;
187 }
188
189 static bool ubus_validate_hdr(struct ubus_msghdr *hdr)
190 {
191 if (hdr->version != 0)
192 return false;
193
194 if (blob_raw_len(hdr->data) < sizeof(*hdr->data))
195 return false;
196
197 if (blob_pad_len(hdr->data) > UBUS_MAX_MSGLEN)
198 return false;
199
200 return true;
201 }
202
203 static bool get_next_msg(struct ubus_context *ctx)
204 {
205 struct iovec iov = STATIC_IOV(ctx->msgbuf.hdr);
206
207 /* receive header + start attribute */
208 iov.iov_len += sizeof(struct blob_attr);
209 if (!recv_retry(ctx->sock.fd, &iov, false))
210 return false;
211
212 iov.iov_len = blob_len(ctx->msgbuf.hdr.data);
213 if (iov.iov_len > 0 && !recv_retry(ctx->sock.fd, &iov, true))
214 return false;
215
216 return ubus_validate_hdr(&ctx->msgbuf.hdr);
217 }
218
219 static bool ubus_get_status(struct ubus_msghdr *hdr, int *ret)
220 {
221 ubus_parse_msg(hdr->data);
222
223 if (!attrbuf[UBUS_ATTR_STATUS])
224 return false;
225
226 *ret = blob_get_u32(attrbuf[UBUS_ATTR_STATUS]);
227 return true;
228 }
229
230 static void req_data_cb(struct ubus_request *req, int type, struct blob_attr *data)
231 {
232 struct blob_attr **attr;
233
234 if (req->raw_data_cb)
235 req->raw_data_cb(req, type, data);
236
237 if (!req->data_cb)
238 return;
239
240 attr = ubus_parse_msg(data);
241 req->data_cb(req, type, attr[UBUS_ATTR_DATA]);
242 }
243
244 static void ubus_process_req_data(struct ubus_request *req)
245 {
246 struct ubus_pending_data *data;
247
248 while (!list_empty(&req->pending)) {
249 data = list_first_entry(&req->pending,
250 struct ubus_pending_data, list);
251 list_del(&data->list);
252 if (!req->cancelled)
253 req_data_cb(req, data->type, data->data);
254 free(data);
255 }
256 }
257
258 static void ubus_req_complete_cb(struct ubus_request *req)
259 {
260 ubus_complete_handler_t cb = req->complete_cb;
261
262 if (!cb)
263 return;
264
265 req->complete_cb = NULL;
266 cb(req, req->status_code);
267 }
268
269 static int ubus_process_req_status(struct ubus_request *req, struct ubus_msghdr *hdr)
270 {
271 int ret = UBUS_STATUS_INVALID_ARGUMENT;
272
273 if (!list_empty(&req->list))
274 list_del(&req->list);
275
276 ubus_get_status(hdr, &ret);
277 req->peer = hdr->peer;
278 req->status_msg = true;
279 req->status_code = ret;
280 if (!req->blocked)
281 ubus_req_complete_cb(req);
282
283 return ret;
284 }
285
286 static void ubus_req_data(struct ubus_request *req, struct ubus_msghdr *hdr)
287 {
288 struct ubus_pending_data *data;
289 int len;
290
291 if (!req->blocked) {
292 req->blocked = true;
293 req_data_cb(req, hdr->type, hdr->data);
294 ubus_process_req_data(req);
295 req->blocked = false;
296
297 if (req->status_msg)
298 ubus_req_complete_cb(req);
299
300 return;
301 }
302
303 len = blob_raw_len(hdr->data);
304 data = calloc(1, sizeof(*data) + len);
305 if (!data)
306 return;
307
308 data->type = hdr->type;
309 memcpy(data->data, hdr->data, len);
310 list_add(&data->list, &req->pending);
311 }
312
313 static struct ubus_request *ubus_find_request(struct ubus_context *ctx, uint32_t seq, uint32_t peer)
314 {
315 struct ubus_request *req;
316
317 list_for_each_entry(req, &ctx->requests, list) {
318 if (seq != req->seq || peer != req->peer)
319 continue;
320
321 return req;
322 }
323 return NULL;
324 }
325
326 static void ubus_process_invoke(struct ubus_context *ctx, struct ubus_msghdr *hdr)
327 {
328 struct ubus_request_data req;
329 struct ubus_object *obj;
330 uint32_t objid = 0;
331 int method;
332 int ret = 0;
333
334 ubus_parse_msg(hdr->data);
335
336 if (!attrbuf[UBUS_ATTR_OBJID])
337 return;
338
339 objid = blob_get_u32(attrbuf[UBUS_ATTR_OBJID]);
340
341 if (!attrbuf[UBUS_ATTR_METHOD]) {
342 ret = UBUS_STATUS_INVALID_ARGUMENT;
343 goto send;
344 }
345
346 obj = avl_find_element(&ctx->objects, &objid, obj, avl);
347 if (!obj) {
348 ret = UBUS_STATUS_NOT_FOUND;
349 goto send;
350 }
351
352 for (method = 0; method < obj->n_methods; method++)
353 if (!obj->methods[method].name ||
354 !strcmp(obj->methods[method].name,
355 blob_data(attrbuf[UBUS_ATTR_METHOD])))
356 goto found;
357
358 /* not found */
359 ret = UBUS_STATUS_METHOD_NOT_FOUND;
360 goto send;
361
362 found:
363 req.object = objid;
364 req.peer = hdr->peer;
365 req.seq = hdr->seq;
366 ret = obj->methods[method].handler(ctx, obj, &req,
367 blob_data(attrbuf[UBUS_ATTR_METHOD]),
368 attrbuf[UBUS_ATTR_DATA]);
369
370 send:
371 blob_buf_init(&b, 0);
372 blob_put_int32(&b, UBUS_ATTR_STATUS, ret);
373 blob_put_int32(&b, UBUS_ATTR_OBJID, objid);
374 ubus_send_msg(ctx, hdr->seq, b.head, UBUS_MSG_STATUS, hdr->peer);
375 }
376
377 static void ubus_process_msg(struct ubus_context *ctx, struct ubus_msghdr *hdr)
378 {
379 struct ubus_request *req;
380
381 switch(hdr->type) {
382 case UBUS_MSG_STATUS:
383 req = ubus_find_request(ctx, hdr->seq, hdr->peer);
384 if (!req)
385 break;
386
387 ubus_process_req_status(req, hdr);
388 break;
389
390 case UBUS_MSG_DATA:
391 req = ubus_find_request(ctx, hdr->seq, hdr->peer);
392 if (req && (req->data_cb || req->raw_data_cb))
393 ubus_req_data(req, hdr);
394 break;
395
396 case UBUS_MSG_INVOKE:
397 ubus_process_invoke(ctx, hdr);
398 break;
399 }
400 }
401
402 void ubus_abort_request(struct ubus_context *ctx, struct ubus_request *req)
403 {
404 if (!list_empty(&req->list))
405 return;
406
407 req->cancelled = true;
408 ubus_process_req_data(req);
409 list_del(&req->list);
410 }
411
412 void ubus_complete_request_async(struct ubus_context *ctx, struct ubus_request *req)
413 {
414 if (!list_empty(&req->list))
415 return;
416
417 list_add(&req->list, &ctx->requests);
418 }
419
420 static void ubus_handle_data(struct uloop_fd *u, unsigned int events)
421 {
422 struct ubus_context *ctx = container_of(u, struct ubus_context, sock);
423 struct ubus_msghdr *hdr = &ctx->msgbuf.hdr;
424
425 while (get_next_msg(ctx)) {
426 ubus_process_msg(ctx, hdr);
427 if (uloop_cancelled)
428 break;
429 }
430
431 if (u->eof)
432 ctx->connection_lost(ctx);
433 }
434
435 static void ubus_sync_req_cb(struct ubus_request *req, int ret)
436 {
437 req->status_msg = true;
438 req->status_code = ret;
439 uloop_end();
440 }
441
442 struct ubus_sync_req_cb {
443 struct uloop_timeout timeout;
444 struct ubus_request *req;
445 };
446
447 static void ubus_sync_req_timeout_cb(struct uloop_timeout *timeout)
448 {
449 struct ubus_sync_req_cb *cb;
450
451 cb = container_of(timeout, struct ubus_sync_req_cb, timeout);
452 ubus_sync_req_cb(cb->req, UBUS_STATUS_TIMEOUT);
453 }
454
455 int ubus_complete_request(struct ubus_context *ctx, struct ubus_request *req,
456 int timeout)
457 {
458 struct ubus_sync_req_cb cb;
459 ubus_complete_handler_t complete_cb = req->complete_cb;
460 bool registered = ctx->sock.registered;
461 bool cancelled = uloop_cancelled;
462 int status = UBUS_STATUS_NO_DATA;
463
464 if (!registered) {
465 uloop_init();
466 ubus_add_uloop(ctx);
467 }
468
469 if (timeout) {
470 memset(&cb, 0, sizeof(cb));
471 cb.req = req;
472 cb.timeout.cb = ubus_sync_req_timeout_cb;
473 uloop_timeout_set(&cb.timeout, timeout);
474 }
475
476 ubus_complete_request_async(ctx, req);
477 req->complete_cb = ubus_sync_req_cb;
478
479 uloop_run();
480
481 if (timeout)
482 uloop_timeout_cancel(&cb.timeout);
483
484 if (req->status_msg)
485 status = req->status_code;
486
487 req->complete_cb = complete_cb;
488 if (req->complete_cb)
489 req->complete_cb(req, status);
490
491 uloop_cancelled = cancelled;
492 if (!registered)
493 uloop_fd_delete(&ctx->sock);
494
495 return status;
496 }
497
498 struct ubus_lookup_request {
499 struct ubus_request req;
500 ubus_lookup_handler_t cb;
501 };
502
503 static void ubus_lookup_cb(struct ubus_request *ureq, int type, struct blob_attr *msg)
504 {
505 struct ubus_lookup_request *req;
506 struct ubus_object_data obj;
507 struct blob_attr **attr;
508
509 req = container_of(ureq, struct ubus_lookup_request, req);
510 attr = ubus_parse_msg(msg);
511
512 if (!attr[UBUS_ATTR_OBJID] || !attr[UBUS_ATTR_OBJPATH] ||
513 !attr[UBUS_ATTR_OBJTYPE])
514 return;
515
516 memset(&obj, 0, sizeof(obj));
517 obj.id = blob_get_u32(attr[UBUS_ATTR_OBJID]);
518 obj.path = blob_data(attr[UBUS_ATTR_OBJPATH]);
519 obj.type_id = blob_get_u32(attr[UBUS_ATTR_OBJTYPE]);
520 obj.signature = attr[UBUS_ATTR_SIGNATURE];
521 req->cb(ureq->ctx, &obj, ureq->priv);
522 }
523
524 int ubus_lookup(struct ubus_context *ctx, const char *path,
525 ubus_lookup_handler_t cb, void *priv)
526 {
527 struct ubus_lookup_request lookup;
528
529 blob_buf_init(&b, 0);
530 if (path)
531 blob_put_string(&b, UBUS_ATTR_OBJPATH, path);
532
533 if (ubus_start_request(ctx, &lookup.req, b.head, UBUS_MSG_LOOKUP, 0) < 0)
534 return UBUS_STATUS_INVALID_ARGUMENT;
535
536 lookup.req.raw_data_cb = ubus_lookup_cb;
537 lookup.req.priv = priv;
538 lookup.cb = cb;
539 return ubus_complete_request(ctx, &lookup.req, 0);
540 }
541
542 static void ubus_lookup_id_cb(struct ubus_request *req, int type, struct blob_attr *msg)
543 {
544 struct blob_attr **attr;
545 uint32_t *id = req->priv;
546
547 attr = ubus_parse_msg(msg);
548
549 if (!attr[UBUS_ATTR_OBJID])
550 return;
551
552 *id = blob_get_u32(attr[UBUS_ATTR_OBJID]);
553 }
554
555 int ubus_lookup_id(struct ubus_context *ctx, const char *path, uint32_t *id)
556 {
557 struct ubus_request req;
558
559 blob_buf_init(&b, 0);
560 if (path)
561 blob_put_string(&b, UBUS_ATTR_OBJPATH, path);
562
563 if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_LOOKUP, 0) < 0)
564 return UBUS_STATUS_INVALID_ARGUMENT;
565
566 req.raw_data_cb = ubus_lookup_id_cb;
567 req.priv = id;
568
569 return ubus_complete_request(ctx, &req, 0);
570 }
571
572 int ubus_send_reply(struct ubus_context *ctx, struct ubus_request_data *req,
573 struct blob_attr *msg)
574 {
575 int ret;
576
577 blob_buf_init(&b, 0);
578 blob_put_int32(&b, UBUS_ATTR_OBJID, req->object);
579 blob_put(&b, UBUS_ATTR_DATA, blob_data(msg), blob_len(msg));
580 ret = ubus_send_msg(ctx, req->seq, b.head, UBUS_MSG_DATA, req->peer);
581 if (ret < 0)
582 return UBUS_STATUS_NO_DATA;
583
584 return 0;
585 }
586
587 int ubus_invoke_async(struct ubus_context *ctx, uint32_t obj, const char *method,
588 struct blob_attr *msg, struct ubus_request *req)
589 {
590 blob_buf_init(&b, 0);
591 blob_put_int32(&b, UBUS_ATTR_OBJID, obj);
592 blob_put_string(&b, UBUS_ATTR_METHOD, method);
593 if (msg)
594 blob_put(&b, UBUS_ATTR_DATA, blob_data(msg), blob_len(msg));
595
596 if (ubus_start_request(ctx, req, b.head, UBUS_MSG_INVOKE, obj) < 0)
597 return UBUS_STATUS_INVALID_ARGUMENT;
598
599 return 0;
600 }
601
602 int ubus_invoke(struct ubus_context *ctx, uint32_t obj, const char *method,
603 struct blob_attr *msg, ubus_data_handler_t cb, void *priv,
604 int timeout)
605 {
606 struct ubus_request req;
607
608 ubus_invoke_async(ctx, obj, method, msg, &req);
609 req.data_cb = cb;
610 req.priv = priv;
611 return ubus_complete_request(ctx, &req, timeout);
612 }
613
614 static void ubus_add_object_cb(struct ubus_request *req, int type, struct blob_attr *msg)
615 {
616 struct ubus_object *obj = req->priv;
617
618 ubus_parse_msg(msg);
619
620 if (!attrbuf[UBUS_ATTR_OBJID])
621 return;
622
623 obj->id = blob_get_u32(attrbuf[UBUS_ATTR_OBJID]);
624
625 if (attrbuf[UBUS_ATTR_OBJTYPE])
626 obj->type->id = blob_get_u32(attrbuf[UBUS_ATTR_OBJTYPE]);
627
628 obj->avl.key = &obj->id;
629 avl_insert(&req->ctx->objects, &obj->avl);
630 }
631
632 static void ubus_push_method_data(const struct ubus_method *m)
633 {
634 void *mtbl;
635 int i;
636
637 mtbl = blobmsg_open_table(&b, m->name);
638
639 for (i = 0; i < m->n_policy; i++)
640 blobmsg_add_u32(&b, m->policy[i].name, m->policy[i].type);
641
642 blobmsg_close_table(&b, mtbl);
643 }
644
645 static bool ubus_push_object_type(const struct ubus_object_type *type)
646 {
647 void *s;
648 int i;
649
650 s = blob_nest_start(&b, UBUS_ATTR_SIGNATURE);
651
652 for (i = 0; i < type->n_methods; i++)
653 ubus_push_method_data(&type->methods[i]);
654
655 blob_nest_end(&b, s);
656
657 return true;
658 }
659
660 static int __ubus_add_object(struct ubus_context *ctx, struct ubus_object *obj)
661 {
662 struct ubus_request req;
663 int ret;
664
665 blob_buf_init(&b, 0);
666
667 if (obj->name && obj->type) {
668 blob_put_string(&b, UBUS_ATTR_OBJPATH, obj->name);
669
670 if (obj->type->id)
671 blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id);
672 else if (!ubus_push_object_type(obj->type))
673 return UBUS_STATUS_INVALID_ARGUMENT;
674 }
675
676 if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_ADD_OBJECT, 0) < 0)
677 return UBUS_STATUS_INVALID_ARGUMENT;
678
679 req.raw_data_cb = ubus_add_object_cb;
680 req.priv = obj;
681 ret = ubus_complete_request(ctx, &req, 0);
682 if (ret)
683 return ret;
684
685 if (!obj->id)
686 return UBUS_STATUS_NO_DATA;
687
688 return 0;
689 }
690
691 int ubus_add_object(struct ubus_context *ctx, struct ubus_object *obj)
692 {
693 if (!obj->name || !obj->type)
694 return UBUS_STATUS_INVALID_ARGUMENT;
695
696 return __ubus_add_object(ctx, obj);
697 }
698
699 static void ubus_remove_object_cb(struct ubus_request *req, int type, struct blob_attr *msg)
700 {
701 struct ubus_object *obj = req->priv;
702
703 ubus_parse_msg(msg);
704
705 if (!attrbuf[UBUS_ATTR_OBJID])
706 return;
707
708 obj->id = 0;
709
710 if (attrbuf[UBUS_ATTR_OBJTYPE] && obj->type)
711 obj->type->id = 0;
712
713 avl_delete(&req->ctx->objects, &obj->avl);
714 }
715
716 int ubus_remove_object(struct ubus_context *ctx, struct ubus_object *obj)
717 {
718 struct ubus_request req;
719 int ret;
720
721 blob_buf_init(&b, 0);
722 blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id);
723
724 if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_REMOVE_OBJECT, 0) < 0)
725 return UBUS_STATUS_INVALID_ARGUMENT;
726
727 req.raw_data_cb = ubus_remove_object_cb;
728 req.priv = obj;
729 ret = ubus_complete_request(ctx, &req, 0);
730 if (ret)
731 return ret;
732
733 if (obj->id)
734 return UBUS_STATUS_NO_DATA;
735
736 return 0;
737 }
738
739 static int ubus_event_cb(struct ubus_context *ctx, struct ubus_object *obj,
740 struct ubus_request_data *req,
741 const char *method, struct blob_attr *msg)
742 {
743 struct ubus_event_handler *ev;
744
745 ev = container_of(obj, struct ubus_event_handler, obj);
746 ev->cb(ctx, ev, method, msg);
747 return 0;
748 }
749
750 static const struct ubus_method event_method = {
751 .name = NULL,
752 .handler = ubus_event_cb,
753 };
754
755 int ubus_register_event_handler(struct ubus_context *ctx,
756 struct ubus_event_handler *ev,
757 const char *pattern)
758 {
759 struct ubus_object *obj = &ev->obj;
760 struct blob_buf b2;
761 int ret;
762
763 if (!obj->id) {
764 obj->methods = &event_method;
765 obj->n_methods = 1;
766
767 if (!!obj->name ^ !!obj->type)
768 return UBUS_STATUS_INVALID_ARGUMENT;
769
770 ret = __ubus_add_object(ctx, obj);
771 if (ret)
772 return ret;
773 }
774
775 /* use a second buffer, ubus_invoke() overwrites the primary one */
776 memset(&b2, 0, sizeof(b2));
777 blob_buf_init(&b2, 0);
778 blobmsg_add_u32(&b2, "object", obj->id);
779 if (pattern)
780 blobmsg_add_string(&b2, "pattern", pattern);
781
782 return ubus_invoke(ctx, UBUS_SYSTEM_OBJECT_EVENT, "register", b2.head,
783 NULL, NULL, 0);
784 }
785
786 int ubus_send_event(struct ubus_context *ctx, const char *id,
787 struct blob_attr *data)
788 {
789 struct ubus_request req;
790 void *s;
791
792 blob_buf_init(&b, 0);
793 blob_put_int32(&b, UBUS_ATTR_OBJID, UBUS_SYSTEM_OBJECT_EVENT);
794 blob_put_string(&b, UBUS_ATTR_METHOD, "send");
795 s = blob_nest_start(&b, UBUS_ATTR_DATA);
796 blobmsg_add_string(&b, "id", id);
797 blobmsg_add_field(&b, BLOBMSG_TYPE_TABLE, "data", blob_data(data), blob_len(data));
798 blob_nest_end(&b, s);
799
800 if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_INVOKE, UBUS_SYSTEM_OBJECT_EVENT) < 0)
801 return UBUS_STATUS_INVALID_ARGUMENT;
802
803 return ubus_complete_request(ctx, &req, 0);
804 }
805
806 static void ubus_default_connection_lost(struct ubus_context *ctx)
807 {
808 if (ctx->sock.registered)
809 uloop_end();
810 }
811
812 struct ubus_context *ubus_connect(const char *path)
813 {
814 struct ubus_context *ctx;
815 struct {
816 struct ubus_msghdr hdr;
817 struct blob_attr data;
818 } hdr;
819 struct blob_attr *buf;
820
821 if (!path)
822 path = UBUS_UNIX_SOCKET;
823
824 ctx = calloc(1, sizeof(*ctx));
825 if (!ctx)
826 goto error;
827
828 ctx->sock.fd = usock(USOCK_UNIX, path, NULL);
829 if (ctx->sock.fd < 0)
830 goto error_free;
831
832 ctx->sock.cb = ubus_handle_data;
833
834 if (read(ctx->sock.fd, &hdr, sizeof(hdr)) != sizeof(hdr))
835 goto error_close;
836
837 if (!ubus_validate_hdr(&hdr.hdr))
838 goto error_close;
839
840 if (hdr.hdr.type != UBUS_MSG_HELLO)
841 goto error_close;
842
843 buf = calloc(1, blob_raw_len(&hdr.data));
844 if (!buf)
845 goto error_close;
846
847 memcpy(buf, &hdr.data, sizeof(hdr.data));
848 if (read(ctx->sock.fd, blob_data(buf), blob_len(buf)) != blob_len(buf))
849 goto error_free_buf;
850
851 ctx->local_id = hdr.hdr.peer;
852 free(buf);
853
854 ctx->connection_lost = ubus_default_connection_lost;
855
856 INIT_LIST_HEAD(&ctx->requests);
857 avl_init(&ctx->objects, ubus_cmp_id, false, NULL);
858
859 if (!ctx->local_id)
860 goto error_close;
861
862 return ctx;
863
864 error_free_buf:
865 free(buf);
866 error_close:
867 close(ctx->sock.fd);
868 error_free:
869 free(ctx);
870 error:
871 return NULL;
872 }
873
874 void ubus_free(struct ubus_context *ctx)
875 {
876 close(ctx->sock.fd);
877 free(ctx);
878 }