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
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef MP_SUBPROCESS_H_
|
|
|
|
#define MP_SUBPROCESS_H_
|
|
|
|
|
2020-02-16 20:27:34 +00:00
|
|
|
#include <stdbool.h>
|
2014-11-22 13:38:58 +00:00
|
|
|
#include <stddef.h>
|
2020-02-16 20:27:34 +00:00
|
|
|
#include <stdint.h>
|
2014-11-22 13:38:58 +00:00
|
|
|
|
2020-08-16 00:54:44 +00:00
|
|
|
#include "misc/bstr.h"
|
|
|
|
|
2014-11-22 13:38:58 +00:00
|
|
|
struct mp_cancel;
|
2014-11-22 06:21:33 +00:00
|
|
|
|
2020-08-16 00:54:44 +00:00
|
|
|
// Incrementally called with data that was read. Buffer valid only during call.
|
|
|
|
// size==0 means EOF.
|
2014-11-22 06:21:33 +00:00
|
|
|
typedef void (*subprocess_read_cb)(void *ctx, char *data, size_t size);
|
2020-08-16 00:54:44 +00:00
|
|
|
// Incrementally called to refill *mp_subprocess_fd.write_buf, whenever write_buf
|
|
|
|
// has length 0 and the pipe is writable. While writing, *write_buf is adjusted
|
|
|
|
// to contain only the not yet written data.
|
|
|
|
// Not filling the buffer means EOF.
|
|
|
|
typedef void (*subprocess_write_cb)(void *ctx);
|
2014-11-22 06:21:33 +00:00
|
|
|
|
2016-07-02 11:51:56 +00:00
|
|
|
void mp_devnull(void *ctx, char *data, size_t size);
|
|
|
|
|
2020-02-16 20:27:34 +00:00
|
|
|
#define MP_SUBPROCESS_MAX_FDS 10
|
|
|
|
|
|
|
|
struct mp_subprocess_fd {
|
|
|
|
int fd; // target FD
|
|
|
|
|
|
|
|
// Only one of on_read or src_fd can be set. If none are set, use /dev/null.
|
|
|
|
// Note: "neutral" initialization requires setting src_fd=-1.
|
|
|
|
subprocess_read_cb on_read; // if not NULL, serve reads
|
|
|
|
void *on_read_ctx; // for on_read(on_read_ctx, ...)
|
2020-08-16 00:54:44 +00:00
|
|
|
subprocess_write_cb on_write; // if not NULL, serve writes
|
|
|
|
void *on_write_ctx; // for on_write(on_write_ctx, ...)
|
|
|
|
bstr *write_buf; // must be !=NULL if on_write is set
|
2020-02-16 20:27:34 +00:00
|
|
|
int src_fd; // if >=0, dup this FD to target FD
|
|
|
|
};
|
|
|
|
|
|
|
|
struct mp_subprocess_opts {
|
|
|
|
char *exe; // binary to execute (never non-NULL)
|
|
|
|
char **args; // argument list (NULL for none, otherwise NULL-terminated)
|
|
|
|
char **env; // if !NULL, set this as environment variable block
|
|
|
|
// Complete set of FDs passed down. All others are supposed to be closed.
|
|
|
|
struct mp_subprocess_fd fds[MP_SUBPROCESS_MAX_FDS];
|
|
|
|
int num_fds;
|
|
|
|
struct mp_cancel *cancel; // if !NULL, asynchronous process abort (kills it)
|
subprocess: implement proper detached processes on POSIX
The previous method for this sucked: for every launched detached
process, it started a thread, which then would leak if the launched
process didn't end before the player uninitialized. This was very racy
(although I bet the race condition wouldn't trigger in a 100 years), and
wasteful (threads aren't a cheap resource).
Implement it for POSIX directly. posix_spawn() has no direct support for
this, so we need to do it ourselves with fork(). We could probably do it
without fork(), and attempt to collect the PID in another thread. But
then we'd either have a waiting thread again, or we'd need to do an
unsafe waitpid(-1, ...) call. (POSIX process management sucks so badly,
how did they even manage this. Hopefully I'm just missing something, but
I'm not.) So now we depend on both posix_spawn() _and_ fork(), isn't it
fun?
Also call setsid(), to essentially detach the child process from the
terminal. (Otherwise it can receive various signals from the terminal,
which is probably not what you want.) posix_spawn() adds
POSIX_SPAWN_SETSID in newer POSIX releases, but we don't want to rely on
this yet.
The posix_spawnp() call is duplicated, but this is better than somehow
trying to unify the code paths.
Only somewhat tested, so enjoy the bugs.
2020-02-16 20:57:17 +00:00
|
|
|
bool detach; // if true, do not wait for process to end
|
2020-02-16 20:27:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct mp_subprocess_result {
|
|
|
|
int error; // one of MP_SUBPROCESS_* (>0 on error)
|
|
|
|
// NB: if WIFEXITED applies, error==0, and this is WEXITSTATUS
|
|
|
|
// on win32, this can use the full 32 bit
|
subprocess: implement proper detached processes on POSIX
The previous method for this sucked: for every launched detached
process, it started a thread, which then would leak if the launched
process didn't end before the player uninitialized. This was very racy
(although I bet the race condition wouldn't trigger in a 100 years), and
wasteful (threads aren't a cheap resource).
Implement it for POSIX directly. posix_spawn() has no direct support for
this, so we need to do it ourselves with fork(). We could probably do it
without fork(), and attempt to collect the PID in another thread. But
then we'd either have a waiting thread again, or we'd need to do an
unsafe waitpid(-1, ...) call. (POSIX process management sucks so badly,
how did they even manage this. Hopefully I'm just missing something, but
I'm not.) So now we depend on both posix_spawn() _and_ fork(), isn't it
fun?
Also call setsid(), to essentially detach the child process from the
terminal. (Otherwise it can receive various signals from the terminal,
which is probably not what you want.) posix_spawn() adds
POSIX_SPAWN_SETSID in newer POSIX releases, but we don't want to rely on
this yet.
The posix_spawnp() call is duplicated, but this is better than somehow
trying to unify the code paths.
Only somewhat tested, so enjoy the bugs.
2020-02-16 20:57:17 +00:00
|
|
|
// if started with detach==true, this is always 0
|
2020-02-16 20:27:34 +00:00
|
|
|
uint32_t exit_status; // if error==0==MP_SUBPROCESS_OK, 0 otherwise
|
|
|
|
};
|
|
|
|
|
|
|
|
// Subprocess error values.
|
|
|
|
#define MP_SUBPROCESS_OK 0 // no error
|
|
|
|
#define MP_SUBPROCESS_EGENERIC -1 // unknown error
|
|
|
|
#define MP_SUBPROCESS_EKILLED_BY_US -2 // mp_cancel was triggered
|
|
|
|
#define MP_SUBPROCESS_EINIT -3 // error during initialization
|
|
|
|
#define MP_SUBPROCESS_EUNSUPPORTED -4 // API not supported
|
|
|
|
|
|
|
|
// Turn MP_SUBPROCESS_* values into a static string. Never returns NULL.
|
|
|
|
const char *mp_subprocess_err_str(int num);
|
|
|
|
|
|
|
|
// Caller must set *opts.
|
|
|
|
void mp_subprocess2(struct mp_subprocess_opts *opts,
|
|
|
|
struct mp_subprocess_result *res);
|
|
|
|
|
2014-11-22 06:21:33 +00:00
|
|
|
#endif
|