1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */ 21843b4e0SJosh Poimboeuf #ifndef __SUBCMD_RUN_COMMAND_H 31843b4e0SJosh Poimboeuf #define __SUBCMD_RUN_COMMAND_H 44b6ab94eSJosh Poimboeuf 54b6ab94eSJosh Poimboeuf #include <unistd.h> 64b6ab94eSJosh Poimboeuf 74b6ab94eSJosh Poimboeuf enum { 84b6ab94eSJosh Poimboeuf ERR_RUN_COMMAND_FORK = 10000, 94b6ab94eSJosh Poimboeuf ERR_RUN_COMMAND_EXEC, 104b6ab94eSJosh Poimboeuf ERR_RUN_COMMAND_PIPE, 114b6ab94eSJosh Poimboeuf ERR_RUN_COMMAND_WAITPID, 124b6ab94eSJosh Poimboeuf ERR_RUN_COMMAND_WAITPID_WRONG_PID, 134b6ab94eSJosh Poimboeuf ERR_RUN_COMMAND_WAITPID_SIGNAL, 144b6ab94eSJosh Poimboeuf ERR_RUN_COMMAND_WAITPID_NOEXIT, 154b6ab94eSJosh Poimboeuf }; 164b6ab94eSJosh Poimboeuf #define IS_RUN_COMMAND_ERR(x) (-(x) >= ERR_RUN_COMMAND_FORK) 174b6ab94eSJosh Poimboeuf 184b6ab94eSJosh Poimboeuf struct child_process { 194b6ab94eSJosh Poimboeuf const char **argv; 204b6ab94eSJosh Poimboeuf pid_t pid; 214b6ab94eSJosh Poimboeuf /* 224b6ab94eSJosh Poimboeuf * Using .in, .out, .err: 234b6ab94eSJosh Poimboeuf * - Specify 0 for no redirections (child inherits stdin, stdout, 244b6ab94eSJosh Poimboeuf * stderr from parent). 254b6ab94eSJosh Poimboeuf * - Specify -1 to have a pipe allocated as follows: 264b6ab94eSJosh Poimboeuf * .in: returns the writable pipe end; parent writes to it, 274b6ab94eSJosh Poimboeuf * the readable pipe end becomes child's stdin 284b6ab94eSJosh Poimboeuf * .out, .err: returns the readable pipe end; parent reads from 294b6ab94eSJosh Poimboeuf * it, the writable pipe end becomes child's stdout/stderr 304b6ab94eSJosh Poimboeuf * The caller of start_command() must close the returned FDs 314b6ab94eSJosh Poimboeuf * after it has completed reading from/writing to it! 324b6ab94eSJosh Poimboeuf * - Specify > 0 to set a channel to a particular FD as follows: 334b6ab94eSJosh Poimboeuf * .in: a readable FD, becomes child's stdin 344b6ab94eSJosh Poimboeuf * .out: a writable FD, becomes child's stdout/stderr 354b6ab94eSJosh Poimboeuf * .err > 0 not supported 364b6ab94eSJosh Poimboeuf * The specified FD is closed by start_command(), even in case 374b6ab94eSJosh Poimboeuf * of errors! 384b6ab94eSJosh Poimboeuf */ 394b6ab94eSJosh Poimboeuf int in; 404b6ab94eSJosh Poimboeuf int out; 414b6ab94eSJosh Poimboeuf int err; 424b6ab94eSJosh Poimboeuf const char *dir; 434b6ab94eSJosh Poimboeuf const char *const *env; 44*705c09bbSIan Rogers int finish_result; 454b6ab94eSJosh Poimboeuf unsigned no_stdin:1; 464b6ab94eSJosh Poimboeuf unsigned no_stdout:1; 474b6ab94eSJosh Poimboeuf unsigned no_stderr:1; 484b6ab94eSJosh Poimboeuf unsigned exec_cmd:1; /* if this is to be external sub-command */ 494b6ab94eSJosh Poimboeuf unsigned stdout_to_stderr:1; 50*705c09bbSIan Rogers unsigned finished:1; 514b6ab94eSJosh Poimboeuf void (*preexec_cb)(void); 521a562c0dSIan Rogers /* If set, call function in child rather than doing an exec. */ 531a562c0dSIan Rogers int (*no_exec_cmd)(struct child_process *process); 544b6ab94eSJosh Poimboeuf }; 554b6ab94eSJosh Poimboeuf 564b6ab94eSJosh Poimboeuf int start_command(struct child_process *); 57*705c09bbSIan Rogers int check_if_command_finished(struct child_process *); 584b6ab94eSJosh Poimboeuf int finish_command(struct child_process *); 594b6ab94eSJosh Poimboeuf int run_command(struct child_process *); 604b6ab94eSJosh Poimboeuf 614b6ab94eSJosh Poimboeuf #define RUN_COMMAND_NO_STDIN 1 624b6ab94eSJosh Poimboeuf #define RUN_EXEC_CMD 2 /*If this is to be external sub-command */ 634b6ab94eSJosh Poimboeuf #define RUN_COMMAND_STDOUT_TO_STDERR 4 644b6ab94eSJosh Poimboeuf int run_command_v_opt(const char **argv, int opt); 654b6ab94eSJosh Poimboeuf 661843b4e0SJosh Poimboeuf #endif /* __SUBCMD_RUN_COMMAND_H */ 67