2008-02-20 14:55:30 +01:00
|
|
|
# -*- cperl -*-
|
2011-06-30 17:37:13 +02:00
|
|
|
# Copyright (c) 2008 MySQL AB, 2008, 2009 Sun Microsystems, Inc.
|
|
|
|
# Use is subject to license terms.
|
2008-02-20 14:55:30 +01:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; version 2 of the License.
|
|
|
|
#
|
|
|
|
# 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 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
|
2019-05-11 20:29:06 +02:00
|
|
|
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA
|
2008-02-20 14:55:30 +01:00
|
|
|
|
|
|
|
package My::Platform;
|
|
|
|
|
|
|
|
use strict;
|
2008-09-20 14:21:29 +02:00
|
|
|
use File::Basename;
|
2008-10-08 22:43:37 +02:00
|
|
|
use File::Path;
|
2023-08-09 19:11:06 +02:00
|
|
|
use Carp;
|
2008-02-20 14:55:30 +01:00
|
|
|
|
|
|
|
use base qw(Exporter);
|
2024-05-15 15:50:11 +02:00
|
|
|
our @EXPORT= qw(IS_CYGWIN IS_MSYS IS_WINDOWS IS_WIN32PERL IS_AIX IS_MAC IS_FREEBSD
|
2008-04-21 18:32:32 +02:00
|
|
|
native_path posix_path mixed_path
|
2016-09-27 14:34:15 +02:00
|
|
|
check_socket_path_length process_alive open_for_append);
|
2008-02-20 14:55:30 +01:00
|
|
|
|
|
|
|
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 }';
|
2023-08-08 01:14:55 +02:00
|
|
|
eval 'sub IS_MSYS { 0 }';
|
|
|
|
}
|
|
|
|
elsif ($^O eq "msys") {
|
|
|
|
eval 'sub IS_CYGWIN { 1 }';
|
|
|
|
eval 'sub IS_MSYS { 1 }';
|
2008-02-20 14:55:30 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
eval 'sub IS_CYGWIN { 0 }';
|
2023-08-08 01:14:55 +02:00
|
|
|
eval 'sub IS_MSYS { 0 }';
|
2008-02-20 14:55:30 +01:00
|
|
|
}
|
|
|
|
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 }';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-11 09:13:19 +02:00
|
|
|
BEGIN {
|
|
|
|
if ($^O eq "aix") {
|
|
|
|
eval 'sub IS_AIX { 1 }';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
eval 'sub IS_AIX { 0 }';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-02 17:44:02 +02:00
|
|
|
BEGIN {
|
|
|
|
if ($^O eq "darwin") {
|
|
|
|
eval 'sub IS_MAC { 1 }';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
eval 'sub IS_MAC { 0 }';
|
|
|
|
}
|
|
|
|
}
|
2008-02-20 14:55:30 +01:00
|
|
|
|
2024-05-15 15:50:11 +02:00
|
|
|
BEGIN {
|
|
|
|
if ($^O eq "freebsd") {
|
|
|
|
eval 'sub IS_FREEBSD { 1 }';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
eval 'sub IS_FREEBSD { 0 }';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-20 14:55:30 +01:00
|
|
|
#
|
|
|
|
# native_path
|
|
|
|
# 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)
|
|
|
|
#
|
|
|
|
|
2008-09-22 18:15:55 +02:00
|
|
|
use Memoize;
|
2008-10-08 20:25:28 +02:00
|
|
|
if (!IS_WIN32PERL){
|
|
|
|
memoize('mixed_path');
|
|
|
|
memoize('native_path');
|
|
|
|
memoize('posix_path');
|
|
|
|
}
|
2008-09-22 18:15:55 +02:00
|
|
|
|
2008-02-20 14:55:30 +01:00
|
|
|
sub mixed_path {
|
|
|
|
my ($path)= @_;
|
|
|
|
if (IS_CYGWIN){
|
|
|
|
return unless defined $path;
|
2008-08-11 10:41:23 +02:00
|
|
|
my $cmd= "cygpath -m $path";
|
2008-09-22 17:44:35 +02:00
|
|
|
$path= `$cmd` or
|
|
|
|
print "Failed to run: '$cmd', $!\n";
|
2008-02-20 14:55:30 +01:00
|
|
|
chomp $path;
|
|
|
|
}
|
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub native_path {
|
|
|
|
my ($path)= @_;
|
2023-08-07 21:33:58 +02:00
|
|
|
if (IS_CYGWIN) {
|
|
|
|
# \\\\ protects against 2 expansions (just for the case)
|
|
|
|
$path=~ s/\/+|\\+/\\\\\\\\/g;
|
|
|
|
}
|
|
|
|
elsif (IS_WINDOWS) {
|
|
|
|
$path=~ s/\/+/\\/g;
|
|
|
|
}
|
2008-02-20 14:55:30 +01:00
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub posix_path {
|
|
|
|
my ($path)= @_;
|
|
|
|
if (IS_CYGWIN){
|
|
|
|
return unless defined $path;
|
|
|
|
$path= `cygpath $path`;
|
|
|
|
chomp $path;
|
|
|
|
}
|
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
|
2009-01-21 18:18:03 +01:00
|
|
|
use File::Temp qw /tempdir/;
|
2008-02-20 14:55:30 +01:00
|
|
|
|
2008-04-21 18:32:32 +02:00
|
|
|
sub check_socket_path_length {
|
|
|
|
my ($path)= @_;
|
|
|
|
|
2008-04-22 21:55:09 +02:00
|
|
|
return 0 if IS_WINDOWS;
|
2009-08-06 09:30:53 +02:00
|
|
|
# This may not be true, but we can't test for it on AIX due to Perl bug
|
|
|
|
# See Bug #45771
|
|
|
|
return 0 if ($^O eq 'aix');
|
2014-10-02 12:57:20 +02:00
|
|
|
# See Debian bug #670722 - failing on kFreeBSD even after setting short path
|
|
|
|
return 0 if $^O eq 'gnukfreebsd' and length $path < 40;
|
2018-06-29 11:01:55 +02:00
|
|
|
# GNU/Hurd doesn't have hostpath(), but no limitation either
|
|
|
|
return 0 if $^O eq 'gnu';
|
2008-04-22 21:55:09 +02:00
|
|
|
|
2008-04-22 12:43:38 +02:00
|
|
|
require IO::Socket::UNIX;
|
|
|
|
|
2009-08-06 09:30:53 +02:00
|
|
|
my $truncated= undef;
|
2009-01-21 18:18:03 +01:00
|
|
|
|
|
|
|
# Create a tempfile name with same length as "path"
|
|
|
|
my $tmpdir = tempdir( CLEANUP => 0);
|
2009-02-25 10:32:13 +01:00
|
|
|
my $len = length($path) - length($tmpdir) - 1;
|
|
|
|
my $testfile = $tmpdir . "/" . "x" x ($len > 0 ? $len : 1);
|
2008-09-15 16:27:12 +02:00
|
|
|
my $sock;
|
|
|
|
eval {
|
|
|
|
$sock= new IO::Socket::UNIX
|
|
|
|
(
|
2009-01-21 18:18:03 +01:00
|
|
|
Local => $testfile,
|
2008-09-15 16:27:12 +02:00
|
|
|
Listen => 1,
|
|
|
|
);
|
2009-08-06 09:30:53 +02:00
|
|
|
$truncated= 1; # Be negatvie
|
2008-09-20 14:21:29 +02:00
|
|
|
|
2009-01-21 18:18:03 +01:00
|
|
|
die "Could not create UNIX domain socket: $!"
|
|
|
|
unless defined $sock;
|
|
|
|
|
2009-02-25 10:32:13 +01:00
|
|
|
die "UNIX domain socket path was truncated"
|
2009-08-06 09:30:53 +02:00
|
|
|
unless ($testfile eq $sock->hostpath());
|
2009-01-21 18:18:03 +01:00
|
|
|
|
|
|
|
$truncated= 0; # Yes, it worked!
|
|
|
|
|
2008-09-15 16:27:12 +02:00
|
|
|
};
|
2009-01-21 18:18:03 +01:00
|
|
|
|
2009-08-05 09:41:40 +02:00
|
|
|
die "Unexpected failure when checking socket path length: $@"
|
2009-08-06 09:30:53 +02:00
|
|
|
if $@ and not defined $truncated;
|
2009-08-05 09:41:40 +02:00
|
|
|
|
2009-01-21 18:18:03 +01:00
|
|
|
$sock= undef; # Close socket
|
2009-02-25 10:32:13 +01:00
|
|
|
rmtree($tmpdir); # Remove the tempdir and any socket file created
|
2008-04-21 18:32:32 +02:00
|
|
|
return $truncated;
|
|
|
|
}
|
|
|
|
|
2008-02-20 14:55:30 +01:00
|
|
|
|
2008-08-10 19:46:43 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-27 14:34:15 +02:00
|
|
|
|
|
|
|
use Symbol qw( gensym );
|
|
|
|
|
|
|
|
use if $^O eq 'MSWin32', 'Win32API::File', qw( CloseHandle CreateFile GetOsFHandle OsFHandleOpen OPEN_ALWAYS FILE_APPEND_DATA
|
|
|
|
FILE_SHARE_READ FILE_SHARE_WRITE FILE_SHARE_DELETE );
|
|
|
|
use if $^O eq 'MSWin32', 'Win32::API';
|
|
|
|
|
|
|
|
use constant WIN32API_FILE_NULL => [];
|
|
|
|
|
|
|
|
# Open a file for append
|
|
|
|
# On Windows we use CreateFile with FILE_APPEND_DATA
|
|
|
|
# to insure that writes are atomic, not interleaved
|
|
|
|
# with writes by another processes.
|
|
|
|
sub open_for_append
|
|
|
|
{
|
|
|
|
my ($file) = @_;
|
|
|
|
my $fh = gensym();
|
|
|
|
|
|
|
|
if (IS_WIN32PERL)
|
|
|
|
{
|
|
|
|
my $handle;
|
|
|
|
if (!($handle = CreateFile(
|
|
|
|
$file,
|
|
|
|
FILE_APPEND_DATA(),
|
|
|
|
FILE_SHARE_READ()|FILE_SHARE_WRITE()|FILE_SHARE_DELETE(),
|
|
|
|
WIN32API_FILE_NULL,
|
|
|
|
OPEN_ALWAYS(),# Create if doesn't exist.
|
|
|
|
0,
|
|
|
|
WIN32API_FILE_NULL,
|
|
|
|
)))
|
|
|
|
{
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!OsFHandleOpen($fh, $handle, 'wat'))
|
|
|
|
{
|
|
|
|
CloseHandle($handle);
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
return $fh;
|
|
|
|
}
|
|
|
|
|
|
|
|
open($fh,">>",$file) or return undef;
|
|
|
|
return $fh;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-08-09 19:11:06 +02:00
|
|
|
sub check_cygwin_subshell
|
|
|
|
{
|
|
|
|
# Only pipe (or sh-expansion) is fed to /bin/sh
|
|
|
|
my $out= `echo %comspec%|cat`;
|
|
|
|
return ($out =~ /\bcmd.exe\b/) ? 0 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub install_shell_wrapper()
|
|
|
|
{
|
|
|
|
system("rm -f /bin/sh.exe") and die $!;
|
|
|
|
my $wrapper= <<'EOF';
|
|
|
|
#!/bin/bash
|
|
|
|
if [[ -n "$MTR_PERL" && "$1" = "-c" ]]; then
|
|
|
|
shift
|
|
|
|
exec $(cygpath -m "$COMSPEC") /C "$@"
|
|
|
|
fi
|
|
|
|
exec /bin/bash "$@"
|
|
|
|
EOF
|
|
|
|
open(OUT, '>', "/bin/sh") or die "/bin/sh: $!\n";
|
|
|
|
print OUT $wrapper;
|
|
|
|
close(OUT);
|
|
|
|
system("chmod +x /bin/sh") and die $!;
|
|
|
|
print "Cygwin subshell wrapper /bin/sh was installed, please restart MTR!\n";
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub uninstall_shell_wrapper()
|
|
|
|
{
|
|
|
|
system("rm -f /bin/sh") and die $!;
|
|
|
|
system("cp /bin/bash.exe /bin/sh.exe") and die $!;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub cygwin_subshell_fix
|
|
|
|
{
|
|
|
|
my ($opt_name, $opt_value)= @_;
|
|
|
|
if ($opt_name ne "cygwin-subshell-fix") {
|
|
|
|
confess "Wrong option name: ${opt_name}";
|
|
|
|
}
|
|
|
|
if ($opt_value eq "do") {
|
|
|
|
if (check_cygwin_subshell()) {
|
|
|
|
install_shell_wrapper();
|
|
|
|
} else {
|
|
|
|
print "Cygwin subshell fix was already installed, skipping...\n";
|
|
|
|
}
|
|
|
|
} elsif ($opt_value eq "remove") {
|
|
|
|
if (check_cygwin_subshell()) {
|
|
|
|
print "Cygwin subshell fix was already uninstalled, skipping...\n";
|
|
|
|
} else {
|
|
|
|
uninstall_shell_wrapper();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
die "Wrong --cygwin-subshell-fix value: ${opt_value} (expected do/remove)";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub options
|
|
|
|
{
|
|
|
|
if (IS_CYGWIN) {
|
|
|
|
return ('cygwin-subshell-fix=s' => \&cygwin_subshell_fix);
|
|
|
|
} else {
|
|
|
|
return ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-20 14:55:30 +01:00
|
|
|
1;
|