c2dfb7
From 6e732b6e44ad8eb3e94c47459c64f0bc6ef2fcb0 Mon Sep 17 00:00:00 2001
c2dfb7
From: Anita Zhang <the.anitazha@gmail.com>
c2dfb7
Date: Sat, 25 Jan 2020 16:46:16 +0100
c2dfb7
Subject: [PATCH] core: transition to FINAL_SIGTERM state after ExecStopPost=
c2dfb7
c2dfb7
Fixes #14566
c2dfb7
c2dfb7
(cherry picked from commit c1566ef0d22ed786b9ecf4c476e53b8a91e67578)
c2dfb7
c2dfb7
Resolves: #1766479
c2dfb7
---
c2dfb7
 src/core/service.c                    | 10 +++++
c2dfb7
 test/TEST-47-ISSUE-14566/Makefile     |  1 +
c2dfb7
 test/TEST-47-ISSUE-14566/repro.sh     |  5 +++
c2dfb7
 test/TEST-47-ISSUE-14566/test.sh      | 55 +++++++++++++++++++++++++++
c2dfb7
 test/TEST-47-ISSUE-14566/testsuite.sh | 23 +++++++++++
c2dfb7
 5 files changed, 94 insertions(+)
c2dfb7
 create mode 120000 test/TEST-47-ISSUE-14566/Makefile
c2dfb7
 create mode 100755 test/TEST-47-ISSUE-14566/repro.sh
c2dfb7
 create mode 100755 test/TEST-47-ISSUE-14566/test.sh
c2dfb7
 create mode 100755 test/TEST-47-ISSUE-14566/testsuite.sh
c2dfb7
c2dfb7
diff --git a/src/core/service.c b/src/core/service.c
c2dfb7
index b1ec52d220..5035dcacac 100644
c2dfb7
--- a/src/core/service.c
c2dfb7
+++ b/src/core/service.c
c2dfb7
@@ -3280,6 +3280,12 @@ static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
c2dfb7
                                 break;
c2dfb7
 
c2dfb7
                         case SERVICE_STOP_POST:
c2dfb7
+
c2dfb7
+                                if (control_pid_good(s) <= 0)
c2dfb7
+                                        service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
c2dfb7
+
c2dfb7
+                                break;
c2dfb7
+
c2dfb7
                         case SERVICE_FINAL_SIGTERM:
c2dfb7
                         case SERVICE_FINAL_SIGKILL:
c2dfb7
 
c2dfb7
@@ -3415,6 +3421,10 @@ static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
c2dfb7
                                 break;
c2dfb7
 
c2dfb7
                         case SERVICE_STOP_POST:
c2dfb7
+                                if (main_pid_good(s) <= 0)
c2dfb7
+                                        service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
c2dfb7
+                                break;
c2dfb7
+
c2dfb7
                         case SERVICE_FINAL_SIGTERM:
c2dfb7
                         case SERVICE_FINAL_SIGKILL:
c2dfb7
                                 if (main_pid_good(s) <= 0)
c2dfb7
diff --git a/test/TEST-47-ISSUE-14566/Makefile b/test/TEST-47-ISSUE-14566/Makefile
c2dfb7
new file mode 120000
c2dfb7
index 0000000000..e9f93b1104
c2dfb7
--- /dev/null
c2dfb7
+++ b/test/TEST-47-ISSUE-14566/Makefile
c2dfb7
@@ -0,0 +1 @@
c2dfb7
+../TEST-01-BASIC/Makefile
c2dfb7
\ No newline at end of file
c2dfb7
diff --git a/test/TEST-47-ISSUE-14566/repro.sh b/test/TEST-47-ISSUE-14566/repro.sh
c2dfb7
new file mode 100755
c2dfb7
index 0000000000..5217602257
c2dfb7
--- /dev/null
c2dfb7
+++ b/test/TEST-47-ISSUE-14566/repro.sh
c2dfb7
@@ -0,0 +1,5 @@
c2dfb7
+#!/bin/bash
c2dfb7
+
c2dfb7
+sleep infinity &
c2dfb7
+echo $! > /leakedtestpid
c2dfb7
+wait $!
c2dfb7
diff --git a/test/TEST-47-ISSUE-14566/test.sh b/test/TEST-47-ISSUE-14566/test.sh
c2dfb7
new file mode 100755
c2dfb7
index 0000000000..0ce772164a
c2dfb7
--- /dev/null
c2dfb7
+++ b/test/TEST-47-ISSUE-14566/test.sh
c2dfb7
@@ -0,0 +1,55 @@
c2dfb7
+#!/bin/bash
c2dfb7
+set -e
c2dfb7
+TEST_DESCRIPTION="Test that KillMode=mixed does not leave left over proccesses with ExecStopPost="
c2dfb7
+. $TEST_BASE_DIR/test-functions
c2dfb7
+
c2dfb7
+test_setup() {
c2dfb7
+    create_empty_image
c2dfb7
+    mkdir -p $TESTDIR/root
c2dfb7
+    mount ${LOOPDEV}p1 $TESTDIR/root
c2dfb7
+
c2dfb7
+    (
c2dfb7
+        LOG_LEVEL=5
c2dfb7
+        eval $(udevadm info --export --query=env --name=${LOOPDEV}p2)
c2dfb7
+
c2dfb7
+        setup_basic_environment
c2dfb7
+
c2dfb7
+        # mask some services that we do not want to run in these tests
c2dfb7
+        ln -fs /dev/null $initdir/etc/systemd/system/systemd-hwdb-update.service
c2dfb7
+        ln -fs /dev/null $initdir/etc/systemd/system/systemd-journal-catalog-update.service
c2dfb7
+        ln -fs /dev/null $initdir/etc/systemd/system/systemd-networkd.service
c2dfb7
+        ln -fs /dev/null $initdir/etc/systemd/system/systemd-networkd.socket
c2dfb7
+        ln -fs /dev/null $initdir/etc/systemd/system/systemd-resolved.service
c2dfb7
+        ln -fs /dev/null $initdir/etc/systemd/system/systemd-machined.service
c2dfb7
+
c2dfb7
+        # setup the testsuite service
c2dfb7
+        cat >$initdir/etc/systemd/system/testsuite.service <
c2dfb7
+[Unit]
c2dfb7
+Description=Testsuite service
c2dfb7
+
c2dfb7
+[Service]
c2dfb7
+ExecStart=/testsuite.sh
c2dfb7
+Type=oneshot
c2dfb7
+EOF
c2dfb7
+        cat > $initdir/etc/systemd/system/issue_14566_test.service << EOF
c2dfb7
+[Unit]
c2dfb7
+Description=Issue 14566 Repro
c2dfb7
+
c2dfb7
+[Service]
c2dfb7
+ExecStart=/repro.sh
c2dfb7
+ExecStopPost=/bin/true
c2dfb7
+KillMode=mixed
c2dfb7
+EOF
c2dfb7
+
c2dfb7
+        cp testsuite.sh $initdir/
c2dfb7
+        cp repro.sh $initdir/
c2dfb7
+
c2dfb7
+        setup_testsuite
c2dfb7
+    )
c2dfb7
+    setup_nspawn_root
c2dfb7
+
c2dfb7
+    ddebug "umount $TESTDIR/root"
c2dfb7
+    umount $TESTDIR/root
c2dfb7
+}
c2dfb7
+
c2dfb7
+do_test "$@"
c2dfb7
diff --git a/test/TEST-47-ISSUE-14566/testsuite.sh b/test/TEST-47-ISSUE-14566/testsuite.sh
c2dfb7
new file mode 100755
c2dfb7
index 0000000000..d917cf52ff
c2dfb7
--- /dev/null
c2dfb7
+++ b/test/TEST-47-ISSUE-14566/testsuite.sh
c2dfb7
@@ -0,0 +1,23 @@
c2dfb7
+#!/bin/bash
c2dfb7
+set -ex
c2dfb7
+set -o pipefail
c2dfb7
+
c2dfb7
+systemd-analyze log-level debug
c2dfb7
+systemd-analyze log-target console
c2dfb7
+
c2dfb7
+systemctl start issue_14566_test
c2dfb7
+systemctl status issue_14566_test
c2dfb7
+
c2dfb7
+leaked_pid=$(cat /leakedtestpid)
c2dfb7
+
c2dfb7
+systemctl stop issue_14566_test
c2dfb7
+
c2dfb7
+# Leaked PID will still be around if we're buggy.
c2dfb7
+# I personally prefer to see 42.
c2dfb7
+ps -p "$leaked_pid" && exit 42
c2dfb7
+
c2dfb7
+systemd-analyze log-level info
c2dfb7
+
c2dfb7
+echo OK > /testok
c2dfb7
+
c2dfb7
+exit 0