#include #include #include #include #include #include #include "array.h" #include "builtins.h" void handle(int sig) { /* Do nothing. */ } void builtin_wait(process p, bool bind) { struct sigaction handler; handler.sa_handler = handle; sigemptyset(&handler.sa_mask); handler.sa_flags = 0; pid_t *pids; arrayInit(pids); size_t i = 1; bool error = false; while (!error && p.argv[i] != NULL) { char *end; pid_t tmp = strtol(p.argv[i], &end, 10); if (*end != '\0') { fprintf(stderr, "Not a valid pid: %s", p.argv[i]); error = true; break; } arrayPush(pids) = tmp; ++i; } if (!error) { while (!arrayIsEmpty(pids)) { int status; pid_t current_pid = arrayPop(pids); if (bind) { printf("Resuming %ld...\n", (long) current_pid); } else { printf("Waiting for %ld...\n", (long) current_pid); } // install signal handler without SA_RESTART, so waitpid gets interrupted sigaction(SIGINT, &handler, NULL); if (waitpid(current_pid, &status, WUNTRACED) < 0) { if (EINTR == errno) { // cancelled by ctrl-c if (bind) { kill(current_pid, SIGKILL); waitpid(current_pid, &status, WUNTRACED); printf("Killed [%ld]\n", (long)current_pid); } else { kill(current_pid, SIGCONT); } } else { perror("Could not wait for process"); } } else { if (bind) { printf("[%i] ", WEXITSTATUS(status)); } else { printf("[%i] TERMINATED\n", current_pid); if (WIFEXITED(status)) { printf("[%i] exited normally with status: %i\n", current_pid, WEXITSTATUS(status)); } else if (WIFSTOPPED(status)) { printf("[%i] was stopped by signal: %s\n", current_pid, strsignal(WSTOPSIG(status))); } else if (WIFSIGNALED(status)) { printf("[%i] was terminated by signal: %s\n", current_pid, strsignal(WTERMSIG(status))); } else { printf("[%i] exited (not sure why), exit status: %i\n", current_pid, WEXITSTATUS(status)); } } } signal(SIGINT, SIG_DFL); } } arrayRelease(pids); }