| 39 | static int installed_child_cleanup_handler; |
| 40 | |
| 41 | static void cleanup_children(int sig, int in_signal) |
| 42 | { |
| 43 | struct child_to_clean *children_to_wait_for = NULL; |
| 44 | |
| 45 | while (children_to_clean) { |
| 46 | struct child_to_clean *p = children_to_clean; |
| 47 | children_to_clean = p->next; |
| 48 | |
| 49 | if (p->process && !in_signal) { |
| 50 | struct child_process *process = p->process; |
| 51 | if (process->clean_on_exit_handler) { |
| 52 | trace_printf( |
| 53 | "trace: run_command: running exit handler for pid %" |
| 54 | PRIuMAX, (uintmax_t)p->pid |
| 55 | ); |
| 56 | process->clean_on_exit_handler(process); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | kill(p->pid, sig); |
| 61 | |
| 62 | if (p->process && p->process->wait_after_clean) { |
| 63 | p->next = children_to_wait_for; |
| 64 | children_to_wait_for = p; |
| 65 | } else { |
| 66 | if (!in_signal) |
| 67 | free(p); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | while (children_to_wait_for) { |
| 72 | struct child_to_clean *p = children_to_wait_for; |
| 73 | children_to_wait_for = p->next; |
| 74 | |
| 75 | while (waitpid(p->pid, NULL, 0) < 0 && errno == EINTR) |
| 76 | ; /* spin waiting for process exit or error */ |
| 77 | |
| 78 | if (!in_signal) |
| 79 | free(p); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | static void cleanup_children_on_signal(int sig) |
| 84 | { |
no test coverage detected