From 293110feaa825d1fcddef616505f15cc008c5e97 Mon Sep 17 00:00:00 2001 From: Adam Date: Tue, 4 Oct 2022 20:02:22 -0400 Subject: [PATCH] Fix process api detach bug (#1137) * Added in conditional, as detaching will fail with "Operation not permitted" on linux; setsid sets a new process group anyway. * Added in variable to check detached state on cleanup, so we don't send TERM. --- src/api/process.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/api/process.c b/src/api/process.c index d713a84b..82842176 100644 --- a/src/api/process.c +++ b/src/api/process.c @@ -29,7 +29,7 @@ typedef int process_handle; #endif typedef struct { - bool running; + bool running, detached; int returncode, deadline; long pid; #if _WIN32 @@ -38,7 +38,7 @@ typedef struct { bool reading[2]; char buffer[2][READ_BUF_SIZE]; #endif - process_handle child_pipes[3][2]; + process_handle child_pipes[3][2]; } process_t; typedef enum { @@ -183,6 +183,7 @@ static int process_start(lua_State* L) { memset(self, 0, sizeof(process_t)); luaL_setmetatable(L, API_TYPE_PROCESS); self->deadline = deadline; + self->detached = detach; #if _WIN32 for (int i = 0; i < 3; ++i) { switch (new_fds[i]) { @@ -278,7 +279,8 @@ static int process_start(lua_State* L) { retval = luaL_error(L, "Error running fork: %s.", strerror(errno)); goto cleanup; } else if (!self->pid) { - setpgid(0,0); + if (!detach) + setpgid(0,0); for (int stream = 0; stream < 3; ++stream) { if (new_fds[stream] == REDIRECT_DISCARD) { // Close the stream if we don't want it. close(self->child_pipes[stream][stream == STDIN_FD ? 0 : 1]); @@ -454,7 +456,8 @@ static int f_kill(lua_State* L) { return self_signal(L, SIGNAL_KILL); } static int f_interrupt(lua_State* L) { return self_signal(L, SIGNAL_INTERRUPT); } static int f_gc(lua_State* L) { process_t* self = (process_t*) luaL_checkudata(L, 1, API_TYPE_PROCESS); - signal_process(self, SIGNAL_TERM); + if (!self->detached) + signal_process(self, SIGNAL_TERM); close_fd(&self->child_pipes[STDIN_FD ][1]); close_fd(&self->child_pipes[STDOUT_FD][0]); close_fd(&self->child_pipes[STDERR_FD][0]);