| 1634 | } |
| 1635 | |
| 1636 | static int cbm_subprocess_spawn_posix(cbm_subprocess_t *process) { |
| 1637 | int input_flags = O_RDONLY; |
| 1638 | #ifdef O_CLOEXEC |
| 1639 | input_flags |= O_CLOEXEC; |
| 1640 | #endif |
| 1641 | if (process->stdin_file) { |
| 1642 | /* O_NONBLOCK prevents waiting for a FIFO writer before fstat rejects |
| 1643 | * non-regular input. No output has been opened at this point. */ |
| 1644 | input_flags |= O_NONBLOCK; |
| 1645 | #ifdef O_NOFOLLOW |
| 1646 | input_flags |= O_NOFOLLOW; |
| 1647 | #endif |
| 1648 | } |
| 1649 | int input = cbm_posix_fd_at_least_three( |
| 1650 | open(process->stdin_file ? process->stdin_file : "/dev/null", input_flags)); |
| 1651 | if (input < 0) { |
| 1652 | return -1; |
| 1653 | } |
| 1654 | if (process->stdin_file) { |
| 1655 | struct stat input_status; |
| 1656 | if (fstat(input, &input_status) != 0 || !S_ISREG(input_status.st_mode)) { |
| 1657 | (void)close(input); |
| 1658 | return -1; |
| 1659 | } |
| 1660 | } |
| 1661 | const char *target = process->log_file ? process->log_file : "/dev/null"; |
| 1662 | int output_flags = O_WRONLY | O_CREAT | O_TRUNC; |
| 1663 | #ifdef O_CLOEXEC |
| 1664 | output_flags |= O_CLOEXEC; |
| 1665 | #endif |
| 1666 | #ifdef O_NOFOLLOW |
| 1667 | output_flags |= O_NOFOLLOW; |
| 1668 | #endif |
| 1669 | int output = cbm_posix_fd_at_least_three(open(target, output_flags, 0600)); |
| 1670 | if (output < 0) { |
| 1671 | (void)close(input); |
| 1672 | return -1; |
| 1673 | } |
| 1674 | struct stat output_status; |
| 1675 | if (fstat(output, &output_status) != 0 || |
| 1676 | (process->log_file && !S_ISREG(output_status.st_mode))) { |
| 1677 | (void)close(input); |
| 1678 | (void)close(output); |
| 1679 | return -1; |
| 1680 | } |
| 1681 | if (process->log_file && fchmod(output, 0600) != 0) { |
| 1682 | (void)close(input); |
| 1683 | (void)close(output); |
| 1684 | return -1; |
| 1685 | } |
| 1686 | |
| 1687 | int stdout_capture = -1; |
| 1688 | if (process->stdout_file) { |
| 1689 | stdout_capture = |
| 1690 | cbm_posix_fd_at_least_three(open(process->stdout_file, output_flags, 0600)); |
| 1691 | struct stat stdout_status; |
| 1692 | if (stdout_capture < 0 || fstat(stdout_capture, &stdout_status) != 0 || |
| 1693 | !S_ISREG(stdout_status.st_mode) || fchmod(stdout_capture, 0600) != 0) { |
no test coverage detected