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