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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
# -*- cperl -*-
# Copyright (c) 2008, 2025, Oracle and/or its affiliates.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.0,
# as published by the Free Software Foundation.
#
# This program is designed to work with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation. The authors of MySQL hereby grant you an additional
# permission to link the program and your derivative works with the
# separately licensed software that they have either included with
# the program or referenced in the documentation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License, version 2.0, for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
package My::Platform;
use strict;
use File::Basename;
use File::Path;
use File::Temp qw /tempdir/;
use base qw(Exporter);
our @EXPORT = qw(IS_CYGWIN IS_MAC IS_WINDOWS IS_WIN32PERL
check_socket_path_length mixed_path native_path
posix_path process_alive);
BEGIN {
if ($^O eq "cygwin") {
# Make sure cygpath works
if ((system("cygpath > /dev/null 2>&1") >> 8) != 1) {
die "Could not execute 'cygpath': $!";
}
eval 'sub IS_CYGWIN { 1 }';
} else {
eval 'sub IS_CYGWIN { 0 }';
}
if ($^O eq "MSWin32") {
eval 'sub IS_WIN32PERL { 1 }';
} else {
eval 'sub IS_WIN32PERL { 0 }';
}
}
BEGIN {
if (IS_CYGWIN or IS_WIN32PERL) {
eval 'sub IS_WINDOWS { 1 }';
} else {
eval 'sub IS_WINDOWS { 0 }';
}
}
BEGIN {
if ($^O eq "darwin") {
eval 'sub IS_MAC { 1 }';
} else {
eval 'sub IS_MAC { 0 }';
}
}
# Convert from path format used by perl to the underlying
# operating systems format.
#
# NOTE
# Used when running windows binaries (that expect windows paths)
# in cygwin perl (that uses unix paths).
use Memoize;
if (!IS_WIN32PERL) {
memoize('mixed_path');
memoize('native_path');
memoize('posix_path');
}
sub mixed_path {
my ($path) = @_;
if (IS_CYGWIN) {
return unless defined $path;
my $cmd = "cygpath -m $path";
$path = `$cmd` or
print "Failed to run: '$cmd', $!\n";
chomp $path;
}
return $path;
}
sub native_path {
my ($path) = @_;
$path =~ s/\//\\/g
if (IS_CYGWIN or IS_WIN32PERL);
return $path;
}
sub posix_path {
my ($path) = @_;
if (IS_CYGWIN) {
return unless defined $path;
$path = `cygpath $path`;
chomp $path;
}
return $path;
}
sub check_socket_path_length {
my ($path, $parallel, $tmpdir_path_updated) = @_;
return 0 if IS_WINDOWS;
# See Debian bug #651002
return 0 if ($^O eq 'gnu');
# See Debian bug #670722 - failing on kFreeBSD even after setting short path
return 0 if length $path < 40;
require IO::Socket::UNIX;
my $truncated = undef;
# Append extra chars if --parallel because $opt_tmpdir will be longer
if (! $tmpdir_path_updated) {
if ($parallel > 9 || $parallel eq "auto") {
$path = $path . "xxx";
} elsif ($parallel > 1) {
$path = $path . "xx";
}
}
# Create a tempfile name with same length as "path"
my $tmpdir = tempdir(CLEANUP => 0);
my $len = length($path) - length($tmpdir) - 1;
my $testfile = $tmpdir . "/" . "x" x ($len > 0 ? $len : 1);
my $sock;
eval {
$sock = new IO::Socket::UNIX(Local => $testfile,
Listen => 1,);
# Be negative
$truncated = 1;
die "Could not create UNIX domain socket: $!"
unless defined $sock;
die "UNIX domain socket path was truncated"
unless ($testfile eq $sock->hostpath());
$truncated = 0; # Yes, it worked!
};
die "Unexpected failure when checking socket path length: $@"
if $@ and
not defined $truncated;
# Close the socket
$sock = undef;
# Remove the tempdir and any socket file created
rmtree($tmpdir);
return $truncated;
}
sub process_alive {
my ($pid) = @_;
die "usage: process_alive(pid)" unless $pid;
return kill(0, $pid) unless IS_WINDOWS;
my @list = split(/,/, `tasklist /FI "PID eq $pid" /NH /FO CSV`);
my $ret_pid = eval($list[1]);
return ($ret_pid == $pid);
}
1;
|