From 7b262aec3843912d68e020f0750b6f96e2f7c8e3 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Mon, 31 Jan 2011 16:15:55 +0100 Subject: [PATCH] add a callback for handling lost ubus connections --- libubus.c | 19 +++++++++++++++++-- libubus.h | 2 ++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/libubus.c b/libubus.c index d90dc1f..64190f7 100644 --- a/libubus.c +++ b/libubus.c @@ -275,12 +275,16 @@ static void ubus_process_invoke(struct ubus_context *ctx, struct ubus_msghdr *hd ubus_parse_msg(hdr->data); - if (!attrbuf[UBUS_ATTR_METHOD] || !attrbuf[UBUS_ATTR_OBJID]) { + if (!attrbuf[UBUS_ATTR_OBJID]) + return; + + objid = blob_get_int32(attrbuf[UBUS_ATTR_OBJID]); + + if (!attrbuf[UBUS_ATTR_METHOD]) { ret = UBUS_STATUS_INVALID_ARGUMENT; goto send; } - objid = blob_get_int32(attrbuf[UBUS_ATTR_OBJID]); obj = avl_find_element(&ctx->objects, &objid, obj, avl); if (!obj) { ret = UBUS_STATUS_NOT_FOUND; @@ -363,6 +367,9 @@ static void ubus_handle_data(struct uloop_fd *u, unsigned int events) while (get_next_msg(ctx, false)) ubus_process_msg(ctx, hdr); + + if (u->eof) + ctx->connection_lost(ctx); } int ubus_complete_request(struct ubus_context *ctx, struct ubus_request *req) @@ -528,6 +535,12 @@ int ubus_publish(struct ubus_context *ctx, struct ubus_object *obj) return 0; } +void ubus_default_connection_lost(struct ubus_context *ctx) +{ + if (ctx->sock.registered) + uloop_end(); +} + struct ubus_context *ubus_connect(const char *path) { struct ubus_context *ctx; @@ -579,6 +592,8 @@ struct ubus_context *ubus_connect(const char *path) ctx->local_id = hdr.hdr.peer; free(buf); + ctx->connection_lost = ubus_default_connection_lost; + INIT_LIST_HEAD(&ctx->requests); avl_init(&ctx->objects, ubus_cmp_id, false, NULL); diff --git a/libubus.h b/libubus.h index 2662878..b741e65 100644 --- a/libubus.h +++ b/libubus.h @@ -79,6 +79,8 @@ struct ubus_context { uint32_t local_id; uint32_t request_seq; + void (*connection_lost)(struct ubus_context *ctx); + struct { struct ubus_msghdr hdr; char data[UBUS_MAX_MSGLEN - sizeof(struct ubus_msghdr)]; -- 2.25.1