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).
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
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):
12 * rsync -av --rsh="savetransfer -i /tmp/to.server ssh"
13 * --rsync-path="savetransfer -i /tmp/from.client rsync" SOURCE DEST
15 * rsync -av --rsh="savetransfer -o /tmp/from.server ssh"
16 * --rsync-path="savetransfer -o /tmp/to.client rsync" SOURCE DEST
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.
28 #define TIMEOUT_SECONDS 30
30 void run_program(char **command);
33 int save_data_from_program = 0;
36 main(int argc, char *argv[])
43 if (--argc && argv[0][0] == '-') {
44 if (argv[0][1] == 'o')
45 save_data_from_program = 1;
46 else if (argv[0][1] == 'i')
47 save_data_from_program = 0;
49 fprintf(stderr, "Unknown option: %s\n", argv[0]);
56 fprintf(stderr, "Usage: savetransfer [-i|-o] OUTPUT_FILE PROGRAM [ARGS...]\n");
57 fprintf(stderr, "-i Save the input going to PROGRAM to the OUTPUT_FILE\n");
58 fprintf(stderr, "-o Save the output coming from PROGRAM to the OUTPUT_FILE\n");
61 if ((fd_file = open(*argv, O_WRONLY|O_TRUNC|O_CREAT|O_BINARY, 0644)) < 0) {
62 fprintf(stderr, "Unable to write to `%s': %s\n", *argv, strerror(errno));
65 set_blocking(fd_file);
67 signal(SIGPIPE, SIG_IGN);
69 run_program(argv + 1);
71 #if defined HAVE_SETMODE && O_BINARY
72 setmode(STDIN_FILENO, O_BINARY);
73 setmode(STDOUT_FILENO, O_BINARY);
75 set_nonblocking(STDIN_FILENO);
76 set_blocking(STDOUT_FILENO);
80 FD_SET(STDIN_FILENO, &fds);
81 tv.tv_sec = TIMEOUT_SECONDS;
83 if (!select(STDIN_FILENO+1, &fds, NULL, NULL, &tv))
85 if (!FD_ISSET(STDIN_FILENO, &fds))
87 if ((len = read(STDIN_FILENO, buf, sizeof buf)) <= 0)
89 if (write(STDOUT_FILENO, buf, len) != len) {
90 fprintf(stderr, "Failed to write data to stdout: %s\n", strerror(errno));
93 if (write(fd_file, buf, len) != len) {
94 fprintf(stderr, "Failed to write data to fd_file: %s\n", strerror(errno));
102 run_program(char **command)
104 int pipe_fds[2], ret;
107 if (pipe(pipe_fds) < 0) {
108 fprintf(stderr, "pipe failed: %s\n", strerror(errno));
112 if ((pid = fork()) < 0) {
113 fprintf(stderr, "fork failed: %s\n", strerror(errno));
118 if (save_data_from_program)
119 ret = dup2(pipe_fds[1], STDOUT_FILENO);
121 ret = dup2(pipe_fds[0], STDIN_FILENO);
123 fprintf(stderr, "Failed to dup (in child): %s\n", strerror(errno));
128 set_blocking(STDIN_FILENO);
129 set_blocking(STDOUT_FILENO);
130 execvp(command[0], command);
131 fprintf(stderr, "Failed to exec %s: %s\n", command[0], strerror(errno));
135 if (save_data_from_program)
136 ret = dup2(pipe_fds[0], STDIN_FILENO);
138 ret = dup2(pipe_fds[1], STDOUT_FILENO);
140 fprintf(stderr, "Failed to dup (in parent): %s\n", strerror(errno));
148 set_nonblocking(int fd)
152 if ((val = fcntl(fd, F_GETFL, 0)) == -1)
154 if (!(val & NONBLOCK_FLAG)) {
155 val |= NONBLOCK_FLAG;
156 fcntl(fd, F_SETFL, val);
165 if ((val = fcntl(fd, F_GETFL, 0)) < 0)
167 if (val & NONBLOCK_FLAG) {
168 val &= ~NONBLOCK_FLAG;
169 fcntl(fd, F_SETFL, val);