From nobody Wed Jul 09 05:12:54 2025 X-Original-To: dev-commits-src-main@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 4bcR1y60FNz61770; Wed, 09 Jul 2025 05:12: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 "R10" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4bcR1y3C22z3LVb; Wed, 09 Jul 2025 05:12:54 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1752037974; 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=MUBKf2uf4OFy+KgjfM8lvY5TwJvkRbUM0kDrZDhrng4=; b=Ek1o9kctWv+G6lDH41XZOs00/wTpNlNtGAzkJ1RKmxKC4xVM5javdukQtg4Ohn0jh8YAAa tseUWkuZkH3vI7l+e5EKWAIqsH5JuLzC/hJZZW9UA1uQH8SGScSIiDGd7cjRrqZDnB+fHo evIVypJUxygMlK11kUIyb+MtdXES+GIbd0PXrHlOUizUgiXqlhcd9ttfoznhrkO1YJ0oUj oCQn/r7AXVd1ToANJz8xIsWlexpnRsORbBOjs8xK1Eq7aCI4Xk2lDQABVYzfI+jvU/zgoh 2mzoUdFopKBWIIrG2gXLJJ3doh9pvoNoSU24ncBZbd6sGzVMAWeT4GINRq6VEw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1752037974; 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=MUBKf2uf4OFy+KgjfM8lvY5TwJvkRbUM0kDrZDhrng4=; b=NNXLbwi6Nut9/vJ/sQuW3uD0yZYeMdjeGx7Ck7tfLsYKrBmE1uPt82pZD3auH2pUpRqJbE DbQJU2BWAc522gUakU/BPAgtiJWW1KElQP7lfwnQy69FsSSK/IrmN/U7HBpBw/NIDl0+GY KUQHgiGyPp0n8FpENA2zphNlAaayy2hY3AgwTXNhc+aaODZpd718Z2UhyrBZiRneO2u00K Id7A7t2ocn130QycHCCSYtPPMMkMXznGne1fPwa0uZG/6R7IVV3Db28BlL3KHRFdMjQQZf /8z3wDjKSoFQUBvYGbULyDQ+U3qD3C0QCp3P5+qfbCJ2/eMTiWCi4t6ONF4wNQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1752037974; a=rsa-sha256; cv=none; b=sl01I39sVPzm8Cj5eayRDQ8epsHSqVBiP2INiaEYNc0OdgEtaZmeYiWmzC1RZCTBI+M/AW kTqMr0FAvP9ih+DubWp0yfbZJSb/NMKXZKOQPVR0usoYTUoGY2+qXZSuWpyITjTrLg/MFH leMFIrMtqmIjzFpKkFPmnszbLNsKnjJFnCRJBGbKUzGK26cEgFNUMNLYtYV5nm9a8NIcIO WB+cn4aBFUsOOjbUvp0GKmPe1egeirHtu8HBPOpPZkPw3jbxvYj8Cy0m1xrF8X7hxaG6WG mEmpmkSXYzaVCk6wkVpT4VpLHViDtPk32u1uRhISZqgpi1T03XqGXULPh2AZyA== 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 4bcR1y2fvMz17KR; Wed, 09 Jul 2025 05:12: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 5695CsWm030490; Wed, 9 Jul 2025 05:12:54 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 5695CsMC030487; Wed, 9 Jul 2025 05:12:54 GMT (envelope-from git) Date: Wed, 9 Jul 2025 05:12:54 GMT Message-Id: <202507090512.5695CsMC030487@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Kyle Evans Subject: git: 6a2c624b35a0 - main - flua: fbsd: return a process handle to operate on when we exec() List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kevans X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 6a2c624b35a0c760b00b9a34c10b7ea0c240c677 Auto-Submitted: auto-generated The branch main has been updated by kevans: URL: https://cgit.FreeBSD.org/src/commit/?id=6a2c624b35a0c760b00b9a34c10b7ea0c240c677 commit 6a2c624b35a0c760b00b9a34c10b7ea0c240c677 Author: Kyle Evans AuthorDate: 2025-07-09 05:12:31 +0000 Commit: Kyle Evans CommitDate: 2025-07-09 05:12:31 +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 --- 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); }