1#ifndef RUN_COMMAND_H 2#define RUN_COMMAND_H 3 4#ifndef NO_PTHREADS 5#include <pthread.h> 6#endif 7 8#include"argv-array.h" 9 10struct child_process { 11const char**argv; 12struct argv_array args; 13struct argv_array env_array; 14 pid_t pid; 15/* 16 * Using .in, .out, .err: 17 * - Specify 0 for no redirections (child inherits stdin, stdout, 18 * stderr from parent). 19 * - Specify -1 to have a pipe allocated as follows: 20 * .in: returns the writable pipe end; parent writes to it, 21 * the readable pipe end becomes child's stdin 22 * .out, .err: returns the readable pipe end; parent reads from 23 * it, the writable pipe end becomes child's stdout/stderr 24 * The caller of start_command() must close the returned FDs 25 * after it has completed reading from/writing to it! 26 * - Specify > 0 to set a channel to a particular FD as follows: 27 * .in: a readable FD, becomes child's stdin 28 * .out: a writable FD, becomes child's stdout/stderr 29 * .err: a writable FD, becomes child's stderr 30 * The specified FD is closed by start_command(), even in case 31 * of errors! 32 */ 33int in; 34int out; 35int err; 36const char*dir; 37const char*const*env; 38unsigned no_stdin:1; 39unsigned no_stdout:1; 40unsigned no_stderr:1; 41unsigned git_cmd:1;/* if this is to be git sub-command */ 42unsigned silent_exec_failure:1; 43unsigned stdout_to_stderr:1; 44unsigned use_shell:1; 45unsigned clean_on_exit:1; 46}; 47 48#define CHILD_PROCESS_INIT { NULL, ARGV_ARRAY_INIT, ARGV_ARRAY_INIT } 49voidchild_process_init(struct child_process *); 50voidchild_process_clear(struct child_process *); 51 52intstart_command(struct child_process *); 53intfinish_command(struct child_process *); 54intfinish_command_in_signal(struct child_process *); 55intrun_command(struct child_process *); 56 57/* 58 * Returns the path to the hook file, or NULL if the hook is missing 59 * or disabled. Note that this points to static storage that will be 60 * overwritten by further calls to find_hook and run_hook_*. 61 */ 62externconst char*find_hook(const char*name); 63LAST_ARG_MUST_BE_NULL 64externintrun_hook_le(const char*const*env,const char*name, ...); 65externintrun_hook_ve(const char*const*env,const char*name,va_list args); 66 67#define RUN_COMMAND_NO_STDIN 1 68#define RUN_GIT_CMD 2/*If this is to be git sub-command */ 69#define RUN_COMMAND_STDOUT_TO_STDERR 4 70#define RUN_SILENT_EXEC_FAILURE 8 71#define RUN_USING_SHELL 16 72#define RUN_CLEAN_ON_EXIT 32 73intrun_command_v_opt(const char**argv,int opt); 74 75/* 76 * env (the environment) is to be formatted like environ: "VAR=VALUE". 77 * To unset an environment variable use just "VAR". 78 */ 79intrun_command_v_opt_cd_env(const char**argv,int opt,const char*dir,const char*const*env); 80 81/** 82 * Execute the given command, sending "in" to its stdin, and capturing its 83 * stdout and stderr in the "out" and "err" strbufs. Any of the three may 84 * be NULL to skip processing. 85 * 86 * Returns -1 if starting the command fails or reading fails, and otherwise 87 * returns the exit code of the command. Any output collected in the 88 * buffers is kept even if the command returns a non-zero exit. The hint fields 89 * gives starting sizes for the strbuf allocations. 90 * 91 * The fields of "cmd" should be set up as they would for a normal run_command 92 * invocation. But note that there is no need to set the in, out, or err 93 * fields; pipe_command handles that automatically. 94 */ 95intpipe_command(struct child_process *cmd, 96const char*in,size_t in_len, 97struct strbuf *out,size_t out_hint, 98struct strbuf *err,size_t err_hint); 99 100/** 101 * Convenience wrapper around pipe_command for the common case 102 * of capturing only stdout. 103 */ 104staticinlineintcapture_command(struct child_process *cmd, 105struct strbuf *out, 106size_t hint) 107{ 108returnpipe_command(cmd, NULL,0, out, hint, NULL,0); 109} 110 111/* 112 * The purpose of the following functions is to feed a pipe by running 113 * a function asynchronously and providing output that the caller reads. 114 * 115 * It is expected that no synchronization and mutual exclusion between 116 * the caller and the feed function is necessary so that the function 117 * can run in a thread without interfering with the caller. 118 */ 119struct async { 120/* 121 * proc reads from in; closes it before return 122 * proc writes to out; closes it before return 123 * returns 0 on success, non-zero on failure 124 */ 125int(*proc)(int in,int out,void*data); 126void*data; 127int in;/* caller writes here and closes it */ 128int out;/* caller reads from here and closes it */ 129#ifdef NO_PTHREADS 130 pid_t pid; 131#else 132 pthread_t tid; 133int proc_in; 134int proc_out; 135#endif 136int isolate_sigpipe; 137}; 138 139intstart_async(struct async *async); 140intfinish_async(struct async *async); 141intin_async(void); 142void NORETURN async_exit(int code); 143 144/** 145 * This callback should initialize the child process and preload the 146 * error channel if desired. The preloading of is useful if you want to 147 * have a message printed directly before the output of the child process. 148 * pp_cb is the callback cookie as passed to run_processes_parallel. 149 * You can store a child process specific callback cookie in pp_task_cb. 150 * 151 * Even after returning 0 to indicate that there are no more processes, 152 * this function will be called again until there are no more running 153 * child processes. 154 * 155 * Return 1 if the next child is ready to run. 156 * Return 0 if there are currently no more tasks to be processed. 157 * To send a signal to other child processes for abortion, 158 * return the negative signal number. 159 */ 160typedefint(*get_next_task_fn)(struct child_process *cp, 161struct strbuf *out, 162void*pp_cb, 163void**pp_task_cb); 164 165/** 166 * This callback is called whenever there are problems starting 167 * a new process. 168 * 169 * You must not write to stdout or stderr in this function. Add your 170 * message to the strbuf out instead, which will be printed without 171 * messing up the output of the other parallel processes. 172 * 173 * pp_cb is the callback cookie as passed into run_processes_parallel, 174 * pp_task_cb is the callback cookie as passed into get_next_task_fn. 175 * 176 * Return 0 to continue the parallel processing. To abort return non zero. 177 * To send a signal to other child processes for abortion, return 178 * the negative signal number. 179 */ 180typedefint(*start_failure_fn)(struct strbuf *out, 181void*pp_cb, 182void*pp_task_cb); 183 184/** 185 * This callback is called on every child process that finished processing. 186 * 187 * You must not write to stdout or stderr in this function. Add your 188 * message to the strbuf out instead, which will be printed without 189 * messing up the output of the other parallel processes. 190 * 191 * pp_cb is the callback cookie as passed into run_processes_parallel, 192 * pp_task_cb is the callback cookie as passed into get_next_task_fn. 193 * 194 * Return 0 to continue the parallel processing. To abort return non zero. 195 * To send a signal to other child processes for abortion, return 196 * the negative signal number. 197 */ 198typedefint(*task_finished_fn)(int result, 199struct strbuf *out, 200void*pp_cb, 201void*pp_task_cb); 202 203/** 204 * Runs up to n processes at the same time. Whenever a process can be 205 * started, the callback get_next_task_fn is called to obtain the data 206 * required to start another child process. 207 * 208 * The children started via this function run in parallel. Their output 209 * (both stdout and stderr) is routed to stderr in a manner that output 210 * from different tasks does not interleave. 211 * 212 * start_failure_fn and task_finished_fn can be NULL to omit any 213 * special handling. 214 */ 215intrun_processes_parallel(int n, 216 get_next_task_fn, 217 start_failure_fn, 218 task_finished_fn, 219void*pp_cb); 220 221#endif