| 1 |
#!/usr/bin/perl -w |
| 2 |
#/**************************************************************************/ |
| 3 |
#/* */ |
| 4 |
#/* Copyright (c) 2015-2016 Qindel Group (http://www.qindel.com) */ |
| 5 |
#/* */ |
| 6 |
#/* NXSCRIPTS, NX protocol compression and NX extensions to this software */ |
| 7 |
#/* are copyright of the aforementioned persons and companies. */ |
| 8 |
#/* */ |
| 9 |
#/* Redistribution and use of the present software is allowed according */ |
| 10 |
#/* to terms specified in the file LICENSE.nxcomp which comes in the */ |
| 11 |
#/* source distribution. */ |
| 12 |
#/* */ |
| 13 |
#/* All rights reserved. */ |
| 14 |
#/* */ |
| 15 |
#/* NOTE: This software has received contributions from various other */ |
| 16 |
#/* contributors, only the core maintainers and supporters are listed as */ |
| 17 |
#/* copyright holders. Please contact us, if you feel you should be listed */ |
| 18 |
#/* as copyright holder, as well. */ |
| 19 |
#/* */ |
| 20 |
#/**************************************************************************/ |
| 21 |
|
| 22 |
# This is a test script for the slave channel feature. While there are no |
| 23 |
# particular requirements for the slave channel command, what makes the most |
| 24 |
# sense is to use the feature to run some sort of multiplexer. |
| 25 |
# |
| 26 |
# This script contains functionality intended to test the channel's bandwidth, |
| 27 |
# latency and integrity. |
| 28 |
# |
| 29 |
# Usage: |
| 30 |
# Set NX_SLAVE_CMD to this script and enable the slave channel |
| 31 |
# |
| 32 |
# Run: |
| 33 |
# nxproxy [...] slave=12000 |
| 34 |
# nxagent -display nx/nx,options=nxagent.conf # slave=22000 in nxagent.conf |
| 35 |
# |
| 36 |
# Where 12000 and 22000 are example TCP ports the program will listen on. |
| 37 |
# |
| 38 |
# For ease of debugging and running both sides on a single machine, the script |
| 39 |
# reacts to its own name and changes the prompt to "Proxy" if $0 contains |
| 40 |
# "proxy", or "Agent" if it contains "agent". This has no other effect. |
| 41 |
# |
| 42 |
# |
| 43 |
|
| 44 |
use strict; |
| 45 |
use POSIX ":sys_wait_h"; |
| 46 |
|
| 47 |
my $me = "?"; |
| 48 |
my $EXIT; |
| 49 |
|
| 50 |
$| = 1; |
| 51 |
|
| 52 |
if ( $0 =~ /proxy/i ) { |
| 53 |
$me = "Proxy"; |
| 54 |
} elsif ( $0 =~ /agent/i ) { |
| 55 |
$me = "Agent"; |
| 56 |
} else { |
| 57 |
$me = $0; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
print "$me slave. "; |
| 62 |
if ( scalar @ARGV ) { |
| 63 |
print "Called with arguments: " . join(' ', @ARGV) . "\n\n"; |
| 64 |
} else { |
| 65 |
print "Called without arguments.\n\n"; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
my %commands; |
| 70 |
register('quit' , \&cmd_quit , "Exit."); |
| 71 |
register('fork' , \&cmd_fork , "Test forking a child process."); |
| 72 |
register('exec' , \&cmd_exec , "Test calling another process via exec."); |
| 73 |
register('system' , \&cmd_system , "Test calling another process via system."); |
| 74 |
register('echo' , \&cmd_echo , "Echo text after the command. Tests channel latency."); |
| 75 |
register('blkecho' , \&cmd_blkecho, "Echo data by 1k blocks. Tests channel integrity."); |
| 76 |
register('reexec' , \&cmd_reexec , "Reexecute slave handler."); |
| 77 |
register('chargen' , \&cmd_chargen, "Output characters forever. Tests channel throughput."); |
| 78 |
register('randgen' , \&cmd_randgen, "Output random characters forever. Tests channel throughput."); |
| 79 |
register('discard' , \&cmd_discard, "Accept characters forever. Tests channel throughput."); |
| 80 |
register('fastgen' , \&cmd_fastgen, "Output a single character forever. Tests channel throughput."); |
| 81 |
register('env' , \&cmd_env , "Dump the environment."); |
| 82 |
register('help' , \&cmd_help , "Shows this help."); |
| 83 |
register('pwd' , \&cmd_pwd , "Print working directory."); |
| 84 |
cmd_help(); |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
#print "$me> "; |
| 89 |
my $line; |
| 90 |
while(!$EXIT) { |
| 91 |
|
| 92 |
print "$me> "; |
| 93 |
|
| 94 |
# Buffered IO screws things up |
| 95 |
my $c=""; |
| 96 |
$line = ""; |
| 97 |
while($c ne "\n") { |
| 98 |
my $ret = sysread(STDIN, $c, 1); |
| 99 |
if (!defined $ret) { |
| 100 |
die "Read failed: $!"; |
| 101 |
} |
| 102 |
|
| 103 |
if (!$ret) { |
| 104 |
last; |
| 105 |
} |
| 106 |
|
| 107 |
$line .= $c; |
| 108 |
} |
| 109 |
|
| 110 |
chomp $line; |
| 111 |
$line =~ s/\r+$//; |
| 112 |
$line =~ s/\n+$//; |
| 113 |
|
| 114 |
next unless ( $line ); |
| 115 |
|
| 116 |
my ($cmd, @args) = split(/\s+/, $line); |
| 117 |
if ( exists $commands{$cmd} ) { |
| 118 |
$commands{$cmd}->{handler}->( @args ); |
| 119 |
} else { |
| 120 |
print "Unknown command: '$cmd'\n"; |
| 121 |
} |
| 122 |
|
| 123 |
} |
| 124 |
|
| 125 |
print "$me slave terminated.\n"; |
| 126 |
|
| 127 |
sub register { |
| 128 |
my ($name, $handler, $desc) = @_; |
| 129 |
$commands{$name} = { handler => $handler, desc => $desc }; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
sub cmd_quit { |
| 134 |
$EXIT = 1; |
| 135 |
} |
| 136 |
|
| 137 |
sub cmd_echo { |
| 138 |
my (@args) = @_; |
| 139 |
print "You said: '" . join(' ', @args) . "'\n\n"; |
| 140 |
} |
| 141 |
|
| 142 |
sub cmd_blkecho { |
| 143 |
my $size = shift // 1024; |
| 144 |
unless ($size =~ /^\d+/ ) { |
| 145 |
print "The argument must be a number\n\n"; |
| 146 |
return; |
| 147 |
} |
| 148 |
|
| 149 |
my $buf; |
| 150 |
while(1) { |
| 151 |
my ($tmp, $ret); |
| 152 |
$buf = ""; |
| 153 |
|
| 154 |
while(length($buf) < $size) { |
| 155 |
$ret = sysread(STDIN, $tmp, $size - length($buf)); |
| 156 |
if ( !defined $ret ) { |
| 157 |
die "Error reading from socket: $!"; |
| 158 |
} |
| 159 |
|
| 160 |
last if ( $ret == 0 ); |
| 161 |
$buf .= $tmp; |
| 162 |
} |
| 163 |
|
| 164 |
my $written =0; |
| 165 |
while($written < $size) { |
| 166 |
my $ret = syswrite(STDOUT, $buf, $size, $written); |
| 167 |
if (!defined $ret) { |
| 168 |
die "Error writing to socket: $!"; |
| 169 |
} |
| 170 |
|
| 171 |
last if ( $ret == 0); |
| 172 |
$written += $ret; |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
sub cmd_reexec { |
| 178 |
print "Will now re-execute: $0 " . join(' ', @ARGV) . "\n"; |
| 179 |
exec($0, @ARGV); |
| 180 |
} |
| 181 |
|
| 182 |
sub cmd_exec { |
| 183 |
print "Will now exec: uname -a\n"; |
| 184 |
exec("uname", "-a"); |
| 185 |
} |
| 186 |
|
| 187 |
sub cmd_system { |
| 188 |
print "Will now call: uptime\n"; |
| 189 |
system("uptime"); |
| 190 |
print "\n"; |
| 191 |
} |
| 192 |
|
| 193 |
# Forks off a short lived process |
| 194 |
|
| 195 |
sub cmd_fork { |
| 196 |
my $pid = fork(); |
| 197 |
if ( $pid == 0 ) { |
| 198 |
print "I am the child, with pid $$. Waiting 10 seconds.\n"; |
| 199 |
sleep(10); |
| 200 |
print "Child exiting with code 123\n"; |
| 201 |
exit(123); |
| 202 |
} else { |
| 203 |
print "I am the parent, my child is $pid\n"; |
| 204 |
my $dead = waitpid($pid, 0); |
| 205 |
print "Reaped child $pid, return $dead\n"; |
| 206 |
} |
| 207 |
|
| 208 |
print "\n"; |
| 209 |
} |
| 210 |
|
| 211 |
sub cmd_help { |
| 212 |
print "Commands:\n"; |
| 213 |
for my $cmd ( sort keys %commands ) { |
| 214 |
print "\t$cmd" . ( " " x (10 - length($cmd))) . ": " . $commands{$cmd}->{desc} . "\n"; |
| 215 |
} |
| 216 |
print "\n"; |
| 217 |
} |
| 218 |
|
| 219 |
# Output a single character really fast. |
| 220 |
# Used to test bandwidth and integrity |
| 221 |
sub cmd_fastgen { |
| 222 |
my $char = chr(shift // ord("x")); |
| 223 |
my $buf = ($char x 72) . "\n"; |
| 224 |
while( syswrite(STDOUT, $buf) ) { |
| 225 |
1; |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
# Output RFC 864 chargen. |
| 230 |
# Used to test bandwidth |
| 231 |
sub cmd_chargen { |
| 232 |
my $text = ""; |
| 233 |
|
| 234 |
for(my $i=33;$i<33+95;$i++) { |
| 235 |
$text .= chr($i); |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
my $pos = 0; |
| 240 |
my $strlen = 72; |
| 241 |
while( 1 ) { |
| 242 |
my $out = substr($text, $pos, $strlen); |
| 243 |
|
| 244 |
if ( $pos + $strlen > length($text) ) { |
| 245 |
$out .= substr($text, 0, $pos + $strlen - length($text) + 1); |
| 246 |
} |
| 247 |
|
| 248 |
$out .= "\n"; |
| 249 |
|
| 250 |
syswrite(STDOUT, $out) or return; |
| 251 |
|
| 252 |
if ( ++$pos >= length($text) ) { |
| 253 |
$pos=0; |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
} |
| 258 |
|
| 259 |
# Output random data |
| 260 |
# Used to test channel integrity and bandwidth with incompressible content. |
| 261 |
# Typically slower than chargen/fastgen. |
| 262 |
|
| 263 |
sub cmd_randgen { |
| 264 |
if ( open(my $fh, '<', '/dev/urandom') ) { |
| 265 |
my $buf; |
| 266 |
while(1) { |
| 267 |
sysread($fh, $buf, 1024); |
| 268 |
syswrite(STDOUT, $buf) or return; |
| 269 |
} |
| 270 |
} else { |
| 271 |
print "Failed to open /dev/urandom: $!. May not be available on this architecture.\n\n"; |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
# Accept arbitrary data |
| 276 |
# Used to test bandwidth |
| 277 |
|
| 278 |
sub cmd_discard { |
| 279 |
my $buf = ""; |
| 280 |
|
| 281 |
while( sysread(STDIN, $buf, 1024) ) { |
| 282 |
1; |
| 283 |
} |
| 284 |
|
| 285 |
} |
| 286 |
|
| 287 |
# Dump the environment |
| 288 |
|
| 289 |
sub cmd_env { |
| 290 |
my $longest = 0; |
| 291 |
|
| 292 |
foreach my $var (keys %ENV) { |
| 293 |
if ( $longest < length($var) ) { |
| 294 |
$longest = length($var); |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
foreach my $var (sort keys %ENV) { |
| 299 |
print "$var" . (" " x ($longest - length($var))) . ": $ENV{$var}\n"; |
| 300 |
} |
| 301 |
|
| 302 |
print "\n"; |
| 303 |
} |
| 304 |
|
| 305 |
# Show the current directory |
| 306 |
|
| 307 |
sub cmd_pwd { |
| 308 |
require Cwd; |
| 309 |
import Cwd; |
| 310 |
print "Current directory: " . getcwd() . "\n\n"; |
| 311 |
} |