From 9d8573cfe85fa1810ec6a18c584e920b47da1dad Mon Sep 17 00:00:00 2001 From: wm4 Date: Sat, 30 Nov 2013 22:47:17 +0100 Subject: Prevent creating zombies The "run" input command does fork+exec to spawn child processes. But it doesn't cleanup the child processes, so they are left as zombies until mpv terminates. Leaving zombie processes around is not very nice, so employ a simple trick to let pid 1 take care of this: we fork twice, and when the first fork exits, the second fork becomes orphaned and becomes pid 1's child. It becomes pid 1's responsibility to cleanup the process. The advantage is that we don't need extra logic to cleanup the spawned process, which could have an arbitrary lifetime. This is e.g. described here: http://yarchive.net/comp/zombie_process.html Also use _exit() instead of exit(). It's not really sane to run cleanup handlers (atexit() etc.) inside a forked process. --- mpvcore/player/command.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/mpvcore/player/command.c b/mpvcore/player/command.c index 129c0aa58a..ae1ba3ca31 100644 --- a/mpvcore/player/command.c +++ b/mpvcore/player/command.c @@ -23,6 +23,8 @@ #include #include #include +#include +#include #include #include @@ -3028,10 +3030,19 @@ void run_command(MPContext *mpctx, mp_cmd_t *cmd) char *args[MP_CMD_MAX_ARGS + 1] = {0}; for (int n = 0; n < cmd->nargs; n++) args[n] = cmd->args[n].v.s; - if (!fork()) { - execvp(args[0], args); - exit(0); + 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); + _exit(0); + } + _exit(0); } + int st; + while (child != -1 && waitpid(child, &st, 0) < 0 && errno == EINTR) {} #endif break; } -- cgit v1.2.3