io.c 13.3 KB
Newer Older
1 2
/* -*- c-file-style: "linux" -*-
   
Martin Pool's avatar
Martin Pool committed
3
   Copyright (C) 1996-2001 by Andrew Tridgell 
4
   Copyright (C) Paul Mackerras 1996
5
   Copyright (C) 2001 by Martin Pool <mbp@samba.org>
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

Martin Pool's avatar
Martin Pool committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
/**
 *
 * @file io.c
 *
 * Socket and pipe IO utilities used in rsync.
 *
 * rsync provides its own multiplexing system, which is used to send
 * stderr and stdout over a single socket.  We need this because
 * stdout normally carries the binary data stream, and stderr all our
 * error messages.
 *
 * For historical reasons this is off during the start of the
 * connection, but it's switched on quite early using
 * io_start_multiplex_out() and io_start_multiplex_in().
 **/
37 38 39

#include "rsync.h"

40 41 42
/* if no timeout is specified then use a 60 second select timeout */
#define SELECT_TIMEOUT 60

43 44
static int io_multiplexing_out;
static int io_multiplexing_in;
Andrew Tridgell's avatar
Andrew Tridgell committed
45 46
static int multiplex_in_fd;
static int multiplex_out_fd;
47
static time_t last_io;
48 49 50
static int no_flush;

extern int bwlimit;
51
extern int verbose;
Andrew Tridgell's avatar
 
Andrew Tridgell committed
52
extern int io_timeout;
53
extern struct stats stats;
54

55 56 57 58 59 60

/** Ignore EOF errors while reading a module listing if the remote
    version is 24 or less. */
int kludge_around_eof = False;


61
static int io_error_fd = -1;
62

63
static void read_loop(int fd, char *buf, size_t len);
64

65 66
static void check_timeout(void)
{
67
	extern int am_server, am_daemon;
68
	time_t t;
69 70

	err_list_push();
71 72 73 74 75 76 77 78 79 80
	
	if (!io_timeout) return;

	if (!last_io) {
		last_io = time(NULL);
		return;
	}

	t = time(NULL);

81
	if (last_io && io_timeout && (t-last_io) >= io_timeout) {
82
		if (!am_server && !am_daemon) {
Martin Pool's avatar
Martin Pool committed
83
			rprintf(FERROR,"io timeout after %d seconds - exiting\n", 
84 85
				(int)(t-last_io));
		}
86
		exit_cleanup(RERR_TIMEOUT);
87 88 89
	}
}

90 91 92 93 94 95
/* setup the fd used to propogate errors */
void io_set_error_fd(int fd)
{
	io_error_fd = fd;
}

96
/* read some data from the error fd and write it to the write log code */
97 98 99
static void read_error_fd(void)
{
	char buf[200];
100
	size_t n;
101
	int fd = io_error_fd;
102 103
	int tag, len;

Martin Pool's avatar
Martin Pool committed
104 105
        /* io_error_fd is temporarily disabled -- is this meant to
         * prevent indefinite recursion? */
106 107
	io_error_fd = -1;

108 109 110 111 112 113 114 115 116
	read_loop(fd, buf, 4);
	tag = IVAL(buf, 0);

	len = tag & 0xFFFFFF;
	tag = tag >> 24;
	tag -= MPLEX_BASE;

	while (len) {
		n = len;
117 118
		if (n > (sizeof(buf)-1))
			n = sizeof(buf)-1;
119 120 121
		read_loop(fd, buf, n);
		rwrite((enum logcode)tag, buf, n);
		len -= n;
122 123 124 125 126
	}

	io_error_fd = fd;
}

127

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
static void whine_about_eof (void)
{
	/**
	   It's almost always an error to get an EOF when we're trying
	   to read from the network, because the protocol is
	   self-terminating.
	   
	   However, there is one unfortunate cases where it is not,
	   which is rsync <2.4.6 sending a list of modules on a
	   server, since the list is terminated by closing the socket.
	   So, for the section of the program where that is a problem
	   (start_socket_client), kludge_around_eof is True and we
	   just exit.
	*/

	if (kludge_around_eof)
		exit_cleanup (0);
	else {
		rprintf (FERROR,
			 "%s: connection unexpectedly closed "
			 "(%.0f bytes read so far)\n",
			 RSYNC_NAME, (double)stats.total_read);
	
		exit_cleanup (RERR_STREAMIO);
	}
}
154

155 156 157 158 159 160 161 162 163 164 165 166 167

static void die_from_readerr (int err)
{
	/* this prevents us trying to write errors on a dead socket */
	io_multiplexing_close();
				
	rprintf(FERROR, "%s: read error: %s\n",
		RSYNC_NAME, strerror (err));
	exit_cleanup(RERR_STREAMIO);
}


/*!
Martin Pool's avatar
Doc.  
Martin Pool committed
168 169 170
 * Read from a socket with IO timeout. return the number of bytes
 * read. If no bytes can be read then exit, never return a number <= 0.
 *
Martin Pool's avatar
Martin Pool committed
171 172 173 174 175 176
 * TODO: If the remote shell connection fails, then current versions
 * actually report an "unexpected EOF" error here.  Since it's a
 * fairly common mistake to try to use rsh when ssh is required, we
 * should trap that: if we fail to read any data at all, we should
 * give a better explanation.  We can tell whether the connection has
 * started by looking e.g. at whether the remote version is known yet.
Martin Pool's avatar
Doc.  
Martin Pool committed
177
 */
178
static int read_timeout (int fd, char *buf, size_t len)
179
{
180 181
	int n, ret=0;

182 183
	io_flush();

184
	while (ret == 0) {
185
		/* until we manage to read *something* */
186 187
		fd_set fds;
		struct timeval tv;
188
		int fd_count = fd+1;
189
		int count;
190 191 192

		FD_ZERO(&fds);
		FD_SET(fd, &fds);
193 194 195 196 197
		if (io_error_fd != -1) {
			FD_SET(io_error_fd, &fds);
			if (io_error_fd > fd) fd_count = io_error_fd+1;
		}

198
		tv.tv_sec = io_timeout?io_timeout:SELECT_TIMEOUT;
199 200
		tv.tv_usec = 0;

201 202
		errno = 0;

203 204 205 206 207 208 209
		count = select(fd_count, &fds, NULL, NULL, &tv);

		if (count == 0) {
			check_timeout();
		}

		if (count <= 0) {
210 211 212
			if (errno == EBADF) {
				exit_cleanup(RERR_SOCKETIO);
			}
213 214 215
			continue;
		}

216 217 218 219 220 221
		if (io_error_fd != -1 && FD_ISSET(io_error_fd, &fds)) {
			read_error_fd();
		}

		if (!FD_ISSET(fd, &fds)) continue;

222 223
		n = read(fd, buf, len);

224 225 226
		if (n > 0) {
			buf += n;
			len -= n;
227 228 229 230
			ret += n;
			if (io_timeout)
				last_io = time(NULL);
			continue;
231 232 233 234 235 236 237 238 239
		} else if (n == 0) {
			whine_about_eof ();
			return -1; /* doesn't return */
		} else if (n == -1) {
			if (errno == EINTR || errno == EWOULDBLOCK ||
			    errno == EAGAIN) 
				continue;
			else
				die_from_readerr (errno);
240
		}
241
	}
242

243 244
	return ret;
}
245

246 247 248 249 250



/*! Continue trying to read len bytes - don't return until len has
  been read.   */
251
static void read_loop (int fd, char *buf, size_t len)
252 253 254 255 256 257
{
	while (len) {
		int n = read_timeout(fd, buf, len);

		buf += n;
		len -= n;
258 259 260
	}
}

261 262 263 264 265 266 267

/**
 * Read from the file descriptor handling multiplexing - return number
 * of bytes read.
 * 
 * Never returns <= 0. 
 */
268
static int read_unbuffered(int fd, char *buf, size_t len)
269
{
270
	static size_t remaining;
Martin Pool's avatar
Martin Pool committed
271
	int tag, ret = 0;
272 273
	char line[1024];

274
	if (!io_multiplexing_in || fd != multiplex_in_fd)
275
		return read_timeout(fd, buf, len);
276 277 278 279 280 281 282 283 284 285

	while (ret == 0) {
		if (remaining) {
			len = MIN(len, remaining);
			read_loop(fd, buf, len);
			remaining -= len;
			ret = len;
			continue;
		}

Martin Pool's avatar
Martin Pool committed
286
		read_loop(fd, line, 4);
287
		tag = IVAL(line, 0);
Andrew Tridgell's avatar
Andrew Tridgell committed
288

289 290 291
		remaining = tag & 0xFFFFFF;
		tag = tag >> 24;

Martin Pool's avatar
Martin Pool committed
292 293
		if (tag == MPLEX_BASE)
			continue;
294 295 296 297

		tag -= MPLEX_BASE;

		if (tag != FERROR && tag != FINFO) {
Martin Pool's avatar
Martin Pool committed
298
			rprintf(FERROR, "unexpected tag %d\n", tag);
299
			exit_cleanup(RERR_STREAMIO);
300 301
		}

Martin Pool's avatar
Martin Pool committed
302 303
		if (remaining > sizeof(line) - 1) {
			rprintf(FERROR, "multiplexing overflow %d\n\n",
304
				remaining);
305
			exit_cleanup(RERR_STREAMIO);
306 307 308 309 310
		}

		read_loop(fd, line, remaining);
		line[remaining] = 0;

Martin Pool's avatar
Martin Pool committed
311
		rprintf((enum logcode) tag, "%s", line);
312 313 314 315 316 317 318
		remaining = 0;
	}

	return ret;
}


Martin Pool's avatar
Martin Pool committed
319

320 321
/* do a buffered read from fd. don't return until all N bytes
   have been read. If all N can't be read then exit with an error */
322
static void readfd (int fd, char *buffer, size_t N)
323
{
Andrew Tridgell's avatar
 
Andrew Tridgell committed
324
	int  ret;
325
	size_t total=0;  
Andrew Tridgell's avatar
 
Andrew Tridgell committed
326 327
	
	while (total < N) {
328 329
		io_flush();

330
		ret = read_unbuffered (fd, buffer + total, N-total);
Andrew Tridgell's avatar
 
Andrew Tridgell committed
331
		total += ret;
332
	}
333 334

	stats.total_read += total;
335 336 337
}


338
int32 read_int(int f)
339
{
340
	char b[4];
341 342
	int32 ret;

343
	readfd(f,b,4);
344 345 346
	ret = IVAL(b,0);
	if (ret == (int32)0xffffffff) return -1;
	return ret;
347 348
}

Andrew Tridgell's avatar
 
Andrew Tridgell committed
349
int64 read_longint(int f)
350 351
{
	extern int remote_version;
Andrew Tridgell's avatar
 
Andrew Tridgell committed
352
	int64 ret;
353 354
	char b[8];
	ret = read_int(f);
Andrew Tridgell's avatar
 
Andrew Tridgell committed
355

356 357 358
	if ((int32)ret != (int32)0xffffffff) {
		return ret;
	}
Andrew Tridgell's avatar
 
Andrew Tridgell committed
359

360
#ifdef NO_INT64
Andrew Tridgell's avatar
 
Andrew Tridgell committed
361
	rprintf(FERROR,"Integer overflow - attempted 64 bit offset\n");
362
	exit_cleanup(RERR_UNSUPPORTED);
Andrew Tridgell's avatar
 
Andrew Tridgell committed
363 364
#else
	if (remote_version >= 16) {
365
		readfd(f,b,8);
Andrew Tridgell's avatar
 
Andrew Tridgell committed
366
		ret = IVAL(b,0) | (((int64)IVAL(b,4))<<32);
367
	}
Andrew Tridgell's avatar
 
Andrew Tridgell committed
368 369
#endif

370 371 372
	return ret;
}

373
void read_buf(int f,char *buf,size_t len)
374
{
375
	readfd(f,buf,len);
376 377
}

378
void read_sbuf(int f,char *buf,size_t len)
Andrew Tridgell's avatar
 
Andrew Tridgell committed
379
{
380
	read_buf (f,buf,len);
Andrew Tridgell's avatar
 
Andrew Tridgell committed
381 382 383
	buf[len] = 0;
}

384 385
unsigned char read_byte(int f)
{
386
	unsigned char c;
387
	read_buf (f, (char *)&c, 1);
388
	return c;
389
}
390

Martin Pool's avatar
Martin Pool committed
391 392
/* Write len bytes to fd.  This underlies the multiplexing system,
 * which is always called by application code.  */
393
static void writefd_unbuffered(int fd,char *buf,size_t len)
394
{
395
	size_t total = 0;
396
	fd_set w_fds, r_fds;
397
	int fd_count, count;
398
	struct timeval tv;
399

400 401
	err_list_push();

402 403
	no_flush++;

404
	while (total < len) {
405 406 407
		FD_ZERO(&w_fds);
		FD_ZERO(&r_fds);
		FD_SET(fd,&w_fds);
408
		fd_count = fd;
409

410 411 412 413
		if (io_error_fd != -1) {
			FD_SET(io_error_fd,&r_fds);
			if (io_error_fd > fd_count) 
				fd_count = io_error_fd;
414 415
		}

416
		tv.tv_sec = io_timeout?io_timeout:SELECT_TIMEOUT;
417
		tv.tv_usec = 0;
418

419 420 421
		errno = 0;

		count = select(fd_count+1,
422
			       io_error_fd != -1?&r_fds:NULL,
423
			       &w_fds,NULL,
424
			       &tv);
425

426 427 428 429
		if (count == 0) {
			check_timeout();
		}

430
		if (count <= 0) {
431 432 433
			if (errno == EBADF) {
				exit_cleanup(RERR_SOCKETIO);
			}
434 435
			continue;
		}
436

437 438 439 440
		if (io_error_fd != -1 && FD_ISSET(io_error_fd, &r_fds)) {
			read_error_fd();
		}

441
		if (FD_ISSET(fd, &w_fds)) {
442 443
			int ret;
			size_t n = len-total;
Andrew Tridgell's avatar
Andrew Tridgell committed
444
			ret = write(fd,buf+total,n);
445 446 447 448 449

			if (ret == -1 && errno == EINTR) {
				continue;
			}

Andrew Tridgell's avatar
Andrew Tridgell committed
450 451
			if (ret == -1 && 
			    (errno == EWOULDBLOCK || errno == EAGAIN)) {
452
				msleep(1);
Andrew Tridgell's avatar
Andrew Tridgell committed
453 454 455
				continue;
			}

456
			if (ret <= 0) {
457 458 459
				/* Don't try to write errors back
				 * across the stream */
				io_multiplexing_close();
Martin Pool's avatar
Martin Pool committed
460 461
				rprintf(FERROR, RSYNC_NAME
					": error writing %d unbuffered bytes"
Martin Pool's avatar
Martin Pool committed
462 463
					" - exiting: %s\n", len,
					strerror(errno));
464
				exit_cleanup(RERR_STREAMIO);
465 466
			}

467 468 469 470 471 472 473 474 475 476 477 478 479
			/* Sleep after writing to limit I/O bandwidth */
			if (bwlimit)
			{
			    tv.tv_sec = 0;
			    tv.tv_usec = ret * 1000 / bwlimit;
			    while (tv.tv_usec > 1000000)
			    {
				tv.tv_sec++;
				tv.tv_usec -= 1000000;
			    }
			    select(0, NULL, NULL, NULL, &tv);
 			}
 
480
			total += ret;
481

482 483
			if (io_timeout)
				last_io = time(NULL);
484
		}
485
	}
486 487

	no_flush--;
488 489
}

490

491 492 493 494 495
static char *io_buffer;
static int io_buffer_count;

void io_start_buffering(int fd)
{
496
	if (io_buffer) return;
Andrew Tridgell's avatar
Andrew Tridgell committed
497
	multiplex_out_fd = fd;
498
	io_buffer = (char *)malloc(IO_BUFFER_SIZE);
499 500
	if (!io_buffer) out_of_memory("writefd");
	io_buffer_count = 0;
501 502 503 504
}

/* write an message to a multiplexed stream. If this fails then rsync
   exits */
505
static void mplex_write(int fd, enum logcode code, char *buf, size_t len)
506 507
{
	char buffer[4096];
508
	size_t n = len;
509

510 511
	SIVAL(buffer, 0, ((MPLEX_BASE + (int)code)<<24) + len);

Andrew Tridgell's avatar
damn!  
Andrew Tridgell committed
512 513
	if (n > (sizeof(buffer)-4)) {
		n = sizeof(buffer)-4;
514 515 516 517 518 519 520 521
	}

	memcpy(&buffer[4], buf, n);
	writefd_unbuffered(fd, buffer, n+4);

	len -= n;
	buf += n;

Andrew Tridgell's avatar
damn!  
Andrew Tridgell committed
522 523 524
	if (len) {
		writefd_unbuffered(fd, buf, len);
	}
525 526
}

527

528
void io_flush(void)
529
{
Andrew Tridgell's avatar
Andrew Tridgell committed
530
	int fd = multiplex_out_fd;
531 532 533

	err_list_push();

534
	if (!io_buffer_count || no_flush) return;
535 536

	if (io_multiplexing_out) {
Andrew Tridgell's avatar
Andrew Tridgell committed
537
		mplex_write(fd, FNONE, io_buffer, io_buffer_count);
538
	} else {
539
		writefd_unbuffered(fd, io_buffer, io_buffer_count);
540
	}
541 542 543
	io_buffer_count = 0;
}

Martin Pool's avatar
Martin Pool committed
544

545
void io_end_buffering(void)
546 547 548
{
	io_flush();
	if (!io_multiplexing_out) {
549
		free(io_buffer);
550 551
		io_buffer = NULL;
	}
552 553
}

554
static void writefd(int fd,char *buf,size_t len)
555
{
556 557
	stats.total_written += len;

558 559
	err_list_push();

560
	if (!io_buffer || fd != multiplex_out_fd) {
561 562 563
		writefd_unbuffered(fd, buf, len);
		return;
	}
564 565

	while (len) {
566
		int n = MIN((int) len, IO_BUFFER_SIZE-io_buffer_count);
567 568 569 570 571 572 573
		if (n > 0) {
			memcpy(io_buffer+io_buffer_count, buf, n);
			buf += n;
			len -= n;
			io_buffer_count += n;
		}
		
574
		if (io_buffer_count == IO_BUFFER_SIZE) io_flush();
575 576
	}
}
577 578


579
void write_int(int f,int32 x)
580
{
581 582
	char b[4];
	SIVAL(b,0,x);
583
	writefd(f,b,4);
584 585
}

586 587 588 589 590

/*
 * Note: int64 may actually be a 32-bit type if ./configure couldn't find any
 * 64-bit types on this platform.
 */
Andrew Tridgell's avatar
 
Andrew Tridgell committed
591
void write_longint(int f, int64 x)
592 593 594 595 596 597 598 599 600
{
	extern int remote_version;
	char b[8];

	if (remote_version < 16 || x <= 0x7FFFFFFF) {
		write_int(f, (int)x);
		return;
	}

601
	write_int(f, (int32)0xFFFFFFFF);
602 603 604
	SIVAL(b,0,(x&0xFFFFFFFF));
	SIVAL(b,4,((x>>32)&0xFFFFFFFF));

605
	writefd(f,b,8);
606 607
}

608
void write_buf(int f,char *buf,size_t len)
609
{
610
	writefd(f,buf,len);
611 612
}

Andrew Tridgell's avatar
 
Andrew Tridgell committed
613
/* write a string to the connection */
614
static void write_sbuf(int f,char *buf)
Andrew Tridgell's avatar
 
Andrew Tridgell committed
615 616 617 618
{
	write_buf(f, buf, strlen(buf));
}

619

620 621
void write_byte(int f,unsigned char c)
{
Andrew Tridgell's avatar
 
Andrew Tridgell committed
622
	write_buf(f,(char *)&c,1);
623 624
}

625 626


627
int read_line(int f, char *buf, size_t maxlen)
Andrew Tridgell's avatar
 
Andrew Tridgell committed
628 629
{
	while (maxlen) {
630
		buf[0] = 0;
Andrew Tridgell's avatar
 
Andrew Tridgell committed
631
		read_buf(f, buf, 1);
632
		if (buf[0] == 0) return 0;
Andrew Tridgell's avatar
 
Andrew Tridgell committed
633 634 635 636 637 638 639 640 641 642 643 644 645
		if (buf[0] == '\n') {
			buf[0] = 0;
			break;
		}
		if (buf[0] != '\r') {
			buf++;
			maxlen--;
		}
	}
	if (maxlen == 0) {
		*buf = 0;
		return 0;
	}
646

Andrew Tridgell's avatar
 
Andrew Tridgell committed
647 648 649 650 651 652 653 654 655 656 657
	return 1;
}


void io_printf(int fd, const char *format, ...)
{
	va_list ap;  
	char buf[1024];
	int len;
	
	va_start(ap, format);
658
	len = vsnprintf(buf, sizeof(buf), format, ap);
Andrew Tridgell's avatar
 
Andrew Tridgell committed
659 660
	va_end(ap);

661
	if (len < 0) exit_cleanup(RERR_STREAMIO);
Andrew Tridgell's avatar
 
Andrew Tridgell committed
662 663 664

	write_sbuf(fd, buf);
}
665 666 667 668 669


/* setup for multiplexing an error stream with the data stream */
void io_start_multiplex_out(int fd)
{
Andrew Tridgell's avatar
Andrew Tridgell committed
670 671
	multiplex_out_fd = fd;
	io_flush();
672 673 674 675 676 677 678
	io_start_buffering(fd);
	io_multiplexing_out = 1;
}

/* setup for multiplexing an error stream with the data stream */
void io_start_multiplex_in(int fd)
{
Andrew Tridgell's avatar
Andrew Tridgell committed
679 680
	multiplex_in_fd = fd;
	io_flush();
681 682 683
	io_multiplexing_in = 1;
}

684
/* write an message to the multiplexed error stream */
685
int io_multiplex_write(enum logcode code, char *buf, size_t len)
686 687 688 689
{
	if (!io_multiplexing_out) return 0;

	io_flush();
690
	stats.total_written += (len+4);
691
	mplex_write(multiplex_out_fd, code, buf, len);
692 693 694
	return 1;
}

695 696 697 698 699 700
/* stop output multiplexing */
void io_multiplexing_close(void)
{
	io_multiplexing_out = 0;
}