^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #ifndef __SUBCMD_RUN_COMMAND_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #define __SUBCMD_RUN_COMMAND_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) ERR_RUN_COMMAND_FORK = 10000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) ERR_RUN_COMMAND_EXEC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) ERR_RUN_COMMAND_PIPE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) ERR_RUN_COMMAND_WAITPID,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) ERR_RUN_COMMAND_WAITPID_WRONG_PID,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) ERR_RUN_COMMAND_WAITPID_SIGNAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) ERR_RUN_COMMAND_WAITPID_NOEXIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #define IS_RUN_COMMAND_ERR(x) (-(x) >= ERR_RUN_COMMAND_FORK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) struct child_process {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) const char **argv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) pid_t pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * Using .in, .out, .err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * - Specify 0 for no redirections (child inherits stdin, stdout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * stderr from parent).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * - Specify -1 to have a pipe allocated as follows:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * .in: returns the writable pipe end; parent writes to it,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * the readable pipe end becomes child's stdin
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * .out, .err: returns the readable pipe end; parent reads from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * it, the writable pipe end becomes child's stdout/stderr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * The caller of start_command() must close the returned FDs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * after it has completed reading from/writing to it!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * - Specify > 0 to set a channel to a particular FD as follows:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * .in: a readable FD, becomes child's stdin
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * .out: a writable FD, becomes child's stdout/stderr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * .err > 0 not supported
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * The specified FD is closed by start_command(), even in case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * of errors!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) int in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) int out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) const char *dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) const char *const *env;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) unsigned no_stdin:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) unsigned no_stdout:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) unsigned no_stderr:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) unsigned exec_cmd:1; /* if this is to be external sub-command */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) unsigned stdout_to_stderr:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) void (*preexec_cb)(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) int start_command(struct child_process *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) int finish_command(struct child_process *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) int run_command(struct child_process *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #define RUN_COMMAND_NO_STDIN 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) #define RUN_EXEC_CMD 2 /*If this is to be external sub-command */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) #define RUN_COMMAND_STDOUT_TO_STDERR 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) int run_command_v_opt(const char **argv, int opt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) #endif /* __SUBCMD_RUN_COMMAND_H */