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