* Create a child connected to us via its stdin/stdout. * * This is derived from CVS code * * Note that in the child STDIN is set to blocking and STDOUT * is set to non-blocking. This is necessary as rsh relies on stdin being blocking * and ssh relies on stdout being non-blocking * * If blocking_io is set then use blocking io on both fds. That can be * used to cope with badly broken rsh i
| 46 | * Solaris. |
| 47 | **/ |
| 48 | pid_t piped_child(char **command, int *f_in, int *f_out) |
| 49 | { |
| 50 | pid_t pid; |
| 51 | int to_child_pipe[2]; |
| 52 | int from_child_pipe[2]; |
| 53 | |
| 54 | if (DEBUG_GTE(CMD, 1)) |
| 55 | print_child_argv("opening connection using:", command); |
| 56 | |
| 57 | if (fd_pair(to_child_pipe) < 0 || fd_pair(from_child_pipe) < 0) { |
| 58 | rsyserr(FERROR, errno, "pipe"); |
| 59 | exit_cleanup(RERR_IPC); |
| 60 | } |
| 61 | |
| 62 | pid = do_fork(); |
| 63 | if (pid == -1) { |
| 64 | rsyserr(FERROR, errno, "fork"); |
| 65 | exit_cleanup(RERR_IPC); |
| 66 | } |
| 67 | |
| 68 | if (pid == 0) { |
| 69 | if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 |
| 70 | || close(to_child_pipe[1]) < 0 |
| 71 | || close(from_child_pipe[0]) < 0 |
| 72 | || dup2(from_child_pipe[1], STDOUT_FILENO) < 0) { |
| 73 | rsyserr(FERROR, errno, "Failed to dup/close"); |
| 74 | exit_cleanup(RERR_IPC); |
| 75 | } |
| 76 | if (to_child_pipe[0] != STDIN_FILENO) |
| 77 | close(to_child_pipe[0]); |
| 78 | if (from_child_pipe[1] != STDOUT_FILENO) |
| 79 | close(from_child_pipe[1]); |
| 80 | set_blocking(STDIN_FILENO); |
| 81 | if (blocking_io > 0) |
| 82 | set_blocking(STDOUT_FILENO); |
| 83 | execvp(command[0], command); |
| 84 | rsyserr(FERROR, errno, "Failed to exec %s", command[0]); |
| 85 | exit_cleanup(RERR_IPC); |
| 86 | } |
| 87 | |
| 88 | if (close(from_child_pipe[1]) < 0 || close(to_child_pipe[0]) < 0) { |
| 89 | rsyserr(FERROR, errno, "Failed to close"); |
| 90 | exit_cleanup(RERR_IPC); |
| 91 | } |
| 92 | |
| 93 | *f_in = from_child_pipe[0]; |
| 94 | *f_out = to_child_pipe[1]; |
| 95 | |
| 96 | return pid; |
| 97 | } |
| 98 | |
| 99 | /* This function forks a child which calls child_main(). First, |
| 100 | * however, it has to establish communication paths to and from the |
no test coverage detected