2 # Functions for parsing linux ps output
8 if (!$get_ps_version_cache) {
9 local $out = &backquote_command("ps V 2>&1");
10 if ($out =~ /version\s+([0-9\.]+)\./) {
11 $get_ps_version_cache = $1;
14 return $get_ps_version_cache;
19 local($pcmd, $line, $i, %pidmap, @plist, $dummy, @w, $_);
20 local $ver = &get_ps_version();
22 # New version of ps, as found in redhat 6
25 # Use width format character if allowed
28 open(PS, "ps --cols 2048 -eo user$width,ruser$width,group$width,rgroup$width,pid,ppid,pgid,pcpu,vsz,nice,etime,time,stime,tty,args 2>/dev/null |");
30 for($i=0; $line=<PS>; $i++) {
33 eval { @w = split(/\s+/, $line, -1); };
38 if ($line =~ /ps --cols 500 -eo user/) {
39 # Skip process ID 0 or ps command
42 if (@_ && &indexof($w[4], @_) < 0) {
43 # Not interested in this PID
46 $plist[$i]->{"pid"} = $w[4];
47 $plist[$i]->{"ppid"} = $w[5];
48 $plist[$i]->{"user"} = $w[0];
49 $plist[$i]->{"cpu"} = "$w[7] %";
50 $plist[$i]->{"size"} = "$w[8] kB";
51 $plist[$i]->{"time"} = $w[11];
52 $plist[$i]->{"_stime"} = $w[12];
53 $plist[$i]->{"nice"} = $w[9];
54 $plist[$i]->{"args"} = @w<15 ? "defunct" : join(' ', @w[14..$#w]);
55 $plist[$i]->{"_group"} = $w[2];
56 $plist[$i]->{"_ruser"} = $w[1];
57 $plist[$i]->{"_rgroup"} = $w[3];
58 $plist[$i]->{"_pgid"} = $w[6];
59 $plist[$i]->{"_tty"} = $w[13] =~ /\?/ ? $text{'edit_none'} : "/dev/$w[13]";
65 $pcmd = join(' ' , @_);
66 open(PS, "ps aulxhwwww $pcmd 2>/dev/nul |");
67 for($i=0; $line=<PS>; $i++) {
69 if ($line =~ /ps aulxhwwww/) { $i--; next; }
70 if ($line !~ /^\s*(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+([\-\d]+)\s+([\-\d]+)\s+(\d+)\s+(\d+)\s+(\S*)\s+(\S+)[\s<>N]+(\S+)\s+([0-9:]+)\s+(.*)$/) {
75 $plist[$i]->{"pid"} = $3;
76 $plist[$i]->{"ppid"} = $4;
77 $plist[$i]->{"user"} = getpwuid($2);
78 $plist[$i]->{"size"} = "$7 kB";
79 $plist[$i]->{"cpu"} = "Unknown";
80 $plist[$i]->{"time"} = $12;
81 $plist[$i]->{"nice"} = $6;
82 $plist[$i]->{"args"} = $13;
83 $plist[$i]->{"_pri"} = $5;
84 $plist[$i]->{"_tty"} = $11 eq "?" ? $text{'edit_none'} : "/dev/tty$11";
85 $plist[$i]->{"_status"} = $stat_map{substr($10, 0, 1)};
86 ($plist[$i]->{"_wchan"} = $9) =~ s/\s+$//g;
87 if (!$plist[$i]->{"_wchan"}) { delete($plist[$i]->{"_wchan"}); }
88 if ($plist[$i]->{"args"} =~ /^\((.*)\)/)
89 { $plist[$i]->{"args"} = $1; }
92 open(PS, "ps auxh $pcmd |");
94 if ($line =~ /^\s*(\S+)\s+(\d+)\s+(\S+)\s+(\S+)\s+/ &&
95 defined($pidmap{$2})) {
96 $plist[$pidmap{$2}]->{"cpu"} = $3;
97 $plist[$pidmap{$2}]->{"_mem"} = "$4 %";
105 # renice_proc(pid, nice)
108 return undef if (&is_readonly_mode());
109 local $out = &backquote_logged("renice $_[1] -p $_[0] 2>&1");
110 if ($?) { return $out; }
114 # find_mount_processes(mountpoint)
115 # Find all processes under some mount point
116 sub find_mount_processes
119 $out = &backquote_command("fuser -m ".quotemeta($_[0])." 2>/dev/null");
120 $out =~ s/[^0-9 ]//g;
121 $out =~ s/^\s+//g; $out =~ s/\s+$//g;
122 return split(/\s+/, $out);
125 # find_file_processes([file]+)
126 # Find all processes with some file open
127 sub find_file_processes
130 $files = join(' ', map { quotemeta($_) } map { glob($_) } @_);
131 $out = &backquote_command("fuser $files 2>/dev/null");
132 $out =~ s/[^0-9 ]//g;
133 $out =~ s/^\s+//g; $out =~ s/\s+$//g;
134 return split(/\s+/, $out);
138 # Returns the filehandles and names for a pty and tty
141 if (-r "/dev/ptmx" && -d "/dev/pts" && open(PTMX, "+>/dev/ptmx")) {
142 # Can use new-style PTY number allocation device
146 # ioctl to unlock the PTY (TIOCSPTLCK)
148 ioctl(PTMX, 0x40045431, $unl) || &error("Unlock ioctl failed : $!");
149 $unl = unpack("i", $unl);
151 # ioctl to request a TTY (TIOCGPTN)
152 ioctl(PTMX, 0x80045430, $ptn) || &error("PTY ioctl failed : $!");
153 $ptn = unpack("i", $ptn);
155 local $tty = "/dev/pts/$ptn";
156 return (*PTMX, undef, $tty, $tty);
159 # Have to search manually through pty files!
163 opendir(DEV, "/dev/pty");
164 @ptys = map { "/dev/pty/$_" } readdir(DEV);
169 opendir(DEV, "/dev");
170 @ptys = map { "/dev/$_" } (grep { /^pty/ } readdir(DEV));
175 foreach $pty (@ptys) {
176 open(PTY, "+>$pty") || next;
178 if ($devstyle == 0) {
182 $tty =~ s/m(\d+)$/s$1/;
184 local $old = select(PTY); $| = 1; select($old);
186 # Don't need to open the TTY file here for root,
187 # as it will be opened later after the controlling
188 # TTY has been released.
189 return (*PTY, undef, $pty, $tty);
194 select(TTY); $| = 1; select($old);
195 return (*PTY, *TTY, $pty, $tty);
202 # close_controlling_pty()
203 # Disconnects this process from it's controlling PTY, if connected
204 sub close_controlling_pty
206 if (open(DEVTTY, "/dev/tty")) {
207 # Special ioctl to disconnect (TIOCNOTTY)
208 ioctl(DEVTTY, 0x5422, 0);
213 # open_controlling_pty(ptyfh, ttyfh, ptyfile, ttyfile)
214 # Makes a PTY returned from get_new_pty the controlling TTY (/dev/tty) for
216 sub open_controlling_pty
218 local ($ptyfh, $ttyfh, $pty, $tty) = @_;
220 # Call special ioctl to attach /dev/tty to this new tty (TIOCSCTTY)
221 ioctl($ttyfh, 0x540e, 0);
225 # Returns a list containing the real mem, free real mem, swap and free swap
230 if (open(BEAN, "/proc/user_beancounters")) {
231 # If we are running under Virtuozzo, there may be a limit on memory
232 # use in force that is less than the real system's memory.
234 if (/^privvmpages\s+(\d+)\s+(\d+)\s+(\d+)/) {
235 return ($3, $3-$1, undef, undef);
240 open(MEMINFO, "/proc/meminfo") || return ();
242 if (/^(\S+):\s+(\d+)/) {
247 return ( $m{'memtotal'}, $m{'cached'} > $m{'memtotal'} ? $m{'memfree'}
248 : $m{'memfree'}+$m{'buffers'}+$m{'cached'},
249 $m{'swaptotal'}, $m{'swapfree'} );
253 # Returns a list containing the 5, 10 and 15 minute load averages, and the
254 # CPU mhz, model, vendor, cache and count
257 open(LOAD, "/proc/loadavg") || return ();
258 local @load = split(/\s+/, <LOAD>);
261 open(CPUINFO, "/proc/cpuinfo");
263 if (/^(\S[^:]*\S)\s*:\s*(.*)/) {
268 $c{'model name'} =~ s/\d+\s*mhz//i;
269 if ($c{'cache size'} =~ /^(\d+)\s+KB/i) {
270 $c{'cache size'} = $1*1024;
272 elsif ($c{'cache size'} =~ /^(\d+)\s+MB/i) {
273 $c{'cache size'} = $1*1024*1024;
276 return ( $load[0], $load[1], $load[2],
277 int($c{'cpu mhz'}), $c{'model name'}, $c{'vendor_id'},
278 $c{'cache size'}, $c{'processor'}+1 );
281 return ( $load[0], $load[1], $load[2] );
285 $has_trace_command = &has_command("strace");
287 # open_process_trace(pid, [&syscalls])
288 # Starts tracing on some process, and returns a trace object
289 sub open_process_trace
291 local $fh = time().$$;
294 $sc = "-e trace=".join(",", @{$_[1]});
296 local $tpid = open($fh, "strace -t -p $_[0] $sc 2>&1 |");
298 return { 'pid' => $_[0],
303 # close_process_trace(&trace)
304 # Halts tracing on some trace object
305 sub close_process_trace
307 kill('TERM', $_[0]->{'tpid'}) if ($_[0]->{'tpid'});
308 close($_[0]->{'fh'});
311 # read_process_trace(&trace)
312 # Returns an action structure representing one action by traced process, or
313 # undef if an error occurred
314 sub read_process_trace
316 local $fh = $_[0]->{'fh'};
317 local @tm = localtime(time());
320 return undef if (!$line);
321 if ($line =~ /^(\d+):(\d+):(\d+)\s+([^\(]+)\((.*)\)\s*=\s*(\-?\d+|\?)/) {
322 local $tm = timelocal($3, $2, $1, $tm[3], $tm[4], $tm[5]);
323 local $action = { 'time' => $tm,
325 'rv' => $6 eq "?" ? undef : $6 };
329 if ($args =~ /^[ ,]*(\{[^}]*\})(.*)$/) {
334 elsif ($args =~ /^[ ,]*"([^"]*)"\.*(.*)$/) {
339 elsif ($args =~ /^[ ,]*\[([^\]]*)\](.*)$/) {
340 # A square-bracket number
344 elsif ($args =~ /^[ ,]*\<([^\>]*)\>(.*)$/) {
345 # An angle-bracketed string
349 elsif ($args =~ /[ ,]*([^, ]+)(.*)$/) {
358 if ($args[$#args] eq $action->{'rv'}) {
359 pop(@args); # last arg is same as return value?
361 $action->{'args'} = \@args;
367 foreach $ia (keys %text) {
368 if ($ia =~ /^linux(_\S+)/) {
369 $info_arg_map{$1} = $text{$ia};
371 elsif ($ia =~ /^linuxstat_(\S+)/) {
372 $stat_map{$1} = $text{$ia};
376 @nice_range = (-20 .. 20);
378 $has_fuser_command = 1;
380 # os_list_scheduling_classes()
381 # Returns a list of Linux scheduling classes, if supported. Each element is a
382 # 2-element array ref containing a code and description.
383 sub os_list_scheduling_classes
385 if (&has_command("ionice")) {
386 return ( [ 1, $text{'linux_real'} ],
387 [ 2, $text{'linux_be'} ],
388 [ 3, $text{'linux_idle'} ] );
393 # os_list_scheduling_priorities()
394 # Returns a list of IO priorities, each of which is an array ref containing
395 # a number and description
396 sub os_list_scheduling_priorities
398 return ( [ 0, "0 ($text{'edit_prihigh'})" ],
399 [ 1 ], [ 2 ], [ 3 ], [ 4 ], [ 5 ], [ 6 ],
400 [ 7, "7 ($text{'edit_prilow'})" ] );
403 # os_get_scheduling_class(pid)
404 # Returns the IO scheduling class and priority for a running program
405 sub os_get_scheduling_class
408 local $out = &backquote_command("ionice -p ".quotemeta($pid));
409 if ($out =~ /^(realtime|best-effort|idle|none):\s+prio\s+(\d+)/) {
410 return ($1 eq "realtime" ? 1 : $1 eq "best-effort" ? 2 :
411 $1 eq "idle" ? 3 : 0, $2);
416 # os_set_scheduling_class(pid, class, priority)
417 # Sets the ID scheduling class and priority for some process. Returns an error
418 # message on failure, undef on success.
419 sub os_set_scheduling_class
421 local ($pid, $class, $prio) = @_;
422 local $cmd = "ionice -c ".quotemeta($class);
423 $cmd .= " -n ".quotemeta($prio) if (defined($prio));
424 $cmd .= " -p ".quotemeta($pid);
425 local $out = &backquote_logged("$cmd 2>&1 </dev/null");
426 return $? ? $out : undef;