#!/usr/bin/perl -w
# @configure_input@
# virt-hostinfo
# Copyright (C) 2009 Red Hat Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

use strict;

use Pod::Usage;
use Getopt::Long;

=pod

=head1 NAME

hostinfo-test - make or test hostinfo requests from a virtual machine

=head1 SYNOPSIS

 hostinfo-test [--options]

=head1 DESCRIPTION

I<Note:> It only makes sense to use this command from inside
a guest (virtual machine).

This command is used to make/test hostinfo requests.  You can
use this to test that the hostinfo system is working end-to-end.

 # 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 non-zero if the
command failed or there was some other error.

=head1 SEE ALSO

L<hostinfo(8)>,
L<hostinfo-set(8)>,
L<hostinfo-status(8)>,
L<hostinfo-protocol(5)>,
L<virt-install(1)>,
L<virt-manager(1)>,
L<http://fedoraproject.org/wiki/Features/Hostinfo>,
L<http://libvirt.org/>.

=head1 AUTHORS

Richard W.M. Jones (C<rjones at redhat dot com>)

=head1 COPYRIGHT

Copyright (C) 2009 Red Hat Inc.
L<http://fedoraproject.org/wiki/Features/Hostinfo>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
