2 # Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; version 2 of the License.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 # This is a library file used by the Perl version of mysql-test-run,
18 # and is part of the translation of the Bourne shell script with the
25 use if IS_WINDOWS
, "Net::Ping";
27 # Ancient perl might not have port_number method for Net::Ping.
28 # Check it and use fallback to connect() if it is not present.
34 my $ping = Net
::Ping
->new();
35 if ($ping->can("port_number"))
40 eval 'sub USE_NETPING { $use_netping }';
43 sub sleep_until_file_created
($$$);
44 sub mtr_ping_port
($);
46 sub mtr_ping_port
($) {
49 mtr_verbose
("mtr_ping_port: $port");
51 if (IS_WINDOWS
&& USE_NETPING
)
53 # Under Windows, connect to a port that is not open is slow
54 # It takes ~1sec. Net::Ping with small timeout is much faster.
55 my $ping = Net
::Ping
->new();
56 $ping->port_number($port);
57 if ($ping->ping("localhost",0.1))
69 my $remote= "localhost";
70 my $iaddr= inet_aton
($remote);
73 mtr_error
("can't find IP number for $remote");
75 my $paddr= sockaddr_in
($port, $iaddr);
76 my $proto= getprotobyname('tcp');
77 if ( ! socket(SOCK
, PF_INET
, SOCK_STREAM
, $proto) )
79 mtr_error
("can't create socket: $!");
82 mtr_debug
("Pinging server (port: $port)...");
84 if ( connect(SOCK
, $paddr) )
86 close(SOCK
); # FIXME check error?
97 ##############################################################################
99 # Wait for a file to be created
101 ##############################################################################
103 # FIXME check that the pidfile contains the expected pid!
105 sub sleep_until_file_created
($$$) {
109 my $sleeptime= 100; # Milliseconds
110 my $loops= ($timeout * 1000) / $sleeptime;
112 for ( my $loop= 1; $loop <= $loops; $loop++ )
119 # Check if it died after the fork() was successful
120 if ( defined $proc and ! $proc->wait_one(0) )
122 mtr_warning
("Process $proc died");
126 mtr_debug
("Sleep $sleeptime milliseconds waiting for $pidfile");
128 # Print extra message every 60 seconds
129 my $seconds= ($loop * $sleeptime) / 1000;
130 if ( $seconds > 1 and int($seconds * 10) % 600 == 0 )
132 my $left= $timeout - $seconds;
133 mtr_warning
("Waited $seconds seconds for $pidfile to be created, " .
134 "still waiting for $left seconds...");
137 mtr_milli_sleep
($sleeptime);