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