The "len" to generate_and_send_sums() is supposed to be an OFF_T.
[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;
28extern int read_batch;
29extern int filesfrom_fd;
30
b35d0d8e
MP
31/**
32 * Create a child connected to use on stdin/stdout.
33 *
e2ccd357
WD
34 * This is derived from CVS code
35 *
b35d0d8e
MP
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 **/
44pid_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];
e2ccd357 49
b35d0d8e
MP
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) {
d62bcc17 55 rsyserr(FERROR, errno, "pipe");
b35d0d8e
MP
56 exit_cleanup(RERR_IPC);
57 }
58
b35d0d8e
MP
59 pid = do_fork();
60 if (pid == -1) {
d62bcc17 61 rsyserr(FERROR, errno, "fork");
b35d0d8e
MP
62 exit_cleanup(RERR_IPC);
63 }
64
65 if (pid == 0) {
b35d0d8e
MP
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) {
d62bcc17 70 rsyserr(FERROR, errno, "Failed to dup/close");
b35d0d8e
MP
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);
7d3f8ae2 79 if (blocking_io > 0)
b35d0d8e 80 set_blocking(STDOUT_FILENO);
b35d0d8e 81 execvp(command[0], command);
d62bcc17 82 rsyserr(FERROR, errno, "Failed to exec %s", command[0]);
b35d0d8e
MP
83 exit_cleanup(RERR_IPC);
84 }
85
86 if (close(from_child_pipe[1]) < 0 || close(to_child_pipe[0]) < 0) {
d62bcc17 87 rsyserr(FERROR, errno, "Failed to close");
b35d0d8e
MP
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
97pid_t local_child(int argc, char **argv,int *f_in,int *f_out,
b44be3e9 98 int (*child_main)(int, char*[]))
b35d0d8e
MP
99{
100 pid_t pid;
101 int to_child_pipe[2];
102 int from_child_pipe[2];
b35d0d8e
MP
103
104 if (fd_pair(to_child_pipe) < 0 ||
105 fd_pair(from_child_pipe) < 0) {
d62bcc17 106 rsyserr(FERROR, errno, "pipe");
b35d0d8e
MP
107 exit_cleanup(RERR_IPC);
108 }
109
b35d0d8e
MP
110 pid = do_fork();
111 if (pid == -1) {
d62bcc17 112 rsyserr(FERROR, errno, "fork");
b35d0d8e
MP
113 exit_cleanup(RERR_IPC);
114 }
115
116 if (pid == 0) {
b35d0d8e 117 am_sender = read_batch ? 0 : !am_sender;
e2ccd357 118 am_server = 1;
b35d0d8e 119
7c2a9e76
WD
120 if (!am_sender)
121 filesfrom_fd = -1;
122
b35d0d8e
MP
123 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
124 close(to_child_pipe[1]) < 0 ||
125 close(from_child_pipe[0]) < 0 ||
126 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
d62bcc17 127 rsyserr(FERROR, errno, "Failed to dup/close");
b35d0d8e
MP
128 exit_cleanup(RERR_IPC);
129 }
130 if (to_child_pipe[0] != STDIN_FILENO) close(to_child_pipe[0]);
131 if (from_child_pipe[1] != STDOUT_FILENO) close(from_child_pipe[1]);
132 child_main(argc, argv);
133 }
134
7c2a9e76
WD
135 if (!am_sender)
136 filesfrom_fd = -1;
137
b35d0d8e
MP
138 if (close(from_child_pipe[1]) < 0 ||
139 close(to_child_pipe[0]) < 0) {
e2ccd357 140 rsyserr(FERROR, errno, "Failed to close");
b35d0d8e
MP
141 exit_cleanup(RERR_IPC);
142 }
143
144 *f_in = from_child_pipe[0];
145 *f_out = to_child_pipe[1];
e2ccd357 146
b35d0d8e
MP
147 return pid;
148}