ryantimwilson / rpms / systemd

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