Changed #if to use "defined".
[rsync/rsync.git] / support / savetransfer.c
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 first two are push commands, the last two are
11  * pull commands):
12  *
13  * rsync -av --rsh="savetransfer -i /tmp/from.sender ssh"
14  *   --rsync-path="savetransfer -i /tmp/to.receiver rsync" FILES HOST:DEST
15  *
16  * rsync -av --rsh="savetransfer -o /tmp/to.sender ssh"
17  *   --rsync-path="savetransfer -o /tmp/from.generator rsync" FILES HOST:DEST
18  *
19  * rsync -av --rsh="savetransfer -i /tmp/from.generator ssh"
20  *   --rsync-path="savetransfer -i /tmp/to.sender rsync" HOST:FILES DEST
21  *
22  * rsync -av --rsh="savetransfer -o /tmp/to.receiver ssh"
23  *   --rsync-path="savetransfer -o /tmp/from.sender rsync" HOST:FILES DEST
24  *
25  * Note that this program aborts after 30 seconds of inactivity, so you'll need
26  * to change it if that is not enough dead time for your transfer.  Also, some
27  * of the above commands will not notice that the transfer is done (if we're
28  * saving the input to a PROGRAM and the PROGRAM goes away:  we won't notice
29  * that it's gone unless more data comes in) -- when this happens it will delay
30  * at the end of the transfer until the timeout period expires.
31  */
32
33 #include "../rsync.h"
34
35 #define TIMEOUT_SECONDS 30
36
37 void run_program(char **command);
38
39 char buf[4096];
40 int save_data_from_program = 0;
41
42 int
43 main(int argc, char *argv[])
44 {
45     int fd_file, len;
46     struct timeval tv;
47     fd_set fds;
48
49     argv++;
50     if (--argc && argv[0][0] == '-') {
51         if (argv[0][1] == 'o')
52             save_data_from_program = 1;
53         else if (argv[0][1] == 'i')
54             save_data_from_program = 0;
55         else {
56             fprintf(stderr, "Unknown option: %s\n", argv[0]);
57             exit(1);
58         }
59         argv++;
60         argc--;
61     }
62     if (argc < 2) {
63         fprintf(stderr, "Usage: savetransfer [-i|-o] OUTPUT_FILE PROGRAM [ARGS...]\n");
64         fprintf(stderr, "-i  Save the input going to PROGRAM to the OUTPUT_FILE\n");
65         fprintf(stderr, "-o  Save the output coming from PROGRAM to the OUTPUT_FILE\n");
66         exit(1);
67     }
68     if ((fd_file = open(*argv, O_WRONLY|O_TRUNC|O_CREAT|O_BINARY, 0644)) < 0) {
69         fprintf(stderr, "Unable to write to `%s': %s\n", *argv, strerror(errno));
70         exit(1);
71     }
72     set_blocking(fd_file);
73
74     signal(SIGPIPE, SIG_IGN);
75
76     run_program(argv + 1);
77
78 #if defined HAVE_SETMODE && O_BINARY
79     setmode(STDIN_FILENO, O_BINARY);
80     setmode(STDOUT_FILENO, O_BINARY);
81 #endif
82     set_nonblocking(STDIN_FILENO);
83     set_blocking(STDOUT_FILENO);
84
85     while (1) {
86         FD_ZERO(&fds);
87         FD_SET(STDIN_FILENO, &fds);
88         tv.tv_sec = TIMEOUT_SECONDS;
89         tv.tv_usec = 0;
90         if (!select(STDIN_FILENO+1, &fds, NULL, NULL, &tv))
91             break;
92         if (!FD_ISSET(STDIN_FILENO, &fds))
93             break;
94         if ((len = read(STDIN_FILENO, buf, sizeof buf)) <= 0)
95             break;
96         if (write(STDOUT_FILENO, buf, len) != len) {
97             fprintf(stderr, "Failed to write data to stdout: %s\n", strerror(errno));
98             exit(1);
99         }
100         if (write(fd_file, buf, len) != len) {
101             fprintf(stderr, "Failed to write data to fd_file: %s\n", strerror(errno));
102             exit(1);
103         }
104     }
105     return 0;
106 }
107
108 void
109 run_program(char **command)
110 {
111     int pipe_fds[2], ret;
112     pid_t pid;
113
114     if (pipe(pipe_fds) < 0) {
115         fprintf(stderr, "pipe failed: %s\n", strerror(errno));
116         exit(1);
117     }
118
119     if ((pid = fork()) < 0) {
120         fprintf(stderr, "fork failed: %s\n", strerror(errno));
121         exit(1);
122     }
123
124     if (pid == 0) {
125         if (save_data_from_program)
126             ret = dup2(pipe_fds[1], STDOUT_FILENO);
127         else
128             ret = dup2(pipe_fds[0], STDIN_FILENO);
129         if (ret < 0) {
130             fprintf(stderr, "Failed to dup (in child): %s\n", strerror(errno));
131             exit(1);
132         }
133         close(pipe_fds[0]);
134         close(pipe_fds[1]);
135         set_blocking(STDIN_FILENO);
136         set_blocking(STDOUT_FILENO);
137         execvp(command[0], command);
138         fprintf(stderr, "Failed to exec %s: %s\n", command[0], strerror(errno));
139         exit(1);
140     }
141
142     if (save_data_from_program)
143         ret = dup2(pipe_fds[0], STDIN_FILENO);
144     else
145         ret = dup2(pipe_fds[1], STDOUT_FILENO);
146     if (ret < 0) {
147         fprintf(stderr, "Failed to dup (in parent): %s\n", strerror(errno));
148         exit(1);
149     }
150     close(pipe_fds[0]);
151     close(pipe_fds[1]);
152 }
153
154 void
155 set_nonblocking(int fd)
156 {
157     int val;
158
159     if ((val = fcntl(fd, F_GETFL, 0)) == -1)
160         return;
161     if (!(val & NONBLOCK_FLAG)) {
162         val |= NONBLOCK_FLAG;
163         fcntl(fd, F_SETFL, val);
164     }
165 }
166
167 void
168 set_blocking(int fd)
169 {
170     int val;
171
172     if ((val = fcntl(fd, F_GETFL, 0)) < 0)
173         return;
174     if (val & NONBLOCK_FLAG) {
175         val &= ~NONBLOCK_FLAG;
176         fcntl(fd, F_SETFL, val);
177     }
178 }