path: root/osdep/subprocess.h
diff options
authorwm4 <wm4@nowhere>2020-02-16 21:27:34 +0100
committerwm4 <wm4@nowhere>2020-02-16 21:27:34 +0100
commit92fee4ebc4852f023527dc64b28e5b10e4507053 (patch)
tree4d4e8c72a52384689cd7531132dbc3d861571661 /osdep/subprocess.h
parenta4b12c54b64f31e005adb8b07d7c45876e91f0f5 (diff)
subprocess: change to a fancier API
Introduce mp_subprocess() and related definitions. This is a bit more flexible than the old stuff. This may or may not be used for a more complicated feature that involves starting processes, and which would require more control. Only port subprocess-posix.c to this API. The player still uses the "old" API, so for win32 and dummy implementations, the new API is simply not available, while for POSIX, the old APIs are emulated on top of the new one. I'm hoping the win32 code can be ported as well, so the ifdefs in subprocess.c can be dropped, and the player can (if convenient or needed) use the new API.
Diffstat (limited to 'osdep/subprocess.h')
1 files changed, 46 insertions, 2 deletions
diff --git a/osdep/subprocess.h b/osdep/subprocess.h
index f272e1ad42..6aa2981f1d 100644
--- a/osdep/subprocess.h
+++ b/osdep/subprocess.h
@@ -18,7 +18,9 @@
+#include <stdbool.h>
#include <stddef.h>
+#include <stdint.h>
struct mp_cancel;
@@ -26,12 +28,54 @@ typedef void (*subprocess_read_cb)(void *ctx, char *data, size_t size);
void mp_devnull(void *ctx, char *data, size_t size);
+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, ...)
+ 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)
+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
+ 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);
// Start a subprocess. Uses callbacks to read from stdout and stderr.
+// Returns any of MP_SUBPROCESS_*, or a value >=0 for the process exir
int mp_subprocess(char **args, struct mp_cancel *cancel, void *ctx,
subprocess_read_cb on_stdout, subprocess_read_cb on_stderr,
char **error);
-// mp_subprocess return values. -1 is a generic error code.
struct mp_log;
void mp_subprocess_detached(struct mp_log *log, char **args);