subprocess, lua: export whether the process was killed by us

We want to distinguish actual errors, and just aborting the program
intentionally.

Also be a bit more careful with handling the wait() exit status: do not
called WEXITSTATUS() without checking WIFEXITED() first.
This commit is contained in:
wm4 2015-06-27 21:08:55 +02:00
parent 6ffb1e2b66
commit 03c70a8d81
5 changed files with 19 additions and 3 deletions

View File

@ -630,6 +630,10 @@ strictly part of the guaranteed API.
On Windows, ``killed`` is only returned when the process has been
killed by mpv as a result of ``cancellable`` being set to ``true``.
``killed_by_us``
Set to ``true`` if the process has been killed by mpv as a result
of ``cancellable`` being set to ``true``.
In all cases, ``mp.resume_all()`` is implicitly called.
``utils.parse_json(str [, trail])``

View File

@ -64,6 +64,8 @@ int mp_subprocess(char **args, struct mp_cancel *cancel, void *ctx,
int p_stderr[2] = {-1, -1};
int devnull = -1;
pid_t pid = -1;
bool spawned = false;
bool killed_by_us = false;
if (on_stdout && mp_make_cloexec_pipe(p_stdout) < 0)
goto done;
@ -89,6 +91,7 @@ int mp_subprocess(char **args, struct mp_cancel *cancel, void *ctx,
pid = -1;
goto done;
}
spawned = true;
close(p_stdout[1]);
p_stdout[1] = -1;
@ -124,6 +127,7 @@ int mp_subprocess(char **args, struct mp_cancel *cancel, void *ctx,
}
if (fds[2].revents) {
kill(pid, SIGKILL);
killed_by_us = true;
break;
}
}
@ -143,12 +147,15 @@ done:
close(p_stderr[1]);
close(devnull);
if (WIFEXITED(status) && WEXITSTATUS(status) != 127) {
if (!spawned || (WIFEXITED(status) && WEXITSTATUS(status) == 127)) {
*error = "init";
status = -1;
} else if (WIFEXITED(status)) {
*error = NULL;
status = WEXITSTATUS(status);
} else {
*error = WEXITSTATUS(status) == 127 ? "init" : "killed";
status = -1;
*error = "killed";
status = killed_by_us ? MP_SUBPROCESS_EKILLED_BY_US : -1;
}
return status;

View File

@ -357,6 +357,7 @@ int mp_subprocess(char **args, struct mp_cancel *cancel, void *ctx,
if (pi.hProcess) {
TerminateProcess(pi.hProcess, 1);
*error = "killed";
status = MP_SUBPROCESS_EKILLED_BY_US;
goto done;
}
break;

View File

@ -28,6 +28,8 @@ typedef void (*subprocess_read_cb)(void *ctx, char *data, size_t size);
int mp_subprocess(char **args, struct mp_cancel *cancel, void *ctx,
subprocess_read_cb on_stdout, subprocess_read_cb on_stderr,
char **error);
// mp_subprocess return values. -1 is a generic error code.
#define MP_SUBPROCESS_EKILLED_BY_US -2
struct mp_log;
void mp_subprocess_detached(struct mp_log *log, char **args);

View File

@ -1243,6 +1243,8 @@ static int script_subprocess(lua_State *L)
lua_setfield(L, -2, "status"); // res
lua_pushlstring(L, cb_ctx.output.start, cb_ctx.output.len); // res d
lua_setfield(L, -2, "stdout"); // res
lua_pushboolean(L, status == MP_SUBPROCESS_EKILLED_BY_US); // res b
lua_setfield(L, -2, "killed_by_us"); // res
return 1;
}