d47a3e98f76163fa503c20a6cb85b5f5377c6aee
[rsync/rsync.git] / pipe.c
1 /*
2  * Routines used to setup various kinds of inter-process pipes.
3  *
4  * Copyright (C) 1996-2000 Andrew Tridgell
5  * Copyright (C) 1996 Paul Mackerras
6  * Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org>
7  * Copyright (C) 2004, 2005, 2006 Wayne Davison
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
22  */
23
24 #include "rsync.h"
25
26 extern int am_sender;
27 extern int am_server;
28 extern int blocking_io;
29 extern int filesfrom_fd;
30 extern mode_t orig_umask;
31 extern char *logfile_name;
32 extern struct chmod_mode_struct *chmod_modes;
33
34 /**
35  * Create a child connected to us via its stdin/stdout.
36  *
37  * This is derived from CVS code
38  *
39  * Note that in the child STDIN is set to blocking and STDOUT
40  * is set to non-blocking. This is necessary as rsh relies on stdin being blocking
41  *  and ssh relies on stdout being non-blocking
42  *
43  * If blocking_io is set then use blocking io on both fds. That can be
44  * used to cope with badly broken rsh implementations like the one on
45  * Solaris.
46  **/
47 pid_t piped_child(char **command, int *f_in, int *f_out)
48 {
49         pid_t pid;
50         int to_child_pipe[2];
51         int from_child_pipe[2];
52
53         if (verbose >= 2) {
54                 print_child_argv(command);
55         }
56
57         if (fd_pair(to_child_pipe) < 0 || fd_pair(from_child_pipe) < 0) {
58                 rsyserr(FERROR, errno, "pipe");
59                 exit_cleanup(RERR_IPC);
60         }
61
62         pid = do_fork();
63         if (pid == -1) {
64                 rsyserr(FERROR, errno, "fork");
65                 exit_cleanup(RERR_IPC);
66         }
67
68         if (pid == 0) {
69                 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
70                     close(to_child_pipe[1]) < 0 ||
71                     close(from_child_pipe[0]) < 0 ||
72                     dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
73                         rsyserr(FERROR, errno, "Failed to dup/close");
74                         exit_cleanup(RERR_IPC);
75                 }
76                 if (to_child_pipe[0] != STDIN_FILENO)
77                         close(to_child_pipe[0]);
78                 if (from_child_pipe[1] != STDOUT_FILENO)
79                         close(from_child_pipe[1]);
80                 umask(orig_umask);
81                 set_blocking(STDIN_FILENO);
82                 if (blocking_io > 0)
83                         set_blocking(STDOUT_FILENO);
84                 execvp(command[0], command);
85                 rsyserr(FERROR, errno, "Failed to exec %s", command[0]);
86                 exit_cleanup(RERR_IPC);
87         }
88
89         if (close(from_child_pipe[1]) < 0 || close(to_child_pipe[0]) < 0) {
90                 rsyserr(FERROR, errno, "Failed to close");
91                 exit_cleanup(RERR_IPC);
92         }
93
94         *f_in = from_child_pipe[0];
95         *f_out = to_child_pipe[1];
96
97         return pid;
98 }
99
100 /* This function forks a child which calls child_main().  First,
101  * however, it has to establish communication paths to and from the
102  * newborn child.  It creates two socket pairs -- one for writing to
103  * the child (from the parent) and one for reading from the child
104  * (writing to the parent).  Since that's four socket ends, each
105  * process has to close the two ends it doesn't need.  The remaining
106  * two socket ends are retained for reading and writing.  In the
107  * child, the STDIN and STDOUT file descriptors refer to these
108  * sockets.  In the parent, the function arguments f_in and f_out are
109  * set to refer to these sockets. */
110 pid_t local_child(int argc, char **argv, int *f_in, int *f_out,
111                   int (*child_main)(int, char*[]))
112 {
113         pid_t pid;
114         int to_child_pipe[2];
115         int from_child_pipe[2];
116
117         /* The parent process is always the sender for a local rsync. */
118         assert(am_sender);
119
120         if (fd_pair(to_child_pipe) < 0 ||
121             fd_pair(from_child_pipe) < 0) {
122                 rsyserr(FERROR, errno, "pipe");
123                 exit_cleanup(RERR_IPC);
124         }
125
126         pid = do_fork();
127         if (pid == -1) {
128                 rsyserr(FERROR, errno, "fork");
129                 exit_cleanup(RERR_IPC);
130         }
131
132         if (pid == 0) {
133                 am_sender = 0;
134                 am_server = 1;
135                 filesfrom_fd = -1;
136                 chmod_modes = NULL; /* Let the sending side handle this. */
137
138                 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
139                     close(to_child_pipe[1]) < 0 ||
140                     close(from_child_pipe[0]) < 0 ||
141                     dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
142                         rsyserr(FERROR, errno, "Failed to dup/close");
143                         exit_cleanup(RERR_IPC);
144                 }
145                 if (to_child_pipe[0] != STDIN_FILENO)
146                         close(to_child_pipe[0]);
147                 if (from_child_pipe[1] != STDOUT_FILENO)
148                         close(from_child_pipe[1]);
149                 child_main(argc, argv);
150         }
151
152         /* Let the client side handle this. */
153         if (logfile_name) {
154                 logfile_name = NULL;
155                 logfile_close();
156         }
157
158         if (close(from_child_pipe[1]) < 0 ||
159             close(to_child_pipe[0]) < 0) {
160                 rsyserr(FERROR, errno, "Failed to close");
161                 exit_cleanup(RERR_IPC);
162         }
163
164         *f_in = from_child_pipe[0];
165         *f_out = to_child_pipe[1];
166
167         return pid;
168 }