git: 1726db7af6b3 - main - flua: Add wrappers for sys/utsname.h
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Thu, 05 Sep 2024 15:54:20 UTC
The branch main has been updated by markj:
URL: https://cgit.FreeBSD.org/src/commit/?id=1726db7af6b3738eb04d962b351d7f4017e1fc77
commit 1726db7af6b3738eb04d962b351d7f4017e1fc77
Author: Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2024-09-05 15:16:29 +0000
Commit: Mark Johnston <markj@FreeBSD.org>
CommitDate: 2024-09-05 15:53:52 +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
---
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 4d4d69920e94..1b7d83016cfe 100644
--- a/libexec/flua/linit_flua.c
+++ b/libexec/flua/linit_flua.c
@@ -59,6 +59,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},
{"fbsd", luaopen_fbsd},
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);