]>
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> | |
e63ff70e | 7 | * Copyright (C) 2004-2020 Wayne Davison |
e2ccd357 | 8 | * |
fcb69e5c | 9 | * This program is free software; you can redistribute it and/or modify |
8e41b68e WD |
10 | * it under the terms of the GNU General Public License as published by |
11 | * the Free Software Foundation; either version 3 of the License, or | |
12 | * (at your option) any later version. | |
e2ccd357 | 13 | * |
fcb69e5c MP |
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. | |
e2ccd357 | 18 | * |
e7c67065 | 19 | * You should have received a copy of the GNU General Public License along |
4fd842f9 | 20 | * with this program; if not, visit the http://fsf.org website. |
fcb69e5c MP |
21 | */ |
22 | ||
b35d0d8e MP |
23 | #include "rsync.h" |
24 | ||
72a6e631 WD |
25 | extern int am_sender; |
26 | extern int am_server; | |
27 | extern int blocking_io; | |
72a6e631 | 28 | extern int filesfrom_fd; |
41adbcec | 29 | extern int munge_symlinks; |
887d7455 | 30 | extern char *logfile_name; |
7a2eca41 WD |
31 | extern int remote_option_cnt; |
32 | extern const char **remote_options; | |
ba30fb5c | 33 | extern struct chmod_mode_struct *chmod_modes; |
72a6e631 | 34 | |
b35d0d8e | 35 | /** |
20c1926a | 36 | * Create a child connected to us via its stdin/stdout. |
b35d0d8e | 37 | * |
e2ccd357 WD |
38 | * This is derived from CVS code |
39 | * | |
b35d0d8e MP |
40 | * Note that in the child STDIN is set to blocking and STDOUT |
41 | * is set to non-blocking. This is necessary as rsh relies on stdin being blocking | |
42 | * and ssh relies on stdout being non-blocking | |
43 | * | |
44 | * If blocking_io is set then use blocking io on both fds. That can be | |
45 | * used to cope with badly broken rsh implementations like the one on | |
46 | * Solaris. | |
47 | **/ | |
48 | pid_t piped_child(char **command, int *f_in, int *f_out) | |
49 | { | |
50 | pid_t pid; | |
51 | int to_child_pipe[2]; | |
52 | int from_child_pipe[2]; | |
e2ccd357 | 53 | |
951e826b | 54 | if (DEBUG_GTE(CMD, 1)) |
0b515981 | 55 | print_child_argv("opening connection using:", command); |
b35d0d8e MP |
56 | |
57 | if (fd_pair(to_child_pipe) < 0 || fd_pair(from_child_pipe) < 0) { | |
d62bcc17 | 58 | rsyserr(FERROR, errno, "pipe"); |
b35d0d8e MP |
59 | exit_cleanup(RERR_IPC); |
60 | } | |
61 | ||
b35d0d8e MP |
62 | pid = do_fork(); |
63 | if (pid == -1) { | |
d62bcc17 | 64 | rsyserr(FERROR, errno, "fork"); |
b35d0d8e MP |
65 | exit_cleanup(RERR_IPC); |
66 | } | |
67 | ||
68 | if (pid == 0) { | |
e63ff70e WD |
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) { | |
d62bcc17 | 73 | rsyserr(FERROR, errno, "Failed to dup/close"); |
b35d0d8e MP |
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]); | |
b35d0d8e | 80 | set_blocking(STDIN_FILENO); |
7d3f8ae2 | 81 | if (blocking_io > 0) |
b35d0d8e | 82 | set_blocking(STDOUT_FILENO); |
b35d0d8e | 83 | execvp(command[0], command); |
45c49b52 | 84 | rsyserr(FERROR, errno, "Failed to exec %s", command[0]); |
b35d0d8e MP |
85 | exit_cleanup(RERR_IPC); |
86 | } | |
87 | ||
88 | if (close(from_child_pipe[1]) < 0 || close(to_child_pipe[0]) < 0) { | |
d62bcc17 | 89 | rsyserr(FERROR, errno, "Failed to close"); |
b35d0d8e MP |
90 | exit_cleanup(RERR_IPC); |
91 | } | |
92 | ||
93 | *f_in = from_child_pipe[0]; | |
94 | *f_out = to_child_pipe[1]; | |
95 | ||
96 | return pid; | |
97 | } | |
98 | ||
391516da | 99 | /* This function forks a child which calls child_main(). First, |
b9f592fb WD |
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 | |
391516da | 108 | * set to refer to these sockets. */ |
b9f592fb | 109 | pid_t local_child(int argc, char **argv, int *f_in, int *f_out, |
b44be3e9 | 110 | int (*child_main)(int, char*[])) |
b35d0d8e MP |
111 | { |
112 | pid_t pid; | |
113 | int to_child_pipe[2]; | |
114 | int from_child_pipe[2]; | |
b35d0d8e | 115 | |
ba30fb5c WD |
116 | /* The parent process is always the sender for a local rsync. */ |
117 | assert(am_sender); | |
118 | ||
e63ff70e | 119 | if (fd_pair(to_child_pipe) < 0 || fd_pair(from_child_pipe) < 0) { |
d62bcc17 | 120 | rsyserr(FERROR, errno, "pipe"); |
b35d0d8e MP |
121 | exit_cleanup(RERR_IPC); |
122 | } | |
123 | ||
8c90957f | 124 | pid = do_fork(); |
b35d0d8e | 125 | if (pid == -1) { |
d62bcc17 | 126 | rsyserr(FERROR, errno, "fork"); |
b35d0d8e MP |
127 | exit_cleanup(RERR_IPC); |
128 | } | |
129 | ||
130 | if (pid == 0) { | |
ba30fb5c | 131 | am_sender = 0; |
e2ccd357 | 132 | am_server = 1; |
ba30fb5c | 133 | filesfrom_fd = -1; |
41adbcec | 134 | munge_symlinks = 0; /* Each side needs its own option. */ |
ba30fb5c | 135 | chmod_modes = NULL; /* Let the sending side handle this. */ |
7c2a9e76 | 136 | |
ddc8110d WD |
137 | /* Let the client side handle this. */ |
138 | if (logfile_name) { | |
139 | logfile_name = NULL; | |
140 | logfile_close(); | |
141 | } | |
142 | ||
7a2eca41 WD |
143 | if (remote_option_cnt) { |
144 | int rc = remote_option_cnt + 1; | |
145 | const char **rv = remote_options; | |
146 | if (!parse_arguments(&rc, &rv)) { | |
147 | option_error(); | |
148 | exit_cleanup(RERR_SYNTAX); | |
149 | } | |
150 | } | |
151 | ||
e63ff70e WD |
152 | if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 |
153 | || close(to_child_pipe[1]) < 0 | |
154 | || close(from_child_pipe[0]) < 0 | |
155 | || dup2(from_child_pipe[1], STDOUT_FILENO) < 0) { | |
d62bcc17 | 156 | rsyserr(FERROR, errno, "Failed to dup/close"); |
b35d0d8e MP |
157 | exit_cleanup(RERR_IPC); |
158 | } | |
3896bca4 WD |
159 | if (to_child_pipe[0] != STDIN_FILENO) |
160 | close(to_child_pipe[0]); | |
161 | if (from_child_pipe[1] != STDOUT_FILENO) | |
162 | close(from_child_pipe[1]); | |
44c4492a | 163 | #ifdef ICONV_CONST |
b61665d5 | 164 | setup_iconv(); |
44c4492a | 165 | #endif |
b35d0d8e MP |
166 | child_main(argc, argv); |
167 | } | |
168 | ||
e63ff70e | 169 | if (close(from_child_pipe[1]) < 0 || close(to_child_pipe[0]) < 0) { |
e2ccd357 | 170 | rsyserr(FERROR, errno, "Failed to close"); |
b35d0d8e MP |
171 | exit_cleanup(RERR_IPC); |
172 | } | |
173 | ||
174 | *f_in = from_child_pipe[0]; | |
175 | *f_out = to_child_pipe[1]; | |
e2ccd357 | 176 | |
b35d0d8e MP |
177 | return pid; |
178 | } |