1 /* 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright 2023 Baptiste Daroussin <bapt@FreeBSD.org> 5 * Copyright (C) 2025 Kyle Evans <kevans@FreeBSD.org> 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted providing that the following conditions~ 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 25 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/wait.h> 30 31 #include <errno.h> 32 #include <fcntl.h> 33 #include <spawn.h> 34 #include <stdbool.h> 35 #include <string.h> 36 #include <stdio.h> 37 #include <unistd.h> 38 39 #include <lua.h> 40 #include "lauxlib.h" 41 #include "lfbsd.h" 42 43 #define FBSD_PROCESSHANDLE "fbsd_process_t*" 44 45 struct fbsd_process { 46 int pid; 47 int stdin_fileno; 48 int stdout_fileno; 49 }; 50 51 extern char **environ; 52 53 static const char** 54 luaL_checkarraystrings(lua_State *L, int arg) 55 { 56 const char **ret; 57 lua_Integer n, i; 58 int t; 59 int abs_arg = lua_absindex(L, arg); 60 luaL_checktype(L, abs_arg, LUA_TTABLE); 61 n = lua_rawlen(L, abs_arg); 62 ret = lua_newuserdata(L, (n+1)*sizeof(char*)); 63 for (i=0; i<n; i++) { 64 t = lua_rawgeti(L, abs_arg, i+1); 65 if (t == LUA_TNIL) 66 break; 67 luaL_argcheck(L, t == LUA_TSTRING, arg, "expected array of strings"); 68 ret[i] = lua_tostring(L, -1); 69 lua_pop(L, 1); 70 } 71 ret[i] = NULL; 72 return ret; 73 } 74 75 static void 76 close_pipes(int pipes[2]) 77 { 78 79 if (pipes[0] != -1) 80 close(pipes[0]); 81 if (pipes[1] != -1) 82 close(pipes[1]); 83 } 84 85 static int 86 lua_exec(lua_State *L) 87 { 88 struct fbsd_process *proc; 89 int r; 90 posix_spawn_file_actions_t action; 91 int stdin_pipe[2] = {-1, -1}; 92 int stdout_pipe[2] = {-1, -1}; 93 pid_t pid; 94 const char **argv; 95 int n = lua_gettop(L); 96 bool capture_stdout; 97 luaL_argcheck(L, n > 0 && n <= 2, n >= 2 ? 2 : n, 98 "fbsd.exec takes exactly one or two arguments"); 99 100 capture_stdout = lua_toboolean(L, 2); 101 if (pipe(stdin_pipe) < 0) { 102 lua_pushnil(L); 103 lua_pushstring(L, strerror(errno)); 104 lua_pushinteger(L, errno); 105 return (3); 106 } 107 if (capture_stdout && pipe(stdout_pipe) < 0) { 108 close_pipes(stdin_pipe); 109 lua_pushnil(L); 110 lua_pushstring(L, strerror(errno)); 111 lua_pushinteger(L, errno); 112 return (3); 113 } 114 115 proc = lua_newuserdata(L, sizeof(*proc)); 116 proc->stdin_fileno = stdin_pipe[1]; 117 proc->stdout_fileno = stdout_pipe[1]; 118 posix_spawn_file_actions_init(&action); 119 posix_spawn_file_actions_adddup2(&action, stdin_pipe[0], STDIN_FILENO); 120 posix_spawn_file_actions_addclose(&action, stdin_pipe[1]); 121 if (stdin_pipe[0] != STDIN_FILENO) 122 posix_spawn_file_actions_addclose(&action, stdin_pipe[0]); 123 124 /* 125 * Setup stdout to be captured if requested. Otherwise, we just let it 126 * go to our own stdout. 127 */ 128 if (stdout_pipe[0] != -1) { 129 posix_spawn_file_actions_adddup2(&action, stdout_pipe[0], 130 STDOUT_FILENO); 131 posix_spawn_file_actions_addclose(&action, stdout_pipe[1]); 132 if (stdout_pipe[0] != STDOUT_FILENO) { 133 posix_spawn_file_actions_addclose(&action, 134 stdout_pipe[0]); 135 } 136 } 137 138 argv = luaL_checkarraystrings(L, 1); 139 if (0 != (r = posix_spawnp(&pid, argv[0], &action, NULL, 140 (char*const*)argv, environ))) { 141 close_pipes(stdin_pipe); 142 close_pipes(stdout_pipe); 143 posix_spawn_file_actions_destroy(&action); 144 lua_pop(L, 2); /* Pop off the process handle and args. */ 145 146 lua_pushnil(L); 147 lua_pushstring(L, strerror(r)); 148 lua_pushinteger(L, r); 149 return (3); 150 } 151 152 lua_pop(L, 1); 153 154 close(stdin_pipe[0]); 155 if (stdout_pipe[0] != -1) 156 close(stdout_pipe[0]); 157 posix_spawn_file_actions_destroy(&action); 158 159 proc->pid = pid; 160 luaL_setmetatable(L, FBSD_PROCESSHANDLE); 161 162 return (1); 163 } 164 165 static int 166 lua_process_close(lua_State *L) 167 { 168 struct fbsd_process *proc; 169 int pstat, r; 170 171 proc = luaL_checkudata(L, 1, FBSD_PROCESSHANDLE); 172 while (waitpid(proc->pid, &pstat, 0) == -1) { 173 if ((r = errno) != EINTR) { 174 lua_pushnil(L); 175 lua_pushstring(L, strerror(r)); 176 lua_pushinteger(L, r); 177 return (3); 178 } 179 } 180 181 if (!WIFEXITED(pstat) || WEXITSTATUS(pstat) != 0) { 182 lua_pushnil(L); 183 lua_pushstring(L, "Abnormal termination"); 184 return (2); 185 } 186 187 if (proc->stdin_fileno >= 0) { 188 close(proc->stdin_fileno); 189 proc->stdin_fileno = -1; 190 } 191 192 if (proc->stdout_fileno >= 0) { 193 close(proc->stdout_fileno); 194 proc->stdout_fileno = -1; 195 } 196 197 lua_pushboolean(L, 1); 198 return (1); 199 } 200 201 static int 202 lua_process_makestdio(lua_State *L, int fd, const char *mode) 203 { 204 luaL_Stream *p; 205 FILE *fp; 206 int r; 207 208 if (fd == -1) { 209 lua_pushnil(L); 210 lua_pushstring(L, "Stream not captured"); 211 return (2); 212 } 213 214 fp = fdopen(fd, mode); 215 if (fp == NULL) { 216 r = errno; 217 218 lua_pushnil(L); 219 lua_pushstring(L, strerror(r)); 220 lua_pushinteger(L, r); 221 return (3); 222 } 223 224 p = lua_newuserdata(L, sizeof(*p)); 225 p->closef = &lua_process_close; 226 p->f = fp; 227 luaL_setmetatable(L, LUA_FILEHANDLE); 228 return (1); 229 } 230 231 static int 232 lua_process_stdin(lua_State *L) 233 { 234 struct fbsd_process *proc; 235 236 proc = luaL_checkudata(L, 1, FBSD_PROCESSHANDLE); 237 return (lua_process_makestdio(L, proc->stdin_fileno, "w")); 238 } 239 240 static int 241 lua_process_stdout(lua_State *L) 242 { 243 struct fbsd_process *proc; 244 245 proc = luaL_checkudata(L, 1, FBSD_PROCESSHANDLE); 246 return (lua_process_makestdio(L, proc->stdout_fileno, "r")); 247 } 248 249 #define PROCESS_SIMPLE(n) { #n, lua_process_ ## n } 250 static const struct luaL_Reg fbsd_process[] = { 251 PROCESS_SIMPLE(close), 252 PROCESS_SIMPLE(stdin), 253 PROCESS_SIMPLE(stdout), 254 { NULL, NULL }, 255 }; 256 257 static const struct luaL_Reg fbsd_process_meta[] = { 258 { "__index", NULL }, 259 { "__gc", lua_process_close }, 260 { "__close", lua_process_close }, 261 { NULL, NULL }, 262 }; 263 264 #define REG_SIMPLE(n) { #n, lua_ ## n } 265 static const struct luaL_Reg fbsd_lib[] = { 266 REG_SIMPLE(exec), 267 { NULL, NULL }, 268 }; 269 #undef REG_SIMPLE 270 271 int 272 luaopen_fbsd(lua_State *L) 273 { 274 luaL_newlib(L, fbsd_lib); 275 276 luaL_newmetatable(L, FBSD_PROCESSHANDLE); 277 luaL_setfuncs(L, fbsd_process_meta, 0); 278 279 luaL_newlibtable(L, fbsd_process); 280 luaL_setfuncs(L, fbsd_process, 0); 281 lua_setfield(L, -2, "__index"); 282 lua_pop(L, 1); 283 284 return (1); 285 } 286