00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "rsync.h"
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 pid_t piped_child(char **command, int *f_in, int *f_out)
00038 {
00039 pid_t pid;
00040 int to_child_pipe[2];
00041 int from_child_pipe[2];
00042 extern int blocking_io;
00043
00044 if (verbose >= 2) {
00045 print_child_argv(command);
00046 }
00047
00048 if (fd_pair(to_child_pipe) < 0 || fd_pair(from_child_pipe) < 0) {
00049 rprintf(FERROR, "pipe: %s\n", strerror(errno));
00050 exit_cleanup(RERR_IPC);
00051 }
00052
00053
00054 pid = do_fork();
00055 if (pid == -1) {
00056 rprintf(FERROR, "fork: %s\n", strerror(errno));
00057 exit_cleanup(RERR_IPC);
00058 }
00059
00060 if (pid == 0) {
00061 extern int orig_umask;
00062 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
00063 close(to_child_pipe[1]) < 0 ||
00064 close(from_child_pipe[0]) < 0 ||
00065 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
00066 rprintf(FERROR, "Failed to dup/close : %s\n",
00067 strerror(errno));
00068 exit_cleanup(RERR_IPC);
00069 }
00070 if (to_child_pipe[0] != STDIN_FILENO)
00071 close(to_child_pipe[0]);
00072 if (from_child_pipe[1] != STDOUT_FILENO)
00073 close(from_child_pipe[1]);
00074 umask(orig_umask);
00075 set_blocking(STDIN_FILENO);
00076 if (blocking_io) {
00077 set_blocking(STDOUT_FILENO);
00078 }
00079 execvp(command[0], command);
00080 rprintf(FERROR, "Failed to exec %s : %s\n",
00081 command[0], strerror(errno));
00082 exit_cleanup(RERR_IPC);
00083 }
00084
00085 if (close(from_child_pipe[1]) < 0 || close(to_child_pipe[0]) < 0) {
00086 rprintf(FERROR, "Failed to close : %s\n", strerror(errno));
00087 exit_cleanup(RERR_IPC);
00088 }
00089
00090 *f_in = from_child_pipe[0];
00091 *f_out = to_child_pipe[1];
00092
00093 return pid;
00094 }
00095
00096 pid_t local_child(int argc, char **argv,int *f_in,int *f_out,
00097 int (*child_main)(int, char*[]))
00098 {
00099 pid_t pid;
00100 int to_child_pipe[2];
00101 int from_child_pipe[2];
00102 extern int read_batch;
00103
00104 if (fd_pair(to_child_pipe) < 0 ||
00105 fd_pair(from_child_pipe) < 0) {
00106 rprintf(FERROR,"pipe: %s\n",strerror(errno));
00107 exit_cleanup(RERR_IPC);
00108 }
00109
00110
00111 pid = do_fork();
00112 if (pid == -1) {
00113 rprintf(FERROR,"fork: %s\n",strerror(errno));
00114 exit_cleanup(RERR_IPC);
00115 }
00116
00117 if (pid == 0) {
00118 extern int am_sender;
00119 extern int am_server;
00120
00121 am_sender = read_batch ? 0 : !am_sender;
00122 am_server = 1;
00123
00124 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
00125 close(to_child_pipe[1]) < 0 ||
00126 close(from_child_pipe[0]) < 0 ||
00127 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
00128 rprintf(FERROR,"Failed to dup/close : %s\n",strerror(errno));
00129 exit_cleanup(RERR_IPC);
00130 }
00131 if (to_child_pipe[0] != STDIN_FILENO) close(to_child_pipe[0]);
00132 if (from_child_pipe[1] != STDOUT_FILENO) close(from_child_pipe[1]);
00133 child_main(argc, argv);
00134 }
00135
00136 if (close(from_child_pipe[1]) < 0 ||
00137 close(to_child_pipe[0]) < 0) {
00138 rprintf(FERROR,"Failed to close : %s\n",strerror(errno));
00139 exit_cleanup(RERR_IPC);
00140 }
00141
00142 *f_in = from_child_pipe[0];
00143 *f_out = to_child_pipe[1];
00144
00145 return pid;
00146 }
00147
00148