2
# Copyright (c) 2006 MySQL AB, 2008 Sun Microsystems, Inc.
3
# Use is subject to license terms.
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; version 2 of the License.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
# This file is used from mysql-test-run.pl when choosing
20
# port numbers and directories to use for running mysqld.
27
# Requested IDs are stored in a hash and released upon END.
29
my %mtr_unique_assigned_ids = ();
32
$mtr_unique_pid = $$ unless defined $mtr_unique_pid;
35
if($mtr_unique_pid == $$) {
36
while(my ($id,$file) = each(%mtr_unique_assigned_ids)) {
37
print "Autoreleasing $file:$id\n";
38
mtr_release_unique_id($file, $id);
44
# Require a unique, numerical ID, given a file name (where all
45
# requested IDs are stored), a minimum and a maximum value.
47
# We use flock to implement locking for the ID file and ignore
48
# possible problems arising from lack of support for it on
49
# some platforms (it should work on most, and the possible
50
# race condition would occur rarely). The proper solution for
51
# this is a daemon that manages IDs, of course.
53
# If no unique ID within the specified parameters can be
54
# obtained, return undef.
56
sub mtr_require_unique_id($$$) {
63
my $can_use_ps = `ps -e | grep '^[ ]*$$ '`;
65
if(eval("readlink '$file'") || eval("readlink '$file.sem'")) {
66
die 'lock file is a symbolic link';
69
chmod 0777, "$file.sem";
70
open SEM, ">", "$file.sem" or die "can't write to $file.sem";
71
flock SEM, LOCK_EX or die "can't lock $file.sem";
73
open FILE, ">", $file or die "can't create $file";
77
if(eval("readlink '$file'") || eval("readlink '$file.sem'")) {
78
die 'lock file is a symbolic link';
82
open FILE, "+<", $file or die "can't open $file";
83
select undef,undef,undef,0.2;
88
my ($id, $pid) = split / /;
91
my $res = `ps -e | grep '^[ ]*$pid '`;
93
print "Ignoring slot $id used by missing process $pid.\n";
99
for(my $i=$min; $i<=$max; ++$i) {
100
if(! exists $taken{$i}) {
109
truncate FILE, 0 or die "can't truncate $file";
110
for my $k (keys %taken) {
111
print FILE $k . ' ' . $taken{$k} . "\n";
115
flock SEM, LOCK_UN or warn "can't unlock $file.sem";
117
$mtr_unique_assigned_ids{$ret} = $file if defined $ret;
122
# Require a unique ID like above, but sleep if no ID can be
123
# obtained immediately.
125
sub mtr_require_unique_id_and_wait($$$) {
126
my $ret = mtr_require_unique_id($_[0],$_[1],$_[2]);
127
while(! defined $ret) {
129
$ret = mtr_require_unique_id($_[0],$_[1],$_[2]);
130
print "Waiting for unique id to become available...\n" unless $ret;
136
# Release a unique ID.
138
sub mtr_release_unique_id($$) {
142
if(eval("readlink '$file'") || eval("readlink '$file.sem'")) {
143
die 'lock file is a symbolic link';
146
open SEM, ">", "$file.sem" or die "can't write to $file.sem";
147
flock SEM, LOCK_EX or die "can't lock $file.sem";
149
if(eval("readlink '$file'") || eval("readlink '$file.sem'")) {
150
die 'lock file is a symbolic link';
154
open FILE, ">", $file or die "can't create $file";
157
open FILE, "+<", $file or die "can't open $file";
158
select undef,undef,undef,0.2;
163
my ($id, $pid) = split / /;
166
delete $taken{$myid};
168
truncate FILE, 0 or die "can't truncate $file";
169
for my $k (keys %taken) {
170
print FILE $k . ' ' . $taken{$k} . "\n";
173
flock SEM, LOCK_UN or warn "can't unlock $file.sem";
175
delete $mtr_unique_assigned_ids{$myid};