From 6f2738df5585f244cdaf64f41a06ecc53e8ccd78 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Sun, 14 Sep 2025 17:07:53 +0200 Subject: [PATCH] ucode: ubus: simplify sharing a global connection state Add connection methods as global functions Signed-off-by: Felix Fietkau --- ...connection-functions-to-global-scope.patch | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 package/utils/ucode/patches/100-ubus-add-connection-functions-to-global-scope.patch diff --git a/package/utils/ucode/patches/100-ubus-add-connection-functions-to-global-scope.patch b/package/utils/ucode/patches/100-ubus-add-connection-functions-to-global-scope.patch new file mode 100644 index 0000000000..33136449a7 --- /dev/null +++ b/package/utils/ucode/patches/100-ubus-add-connection-functions-to-global-scope.patch @@ -0,0 +1,67 @@ +From: Felix Fietkau +Date: Tue, 26 Aug 2025 10:17:22 +0200 +Subject: [PATCH] ubus: add connection functions to global scope + +Allows reusing a common global connection across modules + +Signed-off-by: Felix Fietkau +--- + +--- a/lib/ubus.c ++++ b/lib/ubus.c +@@ -511,16 +511,40 @@ uc_ubus_objects_cb(struct ubus_context * + static bool + _conn_get(uc_vm_t *vm, uc_ubus_connection_t **conn) + { +- uc_ubus_connection_t *c = uc_fn_thisval("ubus.connection"); ++ uc_ubus_connection_t *c; ++ uc_value_t *res; + +- if (!c) +- c = uc_fn_thisval("ubus.channel"); +- if (!c) +- err_return(UBUS_STATUS_INVALID_ARGUMENT, "Invalid connection context"); ++ if (ucv_type(_uc_fn_this_res(vm)) == UC_OBJECT) { ++ res = uc_vm_registry_get(vm, "ubus.connection"); ++ c = ucv_resource_data(res, "ubus.connection"); ++ ++ if (c && c->ctx.sock.fd >= 0) ++ goto out; ++ ++ c = uc_ubus_conn_alloc(vm, NULL, "ubus.connection"); ++ if (!c) ++ return NULL; ++ ++ if (ubus_connect_ctx(&c->ctx, NULL)) { ++ ucv_put(c->res); ++ err_return(UBUS_STATUS_UNKNOWN_ERROR, "Unable to connect to ubus socket"); ++ } + +- if (c->ctx.sock.fd < 0) +- err_return(UBUS_STATUS_CONNECTION_FAILED, "Connection is closed"); ++ uc_vm_registry_set(vm, "ubus.connection", ucv_get(c->res)); ++ } ++ else { ++ c = uc_fn_thisval("ubus.connection"); ++ if (!c) ++ c = uc_fn_thisval("ubus.channel"); ++ ++ if (!c) ++ err_return(UBUS_STATUS_INVALID_ARGUMENT, "Invalid connection context"); + ++ if (c->ctx.sock.fd < 0) ++ err_return(UBUS_STATUS_CONNECTION_FAILED, "Connection is closed"); ++ } ++ ++out: + *conn = c; + + ok_return(true); +@@ -2606,6 +2630,7 @@ static void free_request(void *ud) { + void uc_module_init(uc_vm_t *vm, uc_value_t *scope) + { + uc_function_list_register(scope, global_fns); ++ uc_function_list_register(scope, conn_fns); + + #define ADD_CONST(x) ucv_object_add(scope, #x, ucv_int64_new(UBUS_##x)) + ADD_CONST(STATUS_OK); -- 2.30.2