aboutsummaryrefslogtreecommitdiff
path: root/libexec/flua/libfreebsd
diff options
context:
space:
mode:
Diffstat (limited to 'libexec/flua/libfreebsd')
-rw-r--r--libexec/flua/libfreebsd/Makefile4
-rw-r--r--libexec/flua/libfreebsd/Makefile.inc3
-rw-r--r--libexec/flua/libfreebsd/kenv/Makefile5
-rw-r--r--libexec/flua/libfreebsd/kenv/Makefile.inc2
-rw-r--r--libexec/flua/libfreebsd/kenv/freebsd.kenv.3lua44
-rw-r--r--libexec/flua/libfreebsd/kenv/kenv.c100
-rw-r--r--libexec/flua/libfreebsd/sys/Makefile4
-rw-r--r--libexec/flua/libfreebsd/sys/Makefile.inc3
-rw-r--r--libexec/flua/libfreebsd/sys/linker/Makefile6
-rw-r--r--libexec/flua/libfreebsd/sys/linker/Makefile.inc2
-rw-r--r--libexec/flua/libfreebsd/sys/linker/freebsd.sys.linker.3lua46
-rw-r--r--libexec/flua/libfreebsd/sys/linker/linker.c86
12 files changed, 305 insertions, 0 deletions
diff --git a/libexec/flua/libfreebsd/Makefile b/libexec/flua/libfreebsd/Makefile
new file mode 100644
index 000000000000..36d39d6b0502
--- /dev/null
+++ b/libexec/flua/libfreebsd/Makefile
@@ -0,0 +1,4 @@
+SUBDIR+= kenv
+SUBDIR+= sys
+
+.include <bsd.subdir.mk>
diff --git a/libexec/flua/libfreebsd/Makefile.inc b/libexec/flua/libfreebsd/Makefile.inc
new file mode 100644
index 000000000000..26a1540482c7
--- /dev/null
+++ b/libexec/flua/libfreebsd/Makefile.inc
@@ -0,0 +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..a1b388bb3612
--- /dev/null
+++ b/libexec/flua/libfreebsd/kenv/Makefile
@@ -0,0 +1,5 @@
+SHLIB_NAME= kenv.so
+MAN= freebsd.kenv.3lua
+
+.include "Makefile.inc"
+.include <bsd.lib.mk>
diff --git a/libexec/flua/libfreebsd/kenv/Makefile.inc b/libexec/flua/libfreebsd/kenv/Makefile.inc
new file mode 100644
index 000000000000..05819c5280d9
--- /dev/null
+++ b/libexec/flua/libfreebsd/kenv/Makefile.inc
@@ -0,0 +1,2 @@
+.PATH: ${.PARSEDIR}
+SRCS+= kenv.c
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 <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
diff --git a/libexec/flua/libfreebsd/kenv/kenv.c b/libexec/flua/libfreebsd/kenv/kenv.c
new file mode 100644
index 000000000000..56b24c72904a
--- /dev/null
+++ b/libexec/flua/libfreebsd/kenv/kenv.c
@@ -0,0 +1,100 @@
+/*-
+ * 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>
+
+#include "bootstrap.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);
+}
+
+FLUA_MODULE_NAMED(freebsd_kenv, "freebsd.kenv");
diff --git a/libexec/flua/libfreebsd/sys/Makefile b/libexec/flua/libfreebsd/sys/Makefile
new file mode 100644
index 000000000000..9f38294536f2
--- /dev/null
+++ b/libexec/flua/libfreebsd/sys/Makefile
@@ -0,0 +1,4 @@
+SUBDIR+= linker
+
+.include <bsd.subdir.mk>
+
diff --git a/libexec/flua/libfreebsd/sys/Makefile.inc b/libexec/flua/libfreebsd/sys/Makefile.inc
new file mode 100644
index 000000000000..ca2630721733
--- /dev/null
+++ b/libexec/flua/libfreebsd/sys/Makefile.inc
@@ -0,0 +1,3 @@
+SHLIBDIR?= ${LIBDIR}/flua/freebsd/sys
+
+.include "../Makefile.inc"
diff --git a/libexec/flua/libfreebsd/sys/linker/Makefile b/libexec/flua/libfreebsd/sys/linker/Makefile
new file mode 100644
index 000000000000..f1f65ad5f6c1
--- /dev/null
+++ b/libexec/flua/libfreebsd/sys/linker/Makefile
@@ -0,0 +1,6 @@
+SHLIB_NAME= linker.so
+
+MAN= freebsd.sys.linker.3lua
+
+.include "Makefile.inc"
+.include <bsd.lib.mk>
diff --git a/libexec/flua/libfreebsd/sys/linker/Makefile.inc b/libexec/flua/libfreebsd/sys/linker/Makefile.inc
new file mode 100644
index 000000000000..da65c0070170
--- /dev/null
+++ b/libexec/flua/libfreebsd/sys/linker/Makefile.inc
@@ -0,0 +1,2 @@
+.PATH: ${.PARSEDIR}
+SRCS+= linker.c
diff --git a/libexec/flua/libfreebsd/sys/linker/freebsd.sys.linker.3lua b/libexec/flua/libfreebsd/sys/linker/freebsd.sys.linker.3lua
new file mode 100644
index 000000000000..34198d20463e
--- /dev/null
+++ b/libexec/flua/libfreebsd/sys/linker/freebsd.sys.linker.3lua
@@ -0,0 +1,46 @@
+.\"
+.\" SPDX-License-Identifier: BSD-2-Clause
+.\"
+.\" Copyright (c) 2024, Baptiste Daroussin <bapt@FreeBSD.org>
+.\"
+.Dd September 6, 2024
+.Dt FREEBSD.SYS.LINKER 3lua
+.Os
+.Sh NAME
+.Nm freebsd.sys.linker
+.Nd Lua binding to
+.Fx 's
+Linker functions
+.Sh SYNOPSIS
+.Bd -literal
+local linker = require('freebsd.sys.linker')
+.Ed
+.Pp
+.Bl -tag -width XXXX -compact
+.It Dv fileid, err, errno = linker.kldload(name)
+.It Dv ok, err, errno = linker.kldunload(fileid|name)
+.El
+.Sh DESCRIPTION
+The
+.Nm
+module is a binding to the
+.Fx 's
+linker functions.
+List of functions:
+.Bl -tag -width XXXX
+.It Dv fileid, err = freebsd.sys.linker.kldload(name)
+Load the kernel module named
+.Fa name
+and return the identifier
+.Pq fileid
+as an interger.
+.It Dv ok, err, errno = freebsd.sys.linker.kldunload(fileid|name)
+Unload the kernel module identifier either by
+.Fa name
+as a string, or
+.Fa fileid
+as an integer.
+.El
+.Sh SEE ALSO
+.Xr kldload 2 ,
+.Xr kldunload 2
diff --git a/libexec/flua/libfreebsd/sys/linker/linker.c b/libexec/flua/libfreebsd/sys/linker/linker.c
new file mode 100644
index 000000000000..c78fbb2b39d2
--- /dev/null
+++ b/libexec/flua/libfreebsd/sys/linker/linker.c
@@ -0,0 +1,86 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2024, Baptiste Daroussin <bapt@FreeBSD.org>
+ */
+
+#include <sys/param.h>
+#include <sys/linker.h>
+
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+
+#include <lua.h>
+#include <lualib.h>
+#include <lauxlib.h>
+
+#include "bootstrap.h"
+
+int luaopen_freebsd_sys_linker(lua_State *L);
+
+static int
+lua_kldload(lua_State *L)
+{
+ const char *name;
+ int ret;
+
+ name = luaL_checkstring(L, 1);
+ ret = kldload(name);
+ if (ret == -1) {
+ lua_pushnil(L);
+ lua_pushstring(L, strerror(errno));
+ lua_pushinteger(L, errno);
+ return (3);
+ }
+ lua_pushinteger(L, ret);
+ return (1);
+}
+
+static int
+lua_kldunload(lua_State *L)
+{
+ const char *name;
+ int ret, fileid;
+
+ if (lua_isinteger(L, 1)) {
+ fileid = lua_tointeger(L, 1);
+ } else {
+ name = luaL_checkstring(L, 1);
+ fileid = kldfind(name);
+ }
+ if (fileid == -1) {
+ lua_pushnil(L);
+ lua_pushstring(L, strerror(errno));
+ lua_pushinteger(L, errno);
+ return (3);
+ }
+ ret = kldunload(fileid);
+ lua_pushinteger(L, ret);
+ if (ret == -1) {
+ lua_pushnil(L);
+ lua_pushstring(L, strerror(errno));
+ lua_pushinteger(L, errno);
+ return (3);
+ }
+ lua_pushinteger(L, 0);
+ return (1);
+}
+
+#define REG_SIMPLE(n) { #n, lua_ ## n }
+static const struct luaL_Reg freebsd_sys_linker[] = {
+ REG_SIMPLE(kldload),
+ REG_SIMPLE(kldunload),
+ { NULL, NULL },
+};
+#undef REG_SIMPLE
+
+int
+luaopen_freebsd_sys_linker(lua_State *L)
+{
+ luaL_newlib(L, freebsd_sys_linker);
+
+ return (1);
+}
+
+FLUA_MODULE_NAMED(freebsd_sys_linker, "freebsd.sys.linker");