Added some questionable support for running everything in one process, because iOS.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4520 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
2023-10-15 17:33:36 +00:00
parent 8bd0027e71
commit a69e551968
5 changed files with 98 additions and 34 deletions

View File

@ -63,6 +63,16 @@ static JSValue _taskstub_loadFile(JSContext* context, JSValueConst this_val, int
static void _taskstub_on_process_exit(uv_process_t* process, int64_t status, int terminationSignal);
static void _taskstub_finalizer(JSRuntime *runtime, JSValue value);
static void _tf_taskstub_run_sandbox_thread(void* data)
{
uv_file fd = (uv_file)(intptr_t)data;
tf_task_t* task = tf_task_create();
tf_task_configure_from_fd(task, fd);
/* The caller will trigger tf_task_activate with a message. */
tf_task_run(task);
tf_task_destroy(task);
}
static JSValue _taskstub_create(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
{
tf_task_t* parent = tf_task_get(context);
@ -125,41 +135,73 @@ static JSValue _taskstub_create(JSContext* context, JSValueConst this_val, int a
char arg1[] = "sandbox";
char* command_argv[] = { _executable, arg1, 0 };
uv_pipe_t* pipe = tf_packetstream_get_pipe(stub->_stream);
memset(pipe, 0, sizeof(*pipe));
if (uv_pipe_init(tf_task_get_loop(parent), pipe, 1) != 0)
{
tf_printf("uv_pipe_init failed\n");
}
uv_stdio_container_t io[3];
io[0].flags = UV_CREATE_PIPE | UV_READABLE_PIPE | UV_WRITABLE_PIPE;
io[0].data.stream = (uv_stream_t*)pipe;
io[1].flags = UV_INHERIT_FD;
io[1].data.fd = STDOUT_FILENO;
io[2].flags = UV_INHERIT_FD;
io[2].data.fd = STDERR_FILENO;
uv_process_options_t options = {0};
options.args = command_argv;
options.exit_cb = _taskstub_on_process_exit;
options.stdio = io;
options.stdio_count = sizeof(io) / sizeof(*io);
options.file = command_argv[0];
JSValue result = JS_NULL;
stub->_process.data = stub;
int spawn_result = uv_spawn(tf_task_get_loop(parent), &stub->_process, &options);
if (spawn_result == 0)
if (tf_task_get_one_proc(parent))
{
uv_os_sock_t fds[2];
int pipe_result = uv_socketpair(SOCK_STREAM, AF_UNIX, fds, 0, 0);
if (pipe_result)
{
tf_printf("uv_socketpair failed: %s\n", uv_strerror(pipe_result));
}
uv_pipe_t* pipe = tf_packetstream_get_pipe(stub->_stream);
memset(pipe, 0, sizeof(*pipe));
pipe_result = uv_pipe_init(tf_task_get_loop(parent), pipe, 1);
if (pipe_result != 0)
{
tf_printf("uv_pipe_init failed: %s\n", uv_strerror(pipe_result));
}
pipe_result = uv_pipe_open(pipe, fds[0]);
if (pipe_result != 0)
{
tf_printf("uv_pipe_open failed: %s\n", uv_strerror(pipe_result));
}
uv_thread_t* thread = tf_malloc(sizeof(uv_thread_t));
uv_thread_create(thread, _tf_taskstub_run_sandbox_thread, (void*)(intptr_t)fds[1]);
tf_packetstream_set_on_receive(stub->_stream, tf_task_on_receive_packet, stub);
tf_packetstream_start(stub->_stream);
result = taskObject;
}
else
{
tf_printf("uv_spawn failed: %s\n", uv_strerror(spawn_result));
JS_FreeValue(context, taskObject);
uv_pipe_t* pipe = tf_packetstream_get_pipe(stub->_stream);
memset(pipe, 0, sizeof(*pipe));
if (uv_pipe_init(tf_task_get_loop(parent), pipe, 1) != 0)
{
tf_printf("uv_pipe_init failed\n");
}
uv_stdio_container_t io[3];
io[0].flags = UV_CREATE_PIPE | UV_READABLE_PIPE | UV_WRITABLE_PIPE;
io[0].data.stream = (uv_stream_t*)pipe;
io[1].flags = UV_INHERIT_FD;
io[1].data.fd = STDOUT_FILENO;
io[2].flags = UV_INHERIT_FD;
io[2].data.fd = STDERR_FILENO;
uv_process_options_t options = {0};
options.args = command_argv;
options.exit_cb = _taskstub_on_process_exit;
options.stdio = io;
options.stdio_count = sizeof(io) / sizeof(*io);
options.file = command_argv[0];
stub->_process.data = stub;
int spawn_result = uv_spawn(tf_task_get_loop(parent), &stub->_process, &options);
if (spawn_result == 0)
{
tf_packetstream_set_on_receive(stub->_stream, tf_task_on_receive_packet, stub);
tf_packetstream_start(stub->_stream);
result = taskObject;
}
else
{
tf_printf("uv_spawn failed: %s\n", uv_strerror(spawn_result));
JS_FreeValue(context, taskObject);
}
}
return result;
}
@ -230,9 +272,10 @@ tf_taskstub_t* tf_taskstub_create_parent(tf_task_t* task, uv_file file)
tf_printf("uv_pipe_init failed\n");
}
tf_packetstream_set_on_receive(parentStub->_stream, tf_task_on_receive_packet, parentStub);
if (uv_pipe_open(tf_packetstream_get_pipe(parentStub->_stream), file) != 0)
int result = uv_pipe_open(tf_packetstream_get_pipe(parentStub->_stream), file);
if (result != 0)
{
tf_printf("uv_pipe_open failed\n");
tf_printf("uv_pipe_open failed: %s\n", uv_strerror(result));
}
tf_packetstream_start(parentStub->_stream);