diff mbox series

[1/5] lua: add wrapper for handlers

Message ID 20240309151159.8426-2-stefano.babic@swupdate.org
State Accepted
Headers show
Series Introduce generic Uploader | expand

Commit Message

Stefano Babic March 9, 2024, 3:11 p.m. UTC
Add a way to call a custom Lua function that can  be used by handlers to
check some results. The function receives a string as parameter, and if
multiple paramters are required they must be combined into a string. The
function returns a swupdate_status result, for example:

	return swupdate.swupdate.RECOVERY_STATUS.RUN

Signed-off-by: Stefano Babic <stefano.babic@swupdate.org>
---
 corelib/lua_interface.c | 36 ++++++++++++++++++++++++++++++++++++
 include/lua_util.h      | 17 +++++++++++++++++
 2 files changed, 53 insertions(+)

--
2.34.1
diff mbox series

Patch

diff --git a/corelib/lua_interface.c b/corelib/lua_interface.c
index d785cdb5..fbdb7d05 100644
--- a/corelib/lua_interface.c
+++ b/corelib/lua_interface.c
@@ -1597,3 +1597,39 @@  int lua_parser_fn(lua_State *L, const char *fcn, struct img_type *img)

 	return ret;
 }
+
+int lua_handler_fn(lua_State *L, const char *fcn, const char *parms)
+{
+	int ret = -1;
+
+	lua_getglobal(L, fcn);
+	if(!lua_isfunction(L, lua_gettop(L))) {
+		lua_pop(L, 1);
+		TRACE("Script : no %s in script, exiting", fcn);
+		return -1;
+	}
+	TRACE("Prepared to run %s", fcn);
+
+	/*
+	 * passing arguments
+	 */
+	lua_pushstring(L, parms);
+
+	ret = lua_pcall(L, 1, 1, 0);
+	if (ret || !lua_isinteger(L, -1)) {
+		LUAstackDump(L);
+		ERROR("ERROR Calling Lua %s", fcn);
+		return -1;
+	}
+
+	if (loglevel >= DEBUGLEVEL)
+		LUAstackDump(L);
+
+	ret = lua_tointeger(L, -1);
+
+	lua_pop(L, 1); /* clear stack */
+
+	TRACE("Script returns %d", ret);
+
+	return ret;
+}
diff --git a/include/lua_util.h b/include/lua_util.h
index bf8d2b0e..67c5c47a 100644
--- a/include/lua_util.h
+++ b/include/lua_util.h
@@ -24,6 +24,7 @@  int run_lua_script(lua_State *L, const char *script, bool load, const char *func
 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_handler_fn(lua_State *L, const char *fcn, const char *parms);
 int lua_handlers_init(void);

 int lua_notify_trace(lua_State *L);
@@ -37,11 +38,24 @@  int lua_get_swupdate_version(lua_State *L);

 #define lua_exit(L) lua_close((lua_State *)L)

+#if !defined(lua_isinteger)
+static inline int lua_isinteger (lua_State *L, int index) {
+  if (lua_type(L, index) == LUA_TNUMBER) {
+    lua_Number n = lua_tonumber(L, index);
+    lua_Integer i = lua_tointeger(L, index);
+    if (i == n)
+      return 1;
+  }
+  return 0;
+}
+#endif
+
 #if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM == 501
 #define LUA_OK 0
 #if !defined(luaL_newlib)
 #define luaL_newlib(L, l) (lua_newtable((L)),luaL_setfuncs((L), (l), 0))
 #endif
+
 void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup);
 void luaL_requiref(lua_State *L, char const* modname, lua_CFunction openf, int glb);

@@ -92,5 +106,8 @@  static inline int lua_load_buffer(lua_State __attribute__ ((__unused__)) *L,
 static inline int lua_parser_fn(lua_State __attribute__ ((__unused__)) *L,
 			 const char __attribute__ ((__unused__)) *fcn,
 			 struct img_type __attribute__ ((__unused__)) *img) { return -1; }
+static inline int lua_handler_fn(lua_State __attribute__ ((__unused__)) *L,
+			 const char __attribute__ ((__unused__)) *fcn,
+			 const char __attribute__ ((__unused__)) *parms) { return -1; }
 static inline int lua_handlers_init(void) { return 0; }
 #endif