Lines Matching full:cmd
13 #include "exec-cmd.h"
30 int start_command(struct child_process *cmd) in start_command() argument
41 need_in = !cmd->no_stdin && cmd->in < 0; in start_command()
44 if (cmd->out > 0) in start_command()
45 close(cmd->out); in start_command()
48 cmd->in = fdin[1]; in start_command()
51 need_out = !cmd->no_stdout in start_command()
52 && !cmd->stdout_to_stderr in start_command()
53 && cmd->out < 0; in start_command()
58 else if (cmd->in) in start_command()
59 close(cmd->in); in start_command()
62 cmd->out = fdout[0]; in start_command()
65 need_err = !cmd->no_stderr && cmd->err < 0; in start_command()
70 else if (cmd->in) in start_command()
71 close(cmd->in); in start_command()
74 else if (cmd->out) in start_command()
75 close(cmd->out); in start_command()
78 cmd->err = fderr[0]; in start_command()
82 cmd->pid = fork(); in start_command()
83 if (!cmd->pid) { in start_command()
84 if (cmd->no_stdin) in start_command()
89 } else if (cmd->in) { in start_command()
90 dup2(cmd->in, 0); in start_command()
91 close(cmd->in); in start_command()
94 if (cmd->no_stderr) in start_command()
101 if (cmd->no_stdout) in start_command()
103 else if (cmd->stdout_to_stderr) in start_command()
108 } else if (cmd->out > 1) { in start_command()
109 dup2(cmd->out, 1); in start_command()
110 close(cmd->out); in start_command()
113 if (cmd->dir && chdir(cmd->dir)) in start_command()
114 die("exec %s: cd to %s failed (%s)", cmd->argv[0], in start_command()
115 cmd->dir, str_error_r(errno, sbuf, sizeof(sbuf))); in start_command()
116 if (cmd->env) { in start_command()
117 for (; *cmd->env; cmd->env++) { in start_command()
118 if (strchr(*cmd->env, '=')) in start_command()
119 putenv((char*)*cmd->env); in start_command()
121 unsetenv(*cmd->env); in start_command()
124 if (cmd->preexec_cb) in start_command()
125 cmd->preexec_cb(); in start_command()
126 if (cmd->no_exec_cmd) in start_command()
127 exit(cmd->no_exec_cmd(cmd)); in start_command()
128 if (cmd->exec_cmd) { in start_command()
129 execv_cmd(cmd->argv); in start_command()
131 execvp(cmd->argv[0], (char *const*) cmd->argv); in start_command()
136 if (cmd->pid < 0) { in start_command()
140 else if (cmd->in) in start_command()
141 close(cmd->in); in start_command()
144 else if (cmd->out) in start_command()
145 close(cmd->out); in start_command()
155 else if (cmd->in) in start_command()
156 close(cmd->in); in start_command()
160 else if (cmd->out) in start_command()
161 close(cmd->out); in start_command()
169 static int wait_or_whine(struct child_process *cmd, bool block) in wait_or_whine() argument
171 bool finished = cmd->finished; in wait_or_whine()
172 int result = cmd->finish_result; in wait_or_whine()
176 pid_t waiting = waitpid(cmd->pid, &status, block ? 0 : WNOHANG); in wait_or_whine()
191 } else if (waiting != cmd->pid) { in wait_or_whine()
213 cmd->finished = 1; in wait_or_whine()
214 cmd->finish_result = result; in wait_or_whine()
219 int check_if_command_finished(struct child_process *cmd) in check_if_command_finished() argument
230 sprintf(filename, "/proc/%d/status", cmd->pid); in check_if_command_finished()
252 wait_or_whine(cmd, /*block=*/false); in check_if_command_finished()
253 return cmd->finished; in check_if_command_finished()
257 int finish_command(struct child_process *cmd) in finish_command() argument
259 return wait_or_whine(cmd, /*block=*/true); in finish_command()
262 int run_command(struct child_process *cmd) in run_command() argument
264 int code = start_command(cmd); in run_command()
267 return finish_command(cmd); in run_command()
270 static void prepare_run_command_v_opt(struct child_process *cmd, in prepare_run_command_v_opt() argument
274 memset(cmd, 0, sizeof(*cmd)); in prepare_run_command_v_opt()
275 cmd->argv = argv; in prepare_run_command_v_opt()
276 cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0; in prepare_run_command_v_opt()
277 cmd->exec_cmd = opt & RUN_EXEC_CMD ? 1 : 0; in prepare_run_command_v_opt()
278 cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0; in prepare_run_command_v_opt()
283 struct child_process cmd; in run_command_v_opt() local
284 prepare_run_command_v_opt(&cmd, argv, opt); in run_command_v_opt()
285 return run_command(&cmd); in run_command_v_opt()