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