diff options
Diffstat (limited to 'player')
-rw-r--r-- | player/command.c | 57 |
1 files changed, 35 insertions, 22 deletions
diff --git a/player/command.c b/player/command.c index bd63746669..ddf4768a11 100644 --- a/player/command.c +++ b/player/command.c @@ -63,11 +63,9 @@ #if HAVE_SYS_MMAN_H #include <sys/mman.h> #endif -#ifndef __MINGW32__ -#include <sys/wait.h> -#endif #include "osdep/io.h" +#include "osdep/subprocess.h" #include "core.h" @@ -3841,6 +3839,39 @@ static void overlay_uninit(struct MPContext *mpctx) osd_set_external2(mpctx->osd, NULL); } +struct subprocess_args { + struct mp_log *log; + char **args; +}; + +static void *run_subprocess(void *ptr) +{ + struct subprocess_args *p = ptr; + pthread_detach(pthread_self()); + + mp_msg_flush_status_line(p->log); + + char *err = NULL; + if (mp_subprocess(p->args, NULL, NULL, NULL, NULL, &err) < 0) + mp_err(p->log, "Running subprocess failed: %s\n", err); + + talloc_free(p); + return NULL; +} + +static void subprocess_detached(struct mp_log *log, char **args) +{ + struct subprocess_args *p = talloc_zero(NULL, struct subprocess_args); + p->log = mp_log_new(p, log, NULL); + int num_args = 0; + for (int n = 0; args[n]; n++) + MP_TARRAY_APPEND(p, p->args, num_args, talloc_strdup(p, args[n])); + MP_TARRAY_APPEND(p, p->args, num_args, NULL); + pthread_t thread; + if (pthread_create(&thread, NULL, run_subprocess, p)) + talloc_free(p); +} + struct cycle_counter { char **args; int counter; @@ -4405,28 +4436,10 @@ int run_command(MPContext *mpctx, mp_cmd_t *cmd) break; case MP_CMD_RUN: { -#ifndef __MINGW32__ - mp_msg_flush_status_line(mpctx->global); char *args[MP_CMD_MAX_ARGS + 1] = {0}; for (int n = 0; n < cmd->nargs; n++) args[n] = cmd->args[n].v.s; - pid_t child = fork(); - if (child == 0) { - // Fork twice; the second process will be made child of pid 1 as - // soon as the first process exists, and we don't have to care - // about having to wait for the second process to terminate. - if (fork() == 0) { - execvp(args[0], args); - // mp_msg() is not safe to be called from a forked process. - char s[] = "Executing program failed.\n"; - write(2, s, sizeof(s) - 1); - _exit(1); - } - _exit(0); - } - int st; - while (child != -1 && waitpid(child, &st, 0) < 0 && errno == EINTR) {} -#endif + subprocess_detached(mpctx->log, args); break; } |