| 1862 | } |
| 1863 | |
| 1864 | static cbm_proc_poll_t cbm_subprocess_poll_posix(cbm_subprocess_t *process, |
| 1865 | cbm_proc_result_t *out) { |
| 1866 | uint64_t now = cbm_now_ms(); |
| 1867 | |
| 1868 | if (!process->root_reaped) { |
| 1869 | int status = 0; |
| 1870 | pid_t waited = waitpid(process->pid, &status, WNOHANG); |
| 1871 | if (waited == process->pid) { |
| 1872 | cbm_posix_capture_root(process, status); |
| 1873 | } else if (waited < 0 && errno != EINTR) { |
| 1874 | /* ECHILD means another reaper consumed the status. Other permanent |
| 1875 | * wait failures are treated the same: retain containment, stop the |
| 1876 | * tree, and never spin forever on the failed wait operation. */ |
| 1877 | process->root_reaped = true; |
| 1878 | process->result.outcome = process->timed_out ? CBM_PROC_HANG : CBM_PROC_KILLED; |
| 1879 | process->result.exit_code = -1; |
| 1880 | process->result.term_signal = 0; |
| 1881 | cbm_posix_begin_termination(process, now); |
| 1882 | } |
| 1883 | } |
| 1884 | |
| 1885 | bool group_active = cbm_posix_group_active(process); |
| 1886 | if (!process->termination_started) { |
| 1887 | if (cbm_subprocess_cancellation_requested(process)) { |
| 1888 | cbm_posix_begin_termination(process, now); |
| 1889 | } else if (!process->root_reaped && process->quiet_timeout_ms > 0 && |
| 1890 | now - process->last_activity_ms >= (uint64_t)process->quiet_timeout_ms) { |
| 1891 | process->timed_out = true; |
| 1892 | cbm_posix_begin_termination(process, now); |
| 1893 | } else if (process->root_reaped && group_active) { |
| 1894 | /* A root that daemonizes children is not terminal. Preserve its exit |
| 1895 | * classification, but drain the descendants through the same path. */ |
| 1896 | cbm_posix_begin_termination(process, now); |
| 1897 | } |
| 1898 | } |
| 1899 | if (process->termination_started && group_active && !process->force_sent && |
| 1900 | now - process->termination_started_ms >= (uint64_t)process->cancel_grace_ms) { |
| 1901 | cbm_posix_force_tree(process, now); |
| 1902 | } |
| 1903 | group_active = cbm_posix_group_active(process); |
| 1904 | if (process->force_started_ms != 0 && group_active && |
| 1905 | now - process->force_started_ms >= CBM_SUBPROCESS_FORCE_SETTLE_MS) { |
| 1906 | return cbm_subprocess_finish_failed(process, out); |
| 1907 | } |
| 1908 | if (process->root_reaped && !group_active) { |
| 1909 | return cbm_subprocess_finish(process, out); |
| 1910 | } |
| 1911 | return CBM_PROC_POLL_RUNNING; |
| 1912 | } |
| 1913 | |
| 1914 | #endif /* _WIN32 */ |
| 1915 |
no test coverage detected