--- a/etc/init.d/samba-ad-dc 2023-07-17 11:16:30.000000000 +0200 +++ b/etc/init.d/samba-ad-dc 2023-12-14 12:44:32.176000000 +0100 @@ -77,30 +77,36 @@ log_daemon_msg "Stopping $DESC" $NAME ## sometimes samba takes a long time to terminate, ## which would make starting new samba processes fail. - start-stop-daemon --stop --quiet --pidfile $PIDFILE \ - --name samba --retry 'TERM/15/KILL/1' -v \ - | sed -rn 's/.*, (retry #|refused to die)/\1/p' \ - | while read line; do log_action_cont_msg "$line"; done - ret="$?" - ## check for smbd and winbindd as well, in case ADDS has just been configured - for service in smbd winbindd; do - pid=$(pgrep -x "$service") - if [ -n "$pid" ]; then - start-stop-daemon --stop --quiet --oknodo \ - --name "$service" --retry "TERM/3/KILL/1" -v \ - | sed -rn 's/.*, (retry #|refused to die)/\1/p' \ - | while read line; do log_action_cont_msg "$line"; done + pids="$(pgrep --exact samba$; pgrep --exact smbd$; pgrep --exact winbindd$)" + start-stop-daemon --stop --quiet --pidfile $PIDFILE --name samba + ret=0 + TERM_TIMEOUT=60 + KILL_TIMEOUT=10 + if [ -n "$pids" ]; then + unset pgids kgids + while read pid; do + pgids="$pgids -g $pid" + kgids="$kgids -${pid# }" + done <<-_EOF_ + $pids + _EOF_ + while [ $TERM_TIMEOUT -gt 0 ]; do + [ -z "$(ps -o pid= $pgids)" ] && break + sleep 1 + TERM_TIMEOUT=$((TERM_TIMEOUT-1)) + done + if [ $TERM_TIMEOUT -eq 0 ]; then + log_action_msg "Samba did not terminate in time. Killing remaining processes" + ps -Ho state,pid,pgrp,time,args $pgids \ + | while read line; do log_action_msg "$line"; done + kill -9 $kgids 2>/dev/null + while [ $KILL_TIMEOUT -gt 0 ]; do + [ -z "$(ps -o pid= $pgids)" ] && break + sleep 1 + KILL_TIMEOUT=$((KILL_TIMEOUT-1)) + done + [ $KILL_TIMEOUT -eq 0 ] && ret=1 fi - done - ## Check again for /usr/sbin/samba - pgrep_output="$(pgrep -f /usr/sbin/samba)" - if [ -n "$pgrep_output" ]; then - { - echo -e "ERROR: Stuck process after service stop:\n$pgrep_output" - echo "PIDFILE: $(<$PIDFILE)" - samba-tool processes - } | logger -p daemon.error -t SAMBA - pkill -9 -f /usr/sbin/samba fi pkill samba-dcerpcd pkill samba-bgqd