diff --git a/libexec/flua/libfreebsd/Makefile b/libexec/flua/libfreebsd/Makefile --- 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 --- 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 --- /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 --- /dev/null +++ b/libexec/flua/libfreebsd/kenv/freebsd.kenv.3lua @@ -0,0 +1,65 @@ +.\" +.\" SPDX-License-Identifier: BSD-2-Clause +.\" +.\" Copyright (c) 2024, Baptiste Daroussin +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.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 --- /dev/null +++ b/libexec/flua/libfreebsd/kenv/kenv.c @@ -0,0 +1,110 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2024, Baptiste Daroussin + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#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; + + 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); + } + char buf[1024]; + env = luaL_checkstring(L, 1); + ret = kenv(KENV_GET, env, buf, sizeof(buf)); + if (ret == -1) { + lua_pushnil(L); + return (1); + } + lua_pushstring(L, buf); + 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); +}