[PATCH 1/2] Factorize function to parse linux command line

16 views
Skip to first unread message

Stefano Babic

unread,
May 8, 2025, 12:30:31 PMMay 8
to swup...@googlegroups.com, Stefano Babic
This can be reused inside the project and made available to Lua.

Signed-off-by: Stefano Babic <stefan...@swupdate.org>
---
core/util.c | 23 +++++++++++++++++------
include/util.h | 1 +
2 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/core/util.c b/core/util.c
index c3fca34c..c106a94e 100644
--- a/core/util.c
+++ b/core/util.c
@@ -1164,11 +1164,10 @@ static char *get_root_from_mountinfo(void)
}

#define MAX_CMDLINE_LENGTH 4096
-static char *get_root_from_cmdline(void)
+char **parse_linux_cmdline(void)
{
char *buf;
FILE *fp;
- char *root = NULL;
int ret;
char **parms = NULL;

@@ -1192,10 +1191,25 @@ static char *get_root_from_cmdline(void)
*/
buf[MAX_CMDLINE_LENGTH - 1] = '\0';

+ fclose(fp);
+
if (ret > 0) {
parms = string_split(buf, ' ');
if (!parms)
goto out;
+ }
+out:
+ free(buf);
+ return parms;
+}
+
+static char *get_root_from_cmdline(void)
+{
+ char **parms = NULL;
+ char *root = NULL;
+
+ parms = parse_linux_cmdline();
+ if (parms) {
int nparms = count_string_array((const char **)parms);
for (unsigned int index = 0; index < nparms; index++) {
if (!strncmp(parms[index], "root=", strlen("root="))) {
@@ -1207,11 +1221,8 @@ static char *get_root_from_cmdline(void)
break;
}
}
+ free_string_array(parms);
}
-out:
- fclose(fp);
- free_string_array(parms);
- free(buf);
return root;
}

diff --git a/include/util.h b/include/util.h
index 07c20770..f49fe8cc 100644
--- a/include/util.h
+++ b/include/util.h
@@ -300,6 +300,7 @@ void get_install_swset(char *buf, size_t len);
void get_install_running_mode(char *buf, size_t len);
bool is_dryrun_install(void);
char *get_root_device(void);
+char **parse_linux_cmdline(void);

/* Setting global information */
void set_version_range(const char *minversion,
--
2.43.0

Stefano Babic

unread,
May 8, 2025, 12:30:33 PMMay 8
to swup...@googlegroups.com, Stefano Babic
This parse and convert the Linux command line into a table, easier to
manage in Lua. This simplifies Lua routines because the parsing of
cmdline is already done in core.

Signed-off-by: Stefano Babic <stefan...@swupdate.org>
---
corelib/lua_interface.c | 36 ++++++++++++++++++++++++++++++++++++
handlers/swupdate.lua | 4 ++++
2 files changed, 40 insertions(+)

diff --git a/corelib/lua_interface.c b/corelib/lua_interface.c
index ffe62d88..ec1eadab 100644
--- a/corelib/lua_interface.c
+++ b/corelib/lua_interface.c
@@ -888,6 +888,41 @@ static int l_getroot(lua_State *L) {
return 1;
}

+static int l_get_cmdline(lua_State *L) {
+ char **parms = NULL;
+
+ parms = parse_linux_cmdline();
+ if (!parms) {
+ lua_pushnil(L);
+ return 1;
+ }
+ lua_newtable (L);
+
+ int nparms = count_string_array((const char **)parms);
+ TRACE("parms=%d", nparms);
+ for (unsigned int index = 0; index < nparms; index++) {
+ char *equal = strchr(parms[index], '=');
+ const char *val;
+ if (equal) {
+ val = equal + 1;
+ /* split into two strings */
+ *equal = '\0';
+ } else {
+ /*
+ * A table cannot have a nil value
+ * so just set an empty string
+ */
+ val = "";
+ }
+ lua_pushstring(L, parms[index]);
+ lua_pushstring(L, val);
+ lua_settable(L, -3);
+ }
+
+ free_string_array(parms);
+ return 1;
+}
+
static int l_get_bootenv(lua_State *L) {
const char *name = luaL_checkstring(L, 1);
char *value = NULL;
@@ -1225,6 +1260,7 @@ static const luaL_Reg l_swupdate_bootenv[] = {
{ "get_bootenv", l_get_bootenv },
{ "set_bootenv", l_set_bootenv },
{ "get_selection", l_get_selection },
+ { "get_cmdline", l_get_cmdline },
{ NULL, NULL }
};

diff --git a/handlers/swupdate.lua b/handlers/swupdate.lua
index b1412517..aa8a31d0 100644
--- a/handlers/swupdate.lua
+++ b/handlers/swupdate.lua
@@ -197,6 +197,10 @@ swupdate.getversion = function() end
--- @return string # Mode
swupdate.get_selection = function() end

+--- Get Linux Command Line Parameters
+--
+--- @return table # Table with keys and values fields
+swupdate.get_cmdline = function() end

--- Set Bootloader environment key=value.
--
--
2.43.0

vincent....@gmail.com

unread,
Dec 8, 2025, 4:41:20 AM (13 days ago) Dec 8
to swupdate
Hello Stefano, 

Could you provide a simple  Lua example for this new 2025.12 feature?

Thanks by advance,
Vincent

Stefano Babic

unread,
Dec 9, 2025, 9:57:25 AM (12 days ago) Dec 9
to vincent....@gmail.com, swupdate
Hi Vincent,

On 12/8/25 10:41, vincent....@gmail.com wrote:
> Hello Stefano,
>
> Could you provide a simple Lua example for this new 2025.12 feature?
>

I will write a patch for the doc.

Regards,
Stefano
> --
> You received this message because you are subscribed to the Google
> Groups "swupdate" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to swupdate+u...@googlegroups.com
> <mailto:swupdate+u...@googlegroups.com>.
> To view this discussion visit https://groups.google.com/d/msgid/
> swupdate/ee1f3173-2266-434f-b77c-f82b0f0b1c68n%40googlegroups.com
> <https://groups.google.com/d/msgid/swupdate/ee1f3173-2266-434f-b77c-
> f82b0f0b1c68n%40googlegroups.com?utm_medium=email&utm_source=footer>.

Reply all
Reply to author
Forward
0 new messages