Give the user an idea of how far along in the transfer we are
[rsync/rsync.git] / pipe.c
CommitLineData
fcb69e5c
MP
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
b35d0d8e
MP
22#include "rsync.h"
23
24/**
25 * Create a child connected to use on stdin/stdout.
26 *
27 * This is derived from CVS code
28 *
29 * Note that in the child STDIN is set to blocking and STDOUT
30 * is set to non-blocking. This is necessary as rsh relies on stdin being blocking
31 * and ssh relies on stdout being non-blocking
32 *
33 * If blocking_io is set then use blocking io on both fds. That can be
34 * used to cope with badly broken rsh implementations like the one on
35 * Solaris.
36 **/
37pid_t piped_child(char **command, int *f_in, int *f_out)
38{
39 pid_t pid;
40 int to_child_pipe[2];
41 int from_child_pipe[2];
42 extern int blocking_io;
43
44 if (verbose >= 2) {
45 print_child_argv(command);
46 }
47
48 if (fd_pair(to_child_pipe) < 0 || fd_pair(from_child_pipe) < 0) {
49 rprintf(FERROR, "pipe: %s\n", strerror(errno));
50 exit_cleanup(RERR_IPC);
51 }
52
53
54 pid = do_fork();
55 if (pid == -1) {
56 rprintf(FERROR, "fork: %s\n", strerror(errno));
57 exit_cleanup(RERR_IPC);
58 }
59
60 if (pid == 0) {
61 extern int orig_umask;
62 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
63 close(to_child_pipe[1]) < 0 ||
64 close(from_child_pipe[0]) < 0 ||
65 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
66 rprintf(FERROR, "Failed to dup/close : %s\n",
67 strerror(errno));
68 exit_cleanup(RERR_IPC);
69 }
70 if (to_child_pipe[0] != STDIN_FILENO)
71 close(to_child_pipe[0]);
72 if (from_child_pipe[1] != STDOUT_FILENO)
73 close(from_child_pipe[1]);
74 umask(orig_umask);
75 set_blocking(STDIN_FILENO);
7d3f8ae2 76 if (blocking_io > 0)
b35d0d8e 77 set_blocking(STDOUT_FILENO);
b35d0d8e
MP
78 execvp(command[0], command);
79 rprintf(FERROR, "Failed to exec %s : %s\n",
80 command[0], strerror(errno));
81 exit_cleanup(RERR_IPC);
82 }
83
84 if (close(from_child_pipe[1]) < 0 || close(to_child_pipe[0]) < 0) {
85 rprintf(FERROR, "Failed to close : %s\n", strerror(errno));
86 exit_cleanup(RERR_IPC);
87 }
88
89 *f_in = from_child_pipe[0];
90 *f_out = to_child_pipe[1];
91
92 return pid;
93}
94
95pid_t local_child(int argc, char **argv,int *f_in,int *f_out,
b44be3e9 96 int (*child_main)(int, char*[]))
b35d0d8e
MP
97{
98 pid_t pid;
99 int to_child_pipe[2];
100 int from_child_pipe[2];
7c2a9e76
WD
101 extern int read_batch;
102 extern int am_sender;
103 extern int am_server;
104 extern int filesfrom_fd;
b35d0d8e
MP
105
106 if (fd_pair(to_child_pipe) < 0 ||
107 fd_pair(from_child_pipe) < 0) {
108 rprintf(FERROR,"pipe: %s\n",strerror(errno));
109 exit_cleanup(RERR_IPC);
110 }
111
112
113 pid = do_fork();
114 if (pid == -1) {
115 rprintf(FERROR,"fork: %s\n",strerror(errno));
116 exit_cleanup(RERR_IPC);
117 }
118
119 if (pid == 0) {
b35d0d8e
MP
120 am_sender = read_batch ? 0 : !am_sender;
121 am_server = 1;
122
7c2a9e76
WD
123 if (!am_sender)
124 filesfrom_fd = -1;
125
b35d0d8e
MP
126 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
127 close(to_child_pipe[1]) < 0 ||
128 close(from_child_pipe[0]) < 0 ||
129 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
130 rprintf(FERROR,"Failed to dup/close : %s\n",strerror(errno));
131 exit_cleanup(RERR_IPC);
132 }
133 if (to_child_pipe[0] != STDIN_FILENO) close(to_child_pipe[0]);
134 if (from_child_pipe[1] != STDOUT_FILENO) close(from_child_pipe[1]);
135 child_main(argc, argv);
136 }
137
7c2a9e76
WD
138 if (!am_sender)
139 filesfrom_fd = -1;
140
b35d0d8e
MP
141 if (close(from_child_pipe[1]) < 0 ||
142 close(to_child_pipe[0]) < 0) {
143 rprintf(FERROR,"Failed to close : %s\n",strerror(errno));
144 exit_cleanup(RERR_IPC);
145 }
146
147 *f_in = from_child_pipe[0];
148 *f_out = to_child_pipe[1];
149
150 return pid;
151}