2014-11-22 06:21:33 +00:00
|
|
|
/*
|
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
Relicense some non-MPlayer source files to LGPL 2.1 or later
This covers source files which were added in mplayer2 and mpv times
only, and where all code is covered by LGPL relicensing agreements.
There are probably more files to which this applies, but I'm being
conservative here.
A file named ao_sdl.c exists in MPlayer too, but the mpv one is a
complete rewrite, and was added some time after the original ao_sdl.c
was removed. The same applies to vo_sdl.c, for which the SDL2 API is
radically different in addition (MPlayer supports SDL 1.2 only).
common.c contains only code written by me. But common.h is a strange
case: although it originally was named mp_common.h and exists in MPlayer
too, by now it contains only definitions written by uau and me. The
exceptions are the CONTROL_ defines - thus not changing the license of
common.h yet.
codec_tags.c contained once large tables generated from MPlayer's
codecs.conf, but all of these tables were removed.
From demux_playlist.c I'm removing a code fragment from someone who was
not asked; this probably could be done later (see commit 15dccc37).
misc.c is a bit complicated to reason about (it was split off mplayer.c
and thus contains random functions out of this file), but actually all
functions have been added post-MPlayer. Except get_relative_time(),
which was written by uau, but looks similar to 3 different versions of
something similar in each of the Unix/win32/OSX timer source files. I'm
not sure what that means in regards to copyright, so I've just moved it
into another still-GPL source file for now.
screenshot.c once had some minor parts of MPlayer's vf_screenshot.c, but
they're all gone.
2016-01-19 17:36:06 +00:00
|
|
|
* mpv is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2014-11-22 06:21:33 +00:00
|
|
|
*
|
|
|
|
* mpv is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Relicense some non-MPlayer source files to LGPL 2.1 or later
This covers source files which were added in mplayer2 and mpv times
only, and where all code is covered by LGPL relicensing agreements.
There are probably more files to which this applies, but I'm being
conservative here.
A file named ao_sdl.c exists in MPlayer too, but the mpv one is a
complete rewrite, and was added some time after the original ao_sdl.c
was removed. The same applies to vo_sdl.c, for which the SDL2 API is
radically different in addition (MPlayer supports SDL 1.2 only).
common.c contains only code written by me. But common.h is a strange
case: although it originally was named mp_common.h and exists in MPlayer
too, by now it contains only definitions written by uau and me. The
exceptions are the CONTROL_ defines - thus not changing the license of
common.h yet.
codec_tags.c contained once large tables generated from MPlayer's
codecs.conf, but all of these tables were removed.
From demux_playlist.c I'm removing a code fragment from someone who was
not asked; this probably could be done later (see commit 15dccc37).
misc.c is a bit complicated to reason about (it was split off mplayer.c
and thus contains random functions out of this file), but actually all
functions have been added post-MPlayer. Except get_relative_time(),
which was written by uau, but looks similar to 3 different versions of
something similar in each of the Unix/win32/OSX timer source files. I'm
not sure what that means in regards to copyright, so I've just moved it
into another still-GPL source file for now.
screenshot.c once had some minor parts of MPlayer's vf_screenshot.c, but
they're all gone.
2016-01-19 17:36:06 +00:00
|
|
|
* GNU Lesser General Public License for more details.
|
2014-11-22 06:21:33 +00:00
|
|
|
*
|
Relicense some non-MPlayer source files to LGPL 2.1 or later
This covers source files which were added in mplayer2 and mpv times
only, and where all code is covered by LGPL relicensing agreements.
There are probably more files to which this applies, but I'm being
conservative here.
A file named ao_sdl.c exists in MPlayer too, but the mpv one is a
complete rewrite, and was added some time after the original ao_sdl.c
was removed. The same applies to vo_sdl.c, for which the SDL2 API is
radically different in addition (MPlayer supports SDL 1.2 only).
common.c contains only code written by me. But common.h is a strange
case: although it originally was named mp_common.h and exists in MPlayer
too, by now it contains only definitions written by uau and me. The
exceptions are the CONTROL_ defines - thus not changing the license of
common.h yet.
codec_tags.c contained once large tables generated from MPlayer's
codecs.conf, but all of these tables were removed.
From demux_playlist.c I'm removing a code fragment from someone who was
not asked; this probably could be done later (see commit 15dccc37).
misc.c is a bit complicated to reason about (it was split off mplayer.c
and thus contains random functions out of this file), but actually all
functions have been added post-MPlayer. Except get_relative_time(),
which was written by uau, but looks similar to 3 different versions of
something similar in each of the Unix/win32/OSX timer source files. I'm
not sure what that means in regards to copyright, so I've just moved it
into another still-GPL source file for now.
screenshot.c once had some minor parts of MPlayer's vf_screenshot.c, but
they're all gone.
2016-01-19 17:36:06 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
|
2014-11-22 06:21:33 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <spawn.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
2015-04-15 20:42:20 +00:00
|
|
|
#include <fcntl.h>
|
2014-11-22 06:21:33 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
2014-11-22 13:38:58 +00:00
|
|
|
#include "osdep/subprocess.h"
|
|
|
|
|
2014-11-22 06:21:33 +00:00
|
|
|
#include "osdep/io.h"
|
|
|
|
#include "common/common.h"
|
2014-11-22 13:38:58 +00:00
|
|
|
#include "stream/stream.h"
|
2014-11-22 06:21:33 +00:00
|
|
|
|
|
|
|
extern char **environ;
|
|
|
|
|
2017-06-27 12:22:28 +00:00
|
|
|
#define SAFE_CLOSE(fd) do { if ((fd) >= 0) close((fd)); (fd) = -1; } while (0)
|
|
|
|
|
2014-11-22 06:21:33 +00:00
|
|
|
// A silly helper: automatically skips entries with negative FDs
|
|
|
|
static int sparse_poll(struct pollfd *fds, int num_fds, int timeout)
|
|
|
|
{
|
|
|
|
struct pollfd p_fds[10];
|
|
|
|
int map[10];
|
|
|
|
if (num_fds > MP_ARRAY_SIZE(p_fds))
|
|
|
|
return -1;
|
|
|
|
int p_num_fds = 0;
|
|
|
|
for (int n = 0; n < num_fds; n++) {
|
|
|
|
map[n] = -1;
|
|
|
|
if (fds[n].fd < 0)
|
|
|
|
continue;
|
|
|
|
map[n] = p_num_fds;
|
|
|
|
p_fds[p_num_fds++] = fds[n];
|
|
|
|
}
|
|
|
|
int r = poll(p_fds, p_num_fds, timeout);
|
|
|
|
for (int n = 0; n < num_fds; n++)
|
|
|
|
fds[n].revents = (map[n] < 0 && r >= 0) ? 0 : p_fds[map[n]].revents;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
int mp_subprocess(char **args, struct mp_cancel *cancel, void *ctx,
|
|
|
|
subprocess_read_cb on_stdout, subprocess_read_cb on_stderr,
|
|
|
|
char **error)
|
|
|
|
{
|
|
|
|
posix_spawn_file_actions_t fa;
|
|
|
|
bool fa_destroy = false;
|
|
|
|
int status = -1;
|
|
|
|
int p_stdout[2] = {-1, -1};
|
|
|
|
int p_stderr[2] = {-1, -1};
|
2015-04-15 20:42:20 +00:00
|
|
|
int devnull = -1;
|
2014-11-22 06:21:33 +00:00
|
|
|
pid_t pid = -1;
|
2015-06-27 19:08:55 +00:00
|
|
|
bool spawned = false;
|
|
|
|
bool killed_by_us = false;
|
2014-11-22 06:21:33 +00:00
|
|
|
|
2015-01-01 19:04:38 +00:00
|
|
|
if (on_stdout && mp_make_cloexec_pipe(p_stdout) < 0)
|
2014-11-22 06:21:33 +00:00
|
|
|
goto done;
|
2015-01-01 19:04:38 +00:00
|
|
|
if (on_stderr && mp_make_cloexec_pipe(p_stderr) < 0)
|
2014-11-22 06:21:33 +00:00
|
|
|
goto done;
|
|
|
|
|
2015-04-15 20:42:20 +00:00
|
|
|
devnull = open("/dev/null", O_RDONLY | O_CLOEXEC);
|
|
|
|
if (devnull < 0)
|
|
|
|
goto done;
|
|
|
|
|
2014-11-22 06:21:33 +00:00
|
|
|
if (posix_spawn_file_actions_init(&fa))
|
|
|
|
goto done;
|
|
|
|
fa_destroy = true;
|
2015-04-15 20:42:20 +00:00
|
|
|
// redirect stdin/stdout/stderr
|
|
|
|
if (posix_spawn_file_actions_adddup2(&fa, devnull, 0))
|
|
|
|
goto done;
|
2015-01-01 19:04:38 +00:00
|
|
|
if (p_stdout[1] >= 0 && posix_spawn_file_actions_adddup2(&fa, p_stdout[1], 1))
|
2014-11-22 06:21:33 +00:00
|
|
|
goto done;
|
2015-01-01 19:04:38 +00:00
|
|
|
if (p_stderr[1] >= 0 && posix_spawn_file_actions_adddup2(&fa, p_stderr[1], 2))
|
2014-11-22 06:21:33 +00:00
|
|
|
goto done;
|
|
|
|
|
|
|
|
if (posix_spawnp(&pid, args[0], &fa, NULL, args, environ)) {
|
|
|
|
pid = -1;
|
|
|
|
goto done;
|
|
|
|
}
|
2015-06-27 19:08:55 +00:00
|
|
|
spawned = true;
|
2014-11-22 06:21:33 +00:00
|
|
|
|
2017-06-27 12:22:28 +00:00
|
|
|
SAFE_CLOSE(p_stdout[1]);
|
|
|
|
SAFE_CLOSE(p_stderr[1]);
|
|
|
|
SAFE_CLOSE(devnull);
|
2014-11-22 06:21:33 +00:00
|
|
|
|
|
|
|
int *read_fds[2] = {&p_stdout[0], &p_stderr[0]};
|
|
|
|
subprocess_read_cb read_cbs[2] = {on_stdout, on_stderr};
|
|
|
|
|
|
|
|
while (p_stdout[0] >= 0 || p_stderr[0] >= 0) {
|
|
|
|
struct pollfd fds[] = {
|
|
|
|
{.events = POLLIN, .fd = *read_fds[0]},
|
|
|
|
{.events = POLLIN, .fd = *read_fds[1]},
|
|
|
|
{.events = POLLIN, .fd = cancel ? mp_cancel_get_fd(cancel) : -1},
|
|
|
|
};
|
|
|
|
if (sparse_poll(fds, MP_ARRAY_SIZE(fds), -1) < 0 && errno != EINTR)
|
|
|
|
break;
|
|
|
|
for (int n = 0; n < 2; n++) {
|
|
|
|
if (fds[n].revents) {
|
|
|
|
char buf[4096];
|
|
|
|
ssize_t r = read(*read_fds[n], buf, sizeof(buf));
|
|
|
|
if (r < 0 && errno == EINTR)
|
|
|
|
continue;
|
|
|
|
if (r > 0 && read_cbs[n])
|
|
|
|
read_cbs[n](ctx, buf, r);
|
2017-06-27 12:22:28 +00:00
|
|
|
if (r <= 0)
|
|
|
|
SAFE_CLOSE(*read_fds[n]);
|
2014-11-22 06:21:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fds[2].revents) {
|
|
|
|
kill(pid, SIGKILL);
|
2015-06-27 19:08:55 +00:00
|
|
|
killed_by_us = true;
|
2014-11-22 06:21:33 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: it can happen that a child process closes the pipe, but does not
|
|
|
|
// terminate yet. In this case, we would have to run waitpid() in
|
|
|
|
// a separate thread and use pthread_cancel(), or use other weird
|
|
|
|
// and laborious tricks. So this isn't handled yet.
|
|
|
|
while (waitpid(pid, &status, 0) < 0 && errno == EINTR) {}
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (fa_destroy)
|
|
|
|
posix_spawn_file_actions_destroy(&fa);
|
2017-06-27 12:22:28 +00:00
|
|
|
SAFE_CLOSE(p_stdout[0]);
|
|
|
|
SAFE_CLOSE(p_stdout[1]);
|
|
|
|
SAFE_CLOSE(p_stderr[0]);
|
|
|
|
SAFE_CLOSE(p_stderr[1]);
|
|
|
|
SAFE_CLOSE(devnull);
|
2014-11-22 06:21:33 +00:00
|
|
|
|
2015-06-27 19:08:55 +00:00
|
|
|
if (!spawned || (WIFEXITED(status) && WEXITSTATUS(status) == 127)) {
|
|
|
|
*error = "init";
|
|
|
|
status = -1;
|
|
|
|
} else if (WIFEXITED(status)) {
|
2014-11-22 06:21:33 +00:00
|
|
|
*error = NULL;
|
|
|
|
status = WEXITSTATUS(status);
|
|
|
|
} else {
|
2015-06-27 19:08:55 +00:00
|
|
|
*error = "killed";
|
|
|
|
status = killed_by_us ? MP_SUBPROCESS_EKILLED_BY_US : -1;
|
2014-11-22 06:21:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|