Handle hostnames with upper-case letters
[webmin.git] / proc / osf-lib.pl
1 # sysv-lib.pl
2 # Functions for parsing sysv-style ps output
3
4 # list_processes([pid]*)
5 sub list_processes
6 {
7 local($line, $dummy, @w, $i, $_, $pcmd, @plist);
8 foreach (@_) { $pcmd .= " -p $_"; }
9 if (!$pcmd) { $pcmd = " -e"; }
10 open(PS, "ps -o user,ruser,group,rgroup,pid,ppid,pgid,pcpu,vsz,nice,etime,time,tty,args $pcmd |");
11 $dummy = <PS>;
12 for($i=0; $line=<PS>; $i++) {
13         chop($line);
14         $line =~ s/^\s+//g;
15         @w = split(/\s+/, $line);
16         if ($line =~ /ps -o user,ruser/) {
17                 # Skip ps command
18                 $i--; next;
19                 }
20         $plist[$i]->{"pid"} = $w[4];
21         $plist[$i]->{"ppid"} = $w[5];
22         $plist[$i]->{"user"} = $w[0];
23         $plist[$i]->{"cpu"} = "$w[7] %";
24         if ($w[8] =~ /^([0-9\.]+)K/)
25                 { $plist[$i]->{"size"} = "$1 kB"; }
26         elsif ($w[8] =~ /^([0-9\.]+)M/)
27                 { $plist[$i]->{"size"} = ($1 * 1000)." kB"; }
28         $plist[$i]->{"time"} = $w[11];
29         $plist[$i]->{"nice"} = $w[9];
30         $plist[$i]->{"args"} = $w[13] eq "<defunct>" ? "defunct"
31                                                   : join(' ', @w[13..$#w]);
32         $plist[$i]->{"_group"} = $w[2];
33         $plist[$i]->{"_ruser"} = $w[1];
34         $plist[$i]->{"_rgroup"} = $w[3];
35         $plist[$i]->{"_pgid"} = $w[6];
36         $plist[$i]->{"_tty"} = $w[12] =~ /\?/ ? $text{'edit_none'} : "/dev/$w[12]";
37         }
38 close(PS);
39 return @plist;
40 }
41
42 # find_mount_processes(mountpoint)
43 # Find all processes under some mount point
44 sub find_mount_processes
45 {
46 local($out);
47 $out = `fuser -c $_[0] 2>/dev/null`;
48 $out =~ s/^\s+//g; $out =~ s/\s+$//g;
49 return split(/\s+/, $out);
50 }
51
52 # find_file_processes([file]+)
53 # Find all processes with some file open
54 sub find_file_processes
55 {
56 local($out, $files);
57 $files = join(' ', map { quotemeta($_) } map { glob($_) } @_);
58 $out = &backquote_command("fuser $files 2>/dev/null");
59 $out =~ s/^\s+//g; $out =~ s/\s+$//g;
60 return split(/\s+/, $out);
61 }
62
63 # renice_proc(pid, nice)
64 sub renice_proc
65 {
66 return undef if (&is_readonly_mode());
67 local $out = &backquote_logged("renice $_[1] -p $_[0] 2>&1");
68 if ($?) { return $out; }
69 return undef;
70 }
71
72 # get_new_pty()
73 # Returns the filehandles and names for a pty and tty
74 sub get_new_pty
75 {
76 opendir(DEV, "/dev");
77 local @ptys = map { "/dev/$_" } (grep { /^pty/ } readdir(DEV));
78 closedir(DEV);
79 local ($pty, $tty);
80 foreach $pty (@ptys) {
81         open(PTY, "+>$pty") || next;
82         local $tty = $pty; $tty =~ s/pty/tty/;
83         open(TTY, "+>$tty") || next;
84         local $old = select(PTY); $| = 1;
85         select(TTY); $| = 1; select($old);
86         return (*PTY, *TTY, $pty, $tty);
87         }
88 return ();
89 }
90
91
92
93 foreach $ia (keys %text) {
94         if ($ia =~ /^sysv(_\S+)/) {
95                 $info_arg_map{$1} = $text{$ia};
96                 }
97         }
98
99 @nice_range = (-20 .. 19);
100
101 $has_fuser_command = 1;
102
103 1;
104