Developer-Dashboard

 view release on metacpan or  search on metacpan

lib/Developer/Dashboard/ProcessSupervision.pm  view on Meta::CPAN

    open my $source_fh, '<', $source or return ( 0, "Unable to read $source for in-place overwrite: $!" );
    local $/;
    my $content = <$source_fh>;
    close $source_fh;
    open my $target_fh, '>', $target or return ( 0, "Unable to open $target for in-place overwrite: $!" );
    print {$target_fh} $content
      or return ( 0, "Unable to write $target during in-place overwrite: $!" );
    close $target_fh;
    $self->_unlink_path($source);
    return ( 1, '' );
}

# _open_file_descriptors()
# Lists the current process file-descriptor numbers from procfs or /dev/fd so
# detached runtime children can close inherited caller pipes safely.
# Input: none.
# Output: sorted list of descriptor integers.
sub _open_file_descriptors {
    my ($self) = @_;
    if ( is_windows() ) {
        # There is no /proc or /dev/fd on Windows, so there is no
        # directory to list descriptors from. Probe the CRT descriptor
        # table instead: a descriptor that duplicates is open, one that
        # does not is not. The bound is the common CRT table size; a
        # detached child only ever inherits low-numbered descriptors,
        # and the standard three are always found this way, so the
        # listing still includes stdin.
        my @fds;
        for my $fd ( 0 .. 1023 ) {
            my $handle;
            if ( open( $handle, '<&', $fd ) || open( $handle, '>&', $fd ) ) {
                push @fds, $fd;
                close $handle;
            }
        }
        return @fds;
    }
    my %seen;
    my @fds;
    for my $path ( Developer::Dashboard::ProcessSupervision::_fd_candidate_paths($self) ) {
        my ($fd) = $path =~ m{(\d+)\z};
        $fd += 0;
        next if $seen{$fd}++;
        push @fds, $fd;
    }
    return sort { $a <=> $b } @fds;
}

# _fd_candidate_paths()
# Returns procfs and devfs descriptor paths; isolated as a method so tests can
# supply duplicated directory entries on hosts without both descriptor trees.
# Input: none.
# Output: list of descriptor-path strings.
sub _fd_candidate_paths {
    my ($self) = @_;
    return ( glob('/proc/self/fd/*'), glob('/dev/fd/*') );
}

# _descriptor_is_inherited_pipe($fd)
# Returns whether one descriptor currently points at an inherited capture or
# IPC endpoint that a detached runtime child should close after stdio has been
# redirected.
# Input: descriptor integer.
# Output: boolean true when the descriptor target is an inherited pipe,
# socketpair, or anonymous kernel handle.
sub _descriptor_is_inherited_pipe {
    my ( $self, $fd, %args ) = @_;
    return 0 if !defined $fd || $fd !~ /^\d+$/;
    my $handle;
    open( $handle, '<&', $fd ) or open( $handle, '>&', $fd ) or return 0;
    my $pipe   = -p $handle ? 1 : 0;
    my $socket = -S $handle ? 1 : 0;
    close $handle;
    return 1 if $pipe;
    return 0 if !$args{close_ipc};
    return 1 if $socket;
    my $target = _fd_link_target($fd);
    return defined($target) && $target =~ /^anon_inode:/ ? 1 : 0;
}

# Linux anonymous kernel handles have no ordinary socket or pipe file type.
# Keep their procfs identity check while using portable fstat for other FDs.
sub _fd_link_target {
    my ($fd) = @_;
    my $target = readlink("/proc/self/fd/$fd");
    return defined $target ? $target : readlink("/dev/fd/$fd");
}

# _current_perl_command()
# Resolves a runnable Perl interpreter path for detached helper launches,
# including Windows sessions where $^X can point at a nonexistent local::lib
# shim path.
# Input: none.
# Output: executable path string for the current Perl interpreter.
sub _current_perl_command {
    my ($self) = @_;
    if ($self->is_windows) {
        return command_in_path('perl')     if command_in_path('perl');
        return command_in_path('perl.exe') if command_in_path('perl.exe');
    }
    return $^X if defined $^X && $^X ne '' && -f $^X;
    return command_in_path('perl')     if command_in_path('perl');
    return command_in_path('perl.exe') if command_in_path('perl.exe');
    return $^X;
}

# _perl_library_args()
# Builds literal Perl -I arguments for string-valued entries in the current
# interpreter's @INC, preserving its active module search path in native child
# processes that cannot inherit in-memory use lib changes. Relative entries are
# made absolute against the directory that loaded this helper. Every path is
# canonicalised (File::Spec->canonpath): on Windows @INC routinely mixes
# separators, because Strawberry Perl and local::lib PERL5LIB entries use
# forward slashes (e.g. C:\...\try\lib\perl5/MSWin32-x64-multi-thread), while
# the spawned child must receive the native form the dashboard's own path
# comparisons and the t/236 contract expect.
# Input: none; reads the current interpreter's @INC.
# Output: list of -I<path> argument strings; reference hooks are omitted because
#         they are not filesystem paths and cannot be passed to another Perl.
sub _perl_library_args {
    my ($self) = @_;



( run in 1.859 second using v1.01-cache-2.11-cpan-eb134014566 )