From nobody Sat Sep 14 05:46:54 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4X5Ktk4TClz5VHpC; Sat, 14 Sep 2024 05:46:54 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4X5Ktk3r13z4TRB; Sat, 14 Sep 2024 05:46:54 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1726292814; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=CkF07cdLwl2Ul/s4UMmyVy2T9oSiDNp2w4Q8h1eSZ3o=; b=dsniVagPmDYYHuw2ctxblJCzRyNw6V0/CAJ7yPDbvW33gwJjzj7GdReeYW4bTXKamu9LvU GAdLdsVtCA8nnce1vk2fm+SOSsi/fOdyQtaP9bGVnIl9YRFPqYM51hd4or4BjFiouX+tqm bfZQA6m1ENvxUgU8xfKuQ6SgxfWbUe21xngzgs5dsL9aYCCYW/kmqFexNRA1wuofCcvSCs MrfdNWLY4SlIaF5264G6KfaXbEiPnlRxfIKMryuEYQyg+lXbW2uEx9KTUieNLtMjkM3FYi C6WvWc89v6LWGo+bEKq+ndP0Ch7MhA+EW55O8YNkHa2lK3Zp25Z/VxCTuuOPkQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1726292814; a=rsa-sha256; cv=none; b=TqeEka07JFNYxqgw9/BR/eK/QCpF+hIpxMewBpemerwUjucXNv51tvoHkWXEPgYs+2rdxm jfe7trMwOmMOdEdeEav0kGdjaPjN97OKvuFpxq0dbPSTX1lFWXqvRnoyctcUghfbMMBpQr ZtRZJ8zp+M/TLvtdyLvVokKsERasQdPySHLr/zUiGQZRiQ462CHoV+FEUHkKmOkf8fKByw MQqjlpLIi/xwE7SJRB52veRzt3N+lo/HiCXrdbkcjMVZKv+ElaBF+ZujC19yIk5C5l0rtf d/lvxmGyTPFMnACJqL5vluVNB/JmzjGz1NPYinnfngqxj6GyptJTkpp7ss11pQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1726292814; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=CkF07cdLwl2Ul/s4UMmyVy2T9oSiDNp2w4Q8h1eSZ3o=; b=IzcqJ0KWILu5955ig8j3ROhCgUFQ+Umv994ADvWNxbFdIJlrT9EKQx16PzAlZcr1odqrF/ BhBAb/NYkNpKqzjbBE1/w6hRwL7FCCL9pZyr3BpGrDkyc5rfnG7dd/rOvzFHdxJIpFIu7M mmh8dEqxlgpZzfFbKzVOymoljqRQYKxKelhQVxiRPhHMufJK5HQekwpcIWa+q4ldyUVQ0J Ja406fXPaOJsUK19SwyxxIyf8FLW6P1cwpB4ZVUEchdt7OaZiMRUoWDvNjy7fPmuWfLrLI k88ONJE6xe6MsehbClsouJaiLiSJvNy1jxKcmxFcXgRv8UKMCUIutA3o3AtEHg== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4X5Ktk3JkSzFpH; Sat, 14 Sep 2024 05:46:54 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 48E5ksPr045509; Sat, 14 Sep 2024 05:46:54 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 48E5ksQo045506; Sat, 14 Sep 2024 05:46:54 GMT (envelope-from git) Date: Sat, 14 Sep 2024 05:46:54 GMT Message-Id: <202409140546.48E5ksQo045506@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Baptiste Daroussin Subject: git: 696922fbfa3e - main - flua: add freebsd.kenv, kenv(2) bindings List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: bapt X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 696922fbfa3e1c400701a3a39dcadf7dee12b06c Auto-Submitted: auto-generated The branch main has been updated by bapt: URL: https://cgit.FreeBSD.org/src/commit/?id=696922fbfa3e1c400701a3a39dcadf7dee12b06c commit 696922fbfa3e1c400701a3a39dcadf7dee12b06c Author: Baptiste Daroussin AuthorDate: 2024-09-12 12:42:01 +0000 Commit: Baptiste Daroussin 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 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); +}