>From f00d91c9f8f91ceb8fdc4ade4b33768624c91ae7 Mon Sep 17 00:00:00 2001
From: Mark Hindley <mark@hindley.org.uk>
Date: Sun, 22 Nov 2026 08:10:49 +0000
Subject: [PATCH 3/6] update-rc.d: always manage openrc symlinks.

---
 debian/init-system-helpers.dirs |  1 +
 script/update-rc.d              | 49 +++++++++++++++++++++++----------
 2 files changed, 35 insertions(+), 15 deletions(-)

diff --git a/debian/init-system-helpers.dirs b/debian/init-system-helpers.dirs
index 4f65e8a..f174d2a 100644
--- a/debian/init-system-helpers.dirs
+++ b/debian/init-system-helpers.dirs
@@ -9,3 +9,4 @@
 /etc/systemd/system
 /etc/systemd/user
 /var/lib/systemd
+/etc/runlevels
diff --git a/script/update-rc.d b/script/update-rc.d
index 733d0a5..9f2b6c7 100755
--- a/script/update-rc.d
+++ b/script/update-rc.d
@@ -58,7 +58,7 @@ sub make_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.
+# 0 or 6. Return 0 if no runlevels found.
 sub script_runlevels {
     my ($scriptname) = @_;
     my @links=<"$dpkg_root/etc/rc[S12345].d/S[0-9][0-9]$scriptname">;
@@ -72,6 +72,17 @@ sub script_runlevels {
     }
 }
 
+# Return full path to first RC_PATH directory containing suitable script.
+sub openrc_script {
+    my ($scriptname) = @_;
+    my @rc_path= ("$dpkg_root/etc/rc/init.d",
+		  "$dpkg_root/usr/lib/rc/init.d",
+		  "$dpkg_root/etc/init.d");
+    (my $scriptpath) = grep { -x "$_/$scriptname" } @rc_path;
+    warning("No suitable openrc $scriptname found") unless $scriptpath;
+    return "$scriptpath/$scriptname" if $scriptpath;
+}
+
 # Map the sysvinit runlevel to that of openrc.
 sub openrc_rlconv {
     my %rl_table = (
@@ -98,7 +109,7 @@ sub systemd_reload {
 }
 
 # Creates the necessary links to enable/disable a SysV init script (fallback if
-# no insserv/rc-update exists)
+# no insserv exists)
 sub make_sysv_links {
     my ($scriptname, $action) = @_;
 
@@ -209,8 +220,6 @@ sub create_sequence {
             '--config',      "$dpkg_root/etc/insserv.conf" );
     }
 
-    my $openrc_installed = -x "$dpkg_root/sbin/openrc";
-
     my $sysv_insserv ={};
     $sysv_insserv->{remove} = sub {
         my ($scriptname) = @_;
@@ -290,8 +299,10 @@ sub create_sequence {
     my $openrc = {};
     $openrc->{remove} = sub {
         my ($scriptname) = @_;
-        system("rc-update", "-qqa", "delete", $scriptname);
-
+        for (glob("$dpkg_root/etc/runlevels/*/$scriptname")) {
+	    next unless $force or ! readlink;
+	    unlink($_);
+	}
     };
     $openrc->{defaults} = sub {
         my ($scriptname) = @_;
@@ -300,10 +311,16 @@ sub create_sequence {
         return if ("halt" eq $scriptname || "reboot" eq $scriptname);
         # no need to consider default disabled runlevels
         # because everything is disabled by openrc by default
-        my @rls=script_runlevels($scriptname);
-        if ( @rls ) {
-            system("rc-update", "add", $scriptname, openrc_rlconv(@rls));
+	my @rls=script_runlevels($scriptname);
+	my $orl;
+        if ( @rls = (0) ) {
+	    # Native script
+	    $orl = 'default';
+	} else {
+	    $orl = openrc_rlconv(@rls);
         }
+	make_path("$dpkg_root/etc/runlevels/$orl");
+	symlink(openrc_script($scriptname), "$dpkg_root/etc/runlevels/$orl/$scriptname");
     };
     $openrc->{defaults_disabled} = sub {
         # In openrc everything is disabled by default
@@ -324,9 +341,13 @@ sub create_sequence {
                 error("$scriptname Default-Start contains no runlevels, aborting.");
             }
         }
-        my %openrc_act = ( "disable" => "del", "enable" => "add" );
-        system("rc-update", $openrc_act{$action}, $scriptname,
-               openrc_rlconv(@toggle_lvls))
+	if ($action eq 'disable') {
+	    unlink("$dpkg_root/etc/runlevels/$_/$scriptname") for openrc_rlconv(@toggle_lvls);
+	} elsif ($action eq 'enable') {
+	    symlink(openrc_script($scriptname), "$dpkg_root/etc/runlevels/$_/$scriptname") for openrc_rlconv(@toggle_lvls);
+	} else {
+	    error("Unexpected action: $action");
+	}
     };
 
     my @sequence;
@@ -338,9 +359,7 @@ sub create_sequence {
     }
     # OpenRC has to be after sysv_{insserv,plain} because it depends on them to synchronize
     # states.
-    if ($openrc_installed) {
-        push @sequence, $openrc;
-    }
+    push @sequence, $openrc;
     push @sequence, $systemd;
 
     return @sequence;
-- 
2.53.0

