From nobody Thu Sep 11 18:50:20 2025 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 4cN67c45rWz67Hng; Thu, 11 Sep 2025 18:50:20 +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 "R12" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4cN67c3Nkzz3gwg; Thu, 11 Sep 2025 18:50:20 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1757616620; 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=b975pkbo6Sj1sWkDesGN2IWelvSUvy2ADhy+EKCQXw8=; b=dOVRtiMPRXqaPE8AHIoHZ8x/m1czK1QhIvQc3osXpXczYpz4cBa10CwolWO2jP5FuGU5jS GBJN9jfbrD57CvcNakiIEWF1fW0FnSvCKddcns1S4jIVz+1rzYfgcocn4IdOWr/KuCMpLY ERObdhLEa/gzWjCheD6547sACWjm+rXYry1/xhjZINAmNy5oAzKAn/8ITzBLFJgW214ZFV K+B6PA2Pzu8i30y8YuoBqUPM2kZSiTwctlbV7iI7pZLf8UH5aq+Ng+Ex15BPUk+/rWOwHb StgGgDMH4PO/bKsSr9Pqs758pjUimFeOXMlwRL+7YEgpISjwGA/+yU5gDg1Ieg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1757616620; 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=b975pkbo6Sj1sWkDesGN2IWelvSUvy2ADhy+EKCQXw8=; b=dGmyc4jW/H2ahWSXL3Vk5MawCblCBzFkM4kiSA/tMKlri1LwOV3VqrN2AXsUFq9cmuJtGu RuCniJs9go8N66fMirtXMb23yUffl8rHXOkg5+iRjM+Y9s6jc9aMXdyUFwk+YHNZrjouQx +gEq9BFCco/nXklTRcPKAI37+IjHPz5J2cQMymbq18UKmKTvPUR0m60ijJnHIuzQEXHBI1 voX0QXhoFvZ0vBbJRj4KjrYpveKRt+BxHFC6YXXoga53V/VdUjgGXluNPJVWa7TdERqFn4 V/veRPx/i+hP4Hu/xsysTMhFR2OmbCeR3a6ghcjfvdYSriJ/oOq6GVaROOdwdw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1757616620; a=rsa-sha256; cv=none; b=x5QffyS77JxpUxxyWXEbcg1dDBacv8MueeVm51iwYtP6l0h1zCgRM524ifLPW8ex2Gb4gL ch/9PneeZCEZ2pyhmGBBPwEwyHX4+R05K3Vyp5ixfAAx688bzY+++WbdCeB2Uc26Y1TBGg MCVtnZIn2CRAGxMB+BcW4TOS/WhVjJm3+uYJfkD2YmgHMPB79OItfz0nNDbQ2DSR3Zfpi7 WcsLB/di9WxlhxTsPFxBBaSs2MUD9+QGp1H/g/S5LnKTbQdkcAvTY+PSRwwEUrd856gitF qEAEXHgLL3gg3LL+yEWxpguTUM1l3tWWZLm516t2ed7AOLm3PO7lbl9F9r4OTA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none 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 4cN67c2rYfz4BQ; Thu, 11 Sep 2025 18:50:20 +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 58BIoK5b092834; Thu, 11 Sep 2025 18:50:20 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 58BIoKsF092831; Thu, 11 Sep 2025 18:50:20 GMT (envelope-from git) Date: Thu, 11 Sep 2025 18:50:20 GMT Message-Id: <202509111850.58BIoKsF092831@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Ed Maste Subject: git: c406a288e1e5 - stable/14 - flua: add fbsd module 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: emaste X-Git-Repository: src X-Git-Refname: refs/heads/stable/14 X-Git-Reftype: branch X-Git-Commit: c406a288e1e5083119d3b728c083b129a3ea104f Auto-Submitted: auto-generated The branch stable/14 has been updated by emaste: URL: https://cgit.FreeBSD.org/src/commit/?id=c406a288e1e5083119d3b728c083b129a3ea104f commit c406a288e1e5083119d3b728c083b129a3ea104f Author: Baptiste Daroussin AuthorDate: 2023-09-13 07:43:33 +0000 Commit: Ed Maste CommitDate: 2025-09-11 15:09:58 +0000 flua: add fbsd module This module is bundled into flua, it only provides for now the exec function. The point of the function is to be able to execute a program without actually executing a shell. to use it: fbsd.exec({"id", "bapt"}) Reviewed by: manu Differential Revision: https://reviews.freebsd.org/D41840 (cherry picked from commit 1f31e00e19f9e24d4c891a24973e08a027c4f71c) --- libexec/flua/Makefile | 2 +- libexec/flua/linit_flua.c | 2 + libexec/flua/modules/lfbsd.c | 134 +++++++++++++++++++++++++++++++++++++++++++ libexec/flua/modules/lfbsd.h | 32 +++++++++++ 4 files changed, 169 insertions(+), 1 deletion(-) diff --git a/libexec/flua/Makefile b/libexec/flua/Makefile index 2cfa8649fc01..8296e5ee00b7 100644 --- a/libexec/flua/Makefile +++ b/libexec/flua/Makefile @@ -23,7 +23,7 @@ SRCS+= lua.c # FreeBSD Extensions .PATH: ${.CURDIR}/modules SRCS+= linit_flua.c -SRCS+= lfs.c lposix.c +SRCS+= lfs.c lposix.c lfbsd.c CFLAGS+= -I${SRCTOP}/lib/liblua -I${.CURDIR}/modules -I${LUASRC} CFLAGS+= -DLUA_PROGNAME="\"${PROG}\"" diff --git a/libexec/flua/linit_flua.c b/libexec/flua/linit_flua.c index 9d712066485a..8eaa4af1ffca 100644 --- a/libexec/flua/linit_flua.c +++ b/libexec/flua/linit_flua.c @@ -35,6 +35,7 @@ #include "lauxlib.h" #include "lfs.h" #include "lposix.h" +#include "lfbsd.h" /* ** these libs are loaded by lua.c and are readily available to any Lua @@ -63,6 +64,7 @@ static const luaL_Reg loadedlibs[] = { {"posix.sys.utsname", luaopen_posix_sys_utsname}, {"posix.sys.wait", luaopen_posix_sys_wait}, {"posix.unistd", luaopen_posix_unistd}, + {"fbsd", luaopen_fbsd}, {NULL, NULL} }; diff --git a/libexec/flua/modules/lfbsd.c b/libexec/flua/modules/lfbsd.c new file mode 100644 index 000000000000..30cafcc7309e --- /dev/null +++ b/libexec/flua/modules/lfbsd.c @@ -0,0 +1,134 @@ +/* + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright 2023 Baptiste Daroussin + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted providing 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 ``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 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 +#include "lauxlib.h" +#include "lfbsd.h" + +extern char **environ; + +static const char** +luaL_checkarraystrings(lua_State *L, int arg) +{ + const char **ret; + lua_Integer n, i; + int t; + int abs_arg = lua_absindex(L, arg); + luaL_checktype(L, abs_arg, LUA_TTABLE); + n = lua_rawlen(L, abs_arg); + ret = lua_newuserdata(L, (n+1)*sizeof(char*)); + for (i=0; i 1 ? 2 : n, + "fbsd.exec takes exactly one argument"); + + if (pipe(stdin_pipe) < 0) { + lua_pushnil(L); + lua_pushstring(L, strerror(errno)); + lua_pushinteger(L, errno); + return (3); + } + + posix_spawn_file_actions_init(&action); + posix_spawn_file_actions_adddup2(&action, stdin_pipe[0], STDIN_FILENO); + posix_spawn_file_actions_addclose(&action, stdin_pipe[1]); + + argv = luaL_checkarraystrings(L, 1); + if (0 != (r = posix_spawnp(&pid, argv[0], &action, NULL, + (char*const*)argv, environ))) { + lua_pushnil(L); + lua_pushstring(L, strerror(r)); + lua_pushinteger(L, r); + return (3); + } + while (waitpid(pid, &pstat, 0) == -1) { + if (errno != EINTR) { + lua_pushnil(L); + lua_pushstring(L, strerror(r)); + lua_pushinteger(L, r); + return (3); + } + } + + if (WEXITSTATUS(pstat) != 0) { + lua_pushnil(L); + lua_pushstring(L, "Abnormal termination"); + lua_pushinteger(L, r); + return (3); + } + + posix_spawn_file_actions_destroy(&action); + + if (stdin_pipe[0] != -1) + close(stdin_pipe[0]); + if (stdin_pipe[1] != -1) + close(stdin_pipe[1]); + lua_pushinteger(L, pid); + return 1; +} + +#define REG_SIMPLE(n) { #n, lua_ ## n } +static const struct luaL_Reg fbsd_lib[] = { + REG_SIMPLE(exec), + { NULL, NULL }, +}; +#undef REG_SIMPLE + +int +luaopen_fbsd(lua_State *L) +{ + luaL_newlib(L, fbsd_lib); + return (1); +} diff --git a/libexec/flua/modules/lfbsd.h b/libexec/flua/modules/lfbsd.h new file mode 100644 index 000000000000..01034a3ad7cd --- /dev/null +++ b/libexec/flua/modules/lfbsd.h @@ -0,0 +1,32 @@ +/* + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright 2023 Baptiste Daroussin + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted providing 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 ``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 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. + */ + +#pragma once + +#include + +int luaopen_fbsd(lua_State *L);