From 8900311b7e5450082cace986533a093d37900efa Mon Sep 17 00:00:00 2001 From: "Fiona Klute (WIWA)" Date: Thu, 4 Jul 2024 12:16:29 +0200 Subject: [PATCH] package/openssh: manage sshd using start-stop-daemon The previously used "killall sshd" stopped all instances of sshd. With OpenSSH before 9.8 that meant not only the listening server, but also instances serving currently open sessions, possibly including the one used to send the restart command, preventing it from completing the "start" part of "restart" and leaving the system unreachable over SSH. start-stop-daemon uses the PID file to target only the intended process, and has built-in capability to check if it is running. This ensures any open SSH sessions are unaffected, as well as unrelated processes (in case a daemon crashed and the PID got reused). Signed-off-by: Fiona Klute (WIWA) Signed-off-by: Thomas Petazzoni --- .checkpackageignore | 2 +- package/openssh/S50sshd | 36 ++++++++++++++++++++++++++++-------- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/.checkpackageignore b/.checkpackageignore index d0061485fb..0f33ed0705 100644 --- a/.checkpackageignore +++ b/.checkpackageignore @@ -1103,7 +1103,7 @@ package/openrc/0003-init.d-agetty-replace-sbin-agetty-by-sbin-getty.patch lib_pa package/openrc/0004-init.d-agetty-start-agetty-after-all-sevices.patch lib_patch.Upstream package/openrc/0005-runlevels-do-not-add-agetty.tty-1-6-if-MKSYSVINIT-ye.patch lib_patch.Upstream package/openrc/0006-Also-create-run-lock-subsys-directory.patch lib_patch.Upstream -package/openssh/S50sshd lib_sysv.EmptyLastLine lib_sysv.Indent lib_sysv.Variables +package/openssh/S50sshd lib_sysv.Indent package/openswan/0001-lib-libopenswan-constants.c-workaround-missing-ns_t_.patch lib_patch.Upstream package/opentyrian/0001-Move-definitions-that-don-t-need-to-be-exposed-from-opl-h-to-opl-c.patch lib_patch.Upstream package/openvmtools/0001-no_cflags_werror.patch lib_patch.Upstream diff --git a/package/openssh/S50sshd b/package/openssh/S50sshd index 22da41d1ca..8f4fe10153 100644 --- a/package/openssh/S50sshd +++ b/package/openssh/S50sshd @@ -3,6 +3,9 @@ # sshd Starts sshd. # +DAEMON="sshd" +PIDFILE="/var/run/$DAEMON.pid" + # Make sure the ssh-keygen progam exists [ -f /usr/bin/ssh-keygen ] || exit 0 @@ -12,17 +15,35 @@ start() { # Create any missing keys /usr/bin/ssh-keygen -A - printf "Starting sshd: " - /usr/sbin/sshd - touch /var/lock/sshd - echo "OK" + printf "Starting %s: " "$DAEMON" + start-stop-daemon -S -q -p "$PIDFILE" \ + -x "/usr/sbin/$DAEMON" + status=$? + if [ "$status" -eq 0 ]; then + echo "OK" + else + echo "FAIL" + fi + return "$status" } + stop() { printf "Stopping sshd: " - killall sshd - rm -f /var/lock/sshd - echo "OK" + start-stop-daemon -K -q -p "$PIDFILE" \ + -x "/usr/sbin/$DAEMON" + status=$? + if [ "$status" -eq 0 ]; then + echo "OK" + else + echo "FAIL" + fi + # sshd deletes its PID file on exit, wait for it to be gone + while [ -f "$PIDFILE" ]; do + sleep 0.1 + done + return "$status" } + restart() { stop start @@ -44,4 +65,3 @@ case "$1" in esac exit $? -