1*1f31e00eSBaptiste Daroussin /*
2*1f31e00eSBaptiste Daroussin * SPDX-License-Identifier: BSD-2-Clause
3*1f31e00eSBaptiste Daroussin *
4*1f31e00eSBaptiste Daroussin * Copyright 2023 Baptiste Daroussin <bapt@FreeBSD.org>
5*1f31e00eSBaptiste Daroussin *
6*1f31e00eSBaptiste Daroussin * Redistribution and use in source and binary forms, with or without
7*1f31e00eSBaptiste Daroussin * modification, are permitted providing that the following conditions~
8*1f31e00eSBaptiste Daroussin * are met:
9*1f31e00eSBaptiste Daroussin * 1. Redistributions of source code must retain the above copyright
10*1f31e00eSBaptiste Daroussin * notice, this list of conditions and the following disclaimer.
11*1f31e00eSBaptiste Daroussin * 2. Redistributions in binary form must reproduce the above copyright
12*1f31e00eSBaptiste Daroussin * notice, this list of conditions and the following disclaimer in the
13*1f31e00eSBaptiste Daroussin * documentation and/or other materials provided with the distribution.
14*1f31e00eSBaptiste Daroussin *
15*1f31e00eSBaptiste Daroussin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16*1f31e00eSBaptiste Daroussin * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17*1f31e00eSBaptiste Daroussin * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*1f31e00eSBaptiste Daroussin * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19*1f31e00eSBaptiste Daroussin * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*1f31e00eSBaptiste Daroussin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21*1f31e00eSBaptiste Daroussin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22*1f31e00eSBaptiste Daroussin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23*1f31e00eSBaptiste Daroussin * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24*1f31e00eSBaptiste Daroussin * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25*1f31e00eSBaptiste Daroussin * POSSIBILITY OF SUCH DAMAGE.
26*1f31e00eSBaptiste Daroussin */
27*1f31e00eSBaptiste Daroussin
28*1f31e00eSBaptiste Daroussin #include <sys/wait.h>
29*1f31e00eSBaptiste Daroussin
30*1f31e00eSBaptiste Daroussin #include <errno.h>
31*1f31e00eSBaptiste Daroussin #include <fcntl.h>
32*1f31e00eSBaptiste Daroussin #include <spawn.h>
33*1f31e00eSBaptiste Daroussin #include <string.h>
34*1f31e00eSBaptiste Daroussin #include <stdio.h>
35*1f31e00eSBaptiste Daroussin #include <unistd.h>
36*1f31e00eSBaptiste Daroussin
37*1f31e00eSBaptiste Daroussin #include <lua.h>
38*1f31e00eSBaptiste Daroussin #include "lauxlib.h"
39*1f31e00eSBaptiste Daroussin #include "lfbsd.h"
40*1f31e00eSBaptiste Daroussin
41*1f31e00eSBaptiste Daroussin extern char **environ;
42*1f31e00eSBaptiste Daroussin
43*1f31e00eSBaptiste Daroussin static const char**
luaL_checkarraystrings(lua_State * L,int arg)44*1f31e00eSBaptiste Daroussin luaL_checkarraystrings(lua_State *L, int arg)
45*1f31e00eSBaptiste Daroussin {
46*1f31e00eSBaptiste Daroussin const char **ret;
47*1f31e00eSBaptiste Daroussin lua_Integer n, i;
48*1f31e00eSBaptiste Daroussin int t;
49*1f31e00eSBaptiste Daroussin int abs_arg = lua_absindex(L, arg);
50*1f31e00eSBaptiste Daroussin luaL_checktype(L, abs_arg, LUA_TTABLE);
51*1f31e00eSBaptiste Daroussin n = lua_rawlen(L, abs_arg);
52*1f31e00eSBaptiste Daroussin ret = lua_newuserdata(L, (n+1)*sizeof(char*));
53*1f31e00eSBaptiste Daroussin for (i=0; i<n; i++) {
54*1f31e00eSBaptiste Daroussin t = lua_rawgeti(L, abs_arg, i+1);
55*1f31e00eSBaptiste Daroussin if (t == LUA_TNIL)
56*1f31e00eSBaptiste Daroussin break;
57*1f31e00eSBaptiste Daroussin luaL_argcheck(L, t == LUA_TSTRING, arg, "expected array of strings");
58*1f31e00eSBaptiste Daroussin ret[i] = lua_tostring(L, -1);
59*1f31e00eSBaptiste Daroussin lua_pop(L, 1);
60*1f31e00eSBaptiste Daroussin }
61*1f31e00eSBaptiste Daroussin ret[i] = NULL;
62*1f31e00eSBaptiste Daroussin return ret;
63*1f31e00eSBaptiste Daroussin }
64*1f31e00eSBaptiste Daroussin
65*1f31e00eSBaptiste Daroussin static int
lua_exec(lua_State * L)66*1f31e00eSBaptiste Daroussin lua_exec(lua_State *L)
67*1f31e00eSBaptiste Daroussin {
68*1f31e00eSBaptiste Daroussin int r, pstat;
69*1f31e00eSBaptiste Daroussin posix_spawn_file_actions_t action;
70*1f31e00eSBaptiste Daroussin int stdin_pipe[2] = {-1, -1};
71*1f31e00eSBaptiste Daroussin pid_t pid;
72*1f31e00eSBaptiste Daroussin const char **argv;
73*1f31e00eSBaptiste Daroussin int n = lua_gettop(L);
74*1f31e00eSBaptiste Daroussin luaL_argcheck(L, n == 1, n > 1 ? 2 : n,
75*1f31e00eSBaptiste Daroussin "fbsd.exec takes exactly one argument");
76*1f31e00eSBaptiste Daroussin
77*1f31e00eSBaptiste Daroussin if (pipe(stdin_pipe) < 0) {
78*1f31e00eSBaptiste Daroussin lua_pushnil(L);
79*1f31e00eSBaptiste Daroussin lua_pushstring(L, strerror(errno));
80*1f31e00eSBaptiste Daroussin lua_pushinteger(L, errno);
81*1f31e00eSBaptiste Daroussin return (3);
82*1f31e00eSBaptiste Daroussin }
83*1f31e00eSBaptiste Daroussin
84*1f31e00eSBaptiste Daroussin posix_spawn_file_actions_init(&action);
85*1f31e00eSBaptiste Daroussin posix_spawn_file_actions_adddup2(&action, stdin_pipe[0], STDIN_FILENO);
86*1f31e00eSBaptiste Daroussin posix_spawn_file_actions_addclose(&action, stdin_pipe[1]);
87*1f31e00eSBaptiste Daroussin
88*1f31e00eSBaptiste Daroussin argv = luaL_checkarraystrings(L, 1);
89*1f31e00eSBaptiste Daroussin if (0 != (r = posix_spawnp(&pid, argv[0], &action, NULL,
90*1f31e00eSBaptiste Daroussin (char*const*)argv, environ))) {
91*1f31e00eSBaptiste Daroussin lua_pushnil(L);
92*1f31e00eSBaptiste Daroussin lua_pushstring(L, strerror(r));
93*1f31e00eSBaptiste Daroussin lua_pushinteger(L, r);
94*1f31e00eSBaptiste Daroussin return (3);
95*1f31e00eSBaptiste Daroussin }
96*1f31e00eSBaptiste Daroussin while (waitpid(pid, &pstat, 0) == -1) {
97*1f31e00eSBaptiste Daroussin if (errno != EINTR) {
98*1f31e00eSBaptiste Daroussin lua_pushnil(L);
99*1f31e00eSBaptiste Daroussin lua_pushstring(L, strerror(r));
100*1f31e00eSBaptiste Daroussin lua_pushinteger(L, r);
101*1f31e00eSBaptiste Daroussin return (3);
102*1f31e00eSBaptiste Daroussin }
103*1f31e00eSBaptiste Daroussin }
104*1f31e00eSBaptiste Daroussin
105*1f31e00eSBaptiste Daroussin if (WEXITSTATUS(pstat) != 0) {
106*1f31e00eSBaptiste Daroussin lua_pushnil(L);
107*1f31e00eSBaptiste Daroussin lua_pushstring(L, "Abnormal termination");
108*1f31e00eSBaptiste Daroussin lua_pushinteger(L, r);
109*1f31e00eSBaptiste Daroussin return (3);
110*1f31e00eSBaptiste Daroussin }
111*1f31e00eSBaptiste Daroussin
112*1f31e00eSBaptiste Daroussin posix_spawn_file_actions_destroy(&action);
113*1f31e00eSBaptiste Daroussin
114*1f31e00eSBaptiste Daroussin if (stdin_pipe[0] != -1)
115*1f31e00eSBaptiste Daroussin close(stdin_pipe[0]);
116*1f31e00eSBaptiste Daroussin if (stdin_pipe[1] != -1)
117*1f31e00eSBaptiste Daroussin close(stdin_pipe[1]);
118*1f31e00eSBaptiste Daroussin lua_pushinteger(L, pid);
119*1f31e00eSBaptiste Daroussin return 1;
120*1f31e00eSBaptiste Daroussin }
121*1f31e00eSBaptiste Daroussin
122*1f31e00eSBaptiste Daroussin #define REG_SIMPLE(n) { #n, lua_ ## n }
123*1f31e00eSBaptiste Daroussin static const struct luaL_Reg fbsd_lib[] = {
124*1f31e00eSBaptiste Daroussin REG_SIMPLE(exec),
125*1f31e00eSBaptiste Daroussin { NULL, NULL },
126*1f31e00eSBaptiste Daroussin };
127*1f31e00eSBaptiste Daroussin #undef REG_SIMPLE
128*1f31e00eSBaptiste Daroussin
129*1f31e00eSBaptiste Daroussin int
luaopen_fbsd(lua_State * L)130*1f31e00eSBaptiste Daroussin luaopen_fbsd(lua_State *L)
131*1f31e00eSBaptiste Daroussin {
132*1f31e00eSBaptiste Daroussin luaL_newlib(L, fbsd_lib);
133*1f31e00eSBaptiste Daroussin return (1);
134*1f31e00eSBaptiste Daroussin }
135