blob: 796fed01a5c460bba2643793fd3861d8e92a55c4 [file] [log] [blame]
#!/bin/sh
# Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# Tries to cleanly terminate a process and waits for it to exit.
# If that fails it will kill the process.
term_process() {
local process="$1"
pkill -o "$process"
for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15; do
pgrep "$process"
if [ $? -ne 0 ] ; then
return
fi
sleep .1
done
pkill -KILL "$process"
}
# For a given mountpoint, this will kill all processes with open files
# on that mountpoint so that it can be unmounted. It starts off by sending
# a TERM and if the process hasn't exited quickly enough it will send KILL.
#
# Since a typical shutdown should have no processes with open files on a
# partition that we care about at this point, we log the set of processes
# to /var/log/shutdown_force_kill_processes
kill_with_open_files_on() {
PIDS=$(lsof -t $@ | sort -n | uniq)
if [ -z "$PIDS" ] ; then
return # The typical case; no open files at this point.
fi
# PIDS should have been empty. Since it is not, we log for future inspection.
lsof $@ > /var/log/shutdown_force_kill_processes
# First try a gentle kill -TERM
for i in 1 2 3 4 5 6 7 8 9 10; do
for pid in $PIDS ; do
! kill -TERM $pid
done
PIDS=$(lsof -t $@ | sort -n | uniq)
if [ -z "$PIDS" ] ; then
return
fi
sleep .1
done
# Now kill -KILL as necessary
PIDS=$(lsof -t $@ | sort -n | uniq)
for i in 1 2 3 4 5 6 7 8 9 10; do
for pid in $PIDS ; do
! kill -KILL $pid
done
PIDS=$(lsof -t $@ | sort -n | uniq)
if [ -z "$PIDS" ] ; then
return
fi
sleep .1
done
}