git: cde4ab289dfc - stable/14 - flua: Add wrappers for sys/utsname.h

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Thu, 10 Oct 2024 20:28:28 UTC
The branch stable/14 has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=cde4ab289dfc3c802907fa19df166d18b85f59b8

commit cde4ab289dfc3c802907fa19df166d18b85f59b8
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2024-09-05 15:16:29 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2024-10-10 20:28:20 +0000

    flua: Add wrappers for sys/utsname.h
    
    This allows one to invoke uname from lua scripts.
    
    Reviewed by:    bapt, kevans, emaste
    MFC after:      1 month
    Differential Revision:  https://reviews.freebsd.org/D42017
    
    (cherry picked from commit 1726db7af6b3738eb04d962b351d7f4017e1fc77)
---
 libexec/flua/linit_flua.c     |  1 +
 libexec/flua/modules/lposix.c | 47 ++++++++++++++++++++++++++++++++++++++++++-
 libexec/flua/modules/lposix.h |  1 +
 3 files changed, 48 insertions(+), 1 deletion(-)

diff --git a/libexec/flua/linit_flua.c b/libexec/flua/linit_flua.c
index 671a0300783c..6bc300095c27 100644
--- a/libexec/flua/linit_flua.c
+++ b/libexec/flua/linit_flua.c
@@ -58,6 +58,7 @@ static const luaL_Reg loadedlibs[] = {
   /* FreeBSD Extensions */
   {"lfs", luaopen_lfs},
   {"posix.sys.stat", luaopen_posix_sys_stat},
+  {"posix.sys.utsname", luaopen_posix_sys_utsname},
   {"posix.unistd", luaopen_posix_unistd},
   {"ucl", luaopen_ucl},
   {NULL, NULL}
diff --git a/libexec/flua/modules/lposix.c b/libexec/flua/modules/lposix.c
index 5b6e80a0309f..fa3fd5f8e589 100644
--- a/libexec/flua/modules/lposix.c
+++ b/libexec/flua/modules/lposix.c
@@ -24,8 +24,8 @@
  *
  */
 
-#include <sys/cdefs.h>
 #include <sys/stat.h>
+#include <sys/utsname.h>
 
 #include <errno.h>
 #include <grp.h>
@@ -130,12 +130,50 @@ lua_getpid(lua_State *L)
 	return 1;
 }
 
+static int
+lua_uname(lua_State *L)
+{
+	struct utsname name;
+	int error, n;
+
+	n = lua_gettop(L);
+	luaL_argcheck(L, n == 0, 1, "too many arguments");
+
+	error = uname(&name);
+	if (error != 0) {
+		error = errno;
+		lua_pushnil(L);
+		lua_pushstring(L, strerror(error));
+		lua_pushinteger(L, error);
+		return (3);
+	}
+
+	lua_newtable(L);
+#define	setkv(f) do {			\
+	lua_pushstring(L, name.f);	\
+	lua_setfield(L, -2, #f);	\
+} while (0)
+	setkv(sysname);
+	setkv(nodename);
+	setkv(release);
+	setkv(version);
+	setkv(machine);
+#undef setkv
+
+	return (1);
+}
+
 #define REG_SIMPLE(n)	{ #n, lua_ ## n }
 static const struct luaL_Reg sys_statlib[] = {
 	REG_SIMPLE(chmod),
 	{ NULL, NULL },
 };
 
+static const struct luaL_Reg sys_utsnamelib[] = {
+	REG_SIMPLE(uname),
+	{ NULL, NULL },
+};
+
 static const struct luaL_Reg unistdlib[] = {
 	REG_SIMPLE(getpid),
 	REG_SIMPLE(chown),
@@ -150,6 +188,13 @@ luaopen_posix_sys_stat(lua_State *L)
 	return 1;
 }
 
+int
+luaopen_posix_sys_utsname(lua_State *L)
+{
+	luaL_newlib(L, sys_utsnamelib);
+	return 1;
+}
+
 int
 luaopen_posix_unistd(lua_State *L)
 {
diff --git a/libexec/flua/modules/lposix.h b/libexec/flua/modules/lposix.h
index 6085bf045d79..e37caaae9d04 100644
--- a/libexec/flua/modules/lposix.h
+++ b/libexec/flua/modules/lposix.h
@@ -8,4 +8,5 @@
 #include <lua.h>
 
 int luaopen_posix_sys_stat(lua_State *L);
+int luaopen_posix_sys_utsname(lua_State *L);
 int luaopen_posix_unistd(lua_State *L);