Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | #include "run-command.h" |
| 3 | #include "exec_cmd.h" |
| 4 | |
| 5 | static inline void close_pair(int fd[2]) |
| 6 | { |
| 7 | close(fd[0]); |
| 8 | close(fd[1]); |
| 9 | } |
| 10 | |
| 11 | static inline void dup_devnull(int to) |
| 12 | { |
| 13 | int fd = open("/dev/null", O_RDWR); |
| 14 | dup2(fd, to); |
| 15 | close(fd); |
| 16 | } |
| 17 | |
| 18 | int start_command(struct child_process *cmd) |
| 19 | { |
| 20 | int need_in, need_out, need_err; |
| 21 | int fdin[2], fdout[2], fderr[2]; |
| 22 | |
| 23 | /* |
| 24 | * In case of errors we must keep the promise to close FDs |
| 25 | * that have been passed in via ->in and ->out. |
| 26 | */ |
| 27 | |
| 28 | need_in = !cmd->no_stdin && cmd->in < 0; |
| 29 | if (need_in) { |
| 30 | if (pipe(fdin) < 0) { |
| 31 | if (cmd->out > 0) |
| 32 | close(cmd->out); |
| 33 | return -ERR_RUN_COMMAND_PIPE; |
| 34 | } |
| 35 | cmd->in = fdin[1]; |
| 36 | } |
| 37 | |
| 38 | need_out = !cmd->no_stdout |
| 39 | && !cmd->stdout_to_stderr |
| 40 | && cmd->out < 0; |
| 41 | if (need_out) { |
| 42 | if (pipe(fdout) < 0) { |
| 43 | if (need_in) |
| 44 | close_pair(fdin); |
| 45 | else if (cmd->in) |
| 46 | close(cmd->in); |
| 47 | return -ERR_RUN_COMMAND_PIPE; |
| 48 | } |
| 49 | cmd->out = fdout[0]; |
| 50 | } |
| 51 | |
| 52 | need_err = !cmd->no_stderr && cmd->err < 0; |
| 53 | if (need_err) { |
| 54 | if (pipe(fderr) < 0) { |
| 55 | if (need_in) |
| 56 | close_pair(fdin); |
| 57 | else if (cmd->in) |
| 58 | close(cmd->in); |
| 59 | if (need_out) |
| 60 | close_pair(fdout); |
| 61 | else if (cmd->out) |
| 62 | close(cmd->out); |
| 63 | return -ERR_RUN_COMMAND_PIPE; |
| 64 | } |
| 65 | cmd->err = fderr[0]; |
| 66 | } |
| 67 | |
| 68 | #ifndef __MINGW32__ |
| 69 | fflush(NULL); |
| 70 | cmd->pid = fork(); |
| 71 | if (!cmd->pid) { |
| 72 | if (cmd->no_stdin) |
| 73 | dup_devnull(0); |
| 74 | else if (need_in) { |
| 75 | dup2(fdin[0], 0); |
| 76 | close_pair(fdin); |
| 77 | } else if (cmd->in) { |
| 78 | dup2(cmd->in, 0); |
| 79 | close(cmd->in); |
| 80 | } |
| 81 | |
| 82 | if (cmd->no_stderr) |
| 83 | dup_devnull(2); |
| 84 | else if (need_err) { |
| 85 | dup2(fderr[1], 2); |
| 86 | close_pair(fderr); |
| 87 | } |
| 88 | |
| 89 | if (cmd->no_stdout) |
| 90 | dup_devnull(1); |
| 91 | else if (cmd->stdout_to_stderr) |
| 92 | dup2(2, 1); |
| 93 | else if (need_out) { |
| 94 | dup2(fdout[1], 1); |
| 95 | close_pair(fdout); |
| 96 | } else if (cmd->out > 1) { |
| 97 | dup2(cmd->out, 1); |
| 98 | close(cmd->out); |
| 99 | } |
| 100 | |
| 101 | if (cmd->dir && chdir(cmd->dir)) |
| 102 | die("exec %s: cd to %s failed (%s)", cmd->argv[0], |
| 103 | cmd->dir, strerror(errno)); |
| 104 | if (cmd->env) { |
| 105 | for (; *cmd->env; cmd->env++) { |
| 106 | if (strchr(*cmd->env, '=')) |
| 107 | putenv((char*)*cmd->env); |
| 108 | else |
| 109 | unsetenv(*cmd->env); |
| 110 | } |
| 111 | } |
| 112 | if (cmd->preexec_cb) |
| 113 | cmd->preexec_cb(); |
| 114 | if (cmd->perf_cmd) { |
| 115 | execv_perf_cmd(cmd->argv); |
| 116 | } else { |
| 117 | execvp(cmd->argv[0], (char *const*) cmd->argv); |
| 118 | } |
| 119 | exit(127); |
| 120 | } |
| 121 | #else |
| 122 | int s0 = -1, s1 = -1, s2 = -1; /* backups of stdin, stdout, stderr */ |
| 123 | const char **sargv = cmd->argv; |
| 124 | char **env = environ; |
| 125 | |
| 126 | if (cmd->no_stdin) { |
| 127 | s0 = dup(0); |
| 128 | dup_devnull(0); |
| 129 | } else if (need_in) { |
| 130 | s0 = dup(0); |
| 131 | dup2(fdin[0], 0); |
| 132 | } else if (cmd->in) { |
| 133 | s0 = dup(0); |
| 134 | dup2(cmd->in, 0); |
| 135 | } |
| 136 | |
| 137 | if (cmd->no_stderr) { |
| 138 | s2 = dup(2); |
| 139 | dup_devnull(2); |
| 140 | } else if (need_err) { |
| 141 | s2 = dup(2); |
| 142 | dup2(fderr[1], 2); |
| 143 | } |
| 144 | |
| 145 | if (cmd->no_stdout) { |
| 146 | s1 = dup(1); |
| 147 | dup_devnull(1); |
| 148 | } else if (cmd->stdout_to_stderr) { |
| 149 | s1 = dup(1); |
| 150 | dup2(2, 1); |
| 151 | } else if (need_out) { |
| 152 | s1 = dup(1); |
| 153 | dup2(fdout[1], 1); |
| 154 | } else if (cmd->out > 1) { |
| 155 | s1 = dup(1); |
| 156 | dup2(cmd->out, 1); |
| 157 | } |
| 158 | |
| 159 | if (cmd->dir) |
| 160 | die("chdir in start_command() not implemented"); |
| 161 | if (cmd->env) { |
| 162 | env = copy_environ(); |
| 163 | for (; *cmd->env; cmd->env++) |
| 164 | env = env_setenv(env, *cmd->env); |
| 165 | } |
| 166 | |
| 167 | if (cmd->perf_cmd) { |
| 168 | cmd->argv = prepare_perf_cmd(cmd->argv); |
| 169 | } |
| 170 | |
| 171 | cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, env); |
| 172 | |
| 173 | if (cmd->env) |
| 174 | free_environ(env); |
| 175 | if (cmd->perf_cmd) |
| 176 | free(cmd->argv); |
| 177 | |
| 178 | cmd->argv = sargv; |
| 179 | if (s0 >= 0) |
| 180 | dup2(s0, 0), close(s0); |
| 181 | if (s1 >= 0) |
| 182 | dup2(s1, 1), close(s1); |
| 183 | if (s2 >= 0) |
| 184 | dup2(s2, 2), close(s2); |
| 185 | #endif |
| 186 | |
| 187 | if (cmd->pid < 0) { |
| 188 | int err = errno; |
| 189 | if (need_in) |
| 190 | close_pair(fdin); |
| 191 | else if (cmd->in) |
| 192 | close(cmd->in); |
| 193 | if (need_out) |
| 194 | close_pair(fdout); |
| 195 | else if (cmd->out) |
| 196 | close(cmd->out); |
| 197 | if (need_err) |
| 198 | close_pair(fderr); |
| 199 | return err == ENOENT ? |
| 200 | -ERR_RUN_COMMAND_EXEC : |
| 201 | -ERR_RUN_COMMAND_FORK; |
| 202 | } |
| 203 | |
| 204 | if (need_in) |
| 205 | close(fdin[0]); |
| 206 | else if (cmd->in) |
| 207 | close(cmd->in); |
| 208 | |
| 209 | if (need_out) |
| 210 | close(fdout[1]); |
| 211 | else if (cmd->out) |
| 212 | close(cmd->out); |
| 213 | |
| 214 | if (need_err) |
| 215 | close(fderr[1]); |
| 216 | |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | static int wait_or_whine(pid_t pid) |
| 221 | { |
| 222 | for (;;) { |
| 223 | int status, code; |
| 224 | pid_t waiting = waitpid(pid, &status, 0); |
| 225 | |
| 226 | if (waiting < 0) { |
| 227 | if (errno == EINTR) |
| 228 | continue; |
| 229 | error("waitpid failed (%s)", strerror(errno)); |
| 230 | return -ERR_RUN_COMMAND_WAITPID; |
| 231 | } |
| 232 | if (waiting != pid) |
| 233 | return -ERR_RUN_COMMAND_WAITPID_WRONG_PID; |
| 234 | if (WIFSIGNALED(status)) |
| 235 | return -ERR_RUN_COMMAND_WAITPID_SIGNAL; |
| 236 | |
| 237 | if (!WIFEXITED(status)) |
| 238 | return -ERR_RUN_COMMAND_WAITPID_NOEXIT; |
| 239 | code = WEXITSTATUS(status); |
| 240 | switch (code) { |
| 241 | case 127: |
| 242 | return -ERR_RUN_COMMAND_EXEC; |
| 243 | case 0: |
| 244 | return 0; |
| 245 | default: |
| 246 | return -code; |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | int finish_command(struct child_process *cmd) |
| 252 | { |
| 253 | return wait_or_whine(cmd->pid); |
| 254 | } |
| 255 | |
| 256 | int run_command(struct child_process *cmd) |
| 257 | { |
| 258 | int code = start_command(cmd); |
| 259 | if (code) |
| 260 | return code; |
| 261 | return finish_command(cmd); |
| 262 | } |
| 263 | |
| 264 | static void prepare_run_command_v_opt(struct child_process *cmd, |
| 265 | const char **argv, |
| 266 | int opt) |
| 267 | { |
| 268 | memset(cmd, 0, sizeof(*cmd)); |
| 269 | cmd->argv = argv; |
| 270 | cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0; |
| 271 | cmd->perf_cmd = opt & RUN_PERF_CMD ? 1 : 0; |
| 272 | cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0; |
| 273 | } |
| 274 | |
| 275 | int run_command_v_opt(const char **argv, int opt) |
| 276 | { |
| 277 | struct child_process cmd; |
| 278 | prepare_run_command_v_opt(&cmd, argv, opt); |
| 279 | return run_command(&cmd); |
| 280 | } |
| 281 | |
| 282 | int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env) |
| 283 | { |
| 284 | struct child_process cmd; |
| 285 | prepare_run_command_v_opt(&cmd, argv, opt); |
| 286 | cmd.dir = dir; |
| 287 | cmd.env = env; |
| 288 | return run_command(&cmd); |
| 289 | } |
| 290 | |
| 291 | #ifdef __MINGW32__ |
| 292 | static __stdcall unsigned run_thread(void *data) |
| 293 | { |
| 294 | struct async *async = data; |
| 295 | return async->proc(async->fd_for_proc, async->data); |
| 296 | } |
| 297 | #endif |
| 298 | |
| 299 | int start_async(struct async *async) |
| 300 | { |
| 301 | int pipe_out[2]; |
| 302 | |
| 303 | if (pipe(pipe_out) < 0) |
| 304 | return error("cannot create pipe: %s", strerror(errno)); |
| 305 | async->out = pipe_out[0]; |
| 306 | |
| 307 | #ifndef __MINGW32__ |
| 308 | /* Flush stdio before fork() to avoid cloning buffers */ |
| 309 | fflush(NULL); |
| 310 | |
| 311 | async->pid = fork(); |
| 312 | if (async->pid < 0) { |
| 313 | error("fork (async) failed: %s", strerror(errno)); |
| 314 | close_pair(pipe_out); |
| 315 | return -1; |
| 316 | } |
| 317 | if (!async->pid) { |
| 318 | close(pipe_out[0]); |
| 319 | exit(!!async->proc(pipe_out[1], async->data)); |
| 320 | } |
| 321 | close(pipe_out[1]); |
| 322 | #else |
| 323 | async->fd_for_proc = pipe_out[1]; |
| 324 | async->tid = (HANDLE) _beginthreadex(NULL, 0, run_thread, async, 0, NULL); |
| 325 | if (!async->tid) { |
| 326 | error("cannot create thread: %s", strerror(errno)); |
| 327 | close_pair(pipe_out); |
| 328 | return -1; |
| 329 | } |
| 330 | #endif |
| 331 | return 0; |
| 332 | } |
| 333 | |
| 334 | int finish_async(struct async *async) |
| 335 | { |
| 336 | #ifndef __MINGW32__ |
| 337 | int ret = 0; |
| 338 | |
| 339 | if (wait_or_whine(async->pid)) |
| 340 | ret = error("waitpid (async) failed"); |
| 341 | #else |
| 342 | DWORD ret = 0; |
| 343 | if (WaitForSingleObject(async->tid, INFINITE) != WAIT_OBJECT_0) |
| 344 | ret = error("waiting for thread failed: %lu", GetLastError()); |
| 345 | else if (!GetExitCodeThread(async->tid, &ret)) |
| 346 | ret = error("cannot get thread exit code: %lu", GetLastError()); |
| 347 | CloseHandle(async->tid); |
| 348 | #endif |
| 349 | return ret; |
| 350 | } |
| 351 | |
| 352 | int run_hook(const char *index_file, const char *name, ...) |
| 353 | { |
| 354 | struct child_process hook; |
| 355 | const char **argv = NULL, *env[2]; |
| 356 | char index[PATH_MAX]; |
| 357 | va_list args; |
| 358 | int ret; |
| 359 | size_t i = 0, alloc = 0; |
| 360 | |
| 361 | if (access(perf_path("hooks/%s", name), X_OK) < 0) |
| 362 | return 0; |
| 363 | |
| 364 | va_start(args, name); |
| 365 | ALLOC_GROW(argv, i + 1, alloc); |
| 366 | argv[i++] = perf_path("hooks/%s", name); |
| 367 | while (argv[i-1]) { |
| 368 | ALLOC_GROW(argv, i + 1, alloc); |
| 369 | argv[i++] = va_arg(args, const char *); |
| 370 | } |
| 371 | va_end(args); |
| 372 | |
| 373 | memset(&hook, 0, sizeof(hook)); |
| 374 | hook.argv = argv; |
| 375 | hook.no_stdin = 1; |
| 376 | hook.stdout_to_stderr = 1; |
| 377 | if (index_file) { |
| 378 | snprintf(index, sizeof(index), "PERF_INDEX_FILE=%s", index_file); |
| 379 | env[0] = index; |
| 380 | env[1] = NULL; |
| 381 | hook.env = env; |
| 382 | } |
| 383 | |
| 384 | ret = start_command(&hook); |
| 385 | free(argv); |
| 386 | if (ret) { |
| 387 | warning("Could not spawn %s", argv[0]); |
| 388 | return ret; |
| 389 | } |
| 390 | ret = finish_command(&hook); |
| 391 | if (ret == -ERR_RUN_COMMAND_WAITPID_SIGNAL) |
| 392 | warning("%s exited due to uncaught signal", argv[0]); |
| 393 | |
| 394 | return ret; |
| 395 | } |