Got rid of RERR_LOG_FAILURE.
[rsync.git] / pipe.c
blob01306088da510ac1ad172aa7fa13b41c6aab5930
1 /* -*- c-file-style: "linux" -*-
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>
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.
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.
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.
22 #include "rsync.h"
24 extern int am_sender;
25 extern int am_server;
26 extern int blocking_io;
27 extern int orig_umask;
28 extern int filesfrom_fd;
30 /**
31 * Create a child connected to use on stdin/stdout.
33 * This is derived from CVS code
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
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 **/
43 pid_t piped_child(char **command, int *f_in, int *f_out)
45 pid_t pid;
46 int to_child_pipe[2];
47 int from_child_pipe[2];
49 if (verbose >= 2) {
50 print_child_argv(command);
53 if (fd_pair(to_child_pipe) < 0 || fd_pair(from_child_pipe) < 0) {
54 rsyserr(FERROR, errno, "pipe");
55 exit_cleanup(RERR_IPC);
58 pid = do_fork();
59 if (pid == -1) {
60 rsyserr(FERROR, errno, "fork");
61 exit_cleanup(RERR_IPC);
64 if (pid == 0) {
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) {
69 rsyserr(FERROR, errno, "Failed to dup/close");
70 exit_cleanup(RERR_IPC);
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);
78 if (blocking_io > 0)
79 set_blocking(STDOUT_FILENO);
80 execvp(command[0], command);
81 rsyserr(FERROR, errno, "Failed to exec %s",
82 safe_fname(command[0]));
83 exit_cleanup(RERR_IPC);
86 if (close(from_child_pipe[1]) < 0 || close(to_child_pipe[0]) < 0) {
87 rsyserr(FERROR, errno, "Failed to close");
88 exit_cleanup(RERR_IPC);
91 *f_in = from_child_pipe[0];
92 *f_out = to_child_pipe[1];
94 return pid;
97 /* This function forks a child which calls child_main(). First,
98 * however, it has to establish communication paths to and from the
99 * newborn child. It creates two socket pairs -- one for writing to
100 * the child (from the parent) and one for reading from the child
101 * (writing to the parent). Since that's four socket ends, each
102 * process has to close the two ends it doesn't need. The remaining
103 * two socket ends are retained for reading and writing. In the
104 * child, the STDIN and STDOUT file descriptors refer to these
105 * sockets. In the parent, the function arguments f_in and f_out are
106 * set to refer to these sockets. */
107 pid_t local_child(int argc, char **argv, int *f_in, int *f_out,
108 int (*child_main)(int, char*[]))
110 pid_t pid;
111 int to_child_pipe[2];
112 int from_child_pipe[2];
114 if (fd_pair(to_child_pipe) < 0 ||
115 fd_pair(from_child_pipe) < 0) {
116 rsyserr(FERROR, errno, "pipe");
117 exit_cleanup(RERR_IPC);
120 pid = do_fork();
121 if (pid == -1) {
122 rsyserr(FERROR, errno, "fork");
123 exit_cleanup(RERR_IPC);
126 if (pid == 0) {
127 am_sender = !am_sender;
128 am_server = 1;
130 if (!am_sender)
131 filesfrom_fd = -1;
133 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
134 close(to_child_pipe[1]) < 0 ||
135 close(from_child_pipe[0]) < 0 ||
136 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
137 rsyserr(FERROR, errno, "Failed to dup/close");
138 exit_cleanup(RERR_IPC);
140 if (to_child_pipe[0] != STDIN_FILENO)
141 close(to_child_pipe[0]);
142 if (from_child_pipe[1] != STDOUT_FILENO)
143 close(from_child_pipe[1]);
144 child_main(argc, argv);
147 if (!am_sender)
148 filesfrom_fd = -1;
150 if (close(from_child_pipe[1]) < 0 ||
151 close(to_child_pipe[0]) < 0) {
152 rsyserr(FERROR, errno, "Failed to close");
153 exit_cleanup(RERR_IPC);
156 *f_in = from_child_pipe[0];
157 *f_out = to_child_pipe[1];
159 return pid;