| 1 | package Proc::Background::Win32; | ||
| 2 | $Proc::Background::Win32::VERSION = '1.32'; | ||
| 3 | # ABSTRACT: Windows-specific implementation of process create/wait/kill | ||
| 4 | require 5.004_04; | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use Exporter; | ||
| 8 | use Carp; | ||
| 9 | use Win32; | ||
| 10 | use Win32::Process qw( NORMAL_PRIORITY_CLASS INFINITE ); | ||
| 11 | use Win32::ShellQuote (); | ||
| 12 | |||
| 13 | @Proc::Background::Win32::ISA = qw(Exporter); | ||
| 14 | |||
| 15 | sub _start { | ||
| 16 | my ($self, $options)= @_; | ||
| 17 | my ($cmd, $exe, $cmdline, $err)= @{$self}{'_command','_exe'}; | ||
| 18 | |||
| 19 | # If 'command' is a single string, treat it as system() would and assume | ||
| 20 | # it should be split into arguments. The first argument is then the | ||
| 21 | # application executable, if not already specified as an option. | ||
| 22 | if (ref $cmd ne 'ARRAY') { | ||
| 23 | $cmdline= $cmd; | ||
| 24 | ($exe) = Win32::ShellQuote::unquote_native($cmdline) | ||
| 25 | unless exists $options->{exe}; | ||
| 26 | } | ||
| 27 | # system() would treat a list of arguments as an un-quoted ARGV | ||
| 28 | # for the program, so concatenate them into a command line appropriate | ||
| 29 | # for Win32 CommandLineToArgvW to decode back to what we started with. | ||
| 30 | # Preserve the first un-quoted argument for use as lpApplicationName, | ||
| 31 | # unless user requested some value (including undef). | ||
| 32 | else { | ||
| 33 | $exe = $cmd->[0] unless exists $options->{exe}; | ||
| 34 | $cmdline= Win32::ShellQuote::quote_native(@$cmd); | ||
| 35 | } | ||
| 36 | |||
| 37 | if (defined $exe) { | ||
| 38 | # Find the absolute path to the program. If it cannot be found, | ||
| 39 | # then return. | ||
| 40 | ($exe, $err) = Proc::Background::_resolve_path($exe); | ||
| 41 | return $self->_fatal($err) unless defined $exe; | ||
| 42 | # To work around a problem where Win32::Process::Create cannot start a | ||
| 43 | # process when the full pathname has a space in it, convert the full | ||
| 44 | # pathname to the Windows short 8.3 format which contains no spaces. | ||
| 45 | $exe = Win32::GetShortPathName($exe) | ||
| 46 | } | ||
| 47 | else { | ||
| 48 | Win32::Process->VERSION > 0.16 | ||
| 49 | or croak "{exe => undef} feature requires Win32::Process 0.17"; | ||
| 50 | } | ||
| 51 | |||
| 52 | my $cwd= '.'; | ||
| 53 | if (defined $options->{cwd}) { | ||
| 54 | -d $options->{cwd} | ||
| 55 | or return $self->_fatal("directory does not exist: '$options->{cwd}'"); | ||
| 56 | $cwd= $options->{cwd}; | ||
| 57 | } | ||
| 58 | |||
| 59 | # On Strawberry Perl, CreateProcess will inherit the current process STDIN/STDOUT/STDERR, | ||
| 60 | # but there is no way to specify them without altering the current process. | ||
| 61 | # So, redirect handles, then create process, then revert them. | ||
| 62 | my ($inherit, $new_stdin, $old_stdin, $new_stdout, $old_stdout, $new_stderr, $old_stderr); | ||
| 63 | if (exists $options->{stdin}) { | ||
| 64 | $inherit= 1; | ||
| 65 | $new_stdin= _resolve_file_handle($options->{stdin}, '<', \*STDIN); | ||
| 66 | open $old_stdin, '<&'.fileno(\*STDIN) or croak "Can't save STDIN: $!\n" | ||
| 67 | if defined $new_stdin; | ||
| 68 | } | ||
| 69 | if (exists $options->{stdout}) { | ||
| 70 | $inherit= 1; | ||
| 71 | $new_stdout= _resolve_file_handle($options->{stdout}, '>>', \*STDOUT); | ||
| 72 | open $old_stdout, '>&'.fileno(\*STDOUT) or croak "Can't save STDOUT: $!\n" | ||
| 73 | if defined $new_stdout; | ||
| 74 | } | ||
| 75 | if (exists $options->{stderr}) { | ||
| 76 | $inherit= 1; | ||
| 77 | $new_stderr= _resolve_file_handle($options->{stderr}, '>>', \*STDERR); | ||
| 78 | open $old_stderr, '>&'.fileno(\*STDERR) or croak "Can't save STDERR: $!\n" | ||
| 79 | if defined $new_stderr; | ||
| 80 | } | ||
| 81 | |||
| 82 | { | ||
| 83 | local $@; | ||
| 84 | eval { | ||
| 85 | open STDIN, '<&'.fileno($new_stdin) or die "Can't redirect STDIN: $!\n" | ||
| 86 | if defined $new_stdin; | ||
| 87 | open STDOUT, '>&'.fileno($new_stdout) or die "Can't redirect STDOUT: $!\n" | ||
| 88 | if defined $new_stdout; | ||
| 89 | open STDERR, '>&'.fileno($new_stderr) or die "Can't redirect STDERR: $!\n" | ||
| 90 | if defined $new_stderr; | ||
| 91 | |||
| 92 | # Perl 5.004_04 cannot run Win32::Process::Create on a nonexistant | ||
| 93 | # hash key. | ||
| 94 | my $os_obj = 0; | ||
| 95 | |||
| 96 | # Create the process. | ||
| 97 | Win32::Process::Create($os_obj, $exe, $cmdline, $inherit, NORMAL_PRIORITY_CLASS, $cwd) | ||
| 98 | or die Win32::FormatMessage( Win32::GetLastError() )."\n"; | ||
| 99 | $self->{_pid} = $os_obj->GetProcessID; | ||
| 100 | $self->{_os_obj} = $os_obj; | ||
| 101 | }; | ||
| 102 | chomp($err= $@); | ||
| 103 | # Now restore handles before throwing exception | ||
| 104 | open STDERR, '>&'.fileno($old_stderr) or warn "Can't restore STDERR: $!\n" | ||
| 105 | if defined $old_stderr; | ||
| 106 | open STDOUT, '>&'.fileno($old_stdout) or warn "Can't restore STDOUT: $!\n" | ||
| 107 | if defined $old_stdout; | ||
| 108 | open STDIN, '<&'.fileno($old_stdin) or warn "Can't restore STDIN: $!\n" | ||
| 109 | if defined $old_stdin; | ||
| 110 | } | ||
| 111 | if ($self->{_os_obj}) { | ||
| 112 | return 1; | ||
| 113 | } else { | ||
| 114 | return $self->_fatal($err); | ||
| 115 | } | ||
| 116 | } | ||
| 117 | |||
| 118 | sub _resolve_file_handle { | ||
| 119 | my ($thing, $mode, $default)= @_; | ||
| 120 | if (!defined $thing) { | ||
| 121 | open my $fh, $mode, 'NUL' or croak "open(NUL): $!"; | ||
| 122 | return $fh; | ||
| 123 | } elsif (ref $thing) { | ||
| 124 | return fileno($thing) == fileno($default)? undef : $thing; | ||
| 125 | } else { | ||
| 126 | open my $fh, $mode, $thing or croak "open($thing): $!"; | ||
| 127 | return $fh; | ||
| 128 | } | ||
| 129 | } | ||
| 130 | |||
| 131 | # Reap the child. | ||
| 132 | # (0, exit_value) : sucessfully waited on. | ||
| 133 | # (1, undef) : process already reaped and exit value lost. | ||
| 134 | # (2, undef) : process still running. | ||
| 135 | sub _waitpid { | ||
| 136 | my ($self, $blocking, $wait_seconds) = @_; | ||
| 137 | |||
| 138 | # Try to wait on the process. | ||
| 139 | my $result = $self->{_os_obj}->Wait($wait_seconds? int($wait_seconds * 1000) : $blocking ? INFINITE : 0); | ||
| 140 | # Process finished. Grab the exit value. | ||
| 141 | if ($result == 1) { | ||
| 142 | delete $self->{_suspended}; | ||
| 143 | my $exit_code; | ||
| 144 | $self->{_os_obj}->GetExitCode($exit_code); | ||
| 145 | if ($exit_code == 256 && $self->{_called_terminateprocess}) { | ||
| 146 | return (0, 9); # simulate SIGKILL exit status | ||
| 147 | } else { | ||
| 148 | return (0, $exit_code<<8); | ||
| 149 | } | ||
| 150 | } | ||
| 151 | # Process still running. | ||
| 152 | elsif ($result == 0) { | ||
| 153 | return (2, 0); | ||
| 154 | } | ||
| 155 | # If we reach here, then something odd happened. | ||
| 156 | return (0, 1<<8); | ||
| 157 | } | ||
| 158 | |||
| 159 | sub _suspend { | ||
| 160 | $_[0]->{_os_obj}->Suspend(); | ||
| 161 | } | ||
| 162 | |||
| 163 | sub _resume { | ||
| 164 | $_[0]->{_os_obj}->Resume(); | ||
| 165 | } | ||
| 166 | |||
| 167 | sub _terminate { | ||
| 168 | my $self = shift; | ||
| 169 | my @kill_sequence= @_ && ref $_[0] eq 'ARRAY'? @{ $_[0] } : qw( TERM 2 TERM 8 KILL 3 KILL 7 ); | ||
| 170 | |||
| 171 | # Try the kill the process several times. | ||
| 172 | # _reap will collect the exit status of the program. | ||
| 173 | while (@kill_sequence and $self->alive) { | ||
| 174 | my $sig= shift @kill_sequence; | ||
| 175 | my $delay= shift @kill_sequence; | ||
| 176 | # TODO: fix _taskkill, then re-enable: $sig eq 'KILL'? $self->_terminateprocess : $self->_taskkill; | ||
| 177 | $self->_terminateprocess; | ||
| 178 | next unless defined $delay; | ||
| 179 | last if $self->_reap(1, $delay); # block before sending next signal | ||
| 180 | } | ||
| 181 | } | ||
| 182 | |||
| 183 | # Use taskkill.exe as a sort of graceful SIGTERM substitute. | ||
| 184 | sub _taskkill { | ||
| 185 | my $self = shift; | ||
| 186 | # TODO: This doesn't work reliably. Disabled for now, and continue to be heavy-handed | ||
| 187 | # using TerminateProcess. The right solution would either be to do more elaborate setup | ||
| 188 | # to make sure the correct taskkill.exe is used (and available), or to dig much deeper | ||
| 189 | # into Win32 API to enumerate windows or threads and send WM_QUIT, or whatever other APIs | ||
| 190 | # processes might be watching on Windows. That should probably be its own module. | ||
| 191 | my $pid= $self->{_pid}; | ||
| 192 | my $out= `taskkill.exe /PID $pid`; | ||
| 193 | # If can't run taskkill, fall back to TerminateProcess | ||
| 194 | $self->_terminateprocess unless $? == 0; | ||
| 195 | } | ||
| 196 | |||
| 197 | # Win32 equivalent of SIGKILL is TerminateProcess() | ||
| 198 | sub _terminateprocess { | ||
| 199 | my $self = shift; | ||
| 200 | $self->{_os_obj}->Kill(256); # call TerminateProcess, essentially SIGKILL | ||
| 201 | $self->{_called_terminateprocess} = 1; | ||
| 202 | } | ||
| 203 | |||
| 204 | 1; | ||
| 205 | |||
| 206 | __END__ |