Commit | Line | Data |
---|---|---|
fcb69e5c | 1 | /* -*- c-file-style: "linux" -*- |
e2ccd357 WD |
2 | * |
3 | * Copyright (C) 1996-2000 by Andrew Tridgell | |
fcb69e5c MP |
4 | * Copyright (C) Paul Mackerras 1996 |
5 | * Copyright (C) 2001, 2002 by Martin Pool <mbp@samba.org> | |
e2ccd357 | 6 | * |
fcb69e5c MP |
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. | |
e2ccd357 | 11 | * |
fcb69e5c MP |
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. | |
e2ccd357 | 16 | * |
fcb69e5c MP |
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 | ||
72a6e631 WD |
24 | extern int am_sender; |
25 | extern int am_server; | |
26 | extern int blocking_io; | |
27 | extern int orig_umask; | |
28 | extern int read_batch; | |
b9f592fb | 29 | extern int write_batch; |
72a6e631 WD |
30 | extern int filesfrom_fd; |
31 | ||
b35d0d8e MP |
32 | /** |
33 | * Create a child connected to use on stdin/stdout. | |
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 | |
b35d0d8e MP |
51 | if (verbose >= 2) { |
52 | print_child_argv(command); | |
53 | } | |
54 | ||
55 | if (fd_pair(to_child_pipe) < 0 || fd_pair(from_child_pipe) < 0) { | |
d62bcc17 | 56 | rsyserr(FERROR, errno, "pipe"); |
b35d0d8e MP |
57 | exit_cleanup(RERR_IPC); |
58 | } | |
59 | ||
b35d0d8e MP |
60 | pid = do_fork(); |
61 | if (pid == -1) { | |
d62bcc17 | 62 | rsyserr(FERROR, errno, "fork"); |
b35d0d8e MP |
63 | exit_cleanup(RERR_IPC); |
64 | } | |
65 | ||
66 | if (pid == 0) { | |
b35d0d8e MP |
67 | if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 || |
68 | close(to_child_pipe[1]) < 0 || | |
69 | close(from_child_pipe[0]) < 0 || | |
70 | dup2(from_child_pipe[1], STDOUT_FILENO) < 0) { | |
d62bcc17 | 71 | rsyserr(FERROR, errno, "Failed to dup/close"); |
b35d0d8e MP |
72 | exit_cleanup(RERR_IPC); |
73 | } | |
74 | if (to_child_pipe[0] != STDIN_FILENO) | |
75 | close(to_child_pipe[0]); | |
76 | if (from_child_pipe[1] != STDOUT_FILENO) | |
77 | close(from_child_pipe[1]); | |
78 | umask(orig_umask); | |
79 | set_blocking(STDIN_FILENO); | |
7d3f8ae2 | 80 | if (blocking_io > 0) |
b35d0d8e | 81 | set_blocking(STDOUT_FILENO); |
b35d0d8e | 82 | execvp(command[0], command); |
d62bcc17 | 83 | rsyserr(FERROR, errno, "Failed to exec %s", command[0]); |
b35d0d8e MP |
84 | exit_cleanup(RERR_IPC); |
85 | } | |
86 | ||
87 | if (close(from_child_pipe[1]) < 0 || close(to_child_pipe[0]) < 0) { | |
d62bcc17 | 88 | rsyserr(FERROR, errno, "Failed to close"); |
b35d0d8e MP |
89 | exit_cleanup(RERR_IPC); |
90 | } | |
91 | ||
92 | *f_in = from_child_pipe[0]; | |
93 | *f_out = to_child_pipe[1]; | |
94 | ||
95 | return pid; | |
96 | } | |
97 | ||
b9f592fb WD |
98 | /* |
99 | * This function forks a child which calls child_main(). First, | |
100 | * however, it has to establish communication paths to and from the | |
101 | * newborn child. It creates two socket pairs -- one for writing to | |
102 | * the child (from the parent) and one for reading from the child | |
103 | * (writing to the parent). Since that's four socket ends, each | |
104 | * process has to close the two ends it doesn't need. The remaining | |
105 | * two socket ends are retained for reading and writing. In the | |
106 | * child, the STDIN and STDOUT file descriptors refer to these | |
107 | * sockets. In the parent, the function arguments f_in and f_out are | |
108 | * set to refer to these sockets. | |
109 | */ | |
110 | pid_t local_child(int argc, char **argv, int *f_in, int *f_out, | |
b44be3e9 | 111 | int (*child_main)(int, char*[])) |
b35d0d8e MP |
112 | { |
113 | pid_t pid; | |
114 | int to_child_pipe[2]; | |
115 | int from_child_pipe[2]; | |
b35d0d8e MP |
116 | |
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 | ||
b9f592fb WD |
123 | /* For read-batch, don't even fork. */ |
124 | pid = read_batch ? getpid() : do_fork(); | |
125 | ||
b35d0d8e | 126 | if (pid == -1) { |
d62bcc17 | 127 | rsyserr(FERROR, errno, "fork"); |
b35d0d8e MP |
128 | exit_cleanup(RERR_IPC); |
129 | } | |
130 | ||
131 | if (pid == 0) { | |
b9f592fb | 132 | am_sender = !am_sender; |
e2ccd357 | 133 | am_server = 1; |
b35d0d8e | 134 | |
b9f592fb WD |
135 | /* The server side never writes the batch, even if it |
136 | * is local (it makes the logic easier elsewhere). */ | |
137 | write_batch = 0; | |
138 | ||
7c2a9e76 WD |
139 | if (!am_sender) |
140 | filesfrom_fd = -1; | |
141 | ||
b35d0d8e MP |
142 | if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 || |
143 | close(to_child_pipe[1]) < 0 || | |
144 | close(from_child_pipe[0]) < 0 || | |
145 | dup2(from_child_pipe[1], STDOUT_FILENO) < 0) { | |
d62bcc17 | 146 | rsyserr(FERROR, errno, "Failed to dup/close"); |
b35d0d8e MP |
147 | exit_cleanup(RERR_IPC); |
148 | } | |
3896bca4 WD |
149 | if (to_child_pipe[0] != STDIN_FILENO) |
150 | close(to_child_pipe[0]); | |
151 | if (from_child_pipe[1] != STDOUT_FILENO) | |
152 | close(from_child_pipe[1]); | |
b35d0d8e MP |
153 | child_main(argc, argv); |
154 | } | |
155 | ||
7c2a9e76 WD |
156 | if (!am_sender) |
157 | filesfrom_fd = -1; | |
158 | ||
b35d0d8e MP |
159 | if (close(from_child_pipe[1]) < 0 || |
160 | close(to_child_pipe[0]) < 0) { | |
e2ccd357 | 161 | rsyserr(FERROR, errno, "Failed to close"); |
b35d0d8e MP |
162 | exit_cleanup(RERR_IPC); |
163 | } | |
164 | ||
165 | *f_in = from_child_pipe[0]; | |
166 | *f_out = to_child_pipe[1]; | |
e2ccd357 | 167 | |
b35d0d8e MP |
168 | return pid; |
169 | } |