From nobody Wed Sep 27 14:00:54 2023 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 4RwdYg241zz4vW09; Wed, 27 Sep 2023 14:00:55 +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 "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4RwdYg1c4Tz4cmr; Wed, 27 Sep 2023 14:00:55 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1695823255; 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=RPmZO0XAOCZmquMZ7OfXVX8zyImamMuilXBgh0rrLBM=; b=HV1egBhsFRZ/+y43SsbE/6mjNt16omTeHNICxugldyvY4SRriBa8aWal5ASPPfgMEPz6Wi hP9XTrb/q8VNQJ7z1INQvKXC+5Lo6b03p4AAqokyhfAv4qcIDA4uC8VyVuqrig6EQewB4g 32cRgFuNDBYWSFkxDvM2VMV6xAsnhVEw7APianMa/1BssLDDEbokV45rHOFM5fJn5lJphd LlZdmj73/6LOkKFQAYH0XbrELP4+AiL6kbayrBUCcWBFUwahPlCDfyK6kBcqAlPWttrfxn qvA8lRMb4a8ZTA8/EGTcQeP5wdRHxPLmShxav7OgS5fABpokzEBdhyvc7lLZAA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1695823255; a=rsa-sha256; cv=none; b=Kgsy/SDo9xfeP/2fa44Irbj4ERYpc7ttOERJebs5TYPL9c0PfQq+eZJt8IVJ7O/71PdsxC PMwPhnH+V9B1mniwOnO7nBILgXOeZTpzNLmnIOctQ/dW+F9vXYo0K3smaWfCsiTIQiF5xz O4QUW3t5SOBAc7U8AbKw8q5ZgV4adM4ZrMTOuKD+X6l4KSMNxCUZgk3YcvwpivH8raLPkE 0rP6b1TtWcMCVSFK3pf7yI7jSZQpvIGM60HJEn1/ki/EjLTGm+qmsxm54sU63QUbA+9j0d Ae0oJUJ7iV1R7y36HszqRNpLMkd5kLuG65hL4hBbJuNnDf51Fg3mgc7cBtKCfA== 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=1695823255; 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=RPmZO0XAOCZmquMZ7OfXVX8zyImamMuilXBgh0rrLBM=; b=CJLv7PhSbD9Qmz5CtaYn211wcbweygd1/8e8VZ1UE+znyjc4eRCdbQhaoCA1iIsFESpkL1 ctvOK1Km5iguSIHWz7ZT96eerNuDFAGK1VBI2hVlppmpTdGloRb5BaaxxpXdU2LHeqI00U bmJIUTrQ6Y7ZtOwbY8ZNHs16dv/mxXqU9QoMbXh8+tQDhFfXGawfdlIBGQ5pscFgMGSNSk NdolRC+98utnBCK2mejSSAxC0L8NzYLNZlc8ZpXMln1+D39V/inxx/pQPaynZxLynjfNIb ps9CHRjKV26KaoHhvaTvFiC9w61eEgtpTJFg8mKidIsWZqO2Xw9yPtAYBrZPRg== 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 4RwdYg0fv0zhgQ; Wed, 27 Sep 2023 14:00:55 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 38RE0s0e037016; Wed, 27 Sep 2023 14:00:54 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 38RE0s2R037013; Wed, 27 Sep 2023 14:00:54 GMT (envelope-from git) Date: Wed, 27 Sep 2023 14:00:54 GMT Message-Id: <202309271400.38RE0s2R037013@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: 1f31e00e19f9 - main - 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: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: 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: 1f31e00e19f9e24d4c891a24973e08a027c4f71c Auto-Submitted: auto-generated The branch main has been updated by bapt: URL: https://cgit.FreeBSD.org/src/commit/?id=1f31e00e19f9e24d4c891a24973e08a027c4f71c commit 1f31e00e19f9e24d4c891a24973e08a027c4f71c Author: Baptiste Daroussin AuthorDate: 2023-09-13 07:43:33 +0000 Commit: Baptiste Daroussin CommitDate: 2023-09-27 14:00:00 +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 --- 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 6d05d9d72a52..7a58b2265a5a 100644 --- a/libexec/flua/Makefile +++ b/libexec/flua/Makefile @@ -18,7 +18,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 671a0300783c..4d4d69920e94 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" #include "lua_ucl.h" /* @@ -60,6 +61,7 @@ static const luaL_Reg loadedlibs[] = { {"posix.sys.stat", luaopen_posix_sys_stat}, {"posix.unistd", luaopen_posix_unistd}, {"ucl", luaopen_ucl}, + {"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);