savetransfer.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* This program can record the stream of data flowing to or from a program.
  2. * This allows it to be used to check that rsync's data that is flowing
  3. * through a remote shell is not being corrupted (for example).
  4. *
  5. * Usage: savetransfer [-i|-o] OUTPUT_FILE PROGRAM [ARGS...]
  6. * -i Save the input going to PROGRAM to the OUTPUT_FILE
  7. * -o Save the output coming from PROGRAM to the OUTPUT_FILE
  8. *
  9. * If you want to capture the flow of data for an rsync command, use one of
  10. * the following commands (the resulting files should be identical):
  11. *
  12. * rsync -av --rsh="savetransfer -i /tmp/to.server ssh"
  13. * --rsync-path="savetransfer -i /tmp/from.client rsync" SOURCE DEST
  14. *
  15. * rsync -av --rsh="savetransfer -o /tmp/from.server ssh"
  16. * --rsync-path="savetransfer -o /tmp/to.client rsync" SOURCE DEST
  17. *
  18. * Note that this program aborts after 30 seconds of inactivity, so you'll need
  19. * to change it if that is not enough dead time for your transfer. Also, some
  20. * of the above commands will not notice that the transfer is done (if we're
  21. * saving the input to a PROGRAM and the PROGRAM goes away: we won't notice
  22. * that it's gone unless more data comes in) -- when this happens it will delay
  23. * at the end of the transfer until the timeout period expires.
  24. */
  25. #include "../rsync.h"
  26. #define TIMEOUT_SECONDS 30
  27. #ifdef HAVE_SIGACTION
  28. static struct sigaction sigact;
  29. #endif
  30. void run_program(char **command);
  31. char buf[4096];
  32. int save_data_from_program = 0;
  33. int
  34. main(int argc, char *argv[])
  35. {
  36. int fd_file, len;
  37. struct timeval tv;
  38. fd_set fds;
  39. argv++;
  40. if (--argc && argv[0][0] == '-') {
  41. if (argv[0][1] == 'o')
  42. save_data_from_program = 1;
  43. else if (argv[0][1] == 'i')
  44. save_data_from_program = 0;
  45. else {
  46. fprintf(stderr, "Unknown option: %s\n", argv[0]);
  47. exit(1);
  48. }
  49. argv++;
  50. argc--;
  51. }
  52. if (argc < 2) {
  53. fprintf(stderr, "Usage: savetransfer [-i|-o] OUTPUT_FILE PROGRAM [ARGS...]\n");
  54. fprintf(stderr, "-i Save the input going to PROGRAM to the OUTPUT_FILE\n");
  55. fprintf(stderr, "-o Save the output coming from PROGRAM to the OUTPUT_FILE\n");
  56. exit(1);
  57. }
  58. if ((fd_file = open(*argv, O_WRONLY|O_TRUNC|O_CREAT|O_BINARY, 0644)) < 0) {
  59. fprintf(stderr, "Unable to write to `%s': %s\n", *argv, strerror(errno));
  60. exit(1);
  61. }
  62. set_blocking(fd_file);
  63. SIGACTION(SIGPIPE, SIG_IGN);
  64. run_program(argv + 1);
  65. #if defined HAVE_SETMODE && O_BINARY
  66. setmode(STDIN_FILENO, O_BINARY);
  67. setmode(STDOUT_FILENO, O_BINARY);
  68. #endif
  69. set_nonblocking(STDIN_FILENO);
  70. set_blocking(STDOUT_FILENO);
  71. while (1) {
  72. FD_ZERO(&fds);
  73. FD_SET(STDIN_FILENO, &fds);
  74. tv.tv_sec = TIMEOUT_SECONDS;
  75. tv.tv_usec = 0;
  76. if (!select(STDIN_FILENO+1, &fds, NULL, NULL, &tv))
  77. break;
  78. if (!FD_ISSET(STDIN_FILENO, &fds))
  79. break;
  80. if ((len = read(STDIN_FILENO, buf, sizeof buf)) <= 0)
  81. break;
  82. if (write(STDOUT_FILENO, buf, len) != len) {
  83. fprintf(stderr, "Failed to write data to stdout: %s\n", strerror(errno));
  84. exit(1);
  85. }
  86. if (write(fd_file, buf, len) != len) {
  87. fprintf(stderr, "Failed to write data to fd_file: %s\n", strerror(errno));
  88. exit(1);
  89. }
  90. }
  91. return 0;
  92. }
  93. void
  94. run_program(char **command)
  95. {
  96. int pipe_fds[2], ret;
  97. pid_t pid;
  98. if (pipe(pipe_fds) < 0) {
  99. fprintf(stderr, "pipe failed: %s\n", strerror(errno));
  100. exit(1);
  101. }
  102. if ((pid = fork()) < 0) {
  103. fprintf(stderr, "fork failed: %s\n", strerror(errno));
  104. exit(1);
  105. }
  106. if (pid == 0) {
  107. if (save_data_from_program)
  108. ret = dup2(pipe_fds[1], STDOUT_FILENO);
  109. else
  110. ret = dup2(pipe_fds[0], STDIN_FILENO);
  111. if (ret < 0) {
  112. fprintf(stderr, "Failed to dup (in child): %s\n", strerror(errno));
  113. exit(1);
  114. }
  115. close(pipe_fds[0]);
  116. close(pipe_fds[1]);
  117. set_blocking(STDIN_FILENO);
  118. set_blocking(STDOUT_FILENO);
  119. execvp(command[0], command);
  120. fprintf(stderr, "Failed to exec %s: %s\n", command[0], strerror(errno));
  121. exit(1);
  122. }
  123. if (save_data_from_program)
  124. ret = dup2(pipe_fds[0], STDIN_FILENO);
  125. else
  126. ret = dup2(pipe_fds[1], STDOUT_FILENO);
  127. if (ret < 0) {
  128. fprintf(stderr, "Failed to dup (in parent): %s\n", strerror(errno));
  129. exit(1);
  130. }
  131. close(pipe_fds[0]);
  132. close(pipe_fds[1]);
  133. }
  134. void
  135. set_nonblocking(int fd)
  136. {
  137. int val;
  138. if ((val = fcntl(fd, F_GETFL, 0)) == -1)
  139. return;
  140. if (!(val & NONBLOCK_FLAG)) {
  141. val |= NONBLOCK_FLAG;
  142. fcntl(fd, F_SETFL, val);
  143. }
  144. }
  145. void
  146. set_blocking(int fd)
  147. {
  148. int val;
  149. if ((val = fcntl(fd, F_GETFL, 0)) < 0)
  150. return;
  151. if (val & NONBLOCK_FLAG) {
  152. val &= ~NONBLOCK_FLAG;
  153. fcntl(fd, F_SETFL, val);
  154. }
  155. }