pipe.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Routines used to setup various kinds of inter-process pipes.
  3. *
  4. * Copyright (C) 1996-2000 Andrew Tridgell
  5. * Copyright (C) 1996 Paul Mackerras
  6. * Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org>
  7. * Copyright (C) 2004-2009 Wayne Davison
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  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.
  13. *
  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.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, visit the http://fsf.org website.
  21. */
  22. #include "rsync.h"
  23. extern int am_sender;
  24. extern int am_server;
  25. extern int blocking_io;
  26. extern int filesfrom_fd;
  27. extern mode_t orig_umask;
  28. extern char *logfile_name;
  29. extern struct chmod_mode_struct *chmod_modes;
  30. /**
  31. * Create a child connected to us via its stdin/stdout.
  32. *
  33. * This is derived from CVS code
  34. *
  35. * Note that in the child STDIN is set to blocking and STDOUT
  36. * is set to non-blocking. This is necessary as rsh relies on stdin being blocking
  37. * and ssh relies on stdout being non-blocking
  38. *
  39. * If blocking_io is set then use blocking io on both fds. That can be
  40. * used to cope with badly broken rsh implementations like the one on
  41. * Solaris.
  42. **/
  43. pid_t piped_child(char **command, int *f_in, int *f_out)
  44. {
  45. pid_t pid;
  46. int to_child_pipe[2];
  47. int from_child_pipe[2];
  48. if (verbose >= 2)
  49. print_child_argv("opening connection using:", command);
  50. if (fd_pair(to_child_pipe) < 0 || fd_pair(from_child_pipe) < 0) {
  51. rsyserr(FERROR, errno, "pipe");
  52. exit_cleanup(RERR_IPC);
  53. }
  54. pid = do_fork();
  55. if (pid == -1) {
  56. rsyserr(FERROR, errno, "fork");
  57. exit_cleanup(RERR_IPC);
  58. }
  59. if (pid == 0) {
  60. if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
  61. close(to_child_pipe[1]) < 0 ||
  62. close(from_child_pipe[0]) < 0 ||
  63. dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
  64. rsyserr(FERROR, errno, "Failed to dup/close");
  65. exit_cleanup(RERR_IPC);
  66. }
  67. if (to_child_pipe[0] != STDIN_FILENO)
  68. close(to_child_pipe[0]);
  69. if (from_child_pipe[1] != STDOUT_FILENO)
  70. close(from_child_pipe[1]);
  71. umask(orig_umask);
  72. set_blocking(STDIN_FILENO);
  73. if (blocking_io > 0)
  74. set_blocking(STDOUT_FILENO);
  75. execvp(command[0], command);
  76. rsyserr(FERROR, errno, "Failed to exec %s", command[0]);
  77. exit_cleanup(RERR_IPC);
  78. }
  79. if (close(from_child_pipe[1]) < 0 || close(to_child_pipe[0]) < 0) {
  80. rsyserr(FERROR, errno, "Failed to close");
  81. exit_cleanup(RERR_IPC);
  82. }
  83. *f_in = from_child_pipe[0];
  84. *f_out = to_child_pipe[1];
  85. return pid;
  86. }
  87. /* This function forks a child which calls child_main(). First,
  88. * however, it has to establish communication paths to and from the
  89. * newborn child. It creates two socket pairs -- one for writing to
  90. * the child (from the parent) and one for reading from the child
  91. * (writing to the parent). Since that's four socket ends, each
  92. * process has to close the two ends it doesn't need. The remaining
  93. * two socket ends are retained for reading and writing. In the
  94. * child, the STDIN and STDOUT file descriptors refer to these
  95. * sockets. In the parent, the function arguments f_in and f_out are
  96. * set to refer to these sockets. */
  97. pid_t local_child(int argc, char **argv, int *f_in, int *f_out,
  98. int (*child_main)(int, char*[]))
  99. {
  100. pid_t pid;
  101. int to_child_pipe[2];
  102. int from_child_pipe[2];
  103. /* The parent process is always the sender for a local rsync. */
  104. assert(am_sender);
  105. if (fd_pair(to_child_pipe) < 0 ||
  106. fd_pair(from_child_pipe) < 0) {
  107. rsyserr(FERROR, errno, "pipe");
  108. exit_cleanup(RERR_IPC);
  109. }
  110. pid = do_fork();
  111. if (pid == -1) {
  112. rsyserr(FERROR, errno, "fork");
  113. exit_cleanup(RERR_IPC);
  114. }
  115. if (pid == 0) {
  116. am_sender = 0;
  117. am_server = 1;
  118. filesfrom_fd = -1;
  119. chmod_modes = NULL; /* Let the sending side handle this. */
  120. /* Let the client side handle this. */
  121. if (logfile_name) {
  122. logfile_name = NULL;
  123. logfile_close();
  124. }
  125. if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
  126. close(to_child_pipe[1]) < 0 ||
  127. close(from_child_pipe[0]) < 0 ||
  128. dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
  129. rsyserr(FERROR, errno, "Failed to dup/close");
  130. exit_cleanup(RERR_IPC);
  131. }
  132. if (to_child_pipe[0] != STDIN_FILENO)
  133. close(to_child_pipe[0]);
  134. if (from_child_pipe[1] != STDOUT_FILENO)
  135. close(from_child_pipe[1]);
  136. #ifdef ICONV_CONST
  137. setup_iconv();
  138. #endif
  139. child_main(argc, argv);
  140. }
  141. if (close(from_child_pipe[1]) < 0 ||
  142. close(to_child_pipe[0]) < 0) {
  143. rsyserr(FERROR, errno, "Failed to close");
  144. exit_cleanup(RERR_IPC);
  145. }
  146. *f_in = from_child_pipe[0];
  147. *f_out = to_child_pipe[1];
  148. return pid;
  149. }