ready for tagging
[util-vserver.git] / scripts / legacy / vps.pathsubst
1 #!/usr/bin/perl
2
3 # Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 # based on vps by Krischan Jodies
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2, or (at your option)
9 # any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20 # Wrapper for ps. Adds security context number and name
21 # to the PID column.
22 #
23 # (c) 2002 krischan.jodies@sernet.de
24 # version 1.0
25
26 # 1.1        Added error messages
27
28 if ($ARGV[0] eq "--help") {
29    exec("ps @ARGV");
30    exit;
31
32
33
34 $lockdir = "/var/run/vservers";
35
36 $pid_length = 5;
37 get_server_names();
38 $context=get_context("self");
39
40 if ($context == -1) {
41    print "Can not find my security context. Is this a ctx kernel?\n";
42    exit;
43 }
44 if ($context != 1) {
45    exec("@PKGLIBDIR@/chcontext-compat --silent --ctx 1 $0 @ARGV");
46    print "Can not execute chcontext\n";
47    exit; # not reached
48
49
50
51 if (! open PS, "ps @ARGV|") {
52    print "Can not execute ps\n";
53    exit;
54 }
55 while (<PS>) {
56   chomp;
57   push @ps,$_;
58 }
59
60 $header = shift @ps;
61 $header =~ /(.*)  PID(.+)/;
62 $left = $1;
63 $right = $2;
64 $left_length  = length($left);
65 print "${left}  PID CONTEXT       ${right}\n";
66 foreach $line (@ps) {
67    $pid =  substr $line,$left_length,$pid_length;
68    print   substr $line,0,$left_length;
69    print "$pid";
70    $context = get_context($pid);
71    $context_name = getcontextname($context);
72    printf " %-3s %-10s",$context,$context_name;
73    print   substr $line,$left_length + $pid_length;
74    print "\n";
75    
76 }
77
78 exit;
79
80 sub get_context
81 {
82    my $pid = $_[0];
83    $pid =~ s/ //g;
84    open STATUS, "/proc/$pid/status";
85    while (<STATUS>)
86    {
87       chomp;
88       if (/s_context: (\d+)/) {
89          close STATUS;   
90          return $1;
91       }
92    }
93    close STATUS;
94    return -1;
95 }
96
97 sub getcontextname {
98    if (exists $name{$_[0]}) {
99       return $name{$_[0]};
100    }
101    if    ($_[0] == 1) {
102       return "ALL_PROCS";
103    }
104    elsif ($_[0] == 0) {
105       return "MAIN";
106    }
107    return "UNKNOWN";
108 }
109
110
111 sub get_server_names 
112 {
113     opendir LOCKDIR, "$lockdir";
114     while ($file=readdir(LOCKDIR)) {
115        if (-f "$lockdir/$file") {
116           open FILE, "$lockdir/$file";
117           $file =~ s/(.+)\.ctx/$1/;
118           while (<FILE>) {
119              if (/S_CONTEXT=(\d+)/) {
120                 $name{$1} = $file;
121              }
122           }
123        }
124     }
125 }