use strict;
-#use Sys::Virt;
+use Pod::Usage;
+use Getopt::Long;
=pod
This command is used to make/test hostinfo requests. You can
use this to test that the hostinfo system is working end-to-end.
-On its own, the command will make a simple 'ping' request, just
-to check that the path from the guest to the C<hostinfod> (daemon)
-inside the host is working:
-
# hostinfo-test
or:
# hostinfo-test
error: no response ping request
+=head1 OPTIONS
+
+=over 4
+
+=cut
+
+my $help;
+
+=item B<--help>
+
+Display brief help.
+
+=cut
+
+my $version;
+
+=item B<--version>
+
+Display version number and exit.
+
+=cut
+
+my $serial = "/dev/ttyS1";
+
+=item B<--serial /dev/ttyS?> | B<-s /dev/ttyS?>
+
+Select the serial device to use. The default is C</dev/ttyS1>.
+
+If you use the wrong serial device, you will get no response from
+hostinfo commands, so choosing the correct device is important. To
+find out which serial device to use, you should get the host system
+administrator to run L<hostinfo-status(8)> on the host.
+
=cut
+my $verbose;
+
+=item B<--verbose> | B<-v>
+
+Verbose messages.
+
+=cut
+
+GetOptions ("help|?" => \$help,
+ "version" => \$version,
+ "serial|s=s" => \$serial,
+ "verbose|v" => \$verbose,
+ ) or pod2usage (2);
+pod2usage (1) if $help;
+if ($version) {
+ print "@VERSION@\n";
+ exit
+}
+
+# Set the serial port to raw mode, no echo.
+system ("stty raw -echo < $serial") == 0 or die "stty: $?";
+
+open SERIAL, "+<$serial" or die "$serial: $!";
+
+my $echodata;
+my @chars = ('a'..'z', 'A'..'Z', '0'..'9');
+$echodata .= $chars[rand @chars] foreach (1..16);
+
+print qq{<<< PING "$echodata"\n} if $verbose;
+print SERIAL qq{PING "$echodata"\r\n};
+
+local $SIG{ALRM} = sub { die "error: no response from ping test\n" };
+alarm 3;
+
+my $input = <SERIAL>;
+print ">>> $input" if $verbose;
+
+close SERIAL;
+
+if ($input !~ /^1\.\d+ 200 $echodata/) {
+ die "error: incorrect or unexpected response from ping test\n";
+}
+
+exit 0;
+
+=head1 TROUBLESHOOTING
+
+=over 4
+
+=item *
+
+Use I<-v> option to get more verbose messages.
+
+=item *
+
+Look in the system log files on the host.
+=item *
+Follow the I<TROUBLESHOOTING> section in L<hostinfo(8)> manpage.
+=back
=head1 RETURN VALUE
-This command returns 0 if the test succeeded, or 1 if the test failed
-or there was an error.
+This command returns 0 if the test succeeded, or 1 if the command
+failed or there was some other error.
=head1 SEE ALSO