| 1 | package XAS::Lib::Pidfile::Win32; | ||
| 2 | |||
| 3 | our $VERSION = '0.01'; | ||
| 4 | |||
| 5 | use Win32::OLE('in'); | ||
| 6 | use XAS::Class | ||
| 7 | debug => 0, | ||
| 8 | version => $VERSION, | ||
| 9 | base => 'XAS::Base', | ||
| 10 | utils => 'run_cmd trim compress', | ||
| 11 | constants => 'TRUE FALSE', | ||
| 12 | mixins => 'is_running scan_name', | ||
| 13 | constant => { | ||
| 14 | wbemFlagReturnImmediately => 0x10, | ||
| 15 | wbemFlagForwardOnly => 0x20, | ||
| 16 | } | ||
| 17 | ; | ||
| 18 | |||
| 19 | #use Data::Dumper; | ||
| 20 | |||
| 21 | # ---------------------------------------------------------------------- | ||
| 22 | # Public Methods | ||
| 23 | # ---------------------------------------------------------------------- | ||
| 24 | |||
| 25 | sub scan_name { | ||
| 26 | my $self = shift; | ||
| 27 | |||
| 28 | |||
| 29 | my $c = sprintf('perl %s', $self->env->script); | ||
| 30 | |||
| 31 | return compress($c); | ||
| 32 | |||
| 33 | } | ||
| 34 | |||
| 35 | sub is_running { | ||
| 36 | my $self = shift; | ||
| 37 | |||
| 38 | my $computer = 'localhost'; | ||
| 39 | my $pid = $self->_get_pid(); | ||
| 40 | my $commandline = $self->scan_name(); | ||
| 41 | |||
| 42 | if (defined($pid)) { | ||
| 43 | |||
| 44 | # query wmi for the an existing process with this pid | ||
| 45 | |||
| 46 | my $objWMIService = Win32::OLE->GetObject("winmgmts:\\\\$computer\\root\\CIMV2") or | ||
| 47 | $self->throw_msg( | ||
| 48 | dotid($self->class) . '.is_running.winole', | ||
| 49 | 'unexpected', | ||
| 50 | 'WMI connection failed' | ||
| 51 | ); | ||
| 52 | |||
| 53 | my $colItems = $objWMIService->ExecQuery( | ||
| 54 | "SELECT * FROM Win32_Process WHERE ProcessID = $pid", | ||
| 55 | "WQL", | ||
| 56 | wbemFlagReturnImmediately | wbemFlagForwardOnly | ||
| 57 | ); | ||
| 58 | |||
| 59 | foreach my $objItem (in $colItems) { | ||
| 60 | |||
| 61 | my $line = compress($objItem->{CommandLine}); | ||
| 62 | $self->log->debug(sprintf("\"%s\" = \"%s\"", $commandline, $line)); | ||
| 63 | |||
| 64 | return $pid if ($commandline =~ /$line/); | ||
| 65 | |||
| 66 | } | ||
| 67 | |||
| 68 | } | ||
| 69 | |||
| 70 | return undef; | ||
| 71 | |||
| 72 | } | ||
| 73 | |||
| 74 | # ---------------------------------------------------------------------- | ||
| 75 | # Private Methods | ||
| 76 | # ---------------------------------------------------------------------- | ||
| 77 | |||
| 78 | 1; | ||
| 79 | |||
| 80 | __END__ | ||
| 81 | |||
| 82 | =head1 NAME | ||
| 83 | |||
| 84 | XAS::Lib::PidFile::Win32 - A mixin class to scan for processes on Win32 | ||
| 85 | |||
| 86 | =head1 DESCRIPTION | ||
| 87 | |||
| 88 | This is a mixin class to provide process scanning on a Win32 platform. It | ||
| 89 | invokes WMI thru OLE to do the process scan. | ||
| 90 | |||
| 91 | =head1 METHODS | ||
| 92 | |||
| 93 | =head2 is_running | ||
| 94 | |||
| 95 | Please see L<XAS::Lib::PidFile|XAS::Lib::PidFile> for usage. | ||
| 96 | |||
| 97 | =head1 SEE ALSO | ||
| 98 | |||
| 99 | =over 4 | ||
| 100 | |||
| 101 | =item L<XAS|XAS> | ||
| 102 | |||
| 103 | =back | ||
| 104 | |||
| 105 | =head1 AUTHOR | ||
| 106 | |||
| 107 | Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt> | ||
| 108 | |||
| 109 | =head1 COPYRIGHT AND LICENSE | ||
| 110 | |||
| 111 | Copyright (C) 2014 Kevin L. Esteb | ||
| 112 | |||
| 113 | This is free software; you can redistribute it and/or modify it under | ||
| 114 | the terms of the Artistic License 2.0. For details, see the full text | ||
| 115 | of the license at http://www.perlfoundation.org/artistic_license_2_0. | ||
| 116 | |||
| 117 | =cut |