From patchwork Wed Feb 21 08:22:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefano Babic X-Patchwork-Id: 1901888 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=JogFI5lQ; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=googlegroups.com (client-ip=2a00:1450:4864:20::53a; helo=mail-ed1-x53a.google.com; envelope-from=swupdate+bncbaabbqpf22xamgqehl6i7aq@googlegroups.com; receiver=patchwork.ozlabs.org) Received: from mail-ed1-x53a.google.com (mail-ed1-x53a.google.com [IPv6:2a00:1450:4864:20::53a]) (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 4Tfq5M05lnz23d4 for ; Wed, 21 Feb 2024 19:22:30 +1100 (AEDT) Received: by mail-ed1-x53a.google.com with SMTP id 4fb4d7f45d1cf-5640681bc11sf2772827a12.2 for ; Wed, 21 Feb 2024 00:22:29 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708503747; cv=pass; d=google.com; s=arc-20160816; b=MerH0Ify/HCDD9xyVFN34x7XUPONFGKy0lE3UBGx4Ktxeud+dLNhsJtkhQXBuUASx7 VRd1F7pjngijbT2YjDNiqNKLjwctnmYHRDOhdG6mLFfOcdHGL2Lu4BHXE+2lylKsG8XY KpH2XBVeB/pH8A17GG5wniYDd2gvzZu0rXIfXWulmWnENy1zzU4juZbKT8jVR5hErCH5 Vc+RiDRfX6KF08THRwYs6FvmqxR44cBXXijvmOxyMhUR1qTRmAPn7YIFqcLx6AN41r43 JPqK/s4ov+mOC530/0+2wK0zHfCu3HB1cZxQ4A077QdHx3bDgAmy5jtrk6LVDCjIlenC VUDg== 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=wWEzEYhBP9NlK0L+Ide0mW6Ad6R8IrCHI88E/XOA+Vw=; fh=wm3wGdgSyShPTWuQFANL7TxLJYbI9U8+GZxFCehkarE=; b=rGIif7W9XfD2qhXw7sxcILBuA9qN1iTERn5kLy0U0mfBVPO+1mFKsbkGAduimjRLJS 3BPGpVoo+1SwM4jiSHicsLIxcj9xhOiBq5eBZuKroFlhYj/A2C2/siPX0DIexqNe18b4 dCkLXb4MPCvu3fdZ/H67BC7WbASWEdCxk4okRDGXK+wQYMJy3wWmReSlxdAwpV5HI1K+ fFhGdENAUVdT5+I5K6sRmcb5Uwa6/RCiafz97cDwKctYlzHW11VHH8NJM2w31iXHEBHL 6q3UQzSAaS9NAPynvGr5bCzUYeEL796+v5muuN1VwJ66YdtDJxDBLmctoh20J7M5BjY1 ha4A==; 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="R/3XsAie"; spf=pass (google.com: domain of stefano.babic@swupdate.org designates 212.227.126.134 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=1708503747; x=1709108547; 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=wWEzEYhBP9NlK0L+Ide0mW6Ad6R8IrCHI88E/XOA+Vw=; b=JogFI5lQDjDeAjQYr87VKLWSIQwVQMdQyGucz09CAzTK88iyN0PSN/XIje0jJ6HFCL 68GFCeemFbNi+Tj/8WTSnL345K5gYZXG3RdMBZ64KgRMDHBJyPbnJZoktF4IVrFT53hq Ss+mqLy6Hpla5MqFH9HeTB/iXhD19X5Eroc8gG17q+fcDer/gRJEa7/HGRECo5NgKMcC IDUyltlvgC6TBEOcxsjUV38J0wcLyzT6pmmBS+cIG4XDX640K2kkp1ZPc83EyNO56XNU jwnFPgAlj/FLo9kHkM8BA/0D7drpP3ddMjpVQ42RitPIaSL2RFmMi3IcbAf11BSmxECf zl/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708503747; x=1709108547; 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=wWEzEYhBP9NlK0L+Ide0mW6Ad6R8IrCHI88E/XOA+Vw=; b=SdVEE3EtKVTnduxHlrM6iW4vK2TvKn2+yNVT0LVH/WAaYfBsxE+Iy+izLJbC+WmhWn S/Eqk8uJY4Ft12O3VvZ2tqzRNFm5o+ZzZV7fVuKnwmHBhNV7zXKppDvhNA00m5qmnO56 J+fjNZ1Z2CzctgjKrlVjOJ0vAWD2/WUivC5NwgyZ0JQJjdKFaZIpuiy4WjaAM6IloJhz 1i0exvQROfrISCvuIC3RKuhLFIOWXdLXP0vGDA018cdK2leJ8Mn3saFO8DilN7mmMaQm BX+7BSfaVIaEQYm1613+TgJfAtCkIN7lu3vRwak4FfwfwYmLVQCeg9NKCJOOytlmwB/E 2kkw== Sender: swupdate@googlegroups.com X-Forwarded-Encrypted: i=2; AJvYcCVx4Q1lo2I0SCV6w0SmfHIajXracT2d3PuznrbSlnStp2Tu7YGCqL+JwmwhivapRoGalHqWY4BzeSVJXtmpOKQ9d0DCSaQIRD0yXavb6g== X-Gm-Message-State: AOJu0YyNJ15BNySpU6nwW/rIYwzkX8YO6g2lXFK8FxpqJpEkXyDnRIYc EnFEqxxw2WtClrgJYuCwdSOtMgfc7yF6DLJsiC8xMl+n5aNmPJog X-Google-Smtp-Source: AGHT+IEGvb1V/44J7ReJ8SdpBgZYVbvSZbCrCRmhDlumzJCd4+nc5aDjV5e8NEW0K7aAccKu1oia5w== X-Received: by 2002:a05:6402:134d:b0:564:a4e9:3683 with SMTP id y13-20020a056402134d00b00564a4e93683mr6105685edw.16.1708503746213; Wed, 21 Feb 2024 00:22:26 -0800 (PST) X-BeenThere: swupdate@googlegroups.com Received: by 2002:a05:6402:4018:b0:562:c01:57f9 with SMTP id d24-20020a056402401800b005620c0157f9ls576819eda.1.-pod-prod-01-eu; Wed, 21 Feb 2024 00:22:24 -0800 (PST) X-Received: by 2002:aa7:db53:0:b0:565:dff:cd4d with SMTP id n19-20020aa7db53000000b005650dffcd4dmr181354edt.13.1708503744413; Wed, 21 Feb 2024 00:22:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1708503744; cv=none; d=google.com; s=arc-20160816; b=aU6Dir2cieHrVsXBYZryYU1+ywMM7XTbvZ++rjH2OzI4QkAhRu79u5AMHd3puPQ9N7 5RbjBptFVNVSSWksDslVHCAYoHoEvkv8Mo/JAVoEVOevFJw6ao14z0Kh0y5PVrOimvoE v1Z+Cfodj4t3JSkcJsuKTIeti5aLhIcLiYG245WSup4kYv+94SBD+zVK60eI1vtnxXVg D/9OCytgNuuQ2/uyla2k/wmCrUr/EHCJeObBUk6AxBC3qxdEr+zUWYFaET0Ghwo77XCQ vrjkcAZczPLLzAD385LfJT6BdfpTOMo0fbkp27jXKGUMJgvLa7uuQT6N+SNcEe3YQZqD PgmA== 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=qGHtzsUmj5bw/HTTeGC+H46S8D3k6rtbw9/35TkPi00=; fh=TiEWcqMcKpHk5s7uErzpntoONrNfOXwKpI5P8bIlggk=; b=nIq8yG/doKTpyA7vIK1wohsrl9oHPBpqpxJYwL88bak5llVh1c5dnfNRxqqjB84I6R +nnJkH+Z51sYgilfDLSLTrzwAtuYL/t1bkLHGneDLSHCDRKpaqcvKumHMRi+LF/wtxwb +63ULznWU4MuySQiAY3Ih9InK743UZ9puSS6Kf1plpLg5F1jT6lm/+eZ64wgZlrEBcTO 9a76nX+zVE3ghej+TiR5RgST88sU1/9Da1ghbLZqxaM42+YFMvW8ZFDUy66t8DHp47lF MYSr2j+ulP5rzpH+FTXO+h/vTDrujWv7DePMnl6oJ2lodnGzDEJrvQ9FTjkU3tiqxJKK guyw==; dara=google.com ARC-Authentication-Results: i=1; gmr-mx.google.com; dkim=pass header.i=@swupdate.org header.s=s1-ionos header.b="R/3XsAie"; spf=pass (google.com: domain of stefano.babic@swupdate.org designates 212.227.126.134 as permitted sender) smtp.mailfrom=stefano.babic@swupdate.org Received: from mout.kundenserver.de (mout.kundenserver.de. [212.227.126.134]) by gmr-mx.google.com with ESMTPS id x2-20020a50d602000000b00564af3e693fsi312796edi.5.2024.02.21.00.22.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Feb 2024 00:22:24 -0800 (PST) Received-SPF: pass (google.com: domain of stefano.babic@swupdate.org designates 212.227.126.134 as permitted sender) client-ip=212.227.126.134; 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 1MoOMq-1r9vXz3wrI-00os02; Wed, 21 Feb 2024 09:22:23 +0100 From: Stefano Babic To: swupdate@googlegroups.com Cc: Stefano Babic Subject: [swupdate] [PATCH 01/13] Lua: prepare to extend Lua lifetime Date: Wed, 21 Feb 2024 09:22:09 +0100 Message-Id: <20240221082221.11997-2-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:yeR8HTZfXZPZB7743lk+cL94olCLq+WHmOZglm9QjyyUt9T9cOq sOb+ebwenKaMVggPmv6BgaiRRwGu9TLo7hbpW6ReFNUeltDSkNnafPpBidxNoc5crZzwvJC JAaY5Ojei1xcc0R7comFi8x6WmUa/cqEqQv6eni5PLn8NSs1+zM63MGh9JKzQjTkFrdpiQa m2ws1SQD3bMJteoD6hYRA== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:QPqUIUtWqCY=;se8axlseEphiNpiO12JL8C8Lxf+ N3/PGxDUU+zBr9UnM9dWU73IjUF6kXExSoSr4PBgebjV8RW8nqCtXf5zu+UNsAPlMbNv+UmMI bCmzy0ucAkW049iqNZDvpedozFf6cK2w8g5TO75sjXlyWEOqKmzBdXMXNZteWkVSnesn9KSLD VaBr1D1ENb6gLkQnSZ2+7PPgg8sRt14pDHLNJo4pv1/nyHgKBf1+6RMSiU8LVUWeuK9uC5KZR YQiqZaFg2EJmHQIfjdazNd7WQcS2tOjPA5fEJHEUDtL7Eq+NmxvSViV4fv9guWk40Ei8CgCzT 6hKhMeg871JXhKzKGb90IkHUJn2SyYCCxyijn+t85CmczFtFstD4VQBdd6XvoxpyVYdCEyjJ2 UcJnbOQPQgZZxH5+oMM87KL0YQ9Xfw5xW6PPFszqkEyV2efDJHRCb7MQ9ubCwE50+BXU9SQD/ 3dfdRpsOR2sWPWgjBsCVT6Jo4vUB2oER1Rja/neSKVl7TUR3ylTx3mN29spcOUw3iN+u9wmBZ uGgtbDvtrPYvd66kXGjf6UiB7b3LQrKNGdEpUrM3t+qe14pI+QcAPoh5drouV5EEmhZPCy9Ao 6iodoilfmVQ8ms9Dwa0lpAyuRg3/ApnlPIuTOqAlVLFC9Um9iWCz+u7vtPDQZ+CXF8IGICgra xUCLxM4JPvEFyhOh4CViU/WdNd9N5E3uu27OTsCgOedG1NZ1d7d7qiHYMfre09gZzcUUNN1cd UgoBTrPxC94noRp8eADWhRwd9j7wfNfm2ETX740BqBbjNxeR18FSQo= 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="R/3XsAie"; spf=pass (google.com: domain of stefano.babic@swupdate.org designates 212.227.126.134 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: , A Lua context is created and destroyed for each Lua scriptthat should run. This isolates each script, but it does not allow that a later script can benefit and load a function previously defined. It was decided that so high isolation is not necessary - instead of having a context for each script, create a Lua context for each installation request. The Lua context is then initialized when sw-description is parsed and removed when the installation has finished, independently from the state. Signed-off-by: Stefano Babic --- core/installer.c | 8 ++++++++ corelib/lua_interface.c | 14 +++++++++----- include/lua_util.h | 12 +++++++----- include/swupdate.h | 1 + parser/parser.c | 16 +++++++++++----- 5 files changed, 36 insertions(+), 15 deletions(-) -- 2.34.1 diff --git a/core/installer.c b/core/installer.c index 20b5b51e..2b85a6e7 100644 --- a/core/installer.c +++ b/core/installer.c @@ -33,6 +33,7 @@ #include "progress.h" #include "pctl.h" #include "swupdate_vars.h" +#include "lua_util.h" /* * function returns: @@ -478,6 +479,13 @@ void cleanup_files(struct swupdate_cfg *software) { dict_drop_db(&software->bootloader); dict_drop_db(&software->vars); + /* + * Drop Lua State if instantiated + */ + if (software->lua_state) { + lua_exit(software->lua_state); + software->lua_state = NULL; + } if (asprintf(&fn, "%s%s", TMPDIR, BOOT_SCRIPT_SUFFIX) != ENOMEM_ASPRINTF) { remove_sw_file(fn); free(fn); diff --git a/corelib/lua_interface.c b/corelib/lua_interface.c index 2bd8e6cf..33be8d3b 100644 --- a/corelib/lua_interface.c +++ b/corelib/lua_interface.c @@ -1517,7 +1517,7 @@ int lua_handlers_init(void) return ret; } -lua_State *lua_parser_init(const char *buf, struct dict *bootenv) +lua_State *lua_init(struct dict *bootenv) { lua_State *L = luaL_newstate(); /* opens Lua */ @@ -1533,14 +1533,18 @@ lua_State *lua_parser_init(const char *buf, struct dict *bootenv) luaL_setfuncs(L, l_swupdate_bootenv, 1); lua_pop(L, 1); /* remove unused copy left on stack */ + return L; +} + +int lua_load_buffer(lua_State *L, const char *buf) +{ if (luaL_loadstring(L, buf) || lua_pcall(L, 0, 0, 0)) { LUAstackDump(L); - ERROR("ERROR preparing Lua embedded script in parser"); - lua_close(L); - return NULL; + ERROR("ERROR loading Lua code"); + return 1; } - return L; + return 0; } int lua_parser_fn(lua_State *L, const char *fcn, struct img_type *img) diff --git a/include/lua_util.h b/include/lua_util.h index 1d00bf82..5f5e67a7 100644 --- a/include/lua_util.h +++ b/include/lua_util.h @@ -21,7 +21,8 @@ typedef enum { void LUAstackDump (lua_State *L); int run_lua_script(const char *script, const char *function, char *parms); -lua_State *lua_parser_init(const char *buf, struct dict *bootenv); +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); int lua_handlers_init(void); @@ -34,7 +35,7 @@ int lua_notify_progress(lua_State *L); int lua_get_swupdate_version(lua_State *L); -#define lua_parser_exit(L) lua_close((lua_State *)L) +#define lua_exit(L) lua_close((lua_State *)L) #if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM == 501 #define LUA_OK 0 @@ -81,9 +82,10 @@ void luaL_pushresult(luaL_Buffer_52 *B); #else #define lua_State void -#define lua_parser_exit(L) -static inline lua_State *lua_parser_init(const char __attribute__ ((__unused__)) *buf, - struct dict __attribute__ ((__unused__)) *bootenv) { return NULL;} +#define lua_exit(L) +static inline lua_State *lua_init(struct dict __attribute__ ((__unused__)) *bootenv) { return NULL;} +static inline int lua_load_buffer(lua_State __attribute__ ((__unused__)) *L, + const char __attribute__ ((__unused__)) *buf) {return 1;} static inline int lua_parser_fn(lua_State __attribute__ ((__unused__)) *L, const char __attribute__ ((__unused__)) *fcn, struct img_type __attribute__ ((__unused__)) *img) { return -1; } diff --git a/include/swupdate.h b/include/swupdate.h index 883bad51..e18de8d3 100644 --- a/include/swupdate.h +++ b/include/swupdate.h @@ -59,6 +59,7 @@ struct swupdate_cfg { char mtdblacklist[SWUPDATE_GENERAL_STRING_SIZE]; char forced_signer_name[SWUPDATE_GENERAL_STRING_SIZE]; char namespace_for_vars[SWUPDATE_GENERAL_STRING_SIZE]; + void *lua_state; bool syslog_enabled; bool no_downgrading; bool no_reinstalling; diff --git a/parser/parser.c b/parser/parser.c index 1d9a38db..3944de64 100644 --- a/parser/parser.c +++ b/parser/parser.c @@ -1022,15 +1022,24 @@ static int parser(parsertype p, void *cfg, struct swupdate_cfg *swcfg) swcfg->embscript = get_field_string(p, scriptnode, NULL); } + L = lua_init(&swcfg->bootloader); + if (swcfg->embscript) { + if (!L) { + ERROR("Required embedded script but no Lua not available"); + return -1; + } if (loglevel >= DEBUGLEVEL) TRACE("Found Lua Software:\n%s", swcfg->embscript); - L = lua_parser_init(swcfg->embscript, &swcfg->bootloader); - if (!L) { + if (lua_load_buffer(L, swcfg->embscript)) { ERROR("Required embedded script that cannot be loaded"); + lua_close(L); return -1; } } + + swcfg->lua_state = L; + if (get_hw_revision(&swcfg->hw) < 0) { TRACE("Hardware compatibility not found"); } @@ -1049,9 +1058,6 @@ static int parser(parsertype p, void *cfg, struct swupdate_cfg *swcfg) */ parse_partitions(p, cfg, swcfg, L); - if (L) - lua_parser_exit(L); - if (LIST_EMPTY(&swcfg->images) && LIST_EMPTY(&swcfg->scripts) && LIST_EMPTY(&swcfg->bootloader)) {