2
# Copyright (C) 2006 MySQL AB
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
22
use base qw(Exporter);
23
our @EXPORT= qw(mtr_get_unique_id mtr_release_unique_id);
28
# print "### unique($$) - ", join(" ", @_), "\n";
35
$file= "/tmp/mysql-test-ports";
39
$file= $ENV{'TEMP'}."/mysql-test-ports";
46
my $allocated_id= $mtr_unique_ids{$$};
47
if (defined $allocated_id)
49
mtr_release_unique_id($allocated_id);
51
delete $mtr_unique_ids{$$};
55
# Get a unique, numerical ID, given a file name (where all
56
# requested IDs are stored), a minimum and a maximum value.
58
# If no unique ID within the specified parameters can be
59
# obtained, return undef.
61
sub mtr_get_unique_id($$) {
64
msg("get, '$file', $min-$max");
66
die "Can only get one unique id per process!" if $mtr_unique_ids{$$};
71
if(eval("readlink '$file'") || eval("readlink '$file.sem'")) {
72
die 'lock file is a symbolic link';
75
chmod 0777, "$file.sem";
76
open SEM, ">", "$file.sem" or die "can't write to $file.sem";
77
flock SEM, LOCK_EX or die "can't lock $file.sem";
79
open FILE, ">", $file or die "can't create $file";
85
if(eval("readlink '$file'") || eval("readlink '$file.sem'")) {
86
die 'lock file is a symbolic link';
90
open FILE, "+<", $file or die "can't open $file";
91
#select undef,undef,undef,0.2;
96
my ($id, $pid) = split / /;
98
msg("taken: $id, $pid");
99
# Check if process with given pid is alive
100
if(!process_alive($pid)) {
101
print "Removing slot $id used by missing process $pid\n";
102
msg("Removing slot $id used by missing process $pid");
107
for(my $i=$min; $i<=$max; ++$i) {
108
if(! exists $taken{$i}) {
112
# Remember the id this process got
113
$mtr_unique_ids{$$}= $i;
120
truncate FILE, 0 or die "can't truncate $file";
121
for my $k (keys %taken) {
122
print FILE $k . ' ' . $taken{$k} . "\n";
127
msg("RELEASING THE LOCK");
128
flock SEM, LOCK_UN or warn "can't unlock $file.sem";
136
# Release a unique ID.
138
sub mtr_release_unique_id($) {
141
msg("release, $myid");
144
if(eval("readlink '$file'") || eval("readlink '$file.sem'")) {
145
die 'lock file is a symbolic link';
148
open SEM, ">", "$file.sem" or die "can't write to $file.sem";
149
flock SEM, LOCK_EX or die "can't lock $file.sem";
151
msg("HAVE THE LOCK");
153
if(eval("readlink '$file'") || eval("readlink '$file.sem'")) {
154
die 'lock file is a symbolic link';
158
open FILE, ">", $file or die "can't create $file";
161
open FILE, "+<", $file or die "can't open $file";
162
#select undef,undef,undef,0.2;
167
my ($id, $pid) = split / /;
168
msg(" taken, $id $pid");
172
if ($taken{$myid} != $$)
174
msg(" The unique id for this process does not match pid");
178
msg(" removing $myid");
179
delete $taken{$myid};
181
truncate FILE, 0 or die "can't truncate $file";
182
for my $k (keys %taken) {
183
print FILE $k . ' ' . $taken{$k} . "\n";
187
msg("RELEASE THE LOCK");
189
flock SEM, LOCK_UN or warn "can't unlock $file.sem";
192
delete $mtr_unique_ids{$$};