aboutsummaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorSteven Rostedt (VMware) <rostedt@goodmis.org>2017-02-07 12:22:03 -0500
committerSteven Rostedt (VMware) <rostedt@goodmis.org>2017-02-07 12:23:38 -0500
commit6e98d1b4415fe681ceb245e1374ed5e1942a332b (patch)
treec1789b5e27c60eea88856f8782e5383afa71f527 /tools
parent32677207dcc5e594254b7fb4fb2352b1755b1d5b (diff)
ktest: Add timeout to ssh command
Add a timeout to performing an ssh command. This will let testing if a machine is alive or not, or if something else may be amiss. A timeout can be passed to ssh, where ssh will fail if it does not complete within the given timeout. Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
Diffstat (limited to 'tools')
-rwxr-xr-xtools/testing/ktest/ktest.pl35
1 files changed, 27 insertions, 8 deletions
diff --git a/tools/testing/ktest/ktest.pl b/tools/testing/ktest/ktest.pl
index a64da242b824..d9bdd3d6dba6 100755
--- a/tools/testing/ktest/ktest.pl
+++ b/tools/testing/ktest/ktest.pl
@@ -1668,19 +1668,18 @@ sub fail {
1668} 1668}
1669 1669
1670sub run_command { 1670sub run_command {
1671 my ($command, $redirect) = @_; 1671 my ($command, $redirect, $timeout) = @_;
1672 my $start_time; 1672 my $start_time;
1673 my $end_time; 1673 my $end_time;
1674 my $dolog = 0; 1674 my $dolog = 0;
1675 my $dord = 0; 1675 my $dord = 0;
1676 my $pid; 1676 my $pid;
1677 1677
1678 $start_time = time;
1679
1680 $command =~ s/\$SSH_USER/$ssh_user/g; 1678 $command =~ s/\$SSH_USER/$ssh_user/g;
1681 $command =~ s/\$MACHINE/$machine/g; 1679 $command =~ s/\$MACHINE/$machine/g;
1682 1680
1683 doprint("$command ... "); 1681 doprint("$command ... ");
1682 $start_time = time;
1684 1683
1685 $pid = open(CMD, "$command 2>&1 |") or 1684 $pid = open(CMD, "$command 2>&1 |") or
1686 (fail "unable to exec $command" and return 0); 1685 (fail "unable to exec $command" and return 0);
@@ -1697,14 +1696,34 @@ sub run_command {
1697 $dord = 1; 1696 $dord = 1;
1698 } 1697 }
1699 1698
1700 while (<CMD>) { 1699 my $hit_timeout = 0;
1701 print LOG if ($dolog); 1700
1702 print RD if ($dord); 1701 while (1) {
1702 my $fp = \*CMD;
1703 if (defined($timeout)) {
1704 doprint "timeout = $timeout\n";
1705 }
1706 my $line = wait_for_input($fp, $timeout);
1707 if (!defined($line)) {
1708 my $now = time;
1709 if (defined($timeout) && (($now - $start_time) >= $timeout)) {
1710 doprint "Hit timeout of $timeout, killing process\n";
1711 $hit_timeout = 1;
1712 kill 9, $pid;
1713 }
1714 last;
1715 }
1716 print LOG $line if ($dolog);
1717 print RD $line if ($dord);
1703 } 1718 }
1704 1719
1705 waitpid($pid, 0); 1720 waitpid($pid, 0);
1706 my $failed = $?; 1721 my $failed = $?;
1707 1722
1723 if ($hit_timeout) {
1724 $failed = 1;
1725 }
1726
1708 close(CMD); 1727 close(CMD);
1709 close(LOG) if ($dolog); 1728 close(LOG) if ($dolog);
1710 close(RD) if ($dord); 1729 close(RD) if ($dord);
@@ -1728,11 +1747,11 @@ sub run_command {
1728} 1747}
1729 1748
1730sub run_ssh { 1749sub run_ssh {
1731 my ($cmd) = @_; 1750 my ($cmd, $timeout) = @_;
1732 my $cp_exec = $ssh_exec; 1751 my $cp_exec = $ssh_exec;
1733 1752
1734 $cp_exec =~ s/\$SSH_COMMAND/$cmd/g; 1753 $cp_exec =~ s/\$SSH_COMMAND/$cmd/g;
1735 return run_command "$cp_exec"; 1754 return run_command "$cp_exec", undef , $timeout;
1736} 1755}
1737 1756
1738sub run_scp { 1757sub run_scp {