From nobody Thu Sep 11 18:50:24 2025 X-Original-To: dev-commits-src-branches@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 4cN67j2H5zz67Hnp; Thu, 11 Sep 2025 18:50:25 +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 4cN67h6kygz3hCT; Thu, 11 Sep 2025 18:50:24 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1757616625; 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=tCq1xBqHtQ+CONhZpTjqekUovU3oJp6mp/iI+ooiL9o=; b=LnqyCNr4kZpSBNN7dmyq10/v7fH+95XqVKKYsjEJKT5PJg3v6ZvJvVBq2uyzRNDYiSg+EY IHR+gL8ekuhT1xRtMh/MIDIKlol/+qXh3Z4FVkZ4DNa/vrRwCttAwErHZSh9h0j2BfbUjF S4zbU57pFuEeZe5lPeEsE5/2W/VNCGv7uVZfw5AN/o0jN4Ptsh2YhvWgRUglLl3p7Jkk0P iYohNcoB3RaBsDpGwBnuSOkdJ1p5xhNxuwzNoijZiQ/YxwbCjHyZ428rPLo1dj4oYAUo7o elJKdcYBVGQE0HsexlZbOiCeWQn2ntMgqpH4o4yYBgsTQfTtzMFkj9d4pgvhHQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1757616625; 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=tCq1xBqHtQ+CONhZpTjqekUovU3oJp6mp/iI+ooiL9o=; b=XVEjyvFUWRDvzh5fAqdHX4Y4fUiBAbiPwK3TZbOMv46R+DCLbY3uZ2m5lwpTPPt9KW3ISN PJoqwPQoL1xs83/aHqyhYlk7pFBaUuT/XVUMv12gVxy0W2epwNQYhoYXD/1EpV5wCw2WHc fB1QQi3yhAROKnnrjiPJLp1RLdKlxuNN/usflzkE3nFOZh9EFH9ejCCj3J6bwO8UwhpqVC acwOjjbt8USr7dDtWVZ98oevYuQBVfiTeudqzVR0gO4fxxp/VdWTluk9HiUx9jxQyJVD4E rfmtU9Au7OqyXYdSzu+5haWr6hzH4gzESAGX7gbol0wFQCe4sT/4jrmWOJ1J0A== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1757616625; a=rsa-sha256; cv=none; b=CZcYTWBLl9c+k1cQsi0UZ1uWfexFOt6eXGWv8n0/U5C6edvbj6NYJF5zdKXwKR2L9Shfvx JO0DVscf2B+bQmsv6xNJjy80zQnHp6XE929n70YXyV6/DLXDz5IwvR15oiKMv/HP2mjWPf fGTo9BDv6hmCYy/8IwzpOYtkEHHYZSan6ndIldP86k9CXYY/k79D7W40/BR64k4RgV/KTF fk4c2js6h54clmdexcEtW9rydoCIs2a2JfcheNiu9pkW+hVWd9nn2UGlvFBoRNy+k7xR6W jPcpARtubHh///IpoVXdt7vIbf0Lc1gyBjCwnwHmU5yStjxw4u+BrLs0pse4Jg== 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 4cN67h657yz4DJ; Thu, 11 Sep 2025 18:50:24 +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 58BIoOmZ092976; Thu, 11 Sep 2025 18:50:24 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 58BIoOXd092973; Thu, 11 Sep 2025 18:50:24 GMT (envelope-from git) Date: Thu, 11 Sep 2025 18:50:24 GMT Message-Id: <202509111850.58BIoOXd092973@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: d9412b5fff91 - stable/14 - flua: fbsd: return a process handle to operate on when we exec() List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-branches@freebsd.org Sender: owner-dev-commits-src-branches@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: d9412b5fff915aa4d0c21d863a1a6d2e08bafbb0 Auto-Submitted: auto-generated The branch stable/14 has been updated by emaste: URL: https://cgit.FreeBSD.org/src/commit/?id=d9412b5fff915aa4d0c21d863a1a6d2e08bafbb0 commit d9412b5fff915aa4d0c21d863a1a6d2e08bafbb0 Author: Kyle Evans AuthorDate: 2025-07-09 05:12:31 +0000 Commit: Ed Maste CommitDate: 2025-09-11 15:09:59 +0000 flua: fbsd: return a process handle to operate on when we exec() This gives us some way to be able to write to stdin if we want to, or as a future improvement, will allow us to extract stdout from the process. The handle is setup to close and waitpid() on close/gc so that existing users wouldn't necessarily leak for the lifetime of the script if they weren't adopted to the new model. Reviewed by: bapt Differential Revision: https://reviews.freebsd.org/D50538 (cherry picked from commit 6a2c624b35a0c760b00b9a34c10b7ea0c240c677) --- libexec/flua/modules/lfbsd.c | 94 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 86 insertions(+), 8 deletions(-) diff --git a/libexec/flua/modules/lfbsd.c b/libexec/flua/modules/lfbsd.c index 6279474f8388..a361fa25601b 100644 --- a/libexec/flua/modules/lfbsd.c +++ b/libexec/flua/modules/lfbsd.c @@ -2,6 +2,7 @@ * SPDX-License-Identifier: BSD-2-Clause * * Copyright 2023 Baptiste Daroussin + * Copyright (C) 2025 Kyle Evans * * Redistribution and use in source and binary forms, with or without * modification, are permitted providing that the following conditions~ @@ -38,6 +39,13 @@ #include "lauxlib.h" #include "lfbsd.h" +#define FBSD_PROCESSHANDLE "fbsd_process_t*" + +struct fbsd_process { + int pid; + int stdin_fileno; +}; + extern char **environ; static const char** @@ -65,7 +73,8 @@ luaL_checkarraystrings(lua_State *L, int arg) static int lua_exec(lua_State *L) { - int r, pstat; + struct fbsd_process *proc; + int r; posix_spawn_file_actions_t action; int stdin_pipe[2] = {-1, -1}; pid_t pid; @@ -81,6 +90,9 @@ lua_exec(lua_State *L) return (3); } + proc = lua_newuserdata(L, sizeof(*proc)); + proc->stdin_fileno = stdin_pipe[1]; + 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]); @@ -91,6 +103,7 @@ lua_exec(lua_State *L) close(stdin_pipe[0]); close(stdin_pipe[1]); posix_spawn_file_actions_destroy(&action); + lua_pop(L, 2); /* Pop off the process handle and args. */ lua_pushnil(L); lua_pushstring(L, strerror(r)); @@ -98,12 +111,26 @@ lua_exec(lua_State *L) return (3); } + lua_pop(L, 1); + close(stdin_pipe[0]); posix_spawn_file_actions_destroy(&action); - while (waitpid(pid, &pstat, 0) == -1) { - if (errno != EINTR) { - close(stdin_pipe[1]); + proc->pid = pid; + luaL_setmetatable(L, FBSD_PROCESSHANDLE); + + return 1; +} + +static int +lua_process_close(lua_State *L) +{ + struct fbsd_process *proc; + int pstat, r; + + proc = luaL_checkudata(L, 1, FBSD_PROCESSHANDLE); + while (waitpid(proc->pid, &pstat, 0) == -1) { + if ((r = errno) != EINTR) { lua_pushnil(L); lua_pushstring(L, strerror(r)); lua_pushinteger(L, r); @@ -111,18 +138,60 @@ lua_exec(lua_State *L) } } - if (WEXITSTATUS(pstat) != 0) { + if (!WIFEXITED(pstat) || WEXITSTATUS(pstat) != 0) { lua_pushnil(L); lua_pushstring(L, "Abnormal termination"); + return (2); + } + + if (proc->stdin_fileno >= 0) + close(proc->stdin_fileno); + proc->stdin_fileno = -1; + + lua_pushboolean(L, 1); + return 1; +} + +static int +lua_process_stdin(lua_State *L) +{ + struct fbsd_process *proc; + luaL_Stream *p; + FILE *fp; + int r; + + proc = luaL_checkudata(L, 1, FBSD_PROCESSHANDLE); + fp = fdopen(proc->stdin_fileno, "w"); + if (fp == NULL) { + r = errno; + + lua_pushnil(L); + lua_pushstring(L, strerror(r)); lua_pushinteger(L, r); return (3); } - close(stdin_pipe[1]); - lua_pushinteger(L, pid); - return 1; + p = lua_newuserdata(L, sizeof(*p)); + p->closef = &lua_process_close; + p->f = fp; + luaL_setmetatable(L, LUA_FILEHANDLE); + return (1); } +#define PROCESS_SIMPLE(n) { #n, lua_process_ ## n } +static const struct luaL_Reg fbsd_process[] = { + PROCESS_SIMPLE(close), + PROCESS_SIMPLE(stdin), + { NULL, NULL }, +}; + +static const struct luaL_Reg fbsd_process_meta[] = { + { "__index", NULL }, + { "__gc", lua_process_close }, + { "__close", lua_process_close }, + { NULL, NULL }, +}; + #define REG_SIMPLE(n) { #n, lua_ ## n } static const struct luaL_Reg fbsd_lib[] = { REG_SIMPLE(exec), @@ -134,5 +203,14 @@ int luaopen_fbsd(lua_State *L) { luaL_newlib(L, fbsd_lib); + + luaL_newmetatable(L, FBSD_PROCESSHANDLE); + luaL_setfuncs(L, fbsd_process_meta, 0); + + luaL_newlibtable(L, fbsd_process); + luaL_setfuncs(L, fbsd_process, 0); + lua_setfield(L, -2, "__index"); + lua_pop(L, 1); + return (1); }