Debian Codegraph @

bin/linux-check-removal

raw
1 #!/usr/bin/perl
2
3 # Copyright 1996-2006 Manoj Srivastava
4 # Copyright 2016 Ben Hutchings
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 of the License, or
9 # (at your option) 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
20 use strict;
21 use warnings;
22 use FileHandle;
23 use POSIX ();
24
25 sub usage {
26 my $fh = shift;
27 print $fh (<< "EOT");
28 Usage: $0 KERNEL-VER
29
30 This command is intended to be called from the prerm maintainer scripts
31 of Linux kernel packages.
32
33 The KERNEL-VER argument must be the kernel version string as shown by
34 'uname -r' and used in filenames, not the package version.
35
36 If the currently running kernel matches KERNEL-VER, linux-check-removal
37 will normally prompt the user to confirm this potentially dangerous
38 action and will fail if the user chooses to abort. However, if the
39 current environment is in a chroot or container, or if debconf
40 prompts are disabled, it will always succeed without prompting.
41 EOT
42 }
43
44 sub usage_error {
45 usage(*STDERR{IO});
46 exit 2;
47 }
48
49 # Check usage early, before Debconf::Client::ConfModule initialises and
50 # adds its complaints about permissions
51 BEGIN {
52 if (@ARGV != 1) {
53 usage_error();
54 }
55 if ($ARGV[0] eq 'help' or grep({$_ eq '--help'} @ARGV)) {
56 usage(*STDOUT{IO});
57 exit 0;
58 }
59 }
60
61 # Are we in a container? Check for /run/systemd/container or
62 # $container variable in pid 1's environment.
63 sub in_container {
64 return 1 if -e "/run/systemd/container";
65 my $res = 0;
66 if (my $fh = new FileHandle('/proc/1/environ', 'r')) {
67 local $/ = "\0";
68 $res = grep(/^container=/, <$fh>);
69 close($fh);
70 }
71 return $res;
72 }
73
74 # Are we in in a chroot? Compare root device and inode numbers with pid 1.
75 sub in_chroot {
76 my @my_root_st = stat('/');
77 my @pid1_root_st = stat('/proc/1/root');
78
79 return @my_root_st && @pid1_root_st &&
80 ($my_root_st[0] != $pid1_root_st[0] || $my_root_st[1] != $pid1_root_st[1]);
81 }
82
83 sub check {
84 my ($version) = @_;
85 my (undef, undef, $running, undef, undef) = POSIX::uname();
86
87 if ($running ne $version || in_chroot() || in_container()) {
88 exit 0;
89 }
90
91 if (!exists($ENV{'DEBIAN_FRONTEND'}) ||
92 $ENV{'DEBIAN_FRONTEND'} ne 'noninteractive') {
93 use Debconf::Client::ConfModule qw(:all);
94
95 my $title = 'linux-base/removing-title';
96 my $question = "linux-base/removing-running-kernel";
97 my ($ret, $seen, $answer);
98
99 # debconf only generates a title automatically when used directly
100 # from a maintainer script. Also, the automatic title says we are
101 # 'configuring' a package but we're not.
102 ($ret) = subst($title, 'package', $ENV{DPKG_MAINTSCRIPT_PACKAGE});
103 if ($ret == 10) {
104 # debconf only repopulates the template cache during
105 # package installation and dpkg-reconfigure (and then
106 # only for the package being configured). If the
107 # cache is removed then we won't be able to use our
108 # template (#908001). Treat this as non-fatal.
109 print STDERR "W: debconf template $title is uncached\n";
110 goto proceed;
111 }
112 if ($ret) {
113 die "Failed to substitute package name in title: $ret";
114 }
115 ($ret) = settitle($title);
116 if ($ret) {
117 die "Failed to set title: $ret";
118 }
119 ($ret) = fset($question, 'seen', 'false');
120 if ($ret == 10) {
121 print STDERR "W: debconf template $question is uncached\n";
122 goto proceed;
123 }
124 if ($ret) {
125 die "Failed to reset seen flag for $question: $ret";
126 }
127 ($ret) = reset($question);
128 if ($ret) {
129 die "Failed to reset answer for $question: $ret";
130 }
131 ($ret) = subst($question, 'running', $running);
132 if ($ret) {
133 die "Failed to substitute version in $question: $ret";
134 }
135 ($ret, $seen) = input('critical', $question);
136 if ($ret && $ret != 30) {
137 die "Failed to prepare question $question: $ret $seen";
138 }
139 ($ret, $seen) = go();
140 if ($ret && $ret != 30) {
141 die "Failed to ask question $question: $ret $seen";
142 }
143 ($ret, $answer) = get($question);
144 if ($ret) {
145 die "Failed to retrieve answer for $question: $ret $answer";
146 }
147
148 if ($answer eq 'true') {
149 print STDERR "E: Aborting removal of the running kernel\n";
150 exit 1;
151 }
152 }
153
154 proceed:
155 print STDERR "W: Removing the running kernel\n";
156 exit 0;
157 }
158
159 check(@ARGV);