From patchwork Wed Feb 21 08:22:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefano Babic X-Patchwork-Id: 1901894 X-Patchwork-Delegate: sbabic@denx.de Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=googlegroups.com header.i=@googlegroups.com header.a=rsa-sha256 header.s=20230601 header.b=XeqHRae+; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=googlegroups.com (client-ip=2a00:1450:4864:20::140; helo=mail-lf1-x140.google.com; envelope-from=swupdate+bncbaabbq7f22xamgqe35sp7hi@googlegroups.com; receiver=patchwork.ozlabs.org) Received: from mail-lf1-x140.google.com (mail-lf1-x140.google.com [IPv6:2a00:1450:4864:20::140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Tfq5M4YQTz23hw for ; Wed, 21 Feb 2024 19:22:31 +1100 (AEDT) Received: by mail-lf1-x140.google.com with SMTP id 2adb3069b0e04-512bde6ba39sf2428711e87.0 for ; Wed, 21 Feb 2024 00:22:31 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708503748; cv=pass; d=google.com; s=arc-20160816; b=fQy7HFJUFCT13L7Pc59uDvxWYguFuRJHITeviOaaS+sk6c7V5kAV5Wkg75L61ewDN5 NOU9vCATwFOJLlGRuDHcm453H9k1L1BeFTpTWBD1bxIQAbGd5VUEy8IjgYJAkgj0nwuw IMX2t2jekFi9ugEUNSV02kD1kJaJiKHlUXGWR3W5l6ioofY6q+WTlaJUgfsVI704SM3h 034XSW57YL0Um2Vn7kwfxFNnk0hubzcewtUT7pFBeqPNhl10fYo39PBGrca/UPrVDOAc femGQpl6EuefoFRn25i83T4IF1NLm5ef58/PThWIXbhMSdQlz9ruIMRDf2iTMQeJPHpb 5Pfg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post :list-id:mailing-list:precedence:ui-outboundreport:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from:sender :dkim-signature; bh=KlzdfbnHhFPceSyKy9hZe7I1SORLTS26qMdgwKJHbFE=; fh=Sunj1N57skZOa/kqe7r/hImvzLc1n8z/gzJOupGjXCw=; b=U5H2sZY9bgmSnhRng8RjZjGuBgWzqYKzup2jN1iwlfewWmCcPNejTyjXzi/8GVvgik Q1U3v0wZVNU9OrrMc9Q4Nm4XtRI4gwhIxbjdh95MFuquF51K1ic+lytl4PwQ5Li4dQ9u HtH3O/0CH+AoT5q8uh9S+ib3eQLm3cKR9C3G7+Rsbp9cFSe0FVqnioPHu2K/fvyl9VHc UB4jm2+2cR9qhutob4kBlhVSRZLbjezRCNCDkHwf2aEenS2ybKo/hdnzbnH394WN2cjj aCiJtwyidEoMvHFiljN3QnuK+1sHWcm5Be4X/bT++emAq0Zl/RMPWhjcOaGATVmISA1/ 0Sfg==; darn=patchwork.ozlabs.org ARC-Authentication-Results: i=2; gmr-mx.google.com; dkim=pass header.i=@swupdate.org header.s=s1-ionos header.b=m8se4137; spf=pass (google.com: domain of stefano.babic@swupdate.org designates 212.227.126.187 as permitted sender) smtp.mailfrom=stefano.babic@swupdate.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20230601; t=1708503748; x=1709108548; darn=patchwork.ozlabs.org; h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post :list-id:mailing-list:precedence:x-original-authentication-results :x-original-sender:ui-outboundreport:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:sender:from:to:cc :subject:date:message-id:reply-to; bh=KlzdfbnHhFPceSyKy9hZe7I1SORLTS26qMdgwKJHbFE=; b=XeqHRae+IoLSSv5Us9RzIKV7xVWm5Vsa6kP6b4SExD4kKf2fVwVTRzu6yGygct4g2T wcrQZTT/36+ESdREGDK0TAGw+/HcEgEUkBM8QTpkU3/AVBoZmlHG8jJ34XvzEgG7tzPy ro7SS5VSfGwQZ0D4yUv5g3GIYc7+MK6gxqTaRFbyzGqB0bkSEz0C7uKfRiHD3M2jN4wj jUySLTFU70UiEt/xceJ4fHK6GEmlJ/se81wp8JsOoK7g0lCnGEpo4SsSuX9h09n4i2j9 H5O5/Au24idgpn4E/qUf3QJzpPwYbrS1feLrLomdpaHa37AJpxrWw9VFaz1z4WLFssuv PGKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708503748; x=1709108548; h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post :x-spam-checked-in-group:list-id:mailing-list:precedence :x-original-authentication-results:x-original-sender :ui-outboundreport:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from:x-beenthere:x-gm-message-state:sender:from :to:cc:subject:date:message-id:reply-to; bh=KlzdfbnHhFPceSyKy9hZe7I1SORLTS26qMdgwKJHbFE=; b=Gi0MvszPWXhbqrMUBnKQjWTfjefCjkzryqwx3YpFwAPO3y8bNmAGTLZQwJj5SITZhG ETmlW4NYNn9r0oc+uzAxuJiR6pWUIedC9VlCm1pvFklrZ/Lby/WE1Jd3fpNxYfnZTVe5 AnDU6uXGVicHbAhYk65MEtqul3zRcW0jk+aUF8ao4D6eUIg+UP+MZ7jcTF4d0dUQdGyZ Q1pLrkFdvK2cl/XyHX/8cjJQZSt9TbScP3FLQxKY/PZ6P7koMFV3pdCYFPqxCHsCsLEF 4X+5k/3EBvr/AbxKTG8KW9QPzxhdO7cfCYaNrkGeyJbSi32zVZrVcfjFzx7osnBzeVYu peCg== Sender: swupdate@googlegroups.com X-Forwarded-Encrypted: i=2; AJvYcCVHkGAhksatDkIfCuC44T3HptL2QTyxFy8lYLP539aGWLcHaztscbMF+IO5XzlBQndF8q8H6IzQr15g0MseYu7BdNiWTkTVYd0G8SRDIw== X-Gm-Message-State: AOJu0Yxm5qvHz2/3gNwdl1MAcn6fj9I5tWicKVUGrlbuHPRQCt2Flbk1 5rEO3VXxqRVdSPS6Yxza96jcCjpjUFZq5XJSCcIU9V9eYUQIB4ra X-Google-Smtp-Source: AGHT+IHblJrsNjrTeYMqMFE3PBVAVL0TJviiKVpob49o2KIjHNZMF8NARmZ2aHPVjbyNKq8fvpJgcw== X-Received: by 2002:a05:6512:31c5:b0:512:b773:2800 with SMTP id j5-20020a05651231c500b00512b7732800mr6129330lfe.34.1708503748059; Wed, 21 Feb 2024 00:22:28 -0800 (PST) X-BeenThere: swupdate@googlegroups.com Received: by 2002:a05:6512:3b07:b0:512:bda3:91c6 with SMTP id f7-20020a0565123b0700b00512bda391c6ls27262lfv.1.-pod-prod-08-eu; Wed, 21 Feb 2024 00:22:26 -0800 (PST) X-Received: by 2002:a19:7404:0:b0:512:9d0e:2ca5 with SMTP id v4-20020a197404000000b005129d0e2ca5mr7973470lfe.39.1708503745932; Wed, 21 Feb 2024 00:22:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1708503745; cv=none; d=google.com; s=arc-20160816; b=H49kSRjx44fggyGFkmvchwc5Jbsxxf+ba/6WTleLAdotxqnXVmAOXnPA9s1RuM4RUS B1/Duj+ASvAIouB9lpWD0jdeHAl7nCpJwWmM7zDeAUQ9Ai/CRN2y2DsHOhqb76fBEIQn Gtb01QucClGt3GGNAXpIFt5VfO2XI70H9522GmLrkrZeA43RckKvC8PqNMIGrAo7GVSQ kJyf9vDgeGyTBq6DPS/Q15/ZUeE85CmCITwQBUIAxtdkByIKQ1Do0dIasEMpAU2UmoxD 3evQIouRuJ4dsltvGtLdXYm7Vcvk7sJ5YeLBXehdeLKmLUoRsGOp3yWHdkensgS1ySY4 ao9A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=ui-outboundreport:content-transfer-encoding:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:dkim-signature; bh=DXEkNzDKkIDEhwcekOQETtrRMk1DHGDxN9tVM3AAd6U=; fh=TiEWcqMcKpHk5s7uErzpntoONrNfOXwKpI5P8bIlggk=; b=UkkizXeYgulZXT0I3nUOv4jZoGpSdfhmjCIOxjumQk5Ol+p/KacNkcZXopEBagQW1R dbbfmM7WloK4Jo8ytlkVuPsSi5mDqLFERRkaU/oIn5pnpvXtqGj6pO0/gMtaSG6jdFCh 5zlxrz7/Pb4nI9DfEgDkgXTNHXdrUvM/9YpliNws7CldERK5+8KvpBtrdlR6LL0LmJrI rFYEHpOUJqMXPOz2CbiPDa7OaRnA8ObvKom1Qb14PnDN4d16Z76ct8B8Egat69FlaGpa zrJiSQxNUAYzWT+mNW2M8Hk8ULsP0iirfXiPhtaoj3cooZq29DN+QFGcfX6knwuT07y9 CP+g==; dara=google.com ARC-Authentication-Results: i=1; gmr-mx.google.com; dkim=pass header.i=@swupdate.org header.s=s1-ionos header.b=m8se4137; spf=pass (google.com: domain of stefano.babic@swupdate.org designates 212.227.126.187 as permitted sender) smtp.mailfrom=stefano.babic@swupdate.org Received: from mout.kundenserver.de (mout.kundenserver.de. [212.227.126.187]) by gmr-mx.google.com with ESMTPS id k21-20020ac24f15000000b00511a71805a8si466345lfr.8.2024.02.21.00.22.25 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Feb 2024 00:22:25 -0800 (PST) Received-SPF: pass (google.com: domain of stefano.babic@swupdate.org designates 212.227.126.187 as permitted sender) client-ip=212.227.126.187; X-UI-Sender-Class: 55c96926-9e95-11ee-ae09-1f7a4046a0f6 Received: from paperino.fritz.box ([88.217.136.221]) by mrelayeu.kundenserver.de (mreue011 [213.165.67.97]) with ESMTPSA (Nemesis) id 1MD9jV-1rlCKW0pDl-009BSd; Wed, 21 Feb 2024 09:22:25 +0100 From: Stefano Babic To: swupdate@googlegroups.com Cc: Stefano Babic Subject: [swupdate] [PATCH 11/13] Lua Script Handler: allows to run scripts globally Date: Wed, 21 Feb 2024 09:22:19 +0100 Message-Id: <20240221082221.11997-12-stefano.babic@swupdate.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240221082221.11997-1-stefano.babic@swupdate.org> References: <20240221082221.11997-1-stefano.babic@swupdate.org> MIME-Version: 1.0 X-Provags-ID: V03:K1:Zl/IbtvW5fRFyZtPvXxSbFGjixxAcU3L9mcCmELcuoWjlsSWJpa xcbaAuI3FMo9kyDGU9V5D89/SjQIEEFvoogz+G8NwBSSlFmMuUX/8u/DXsnlfq7zT0L+ukx +Mn8LsLCXLSZ+QNIP4V5S0xitHCgoe07Ah5kJtQF3eDUvFM/xl/BDl7wMgdr123ETf18nqd IAawT/G05vIt+dY128f1A== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:xF9auli3NgM=;BL358p0rnh4JlXJjTcP1m6MIQCk jNkwPe/eNcLdSe1DLqnBms5wK7iEj81ykwURRbK5lLSvR1D0aU4dia1H/253MInkgDmREFr9m VVjlnmjdT18sTG++1dIRPj5OhHkAB2rsokWt9SywxeZQaZQeC7c9BD+BpRWrjhz1LFkwecebb QW2HZOzB/JbdknAIHqVPj1YTGKIEK6EXog1HdzPBVq97eJrvAN3ukkE9rs4+mrDhsKG+wd5Do X1htRBOcjfnE2dRALFhki4aCpS8nVctLcEYudiFuhKIhEyodXMT9u9YqPy+MVWZCDG09x8Mxf UCFUas7ojNP/duGEDrugJcVmzc8laGxwc/eXQOa4QuYM8fpo+9BOy+OCfJPPm6fUETfJrwk8W t37ARMhj5l5iKGXg3vHW+LLqqLe6+tHOHHGiXSJPcU8BNm1Ui4nTG/ziigvITiFECVe88aepF ZahQZ95bUkdvJ99jEOLsGa/ZOgzkIQl0FZLRt7FsTfX4edFt3x73PQR6ul8HLfHQxkVjcpM7n 5lWTGbkdFx9UEaoZpp00BWROg8tBHuM4sr+1pWO+Tp8oYgaz78PmqeGnsMQWsj4bLU3PuJzGe op9OknrdUuMmAKX/9PEwnDEuOovEzuGBFJlae7ZV6eUDEaEiSk5rKQKvbbx4EjX8n8Qw8/vJ1 ae2bzgNQs5JiEq1pXRbagN/rofOeHaDR4LJGZLuAE/+TBjOECaVK/a4YMNXvOYbK+WDSYd7of /sXUGJMqNif6zAeU7dHbmeUkjFLY7iZZrusQV3m5+JWt68o5Pp2e1A= X-Original-Sender: stefano.babic@swupdate.org X-Original-Authentication-Results: gmr-mx.google.com; dkim=pass header.i=@swupdate.org header.s=s1-ionos header.b=m8se4137; spf=pass (google.com: domain of stefano.babic@swupdate.org designates 212.227.126.187 as permitted sender) smtp.mailfrom=stefano.babic@swupdate.org Precedence: list Mailing-list: list swupdate@googlegroups.com; contact swupdate+owners@googlegroups.com List-ID: X-Spam-Checked-In-Group: swupdate@googlegroups.com X-Google-Group-Id: 605343134186 List-Post: , List-Help: , List-Archive: , List-Unsubscribe: , Up now, scripts have run in isolated context: SWUpdate allocates a new Lua state, and import the basic libraries before loading the script. A script is then isolated, but it cannot access to function already loaded, or it is not possible to reuse functions from 2 or more scripts. With the introduction of a per installation Lua state, Lua scripts can call functions already defined in previous scripts, or defined in sw-description. Because when a script is loaded, it could overwrite existing functions with the same name, it was decided that functions in scripts must be unique, that is each function should be declared just once during an installation process. This means that for global state, sw-description should contain the name of the function for each step (pre- , postinstall or postfailure) that should be called: the names preinst, postinst and postfailure are still valid in case the script runs with isolated state. This allows also to load a script without executing if no functions are defined, and functions in the script can be called by later scripts. Note that the handler will load the script in case of global state just once during the "preinstall" call. Later, it is assumed that functions will be already available. Signed-off-by: Stefano Babic --- corelib/lua_interface.c | 26 ++++++---- handlers/lua_scripthandler.c | 95 +++++++++++++++++++++++++++--------- include/lua_util.h | 2 +- 3 files changed, 90 insertions(+), 33 deletions(-) -- 2.34.1 diff --git a/corelib/lua_interface.c b/corelib/lua_interface.c index 78e27576..747cd96b 100644 --- a/corelib/lua_interface.c +++ b/corelib/lua_interface.c @@ -204,27 +204,35 @@ static void lua_report_exception(lua_State *L) } while (*s++); } -int run_lua_script(lua_State *L, const char *script, const char *function, char *parms) +int run_lua_script(lua_State *L, const char *script, bool load, const char *function, char *parms) { int ret; const char *output; - if (!function || !L) + if (!L) { + ERROR("Lua script must be executed, but no valid Lua state was set"); return -EINVAL; + } - if (script) { + if (load) { + TRACE("Loading Lua %s script", script); if (luaL_loadfile(L, script)) { ERROR("ERROR loading %s", script); return -1; } + + ret = lua_pcall(L, 0, 0, 0); + if (ret) { + LUAstackDump(L); + ERROR("ERROR preparing Lua script %s %d", + script, ret); + return -1; + } } - ret = lua_pcall(L, 0, 0, 0); - if (ret) { - LUAstackDump(L); - ERROR("ERROR preparing Lua script %s %d", - script, ret); - return -1; + if (!function) { + WARN("Script was loaded, no function was set to be executed !"); + return 0; } lua_getglobal(L, function); diff --git a/handlers/lua_scripthandler.c b/handlers/lua_scripthandler.c index 83440e81..009c0557 100644 --- a/handlers/lua_scripthandler.c +++ b/handlers/lua_scripthandler.c @@ -24,10 +24,21 @@ static void lua_handler(void); +struct fn_names { + const char *property_name; /* Name of property in sw-description */ + const char *def_fn; /* Default name function if property is not set */ +}; + +const struct fn_names fn_property_names[] = { + [PREINSTALL] = {"preinstall", "preinst"}, + [POSTINSTALL] = {"postinstall", "postinst"}, + [POSTFAILURE] = {"postfailure", "postfailure"} +}; + static int start_lua_script(struct img_type *img, void *data) { int ret; - const char *fnname; + const char *fnname = NULL; struct script_handler_data *script_data; lua_State *L; const char* tmp = get_tmpdirscripts(); @@ -36,8 +47,29 @@ static int start_lua_script(struct img_type *img, void *data) if (!data) return -1; + script_data = data; + + /* + * A little paranoid, thios shouln't happen + */ + if (script_data->scriptfn < PREINSTALL || script_data->scriptfn > POSTFAILURE) + return -EINVAL; + bool global = strtobool(dict_get_value(&img->properties, "global-state")); + /* + * Note: if global is set, functions should be unique + * The name of function should be set inside the script + */ + fnname = dict_get_value(&img->properties, fn_property_names[script_data->scriptfn].property_name); + + if (!fnname && !global) { + fnname = fn_property_names[script_data->scriptfn].def_fn; + } + + /* + * Assign the Lua state + */ if (global) { TRACE("Executing with global state"); L = img->L; @@ -50,34 +82,51 @@ static int start_lua_script(struct img_type *img, void *data) return -1; } - script_data = data; + /* + * In case of global, loads the script just once. Check if a function is already + * present. An Overwrite is excluded by design. All functions should be unique. + */ - switch (script_data->scriptfn) { - case PREINSTALL: - fnname = dict_get_value(&img->properties, "preinstall"); - if (!fnname) - fnname="preinst"; - break; - case POSTINSTALL: - fnname = dict_get_value(&img->properties, "postinstall"); - if (!fnname) - fnname="postinst"; - break; - case POSTFAILURE: - fnname = dict_get_value(&img->properties, "postfailure"); - if (!fnname) - fnname="postfailure"; - break; - default: - /* no error, simply no call */ - return 0; + bool load_script = true; + + if (global) { + if (script_data->scriptfn == PREINSTALL) { + for (int i = PREINSTALL; i <= POSTINSTALL; i++) { + const char *fn = fn_property_names[script_data->scriptfn].property_name; + if (!fn) + continue; + lua_getglobal(L, fn); + if(lua_isfunction(L,lua_gettop(L))) { + ERROR("Function %s already defined, functions must be unique", fn); + return -1; + } + } + } else { + /* + * Script was already loaded, skip it + */ + load_script = false; + } } + /* + * Trace what should be done + */ snprintf(filename, sizeof(filename), "%s%s", tmp, img->fname); - TRACE("Calling Lua %s with %s", filename, fnname); + TRACE("%s: Calling Lua %s with %s", + fn_property_names[script_data->scriptfn].property_name, + filename, + fnname ? fnname : "no function, just loaded"); + + /* + * In case no function is selected and we run in global, + * script was already loaded and there is nothing to do + */ + if (global && !fnname && !load_script) + return 0; - ret = run_lua_script(L, filename, fnname, img->type_data); + ret = run_lua_script(L, filename, load_script, fnname, img->type_data); if (!global) lua_close(L); diff --git a/include/lua_util.h b/include/lua_util.h index ff603b5c..7b34eb89 100644 --- a/include/lua_util.h +++ b/include/lua_util.h @@ -20,7 +20,7 @@ typedef enum { } root_dev_type; void LUAstackDump (lua_State *L); -int run_lua_script(lua_State *L, const char *script, const char *function, char *parms); +int run_lua_script(lua_State *L, const char *script, bool load, const char *function, char *parms); lua_State *lua_init(struct dict *bootenv); int lua_load_buffer(lua_State *L, const char *buf); int lua_parser_fn(lua_State *L, const char *fcn, struct img_type *img);