2 * Copyright (C) 2011 Felix Fietkau <nbd@openwrt.org>
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
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.
14 #include <sys/types.h>
16 #include <sys/socket.h>
21 #include <libubox/blob.h>
22 #include <libubox/blobmsg.h>
23 #include <libubox/usock.h>
28 #define STATIC_IOV(_var) { .iov_base = (char *) &(_var), .iov_len = sizeof(_var) }
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",
43 static struct blob_buf b
;
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
},
51 static struct blob_attr
*attrbuf
[UBUS_ATTR_MAX
];
53 struct ubus_pending_data
{
54 struct list_head list
;
56 struct blob_attr data
[];
59 static int ubus_cmp_id(const void *k1
, const void *k2
, void *ptr
)
61 const uint32_t *id1
= k1
, *id2
= k2
;
69 static struct blob_attr
**ubus_parse_msg(struct blob_attr
*msg
)
71 blob_parse(msg
, attrbuf
, ubus_policy
, UBUS_ATTR_MAX
);
75 const char *ubus_strerror(int error
)
79 if (error
< 0 || error
>= __UBUS_STATUS_LAST
)
82 if (!__ubus_strerror
[error
])
85 return __ubus_strerror
[error
];
88 sprintf(err
, "Unknown error: %d", error
);
92 static void wait_data(int fd
, bool write
)
94 struct pollfd pfd
= { .fd
= fd
};
96 pfd
.events
= write
? POLLOUT
: POLLIN
;
100 static int writev_retry(int fd
, struct iovec
*iov
, int iov_len
)
105 int cur_len
= writev(fd
, iov
, iov_len
);
119 while (cur_len
>= iov
->iov_len
) {
120 cur_len
-= iov
->iov_len
;
123 if (!cur_len
|| !iov_len
)
126 iov
->iov_len
-= cur_len
;
130 static int ubus_send_msg(struct ubus_context
*ctx
, uint32_t seq
,
131 struct blob_attr
*msg
, int cmd
, uint32_t peer
)
133 struct ubus_msghdr hdr
;
134 struct iovec iov
[2] = {
144 blob_buf_init(&b
, 0);
148 iov
[1].iov_base
= (char *) msg
;
149 iov
[1].iov_len
= blob_raw_len(msg
);
151 return writev_retry(ctx
->sock
.fd
, iov
, ARRAY_SIZE(iov
));
154 static int ubus_start_request(struct ubus_context
*ctx
, struct ubus_request
*req
,
155 struct blob_attr
*msg
, int cmd
, uint32_t peer
)
157 memset(req
, 0, sizeof(*req
));
159 if (msg
&& blob_pad_len(msg
) > UBUS_MAX_MSGLEN
)
162 INIT_LIST_HEAD(&req
->list
);
163 INIT_LIST_HEAD(&req
->pending
);
166 req
->seq
= ++ctx
->request_seq
;
167 return ubus_send_msg(ctx
, req
->seq
, msg
, cmd
, peer
);
170 static bool recv_retry(int fd
, struct iovec
*iov
, bool wait
)
174 while (iov
->iov_len
> 0) {
176 wait_data(fd
, false);
178 bytes
= read(fd
, iov
->iov_base
, iov
->iov_len
);
193 iov
->iov_len
-= bytes
;
194 iov
->iov_base
+= bytes
;
200 static bool ubus_validate_hdr(struct ubus_msghdr
*hdr
)
202 if (hdr
->version
!= 0)
205 if (blob_raw_len(hdr
->data
) < sizeof(*hdr
->data
))
208 if (blob_pad_len(hdr
->data
) > UBUS_MAX_MSGLEN
)
214 static bool get_next_msg(struct ubus_context
*ctx
)
216 struct iovec iov
= STATIC_IOV(ctx
->msgbuf
.hdr
);
218 /* receive header + start attribute */
219 iov
.iov_len
+= sizeof(struct blob_attr
);
220 if (!recv_retry(ctx
->sock
.fd
, &iov
, false))
223 iov
.iov_len
= blob_len(ctx
->msgbuf
.hdr
.data
);
224 if (iov
.iov_len
> 0 && !recv_retry(ctx
->sock
.fd
, &iov
, true))
227 return ubus_validate_hdr(&ctx
->msgbuf
.hdr
);
230 static bool ubus_get_status(struct ubus_msghdr
*hdr
, int *ret
)
232 ubus_parse_msg(hdr
->data
);
234 if (!attrbuf
[UBUS_ATTR_STATUS
])
237 *ret
= blob_get_u32(attrbuf
[UBUS_ATTR_STATUS
]);
241 static void req_data_cb(struct ubus_request
*req
, int type
, struct blob_attr
*data
)
243 struct blob_attr
**attr
;
245 if (req
->raw_data_cb
)
246 req
->raw_data_cb(req
, type
, data
);
251 attr
= ubus_parse_msg(data
);
252 req
->data_cb(req
, type
, attr
[UBUS_ATTR_DATA
]);
255 static void ubus_process_req_data(struct ubus_request
*req
)
257 struct ubus_pending_data
*data
;
259 while (!list_empty(&req
->pending
)) {
260 data
= list_first_entry(&req
->pending
,
261 struct ubus_pending_data
, list
);
262 list_del(&data
->list
);
264 req_data_cb(req
, data
->type
, data
->data
);
269 static void ubus_req_complete_cb(struct ubus_request
*req
)
271 ubus_complete_handler_t cb
= req
->complete_cb
;
276 req
->complete_cb
= NULL
;
277 cb(req
, req
->status_code
);
280 static int ubus_process_req_status(struct ubus_request
*req
, struct ubus_msghdr
*hdr
)
282 int ret
= UBUS_STATUS_INVALID_ARGUMENT
;
284 if (!list_empty(&req
->list
))
285 list_del_init(&req
->list
);
287 ubus_get_status(hdr
, &ret
);
288 req
->peer
= hdr
->peer
;
289 req
->status_msg
= true;
290 req
->status_code
= ret
;
292 ubus_req_complete_cb(req
);
297 static void ubus_req_data(struct ubus_request
*req
, struct ubus_msghdr
*hdr
)
299 struct ubus_pending_data
*data
;
304 req_data_cb(req
, hdr
->type
, hdr
->data
);
305 ubus_process_req_data(req
);
306 req
->blocked
= false;
309 ubus_req_complete_cb(req
);
314 len
= blob_raw_len(hdr
->data
);
315 data
= calloc(1, sizeof(*data
) + len
);
319 data
->type
= hdr
->type
;
320 memcpy(data
->data
, hdr
->data
, len
);
321 list_add(&data
->list
, &req
->pending
);
324 static struct ubus_request
*ubus_find_request(struct ubus_context
*ctx
, uint32_t seq
, uint32_t peer
)
326 struct ubus_request
*req
;
328 list_for_each_entry(req
, &ctx
->requests
, list
) {
329 if (seq
!= req
->seq
|| peer
!= req
->peer
)
337 static void ubus_process_invoke(struct ubus_context
*ctx
, struct ubus_msghdr
*hdr
)
339 struct ubus_request_data req
;
340 struct ubus_object
*obj
;
345 req
.peer
= hdr
->peer
;
347 ubus_parse_msg(hdr
->data
);
349 if (!attrbuf
[UBUS_ATTR_OBJID
])
352 objid
= blob_get_u32(attrbuf
[UBUS_ATTR_OBJID
]);
354 if (!attrbuf
[UBUS_ATTR_METHOD
]) {
355 ret
= UBUS_STATUS_INVALID_ARGUMENT
;
359 obj
= avl_find_element(&ctx
->objects
, &objid
, obj
, avl
);
361 ret
= UBUS_STATUS_NOT_FOUND
;
365 for (method
= 0; method
< obj
->n_methods
; method
++)
366 if (!obj
->methods
[method
].name
||
367 !strcmp(obj
->methods
[method
].name
,
368 blob_data(attrbuf
[UBUS_ATTR_METHOD
])))
372 ret
= UBUS_STATUS_METHOD_NOT_FOUND
;
377 ret
= obj
->methods
[method
].handler(ctx
, obj
, &req
,
378 blob_data(attrbuf
[UBUS_ATTR_METHOD
]),
379 attrbuf
[UBUS_ATTR_DATA
]);
382 blob_buf_init(&b
, 0);
383 blob_put_int32(&b
, UBUS_ATTR_STATUS
, ret
);
384 blob_put_int32(&b
, UBUS_ATTR_OBJID
, objid
);
385 ubus_send_msg(ctx
, req
.seq
, b
.head
, UBUS_MSG_STATUS
, req
.peer
);
388 static void ubus_process_msg(struct ubus_context
*ctx
, struct ubus_msghdr
*hdr
)
390 struct ubus_request
*req
;
393 case UBUS_MSG_STATUS
:
394 req
= ubus_find_request(ctx
, hdr
->seq
, hdr
->peer
);
398 ubus_process_req_status(req
, hdr
);
402 req
= ubus_find_request(ctx
, hdr
->seq
, hdr
->peer
);
403 if (req
&& (req
->data_cb
|| req
->raw_data_cb
))
404 ubus_req_data(req
, hdr
);
407 case UBUS_MSG_INVOKE
:
408 ubus_process_invoke(ctx
, hdr
);
413 void ubus_abort_request(struct ubus_context
*ctx
, struct ubus_request
*req
)
415 if (!list_empty(&req
->list
))
418 req
->cancelled
= true;
419 ubus_process_req_data(req
);
420 list_del_init(&req
->list
);
423 void ubus_complete_request_async(struct ubus_context
*ctx
, struct ubus_request
*req
)
425 if (!list_empty(&req
->list
))
428 list_add(&req
->list
, &ctx
->requests
);
431 static void ubus_handle_data(struct uloop_fd
*u
, unsigned int events
)
433 struct ubus_context
*ctx
= container_of(u
, struct ubus_context
, sock
);
434 struct ubus_msghdr
*hdr
= &ctx
->msgbuf
.hdr
;
436 while (get_next_msg(ctx
)) {
437 ubus_process_msg(ctx
, hdr
);
443 ctx
->connection_lost(ctx
);
446 static void ubus_sync_req_cb(struct ubus_request
*req
, int ret
)
448 req
->status_msg
= true;
449 req
->status_code
= ret
;
453 struct ubus_sync_req_cb
{
454 struct uloop_timeout timeout
;
455 struct ubus_request
*req
;
458 static void ubus_sync_req_timeout_cb(struct uloop_timeout
*timeout
)
460 struct ubus_sync_req_cb
*cb
;
462 cb
= container_of(timeout
, struct ubus_sync_req_cb
, timeout
);
463 ubus_sync_req_cb(cb
->req
, UBUS_STATUS_TIMEOUT
);
466 int ubus_complete_request(struct ubus_context
*ctx
, struct ubus_request
*req
,
469 struct ubus_sync_req_cb cb
;
470 ubus_complete_handler_t complete_cb
= req
->complete_cb
;
471 bool registered
= ctx
->sock
.registered
;
472 int status
= UBUS_STATUS_NO_DATA
;
480 memset(&cb
, 0, sizeof(cb
));
482 cb
.timeout
.cb
= ubus_sync_req_timeout_cb
;
483 uloop_timeout_set(&cb
.timeout
, timeout
);
486 ubus_complete_request_async(ctx
, req
);
487 req
->complete_cb
= ubus_sync_req_cb
;
489 while (!req
->status_msg
) {
490 bool cancelled
= uloop_cancelled
;
491 uloop_cancelled
= false;
493 uloop_cancelled
= cancelled
;
497 uloop_timeout_cancel(&cb
.timeout
);
500 status
= req
->status_code
;
502 req
->complete_cb
= complete_cb
;
503 if (req
->complete_cb
)
504 req
->complete_cb(req
, status
);
507 uloop_fd_delete(&ctx
->sock
);
512 struct ubus_lookup_request
{
513 struct ubus_request req
;
514 ubus_lookup_handler_t cb
;
517 static void ubus_lookup_cb(struct ubus_request
*ureq
, int type
, struct blob_attr
*msg
)
519 struct ubus_lookup_request
*req
;
520 struct ubus_object_data obj
;
521 struct blob_attr
**attr
;
523 req
= container_of(ureq
, struct ubus_lookup_request
, req
);
524 attr
= ubus_parse_msg(msg
);
526 if (!attr
[UBUS_ATTR_OBJID
] || !attr
[UBUS_ATTR_OBJPATH
] ||
527 !attr
[UBUS_ATTR_OBJTYPE
])
530 memset(&obj
, 0, sizeof(obj
));
531 obj
.id
= blob_get_u32(attr
[UBUS_ATTR_OBJID
]);
532 obj
.path
= blob_data(attr
[UBUS_ATTR_OBJPATH
]);
533 obj
.type_id
= blob_get_u32(attr
[UBUS_ATTR_OBJTYPE
]);
534 obj
.signature
= attr
[UBUS_ATTR_SIGNATURE
];
535 req
->cb(ureq
->ctx
, &obj
, ureq
->priv
);
538 int ubus_lookup(struct ubus_context
*ctx
, const char *path
,
539 ubus_lookup_handler_t cb
, void *priv
)
541 struct ubus_lookup_request lookup
;
543 blob_buf_init(&b
, 0);
545 blob_put_string(&b
, UBUS_ATTR_OBJPATH
, path
);
547 if (ubus_start_request(ctx
, &lookup
.req
, b
.head
, UBUS_MSG_LOOKUP
, 0) < 0)
548 return UBUS_STATUS_INVALID_ARGUMENT
;
550 lookup
.req
.raw_data_cb
= ubus_lookup_cb
;
551 lookup
.req
.priv
= priv
;
553 return ubus_complete_request(ctx
, &lookup
.req
, 0);
556 static void ubus_lookup_id_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
558 struct blob_attr
**attr
;
559 uint32_t *id
= req
->priv
;
561 attr
= ubus_parse_msg(msg
);
563 if (!attr
[UBUS_ATTR_OBJID
])
566 *id
= blob_get_u32(attr
[UBUS_ATTR_OBJID
]);
569 int ubus_lookup_id(struct ubus_context
*ctx
, const char *path
, uint32_t *id
)
571 struct ubus_request req
;
573 blob_buf_init(&b
, 0);
575 blob_put_string(&b
, UBUS_ATTR_OBJPATH
, path
);
577 if (ubus_start_request(ctx
, &req
, b
.head
, UBUS_MSG_LOOKUP
, 0) < 0)
578 return UBUS_STATUS_INVALID_ARGUMENT
;
580 req
.raw_data_cb
= ubus_lookup_id_cb
;
583 return ubus_complete_request(ctx
, &req
, 0);
586 int ubus_send_reply(struct ubus_context
*ctx
, struct ubus_request_data
*req
,
587 struct blob_attr
*msg
)
591 blob_buf_init(&b
, 0);
592 blob_put_int32(&b
, UBUS_ATTR_OBJID
, req
->object
);
593 blob_put(&b
, UBUS_ATTR_DATA
, blob_data(msg
), blob_len(msg
));
594 ret
= ubus_send_msg(ctx
, req
->seq
, b
.head
, UBUS_MSG_DATA
, req
->peer
);
596 return UBUS_STATUS_NO_DATA
;
601 int ubus_invoke_async(struct ubus_context
*ctx
, uint32_t obj
, const char *method
,
602 struct blob_attr
*msg
, struct ubus_request
*req
)
604 blob_buf_init(&b
, 0);
605 blob_put_int32(&b
, UBUS_ATTR_OBJID
, obj
);
606 blob_put_string(&b
, UBUS_ATTR_METHOD
, method
);
608 blob_put(&b
, UBUS_ATTR_DATA
, blob_data(msg
), blob_len(msg
));
610 if (ubus_start_request(ctx
, req
, b
.head
, UBUS_MSG_INVOKE
, obj
) < 0)
611 return UBUS_STATUS_INVALID_ARGUMENT
;
616 int ubus_invoke(struct ubus_context
*ctx
, uint32_t obj
, const char *method
,
617 struct blob_attr
*msg
, ubus_data_handler_t cb
, void *priv
,
620 struct ubus_request req
;
622 ubus_invoke_async(ctx
, obj
, method
, msg
, &req
);
625 return ubus_complete_request(ctx
, &req
, timeout
);
628 static void ubus_add_object_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
630 struct ubus_object
*obj
= req
->priv
;
634 if (!attrbuf
[UBUS_ATTR_OBJID
])
637 obj
->id
= blob_get_u32(attrbuf
[UBUS_ATTR_OBJID
]);
639 if (attrbuf
[UBUS_ATTR_OBJTYPE
])
640 obj
->type
->id
= blob_get_u32(attrbuf
[UBUS_ATTR_OBJTYPE
]);
642 obj
->avl
.key
= &obj
->id
;
643 avl_insert(&req
->ctx
->objects
, &obj
->avl
);
646 static void ubus_push_method_data(const struct ubus_method
*m
)
651 mtbl
= blobmsg_open_table(&b
, m
->name
);
653 for (i
= 0; i
< m
->n_policy
; i
++)
654 blobmsg_add_u32(&b
, m
->policy
[i
].name
, m
->policy
[i
].type
);
656 blobmsg_close_table(&b
, mtbl
);
659 static bool ubus_push_object_type(const struct ubus_object_type
*type
)
664 s
= blob_nest_start(&b
, UBUS_ATTR_SIGNATURE
);
666 for (i
= 0; i
< type
->n_methods
; i
++)
667 ubus_push_method_data(&type
->methods
[i
]);
669 blob_nest_end(&b
, s
);
674 static int __ubus_add_object(struct ubus_context
*ctx
, struct ubus_object
*obj
)
676 struct ubus_request req
;
679 blob_buf_init(&b
, 0);
681 if (obj
->name
&& obj
->type
) {
682 blob_put_string(&b
, UBUS_ATTR_OBJPATH
, obj
->name
);
685 blob_put_int32(&b
, UBUS_ATTR_OBJTYPE
, obj
->type
->id
);
686 else if (!ubus_push_object_type(obj
->type
))
687 return UBUS_STATUS_INVALID_ARGUMENT
;
690 if (ubus_start_request(ctx
, &req
, b
.head
, UBUS_MSG_ADD_OBJECT
, 0) < 0)
691 return UBUS_STATUS_INVALID_ARGUMENT
;
693 req
.raw_data_cb
= ubus_add_object_cb
;
695 ret
= ubus_complete_request(ctx
, &req
, 0);
700 return UBUS_STATUS_NO_DATA
;
705 int ubus_add_object(struct ubus_context
*ctx
, struct ubus_object
*obj
)
707 if (!obj
->name
|| !obj
->type
)
708 return UBUS_STATUS_INVALID_ARGUMENT
;
710 return __ubus_add_object(ctx
, obj
);
713 static void ubus_remove_object_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
715 struct ubus_object
*obj
= req
->priv
;
719 if (!attrbuf
[UBUS_ATTR_OBJID
])
724 if (attrbuf
[UBUS_ATTR_OBJTYPE
] && obj
->type
)
727 avl_delete(&req
->ctx
->objects
, &obj
->avl
);
730 int ubus_remove_object(struct ubus_context
*ctx
, struct ubus_object
*obj
)
732 struct ubus_request req
;
735 blob_buf_init(&b
, 0);
736 blob_put_int32(&b
, UBUS_ATTR_OBJID
, obj
->id
);
738 if (ubus_start_request(ctx
, &req
, b
.head
, UBUS_MSG_REMOVE_OBJECT
, 0) < 0)
739 return UBUS_STATUS_INVALID_ARGUMENT
;
741 req
.raw_data_cb
= ubus_remove_object_cb
;
743 ret
= ubus_complete_request(ctx
, &req
, 0);
748 return UBUS_STATUS_NO_DATA
;
753 static int ubus_event_cb(struct ubus_context
*ctx
, struct ubus_object
*obj
,
754 struct ubus_request_data
*req
,
755 const char *method
, struct blob_attr
*msg
)
757 struct ubus_event_handler
*ev
;
759 ev
= container_of(obj
, struct ubus_event_handler
, obj
);
760 ev
->cb(ctx
, ev
, method
, msg
);
764 static const struct ubus_method event_method
= {
766 .handler
= ubus_event_cb
,
769 int ubus_register_event_handler(struct ubus_context
*ctx
,
770 struct ubus_event_handler
*ev
,
773 struct ubus_object
*obj
= &ev
->obj
;
778 obj
->methods
= &event_method
;
781 if (!!obj
->name
^ !!obj
->type
)
782 return UBUS_STATUS_INVALID_ARGUMENT
;
784 ret
= __ubus_add_object(ctx
, obj
);
789 /* use a second buffer, ubus_invoke() overwrites the primary one */
790 memset(&b2
, 0, sizeof(b2
));
791 blob_buf_init(&b2
, 0);
792 blobmsg_add_u32(&b2
, "object", obj
->id
);
794 blobmsg_add_string(&b2
, "pattern", pattern
);
796 return ubus_invoke(ctx
, UBUS_SYSTEM_OBJECT_EVENT
, "register", b2
.head
,
800 int ubus_send_event(struct ubus_context
*ctx
, const char *id
,
801 struct blob_attr
*data
)
803 struct ubus_request req
;
806 blob_buf_init(&b
, 0);
807 blob_put_int32(&b
, UBUS_ATTR_OBJID
, UBUS_SYSTEM_OBJECT_EVENT
);
808 blob_put_string(&b
, UBUS_ATTR_METHOD
, "send");
809 s
= blob_nest_start(&b
, UBUS_ATTR_DATA
);
810 blobmsg_add_string(&b
, "id", id
);
811 blobmsg_add_field(&b
, BLOBMSG_TYPE_TABLE
, "data", blob_data(data
), blob_len(data
));
812 blob_nest_end(&b
, s
);
814 if (ubus_start_request(ctx
, &req
, b
.head
, UBUS_MSG_INVOKE
, UBUS_SYSTEM_OBJECT_EVENT
) < 0)
815 return UBUS_STATUS_INVALID_ARGUMENT
;
817 return ubus_complete_request(ctx
, &req
, 0);
820 static void ubus_default_connection_lost(struct ubus_context
*ctx
)
822 if (ctx
->sock
.registered
)
826 struct ubus_context
*ubus_connect(const char *path
)
828 struct ubus_context
*ctx
;
830 struct ubus_msghdr hdr
;
831 struct blob_attr data
;
833 struct blob_attr
*buf
;
836 path
= UBUS_UNIX_SOCKET
;
838 ctx
= calloc(1, sizeof(*ctx
));
842 ctx
->sock
.fd
= usock(USOCK_UNIX
, path
, NULL
);
843 if (ctx
->sock
.fd
< 0)
846 ctx
->sock
.cb
= ubus_handle_data
;
848 if (read(ctx
->sock
.fd
, &hdr
, sizeof(hdr
)) != sizeof(hdr
))
851 if (!ubus_validate_hdr(&hdr
.hdr
))
854 if (hdr
.hdr
.type
!= UBUS_MSG_HELLO
)
857 buf
= calloc(1, blob_raw_len(&hdr
.data
));
861 memcpy(buf
, &hdr
.data
, sizeof(hdr
.data
));
862 if (read(ctx
->sock
.fd
, blob_data(buf
), blob_len(buf
)) != blob_len(buf
))
865 ctx
->local_id
= hdr
.hdr
.peer
;
868 ctx
->connection_lost
= ubus_default_connection_lost
;
870 INIT_LIST_HEAD(&ctx
->requests
);
871 avl_init(&ctx
->objects
, ubus_cmp_id
, false, NULL
);
876 fcntl(ctx
->sock
.fd
, F_SETFL
, fcntl(ctx
->sock
.fd
, F_GETFL
) | O_NONBLOCK
);
890 void ubus_free(struct ubus_context
*ctx
)