Add a little more protocol documentation.
[rsync/rsync.git] / io.c
CommitLineData
7a24c346
MP
1/* -*- c-file-style: "linux" -*-
2
ce6c7c63 3 Copyright (C) 1996-2001 by Andrew Tridgell
720b47f2
AT
4 Copyright (C) Paul Mackerras 1996
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19*/
20
21/*
08f15335 22 socket and pipe IO utilities used in rsync
720b47f2
AT
23
24 tridge, June 1996
25 */
26#include "rsync.h"
27
8cd9fd4e
AT
28/* if no timeout is specified then use a 60 second select timeout */
29#define SELECT_TIMEOUT 60
30
ef5d23eb
DD
31extern int bwlimit;
32
8d9dc9f9
AT
33static int io_multiplexing_out;
34static int io_multiplexing_in;
679e7657
AT
35static int multiplex_in_fd;
36static int multiplex_out_fd;
8d9dc9f9 37static time_t last_io;
528bfcd7 38static int eof_error=1;
720b47f2 39extern int verbose;
6ba9279f 40extern int io_timeout;
a800434a 41extern struct stats stats;
720b47f2 42
554e0a8d 43static int io_error_fd = -1;
720b47f2 44
ff41a59f
AT
45static void read_loop(int fd, char *buf, int len);
46
8d9dc9f9
AT
47static void check_timeout(void)
48{
0adb99b9 49 extern int am_server, am_daemon;
8d9dc9f9 50 time_t t;
90ba34e2
AT
51
52 err_list_push();
8d9dc9f9
AT
53
54 if (!io_timeout) return;
55
56 if (!last_io) {
57 last_io = time(NULL);
58 return;
59 }
60
61 t = time(NULL);
62
86ffe37f 63 if (last_io && io_timeout && (t-last_io) >= io_timeout) {
0adb99b9 64 if (!am_server && !am_daemon) {
ce6c7c63 65 rprintf(FERROR,"io timeout after %d seconds - exiting\n",
0adb99b9
AT
66 (int)(t-last_io));
67 }
65417579 68 exit_cleanup(RERR_TIMEOUT);
8d9dc9f9
AT
69 }
70}
71
554e0a8d
AT
72/* setup the fd used to propogate errors */
73void io_set_error_fd(int fd)
74{
75 io_error_fd = fd;
76}
77
ff41a59f 78/* read some data from the error fd and write it to the write log code */
554e0a8d
AT
79static void read_error_fd(void)
80{
81 char buf[200];
82 int n;
83 int fd = io_error_fd;
ff41a59f
AT
84 int tag, len;
85
8886f8d0
MP
86 /* io_error_fd is temporarily disabled -- is this meant to
87 * prevent indefinite recursion? */
554e0a8d
AT
88 io_error_fd = -1;
89
ff41a59f
AT
90 read_loop(fd, buf, 4);
91 tag = IVAL(buf, 0);
92
93 len = tag & 0xFFFFFF;
94 tag = tag >> 24;
95 tag -= MPLEX_BASE;
96
97 while (len) {
98 n = len;
99 if (n > (sizeof(buf)-1)) n = sizeof(buf)-1;
100 read_loop(fd, buf, n);
101 rwrite((enum logcode)tag, buf, n);
102 len -= n;
554e0a8d
AT
103 }
104
105 io_error_fd = fd;
106}
107
720b47f2 108
e44f9a12 109static int no_flush;
720b47f2 110
c3563c46
MP
111/*
112 * Read from a socket with IO timeout. return the number of bytes
113 * read. If no bytes can be read then exit, never return a number <= 0.
114 *
8886f8d0
MP
115 * TODO: If the remote shell connection fails, then current versions
116 * actually report an "unexpected EOF" error here. Since it's a
117 * fairly common mistake to try to use rsh when ssh is required, we
118 * should trap that: if we fail to read any data at all, we should
119 * give a better explanation. We can tell whether the connection has
120 * started by looking e.g. at whether the remote version is known yet.
c3563c46 121 */
4c36ddbe 122static int read_timeout(int fd, char *buf, int len)
8d9dc9f9 123{
4c36ddbe
AT
124 int n, ret=0;
125
ea2111d1
AT
126 io_flush();
127
4c36ddbe
AT
128 while (ret == 0) {
129 fd_set fds;
130 struct timeval tv;
554e0a8d 131 int fd_count = fd+1;
4c36ddbe
AT
132
133 FD_ZERO(&fds);
134 FD_SET(fd, &fds);
554e0a8d
AT
135 if (io_error_fd != -1) {
136 FD_SET(io_error_fd, &fds);
137 if (io_error_fd > fd) fd_count = io_error_fd+1;
138 }
139
8cd9fd4e 140 tv.tv_sec = io_timeout?io_timeout:SELECT_TIMEOUT;
4c36ddbe
AT
141 tv.tv_usec = 0;
142
554e0a8d
AT
143 errno = 0;
144
145 if (select(fd_count, &fds, NULL, NULL, &tv) < 1) {
146 if (errno == EBADF) {
147 exit_cleanup(RERR_SOCKETIO);
148 }
4c36ddbe
AT
149 check_timeout();
150 continue;
151 }
152
554e0a8d
AT
153 if (io_error_fd != -1 && FD_ISSET(io_error_fd, &fds)) {
154 read_error_fd();
155 }
156
157 if (!FD_ISSET(fd, &fds)) continue;
158
4c36ddbe
AT
159 n = read(fd, buf, len);
160
8d9dc9f9
AT
161 if (n > 0) {
162 buf += n;
163 len -= n;
4c36ddbe
AT
164 ret += n;
165 if (io_timeout)
166 last_io = time(NULL);
167 continue;
8d9dc9f9 168 }
4c36ddbe
AT
169
170 if (n == -1 && errno == EINTR) {
171 continue;
172 }
173
f0359dd0
AT
174 if (n == -1 &&
175 (errno == EWOULDBLOCK || errno == EAGAIN)) {
176 continue;
177 }
178
5d1e1dcf 179
8d9dc9f9 180 if (n == 0) {
528bfcd7 181 if (eof_error) {
8886f8d0
MP
182 rprintf(FERROR,
183 "%s: connection to server unexpectedly closed"
81c99202
AT
184 " (%.0f bytes read so far)\n",
185 RSYNC_NAME, (double)stats.total_read);
528bfcd7 186 }
65417579 187 exit_cleanup(RERR_STREAMIO);
8d9dc9f9 188 }
8d9dc9f9 189
5d1e1dcf 190 /* this prevents us trying to write errors on a dead socket */
554e0a8d 191 io_multiplexing_close();
5d1e1dcf 192
4c36ddbe 193 rprintf(FERROR,"read error: %s\n", strerror(errno));
65417579 194 exit_cleanup(RERR_STREAMIO);
4c36ddbe 195 }
8d9dc9f9 196
4c36ddbe
AT
197 return ret;
198}
8d9dc9f9 199
4c36ddbe
AT
200/* continue trying to read len bytes - don't return until len
201 has been read */
202static void read_loop(int fd, char *buf, int len)
203{
204 while (len) {
205 int n = read_timeout(fd, buf, len);
206
207 buf += n;
208 len -= n;
8d9dc9f9
AT
209 }
210}
211
cad2bba7 212/* read from the file descriptor handling multiplexing -
4c36ddbe
AT
213 return number of bytes read
214 never return <= 0 */
8d9dc9f9
AT
215static int read_unbuffered(int fd, char *buf, int len)
216{
217 static int remaining;
8d9dc9f9
AT
218 int tag, ret=0;
219 char line[1024];
220
679e7657 221 if (!io_multiplexing_in || fd != multiplex_in_fd)
4c36ddbe 222 return read_timeout(fd, buf, len);
8d9dc9f9
AT
223
224 while (ret == 0) {
225 if (remaining) {
226 len = MIN(len, remaining);
227 read_loop(fd, buf, len);
228 remaining -= len;
229 ret = len;
230 continue;
231 }
232
ff41a59f
AT
233 read_loop(fd, line, 4);
234 tag = IVAL(line, 0);
679e7657 235
8d9dc9f9
AT
236 remaining = tag & 0xFFFFFF;
237 tag = tag >> 24;
238
239 if (tag == MPLEX_BASE) continue;
240
241 tag -= MPLEX_BASE;
242
243 if (tag != FERROR && tag != FINFO) {
244 rprintf(FERROR,"unexpected tag %d\n", tag);
65417579 245 exit_cleanup(RERR_STREAMIO);
8d9dc9f9
AT
246 }
247
248 if (remaining > sizeof(line)-1) {
249 rprintf(FERROR,"multiplexing overflow %d\n\n",
250 remaining);
65417579 251 exit_cleanup(RERR_STREAMIO);
8d9dc9f9
AT
252 }
253
254 read_loop(fd, line, remaining);
255 line[remaining] = 0;
256
ff41a59f 257 rprintf((enum logcode)tag,"%s", line);
8d9dc9f9
AT
258 remaining = 0;
259 }
260
261 return ret;
262}
263
264
4c36ddbe
AT
265/* do a buffered read from fd. don't return until all N bytes
266 have been read. If all N can't be read then exit with an error */
267static void readfd(int fd,char *buffer,int N)
720b47f2 268{
6ba9279f
AT
269 int ret;
270 int total=0;
6ba9279f 271
6ba9279f 272 while (total < N) {
8d9dc9f9
AT
273 io_flush();
274
4c36ddbe 275 ret = read_unbuffered(fd,buffer + total,N-total);
6ba9279f 276 total += ret;
7f28dbee 277 }
1b7c47cb
AT
278
279 stats.total_read += total;
720b47f2
AT
280}
281
282
b7922338 283int32 read_int(int f)
720b47f2 284{
4c36ddbe 285 char b[4];
d730b113
AT
286 int32 ret;
287
4c36ddbe 288 readfd(f,b,4);
d730b113
AT
289 ret = IVAL(b,0);
290 if (ret == (int32)0xffffffff) return -1;
291 return ret;
720b47f2
AT
292}
293
71c46176 294int64 read_longint(int f)
3a6a366f
AT
295{
296 extern int remote_version;
71c46176 297 int64 ret;
3a6a366f
AT
298 char b[8];
299 ret = read_int(f);
71c46176 300
8de330a3
AT
301 if ((int32)ret != (int32)0xffffffff) {
302 return ret;
303 }
71c46176 304
3bee6733 305#ifdef NO_INT64
9486289c 306 rprintf(FERROR,"Integer overflow - attempted 64 bit offset\n");
65417579 307 exit_cleanup(RERR_UNSUPPORTED);
71c46176
AT
308#else
309 if (remote_version >= 16) {
4c36ddbe 310 readfd(f,b,8);
71c46176 311 ret = IVAL(b,0) | (((int64)IVAL(b,4))<<32);
3a6a366f 312 }
71c46176
AT
313#endif
314
3a6a366f
AT
315 return ret;
316}
317
720b47f2
AT
318void read_buf(int f,char *buf,int len)
319{
4c36ddbe 320 readfd(f,buf,len);
720b47f2
AT
321}
322
575f2fca
AT
323void read_sbuf(int f,char *buf,int len)
324{
325 read_buf(f,buf,len);
326 buf[len] = 0;
327}
328
182dca5c
AT
329unsigned char read_byte(int f)
330{
4c36ddbe
AT
331 unsigned char c;
332 read_buf(f,(char *)&c,1);
333 return c;
182dca5c 334}
720b47f2 335
ae682c3e 336/* write len bytes to fd */
4c36ddbe 337static void writefd_unbuffered(int fd,char *buf,int len)
720b47f2 338{
8d9dc9f9
AT
339 int total = 0;
340 fd_set w_fds, r_fds;
4c36ddbe 341 int fd_count, count;
8d9dc9f9 342 struct timeval tv;
720b47f2 343
90ba34e2
AT
344 err_list_push();
345
e44f9a12
AT
346 no_flush++;
347
4c36ddbe 348 while (total < len) {
8d9dc9f9
AT
349 FD_ZERO(&w_fds);
350 FD_ZERO(&r_fds);
351 FD_SET(fd,&w_fds);
554e0a8d 352 fd_count = fd;
4c36ddbe 353
554e0a8d
AT
354 if (io_error_fd != -1) {
355 FD_SET(io_error_fd,&r_fds);
356 if (io_error_fd > fd_count)
357 fd_count = io_error_fd;
8d9dc9f9
AT
358 }
359
8cd9fd4e 360 tv.tv_sec = io_timeout?io_timeout:SELECT_TIMEOUT;
8d9dc9f9 361 tv.tv_usec = 0;
4c36ddbe 362
554e0a8d
AT
363 errno = 0;
364
365 count = select(fd_count+1,
08f15335 366 io_error_fd != -1?&r_fds:NULL,
4c36ddbe 367 &w_fds,NULL,
8cd9fd4e 368 &tv);
4c36ddbe
AT
369
370 if (count <= 0) {
554e0a8d
AT
371 if (errno == EBADF) {
372 exit_cleanup(RERR_SOCKETIO);
373 }
8d9dc9f9
AT
374 check_timeout();
375 continue;
376 }
4c36ddbe 377
554e0a8d
AT
378 if (io_error_fd != -1 && FD_ISSET(io_error_fd, &r_fds)) {
379 read_error_fd();
380 }
381
8d9dc9f9 382 if (FD_ISSET(fd, &w_fds)) {
07b7c86c 383 int ret, n = len-total;
f0359dd0 384 ret = write(fd,buf+total,n);
4c36ddbe
AT
385
386 if (ret == -1 && errno == EINTR) {
387 continue;
388 }
389
f0359dd0
AT
390 if (ret == -1 &&
391 (errno == EWOULDBLOCK || errno == EAGAIN)) {
e92ee128 392 msleep(1);
f0359dd0
AT
393 continue;
394 }
395
4c36ddbe 396 if (ret <= 0) {
ce6c7c63
MP
397 rprintf(FERROR,
398 "error writing %d unbuffered bytes"
399 " - exiting: %s\n", len,
400 strerror(errno));
65417579 401 exit_cleanup(RERR_STREAMIO);
4c36ddbe
AT
402 }
403
ef5d23eb
DD
404 /* Sleep after writing to limit I/O bandwidth */
405 if (bwlimit)
406 {
407 tv.tv_sec = 0;
408 tv.tv_usec = ret * 1000 / bwlimit;
409 while (tv.tv_usec > 1000000)
410 {
411 tv.tv_sec++;
412 tv.tv_usec -= 1000000;
413 }
414 select(0, NULL, NULL, NULL, &tv);
415 }
416
4c36ddbe 417 total += ret;
a800434a 418
4c36ddbe
AT
419 if (io_timeout)
420 last_io = time(NULL);
8d9dc9f9 421 }
4c36ddbe 422 }
e44f9a12
AT
423
424 no_flush--;
720b47f2
AT
425}
426
8d9dc9f9 427
d6dead6b
AT
428static char *io_buffer;
429static int io_buffer_count;
430
431void io_start_buffering(int fd)
432{
8d9dc9f9 433 if (io_buffer) return;
679e7657 434 multiplex_out_fd = fd;
ff41a59f 435 io_buffer = (char *)malloc(IO_BUFFER_SIZE);
d6dead6b
AT
436 if (!io_buffer) out_of_memory("writefd");
437 io_buffer_count = 0;
ff41a59f
AT
438}
439
440/* write an message to a multiplexed stream. If this fails then rsync
441 exits */
442static void mplex_write(int fd, enum logcode code, char *buf, int len)
443{
444 char buffer[4096];
445 int n = len;
8d9dc9f9 446
ff41a59f
AT
447 SIVAL(buffer, 0, ((MPLEX_BASE + (int)code)<<24) + len);
448
6d7b6081
AT
449 if (n > (sizeof(buffer)-4)) {
450 n = sizeof(buffer)-4;
ff41a59f
AT
451 }
452
453 memcpy(&buffer[4], buf, n);
454 writefd_unbuffered(fd, buffer, n+4);
455
456 len -= n;
457 buf += n;
458
6d7b6081
AT
459 if (len) {
460 writefd_unbuffered(fd, buf, len);
461 }
d6dead6b
AT
462}
463
ff41a59f 464
8d9dc9f9 465void io_flush(void)
d6dead6b 466{
679e7657 467 int fd = multiplex_out_fd;
90ba34e2
AT
468
469 err_list_push();
470
e44f9a12 471 if (!io_buffer_count || no_flush) return;
8d9dc9f9
AT
472
473 if (io_multiplexing_out) {
0f3203c3 474 mplex_write(fd, FNONE, io_buffer, io_buffer_count);
8d9dc9f9 475 } else {
4c36ddbe 476 writefd_unbuffered(fd, io_buffer, io_buffer_count);
d6dead6b 477 }
8d9dc9f9
AT
478 io_buffer_count = 0;
479}
480
0ba48136
MP
481
482/* XXX: fd is ignored, which seems a little strange. */
8d9dc9f9
AT
483void io_end_buffering(int fd)
484{
485 io_flush();
486 if (!io_multiplexing_out) {
ff41a59f 487 free(io_buffer);
8d9dc9f9
AT
488 io_buffer = NULL;
489 }
d6dead6b
AT
490}
491
4c36ddbe 492static void writefd(int fd,char *buf,int len)
d6dead6b 493{
1b7c47cb
AT
494 stats.total_written += len;
495
90ba34e2
AT
496 err_list_push();
497
554e0a8d 498 if (!io_buffer || fd != multiplex_out_fd) {
4c36ddbe
AT
499 writefd_unbuffered(fd, buf, len);
500 return;
501 }
d6dead6b
AT
502
503 while (len) {
504 int n = MIN(len, IO_BUFFER_SIZE-io_buffer_count);
505 if (n > 0) {
506 memcpy(io_buffer+io_buffer_count, buf, n);
507 buf += n;
508 len -= n;
509 io_buffer_count += n;
510 }
511
8d9dc9f9 512 if (io_buffer_count == IO_BUFFER_SIZE) io_flush();
d6dead6b 513 }
d6dead6b 514}
720b47f2
AT
515
516
b7922338 517void write_int(int f,int32 x)
720b47f2 518{
8d9dc9f9
AT
519 char b[4];
520 SIVAL(b,0,x);
4c36ddbe 521 writefd(f,b,4);
720b47f2
AT
522}
523
7a24c346
MP
524
525/*
526 * Note: int64 may actually be a 32-bit type if ./configure couldn't find any
527 * 64-bit types on this platform.
528 */
71c46176 529void write_longint(int f, int64 x)
3a6a366f
AT
530{
531 extern int remote_version;
532 char b[8];
3a6a366f
AT
533
534 if (remote_version < 16 || x <= 0x7FFFFFFF) {
535 write_int(f, (int)x);
536 return;
537 }
538
8de330a3 539 write_int(f, (int32)0xFFFFFFFF);
3a6a366f
AT
540 SIVAL(b,0,(x&0xFFFFFFFF));
541 SIVAL(b,4,((x>>32)&0xFFFFFFFF));
542
4c36ddbe 543 writefd(f,b,8);
3a6a366f
AT
544}
545
720b47f2
AT
546void write_buf(int f,char *buf,int len)
547{
4c36ddbe 548 writefd(f,buf,len);
720b47f2
AT
549}
550
f0fca04e 551/* write a string to the connection */
6e4fb64e 552static void write_sbuf(int f,char *buf)
f0fca04e
AT
553{
554 write_buf(f, buf, strlen(buf));
555}
556
720b47f2 557
182dca5c
AT
558void write_byte(int f,unsigned char c)
559{
f0fca04e 560 write_buf(f,(char *)&c,1);
182dca5c
AT
561}
562
f0fca04e
AT
563int read_line(int f, char *buf, int maxlen)
564{
528bfcd7
AT
565 eof_error = 0;
566
f0fca04e 567 while (maxlen) {
528bfcd7 568 buf[0] = 0;
f0fca04e 569 read_buf(f, buf, 1);
528bfcd7 570 if (buf[0] == 0) return 0;
f0fca04e
AT
571 if (buf[0] == '\n') {
572 buf[0] = 0;
573 break;
574 }
575 if (buf[0] != '\r') {
576 buf++;
577 maxlen--;
578 }
579 }
580 if (maxlen == 0) {
581 *buf = 0;
582 return 0;
583 }
528bfcd7
AT
584
585 eof_error = 1;
586
f0fca04e
AT
587 return 1;
588}
589
590
591void io_printf(int fd, const char *format, ...)
592{
593 va_list ap;
594 char buf[1024];
595 int len;
596
597 va_start(ap, format);
8950ac03 598 len = vsnprintf(buf, sizeof(buf), format, ap);
f0fca04e
AT
599 va_end(ap);
600
65417579 601 if (len < 0) exit_cleanup(RERR_STREAMIO);
f0fca04e
AT
602
603 write_sbuf(fd, buf);
604}
8d9dc9f9
AT
605
606
607/* setup for multiplexing an error stream with the data stream */
608void io_start_multiplex_out(int fd)
609{
679e7657
AT
610 multiplex_out_fd = fd;
611 io_flush();
8d9dc9f9
AT
612 io_start_buffering(fd);
613 io_multiplexing_out = 1;
614}
615
616/* setup for multiplexing an error stream with the data stream */
617void io_start_multiplex_in(int fd)
618{
679e7657
AT
619 multiplex_in_fd = fd;
620 io_flush();
8d9dc9f9
AT
621 io_multiplexing_in = 1;
622}
623
554e0a8d 624/* write an message to the multiplexed error stream */
ff41a59f 625int io_multiplex_write(enum logcode code, char *buf, int len)
8d9dc9f9
AT
626{
627 if (!io_multiplexing_out) return 0;
628
629 io_flush();
1b7c47cb 630 stats.total_written += (len+4);
ff41a59f 631 mplex_write(multiplex_out_fd, code, buf, len);
8d9dc9f9
AT
632 return 1;
633}
634
554e0a8d
AT
635/* stop output multiplexing */
636void io_multiplexing_close(void)
637{
638 io_multiplexing_out = 0;
639}
640