Frederico M.
5/5
#! /usr/bin/perl
# vim: ft=perl
#
# update-rc.d Update the links in /etc/rc[0-9S].d/
#
use strict;
use warnings;
# NB: All Perl modules used here must be in perl-base. Specifically, depending
# on modules in perl-modules is not okay! See bug #716923
my $initd = "/etc/init.d";
my $etcd = "/etc/rc";
my $dpkg_root = $ENV{DPKG_ROOT} // '';
# Print usage message and die.
sub usage {
print STDERR "update-rc.d: error: @_\n" if ($#_ >= 0);
print STDERR < remove
update-rc.d [-f] defaults
update-rc.d [-f] defaults-disabled
update-rc.d disable|enable [S|2|3|4|5]
-f: force
The disable|enable API is not stable and might change in the future.
EOF
exit (1);
}
exit main(@ARGV);
sub info {
print STDOUT "update-rc.d: @_\n";
}
sub warning {
print STDERR "update-rc.d: warning: @_\n";
}
sub error {
print STDERR "update-rc.d: error: @_\n";
exit (1);
}
sub error_code {
my $rc = shift;
print STDERR "update-rc.d: error: @_\n";
exit ($rc);
}
sub make_path {
my ($path) = @_;
my @dirs = ();
my @path = split /\//, $path;
map { push @dirs, $_; mkdir join('/', @dirs), 0755; } @path;
}
# Given a script name, return any runlevels except 0 or 6 in which the
# script is enabled. If that gives nothing and the script is not
# explicitly disabled, return 6 if the script is disabled in runlevel
# 0 or 6.
sub script_runlevels {
my ($scriptname) = @_;
my @links=<"$dpkg_root/etc/rc[S12345].d/S[0-9][0-9]$scriptname">;
if (@links) {
return map(substr($_, 7, 1), @links);
} elsif (! <"$dpkg_root/etc/rc[S12345].d/K[0-9][0-9]$scriptname">) {
@links=<"$dpkg_root/etc/rc[06].d/K[0-9][0-9]$scriptname">;
return ("6") if (@links);
} else {
return ;
}
}
# Map the sysvinit runlevel to that of openrc.
sub openrc_rlconv {
my %rl_table = (
"S" => "sysinit",
"1" => "recovery",
"2" => "default",
"3" => "default",
"4" => "default",
"5" => "default",
"6" => "off" );
my %seen; # return unique runlevels
return grep !$seen{$_}++, map($rl_table{$_}, @_);
}
sub systemd_reload {
if (length $ENV{DPKG_ROOT}) {
# if we operate on a chroot from the outside, do not attempt to reload
return;
}
if (-d "/run/systemd/system") {
system("systemctl", "daemon-reload");
}
}
# Creates the necessary links to enable/disable a SysV init script (fallback if
# no insserv/rc-update exists)
sub make_sysv_links {
my ($scriptname, $action) = @_;
# for "remove" we cannot rely on the init script still being present, as
# this gets called in postrm for purging. Just remove all symlinks.
if ("remove" eq $action) { unlink($_) for
glob("$dpkg_root/etc/rc?.d/[SK][0-9][0-9]$scriptname"); return; }
# if the service already has any links, do not touch them
# numbers we don't care about, but enabled/disabled state we do
return if glob("$dpkg_root/etc/rc?.d/[SK][0-9][0-9]$scriptname");
# for "defaults", parse Default-{Start,Stop} and create these links
my ($lsb_start_ref, $lsb_stop_ref) = parse_def_start_stop("$dpkg_root/etc/init.d/$scriptname");
my $start = $action eq "defaults-disabled" ? "K" : "S";
foreach my $lvl (@$lsb_start_ref) {
make_path("$dpkg_root/etc/rc$lvl.d");
my $l = "$dpkg_root/etc/rc$lvl.d/${start}01$scriptname";
symlink("../init.d/$scriptname", $l);
}
foreach my $lvl (@$lsb_stop_ref) {
make_path("$dpkg_root/etc/rc$lvl.d");
my $l = "$dpkg_root/etc/rc$lvl.d/K01$scriptname";
symlink("../init.d/$scriptname", $l);
}
}
# Creates the necessary links to enable/disable the service (equivalent of an
# initscript) in systemd.
sub make_systemd_links {
my ($scriptname, $action) = @_;
# If called by systemctl (via systemd-sysv-install), do nothing to avoid
# an endless loop.
if (defined($ENV{_SKIP_SYSTEMD_NATIVE}) && $ENV{_SKIP_SYSTEMD_NATIVE} == 1) {
return;
}
# If syst