2 * Routines used to setup various kinds of inter-process pipes.
4 * Copyright (C) 1996-2000 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org>
7 * Copyright (C) 2004-2007 Wayne Davison
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
27 extern int blocking_io;
28 extern int filesfrom_fd;
29 extern mode_t orig_umask;
30 extern char *logfile_name;
31 extern struct chmod_mode_struct *chmod_modes;
34 * Create a child connected to us via its stdin/stdout.
36 * This is derived from CVS code
38 * Note that in the child STDIN is set to blocking and STDOUT
39 * is set to non-blocking. This is necessary as rsh relies on stdin being blocking
40 * and ssh relies on stdout being non-blocking
42 * If blocking_io is set then use blocking io on both fds. That can be
43 * used to cope with badly broken rsh implementations like the one on
46 pid_t piped_child(char **command, int *f_in, int *f_out)
50 int from_child_pipe[2];
53 print_child_argv(command);
55 if (fd_pair(to_child_pipe) < 0 || fd_pair(from_child_pipe) < 0) {
56 rsyserr(FERROR, errno, "pipe");
57 exit_cleanup(RERR_IPC);
62 rsyserr(FERROR, errno, "fork");
63 exit_cleanup(RERR_IPC);
67 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
68 close(to_child_pipe[1]) < 0 ||
69 close(from_child_pipe[0]) < 0 ||
70 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
71 rsyserr(FERROR, errno, "Failed to dup/close");
72 exit_cleanup(RERR_IPC);
74 if (to_child_pipe[0] != STDIN_FILENO)
75 close(to_child_pipe[0]);
76 if (from_child_pipe[1] != STDOUT_FILENO)
77 close(from_child_pipe[1]);
79 set_blocking(STDIN_FILENO);
81 set_blocking(STDOUT_FILENO);
82 execvp(command[0], command);
83 rsyserr(FERROR, errno, "Failed to exec %s", command[0]);
84 exit_cleanup(RERR_IPC);
87 if (close(from_child_pipe[1]) < 0 || close(to_child_pipe[0]) < 0) {
88 rsyserr(FERROR, errno, "Failed to close");
89 exit_cleanup(RERR_IPC);
92 *f_in = from_child_pipe[0];
93 *f_out = to_child_pipe[1];
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 pid_t local_child(int argc, char **argv, int *f_in, int *f_out,
109 int (*child_main)(int, char*[]))
112 int to_child_pipe[2];
113 int from_child_pipe[2];
115 /* The parent process is always the sender for a local rsync. */
118 if (fd_pair(to_child_pipe) < 0 ||
119 fd_pair(from_child_pipe) < 0) {
120 rsyserr(FERROR, errno, "pipe");
121 exit_cleanup(RERR_IPC);
126 rsyserr(FERROR, errno, "fork");
127 exit_cleanup(RERR_IPC);
134 chmod_modes = NULL; /* Let the sending side handle this. */
136 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
137 close(to_child_pipe[1]) < 0 ||
138 close(from_child_pipe[0]) < 0 ||
139 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
140 rsyserr(FERROR, errno, "Failed to dup/close");
141 exit_cleanup(RERR_IPC);
143 if (to_child_pipe[0] != STDIN_FILENO)
144 close(to_child_pipe[0]);
145 if (from_child_pipe[1] != STDOUT_FILENO)
146 close(from_child_pipe[1]);
147 child_main(argc, argv);
150 /* Let the client side handle this. */
156 if (close(from_child_pipe[1]) < 0 ||
157 close(to_child_pipe[0]) < 0) {
158 rsyserr(FERROR, errno, "Failed to close");
159 exit_cleanup(RERR_IPC);
162 *f_in = from_child_pipe[0];
163 *f_out = to_child_pipe[1];