Commit | Line | Data |
---|---|---|
2836ee9b WD |
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 | |
37cf7b41 | 10 | * the following commands (the resulting files should be identical): |
2836ee9b | 11 | * |
37cf7b41 WD |
12 | * rsync -av --rsh="savetransfer -i /tmp/to.server ssh" |
13 | * --rsync-path="savetransfer -i /tmp/from.client rsync" SOURCE DEST | |
2836ee9b | 14 | * |
37cf7b41 WD |
15 | * rsync -av --rsh="savetransfer -o /tmp/from.server ssh" |
16 | * --rsync-path="savetransfer -o /tmp/to.client rsync" SOURCE DEST | |
2836ee9b WD |
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 | ||
26 | #include "../rsync.h" | |
27 | ||
28 | #define TIMEOUT_SECONDS 30 | |
29 | ||
30 | void run_program(char **command); | |
31 | ||
32 | char buf[4096]; | |
33 | int save_data_from_program = 0; | |
34 | ||
35 | int | |
36 | main(int argc, char *argv[]) | |
37 | { | |
38 | int fd_file, len; | |
39 | struct timeval tv; | |
40 | fd_set fds; | |
41 | ||
42 | 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; | |
48 | else { | |
49 | fprintf(stderr, "Unknown option: %s\n", argv[0]); | |
50 | exit(1); | |
51 | } | |
52 | argv++; | |
53 | argc--; | |
54 | } | |
55 | if (argc < 2) { | |
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"); | |
59 | exit(1); | |
60 | } | |
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)); | |
63 | exit(1); | |
64 | } | |
65 | set_blocking(fd_file); | |
66 | ||
67 | signal(SIGPIPE, SIG_IGN); | |
68 | ||
69 | run_program(argv + 1); | |
70 | ||
d2004814 | 71 | #if defined HAVE_SETMODE && O_BINARY |
2836ee9b WD |
72 | setmode(STDIN_FILENO, O_BINARY); |
73 | setmode(STDOUT_FILENO, O_BINARY); | |
74 | #endif | |
75 | set_nonblocking(STDIN_FILENO); | |
76 | set_blocking(STDOUT_FILENO); | |
77 | ||
78 | while (1) { | |
79 | FD_ZERO(&fds); | |
80 | FD_SET(STDIN_FILENO, &fds); | |
81 | tv.tv_sec = TIMEOUT_SECONDS; | |
82 | tv.tv_usec = 0; | |
83 | if (!select(STDIN_FILENO+1, &fds, NULL, NULL, &tv)) | |
84 | break; | |
85 | if (!FD_ISSET(STDIN_FILENO, &fds)) | |
86 | break; | |
87 | if ((len = read(STDIN_FILENO, buf, sizeof buf)) <= 0) | |
88 | break; | |
89 | if (write(STDOUT_FILENO, buf, len) != len) { | |
90 | fprintf(stderr, "Failed to write data to stdout: %s\n", strerror(errno)); | |
91 | exit(1); | |
92 | } | |
93 | if (write(fd_file, buf, len) != len) { | |
94 | fprintf(stderr, "Failed to write data to fd_file: %s\n", strerror(errno)); | |
95 | exit(1); | |
96 | } | |
97 | } | |
98 | return 0; | |
99 | } | |
100 | ||
101 | void | |
102 | run_program(char **command) | |
103 | { | |
104 | int pipe_fds[2], ret; | |
105 | pid_t pid; | |
106 | ||
107 | if (pipe(pipe_fds) < 0) { | |
108 | fprintf(stderr, "pipe failed: %s\n", strerror(errno)); | |
109 | exit(1); | |
110 | } | |
111 | ||
112 | if ((pid = fork()) < 0) { | |
113 | fprintf(stderr, "fork failed: %s\n", strerror(errno)); | |
114 | exit(1); | |
115 | } | |
116 | ||
117 | if (pid == 0) { | |
118 | if (save_data_from_program) | |
119 | ret = dup2(pipe_fds[1], STDOUT_FILENO); | |
120 | else | |
121 | ret = dup2(pipe_fds[0], STDIN_FILENO); | |
122 | if (ret < 0) { | |
123 | fprintf(stderr, "Failed to dup (in child): %s\n", strerror(errno)); | |
124 | exit(1); | |
125 | } | |
126 | close(pipe_fds[0]); | |
127 | close(pipe_fds[1]); | |
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)); | |
132 | exit(1); | |
133 | } | |
134 | ||
135 | if (save_data_from_program) | |
136 | ret = dup2(pipe_fds[0], STDIN_FILENO); | |
137 | else | |
138 | ret = dup2(pipe_fds[1], STDOUT_FILENO); | |
139 | if (ret < 0) { | |
140 | fprintf(stderr, "Failed to dup (in parent): %s\n", strerror(errno)); | |
141 | exit(1); | |
142 | } | |
143 | close(pipe_fds[0]); | |
144 | close(pipe_fds[1]); | |
145 | } | |
146 | ||
147 | void | |
148 | set_nonblocking(int fd) | |
149 | { | |
150 | int val; | |
151 | ||
152 | if ((val = fcntl(fd, F_GETFL, 0)) == -1) | |
153 | return; | |
154 | if (!(val & NONBLOCK_FLAG)) { | |
155 | val |= NONBLOCK_FLAG; | |
156 | fcntl(fd, F_SETFL, val); | |
157 | } | |
158 | } | |
159 | ||
160 | void | |
161 | set_blocking(int fd) | |
162 | { | |
163 | int val; | |
164 | ||
165 | if ((val = fcntl(fd, F_GETFL, 0)) < 0) | |
166 | return; | |
167 | if (val & NONBLOCK_FLAG) { | |
168 | val &= ~NONBLOCK_FLAG; | |
169 | fcntl(fd, F_SETFL, val); | |
170 | } | |
171 | } |