Commit | Line | Data |
---|---|---|
0f78b815 WD |
1 | /* |
2 | * Routines used to setup various kinds of inter-process pipes. | |
e2ccd357 | 3 | * |
0f78b815 WD |
4 | * Copyright (C) 1996-2000 Andrew Tridgell |
5 | * Copyright (C) 1996 Paul Mackerras | |
6 | * Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org> | |
ba2133d6 | 7 | * Copyright (C) 2004-2007 Wayne Davison |
e2ccd357 | 8 | * |
fcb69e5c | 9 | * This program is free software; you can redistribute it and/or modify |
4fd842f9 | 10 | * it under the terms of the GNU General Public License version 3 as |
ba2133d6 | 11 | * published by the Free Software Foundation. |
e2ccd357 | 12 | * |
fcb69e5c MP |
13 | * This program is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
e2ccd357 | 17 | * |
e7c67065 | 18 | * You should have received a copy of the GNU General Public License along |
4fd842f9 | 19 | * with this program; if not, visit the http://fsf.org website. |
fcb69e5c MP |
20 | */ |
21 | ||
b35d0d8e MP |
22 | #include "rsync.h" |
23 | ||
72a6e631 WD |
24 | extern int am_sender; |
25 | extern int am_server; | |
26 | extern int blocking_io; | |
72a6e631 | 27 | extern int filesfrom_fd; |
c5b7aa15 | 28 | extern mode_t orig_umask; |
887d7455 | 29 | extern char *logfile_name; |
ba30fb5c | 30 | extern struct chmod_mode_struct *chmod_modes; |
72a6e631 | 31 | |
b35d0d8e | 32 | /** |
20c1926a | 33 | * Create a child connected to us via its stdin/stdout. |
b35d0d8e | 34 | * |
e2ccd357 WD |
35 | * This is derived from CVS code |
36 | * | |
b35d0d8e MP |
37 | * Note that in the child STDIN is set to blocking and STDOUT |
38 | * is set to non-blocking. This is necessary as rsh relies on stdin being blocking | |
39 | * and ssh relies on stdout being non-blocking | |
40 | * | |
41 | * If blocking_io is set then use blocking io on both fds. That can be | |
42 | * used to cope with badly broken rsh implementations like the one on | |
43 | * Solaris. | |
44 | **/ | |
45 | pid_t piped_child(char **command, int *f_in, int *f_out) | |
46 | { | |
47 | pid_t pid; | |
48 | int to_child_pipe[2]; | |
49 | int from_child_pipe[2]; | |
e2ccd357 | 50 | |
4a19c3b2 | 51 | if (verbose >= 2) |
b35d0d8e | 52 | print_child_argv(command); |
b35d0d8e MP |
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); |
45c49b52 | 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 | ||
391516da | 97 | /* This function forks a child which calls child_main(). First, |
b9f592fb WD |
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 | |
391516da | 106 | * set to refer to these sockets. */ |
b9f592fb | 107 | pid_t local_child(int argc, char **argv, int *f_in, int *f_out, |
b44be3e9 | 108 | int (*child_main)(int, char*[])) |
b35d0d8e MP |
109 | { |
110 | pid_t pid; | |
111 | int to_child_pipe[2]; | |
112 | int from_child_pipe[2]; | |
b35d0d8e | 113 | |
ba30fb5c WD |
114 | /* The parent process is always the sender for a local rsync. */ |
115 | assert(am_sender); | |
116 | ||
b35d0d8e MP |
117 | if (fd_pair(to_child_pipe) < 0 || |
118 | fd_pair(from_child_pipe) < 0) { | |
d62bcc17 | 119 | rsyserr(FERROR, errno, "pipe"); |
b35d0d8e MP |
120 | exit_cleanup(RERR_IPC); |
121 | } | |
122 | ||
8c90957f | 123 | pid = do_fork(); |
b35d0d8e | 124 | if (pid == -1) { |
d62bcc17 | 125 | rsyserr(FERROR, errno, "fork"); |
b35d0d8e MP |
126 | exit_cleanup(RERR_IPC); |
127 | } | |
128 | ||
129 | if (pid == 0) { | |
ba30fb5c | 130 | am_sender = 0; |
e2ccd357 | 131 | am_server = 1; |
ba30fb5c WD |
132 | filesfrom_fd = -1; |
133 | chmod_modes = NULL; /* Let the sending side handle this. */ | |
7c2a9e76 | 134 | |
b35d0d8e MP |
135 | if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 || |
136 | close(to_child_pipe[1]) < 0 || | |
137 | close(from_child_pipe[0]) < 0 || | |
138 | dup2(from_child_pipe[1], STDOUT_FILENO) < 0) { | |
d62bcc17 | 139 | rsyserr(FERROR, errno, "Failed to dup/close"); |
b35d0d8e MP |
140 | exit_cleanup(RERR_IPC); |
141 | } | |
3896bca4 WD |
142 | if (to_child_pipe[0] != STDIN_FILENO) |
143 | close(to_child_pipe[0]); | |
144 | if (from_child_pipe[1] != STDOUT_FILENO) | |
145 | close(from_child_pipe[1]); | |
b35d0d8e MP |
146 | child_main(argc, argv); |
147 | } | |
148 | ||
887d7455 WD |
149 | /* Let the client side handle this. */ |
150 | if (logfile_name) { | |
151 | logfile_name = NULL; | |
152 | logfile_close(); | |
153 | } | |
154 | ||
b35d0d8e MP |
155 | if (close(from_child_pipe[1]) < 0 || |
156 | close(to_child_pipe[0]) < 0) { | |
e2ccd357 | 157 | rsyserr(FERROR, errno, "Failed to close"); |
b35d0d8e MP |
158 | exit_cleanup(RERR_IPC); |
159 | } | |
160 | ||
161 | *f_in = from_child_pipe[0]; | |
162 | *f_out = to_child_pipe[1]; | |
e2ccd357 | 163 | |
b35d0d8e MP |
164 | return pid; |
165 | } |