Added new IOERR_* defines.
[rsync.git] / pipe.c
blob59a0d762643290c93eb0403bc09dfaefec7b56b8
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.
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 /**
25 * Create a child connected to use on stdin/stdout.
27 * This is derived from CVS code
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
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 **/
37 pid_t piped_child(char **command, int *f_in, int *f_out)
39 pid_t pid;
40 int to_child_pipe[2];
41 int from_child_pipe[2];
42 extern int blocking_io;
44 if (verbose >= 2) {
45 print_child_argv(command);
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);
54 pid = do_fork();
55 if (pid == -1) {
56 rprintf(FERROR, "fork: %s\n", strerror(errno));
57 exit_cleanup(RERR_IPC);
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);
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);
76 if (blocking_io) {
77 set_blocking(STDOUT_FILENO);
79 execvp(command[0], command);
80 rprintf(FERROR, "Failed to exec %s : %s\n",
81 command[0], strerror(errno));
82 exit_cleanup(RERR_IPC);
85 if (close(from_child_pipe[1]) < 0 || close(to_child_pipe[0]) < 0) {
86 rprintf(FERROR, "Failed to close : %s\n", strerror(errno));
87 exit_cleanup(RERR_IPC);
90 *f_in = from_child_pipe[0];
91 *f_out = to_child_pipe[1];
93 return pid;
96 pid_t local_child(int argc, char **argv,int *f_in,int *f_out,
97 int (*child_main)(int, char*[]))
99 pid_t pid;
100 int to_child_pipe[2];
101 int from_child_pipe[2];
102 extern int read_batch;
103 extern int am_sender;
104 extern int am_server;
105 extern int filesfrom_fd;
107 if (fd_pair(to_child_pipe) < 0 ||
108 fd_pair(from_child_pipe) < 0) {
109 rprintf(FERROR,"pipe: %s\n",strerror(errno));
110 exit_cleanup(RERR_IPC);
114 pid = do_fork();
115 if (pid == -1) {
116 rprintf(FERROR,"fork: %s\n",strerror(errno));
117 exit_cleanup(RERR_IPC);
120 if (pid == 0) {
121 am_sender = read_batch ? 0 : !am_sender;
122 am_server = 1;
124 if (!am_sender)
125 filesfrom_fd = -1;
127 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
128 close(to_child_pipe[1]) < 0 ||
129 close(from_child_pipe[0]) < 0 ||
130 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
131 rprintf(FERROR,"Failed to dup/close : %s\n",strerror(errno));
132 exit_cleanup(RERR_IPC);
134 if (to_child_pipe[0] != STDIN_FILENO) close(to_child_pipe[0]);
135 if (from_child_pipe[1] != STDOUT_FILENO) close(from_child_pipe[1]);
136 child_main(argc, argv);
139 if (!am_sender)
140 filesfrom_fd = -1;
142 if (close(from_child_pipe[1]) < 0 ||
143 close(to_child_pipe[0]) < 0) {
144 rprintf(FERROR,"Failed to close : %s\n",strerror(errno));
145 exit_cleanup(RERR_IPC);
148 *f_in = from_child_pipe[0];
149 *f_out = to_child_pipe[1];
151 return pid;