| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#include "rsync.h" |
| 24 |
|
| 25 |
extern int am_sender; |
| 26 |
extern int am_server; |
| 27 |
extern int blocking_io; |
| 28 |
extern int filesfrom_fd; |
| 29 |
extern int munge_symlinks; |
| 30 |
extern char *logfile_name; |
| 31 |
extern int remote_option_cnt; |
| 32 |
extern const char **remote_options; |
| 33 |
extern struct chmod_mode_struct *chmod_modes; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 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]; |
| 53 |
|
| 54 |
if (DEBUG_GTE(CMD, 1)) |
| 55 |
print_child_argv("opening connection using:", command); |
| 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 |
set_blocking(STDIN_FILENO); |
| 81 |
if (blocking_io > 0) |
| 82 |
set_blocking(STDOUT_FILENO); |
| 83 |
execvp(command[0], command); |
| 84 |
rsyserr(FERROR, errno, "Failed to exec %s", command[0]); |
| 85 |
exit_cleanup(RERR_IPC); |
| 86 |
} |
| 87 |
|
| 88 |
if (close(from_child_pipe[1]) < 0 || close(to_child_pipe[0]) < 0) { |
| 89 |
rsyserr(FERROR, errno, "Failed to close"); |
| 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 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
pid_t local_child(int argc, char **argv, int *f_in, int *f_out, |
| 110 |
int (*child_main)(int, char*[])) |
| 111 |
{ |
| 112 |
pid_t pid; |
| 113 |
int to_child_pipe[2]; |
| 114 |
int from_child_pipe[2]; |
| 115 |
|
| 116 |
|
| 117 |
assert(am_sender); |
| 118 |
|
| 119 |
if (fd_pair(to_child_pipe) < 0 || fd_pair(from_child_pipe) < 0) { |
| 120 |
rsyserr(FERROR, errno, "pipe"); |
| 121 |
exit_cleanup(RERR_IPC); |
| 122 |
} |
| 123 |
|
| 124 |
pid = do_fork(); |
| 125 |
if (pid == -1) { |
| 126 |
rsyserr(FERROR, errno, "fork"); |
| 127 |
exit_cleanup(RERR_IPC); |
| 128 |
} |
| 129 |
|
| 130 |
if (pid == 0) { |
| 131 |
am_sender = 0; |
| 132 |
am_server = 1; |
| 133 |
filesfrom_fd = -1; |
| 134 |
munge_symlinks = 0; |
| 135 |
chmod_modes = NULL; |
| 136 |
|
| 137 |
|
| 138 |
if (logfile_name) { |
| 139 |
logfile_name = NULL; |
| 140 |
logfile_close(); |
| 141 |
} |
| 142 |
|
| 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 |
|
| 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) { |
| 156 |
rsyserr(FERROR, errno, "Failed to dup/close"); |
| 157 |
exit_cleanup(RERR_IPC); |
| 158 |
} |
| 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]); |
| 163 |
#ifdef ICONV_CONST |
| 164 |
setup_iconv(); |
| 165 |
#endif |
| 166 |
child_main(argc, argv); |
| 167 |
} |
| 168 |
|
| 169 |
if (close(from_child_pipe[1]) < 0 || close(to_child_pipe[0]) < 0) { |
| 170 |
rsyserr(FERROR, errno, "Failed to close"); |
| 171 |
exit_cleanup(RERR_IPC); |
| 172 |
} |
| 173 |
|
| 174 |
*f_in = from_child_pipe[0]; |
| 175 |
*f_out = to_child_pipe[1]; |
| 176 |
|
| 177 |
return pid; |
| 178 |
} |