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