From 50b54599ef0a8c5dbe22b3af4c7ac0155b5b21dd Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Thu, 4 Jul 2024 13:18:23 -0400 Subject: [PATCH] Minor cleanup. --- src/main.c | 1 - src/taskstub.js.c | 102 ++++++++++++++++++---------------------------- 2 files changed, 39 insertions(+), 64 deletions(-) diff --git a/src/main.c b/src/main.c index 37b203d1..68652498 100644 --- a/src/main.c +++ b/src/main.c @@ -629,7 +629,6 @@ static int _tf_command_sandbox(const char* file, int argc, char* argv[]) show_usage = true; break; case 'f': - tf_printf("got -f %s\n", optarg); fd = atoi(optarg); break; } diff --git a/src/taskstub.js.c b/src/taskstub.js.c index e2071e12..80c38df2 100644 --- a/src/taskstub.js.c +++ b/src/taskstub.js.c @@ -122,10 +122,11 @@ static JSValue _taskstub_create(JSContext* context, JSValueConst this_val, int a char arg1[] = "sandbox"; char* command_argv[] = { _executable, arg1, 0 }; + tf_android_start_service_t* start_service = tf_task_get_android_start_service(); JSValue result = JS_NULL; - if (tf_task_get_one_proc(parent)) + if (tf_task_get_one_proc(parent) || start_service) { - uv_os_sock_t fds[2]; + uv_os_sock_t fds[2] = { 0 }; int pipe_result = uv_socketpair(SOCK_STREAM, 0, fds, 0, 0); if (pipe_result) { @@ -133,7 +134,7 @@ static JSValue _taskstub_create(JSContext* context, JSValueConst this_val, int a } uv_pipe_t* pipe = tf_packetstream_get_pipe(stub->_stream); - memset(pipe, 0, sizeof(*pipe)); + *pipe = (uv_pipe_t) { 0 }; pipe_result = uv_pipe_init(tf_task_get_loop(parent), pipe, 1); if (pipe_result != 0) { @@ -145,8 +146,16 @@ static JSValue _taskstub_create(JSContext* context, JSValueConst this_val, int a 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]); + if (start_service) + { + start_service(fds[1]); + } + else + { + /* XXX: This is a leak. */ + 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); @@ -154,74 +163,41 @@ static JSValue _taskstub_create(JSContext* context, JSValueConst this_val, int a } else { - tf_android_start_service_t* start_service = tf_task_get_android_start_service(); - if (start_service) + 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) { - uv_os_sock_t fds[2]; - int socketpair_result = uv_socketpair(SOCK_STREAM, 0, fds, 0, 0); - if (socketpair_result) - { - tf_printf("uv_socketpair: %s\n", uv_strerror(socketpair_result)); - } + tf_printf("uv_pipe_init failed\n"); + } - uv_pipe_t* pipe = tf_packetstream_get_pipe(stub->_stream); - *pipe = (uv_pipe_t) { 0 }; - 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; - int pipe_result = uv_pipe_open(pipe, fds[0]); - if (pipe_result) - { - tf_printf("uv_pipe_open: %s\n", uv_strerror(pipe_result)); - } + 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]; + options.env = (char*[]) { "ASAN_OPTIONS=detect_leaks=0", NULL }; - start_service(fds[1]); - - stub->_process.data = stub; + 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 { - 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]; - options.env = (char*[]) { "ASAN_OPTIONS=detect_leaks=0", NULL }; - - 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); - } + tf_printf("uv_spawn failed: %s\n", uv_strerror(spawn_result)); + JS_FreeValue(context, taskObject); } } return JS_DupValue(context, result);