summaryrefslogtreecommitdiff
path: root/scp.c
diff options
context:
space:
mode:
Diffstat (limited to 'scp.c')
-rw-r--r--scp.c116
1 files changed, 85 insertions, 31 deletions
diff --git a/scp.c b/scp.c
index 1765a44e6..9f8b7a192 100644
--- a/scp.c
+++ b/scp.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: scp.c,v 1.160 2007/08/06 19:16:06 sobrado Exp $ */ 1/* $OpenBSD: scp.c,v 1.163 2008/06/13 18:55:22 dtucker Exp $ */
2/* 2/*
3 * scp - secure remote copy. This is basically patched BSD rcp which 3 * scp - secure remote copy. This is basically patched BSD rcp which
4 * uses ssh to do the data transfer (instead of using rcmd). 4 * uses ssh to do the data transfer (instead of using rcmd).
@@ -78,6 +78,13 @@
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#ifdef HAVE_POLL_H
82#include <poll.h>
83#else
84# ifdef HAVE_SYS_POLL_H
85# include <sys/poll.h>
86# endif
87#endif
81#ifdef HAVE_SYS_TIME_H 88#ifdef HAVE_SYS_TIME_H
82# include <sys/time.h> 89# include <sys/time.h>
83#endif 90#endif
@@ -109,6 +116,8 @@
109 116
110extern char *__progname; 117extern char *__progname;
111 118
119#define COPY_BUFLEN 16384
120
112int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout); 121int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout);
113 122
114void bwlimit(int); 123void bwlimit(int);
@@ -282,6 +291,7 @@ void sink(int, char *[]);
282void source(int, char *[]); 291void source(int, char *[]);
283void tolocal(int, char *[]); 292void tolocal(int, char *[]);
284void toremote(char *, int, char *[]); 293void toremote(char *, int, char *[]);
294size_t scpio(ssize_t (*)(int, void *, size_t), int, void *, size_t, off_t *);
285void usage(void); 295void usage(void);
286 296
287int 297int
@@ -441,6 +451,43 @@ main(int argc, char **argv)
441 exit(errs != 0); 451 exit(errs != 0);
442} 452}
443 453
454/*
455 * atomicio-like wrapper that also applies bandwidth limits and updates
456 * the progressmeter counter.
457 */
458size_t
459scpio(ssize_t (*f)(int, void *, size_t), int fd, void *_p, size_t l, off_t *c)
460{
461 u_char *p = (u_char *)_p;
462 size_t offset;
463 ssize_t r;
464 struct pollfd pfd;
465
466 pfd.fd = fd;
467 pfd.events = f == read ? POLLIN : POLLOUT;
468 for (offset = 0; offset < l;) {
469 r = f(fd, p + offset, l - offset);
470 if (r == 0) {
471 errno = EPIPE;
472 return offset;
473 }
474 if (r < 0) {
475 if (errno == EINTR)
476 continue;
477 if (errno == EAGAIN || errno == EWOULDBLOCK) {
478 (void)poll(&pfd, 1, -1); /* Ignore errors */
479 continue;
480 }
481 return offset;
482 }
483 offset += (size_t)r;
484 *c += (off_t)r;
485 if (limit_rate)
486 bwlimit(r);
487 }
488 return offset;
489}
490
444void 491void
445toremote(char *targ, int argc, char **argv) 492toremote(char *targ, int argc, char **argv)
446{ 493{
@@ -582,8 +629,8 @@ source(int argc, char **argv)
582 struct stat stb; 629 struct stat stb;
583 static BUF buffer; 630 static BUF buffer;
584 BUF *bp; 631 BUF *bp;
585 off_t i, amt, statbytes; 632 off_t i, statbytes;
586 size_t result; 633 size_t amt;
587 int fd = -1, haderr, indx; 634 int fd = -1, haderr, indx;
588 char *last, *name, buf[2048], encname[MAXPATHLEN]; 635 char *last, *name, buf[2048], encname[MAXPATHLEN];
589 int len; 636 int len;
@@ -604,6 +651,10 @@ source(int argc, char **argv)
604syserr: run_err("%s: %s", name, strerror(errno)); 651syserr: run_err("%s: %s", name, strerror(errno));
605 goto next; 652 goto next;
606 } 653 }
654 if (stb.st_size < 0) {
655 run_err("%s: %s", name, "Negative file size");
656 goto next;
657 }
607 unset_nonblock(fd); 658 unset_nonblock(fd);
608 switch (stb.st_mode & S_IFMT) { 659 switch (stb.st_mode & S_IFMT) {
609 case S_IFREG: 660 case S_IFREG:
@@ -629,8 +680,14 @@ syserr: run_err("%s: %s", name, strerror(errno));
629 * versions expecting microseconds. 680 * versions expecting microseconds.
630 */ 681 */
631 (void) snprintf(buf, sizeof buf, "T%lu 0 %lu 0\n", 682 (void) snprintf(buf, sizeof buf, "T%lu 0 %lu 0\n",
632 (u_long) stb.st_mtime, 683 (u_long) (stb.st_mtime < 0 ? 0 : stb.st_mtime),
633 (u_long) stb.st_atime); 684 (u_long) (stb.st_atime < 0 ? 0 : stb.st_atime));
685 if (verbose_mode) {
686 fprintf(stderr, "File mtime %ld atime %ld\n",
687 (long)stb.st_mtime, (long)stb.st_atime);
688 fprintf(stderr, "Sending file timestamps: %s",
689 buf);
690 }
634 (void) atomicio(vwrite, remout, buf, strlen(buf)); 691 (void) atomicio(vwrite, remout, buf, strlen(buf));
635 if (response() < 0) 692 if (response() < 0)
636 goto next; 693 goto next;
@@ -645,7 +702,7 @@ syserr: run_err("%s: %s", name, strerror(errno));
645 (void) atomicio(vwrite, remout, buf, strlen(buf)); 702 (void) atomicio(vwrite, remout, buf, strlen(buf));
646 if (response() < 0) 703 if (response() < 0)
647 goto next; 704 goto next;
648 if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) { 705 if ((bp = allocbuf(&buffer, fd, COPY_BUFLEN)) == NULL) {
649next: if (fd != -1) { 706next: if (fd != -1) {
650 (void) close(fd); 707 (void) close(fd);
651 fd = -1; 708 fd = -1;
@@ -654,27 +711,25 @@ next: if (fd != -1) {
654 } 711 }
655 if (showprogress) 712 if (showprogress)
656 start_progress_meter(curfile, stb.st_size, &statbytes); 713 start_progress_meter(curfile, stb.st_size, &statbytes);
657 /* Keep writing after an error so that we stay sync'd up. */ 714 set_nonblock(remout);
658 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) { 715 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
659 amt = bp->cnt; 716 amt = bp->cnt;
660 if (i + amt > stb.st_size) 717 if (i + (off_t)amt > stb.st_size)
661 amt = stb.st_size - i; 718 amt = stb.st_size - i;
662 if (!haderr) { 719 if (!haderr) {
663 result = atomicio(read, fd, bp->buf, amt); 720 if (atomicio(read, fd, bp->buf, amt) != amt)
664 if (result != amt)
665 haderr = errno; 721 haderr = errno;
666 } 722 }
667 if (haderr) 723 /* Keep writing after error to retain sync */
668 (void) atomicio(vwrite, remout, bp->buf, amt); 724 if (haderr) {
669 else { 725 (void)atomicio(vwrite, remout, bp->buf, amt);
670 result = atomicio(vwrite, remout, bp->buf, amt); 726 continue;
671 if (result != amt)
672 haderr = errno;
673 statbytes += result;
674 } 727 }
675 if (limit_rate) 728 if (scpio(vwrite, remout, bp->buf, amt,
676 bwlimit(amt); 729 &statbytes) != amt)
730 haderr = errno;
677 } 731 }
732 unset_nonblock(remout);
678 if (showprogress) 733 if (showprogress)
679 stop_progress_meter(); 734 stop_progress_meter();
680 735
@@ -780,10 +835,10 @@ bwlimit(int amount)
780 thresh /= 2; 835 thresh /= 2;
781 if (thresh < 2048) 836 if (thresh < 2048)
782 thresh = 2048; 837 thresh = 2048;
783 } else if (bwend.tv_usec < 100) { 838 } else if (bwend.tv_usec < 10000) {
784 thresh *= 2; 839 thresh *= 2;
785 if (thresh > 32768) 840 if (thresh > COPY_BUFLEN * 4)
786 thresh = 32768; 841 thresh = COPY_BUFLEN * 4;
787 } 842 }
788 843
789 TIMEVAL_TO_TIMESPEC(&bwend, &ts); 844 TIMEVAL_TO_TIMESPEC(&bwend, &ts);
@@ -974,7 +1029,7 @@ bad: run_err("%s: %s", np, strerror(errno));
974 continue; 1029 continue;
975 } 1030 }
976 (void) atomicio(vwrite, remout, "", 1); 1031 (void) atomicio(vwrite, remout, "", 1);
977 if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) { 1032 if ((bp = allocbuf(&buffer, ofd, COPY_BUFLEN)) == NULL) {
978 (void) close(ofd); 1033 (void) close(ofd);
979 continue; 1034 continue;
980 } 1035 }
@@ -984,26 +1039,24 @@ bad: run_err("%s: %s", np, strerror(errno));
984 statbytes = 0; 1039 statbytes = 0;
985 if (showprogress) 1040 if (showprogress)
986 start_progress_meter(curfile, size, &statbytes); 1041 start_progress_meter(curfile, size, &statbytes);
987 for (count = i = 0; i < size; i += 4096) { 1042 set_nonblock(remin);
988 amt = 4096; 1043 for (count = i = 0; i < size; i += bp->cnt) {
1044 amt = bp->cnt;
989 if (i + amt > size) 1045 if (i + amt > size)
990 amt = size - i; 1046 amt = size - i;
991 count += amt; 1047 count += amt;
992 do { 1048 do {
993 j = atomicio(read, remin, cp, amt); 1049 j = scpio(read, remin, cp, amt, &statbytes);
994 if (j == 0) { 1050 if (j == 0) {
995 run_err("%s", j ? strerror(errno) : 1051 run_err("%s", j != EPIPE ?
1052 strerror(errno) :
996 "dropped connection"); 1053 "dropped connection");
997 exit(1); 1054 exit(1);
998 } 1055 }
999 amt -= j; 1056 amt -= j;
1000 cp += j; 1057 cp += j;
1001 statbytes += j;
1002 } while (amt > 0); 1058 } while (amt > 0);
1003 1059
1004 if (limit_rate)
1005 bwlimit(4096);
1006
1007 if (count == bp->cnt) { 1060 if (count == bp->cnt) {
1008 /* Keep reading so we stay sync'd up. */ 1061 /* Keep reading so we stay sync'd up. */
1009 if (wrerr == NO) { 1062 if (wrerr == NO) {
@@ -1017,6 +1070,7 @@ bad: run_err("%s: %s", np, strerror(errno));
1017 cp = bp->buf; 1070 cp = bp->buf;
1018 } 1071 }
1019 } 1072 }
1073 unset_nonblock(remin);
1020 if (showprogress) 1074 if (showprogress)
1021 stop_progress_meter(); 1075 stop_progress_meter();
1022 if (count != 0 && wrerr == NO && 1076 if (count != 0 && wrerr == NO &&