The ndx arg passed to increment_active_files() is now the unique,
[rsync/rsync.git] / pipe.c
CommitLineData
0f78b815
WD
1/*
2 * Routines used to setup various kinds of inter-process pipes.
e2ccd357 3 *
0f78b815
WD
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, 2005, 2006 Wayne Davison
e2ccd357 8 *
fcb69e5c
MP
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
e2ccd357 13 *
fcb69e5c
MP
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
e2ccd357 18 *
e7c67065
WD
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
fcb69e5c
MP
22 */
23
b35d0d8e
MP
24#include "rsync.h"
25
72a6e631
WD
26extern int am_sender;
27extern int am_server;
28extern int blocking_io;
72a6e631 29extern int filesfrom_fd;
c5b7aa15 30extern mode_t orig_umask;
887d7455 31extern char *logfile_name;
ba30fb5c 32extern struct chmod_mode_struct *chmod_modes;
72a6e631 33
b35d0d8e 34/**
20c1926a 35 * Create a child connected to us via its stdin/stdout.
b35d0d8e 36 *
e2ccd357
WD
37 * This is derived from CVS code
38 *
b35d0d8e
MP
39 * Note that in the child STDIN is set to blocking and STDOUT
40 * is set to non-blocking. This is necessary as rsh relies on stdin being blocking
41 * and ssh relies on stdout being non-blocking
42 *
43 * If blocking_io is set then use blocking io on both fds. That can be
44 * used to cope with badly broken rsh implementations like the one on
45 * Solaris.
46 **/
47pid_t piped_child(char **command, int *f_in, int *f_out)
48{
49 pid_t pid;
50 int to_child_pipe[2];
51 int from_child_pipe[2];
e2ccd357 52
4a19c3b2 53 if (verbose >= 2)
b35d0d8e 54 print_child_argv(command);
b35d0d8e
MP
55
56 if (fd_pair(to_child_pipe) < 0 || fd_pair(from_child_pipe) < 0) {
d62bcc17 57 rsyserr(FERROR, errno, "pipe");
b35d0d8e
MP
58 exit_cleanup(RERR_IPC);
59 }
60
b35d0d8e
MP
61 pid = do_fork();
62 if (pid == -1) {
d62bcc17 63 rsyserr(FERROR, errno, "fork");
b35d0d8e
MP
64 exit_cleanup(RERR_IPC);
65 }
66
67 if (pid == 0) {
b35d0d8e
MP
68 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
69 close(to_child_pipe[1]) < 0 ||
70 close(from_child_pipe[0]) < 0 ||
71 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
d62bcc17 72 rsyserr(FERROR, errno, "Failed to dup/close");
b35d0d8e
MP
73 exit_cleanup(RERR_IPC);
74 }
75 if (to_child_pipe[0] != STDIN_FILENO)
76 close(to_child_pipe[0]);
77 if (from_child_pipe[1] != STDOUT_FILENO)
78 close(from_child_pipe[1]);
79 umask(orig_umask);
80 set_blocking(STDIN_FILENO);
7d3f8ae2 81 if (blocking_io > 0)
b35d0d8e 82 set_blocking(STDOUT_FILENO);
b35d0d8e 83 execvp(command[0], command);
45c49b52 84 rsyserr(FERROR, errno, "Failed to exec %s", command[0]);
b35d0d8e
MP
85 exit_cleanup(RERR_IPC);
86 }
87
88 if (close(from_child_pipe[1]) < 0 || close(to_child_pipe[0]) < 0) {
d62bcc17 89 rsyserr(FERROR, errno, "Failed to close");
b35d0d8e
MP
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
391516da 99/* This function forks a child which calls child_main(). First,
b9f592fb
WD
100 * however, it has to establish communication paths to and from the
101 * newborn child. It creates two socket pairs -- one for writing to
102 * the child (from the parent) and one for reading from the child
103 * (writing to the parent). Since that's four socket ends, each
104 * process has to close the two ends it doesn't need. The remaining
105 * two socket ends are retained for reading and writing. In the
106 * child, the STDIN and STDOUT file descriptors refer to these
107 * sockets. In the parent, the function arguments f_in and f_out are
391516da 108 * set to refer to these sockets. */
b9f592fb 109pid_t local_child(int argc, char **argv, int *f_in, int *f_out,
b44be3e9 110 int (*child_main)(int, char*[]))
b35d0d8e
MP
111{
112 pid_t pid;
113 int to_child_pipe[2];
114 int from_child_pipe[2];
b35d0d8e 115
ba30fb5c
WD
116 /* The parent process is always the sender for a local rsync. */
117 assert(am_sender);
118
b35d0d8e
MP
119 if (fd_pair(to_child_pipe) < 0 ||
120 fd_pair(from_child_pipe) < 0) {
d62bcc17 121 rsyserr(FERROR, errno, "pipe");
b35d0d8e
MP
122 exit_cleanup(RERR_IPC);
123 }
124
8c90957f 125 pid = do_fork();
b35d0d8e 126 if (pid == -1) {
d62bcc17 127 rsyserr(FERROR, errno, "fork");
b35d0d8e
MP
128 exit_cleanup(RERR_IPC);
129 }
130
131 if (pid == 0) {
ba30fb5c 132 am_sender = 0;
e2ccd357 133 am_server = 1;
ba30fb5c
WD
134 filesfrom_fd = -1;
135 chmod_modes = NULL; /* Let the sending side handle this. */
7c2a9e76 136
b35d0d8e
MP
137 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
138 close(to_child_pipe[1]) < 0 ||
139 close(from_child_pipe[0]) < 0 ||
140 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
d62bcc17 141 rsyserr(FERROR, errno, "Failed to dup/close");
b35d0d8e
MP
142 exit_cleanup(RERR_IPC);
143 }
3896bca4
WD
144 if (to_child_pipe[0] != STDIN_FILENO)
145 close(to_child_pipe[0]);
146 if (from_child_pipe[1] != STDOUT_FILENO)
147 close(from_child_pipe[1]);
b35d0d8e
MP
148 child_main(argc, argv);
149 }
150
887d7455
WD
151 /* Let the client side handle this. */
152 if (logfile_name) {
153 logfile_name = NULL;
154 logfile_close();
155 }
156
b35d0d8e
MP
157 if (close(from_child_pipe[1]) < 0 ||
158 close(to_child_pipe[0]) < 0) {
e2ccd357 159 rsyserr(FERROR, errno, "Failed to close");
b35d0d8e
MP
160 exit_cleanup(RERR_IPC);
161 }
162
163 *f_in = from_child_pipe[0];
164 *f_out = to_child_pipe[1];
e2ccd357 165
b35d0d8e
MP
166 return pid;
167}