| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
# Copyright 2025 Ben Hutchings |
| 4 |
# |
| 5 |
# This program is free software; you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation; either version 2 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# This program is distributed in the hope that it will be useful, |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
use strict; |
| 19 |
use warnings; |
| 20 |
|
| 21 |
sub usage { |
| 22 |
my $fh = shift; |
| 23 |
print $fh (<< "EOT"); |
| 24 |
Usage: $0 image MAINT-SCRIPT KERNEL-VER IMAGE-PATH -- MAINT-PARAMS ... |
| 25 |
$0 headers MAINT-SCRIPT KERNEL-VER -- MAINT-PARAMS ... |
| 26 |
|
| 27 |
This command is intended to be called from the maintainer scripts of |
| 28 |
Linux kernel image and headers packages. It executes hooks installed |
| 29 |
in the appropriate subdirectories of /etc/kernel and |
| 30 |
/usr/share/kernel. |
| 31 |
|
| 32 |
The MAINT-SCRIPT argument must be the name of the maintainer script: |
| 33 |
preinst, postinst, prerm, or postrm. |
| 34 |
|
| 35 |
The KERNEL-VER argument must be the kernel version string as shown by |
| 36 |
'uname -r' and used in filenames, not the package version. |
| 37 |
|
| 38 |
The IMAGE-PATH argument must be the absolute filename of the kernel |
| 39 |
image. |
| 40 |
|
| 41 |
The MAINT-PARAMS arguments must be the parameters received by the |
| 42 |
maintainer script. |
| 43 |
EOT |
| 44 |
} |
| 45 |
|
| 46 |
sub usage_error { |
| 47 |
usage(*STDERR{IO}); |
| 48 |
exit 2; |
| 49 |
} |
| 50 |
|
| 51 |
sub run_hooks { |
| 52 |
my ($type, $hook_args, $maint_params) = @_; |
| 53 |
my @hook_dirs; |
| 54 |
|
| 55 |
# run-parts handling of directory arguments differs between |
| 56 |
# versions of debianutils: |
| 57 |
# |
| 58 |
# <= 5.20: must pass exactly 1 existing directory name |
| 59 |
# == 5.21: must pass 1 or more existing directory names |
| 60 |
# >= 5.22: must pass 1 or more directory names; warning |
| 61 |
# emitted for non-existent directories |
| 62 |
# |
| 63 |
# So, to be backward-compatible, we only pass directories that |
| 64 |
# exist and do not run it at all if none exist. We rely on |
| 65 |
# packages not to install hooks under /usr/share/kernel without a |
| 66 |
# dependency or other guarantee that debianutils is >= 5.21. |
| 67 |
for my $dir ("/etc/kernel/$type.d", |
| 68 |
"/usr/share/kernel/$type.d") { |
| 69 |
push @hook_dirs, $dir if -d $dir; |
| 70 |
} |
| 71 |
if (@hook_dirs == 0) { |
| 72 |
exit 0; |
| 73 |
} |
| 74 |
|
| 75 |
$ENV{'DEB_MAINT_PARAMS'} = join(' ', @$maint_params); |
| 76 |
exec 'run-parts', '--report', '--exit-on-error', |
| 77 |
map({"--arg=$_"} @$hook_args), |
| 78 |
@hook_dirs; |
| 79 |
|
| 80 |
# run-parts could not be executed. exec already reported an error |
| 81 |
# message, so just return failure. |
| 82 |
exit 1; |
| 83 |
} |
| 84 |
|
| 85 |
if (@ARGV == 0) { |
| 86 |
usage_error(); |
| 87 |
} |
| 88 |
if ($ARGV[0] eq 'help' or grep({$_ eq '--help'} @ARGV)) { |
| 89 |
usage(*STDOUT{IO}); |
| 90 |
exit 0; |
| 91 |
} |
| 92 |
|
| 93 |
my ($pkg_type, $script_type) = @ARGV; |
| 94 |
if (!defined($pkg_type) |
| 95 |
or !defined($script_type) |
| 96 |
or ($script_type !~ /^(?:pre|post)(?:inst|rm)$/)) { |
| 97 |
usage_error(); |
| 98 |
} |
| 99 |
my ($hook_type, $n_hook_args); |
| 100 |
if ($pkg_type eq 'image') { |
| 101 |
$hook_type = $script_type; |
| 102 |
$n_hook_args = 2; |
| 103 |
} elsif ($pkg_type eq 'headers') { |
| 104 |
# For backward compatibility, hook directory names start with |
| 105 |
# 'header_' not 'headers_' |
| 106 |
$hook_type = "header_${script_type}"; |
| 107 |
$n_hook_args = 1; |
| 108 |
} else { |
| 109 |
usage_error(); |
| 110 |
} |
| 111 |
my ($arg_sep_index) = grep({ $ARGV[$_] eq '--' } 2..$#ARGV); |
| 112 |
if (!defined($arg_sep_index) |
| 113 |
or $arg_sep_index != 2 + $n_hook_args) { |
| 114 |
usage_error(); |
| 115 |
} |
| 116 |
|
| 117 |
run_hooks($hook_type, |
| 118 |
[@ARGV[2 .. $arg_sep_index-1]], |
| 119 |
[@ARGV[$arg_sep_index+1 .. $#ARGV]]); |