We don't ever need to send the --chmod option to the remote rsync.
[rsync/rsync.git] / pipe.c
CommitLineData
fcb69e5c 1/* -*- c-file-style: "linux" -*-
e2ccd357
WD
2 *
3 * Copyright (C) 1996-2000 by Andrew Tridgell
fcb69e5c
MP
4 * Copyright (C) Paul Mackerras 1996
5 * Copyright (C) 2001, 2002 by Martin Pool <mbp@samba.org>
e2ccd357 6 *
fcb69e5c
MP
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.
e2ccd357 11 *
fcb69e5c
MP
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.
e2ccd357 16 *
fcb69e5c
MP
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
b35d0d8e
MP
22#include "rsync.h"
23
72a6e631
WD
24extern int am_sender;
25extern int am_server;
26extern int blocking_io;
27extern int orig_umask;
72a6e631
WD
28extern int filesfrom_fd;
29
b35d0d8e 30/**
20c1926a 31 * Create a child connected to us via its stdin/stdout.
b35d0d8e 32 *
e2ccd357
WD
33 * This is derived from CVS code
34 *
b35d0d8e
MP
35 * Note that in the child STDIN is set to blocking and STDOUT
36 * is set to non-blocking. This is necessary as rsh relies on stdin being blocking
37 * and ssh relies on stdout being non-blocking
38 *
39 * If blocking_io is set then use blocking io on both fds. That can be
40 * used to cope with badly broken rsh implementations like the one on
41 * Solaris.
42 **/
43pid_t piped_child(char **command, int *f_in, int *f_out)
44{
45 pid_t pid;
46 int to_child_pipe[2];
47 int from_child_pipe[2];
e2ccd357 48
b35d0d8e
MP
49 if (verbose >= 2) {
50 print_child_argv(command);
51 }
52
53 if (fd_pair(to_child_pipe) < 0 || fd_pair(from_child_pipe) < 0) {
d62bcc17 54 rsyserr(FERROR, errno, "pipe");
b35d0d8e
MP
55 exit_cleanup(RERR_IPC);
56 }
57
b35d0d8e
MP
58 pid = do_fork();
59 if (pid == -1) {
d62bcc17 60 rsyserr(FERROR, errno, "fork");
b35d0d8e
MP
61 exit_cleanup(RERR_IPC);
62 }
63
64 if (pid == 0) {
b35d0d8e
MP
65 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
66 close(to_child_pipe[1]) < 0 ||
67 close(from_child_pipe[0]) < 0 ||
68 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
d62bcc17 69 rsyserr(FERROR, errno, "Failed to dup/close");
b35d0d8e
MP
70 exit_cleanup(RERR_IPC);
71 }
72 if (to_child_pipe[0] != STDIN_FILENO)
73 close(to_child_pipe[0]);
74 if (from_child_pipe[1] != STDOUT_FILENO)
75 close(from_child_pipe[1]);
76 umask(orig_umask);
77 set_blocking(STDIN_FILENO);
7d3f8ae2 78 if (blocking_io > 0)
b35d0d8e 79 set_blocking(STDOUT_FILENO);
b35d0d8e 80 execvp(command[0], command);
45c49b52 81 rsyserr(FERROR, errno, "Failed to exec %s", command[0]);
b35d0d8e
MP
82 exit_cleanup(RERR_IPC);
83 }
84
85 if (close(from_child_pipe[1]) < 0 || close(to_child_pipe[0]) < 0) {
d62bcc17 86 rsyserr(FERROR, errno, "Failed to close");
b35d0d8e
MP
87 exit_cleanup(RERR_IPC);
88 }
89
90 *f_in = from_child_pipe[0];
91 *f_out = to_child_pipe[1];
92
93 return pid;
94}
95
391516da 96/* This function forks a child which calls child_main(). First,
b9f592fb
WD
97 * however, it has to establish communication paths to and from the
98 * newborn child. It creates two socket pairs -- one for writing to
99 * the child (from the parent) and one for reading from the child
100 * (writing to the parent). Since that's four socket ends, each
101 * process has to close the two ends it doesn't need. The remaining
102 * two socket ends are retained for reading and writing. In the
103 * child, the STDIN and STDOUT file descriptors refer to these
104 * sockets. In the parent, the function arguments f_in and f_out are
391516da 105 * set to refer to these sockets. */
b9f592fb 106pid_t local_child(int argc, char **argv, int *f_in, int *f_out,
b44be3e9 107 int (*child_main)(int, char*[]))
b35d0d8e
MP
108{
109 pid_t pid;
110 int to_child_pipe[2];
111 int from_child_pipe[2];
b35d0d8e
MP
112
113 if (fd_pair(to_child_pipe) < 0 ||
114 fd_pair(from_child_pipe) < 0) {
d62bcc17 115 rsyserr(FERROR, errno, "pipe");
b35d0d8e
MP
116 exit_cleanup(RERR_IPC);
117 }
118
8c90957f 119 pid = do_fork();
b35d0d8e 120 if (pid == -1) {
d62bcc17 121 rsyserr(FERROR, errno, "fork");
b35d0d8e
MP
122 exit_cleanup(RERR_IPC);
123 }
124
125 if (pid == 0) {
b9f592fb 126 am_sender = !am_sender;
e2ccd357 127 am_server = 1;
b35d0d8e 128
7c2a9e76
WD
129 if (!am_sender)
130 filesfrom_fd = -1;
131
b35d0d8e
MP
132 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
133 close(to_child_pipe[1]) < 0 ||
134 close(from_child_pipe[0]) < 0 ||
135 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
d62bcc17 136 rsyserr(FERROR, errno, "Failed to dup/close");
b35d0d8e
MP
137 exit_cleanup(RERR_IPC);
138 }
3896bca4
WD
139 if (to_child_pipe[0] != STDIN_FILENO)
140 close(to_child_pipe[0]);
141 if (from_child_pipe[1] != STDOUT_FILENO)
142 close(from_child_pipe[1]);
b35d0d8e
MP
143 child_main(argc, argv);
144 }
145
7c2a9e76
WD
146 if (!am_sender)
147 filesfrom_fd = -1;
148
b35d0d8e
MP
149 if (close(from_child_pipe[1]) < 0 ||
150 close(to_child_pipe[0]) < 0) {
e2ccd357 151 rsyserr(FERROR, errno, "Failed to close");
b35d0d8e
MP
152 exit_cleanup(RERR_IPC);
153 }
154
155 *f_in = from_child_pipe[0];
156 *f_out = to_child_pipe[1];
e2ccd357 157
b35d0d8e
MP
158 return pid;
159}