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
This commit is contained in:
Baptiste Daroussin 2024-09-12 14:42:01 +02:00
parent 50a03971d3
commit 696922fbfa
5 changed files with 148 additions and 0 deletions

View File

@ -1,3 +1,4 @@
SUBDIR+= kenv
SUBDIR+= sys
.include <bsd.subdir.mk>

View File

@ -1 +1,3 @@
SHLIBDIR?= ${LIBDIR}/flua/freebsd
.include "../Makefile.inc"

View File

@ -0,0 +1,5 @@
SHLIB_NAME= kenv.so
SRCS+= kenv.c
MAN= freebsd.kenv.3lua
.include <bsd.lib.mk>

View File

@ -0,0 +1,44 @@
.\"
.\" SPDX-License-Identifier: BSD-2-Clause
.\"
.\" Copyright (c) 2024, Baptiste Daroussin <bapt@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

View File

@ -0,0 +1,96 @@
/*-
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2024, Baptiste Daroussin <bapt@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);
}