mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / mysql-test / lib / mtr_process.pl
blob3c8a938ae547b4817aa452ac74e210831964ac1c
1 # -*- cperl -*-
2 # Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
3 #
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.
7 #
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
19 # same name.
21 use strict;
22 use Socket;
23 use Errno;
24 use My::Platform;
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.
29 BEGIN
31 my $use_netping= 0;
32 if (IS_WINDOWS)
34 my $ping = Net::Ping->new();
35 if ($ping->can("port_number"))
37 $use_netping= 1;
40 eval 'sub USE_NETPING { $use_netping }';
43 sub sleep_until_file_created ($$$);
44 sub mtr_ping_port ($);
46 sub mtr_ping_port ($) {
47 my $port= shift;
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))
59 mtr_verbose("USED");
60 return 1;
62 else
64 mtr_verbose("FREE");
65 return 0;
69 my $remote= "localhost";
70 my $iaddr= inet_aton($remote);
71 if ( ! $iaddr )
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?
87 mtr_verbose("USED");
88 return 1;
90 else
92 mtr_verbose("FREE");
93 return 0;
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 ($$$) {
106 my $pidfile= shift;
107 my $timeout= shift;
108 my $proc= shift;
109 my $sleeptime= 100; # Milliseconds
110 my $loops= ($timeout * 1000) / $sleeptime;
112 for ( my $loop= 1; $loop <= $loops; $loop++ )
114 if ( -r $pidfile )
116 return 1;
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");
123 return 0;
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);
141 return 0;