1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
|
BEGIN { $^W = 1; $| = 1;}
use Test;
#!/usr/local/bin/perl -w
#
# Severely hacked test based on IPC example from
# Advanced Perl/Tk Programming.
use 5.005;
use Carp;
#use IPADM;
use IO::Socket;
use Tk;
use Tk::LabEntry;
use Tk::ROText;
use strict;
use vars qw/$EXIT $HN $ME $MW $PID/;
use subs qw/do_command fini init ipsnd list_subnet open_subnet
pipe_in pipe_out read_sdb start_ipc_helper/;
use vars qw/$AUTHORIZED_CLIENTS $DAEMON_HOST $DAEMON_PORT $DEBUG
$EOF $MOD_DB_PATH $ORGANIZATION $SDB_PATH $VERSION/;
BEGIN { plan test => 1}
if ($^O eq 'MSWin32') {
skip(1, 'Cannot test pipe/fork/exec/fileevent on Win32 systems.');
exit;
}
$AUTHORIZED_CLIENTS = 'localhost|loopback'; # a regex
$DAEMON_HOST = 'localhost';
$DAEMON_PORT = 1234;
$DEBUG = 1;
$EOF = '_END_OF_INFORMATION_';
$ORGANIZATION = 'Subnet List for ACME Rocket Supply, Inc.';
init;
MainLoop;
fini;
sub do_command {
# Issue a single IPADM command and wait for a reply. Using
# pipes and fileevent() allows X11 events to continue flowing.
pipe_out @_;
return pipe_in;
} # end do_command
sub pipe_in {
# Now that the IPADM command has been issued, keep sysread-ing
# until the $EOF string is read, and return all the accumulated
# data, excluding $EOF.
my(@data, $sysbuf, $sysdata, $sysstat, $wait);
$MW->fileevent(\*PR, 'readable' => sub {
# print STDERR "IN: about to sysread ...\n" if $DEBUG;
if ( ($Tk::VERSION cmp '800.015') <= 0 ) {
# if ( defined($ARGV[0]) ) {
$sysbuf = <PR>;
# print STDERR "IN: sysbuf=$sysbuf\n" if $DEBUG;
} else {
$sysstat = sysread PR, $sysbuf, 7;
die "ipadm: sysread error $!" unless defined $sysstat;
# print STDERR "IN: bytes=$sysstat\n" if $DEBUG;
}
$sysdata .= $sysbuf;
if ($sysdata =~ /$EOF$/s) {
@data = split /\n/, $sysdata;
$#data--; # throw $EOF away
$wait++; # unblock waitVariable()
}
});
$MW->waitVariable(\$wait);
$MW->fileevent(\*PR, 'readable' => '');
# print STDERR "IN: @data\n" if $DEBUG;
@data;
} # end pipe_in
sub pipe_out {
# Issue an IPADM command by syswrite-ing all the data plus
# the terminating $EOF.
return unless defined $_[0];
my($bytes, $offset, $sysdata, $sysstat, $wait);
$sysdata = join '', @_, "$EOF\n";
$bytes = length $sysdata;
$offset = 0;
$MW->fileevent(\*PW, 'writable' => sub {
while ($bytes > 0) {
$sysstat = syswrite PW, $sysdata, $bytes, $offset;
die "ipadm: syswrite error $!" unless defined $sysstat;
$bytes -= $sysstat;
$offset += $sysstat;
}
$wait++;
});
$MW->waitVariable(\$wait);
$MW->fileevent(\*PW, 'writable' => '');
# print STDERR "OT: $sysdata\n" if $DEBUG;
} # end pipe_out
sub fini {
kill 'SIGTERM', $PID;
unlink '/tmp/ipadmh';
exit $EXIT;
}
sub init {
my $frog = defined $ARGV[0] ? '<HANDLE>' : 'sysread()';
my $well = 'all is well';
if ( (($Tk::VERSION cmp '800.015') <= 0) and ($frog eq 'sysread()') ) {
$well = 'all is NOT well';
}
if ( (($Tk::VERSION cmp '800.015') > 0) and ($frog eq '<HANDLE>') ) {
$well = 'all is NOT well';
}
open(HELPER, ">/tmp/ipadmh") or die "cannot write helper program: $!";
while ($_ = <DATA>) {
print HELPER;
}
close HELPER or die $!;
chmod 0755, '/tmp/ipadmh';
$MW = MainWindow->new;
$MW->title('ipadm - Administer IP Nodes');
$MW->iconname('ipadm');
$MW->minsize(50, 50);
$MW->protocol('WM_DELETE_WINDOW' => \&fini);
# Create the menubar and friends.
my $menubar = $MW->Menu;
$MW->configure(-menu => $menubar);
my $file = $menubar->cascade(-label => '~File');
my $edit = $menubar->cascade(-label => '~Edit');
my $help = $menubar->cascade(-label => '~Help');
$file->command(-label => 'Quit', -command => \&fini);
$edit->command(-label => 'Fast Find', -command => [$MW => 'bell']);
$help->command(-label => 'About', -command => sub {
$MW->messageBox(-message => "ipadm $VERSION\n\n99/07/15")});
# Create the subnets table, a list of hypertext links, and some tags
# to highlight the active entry and a binding to load a subnet.
my $t = $MW->Scrolled('ROText',
qw/-width 80 -height 10 -relief ridge -scrollbars w/);
$t->pack(qw/-padx 5 -pady 3 -fill both -expand 1/);
$t->tagConfigure(qw/title -font/ => 'Helvetica 18 bold');
$t->tagConfigure(qw/subnet -lmargin1 .5c -lmargin2 1c -foreground blue/);
$t->tagConfigure(qw/hot -relief raised -borderwidth 1 -background green/);
start_ipc_helper;
$t->insert('end', "\n$ORGANIZATION\n\n", ['title']);
# Get a list of subnets from ipadmd, sort the subnets by
# IP number, and add the title string to the text widget,
# tagged with the SDB file name.
my($status, @subnet_list) = do_command "get_subnet_list\n";
die "Cannot get SDB list" unless $status =~ /OK/;
foreach (sort numerically @subnet_list) {
my($sdb, $title) = /^(\S+)\s+(.*)$/;
$t->insert('end', "$title\n", ['subnet', $sdb]);
}
#$t->tagBind(qw/subnet <ButtonRelease-1>/ => \&open_subnet);
my $last_hot = '';
$t->tagBind(qw/subnet <Enter>/ => sub {
my $text = shift; # SUBTLE STATEMENT HERE (-:
my($x, $y) = ($Tk::event->x, $Tk::event->y);
$last_hot = $text->index("\@$x,$y linestart");
$text->tagAdd('hot', $last_hot, "$last_hot lineend");
});
$t->tagBind(qw/subnet <Leave>/ => sub {
shift->tagRemove(qw/hot 1.0 end/);
});
$t->tagBind(qw/subnet <Motion>/ => sub {
my $text = shift;
my($x, $y) = ($Tk::event->x, $Tk::event->y);
my $new_hot = $text->index("\@$x,$y linestart");
if ($new_hot ne $last_hot) {
$text->tagRemove(qw/hot 1.0 end/);
$text->tagAdd('hot', $new_hot, "$new_hot lineend");
$last_hot = $new_hot;
}
});
chomp($HN = `hostname`);
$ME = getlogin;
# Sanity check, see if the 4th line was read.
my $fourth = 'Subnet 128B, ACME Rubber Band Development';
$MW->update;
$MW->after(1000);
my $data = $t->get('7.0', '7.0 lineend');
#print "four=$fourth!\n";
#print "data=$data!\n";
$EXIT = ($data eq $fourth) ? 0 : 1;
ok($data, $fourth);
$MW->destroy;
} # end init
sub lsearch { # $o = lsearch $regexp, @list;
# Search the list using the supplied regular expression and return it's
# ordinal, or -1 if not found.
my($regexp, @list) = @_;
my($i);
for ($i=0; $i<=$#list; $i++) {
return $i if $list[$i] =~ /$regexp/;
}
return -1;
} # end lsearch
sub numerically {
my($n1, $n2);
($n1) = $a =~ /Subnet_(\d+)/;
($n2) = $b =~ /Subnet_(\d+)/;
if ($n1 != $n2) {
$n1 <=> $n2;
} else {
$a cmp $b;
}
}
sub start_ipc_helper {
# Start a child process and use pipes to talk with it. The child
# uses sockets to talk to the remote IPADM daemon.
$SIG{PIPE} = sub {print STDERR "ipadmh pipe failure.\n"; exit};
pipe CR, PW or die "cr/pw pipe $!";
pipe PR, CW or die "pr/cw pipe $!";
if ($PID = fork) { # parent, ipadm/Tk
close CR;
close CW;
PW->autoflush(1);
} elsif (defined $PID) { # child, exec ipadmh
close PR;
close PW;
open STDIN, "<&CR" or die "STDIN open $!";
open STDOUT, ">&CW" or die "STDOUT open $!";
open STDERR, ">&CW" or die "STDERR open $!";
STDOUT->autoflush(1);
STDERR->autoflush(1);
exec($^X,"/tmp/ipadmh", $DAEMON_HOST, $DAEMON_PORT) or die "exec $!";
die "exec warp $!";
} else {
die "fork $!";
} # ifend fork
my(@stat) = do_command undef; # did helper make a connection?
return if $stat[0] =~ /Connect OK/;
$MW->messageBox(-message => "Cannot connect to remote IPADM daemon " .
"$DAEMON_HOST:$DAEMON_PORT. Please try again later.",
-title => 'Daemon is Dead', -icon => 'warning',
-type => 'OK');
fini;
} # end start_ipc_helper
__DATA__
#!/usr/local/bin/perl
use 5.005;
#use IPADM;
use IO::Socket;
use strict;
my $EOF = '_END_OF_INFORMATION_';
do {print "Usage: ipadmh host port\n"; exit} unless @ARGV == 2;
STDOUT->autoflush(1); # unbuffer output
sub timeout {print "1 Socket Timeout\n$EOF\n"; $SIG{ALRM} = \&timeout}
$SIG{PIPE} = sub {print "2 Pipe Error.\n$EOF\n"};
my $sock = 'defined';
print +((defined $sock) ? "0 Connect OK" : "3 Connect Failed"), "\n$EOF\n";
while(<STDIN>) {
last if /^$EOF$/;
}
my(@data) = ();
$SIG{ALRM} = \&timeout; # reset handler
alarm 60;
@data = <<"END-DATA";
0 OK
Subnet_1.sdb Subnet 1, ACME Bean Counting Department
Subnet_128A.sdb Subnet 128A, ACME Coil Spring Development
Subnet_128B.sdb Subnet 128B, ACME Rubber Band Development
Subnet_2.sdb Subnet 2, ACME Purchasing Department
$EOF
END-DATA
alarm 0;
#print (/^$EOF$/ ? @data : "4 Daemon Failure\n$EOF\n");
print @data;
__END__
print <<"END-HELP";
Run this program with or without a command line argument. If
\$ARGV[0] is defined then <HANDLE> is used by the fileevent()
callback to read from the pipe. If \$ARGV[0] is not defined,
then sysread() is used.
This is Tk version $Tk::VERSION, and we'll use $frog to read
from the pipe.
When all is well, you should see a MainWindow with the heading
"Subnet List for ACME Rocket Supply, Inc", followed by a list
of (4) subnets.
================================================================
= With this combination of Tk $Tk::VERSION and $frog I suspect
= $well!
================================================================
The subnet list comes from a helper program via a pipe - the
real helper program gets this data via a socket from a server,
but for this demo the subnet data is hardcoded in the helper.
Regardless, the parent Tk program execs the helper and first
"asks" for the subnet list by issuing a command on its write
pipe, which the helper reads on its STDIN. For this demo, the
helper doesn't even look at what it reads but simply writes the
subnet list to its STDOUT.
Now for the bug: the Perl/Tk parent needs to read this subnet
list from its input pipe, but the fileevent() *mechanism*
changed after Tk version 800.015. The subroutine pipe_in() in
the Tk parent "ipadm" needs to read via a <HANDLE> for Tk800.015
and before, but sysread() for Tk800.018.
END-HELP
|