| 1 | /* -*- c-file-style: "linux" -*- |
| 2 | * |
| 3 | * Copyright (C) 1996-2000 by Andrew Tridgell |
| 4 | * Copyright (C) Paul Mackerras 1996 |
| 5 | * Copyright (C) 2001, 2002 by Martin Pool <mbp@samba.org> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | */ |
| 21 | |
| 22 | #include "rsync.h" |
| 23 | |
| 24 | extern int am_sender; |
| 25 | extern int am_server; |
| 26 | extern int blocking_io; |
| 27 | extern int orig_umask; |
| 28 | extern int write_batch; |
| 29 | extern int filesfrom_fd; |
| 30 | |
| 31 | /** |
| 32 | * Create a child connected to use on stdin/stdout. |
| 33 | * |
| 34 | * This is derived from CVS code |
| 35 | * |
| 36 | * Note that in the child STDIN is set to blocking and STDOUT |
| 37 | * is set to non-blocking. This is necessary as rsh relies on stdin being blocking |
| 38 | * and ssh relies on stdout being non-blocking |
| 39 | * |
| 40 | * If blocking_io is set then use blocking io on both fds. That can be |
| 41 | * used to cope with badly broken rsh implementations like the one on |
| 42 | * Solaris. |
| 43 | **/ |
| 44 | pid_t piped_child(char **command, int *f_in, int *f_out) |
| 45 | { |
| 46 | pid_t pid; |
| 47 | int to_child_pipe[2]; |
| 48 | int from_child_pipe[2]; |
| 49 | |
| 50 | if (verbose >= 2) { |
| 51 | print_child_argv(command); |
| 52 | } |
| 53 | |
| 54 | if (fd_pair(to_child_pipe) < 0 || fd_pair(from_child_pipe) < 0) { |
| 55 | rsyserr(FERROR, errno, "pipe"); |
| 56 | exit_cleanup(RERR_IPC); |
| 57 | } |
| 58 | |
| 59 | pid = do_fork(); |
| 60 | if (pid == -1) { |
| 61 | rsyserr(FERROR, errno, "fork"); |
| 62 | exit_cleanup(RERR_IPC); |
| 63 | } |
| 64 | |
| 65 | if (pid == 0) { |
| 66 | if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 || |
| 67 | close(to_child_pipe[1]) < 0 || |
| 68 | close(from_child_pipe[0]) < 0 || |
| 69 | dup2(from_child_pipe[1], STDOUT_FILENO) < 0) { |
| 70 | rsyserr(FERROR, errno, "Failed to dup/close"); |
| 71 | exit_cleanup(RERR_IPC); |
| 72 | } |
| 73 | if (to_child_pipe[0] != STDIN_FILENO) |
| 74 | close(to_child_pipe[0]); |
| 75 | if (from_child_pipe[1] != STDOUT_FILENO) |
| 76 | close(from_child_pipe[1]); |
| 77 | umask(orig_umask); |
| 78 | set_blocking(STDIN_FILENO); |
| 79 | if (blocking_io > 0) |
| 80 | set_blocking(STDOUT_FILENO); |
| 81 | execvp(command[0], command); |
| 82 | rsyserr(FERROR, errno, "Failed to exec %s", command[0]); |
| 83 | exit_cleanup(RERR_IPC); |
| 84 | } |
| 85 | |
| 86 | if (close(from_child_pipe[1]) < 0 || close(to_child_pipe[0]) < 0) { |
| 87 | rsyserr(FERROR, errno, "Failed to close"); |
| 88 | exit_cleanup(RERR_IPC); |
| 89 | } |
| 90 | |
| 91 | *f_in = from_child_pipe[0]; |
| 92 | *f_out = to_child_pipe[1]; |
| 93 | |
| 94 | return pid; |
| 95 | } |
| 96 | |
| 97 | /* |
| 98 | * This function forks a child which calls child_main(). First, |
| 99 | * however, it has to establish communication paths to and from the |
| 100 | * newborn child. It creates two socket pairs -- one for writing to |
| 101 | * the child (from the parent) and one for reading from the child |
| 102 | * (writing to the parent). Since that's four socket ends, each |
| 103 | * process has to close the two ends it doesn't need. The remaining |
| 104 | * two socket ends are retained for reading and writing. In the |
| 105 | * child, the STDIN and STDOUT file descriptors refer to these |
| 106 | * sockets. In the parent, the function arguments f_in and f_out are |
| 107 | * set to refer to these sockets. |
| 108 | */ |
| 109 | pid_t local_child(int argc, char **argv, int *f_in, int *f_out, |
| 110 | int (*child_main)(int, char*[])) |
| 111 | { |
| 112 | pid_t pid; |
| 113 | int to_child_pipe[2]; |
| 114 | int from_child_pipe[2]; |
| 115 | |
| 116 | if (fd_pair(to_child_pipe) < 0 || |
| 117 | fd_pair(from_child_pipe) < 0) { |
| 118 | rsyserr(FERROR, errno, "pipe"); |
| 119 | exit_cleanup(RERR_IPC); |
| 120 | } |
| 121 | |
| 122 | /* For read-batch, don't even fork. */ |
| 123 | pid = do_fork(); |
| 124 | |
| 125 | if (pid == -1) { |
| 126 | rsyserr(FERROR, errno, "fork"); |
| 127 | exit_cleanup(RERR_IPC); |
| 128 | } |
| 129 | |
| 130 | if (pid == 0) { |
| 131 | am_sender = !am_sender; |
| 132 | am_server = 1; |
| 133 | |
| 134 | /* The server side never writes the batch, even if it |
| 135 | * is local (it makes the logic easier elsewhere). */ |
| 136 | write_batch = 0; |
| 137 | |
| 138 | if (!am_sender) |
| 139 | filesfrom_fd = -1; |
| 140 | |
| 141 | if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 || |
| 142 | close(to_child_pipe[1]) < 0 || |
| 143 | close(from_child_pipe[0]) < 0 || |
| 144 | dup2(from_child_pipe[1], STDOUT_FILENO) < 0) { |
| 145 | rsyserr(FERROR, errno, "Failed to dup/close"); |
| 146 | exit_cleanup(RERR_IPC); |
| 147 | } |
| 148 | if (to_child_pipe[0] != STDIN_FILENO) |
| 149 | close(to_child_pipe[0]); |
| 150 | if (from_child_pipe[1] != STDOUT_FILENO) |
| 151 | close(from_child_pipe[1]); |
| 152 | child_main(argc, argv); |
| 153 | } |
| 154 | |
| 155 | if (!am_sender) |
| 156 | filesfrom_fd = -1; |
| 157 | |
| 158 | if (close(from_child_pipe[1]) < 0 || |
| 159 | close(to_child_pipe[0]) < 0) { |
| 160 | rsyserr(FERROR, errno, "Failed to close"); |
| 161 | exit_cleanup(RERR_IPC); |
| 162 | } |
| 163 | |
| 164 | *f_in = from_child_pipe[0]; |
| 165 | *f_out = to_child_pipe[1]; |
| 166 | |
| 167 | return pid; |
| 168 | } |