mtr_unique.pl 3.64 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#
# This file is used from mysql-test-run.pl when choosing
# port numbers and directories to use for running mysqld.
#

use strict;
use Fcntl ':flock';

#
# Requested IDs are stored in a hash and released upon END.
#
my %mtr_unique_assigned_ids = ();
END { 
	while(my ($id,$file) = each(%mtr_unique_assigned_ids)) {
		print "Autoreleasing $file:$id\n";
		mtr_release_unique_id($file, $id);
	}
}

#
# Require a unique, numerical ID, given a file name (where all
# requested IDs are stored), a minimum and a maximum value.
#
# We use flock to implement locking for the ID file and ignore
# possible problems arising from lack of support for it on 
# some platforms (it should work on most, and the possible
# race condition would occur rarely). The proper solution for
# this is a daemon that manages IDs, of course.
#
# If no unique ID within the specified parameters can be 
# obtained, return undef.
#
sub mtr_require_unique_id($$$) {
	my $file = shift;
	my $min = shift;
	my $max = shift;
	my $ret = undef;
unknown's avatar
unknown committed
38 39 40 41 42 43 44
	my $changed = 0;

	my $can_use_ps = `ps -e | grep '^[ ]*$$ '`;

	if(eval("readlink '$file'") || eval("readlink '$file.sem'")) {
		die 'lock file is a symbolic link';
	}
45 46 47 48 49 50 51 52

	chmod 0777, "$file.sem";
	open SEM, ">", "$file.sem" or die "can't write to $file.sem";
	flock SEM, LOCK_EX or die "can't lock $file.sem";
	if(! -e $file) {
		open FILE, ">", $file or die "can't create $file";
		close FILE;
	}
unknown's avatar
unknown committed
53 54 55 56 57

	if(eval("readlink '$file'") || eval("readlink '$file.sem'")) {
		die 'lock file is a symbolic link';
	}

58 59 60 61 62 63 64 65 66
	chmod 0777, $file;
	open FILE, "+<", $file or die "can't open $file";
	select undef,undef,undef,0.2;
	seek FILE, 0, 0;
	my %taken = ();
	while(<FILE>) {
		chomp;
		my ($id, $pid) = split / /;
		$taken{$id} = $pid;
unknown's avatar
unknown committed
67 68 69 70 71 72 73 74
		if($can_use_ps) {
			my $res = `ps -e | grep '^[ ]*$pid '`;
			if(!$res) {
				print "Ignoring slot $id used by missing process $pid.\n";
				delete $taken{$id};
				++$changed;
			}
		}
75 76 77 78
	}
	for(my $i=$min; $i<=$max; ++$i) {
		if(! exists $taken{$i}) {
			$ret = $i;
unknown's avatar
unknown committed
79 80
			$taken{$i} = $$;
			++$changed;
81 82 83
			last;
		}
	}
unknown's avatar
unknown committed
84 85 86 87 88 89 90
	if($changed) {
		seek FILE, 0, 0;
		truncate FILE, 0 or die "can't truncate $file";
		for my $k (keys %taken) {
			print FILE $k . ' ' . $taken{$k} . "\n";
		}
	}
91 92 93 94 95 96 97 98 99 100 101 102 103 104
	close FILE;
	flock SEM, LOCK_UN or warn "can't unlock $file.sem";
	close SEM;
	$mtr_unique_assigned_ids{$ret} = $file if defined $ret;
	return $ret;
}

#
# Require a unique ID like above, but sleep if no ID can be
# obtained immediately.
#
sub mtr_require_unique_id_and_wait($$$) {
	my $ret = mtr_require_unique_id($_[0],$_[1],$_[2]);
	while(! defined $ret) {
unknown's avatar
unknown committed
105
		sleep 30;
106
		$ret = mtr_require_unique_id($_[0],$_[1],$_[2]);
unknown's avatar
unknown committed
107
		print "Waiting for unique id to become available...\n" unless $ret;
108 109 110 111 112 113 114 115 116 117 118
	}
	return $ret;
}

#
# Release a unique ID.
#
sub mtr_release_unique_id($$) {
	my $file = shift;
	my $myid = shift;

unknown's avatar
unknown committed
119 120 121 122
	if(eval("readlink '$file'") || eval("readlink '$file.sem'")) {
		die 'lock file is a symbolic link';
	}

123 124
	open SEM, ">", "$file.sem" or die "can't write to $file.sem";
	flock SEM, LOCK_EX or die "can't lock $file.sem";
unknown's avatar
unknown committed
125 126 127 128 129

	if(eval("readlink '$file'") || eval("readlink '$file.sem'")) {
		die 'lock file is a symbolic link';
	}

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
	if(! -e $file) {
		open FILE, ">", $file or die "can't create $file";
		close FILE;
	}
	open FILE, "+<", $file or die "can't open $file";
	select undef,undef,undef,0.2;
	seek FILE, 0, 0;
	my %taken = ();
	while(<FILE>) {
		chomp;
		my ($id, $pid) = split / /;
		$taken{$id} = $pid;
	}
	delete $taken{$myid};
	seek FILE, 0, 0;
	truncate FILE, 0 or die "can't truncate $file";
	for my $k (keys %taken) {
		print FILE $k . ' ' . $taken{$k} . "\n";
	}
	close FILE;
	flock SEM, LOCK_UN or warn "can't unlock $file.sem";
	close SEM;
	delete $mtr_unique_assigned_ids{$myid};
}

1;