summaryrefslogtreecommitdiff
path: root/scp.c
diff options
context:
space:
mode:
authorDamien Miller <djm@mindrot.org>2007-10-26 15:39:15 +1000
committerDamien Miller <djm@mindrot.org>2007-10-26 15:39:15 +1000
commitc77cadbff06a0b1f05dfacb94449094106d70faf (patch)
treef294a60642c98b0d5d4a2f7e9414c2231828e24a /scp.c
parent89437edafd157a3a01589a72d3779dafa5a64311 (diff)
- djm@cvs.openbsd.org 2007/10/24 03:44:02
[scp.c] factor out network read/write into an atomicio()-like function, and use it to handle short reads, apply bandwidth limits and update counters. make network IO non-blocking, so a small trickle of reads/writes has a chance of updating the progress meter; bz #799 ok dtucker@
Diffstat (limited to 'scp.c')
-rw-r--r--scp.c91
1 files changed, 64 insertions, 27 deletions
diff --git a/scp.c b/scp.c
index 1765a44e6..fb17f2879 100644
--- a/scp.c
+++ b/scp.c
@@ -78,6 +78,7 @@
78#ifdef HAVE_SYS_STAT_H 78#ifdef HAVE_SYS_STAT_H
79# include <sys/stat.h> 79# include <sys/stat.h>
80#endif 80#endif
81# include <sys/poll.h>
81#ifdef HAVE_SYS_TIME_H 82#ifdef HAVE_SYS_TIME_H
82# include <sys/time.h> 83# include <sys/time.h>
83#endif 84#endif
@@ -109,6 +110,8 @@
109 110
110extern char *__progname; 111extern char *__progname;
111 112
113#define COPY_BUFLEN 16384
114
112int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout); 115int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout);
113 116
114void bwlimit(int); 117void bwlimit(int);
@@ -129,7 +132,7 @@ int verbose_mode = 0;
129int showprogress = 1; 132int showprogress = 1;
130 133
131/* This is the program to execute for the secured connection. ("ssh" or -S) */ 134/* This is the program to execute for the secured connection. ("ssh" or -S) */
132char *ssh_program = _PATH_SSH_PROGRAM; 135char *ssh_program = "/home/djm/bin/ssh";
133 136
134/* This is used to store the pid of ssh_program */ 137/* This is used to store the pid of ssh_program */
135pid_t do_cmd_pid = -1; 138pid_t do_cmd_pid = -1;
@@ -282,6 +285,7 @@ void sink(int, char *[]);
282void source(int, char *[]); 285void source(int, char *[]);
283void tolocal(int, char *[]); 286void tolocal(int, char *[]);
284void toremote(char *, int, char *[]); 287void toremote(char *, int, char *[]);
288size_t scpio(ssize_t (*)(int, void *, size_t), int, void *, size_t, off_t *);
285void usage(void); 289void usage(void);
286 290
287int 291int
@@ -441,6 +445,43 @@ main(int argc, char **argv)
441 exit(errs != 0); 445 exit(errs != 0);
442} 446}
443 447
448/*
449 * atomicio-like wrapper that also applies bandwidth limits and updates
450 * the progressmeter counter.
451 */
452size_t
453scpio(ssize_t (*f)(int, void *, size_t), int fd, void *_p, size_t l, off_t *c)
454{
455 u_char *p = (u_char *)_p;
456 size_t offset;
457 ssize_t r;
458 struct pollfd pfd;
459
460 pfd.fd = fd;
461 pfd.events = f == read ? POLLIN : POLLOUT;
462 for (offset = 0; offset < l;) {
463 r = f(fd, p + offset, l - offset);
464 if (r == 0) {
465 errno = EPIPE;
466 return offset;
467 }
468 if (r < 0) {
469 if (errno == EINTR)
470 continue;
471 if (errno == EAGAIN) {
472 (void)poll(&pfd, 1, -1); /* Ignore errors */
473 continue;
474 }
475 return offset;
476 }
477 offset += (size_t)r;
478 *c += (off_t)r;
479 if (limit_rate)
480 bwlimit(r);
481 }
482 return offset;
483}
484
444void 485void
445toremote(char *targ, int argc, char **argv) 486toremote(char *targ, int argc, char **argv)
446{ 487{
@@ -583,7 +624,6 @@ source(int argc, char **argv)
583 static BUF buffer; 624 static BUF buffer;
584 BUF *bp; 625 BUF *bp;
585 off_t i, amt, statbytes; 626 off_t i, amt, statbytes;
586 size_t result;
587 int fd = -1, haderr, indx; 627 int fd = -1, haderr, indx;
588 char *last, *name, buf[2048], encname[MAXPATHLEN]; 628 char *last, *name, buf[2048], encname[MAXPATHLEN];
589 int len; 629 int len;
@@ -645,7 +685,7 @@ syserr: run_err("%s: %s", name, strerror(errno));
645 (void) atomicio(vwrite, remout, buf, strlen(buf)); 685 (void) atomicio(vwrite, remout, buf, strlen(buf));
646 if (response() < 0) 686 if (response() < 0)
647 goto next; 687 goto next;
648 if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) { 688 if ((bp = allocbuf(&buffer, fd, COPY_BUFLEN)) == NULL) {
649next: if (fd != -1) { 689next: if (fd != -1) {
650 (void) close(fd); 690 (void) close(fd);
651 fd = -1; 691 fd = -1;
@@ -654,27 +694,25 @@ next: if (fd != -1) {
654 } 694 }
655 if (showprogress) 695 if (showprogress)
656 start_progress_meter(curfile, stb.st_size, &statbytes); 696 start_progress_meter(curfile, stb.st_size, &statbytes);
657 /* Keep writing after an error so that we stay sync'd up. */ 697 set_nonblock(remout);
658 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) { 698 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
659 amt = bp->cnt; 699 amt = bp->cnt;
660 if (i + amt > stb.st_size) 700 if (i + amt > stb.st_size)
661 amt = stb.st_size - i; 701 amt = stb.st_size - i;
662 if (!haderr) { 702 if (!haderr) {
663 result = atomicio(read, fd, bp->buf, amt); 703 if (atomicio(read, fd, bp->buf, amt) != amt)
664 if (result != amt)
665 haderr = errno; 704 haderr = errno;
666 } 705 }
667 if (haderr) 706 /* Keep writing after error to retain sync */
668 (void) atomicio(vwrite, remout, bp->buf, amt); 707 if (haderr) {
669 else { 708 (void)atomicio(vwrite, remout, bp->buf, amt);
670 result = atomicio(vwrite, remout, bp->buf, amt); 709 continue;
671 if (result != amt)
672 haderr = errno;
673 statbytes += result;
674 } 710 }
675 if (limit_rate) 711 if (scpio(vwrite, remout, bp->buf, amt,
676 bwlimit(amt); 712 &statbytes) != amt)
713 haderr = errno;
677 } 714 }
715 unset_nonblock(remout);
678 if (showprogress) 716 if (showprogress)
679 stop_progress_meter(); 717 stop_progress_meter();
680 718
@@ -780,10 +818,10 @@ bwlimit(int amount)
780 thresh /= 2; 818 thresh /= 2;
781 if (thresh < 2048) 819 if (thresh < 2048)
782 thresh = 2048; 820 thresh = 2048;
783 } else if (bwend.tv_usec < 100) { 821 } else if (bwend.tv_usec < 10000) {
784 thresh *= 2; 822 thresh *= 2;
785 if (thresh > 32768) 823 if (thresh > COPY_BUFLEN * 4)
786 thresh = 32768; 824 thresh = COPY_BUFLEN * 4;
787 } 825 }
788 826
789 TIMEVAL_TO_TIMESPEC(&bwend, &ts); 827 TIMEVAL_TO_TIMESPEC(&bwend, &ts);
@@ -974,7 +1012,7 @@ bad: run_err("%s: %s", np, strerror(errno));
974 continue; 1012 continue;
975 } 1013 }
976 (void) atomicio(vwrite, remout, "", 1); 1014 (void) atomicio(vwrite, remout, "", 1);
977 if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) { 1015 if ((bp = allocbuf(&buffer, ofd, COPY_BUFLEN)) == NULL) {
978 (void) close(ofd); 1016 (void) close(ofd);
979 continue; 1017 continue;
980 } 1018 }
@@ -984,26 +1022,24 @@ bad: run_err("%s: %s", np, strerror(errno));
984 statbytes = 0; 1022 statbytes = 0;
985 if (showprogress) 1023 if (showprogress)
986 start_progress_meter(curfile, size, &statbytes); 1024 start_progress_meter(curfile, size, &statbytes);
987 for (count = i = 0; i < size; i += 4096) { 1025 set_nonblock(remin);
988 amt = 4096; 1026 for (count = i = 0; i < size; i += bp->cnt) {
1027 amt = bp->cnt;
989 if (i + amt > size) 1028 if (i + amt > size)
990 amt = size - i; 1029 amt = size - i;
991 count += amt; 1030 count += amt;
992 do { 1031 do {
993 j = atomicio(read, remin, cp, amt); 1032 j = scpio(read, remin, cp, amt, &statbytes);
994 if (j == 0) { 1033 if (j == 0) {
995 run_err("%s", j ? strerror(errno) : 1034 run_err("%s", j != EPIPE ?
1035 strerror(errno) :
996 "dropped connection"); 1036 "dropped connection");
997 exit(1); 1037 exit(1);
998 } 1038 }
999 amt -= j; 1039 amt -= j;
1000 cp += j; 1040 cp += j;
1001 statbytes += j;
1002 } while (amt > 0); 1041 } while (amt > 0);
1003 1042
1004 if (limit_rate)
1005 bwlimit(4096);
1006
1007 if (count == bp->cnt) { 1043 if (count == bp->cnt) {
1008 /* Keep reading so we stay sync'd up. */ 1044 /* Keep reading so we stay sync'd up. */
1009 if (wrerr == NO) { 1045 if (wrerr == NO) {
@@ -1017,6 +1053,7 @@ bad: run_err("%s: %s", np, strerror(errno));
1017 cp = bp->buf; 1053 cp = bp->buf;
1018 } 1054 }
1019 } 1055 }
1056 unset_nonblock(remin);
1020 if (showprogress) 1057 if (showprogress)
1021 stop_progress_meter(); 1058 stop_progress_meter();
1022 if (count != 0 && wrerr == NO && 1059 if (count != 0 && wrerr == NO &&