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.
This commit is contained in:
parent
1580d923d3
commit
293110feaa
|
@ -29,7 +29,7 @@ typedef int process_handle;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
bool running;
|
bool running, detached;
|
||||||
int returncode, deadline;
|
int returncode, deadline;
|
||||||
long pid;
|
long pid;
|
||||||
#if _WIN32
|
#if _WIN32
|
||||||
|
@ -183,6 +183,7 @@ static int process_start(lua_State* L) {
|
||||||
memset(self, 0, sizeof(process_t));
|
memset(self, 0, sizeof(process_t));
|
||||||
luaL_setmetatable(L, API_TYPE_PROCESS);
|
luaL_setmetatable(L, API_TYPE_PROCESS);
|
||||||
self->deadline = deadline;
|
self->deadline = deadline;
|
||||||
|
self->detached = detach;
|
||||||
#if _WIN32
|
#if _WIN32
|
||||||
for (int i = 0; i < 3; ++i) {
|
for (int i = 0; i < 3; ++i) {
|
||||||
switch (new_fds[i]) {
|
switch (new_fds[i]) {
|
||||||
|
@ -278,6 +279,7 @@ static int process_start(lua_State* L) {
|
||||||
retval = luaL_error(L, "Error running fork: %s.", strerror(errno));
|
retval = luaL_error(L, "Error running fork: %s.", strerror(errno));
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
} else if (!self->pid) {
|
} else if (!self->pid) {
|
||||||
|
if (!detach)
|
||||||
setpgid(0,0);
|
setpgid(0,0);
|
||||||
for (int stream = 0; stream < 3; ++stream) {
|
for (int stream = 0; stream < 3; ++stream) {
|
||||||
if (new_fds[stream] == REDIRECT_DISCARD) { // Close the stream if we don't want it.
|
if (new_fds[stream] == REDIRECT_DISCARD) { // Close the stream if we don't want it.
|
||||||
|
@ -454,6 +456,7 @@ 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_interrupt(lua_State* L) { return self_signal(L, SIGNAL_INTERRUPT); }
|
||||||
static int f_gc(lua_State* L) {
|
static int f_gc(lua_State* L) {
|
||||||
process_t* self = (process_t*) luaL_checkudata(L, 1, API_TYPE_PROCESS);
|
process_t* self = (process_t*) luaL_checkudata(L, 1, API_TYPE_PROCESS);
|
||||||
|
if (!self->detached)
|
||||||
signal_process(self, SIGNAL_TERM);
|
signal_process(self, SIGNAL_TERM);
|
||||||
close_fd(&self->child_pipes[STDIN_FD ][1]);
|
close_fd(&self->child_pipes[STDIN_FD ][1]);
|
||||||
close_fd(&self->child_pipes[STDOUT_FD][0]);
|
close_fd(&self->child_pipes[STDOUT_FD][0]);
|
||||||
|
|
Loading…
Reference in New Issue