summaryrefslogtreecommitdiffstats
path: root/osdep/subprocess.h
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2020-02-16 21:57:17 +0100
committerwm4 <wm4@nowhere>2020-02-16 22:08:48 +0100
commitf2c7c641b38d060a7eea52a92bc0239f1603bffb (patch)
treed6f6df3c7a8325edf6c22d45c846d022f50e57a7 /osdep/subprocess.h
parent92fee4ebc4852f023527dc64b28e5b10e4507053 (diff)
downloadmpv-f2c7c641b38d060a7eea52a92bc0239f1603bffb.tar.bz2
mpv-f2c7c641b38d060a7eea52a92bc0239f1603bffb.tar.xz
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.
Diffstat (limited to 'osdep/subprocess.h')
-rw-r--r--osdep/subprocess.h2
1 files changed, 2 insertions, 0 deletions
diff --git a/osdep/subprocess.h b/osdep/subprocess.h
index 6aa2981f1d..ea9c43ba34 100644
--- a/osdep/subprocess.h
+++ b/osdep/subprocess.h
@@ -48,12 +48,14 @@ struct mp_subprocess_opts {
struct mp_subprocess_fd fds[MP_SUBPROCESS_MAX_FDS];
int num_fds;
struct mp_cancel *cancel; // if !NULL, asynchronous process abort (kills it)
+ bool detach; // if true, do not wait for process to end
};
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
+ // if started with detach==true, this is always 0
uint32_t exit_status; // if error==0==MP_SUBPROCESS_OK, 0 otherwise
};