summaryrefslogtreecommitdiffstats
path: root/osdep/subprocess-posix.c
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2017-06-27 14:22:28 +0200
committerwm4 <wm4@nowhere>2017-06-29 10:31:13 +0200
commitcd25d98bfa38c87bd857264e876cd8be42eb3678 (patch)
treef721ae2930ac3ce823326592f986f4f4c20654e6 /osdep/subprocess-posix.c
parent7eca787571aab982acaadee79abb0f40f9f14b6a (diff)
downloadmpv-cd25d98bfa38c87bd857264e876cd8be42eb3678.tar.bz2
mpv-cd25d98bfa38c87bd857264e876cd8be42eb3678.tar.xz
Avoid calling close(-1)
While this is perfectly OK on Unix, it causes annoying valgrind warnings, and might be otherwise confusing to others. On Windows, the runtime can actually abort the process if this is called. push.c part taken from a patch by Pedro Pombeiro.
Diffstat (limited to 'osdep/subprocess-posix.c')
-rw-r--r--osdep/subprocess-posix.c27
1 files changed, 12 insertions, 15 deletions
diff --git a/osdep/subprocess-posix.c b/osdep/subprocess-posix.c
index 163559edbc..c780473fce 100644
--- a/osdep/subprocess-posix.c
+++ b/osdep/subprocess-posix.c
@@ -32,6 +32,8 @@
extern char **environ;
+#define SAFE_CLOSE(fd) do { if ((fd) >= 0) close((fd)); (fd) = -1; } while (0)
+
// A silly helper: automatically skips entries with negative FDs
static int sparse_poll(struct pollfd *fds, int num_fds, int timeout)
{
@@ -93,12 +95,9 @@ int mp_subprocess(char **args, struct mp_cancel *cancel, void *ctx,
}
spawned = true;
- close(p_stdout[1]);
- p_stdout[1] = -1;
- close(p_stderr[1]);
- p_stderr[1] = -1;
- close(devnull);
- devnull = -1;
+ SAFE_CLOSE(p_stdout[1]);
+ SAFE_CLOSE(p_stderr[1]);
+ SAFE_CLOSE(devnull);
int *read_fds[2] = {&p_stdout[0], &p_stderr[0]};
subprocess_read_cb read_cbs[2] = {on_stdout, on_stderr};
@@ -119,10 +118,8 @@ int mp_subprocess(char **args, struct mp_cancel *cancel, void *ctx,
continue;
if (r > 0 && read_cbs[n])
read_cbs[n](ctx, buf, r);
- if (r <= 0) {
- close(*read_fds[n]);
- *read_fds[n] = -1;
- }
+ if (r <= 0)
+ SAFE_CLOSE(*read_fds[n]);
}
}
if (fds[2].revents) {
@@ -141,11 +138,11 @@ int mp_subprocess(char **args, struct mp_cancel *cancel, void *ctx,
done:
if (fa_destroy)
posix_spawn_file_actions_destroy(&fa);
- close(p_stdout[0]);
- close(p_stdout[1]);
- close(p_stderr[0]);
- close(p_stderr[1]);
- close(devnull);
+ SAFE_CLOSE(p_stdout[0]);
+ SAFE_CLOSE(p_stdout[1]);
+ SAFE_CLOSE(p_stderr[0]);
+ SAFE_CLOSE(p_stderr[1]);
+ SAFE_CLOSE(devnull);
if (!spawned || (WIFEXITED(status) && WEXITSTATUS(status) == 127)) {
*error = "init";