The branch main has been updated by bapt:

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

commit 696922fbfa3e1c400701a3a39dcadf7dee12b06c
Author:     Baptiste Daroussin <b...@freebsd.org>
AuthorDate: 2024-09-12 12:42:01 +0000
Commit:     Baptiste Daroussin <b...@freebsd.org>
CommitDate: 2024-09-14 05:43:50 +0000

    flua: add freebsd.kenv, kenv(2) bindings
    
    Add bindings for kenv(2) right now only get() has been created
    it allows do dump into a key/value table the kernel environement if
    no argument is passed, or it returns the value associated to the
    provided key.
    
    Reviewed by:    imp, kevans, markj
    Accepted by:    imp, kevans
    Differential Revision:  https://reviews.freebsd.org/D46654
---
 libexec/flua/libfreebsd/Makefile               |  1 +
 libexec/flua/libfreebsd/Makefile.inc           |  2 +
 libexec/flua/libfreebsd/kenv/Makefile          |  5 ++
 libexec/flua/libfreebsd/kenv/freebsd.kenv.3lua | 44 ++++++++++++
 libexec/flua/libfreebsd/kenv/kenv.c            | 96 ++++++++++++++++++++++++++
 5 files changed, 148 insertions(+)

diff --git a/libexec/flua/libfreebsd/Makefile b/libexec/flua/libfreebsd/Makefile
index 6ed0451055ff..36d39d6b0502 100644
--- a/libexec/flua/libfreebsd/Makefile
+++ b/libexec/flua/libfreebsd/Makefile
@@ -1,3 +1,4 @@
+SUBDIR+=       kenv
 SUBDIR+=       sys
 
 .include <bsd.subdir.mk>
diff --git a/libexec/flua/libfreebsd/Makefile.inc 
b/libexec/flua/libfreebsd/Makefile.inc
index 01b5f23410c8..26a1540482c7 100644
--- a/libexec/flua/libfreebsd/Makefile.inc
+++ b/libexec/flua/libfreebsd/Makefile.inc
@@ -1 +1,3 @@
+SHLIBDIR?=     ${LIBDIR}/flua/freebsd
+
 .include "../Makefile.inc"
diff --git a/libexec/flua/libfreebsd/kenv/Makefile 
b/libexec/flua/libfreebsd/kenv/Makefile
new file mode 100644
index 000000000000..1726c892c515
--- /dev/null
+++ b/libexec/flua/libfreebsd/kenv/Makefile
@@ -0,0 +1,5 @@
+SHLIB_NAME=    kenv.so
+SRCS+=         kenv.c
+MAN=           freebsd.kenv.3lua
+
+.include <bsd.lib.mk>
diff --git a/libexec/flua/libfreebsd/kenv/freebsd.kenv.3lua 
b/libexec/flua/libfreebsd/kenv/freebsd.kenv.3lua
new file mode 100644
index 000000000000..d254dd22c91c
--- /dev/null
+++ b/libexec/flua/libfreebsd/kenv/freebsd.kenv.3lua
@@ -0,0 +1,44 @@
+.\"
+.\" SPDX-License-Identifier: BSD-2-Clause
+.\"
+.\" Copyright (c) 2024, Baptiste Daroussin <b...@freebsd.org>
+.\"
+.Dd September 6, 2024
+.Dt FREEBSD.KENV 3lua
+.Os
+.Sh NAME
+.Nm freebsd.kenv
+.Nd Lua binding to
+.Fx 's
+Linker functions
+.Sh SYNOPSIS
+.Bd -literal
+local kenv = require('freebsd.kenv')
+.Ed
+.Pp
+.Bl -tag -width XXXX -compact
+.It Dv table = kenv.get()
+.It Dv value = kenv.get(key)
+.El
+.Sh DESCRIPTION
+The
+.Nm
+module is a binding to the
+.Xr kenv 2
+function.
+.Pp
+List of functions:
+.Bl -tag -width XXXX
+.It Dv table = freebsd.kenv.get()
+Dump the kernel environnement into a key/value
+.Fa table .
+.It Dv value = freebsd.kenv.get(key)
+Return the
+.Fa value
+associated to the
+.Fa key ,
+if it exists, or
+.Va nil
+otherwise.
+.Sh SEE ALSO
+.Xr kenv 2
diff --git a/libexec/flua/libfreebsd/kenv/kenv.c 
b/libexec/flua/libfreebsd/kenv/kenv.c
new file mode 100644
index 000000000000..954baa00facb
--- /dev/null
+++ b/libexec/flua/libfreebsd/kenv/kenv.c
@@ -0,0 +1,96 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2024, Baptiste Daroussin <b...@freebsd.org>
+ */
+
+#include <errno.h>
+#include <kenv.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <lua.h>
+#include <lualib.h>
+#include <lauxlib.h>
+
+int luaopen_freebsd_kenv(lua_State *L);
+
+static int
+lua_kenv_get(lua_State *L)
+{
+       const char *env;
+       int ret, n;
+       char value[1024];
+
+       n = lua_gettop(L);
+       if (n == 0) {
+               char *buf, *bp, *cp;
+               int size;
+
+               size = kenv(KENV_DUMP, NULL, NULL, 0);
+               if (size < 0) {
+                       lua_pushnil(L);
+                       lua_pushstring(L, strerror(errno));
+                       lua_pushinteger(L, errno);
+                       return (3);
+               }
+               size += 1;
+               buf = malloc(size);
+               if (buf == NULL) {
+                       lua_pushnil(L);
+                       lua_pushstring(L, strerror(errno));
+                       lua_pushinteger(L, errno);
+                       return (3);
+               }
+               if (kenv(KENV_DUMP, NULL, buf, size) < 0) {
+                       free(buf);
+                       lua_pushnil(L);
+                       lua_pushstring(L, strerror(errno));
+                       lua_pushinteger(L, errno);
+                       return (3);
+               }
+
+               lua_newtable(L);
+               for (bp = buf; *bp != '\0'; bp += strlen(bp) + 1) {
+                       cp = strchr(bp, '=');
+                       if (cp == NULL)
+                               continue;
+                       *cp++ = '\0';
+                       lua_pushstring(L, cp);
+                       lua_setfield(L, -2, bp);
+                       bp = cp;
+               }
+               free(buf);
+               return (1);
+       }
+       env = luaL_checkstring(L, 1);
+       ret = kenv(KENV_GET, env, value, sizeof(value));
+       if (ret == -1) {
+               if (errno == ENOENT) {
+                       lua_pushnil(L);
+                       return (1);
+               }
+               lua_pushnil(L);
+               lua_pushstring(L, strerror(errno));
+               lua_pushinteger(L, errno);
+               return (3);
+       }
+       lua_pushstring(L, value);
+       return (1);
+}
+
+#define REG_SIMPLE(n)  { #n, lua_kenv_ ## n }
+static const struct luaL_Reg freebsd_kenv[] = {
+       REG_SIMPLE(get),
+       { NULL, NULL },
+};
+#undef REG_SIMPLE
+
+int
+luaopen_freebsd_kenv(lua_State *L)
+{
+       luaL_newlib(L, freebsd_kenv);
+
+       return (1);
+}

Reply via email to