Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/libexec/flua/lfbsd/lfbsd.c
39536 views
1
/*
2
* SPDX-License-Identifier: BSD-2-Clause
3
*
4
* Copyright 2023 Baptiste Daroussin <[email protected]>
5
* Copyright (C) 2025 Kyle Evans <[email protected]>
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
#include "bootstrap.h"
44
45
#define FBSD_PROCESSHANDLE "fbsd_process_t*"
46
47
struct fbsd_process {
48
int pid;
49
int stdin_fileno;
50
int stdout_fileno;
51
};
52
53
extern char **environ;
54
55
static const char**
56
luaL_checkarraystrings(lua_State *L, int arg)
57
{
58
const char **ret;
59
lua_Integer n, i;
60
int t;
61
int abs_arg = lua_absindex(L, arg);
62
luaL_checktype(L, abs_arg, LUA_TTABLE);
63
n = lua_rawlen(L, abs_arg);
64
ret = lua_newuserdata(L, (n+1)*sizeof(char*));
65
for (i=0; i<n; i++) {
66
t = lua_rawgeti(L, abs_arg, i+1);
67
if (t == LUA_TNIL)
68
break;
69
luaL_argcheck(L, t == LUA_TSTRING, arg, "expected array of strings");
70
ret[i] = lua_tostring(L, -1);
71
lua_pop(L, 1);
72
}
73
ret[i] = NULL;
74
return ret;
75
}
76
77
static void
78
close_pipes(int pipes[2])
79
{
80
81
if (pipes[0] != -1)
82
close(pipes[0]);
83
if (pipes[1] != -1)
84
close(pipes[1]);
85
}
86
87
static int
88
lua_exec(lua_State *L)
89
{
90
struct fbsd_process *proc;
91
int r;
92
posix_spawn_file_actions_t action;
93
int stdin_pipe[2] = {-1, -1};
94
int stdout_pipe[2] = {-1, -1};
95
pid_t pid;
96
const char **argv;
97
int n = lua_gettop(L);
98
bool capture_stdout;
99
luaL_argcheck(L, n > 0 && n <= 2, n >= 2 ? 2 : n,
100
"fbsd.exec takes exactly one or two arguments");
101
102
capture_stdout = lua_toboolean(L, 2);
103
if (pipe(stdin_pipe) < 0) {
104
lua_pushnil(L);
105
lua_pushstring(L, strerror(errno));
106
lua_pushinteger(L, errno);
107
return (3);
108
}
109
if (capture_stdout && pipe(stdout_pipe) < 0) {
110
close_pipes(stdin_pipe);
111
lua_pushnil(L);
112
lua_pushstring(L, strerror(errno));
113
lua_pushinteger(L, errno);
114
return (3);
115
}
116
117
proc = lua_newuserdata(L, sizeof(*proc));
118
proc->stdin_fileno = stdin_pipe[1];
119
proc->stdout_fileno = stdout_pipe[1];
120
posix_spawn_file_actions_init(&action);
121
posix_spawn_file_actions_adddup2(&action, stdin_pipe[0], STDIN_FILENO);
122
posix_spawn_file_actions_addclose(&action, stdin_pipe[1]);
123
if (stdin_pipe[0] != STDIN_FILENO)
124
posix_spawn_file_actions_addclose(&action, stdin_pipe[0]);
125
126
/*
127
* Setup stdout to be captured if requested. Otherwise, we just let it
128
* go to our own stdout.
129
*/
130
if (stdout_pipe[0] != -1) {
131
posix_spawn_file_actions_adddup2(&action, stdout_pipe[0],
132
STDOUT_FILENO);
133
posix_spawn_file_actions_addclose(&action, stdout_pipe[1]);
134
if (stdout_pipe[0] != STDOUT_FILENO) {
135
posix_spawn_file_actions_addclose(&action,
136
stdout_pipe[0]);
137
}
138
}
139
140
argv = luaL_checkarraystrings(L, 1);
141
if (0 != (r = posix_spawnp(&pid, argv[0], &action, NULL,
142
(char*const*)argv, environ))) {
143
close_pipes(stdin_pipe);
144
close_pipes(stdout_pipe);
145
posix_spawn_file_actions_destroy(&action);
146
lua_pop(L, 2); /* Pop off the process handle and args. */
147
148
lua_pushnil(L);
149
lua_pushstring(L, strerror(r));
150
lua_pushinteger(L, r);
151
return (3);
152
}
153
154
lua_pop(L, 1);
155
156
close(stdin_pipe[0]);
157
if (stdout_pipe[0] != -1)
158
close(stdout_pipe[0]);
159
posix_spawn_file_actions_destroy(&action);
160
161
proc->pid = pid;
162
luaL_setmetatable(L, FBSD_PROCESSHANDLE);
163
164
return (1);
165
}
166
167
static int
168
lua_process_close(lua_State *L)
169
{
170
struct fbsd_process *proc;
171
int pstat, r;
172
173
proc = luaL_checkudata(L, 1, FBSD_PROCESSHANDLE);
174
while (waitpid(proc->pid, &pstat, 0) == -1) {
175
if ((r = errno) != EINTR) {
176
lua_pushnil(L);
177
lua_pushstring(L, strerror(r));
178
lua_pushinteger(L, r);
179
return (3);
180
}
181
}
182
183
if (!WIFEXITED(pstat) || WEXITSTATUS(pstat) != 0) {
184
lua_pushnil(L);
185
lua_pushstring(L, "Abnormal termination");
186
return (2);
187
}
188
189
if (proc->stdin_fileno >= 0) {
190
close(proc->stdin_fileno);
191
proc->stdin_fileno = -1;
192
}
193
194
if (proc->stdout_fileno >= 0) {
195
close(proc->stdout_fileno);
196
proc->stdout_fileno = -1;
197
}
198
199
lua_pushboolean(L, 1);
200
return (1);
201
}
202
203
static int
204
lua_process_makestdio(lua_State *L, int fd, const char *mode)
205
{
206
luaL_Stream *p;
207
FILE *fp;
208
int r;
209
210
if (fd == -1) {
211
lua_pushnil(L);
212
lua_pushstring(L, "Stream not captured");
213
return (2);
214
}
215
216
fp = fdopen(fd, mode);
217
if (fp == NULL) {
218
r = errno;
219
220
lua_pushnil(L);
221
lua_pushstring(L, strerror(r));
222
lua_pushinteger(L, r);
223
return (3);
224
}
225
226
p = lua_newuserdata(L, sizeof(*p));
227
p->closef = &lua_process_close;
228
p->f = fp;
229
luaL_setmetatable(L, LUA_FILEHANDLE);
230
return (1);
231
}
232
233
static int
234
lua_process_stdin(lua_State *L)
235
{
236
struct fbsd_process *proc;
237
238
proc = luaL_checkudata(L, 1, FBSD_PROCESSHANDLE);
239
return (lua_process_makestdio(L, proc->stdin_fileno, "w"));
240
}
241
242
static int
243
lua_process_stdout(lua_State *L)
244
{
245
struct fbsd_process *proc;
246
247
proc = luaL_checkudata(L, 1, FBSD_PROCESSHANDLE);
248
return (lua_process_makestdio(L, proc->stdout_fileno, "r"));
249
}
250
251
#define PROCESS_SIMPLE(n) { #n, lua_process_ ## n }
252
static const struct luaL_Reg fbsd_process[] = {
253
PROCESS_SIMPLE(close),
254
PROCESS_SIMPLE(stdin),
255
PROCESS_SIMPLE(stdout),
256
{ NULL, NULL },
257
};
258
259
static const struct luaL_Reg fbsd_process_meta[] = {
260
{ "__index", NULL },
261
{ "__gc", lua_process_close },
262
{ "__close", lua_process_close },
263
{ NULL, NULL },
264
};
265
266
#define REG_SIMPLE(n) { #n, lua_ ## n }
267
static const struct luaL_Reg fbsd_lib[] = {
268
REG_SIMPLE(exec),
269
{ NULL, NULL },
270
};
271
#undef REG_SIMPLE
272
273
int
274
luaopen_fbsd(lua_State *L)
275
{
276
luaL_newlib(L, fbsd_lib);
277
278
luaL_newmetatable(L, FBSD_PROCESSHANDLE);
279
luaL_setfuncs(L, fbsd_process_meta, 0);
280
281
luaL_newlibtable(L, fbsd_process);
282
luaL_setfuncs(L, fbsd_process, 0);
283
lua_setfield(L, -2, "__index");
284
lua_pop(L, 1);
285
286
return (1);
287
}
288
289
FLUA_MODULE(fbsd);
290
291