Lines Matching full:cmd

12 #include "exec-cmd.h"
29 int start_command(struct child_process *cmd) in start_command() argument
40 need_in = !cmd->no_stdin && cmd->in < 0; in start_command()
43 if (cmd->out > 0) in start_command()
44 close(cmd->out); in start_command()
47 cmd->in = fdin[1]; in start_command()
50 need_out = !cmd->no_stdout in start_command()
51 && !cmd->stdout_to_stderr in start_command()
52 && cmd->out < 0; in start_command()
57 else if (cmd->in) in start_command()
58 close(cmd->in); in start_command()
61 cmd->out = fdout[0]; in start_command()
64 need_err = !cmd->no_stderr && cmd->err < 0; in start_command()
69 else if (cmd->in) in start_command()
70 close(cmd->in); in start_command()
73 else if (cmd->out) in start_command()
74 close(cmd->out); in start_command()
77 cmd->err = fderr[0]; in start_command()
81 cmd->pid = fork(); in start_command()
82 if (!cmd->pid) { in start_command()
83 if (cmd->no_stdin) in start_command()
88 } else if (cmd->in) { in start_command()
89 dup2(cmd->in, 0); in start_command()
90 close(cmd->in); in start_command()
93 if (cmd->no_stderr) in start_command()
100 if (cmd->no_stdout) in start_command()
102 else if (cmd->stdout_to_stderr) in start_command()
107 } else if (cmd->out > 1) { in start_command()
108 dup2(cmd->out, 1); in start_command()
109 close(cmd->out); in start_command()
112 if (cmd->dir && chdir(cmd->dir)) in start_command()
113 die("exec %s: cd to %s failed (%s)", cmd->argv[0], in start_command()
114 cmd->dir, str_error_r(errno, sbuf, sizeof(sbuf))); in start_command()
115 if (cmd->env) { in start_command()
116 for (; *cmd->env; cmd->env++) { in start_command()
117 if (strchr(*cmd->env, '=')) in start_command()
118 putenv((char*)*cmd->env); in start_command()
120 unsetenv(*cmd->env); in start_command()
123 if (cmd->preexec_cb) in start_command()
124 cmd->preexec_cb(); in start_command()
125 if (cmd->no_exec_cmd) in start_command()
126 exit(cmd->no_exec_cmd(cmd)); in start_command()
127 if (cmd->exec_cmd) { in start_command()
128 execv_cmd(cmd->argv); in start_command()
130 execvp(cmd->argv[0], (char *const*) cmd->argv); in start_command()
135 if (cmd->pid < 0) { in start_command()
139 else if (cmd->in) in start_command()
140 close(cmd->in); in start_command()
143 else if (cmd->out) in start_command()
144 close(cmd->out); in start_command()
154 else if (cmd->in) in start_command()
155 close(cmd->in); in start_command()
159 else if (cmd->out) in start_command()
160 close(cmd->out); in start_command()
168 static int wait_or_whine(struct child_process *cmd, bool block) in wait_or_whine() argument
170 bool finished = cmd->finished; in wait_or_whine()
171 int result = cmd->finish_result; in wait_or_whine()
175 pid_t waiting = waitpid(cmd->pid, &status, block ? 0 : WNOHANG); in wait_or_whine()
190 } else if (waiting != cmd->pid) { in wait_or_whine()
212 cmd->finished = 1; in wait_or_whine()
213 cmd->finish_result = result; in wait_or_whine()
218 int check_if_command_finished(struct child_process *cmd) in check_if_command_finished() argument
220 wait_or_whine(cmd, /*block=*/false); in check_if_command_finished()
221 return cmd->finished; in check_if_command_finished()
224 int finish_command(struct child_process *cmd) in finish_command() argument
226 return wait_or_whine(cmd, /*block=*/true); in finish_command()
229 int run_command(struct child_process *cmd) in run_command() argument
231 int code = start_command(cmd); in run_command()
234 return finish_command(cmd); in run_command()
237 static void prepare_run_command_v_opt(struct child_process *cmd, in prepare_run_command_v_opt() argument
241 memset(cmd, 0, sizeof(*cmd)); in prepare_run_command_v_opt()
242 cmd->argv = argv; in prepare_run_command_v_opt()
243 cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0; in prepare_run_command_v_opt()
244 cmd->exec_cmd = opt & RUN_EXEC_CMD ? 1 : 0; in prepare_run_command_v_opt()
245 cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0; in prepare_run_command_v_opt()
250 struct child_process cmd; in run_command_v_opt() local
251 prepare_run_command_v_opt(&cmd, argv, opt); in run_command_v_opt()
252 return run_command(&cmd); in run_command_v_opt()