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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
package Proc::Background::Win32;
$Proc::Background::Win32::VERSION = '1.32';
# ABSTRACT: Windows-specific implementation of process create/wait/kill
require 5.004_04;
use strict;
use Exporter;
use Carp;
use Win32;
use Win32::Process qw( NORMAL_PRIORITY_CLASS INFINITE );
use Win32::ShellQuote ();
@Proc::Background::Win32::ISA = qw(Exporter);
sub _start {
my ($self, $options)= @_;
my ($cmd, $exe, $cmdline, $err)= @{$self}{'_command','_exe'};
# If 'command' is a single string, treat it as system() would and assume
# it should be split into arguments. The first argument is then the
# application executable, if not already specified as an option.
if (ref $cmd ne 'ARRAY') {
$cmdline= $cmd;
($exe) = Win32::ShellQuote::unquote_native($cmdline)
unless exists $options->{exe};
}
# system() would treat a list of arguments as an un-quoted ARGV
# for the program, so concatenate them into a command line appropriate
# for Win32 CommandLineToArgvW to decode back to what we started with.
# Preserve the first un-quoted argument for use as lpApplicationName,
# unless user requested some value (including undef).
else {
$exe = $cmd->[0] unless exists $options->{exe};
$cmdline= Win32::ShellQuote::quote_native(@$cmd);
}
if (defined $exe) {
# Find the absolute path to the program. If it cannot be found,
# then return.
($exe, $err) = Proc::Background::_resolve_path($exe);
return $self->_fatal($err) unless defined $exe;
# To work around a problem where Win32::Process::Create cannot start a
# process when the full pathname has a space in it, convert the full
# pathname to the Windows short 8.3 format which contains no spaces.
$exe = Win32::GetShortPathName($exe)
}
else {
Win32::Process->VERSION > 0.16
or croak "{exe => undef} feature requires Win32::Process 0.17";
}
my $cwd= '.';
if (defined $options->{cwd}) {
-d $options->{cwd}
or return $self->_fatal("directory does not exist: '$options->{cwd}'");
$cwd= $options->{cwd};
}
# On Strawberry Perl, CreateProcess will inherit the current process STDIN/STDOUT/STDERR,
# but there is no way to specify them without altering the current process.
# So, redirect handles, then create process, then revert them.
my ($inherit, $new_stdin, $old_stdin, $new_stdout, $old_stdout, $new_stderr, $old_stderr);
if (exists $options->{stdin}) {
$inherit= 1;
$new_stdin= _resolve_file_handle($options->{stdin}, '<', \*STDIN);
open $old_stdin, '<&'.fileno(\*STDIN) or croak "Can't save STDIN: $!\n"
if defined $new_stdin;
}
if (exists $options->{stdout}) {
$inherit= 1;
$new_stdout= _resolve_file_handle($options->{stdout}, '>>', \*STDOUT);
open $old_stdout, '>&'.fileno(\*STDOUT) or croak "Can't save STDOUT: $!\n"
if defined $new_stdout;
}
if (exists $options->{stderr}) {
$inherit= 1;
$new_stderr= _resolve_file_handle($options->{stderr}, '>>', \*STDERR);
open $old_stderr, '>&'.fileno(\*STDERR) or croak "Can't save STDERR: $!\n"
if defined $new_stderr;
}
{
local $@;
eval {
open STDIN, '<&'.fileno($new_stdin) or die "Can't redirect STDIN: $!\n"
if defined $new_stdin;
open STDOUT, '>&'.fileno($new_stdout) or die "Can't redirect STDOUT: $!\n"
if defined $new_stdout;
open STDERR, '>&'.fileno($new_stderr) or die "Can't redirect STDERR: $!\n"
if defined $new_stderr;
# Perl 5.004_04 cannot run Win32::Process::Create on a nonexistant
# hash key.
my $os_obj = 0;
# Create the process.
Win32::Process::Create($os_obj, $exe, $cmdline, $inherit, NORMAL_PRIORITY_CLASS, $cwd)
or die Win32::FormatMessage( Win32::GetLastError() )."\n";
$self->{_pid} = $os_obj->GetProcessID;
$self->{_os_obj} = $os_obj;
};
chomp($err= $@);
# Now restore handles before throwing exception
open STDERR, '>&'.fileno($old_stderr) or warn "Can't restore STDERR: $!\n"
if defined $old_stderr;
open STDOUT, '>&'.fileno($old_stdout) or warn "Can't restore STDOUT: $!\n"
if defined $old_stdout;
open STDIN, '<&'.fileno($old_stdin) or warn "Can't restore STDIN: $!\n"
if defined $old_stdin;
}
if ($self->{_os_obj}) {
return 1;
} else {
return $self->_fatal($err);
}
}
sub _resolve_file_handle {
my ($thing, $mode, $default)= @_;
if (!defined $thing) {
open my $fh, $mode, 'NUL' or croak "open(NUL): $!";
return $fh;
} elsif (ref $thing) {
return fileno($thing) == fileno($default)? undef : $thing;
} else {
open my $fh, $mode, $thing or croak "open($thing): $!";
return $fh;
}
}
# Reap the child.
# (0, exit_value) : sucessfully waited on.
# (1, undef) : process already reaped and exit value lost.
# (2, undef) : process still running.
sub _waitpid {
my ($self, $blocking, $wait_seconds) = @_;
# Try to wait on the process.
my $result = $self->{_os_obj}->Wait($wait_seconds? int($wait_seconds * 1000) : $blocking ? INFINITE : 0);
# Process finished. Grab the exit value.
if ($result == 1) {
delete $self->{_suspended};
my $exit_code;
$self->{_os_obj}->GetExitCode($exit_code);
if ($exit_code == 256 && $self->{_called_terminateprocess}) {
return (0, 9); # simulate SIGKILL exit status
} else {
return (0, $exit_code<<8);
}
}
# Process still running.
elsif ($result == 0) {
return (2, 0);
}
# If we reach here, then something odd happened.
return (0, 1<<8);
}
sub _suspend {
$_[0]->{_os_obj}->Suspend();
}
sub _resume {
$_[0]->{_os_obj}->Resume();
}
sub _terminate {
my $self = shift;
my @kill_sequence= @_ && ref $_[0] eq 'ARRAY'? @{ $_[0] } : qw( TERM 2 TERM 8 KILL 3 KILL 7 );
# Try the kill the process several times.
# _reap will collect the exit status of the program.
while (@kill_sequence and $self->alive) {
my $sig= shift @kill_sequence;
my $delay= shift @kill_sequence;
# TODO: fix _taskkill, then re-enable: $sig eq 'KILL'? $self->_terminateprocess : $self->_taskkill;
$self->_terminateprocess;
next unless defined $delay;
last if $self->_reap(1, $delay); # block before sending next signal
}
}
# Use taskkill.exe as a sort of graceful SIGTERM substitute.
sub _taskkill {
my $self = shift;
# TODO: This doesn't work reliably. Disabled for now, and continue to be heavy-handed
# using TerminateProcess. The right solution would either be to do more elaborate setup
# to make sure the correct taskkill.exe is used (and available), or to dig much deeper
# into Win32 API to enumerate windows or threads and send WM_QUIT, or whatever other APIs
# processes might be watching on Windows. That should probably be its own module.
my $pid= $self->{_pid};
my $out= `taskkill.exe /PID $pid`;
# If can't run taskkill, fall back to TerminateProcess
$self->_terminateprocess unless $? == 0;
}
# Win32 equivalent of SIGKILL is TerminateProcess()
sub _terminateprocess {
my $self = shift;
$self->{_os_obj}->Kill(256); # call TerminateProcess, essentially SIGKILL
$self->{_called_terminateprocess} = 1;
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Proc::Background::Win32 - Windows-specific implementation of process create/wait/kill
=head1 DESCRIPTION
This module does not have a public interface. Use L<Proc::Background>.
=head1 IMPLEMENTATION
=head2 Perl Fork Limitations
When Perl is built as a native Win32 application, the C<fork> and C<exec> are
a broken approximation of their Unix counterparts. Calling C<fork> creates a
I<thread> instead of a process, and there is no way to exit the thread without
running Perl cleanup code, which could damage the parent in unpredictable
ways, like closing file handles. Calling C<POSIX::_exit> will kill both
parent and child (the whole process), and even calling C<exec> in the child
still runs global destruction. File handles are shared between parent and
child, so any file handle redirection you perform in the forked child will
affect the parent and vice versa.
In short, B<never> call C<fork> or C<exec> on native Win32 Perl.
=head2 Command Line
This module implements background processes using L<Win32::Process>, which
uses the Windows API's concepts of C<CreateProcess>, C<TerminateProces>,
C<WaitForSingleObject>, C<GetExitCode>, and so on.
Windows CreateProcess expects an executable name and a command line; breaking
the command line into an argument list is left to each individual application,
most of which use the library function L<https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-commandlinetoargvw|CommandLineToArgvW>. This module
uses L<Win32::ShellQuote> to parse and format Windows command lines.
If you supply a single-string command line, and don't specify the executable
with the C<exe> option, this module parses the first argument from the
command line to be the 'exe' option. It then looks for the 'exe' in
C<< $ENV{PATH} >>, and tries again with a suffix of C<< .exe >> if it didn't
find one. If you specify the option as C<< { exe => undef } >>, this module
skips that step and passes NULL to Win32 C<CreateProcess>, which causes
Windows to parse the first argument and find the executable on its own.
(Letting Windows search for the executable is probably a better idea anyway,
and this might become the default in the future. It only works with
Win32::Process 0.17 or later)
If you supply an array of arguments as the command, this module combines them
into a command line using C<Win32::ShellQuote/quote_native>. The first
argument is used as the executable (unless you specified the C<'exe'> option,
like above).
=head2 Initial File Handles
When B<no handle options> are specified, the new process does B<not inherit any file handles>
of the current process. This differs from the Unix implementation where they are all
inherited by default, but I'm leaving it this way for backward compatibility.
In other words, yes, they ought to be inherited by default, but changing that now
is more likely to break things than fix things.
If you specify B<any> of C<stdin>, C<stdout>, or C<stderr>, any handle not
specified B<will be inherited> as-is. In other words, by indicating you are
interested in passing file handles, the default Unix behavior occurs.
If you wish to redirect a handle to NUL, set the option to C<undef>:
stdin => undef, # stdin will read from NUL device
stdout => $some_fh, # stdout will write to a file handle
stderr => \*STDERR, # stderr will go to the same STDERR of the current process
You may set a file handle to a pipe, but beware, Windows does not support
non-blocking reads or writes to pipes.
=head1 AUTHORS
=over 4
=item *
Blair Zajac <blair@orcaware.com>
=item *
Michael Conrad <mike@nrdvana.net>
=back
=head1 VERSION
version 1.32
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2023 by Michael Conrad, (C) 1998-2009 by Blair Zajac.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|