change the date
[tools/build.git] / killchroot
1 #!/usr/bin/perl
2
3 ################################################################
4 #
5 # Copyright (c) 1995-2014 SUSE Linux Products GmbH
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License version 2 or 3 as
9 # published by the Free Software Foundation.
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 (see the file COPYING); if not, write to the
18 # Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20 #
21 ################################################################
22
23 my $sig = 15;
24 my $verbose = 0;
25 my $msg = '';
26 my $doit = 1;
27
28 while (@ARGV) {
29   if ($ARGV[0] eq '-s' || $ARGV[0] eq '--signal') {
30     die("$ARGV[0]: argument required") unless @ARGV > 1;
31     $sig = $ARGV[1];
32     shift @ARGV;
33     shift @ARGV;
34     next;
35   }
36   if ($ARGV[0] eq '-v' || $ARGV[0] eq '--verbose') {
37     $verbose = 1;
38     shift @ARGV;
39     next;
40   }
41   if ($ARGV[0] eq '-n') {
42     $doit = 0;
43     $verbose = 1;
44     shift @ARGV;
45     next;
46   }
47   if ($ARGV[0] eq '-m' || $ARGV[0] eq '--message') {
48     die("$ARGV[0]: argument required") unless @ARGV > 1;
49     $msg = $ARGV[1];
50     shift @ARGV;
51     shift @ARGV;
52     next;
53   }
54   last;
55 }
56
57 die("usage: killchroot [-s sig] <rootdir>") unless @ARGV == 1 && $ARGV[0] ne '';
58 my $dir = $ARGV[0];
59 chdir($dir) || die("$dir: $!\n");
60
61 $dir = readlink('/proc/self/cwd');
62 die("readlink /proc/self/cwd: $!\n") unless defined $dir;
63
64 my %pids;
65 my $pid;
66 my $path;
67
68 opendir(D, "/proc") || die("/proc: $!\n");
69 for $pid (readdir(D)) {
70   next unless $pid =~ /^\d+$/;
71   $path = readlink("/proc/$pid/root");
72   next unless defined $path;
73   if ($path =~ /^\Q$dir\E(\/.*)?$/) {
74     $pids{$pid} = 1;
75   }
76   $path = readlink("/proc/$pid/exe");
77   if ($path =~ /^\Q$dir\E(\/.*)?$/) {
78     $pids{$pid} = 1;
79   }
80 }
81 closedir(D);
82
83 my @pids = sort keys %pids;
84 exit 0 unless @pids;
85
86 print "$msg\n" if $msg ne '';
87
88 if ($verbose) {
89   my @pidsv = ();
90   for $pid (@pids) {
91     open(F, "</proc/$pid/cmdline");
92     my $cmd = '';
93     sysread(F, $cmd, 128);
94     close F;
95     $cmd =~ s/\0.*//;
96     #$cmd =~ s/ .*//;
97     push @pidsv, "$pid($cmd)";
98   }
99   if (@pids > 1) {
100     print "sending signal $sig to processes @pidsv\n";
101   } else {
102     print "sending signal $sig to process @pidsv\n";
103   }
104 }
105 exit 0 unless $doit;
106 kill $sig => @pids;
107 kill CONT => @pids if $sig eq 9;
108 exit 0;