Lines Matching full:cmd
14 #include "exec-cmd.h"
31 int start_command(struct child_process *cmd) in start_command() argument
42 need_in = !cmd->no_stdin && cmd->in < 0; in start_command()
45 if (cmd->out > 0) in start_command()
46 close(cmd->out); in start_command()
49 cmd->in = fdin[1]; in start_command()
52 need_out = !cmd->no_stdout in start_command()
53 && !cmd->stdout_to_stderr in start_command()
54 && cmd->out < 0; in start_command()
59 else if (cmd->in) in start_command()
60 close(cmd->in); in start_command()
63 cmd->out = fdout[0]; in start_command()
66 need_err = !cmd->no_stderr && cmd->err < 0; in start_command()
71 else if (cmd->in) in start_command()
72 close(cmd->in); in start_command()
75 else if (cmd->out) in start_command()
76 close(cmd->out); in start_command()
79 cmd->err = fderr[0]; in start_command()
83 cmd->pid = fork(); in start_command()
84 if (!cmd->pid) { in start_command()
85 if (cmd->no_stdin) in start_command()
90 } else if (cmd->in) { in start_command()
91 dup2(cmd->in, 0); in start_command()
92 close(cmd->in); in start_command()
95 if (cmd->no_stderr) in start_command()
102 if (cmd->no_stdout) in start_command()
104 else if (cmd->stdout_to_stderr) in start_command()
109 } else if (cmd->out > 1) { in start_command()
110 dup2(cmd->out, 1); in start_command()
111 close(cmd->out); in start_command()
114 if (cmd->dir && chdir(cmd->dir)) in start_command()
115 die("exec %s: cd to %s failed (%s)", cmd->argv[0], in start_command()
116 cmd->dir, str_error_r(errno, sbuf, sizeof(sbuf))); in start_command()
117 if (cmd->env) { in start_command()
118 for (; *cmd->env; cmd->env++) { in start_command()
119 if (strchr(*cmd->env, '=')) in start_command()
120 putenv((char*)*cmd->env); in start_command()
122 unsetenv(*cmd->env); in start_command()
125 if (cmd->preexec_cb) in start_command()
126 cmd->preexec_cb(); in start_command()
127 if (cmd->no_exec_cmd) in start_command()
128 exit(cmd->no_exec_cmd(cmd)); in start_command()
129 if (cmd->exec_cmd) { in start_command()
130 execv_cmd(cmd->argv); in start_command()
132 execvp(cmd->argv[0], (char *const*) cmd->argv); in start_command()
137 if (cmd->pid < 0) { in start_command()
141 else if (cmd->in) in start_command()
142 close(cmd->in); in start_command()
145 else if (cmd->out) in start_command()
146 close(cmd->out); in start_command()
156 else if (cmd->in) in start_command()
157 close(cmd->in); in start_command()
161 else if (cmd->out) in start_command()
162 close(cmd->out); in start_command()
170 static int wait_or_whine(struct child_process *cmd, bool block) in wait_or_whine() argument
172 bool finished = cmd->finished; in wait_or_whine()
173 int result = cmd->finish_result; in wait_or_whine()
177 pid_t waiting = waitpid(cmd->pid, &status, block ? 0 : WNOHANG); in wait_or_whine()
192 } else if (waiting != cmd->pid) { in wait_or_whine()
214 cmd->finished = 1; in wait_or_whine()
215 cmd->finish_result = result; in wait_or_whine()
230 int check_if_command_finished(struct child_process *cmd) in check_if_command_finished() argument
233 char filename[6 + MAX_STRLEN_TYPE(typeof(cmd->pid)) + 7 + 1]; in check_if_command_finished()
241 sprintf(filename, "/proc/%u/status", cmd->pid); in check_if_command_finished()
263 wait_or_whine(cmd, /*block=*/false); in check_if_command_finished()
264 return cmd->finished; in check_if_command_finished()
268 int finish_command(struct child_process *cmd) in finish_command() argument
270 return wait_or_whine(cmd, /*block=*/true); in finish_command()
273 int run_command(struct child_process *cmd) in run_command() argument
275 int code = start_command(cmd); in run_command()
278 return finish_command(cmd); in run_command()
281 static void prepare_run_command_v_opt(struct child_process *cmd, in prepare_run_command_v_opt() argument
285 memset(cmd, 0, sizeof(*cmd)); in prepare_run_command_v_opt()
286 cmd->argv = argv; in prepare_run_command_v_opt()
287 cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0; in prepare_run_command_v_opt()
288 cmd->exec_cmd = opt & RUN_EXEC_CMD ? 1 : 0; in prepare_run_command_v_opt()
289 cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0; in prepare_run_command_v_opt()
294 struct child_process cmd; in run_command_v_opt() local
295 prepare_run_command_v_opt(&cmd, argv, opt); in run_command_v_opt()
296 return run_command(&cmd); in run_command_v_opt()