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) <fiona.klute@gmx.de>
Signed-off-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
This commit is contained in:
Fiona Klute (WIWA)
2024-07-08 23:49:01 +02:00
committed by Thomas Petazzoni
parent cb25b54c2d
commit 8900311b7e
2 changed files with 29 additions and 9 deletions
+1 -1
View File
@@ -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
+28 -8
View File
@@ -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 $?