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 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 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 +.\" +.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 + */ + +#include +#include +#include +#include +#include + +#include +#include +#include + +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); +}