| 1 | package XAS::Lib::Process::Win32; | ||
| 2 | |||
| 3 | our $VERSION = '0.01'; | ||
| 4 | |||
| 5 | use POE; | ||
| 6 | use Win32::Process; | ||
| 7 | use Win32::OLE('in'); | ||
| 8 | use Win32::Socketpair 'winsocketpair'; | ||
| 9 | |||
| 10 | use XAS::Class | ||
| 11 | debug => 0, | ||
| 12 | version => $VERSION, | ||
| 13 | base => 'XAS::Base', | ||
| 14 | utils => ':env dotid compress', | ||
| 15 | filesystem => 'Dir Cwd', | ||
| 16 | mixins => 'start_process stop_process pause_process resume_process | ||
| 17 | stat_process kill_process init_process _poll_child | ||
| 18 | _parse_command RUNNING STOPPED PAUSED SHUTDOWN', | ||
| 19 | constant => { | ||
| 20 | RUNNING => 0, | ||
| 21 | STOPPED => 1, | ||
| 22 | PAUSED => 2, | ||
| 23 | SHUTDOWN => 3, | ||
| 24 | wbemFlagReturnImmediately => 0x10, | ||
| 25 | wbemFlagForwardOnly => 0x20, | ||
| 26 | } | ||
| 27 | ; | ||
| 28 | |||
| 29 | # ---------------------------------------------------------------------- | ||
| 30 | # Public Methods | ||
| 31 | # ---------------------------------------------------------------------- | ||
| 32 | |||
| 33 | sub start_process { | ||
| 34 | my $self = shift; | ||
| 35 | |||
| 36 | my $alias = $self->alias; | ||
| 37 | my @args = $self->_parse_command(); | ||
| 38 | my $dir = $self->directory->path; | ||
| 39 | |||
| 40 | my $process; | ||
| 41 | my $parent; | ||
| 42 | my $child; | ||
| 43 | my $stdin; | ||
| 44 | my $stderr; | ||
| 45 | my $stdout; | ||
| 46 | my $inherit; | ||
| 47 | my $env = $self->environment; | ||
| 48 | my $flags = 0; | ||
| 49 | |||
| 50 | $self->log->debug("$alias: entering start_process"); | ||
| 51 | $self->log->debug("$alias: app: $args[0], args: @args"); | ||
| 52 | |||
| 53 | # create the new environment, this is inherited by the new process | ||
| 54 | |||
| 55 | my $oldenv = env_store(); | ||
| 56 | env_create($env); | ||
| 57 | |||
| 58 | # dup stdin, stdout and stderr | ||
| 59 | |||
| 60 | open($stdin, '<&', \*STDIN); | ||
| 61 | open($stdout, '>&', \*STDOUT); | ||
| 62 | open($stderr, '>&', \*STDERR); | ||
| 63 | |||
| 64 | if ($self->redirect) { | ||
| 65 | |||
| 66 | # get a socket pair - this needs the standard stdin, stdout | ||
| 67 | # and stderr otherwise it errors out | ||
| 68 | |||
| 69 | unless (($parent, $child) = winsocketpair()) { | ||
| 70 | |||
| 71 | $self->throw_msg( | ||
| 72 | dotid($self->class) . '.start_process.socketpair', | ||
| 73 | 'unexpected', | ||
| 74 | "unable to create a socketpair, reason: $!" | ||
| 75 | ); | ||
| 76 | |||
| 77 | } | ||
| 78 | |||
| 79 | # redirect stdin, stdout and stderr to the child socket | ||
| 80 | # with stderr combined with stdout | ||
| 81 | |||
| 82 | open(STDIN, '<&', $child); | ||
| 83 | open(STDOUT, '>&', $child); | ||
| 84 | open(STDERR, '>&', $child); | ||
| 85 | |||
| 86 | # spawn the process, this will inherit stdin, stdout and stderr | ||
| 87 | |||
| 88 | $inherit = 1; # tell Create() to inherit open file handles | ||
| 89 | # note: doesn't seem to work as documented | ||
| 90 | |||
| 91 | unless (Win32::Process::Create($process, $args[0], "@args", $inherit, $flags, $dir)) { | ||
| 92 | |||
| 93 | $self->throw_msg( | ||
| 94 | dotid($self->class) . '.start_process.creation', | ||
| 95 | 'unexpected', | ||
| 96 | _get_error() | ||
| 97 | ); | ||
| 98 | |||
| 99 | } | ||
| 100 | |||
| 101 | # close the child socket | ||
| 102 | |||
| 103 | close($child); | ||
| 104 | |||
| 105 | # listen on the parent socket | ||
| 106 | |||
| 107 | $self->input_handle($parent); | ||
| 108 | $self->output_handle($parent); | ||
| 109 | |||
| 110 | # setup POE's I/O handling | ||
| 111 | |||
| 112 | $self->_process_output(); | ||
| 113 | $self->_process_input(); | ||
| 114 | |||
| 115 | } else { | ||
| 116 | |||
| 117 | # no redirected stdin, stdout or stderr | ||
| 118 | |||
| 119 | $inherit = 0; # tell Create() to not inherit open file handles | ||
| 120 | # note: doesn't seem to work as documented | ||
| 121 | |||
| 122 | # close stdin, stdout and stderr - this really stops the inheritance | ||
| 123 | |||
| 124 | close(STDIN); | ||
| 125 | close(STDOUT); | ||
| 126 | close(STDERR); | ||
| 127 | |||
| 128 | # spawn the process | ||
| 129 | |||
| 130 | unless (Win32::Process::Create($process, $args[0], "@args", $inherit, $flags, $dir)) { | ||
| 131 | |||
| 132 | $self->throw_msg( | ||
| 133 | dotid($self->class) . '.start_process.creation', | ||
| 134 | 'unexpected', | ||
| 135 | _get_error() | ||
| 136 | ); | ||
| 137 | |||
| 138 | } | ||
| 139 | |||
| 140 | } | ||
| 141 | |||
| 142 | # recover the original stdin, stdout and stderr | ||
| 143 | |||
| 144 | open(STDIN, '<&', $stdin); | ||
| 145 | open(STDOUT, '>&', $stdout); | ||
| 146 | open(STDERR, '>&', $stderr); | ||
| 147 | |||
| 148 | # recover the old environment | ||
| 149 | |||
| 150 | env_restore($oldenv); | ||
| 151 | |||
| 152 | # retrieve the process id and the process object | ||
| 153 | |||
| 154 | $self->status(RUNNING); | ||
| 155 | |||
| 156 | $self->{pid} = $process->GetProcessID(); | ||
| 157 | $self->{process} = $process; | ||
| 158 | |||
| 159 | $self->log->info_msg('process_started', $alias, $self->pid); | ||
| 160 | |||
| 161 | # start the background child poller | ||
| 162 | |||
| 163 | $poe_kernel->delay('poll_child', 5); | ||
| 164 | |||
| 165 | $self->log->debug("$alias: leaving start_process"); | ||
| 166 | |||
| 167 | } | ||
| 168 | |||
| 169 | sub stat_process { | ||
| 170 | my $self = shift; | ||
| 171 | |||
| 172 | my $computer = 'localhost'; | ||
| 173 | my $alias = $self->alias; | ||
| 174 | |||
| 175 | $self->log->debug("$alias: entering stat_process"); | ||
| 176 | |||
| 177 | if (my $pid = $self->pid) { | ||
| 178 | |||
| 179 | # query wmi for the an existing process with this pid | ||
| 180 | |||
| 181 | my $objWMIService = Win32::OLE->GetObject("winmgmts:\\\\$computer\\root\\CIMV2") or | ||
| 182 | $self->throw_msg( | ||
| 183 | dotid($self->class) . '.stat_process.winole', | ||
| 184 | 'unexpected', | ||
| 185 | 'WMI connection failed' | ||
| 186 | ); | ||
| 187 | |||
| 188 | my $colItems = $objWMIService->ExecQuery( | ||
| 189 | "SELECT * FROM Win32_Process WHERE ProcessID = $pid", | ||
| 190 | "WQL", | ||
| 191 | wbemFlagReturnImmediately | wbemFlagForwardOnly | ||
| 192 | ); | ||
| 193 | |||
| 194 | # win32 wmi ExecutionState codes | ||
| 195 | # from http://msdn.microsoft.com/en-us/library/aa394372(v=vs.85).aspx | ||
| 196 | # | ||
| 197 | # unknown - 0 | ||
| 198 | # other - 1 | ||
| 199 | # ready - 2 | ||
| 200 | # running - 3 | ||
| 201 | # blocked - 4 | ||
| 202 | # suspended blocked - 5 | ||
| 203 | # suspended ready - 6 | ||
| 204 | |||
| 205 | foreach my $objItem (in $colItems) { | ||
| 206 | |||
| 207 | return $objItem->{ExecutionState} + 0 if ($objItem->{ProcessId} eq $pid); | ||
| 208 | |||
| 209 | } | ||
| 210 | |||
| 211 | } | ||
| 212 | |||
| 213 | $self->log->debug("$alias: leaving stat_process"); | ||
| 214 | |||
| 215 | return 0; | ||
| 216 | |||
| 217 | } | ||
| 218 | |||
| 219 | sub pause_process { | ||
| 220 | my $self = shift; | ||
| 221 | |||
| 222 | my $alias = $self->alias; | ||
| 223 | |||
| 224 | $self->log->debug("$alias: entering pause_process"); | ||
| 225 | |||
| 226 | if (my $pid = $self->pid) { | ||
| 227 | |||
| 228 | my $code = $self->stat_process(); | ||
| 229 | |||
| 230 | if (($code == 3) || ($code == 2)) { # process is running or ready | ||
| 231 | |||
| 232 | $self->status(PAUSED); | ||
| 233 | $self->process->Suspend(); | ||
| 234 | $self->log->warn_msg('process_paused', $alias, $self->pid); | ||
| 235 | |||
| 236 | } | ||
| 237 | |||
| 238 | } | ||
| 239 | |||
| 240 | $self->log->debug("$alias: leaving pause_process"); | ||
| 241 | |||
| 242 | } | ||
| 243 | |||
| 244 | sub resume_process { | ||
| 245 | my $self = shift; | ||
| 246 | |||
| 247 | my $alias = $self->alias; | ||
| 248 | |||
| 249 | $self->log->debug("$alias: entering resume_process"); | ||
| 250 | |||
| 251 | if (my $pid = $self->pid) { | ||
| 252 | |||
| 253 | my $code = $self->stat_process(); | ||
| 254 | |||
| 255 | unless ($code == 6) { # process is suspended ready | ||
| 256 | |||
| 257 | $self->status(RUNNING); | ||
| 258 | $self->process->Resume(); | ||
| 259 | $self->log->warn_msg('process_started', $alias, $self->pid); | ||
| 260 | |||
| 261 | } | ||
| 262 | |||
| 263 | } | ||
| 264 | |||
| 265 | $self->log->debug("$alias: leavin resume_process"); | ||
| 266 | |||
| 267 | } | ||
| 268 | |||
| 269 | sub stop_process { | ||
| 270 | my $self = shift; | ||
| 271 | |||
| 272 | my $exitcode = 0; | ||
| 273 | my $alias = $self->alias; | ||
| 274 | |||
| 275 | $self->log->debug("$alias: entering stop_process"); | ||
| 276 | |||
| 277 | if (my $pid = $self->pid) { | ||
| 278 | |||
| 279 | $self->status(STOPPED); | ||
| 280 | $self->retries(0); | ||
| 281 | |||
| 282 | Win32::Process::KillProcess($pid, $exitcode); | ||
| 283 | $self->log->warn_msg('process_stopped', $alias, $self->pid); | ||
| 284 | |||
| 285 | } | ||
| 286 | |||
| 287 | $self->log->debug("$alias: leaving stop_process"); | ||
| 288 | |||
| 289 | } | ||
| 290 | |||
| 291 | sub kill_process { | ||
| 292 | my $self = shift; | ||
| 293 | |||
| 294 | my $alias = $self->alias; | ||
| 295 | |||
| 296 | $self->log->warn_msg('process_killed', $alias, $self->pid); | ||
| 297 | $self->stop_process(); | ||
| 298 | |||
| 299 | } | ||
| 300 | |||
| 301 | sub init_process { | ||
| 302 | my $self = shift; | ||
| 303 | |||
| 304 | $self->{process} = undef; | ||
| 305 | |||
| 306 | } | ||
| 307 | |||
| 308 | # ---------------------------------------------------------------------- | ||
| 309 | # Private Events | ||
| 310 | # ---------------------------------------------------------------------- | ||
| 311 | |||
| 312 | sub _poll_child { | ||
| 313 | my ($self) = $_[OBJECT]; | ||
| 314 | |||
| 315 | my $exitcode; | ||
| 316 | my $pid = $self->pid; | ||
| 317 | my $alias = $self->alias; | ||
| 318 | |||
| 319 | $self->log->debug("$alias: entering poll_child"); | ||
| 320 | |||
| 321 | unless ($self->process->Wait(1000)) { | ||
| 322 | |||
| 323 | $poe_kernel->delay('poll_child', 5); | ||
| 324 | |||
| 325 | return; | ||
| 326 | |||
| 327 | } | ||
| 328 | |||
| 329 | $self->status(STOPPED); | ||
| 330 | $self->process->GetExitCode($exitcode); | ||
| 331 | $exitcode = ($exitcode * 256); # convert to perl semantics | ||
| 332 | |||
| 333 | # turn off various POE 'selects' and 'delays', otherwise the session 'hangs' | ||
| 334 | |||
| 335 | $poe_kernel->delay('poll_child'); | ||
| 336 | $poe_kernel->select_read($self->input_handle) if (defined($self->input_handle)); | ||
| 337 | $poe_kernel->select_write($self->output_handle) if (defined($self->output_handle)); | ||
| 338 | |||
| 339 | # notify 'child_exit' that we are done | ||
| 340 | |||
| 341 | $poe_kernel->post($alias, 'child_exit', 'CHLD', $pid, $exitcode); | ||
| 342 | |||
| 343 | } | ||
| 344 | |||
| 345 | # ---------------------------------------------------------------------- | ||
| 346 | # Private Methods | ||
| 347 | # ---------------------------------------------------------------------- | ||
| 348 | |||
| 349 | sub _parse_command { | ||
| 350 | my $self = shift; | ||
| 351 | |||
| 352 | my @args = split(' ', $self->command); | ||
| 353 | |||
| 354 | my @extensions = ('.exe', '.bat', '.cmd', '.pl'); | ||
| 355 | my @path = split(';', $ENV{PATH}); | ||
| 356 | my $is_absolute_re = '^(?:(?:[a-zA-Z]:[\\\\/])|(?:[\\\\/]{2}\w+[\\\\/]))'; | ||
| 357 | my $has_dir_element_re = "[\\\\/]"; | ||
| 358 | |||
| 359 | # Stolen from Proc::Background | ||
| 360 | # | ||
| 361 | # If there is only one element in the @args array, then just split the | ||
| 362 | # argument by whitespace. If there is more than one element in @args, | ||
| 363 | # then assume that each argument should be properly protected from | ||
| 364 | # the shell so that whitespace and special characters are passed | ||
| 365 | # properly to the program, just as it would be in a Unix | ||
| 366 | # environment. This will ensure that a single argument with | ||
| 367 | # whitespace will not be split into multiple arguments by the time | ||
| 368 | # the program is run. Make sure that any arguments that are already | ||
| 369 | # protected stay protected. Then convert unquoted "'s into \"'s. | ||
| 370 | # Finally, check for whitespace and protect it. | ||
| 371 | |||
| 372 | for (my $i = 1; $i < @args; ++$i) { | ||
| 373 | |||
| 374 | my $arg = $args[$i]; | ||
| 375 | $arg =~ s#\\\\#\200#g; | ||
| 376 | $arg =~ s#\\"#\201#g; | ||
| 377 | $arg =~ s#"#\\"#g; | ||
| 378 | $arg =~ s#\200#\\\\#g; | ||
| 379 | $arg =~ s#\201#\\"#g; | ||
| 380 | |||
| 381 | if (length($arg) == 0 or $arg =~ /\s/) { | ||
| 382 | |||
| 383 | $arg = "\"$arg\""; | ||
| 384 | |||
| 385 | } | ||
| 386 | |||
| 387 | $args[$i] = $arg; | ||
| 388 | |||
| 389 | } | ||
| 390 | |||
| 391 | # Find the absolute path to the program. If it cannot be found, | ||
| 392 | # then return. To work around a problem where | ||
| 393 | # Win32::Process::Create cannot start a process when the full | ||
| 394 | # pathname has a space in it, convert the full pathname to the | ||
| 395 | # Windows short 8.3 format which contains no spaces. | ||
| 396 | |||
| 397 | $args[0] = $self->_resolve_path($args[0], $is_absolute_re, $has_dir_element_re, \@extensions, \@path) or return; | ||
| 398 | $args[0] = Win32::GetShortPathName($args[0]); | ||
| 399 | |||
| 400 | return @args; | ||
| 401 | |||
| 402 | } | ||
| 403 | |||
| 404 | sub _get_error { | ||
| 405 | |||
| 406 | return(compress(Win32::FormatMessage(Win32::GetLastError()))); | ||
| 407 | |||
| 408 | } | ||
| 409 | |||
| 410 | 1; | ||
| 411 | |||
| 412 | __END__ | ||
| 413 | |||
| 414 | =head1 NAME | ||
| 415 | |||
| 416 | XAS::Lib::Process::Win32 - A mixin class for process management within the XAS environment | ||
| 417 | |||
| 418 | =head1 DESCRIPTION | ||
| 419 | |||
| 420 | This module is a mixin class to handle the needs for process management | ||
| 421 | under a Windows system. | ||
| 422 | |||
| 423 | =head1 METHODS | ||
| 424 | |||
| 425 | =head2 init_process | ||
| 426 | |||
| 427 | This method initializes the module so that it can function properly. | ||
| 428 | |||
| 429 | =head2 start_process | ||
| 430 | |||
| 431 | This method does the necessary things to spawn a new process. | ||
| 432 | |||
| 433 | =head2 stat_process | ||
| 434 | |||
| 435 | This method returns the status of the process. These are the possible | ||
| 436 | values. | ||
| 437 | |||
| 438 | =over 4 | ||
| 439 | |||
| 440 | =item B<6> | ||
| 441 | |||
| 442 | This status indicates that the process is in a "suspended ready" state. | ||
| 443 | |||
| 444 | =item B<5> | ||
| 445 | |||
| 446 | This status indicates that the process is in a "suspended blocked" state. | ||
| 447 | |||
| 448 | =item B<4> | ||
| 449 | |||
| 450 | This status indicates that the process is in a "blocked" state. | ||
| 451 | |||
| 452 | =item B<3> | ||
| 453 | |||
| 454 | This status indicates that the process is in a "running" state. | ||
| 455 | |||
| 456 | =item B<2> | ||
| 457 | |||
| 458 | This indicates that the process is in a "ready" state. | ||
| 459 | |||
| 460 | =item B<1> | ||
| 461 | |||
| 462 | This indicates that the process is in a "other" state. | ||
| 463 | |||
| 464 | =item B<0> | ||
| 465 | |||
| 466 | This indicates that the process is in an unknown state. | ||
| 467 | |||
| 468 | =back | ||
| 469 | |||
| 470 | =head2 stop_process | ||
| 471 | |||
| 472 | This method will use Win32::Process:KillProcess to stop the process. | ||
| 473 | |||
| 474 | =head2 pause_process | ||
| 475 | |||
| 476 | This method will use the Pause() method to pause the process. | ||
| 477 | |||
| 478 | =head2 resume_process | ||
| 479 | |||
| 480 | This method will Resume() method to resume the process. | ||
| 481 | |||
| 482 | =head2 kill_process | ||
| 483 | |||
| 484 | This method calls stop_process(). | ||
| 485 | |||
| 486 | =head1 SEE ALSO | ||
| 487 | |||
| 488 | =over 4 | ||
| 489 | |||
| 490 | =item L<Win32::Process|https://metacpan.org/pod/Win32::Process> | ||
| 491 | |||
| 492 | =item L<XAS::Lib::Process|XAS::Lib::Process> | ||
| 493 | |||
| 494 | =item L<XAS|XAS> | ||
| 495 | |||
| 496 | =back | ||
| 497 | |||
| 498 | =head1 AUTHOR | ||
| 499 | |||
| 500 | Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt> | ||
| 501 | |||
| 502 | =head1 COPYRIGHT AND LICENSE | ||
| 503 | |||
| 504 | Copyright (C) 2015 Kevin L. Esteb | ||
| 505 | |||
| 506 | This is free software; you can redistribute it and/or modify it under | ||
| 507 | the terms of the Artistic License 2.0. For details, see the full text | ||
| 508 | of the license at http://www.perlfoundation.org/artistic_license_2_0. | ||
| 509 | |||
| 510 | =cut |