From 4f11eccbf6879126d88db791bda0b392c219f88f Mon Sep 17 00:00:00 2001
From: Franck Bui <fbui@suse.com>
Date: Wed, 30 Sep 2020 21:20:32 +0200
Subject: [PATCH 1007/1007] Restore support for halt.local

This patch basically reverts the following commits:

  - commit 44508946534eee032927c263b79464832656dd6e.
    "Drop support for /usr/sbin/halt.local"

  - commit c0d11245d2bdcf2a4333c3d49c973d83dfbe3791.
    "Drop no longer needed halt-local.service.in"

The paths of the scripts in man/systemd-rc-local-generator.xml have been
changed to match those used by SUSE.

The use of halt.local is now deprecated and its usage is logged at notice
level.
---
 docs/DISTRO_PORTING.md                      |  1 +
 man/custom-entities.ent.in                  |  1 +
 man/systemd-rc-local-generator.xml          | 17 +++++++++++++---
 meson.build                                 |  2 ++
 meson_options.txt                           |  2 ++
 src/rc-local-generator/rc-local-generator.c |  8 ++++++++
 units/halt-local.service.in                 | 22 +++++++++++++++++++++
 units/meson.build                           |  1 +
 8 files changed, 51 insertions(+), 3 deletions(-)
 create mode 100644 units/halt-local.service.in

diff --git a/docs/DISTRO_PORTING.md b/docs/DISTRO_PORTING.md
index 2e4782f401..8c2af4f82f 100644
--- a/docs/DISTRO_PORTING.md
+++ b/docs/DISTRO_PORTING.md
@@ -17,6 +17,7 @@ distribution:
    * `-Dsysvinit-path=`
    * `-Dsysvrcnd-path=`
    * `-Drc-local=`
+   * `-Dhalt-local=`
    * `-Dloadkeys-path=`
    * `-Dsetfont-path=`
    * `-Dtty-gid=`
diff --git a/man/custom-entities.ent.in b/man/custom-entities.ent.in
index 76a3dace33..58416c9ee7 100644
--- a/man/custom-entities.ent.in
+++ b/man/custom-entities.ent.in
@@ -11,6 +11,7 @@
 <!ENTITY KILL_USER_PROCESSES "{{ 'yes' if KILL_USER_PROCESSES else 'no' }}">
 <!ENTITY DEBUGTTY "{{DEBUGTTY}}">
 <!ENTITY RC_LOCAL_PATH "{{RC_LOCAL_PATH}}">
+<!ENTITY RC_LOCAL_SCRIPT_PATH_STOP "{{RC_LOCAL_SCRIPT_PATH_STOP}}">
 <!ENTITY HIGH_RLIMIT_NOFILE "{{HIGH_RLIMIT_NOFILE}}">
 <!ENTITY fedora_latest_version "34">
 <!ENTITY fedora_cloud_release "1.2">
diff --git a/man/systemd-rc-local-generator.xml b/man/systemd-rc-local-generator.xml
index f0e38ead47..39c1fc9340 100644
--- a/man/systemd-rc-local-generator.xml
+++ b/man/systemd-rc-local-generator.xml
@@ -20,7 +20,7 @@
   <refnamediv>
     <refname>systemd-rc-local-generator</refname>
     <refname>rc-local.service</refname>
-    <refpurpose>Compatibility generator and service to start <filename>&RC_LOCAL_PATH;</filename> during boot</refpurpose>
+    <refpurpose>Compatibility generator and service to start <filename>&RC_LOCAL_PATH;</filename> and <filename>&RC_LOCAL_SCRIPT_PATH_STOP;</filename> during boot and shutdown</refpurpose>
   </refnamediv>
 
   <refsynopsisdiv>
@@ -53,11 +53,22 @@ Wants=network-online.target
 After=network-online.target
 </programlisting>
 
-    <para>Support for <filename>&RC_LOCAL_PATH;</filename> is provided for compatibility with specific System
-    V systems only. However, it is strongly recommended to avoid making use of this script today, and instead
+    <para><filename>systemd-rc-local-generator</filename> also checks whether
+    <filename>&RC_LOCAL_SCRIPT_PATH_STOP;</filename> exists and is executable, and if it is pulls the
+    <filename>halt-local.service</filename> unit into the shutdown process. This unit is responsible for
+    running this script during later shutdown.</para>
+
+    <para>Support for <filename>&RC_LOCAL_PATH;</filename> and
+    <filename>&RC_LOCAL_SCRIPT_PATH_STOP;</filename> is provided for compatibility with specific System V
+    systems only. However, it is strongly recommended to avoid making use of this script today, and instead
     provide proper unit files with appropriate dependencies for any scripts to run during the boot process.
     Note that the path to the script is set at compile time and varies between distributions.</para>
 
+    <para>Please note that the support for <filename>&RC_LOCAL_SCRIPT_PATH_STOP;</filename> will be removed in
+    the future. It is recommended to use the use the mechanism described in
+    <citerefentry><refentrytitle>systemd-shutdown</refentrytitle><manvolnum>8</manvolnum></citerefentry>
+    instead.</para>
+
     <para><filename>systemd-rc-local-generator</filename> implements
     <citerefentry><refentrytitle>systemd.generator</refentrytitle><manvolnum>7</manvolnum></citerefentry>.</para>
   </refsect1>
diff --git a/meson.build b/meson.build
index 3f31298b35..3319c35093 100644
--- a/meson.build
+++ b/meson.build
@@ -227,6 +227,7 @@ conf.set_quoted('PREFIX',                                     prefixdir)
 conf.set_quoted('RANDOM_SEED',                                join_paths(randomseeddir, 'random-seed'))
 conf.set_quoted('RANDOM_SEED_DIR',                            randomseeddir)
 conf.set_quoted('RC_LOCAL_PATH',                              get_option('rc-local'))
+conf.set_quoted('RC_LOCAL_SCRIPT_PATH_STOP',                  get_option('halt-local'))
 conf.set_quoted('ROOTBINDIR',                                 rootbindir)
 conf.set_quoted('ROOTLIBDIR',                                 rootlibdir)
 conf.set_quoted('ROOTLIBEXECDIR',                             rootlibexecdir)
@@ -3734,6 +3735,7 @@ status = [
         'bash completions directory:        @0@'.format(bashcompletiondir),
         'zsh completions directory:         @0@'.format(zshcompletiondir),
         'extra start script:                @0@'.format(get_option('rc-local')),
+        'extra stop script:                 @0@'.format(get_option('halt-local')),
         'debug shell:                       @0@ @ @1@'.format(get_option('debug-shell'),
                                                               get_option('debug-tty')),
         'system UIDs:                       <=@0@ (alloc >=@1@)'.format(conf.get('SYSTEM_UID_MAX'),
diff --git a/meson_options.txt b/meson_options.txt
index 163c8df87d..89a243e553 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -40,6 +40,8 @@ option('telinit-path', type : 'string', value : '/lib/sysvinit/telinit',
        description : 'path to telinit')
 option('rc-local', type : 'string',
        value : '/etc/rc.local')
+option('halt-local', type : 'string',
+       value : '/usr/sbin/halt.local')
 option('initrd', type : 'boolean',
        description : 'install services for use when running systemd in initrd')
 option('compat-mutable-uid-boundaries', type : 'boolean', value : 'false',
diff --git a/src/rc-local-generator/rc-local-generator.c b/src/rc-local-generator/rc-local-generator.c
index 99cffee3ec..96ee56854c 100644
--- a/src/rc-local-generator/rc-local-generator.c
+++ b/src/rc-local-generator/rc-local-generator.c
@@ -65,6 +65,14 @@ static int run(const char *dest, const char *dest_early, const char *dest_late)
                 r = add_symlink("rc-local.service", "multi-user.target");
         }
 
+        if (check_executable(RC_LOCAL_SCRIPT_PATH_STOP) >= 0) {
+                log_debug("Automatically adding halt-local.service.");
+                log_notice("Use of %s is deprecated, see systemd-shutdown(8) man page for an alternative.",
+                           RC_LOCAL_SCRIPT_PATH_STOP);
+
+                k = add_symlink("halt-local.service", "final.target");
+        }
+
         return r < 0 ? r : k;
 }
 
diff --git a/units/halt-local.service.in b/units/halt-local.service.in
new file mode 100644
index 0000000000..5fc78b5580
--- /dev/null
+++ b/units/halt-local.service.in
@@ -0,0 +1,22 @@
+#  SPDX-License-Identifier: LGPL-2.1+
+#
+#  This file is part of systemd.
+#
+#  systemd is free software; you can redistribute it and/or modify it
+#  under the terms of the GNU Lesser General Public License as published by
+#  the Free Software Foundation; either version 2.1 of the License, or
+#  (at your option) any later version.
+
+[Unit]
+Description={{RC_LOCAL_SCRIPT_PATH_STOP}} Compatibility
+ConditionFileIsExecutable={{RC_LOCAL_SCRIPT_PATH_STOP}}
+DefaultDependencies=no
+After=shutdown.target
+Before=final.target
+
+[Service]
+Type=oneshot
+ExecStart={{RC_LOCAL_SCRIPT_PATH_STOP}}
+TimeoutSec=0
+StandardOutput=tty
+RemainAfterExit=yes
diff --git a/units/meson.build b/units/meson.build
index 17e9ead9c1..976c1b0c57 100644
--- a/units/meson.build
+++ b/units/meson.build
@@ -175,6 +175,7 @@ in_units = [
          'sysinit.target.wants/'],
         ['quotaon.service',                      'ENABLE_QUOTACHECK'],
         ['rc-local.service',                     'HAVE_SYSV_COMPAT'],
+        ['halt-local.service',                   'HAVE_SYSV_COMPAT'],
         ['rescue.service',                       ''],
         ['serial-getty@.service',                ''],
         ['systemd-backlight@.service',           'ENABLE_BACKLIGHT'],
-- 
2.31.1

