2004-12-30 16:34:01 +01:00
|
|
|
# -*- cperl -*-
|
2006-12-31 01:02:27 +01:00
|
|
|
# Copyright (C) 2004-2006 MySQL AB
|
|
|
|
#
|
|
|
|
# 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; version 2 of the License.
|
|
|
|
#
|
|
|
|
# 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2004-12-30 16:34:01 +01:00
|
|
|
|
|
|
|
# This is a library file used by the Perl version of mysql-test-run,
|
|
|
|
# and is part of the translation of the Bourne shell script with the
|
|
|
|
# same name.
|
|
|
|
|
2007-12-12 18:19:24 +01:00
|
|
|
use strict;
|
2005-02-03 21:13:27 +01:00
|
|
|
use Socket;
|
|
|
|
use Errno;
|
2009-04-28 23:06:36 +02:00
|
|
|
use My::Platform;
|
|
|
|
use if IS_WINDOWS, "Net::Ping";
|
|
|
|
|
2009-05-08 01:10:53 +02:00
|
|
|
# Ancient perl might not have port_number method for Net::Ping.
|
|
|
|
# Check it and use fallback to connect() if it is not present.
|
|
|
|
BEGIN
|
|
|
|
{
|
|
|
|
my $use_netping= 0;
|
|
|
|
if (IS_WINDOWS)
|
|
|
|
{
|
|
|
|
my $ping = Net::Ping->new();
|
|
|
|
if ($ping->can("port_number"))
|
|
|
|
{
|
|
|
|
$use_netping= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
eval 'sub USE_NETPING { $use_netping }';
|
|
|
|
}
|
|
|
|
|
2005-02-03 21:13:27 +01:00
|
|
|
sub sleep_until_file_created ($$$);
|
2006-10-04 12:47:32 +02:00
|
|
|
sub mtr_ping_port ($);
|
2004-12-30 16:34:01 +01:00
|
|
|
|
2006-10-04 12:47:32 +02:00
|
|
|
sub mtr_ping_port ($) {
|
2005-02-03 21:13:27 +01:00
|
|
|
my $port= shift;
|
|
|
|
|
2006-10-04 12:47:32 +02:00
|
|
|
mtr_verbose("mtr_ping_port: $port");
|
|
|
|
|
2009-05-08 01:10:53 +02:00
|
|
|
if (IS_WINDOWS && USE_NETPING)
|
2009-04-28 23:06:36 +02:00
|
|
|
{
|
|
|
|
# Under Windows, connect to a port that is not open is slow
|
|
|
|
# It takes ~1sec. Net::Ping with small timeout is much faster.
|
|
|
|
my $ping = Net::Ping->new();
|
|
|
|
$ping->port_number($port);
|
|
|
|
if ($ping->ping("localhost",0.1))
|
|
|
|
{
|
2009-05-08 01:10:53 +02:00
|
|
|
mtr_verbose("USED");
|
|
|
|
return 1;
|
2009-04-28 23:06:36 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-05-08 01:10:53 +02:00
|
|
|
mtr_verbose("FREE");
|
|
|
|
return 0;
|
2009-04-28 23:06:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-02-03 21:13:27 +01:00
|
|
|
my $remote= "localhost";
|
|
|
|
my $iaddr= inet_aton($remote);
|
|
|
|
if ( ! $iaddr )
|
|
|
|
{
|
|
|
|
mtr_error("can't find IP number for $remote");
|
|
|
|
}
|
|
|
|
my $paddr= sockaddr_in($port, $iaddr);
|
|
|
|
my $proto= getprotobyname('tcp');
|
|
|
|
if ( ! socket(SOCK, PF_INET, SOCK_STREAM, $proto) )
|
|
|
|
{
|
|
|
|
mtr_error("can't create socket: $!");
|
|
|
|
}
|
2006-10-04 12:47:32 +02:00
|
|
|
|
|
|
|
mtr_debug("Pinging server (port: $port)...");
|
|
|
|
|
2005-02-03 21:13:27 +01:00
|
|
|
if ( connect(SOCK, $paddr) )
|
|
|
|
{
|
|
|
|
close(SOCK); # FIXME check error?
|
2006-10-04 12:47:32 +02:00
|
|
|
mtr_verbose("USED");
|
2005-02-03 21:13:27 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-10-04 12:47:32 +02:00
|
|
|
mtr_verbose("FREE");
|
2005-02-03 21:13:27 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-11 01:35:08 +01:00
|
|
|
##############################################################################
|
|
|
|
#
|
|
|
|
# Wait for a file to be created
|
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
2005-08-16 19:49:43 +02:00
|
|
|
# FIXME check that the pidfile contains the expected pid!
|
2005-01-11 01:35:08 +01:00
|
|
|
|
2005-02-03 21:13:27 +01:00
|
|
|
sub sleep_until_file_created ($$$) {
|
2005-01-11 01:35:08 +01:00
|
|
|
my $pidfile= shift;
|
|
|
|
my $timeout= shift;
|
2007-12-12 18:19:24 +01:00
|
|
|
my $proc= shift;
|
2006-10-04 12:47:32 +02:00
|
|
|
my $sleeptime= 100; # Milliseconds
|
|
|
|
my $loops= ($timeout * 1000) / $sleeptime;
|
2005-01-11 01:35:08 +01:00
|
|
|
|
2006-10-04 12:47:32 +02:00
|
|
|
for ( my $loop= 1; $loop <= $loops; $loop++ )
|
2005-01-11 01:35:08 +01:00
|
|
|
{
|
|
|
|
if ( -r $pidfile )
|
|
|
|
{
|
2007-06-26 12:26:21 +02:00
|
|
|
return 1;
|
2005-01-11 01:35:08 +01:00
|
|
|
}
|
|
|
|
|
2006-10-04 12:47:32 +02:00
|
|
|
# Check if it died after the fork() was successful
|
2007-12-12 18:19:24 +01:00
|
|
|
if ( defined $proc and ! $proc->wait_one(0) )
|
2005-01-11 01:35:08 +01:00
|
|
|
{
|
2007-12-12 18:19:24 +01:00
|
|
|
mtr_warning("Process $proc died");
|
2005-02-03 21:13:27 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-10-04 12:47:32 +02:00
|
|
|
mtr_debug("Sleep $sleeptime milliseconds waiting for $pidfile");
|
2005-02-03 21:13:27 +01:00
|
|
|
|
2006-10-04 12:47:32 +02:00
|
|
|
# Print extra message every 60 seconds
|
|
|
|
my $seconds= ($loop * $sleeptime) / 1000;
|
2006-11-30 13:16:12 +01:00
|
|
|
if ( $seconds > 1 and int($seconds * 10) % 600 == 0 )
|
2005-02-03 21:13:27 +01:00
|
|
|
{
|
2006-10-04 12:47:32 +02:00
|
|
|
my $left= $timeout - $seconds;
|
|
|
|
mtr_warning("Waited $seconds seconds for $pidfile to be created, " .
|
2005-02-03 21:13:27 +01:00
|
|
|
"still waiting for $left seconds...");
|
2005-01-11 01:35:08 +01:00
|
|
|
}
|
|
|
|
|
2007-12-12 18:19:24 +01:00
|
|
|
mtr_milli_sleep($sleeptime);
|
2005-01-11 01:35:08 +01:00
|
|
|
|
2005-08-31 16:28:47 +02:00
|
|
|
}
|
2005-06-05 20:10:47 +02:00
|
|
|
|
2007-12-12 18:19:24 +01:00
|
|
|
return 0;
|
2005-06-05 20:10:47 +02:00
|
|
|
}
|
|
|
|
|
2006-10-04 12:47:32 +02:00
|
|
|
|
2004-12-30 16:34:01 +01:00
|
|
|
1;
|