| 1 | package P9Y::ProcessTable::Role::Table::OS::MSWin32; | ||
| 2 | |||
| 3 | our $AUTHORITY = 'cpan:BBYRD'; # AUTHORITY | ||
| 4 | our $VERSION = '1.06_01'; # VERSION | ||
| 5 | |||
| 6 | ############################################################################# | ||
| 7 | # Modules | ||
| 8 | |||
| 9 | # use sanity; | ||
| 10 | use strict qw(subs vars); | ||
| 11 | no strict 'refs'; | ||
| 12 | use warnings FATAL => 'all'; | ||
| 13 | no warnings qw(uninitialized); | ||
| 14 | |||
| 15 | use Moo::Role; | ||
| 16 | |||
| 17 | requires 'table'; | ||
| 18 | |||
| 19 | use Win32::Process; | ||
| 20 | use Win32::Process::Info; | ||
| 21 | use Path::Class; | ||
| 22 | |||
| 23 | use namespace::clean; | ||
| 24 | no warnings 'uninitialized'; | ||
| 25 | |||
| 26 | my $pi = Win32::Process::Info->new(); | ||
| 27 | |||
| 28 | my $IS_CYGWIN = ($^O =~ /cygwin/i) ? 1 : 0; | ||
| 29 | |||
| 30 | ############################################################################# | ||
| 31 | # Methods | ||
| 32 | |||
| 33 | sub list { | ||
| 34 | my ($self) = @_; | ||
| 35 | my %winpids = map { $_ => 1 } $self->_win32_list; | ||
| 36 | my %cygpids = map { $_ => 1 } ($IS_CYGWIN ? $self->_cyg_list : ()); | ||
| 37 | my %pids = (%winpids, %cygpids); | ||
| 38 | return sort { $a <=> $b } keys %pids; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub fields { | ||
| 42 | my ($self) = @_; | ||
| 43 | my %winflds = map { $_ => 1 } $self->_win32_fields; | ||
| 44 | my %cygflds = map { $_ => 1 } ($IS_CYGWIN ? $self->_cyg_fields : ()); | ||
| 45 | my %fields = (%winflds, %cygflds); | ||
| 46 | |||
| 47 | # (keeps the order straight) | ||
| 48 | return grep { $fields{$_} } ( qw/ | ||
| 49 | pid uid gid euid egid suid sgid ppid pgrp sess | ||
| 50 | cwd exe root cmdline environ | ||
| 51 | minflt cminflt majflt cmajflt ttlflt cttlflt utime stime cutime cstime start time ctime | ||
| 52 | priority fname state ttynum ttydev flags threads size rss wchan cpuid pctcpu pctmem | ||
| 53 | winpid winexe | ||
| 54 | / ); | ||
| 55 | } | ||
| 56 | |||
| 57 | sub process { | ||
| 58 | my ($self, $pid) = @_; | ||
| 59 | $pid = $$ unless defined $pid; | ||
| 60 | my $hash = $self->_process_hash($pid); | ||
| 61 | return unless $hash && $hash->{pid} && $hash->{ppid}; | ||
| 62 | |||
| 63 | $hash->{_pt_obj} = $self; | ||
| 64 | return P9Y::ProcessTable::Process->new($hash); | ||
| 65 | } | ||
| 66 | |||
| 67 | sub _process_hash { | ||
| 68 | my ($self, $pid) = @_; | ||
| 69 | return ($IS_CYGWIN and Cygwin::pid_to_winpid($pid)) ? | ||
| 70 | $self-> _cyg_process_hash($pid) : | ||
| 71 | $self->_win32_process_hash($pid) | ||
| 72 | ; | ||
| 73 | } | ||
| 74 | |||
| 75 | ############################ | ||
| 76 | ## Win32 only methods | ||
| 77 | |||
| 78 | sub _win32_list { | ||
| 79 | return sort { $a <=> $b } ($pi->ListPids); | ||
| 80 | } | ||
| 81 | |||
| 82 | sub _win32_fields { | ||
| 83 | return qw( | ||
| 84 | pid uid ppid sess | ||
| 85 | exe root | ||
| 86 | ttlflt utime stime start state time | ||
| 87 | threads priority fname state size rss | ||
| 88 | ); | ||
| 89 | } | ||
| 90 | |||
| 91 | sub _win32_process_hash { | ||
| 92 | my ($self, $pid) = @_; | ||
| 93 | my $info = $pi->GetProcInfo($pid); | ||
| 94 | return unless $info; | ||
| 95 | $info = $info->[0]; | ||
| 96 | |||
| 97 | my $hash = {}; | ||
| 98 | my $stat_loc = { qw/ | ||
| 99 | pid ProcessId | ||
| 100 | uid Owner | ||
| 101 | ppid ParentProcessId | ||
| 102 | sess SessionId | ||
| 103 | exe ExecutablePath | ||
| 104 | threads ThreadCount | ||
| 105 | priority Priority | ||
| 106 | ttlflt PageFaults | ||
| 107 | utime UserModeTime | ||
| 108 | stime KernelModeTime | ||
| 109 | size VirtualSize | ||
| 110 | rss WorkingSetSize | ||
| 111 | fname Caption | ||
| 112 | start CreationDate | ||
| 113 | state Status | ||
| 114 | cmdline CommandLine | ||
| 115 | / }; | ||
| 116 | |||
| 117 | foreach my $key (keys %$stat_loc) { | ||
| 118 | my $item = $info->{ $stat_loc->{$key} }; | ||
| 119 | $hash->{$key} = $item if defined $item; | ||
| 120 | } | ||
| 121 | |||
| 122 | $hash->{exe} =~ /^(\w\:\\)/; | ||
| 123 | $hash->{root} = $1; | ||
| 124 | $hash->{time} = $hash->{utime} + $hash->{stime}; | ||
| 125 | |||
| 126 | return $hash; | ||
| 127 | } | ||
| 128 | |||
| 129 | ############################ | ||
| 130 | ## Cygwin only methods | ||
| 131 | |||
| 132 | ### TODO: Leverage ProcFS, instead of copying the same code ### | ||
| 133 | |||
| 134 | sub _cyg_list { | ||
| 135 | my @list; | ||
| 136 | |||
| 137 | my $dir = dir('', 'proc'); | ||
| 138 | while (my $pdir = $dir->next) { | ||
| 139 | next unless ($pdir->is_dir); | ||
| 140 | next unless (-e $pdir->file('status')); | ||
| 141 | next unless ($pdir->basename =~ /^\d+$/); | ||
| 142 | |||
| 143 | push @list, $pdir->basename; | ||
| 144 | } | ||
| 145 | |||
| 146 | return @list; | ||
| 147 | } | ||
| 148 | |||
| 149 | sub _cyg_fields { | ||
| 150 | return qw( | ||
| 151 | pid uid gid ppid pgrp sess | ||
| 152 | cwd exe root cmdline | ||
| 153 | minflt cminflt majflt cmajflt ttlflt cttlflt | ||
| 154 | utime stime cutime cstime start time ctime | ||
| 155 | priority fname state ttynum flags size rss | ||
| 156 | winpid winexe | ||
| 157 | ); | ||
| 158 | } | ||
| 159 | |||
| 160 | sub _cyg_process_hash { | ||
| 161 | my ($self, $pid) = @_; | ||
| 162 | |||
| 163 | my $pdir = dir('', 'proc', $pid); | ||
| 164 | return unless (-d $pdir); | ||
| 165 | my $hash = { | ||
| 166 | pid => $pid, | ||
| 167 | uid => $pdir->stat->uid, | ||
| 168 | gid => $pdir->stat->gid, | ||
| 169 | start => $pdir->stat->mtime, | ||
| 170 | }; | ||
| 171 | |||
| 172 | # process links | ||
| 173 | foreach my $ln (qw{cwd exe root}) { | ||
| 174 | my $link = $pdir->file($ln); | ||
| 175 | $hash->{$ln} = readlink $link if (-l $link); | ||
| 176 | } | ||
| 177 | |||
| 178 | # process simple cats | ||
| 179 | foreach my $fn (qw{cmdline winpid winexename}) { | ||
| 180 | my $file = $pdir->file($fn); | ||
| 181 | next unless (-f $file); | ||
| 182 | $hash->{$fn} = $file->slurp; | ||
| 183 | $hash->{$fn} =~ s/\0/ /g; | ||
| 184 | $hash->{$fn} =~ s/^\s+|\s+$//g; | ||
| 185 | $hash->{winexe} = delete $hash->{$fn} if ($fn eq 'winexename'); | ||
| 186 | } | ||
| 187 | |||
| 188 | # process main PID stats | ||
| 189 | if (-f $pdir->file('stat')) { | ||
| 190 | |||
| 191 | # stat | ||
| 192 | my $data = $pdir->file('stat')->slurp; | ||
| 193 | my @data = split /\s+/, $data; | ||
| 194 | |||
| 195 | my $states = { | ||
| 196 | R => 'run', | ||
| 197 | S => 'sleep', | ||
| 198 | D => 'disk sleep', | ||
| 199 | Z => 'defunct', | ||
| 200 | T => 'stop', | ||
| 201 | W => 'paging', | ||
| 202 | }; | ||
| 203 | |||
| 204 | # See cygwin/fhandler_process.cc for the order | ||
| 205 | my $stat_loc = [ qw( | ||
| 206 | pid fname state ppid pgrp sess ttynum . flags minflt cminflt majflt cmajflt | ||
| 207 | utime stime cutime cstime priority . . . . size rss . | ||
| 208 | ) ]; | ||
| 209 | |||
| 210 | foreach my $i (0 .. @data - 1) { | ||
| 211 | next if $stat_loc->[$i] eq '.'; | ||
| 212 | last if ($i >= @$stat_loc); | ||
| 213 | $hash->{ $stat_loc->[$i] } = $data[$i]; | ||
| 214 | } | ||
| 215 | |||
| 216 | $hash->{fname} =~ s/^\((.+)\)$/$1/; | ||
| 217 | $hash->{state} = $states->{ $hash->{state} }; | ||
| 218 | $hash->{ time} = $hash->{ utime} + $hash->{ stime}; | ||
| 219 | $hash->{ctime} = $hash->{cutime} + $hash->{cstime}; | ||
| 220 | |||
| 221 | $hash->{ ttlflt} = $hash->{ minflt} + $hash->{ majflt}; | ||
| 222 | $hash->{cttlflt} = $hash->{cminflt} + $hash->{cmajflt}; | ||
| 223 | } | ||
| 224 | |||
| 225 | return $hash; | ||
| 226 | } | ||
| 227 | |||
| 228 | 42; |