Fix the proc_kill ocf helper func

W/o this fix, the command "proc_kill pid name count" will misplace
the passed count of tries as the signal name for the pkill, which
is wrong and might be unpredictably harmful for the proces group
containing the given pid.

The fix is to correctly specify params for the proc_kill calls.
Add bats tests for the proc_kill as well.

Closes-bug: #1528889

Change-Id: I992480f4e0d3380215d3f8bd910553070334c343
Signed-off-by: Bogdan Dobrelya <bdobrelia@mirantis.com>
This commit is contained in:
Bogdan Dobrelya 2015-12-28 18:06:38 +01:00
parent 12646476cb
commit d961691e5f
2 changed files with 51 additions and 1 deletions

View File

@ -127,7 +127,7 @@ proc_stop()
if [ -n "${pid}" ]; then
ocf_log info "${LH} Stopping ${service_name}"
proc_kill "${pid}" "${service_name}" $stop_count
proc_kill "${pid}" "${service_name}" SIGTERM $stop_count
if [ $? -ne 0 ]; then
# SIGTERM failed, send a single SIGKILL
proc_kill "${pid}" "${service_name}" SIGKILL 1 2

50
tests/bats/ocf-fuel-funcs Executable file
View File

@ -0,0 +1,50 @@
#!/usr/bin/env bats
# For testing use bats framework -
# https://github.com/sstephenson/bats
fms="$BATS_TEST_DIRNAME/../../files/fuel-ha-utils/ocf/ocf-fuel-funcs"
load "$fms"
@test "Check proc_kill params" {
# $1 - pid of the process to try and kill
# $2 - service name used for logging
# $3 - signal to use, defaults to SIGTERM
# $4 - number of retries, defaults to 5
# $5 - time to sleep between retries, defaults to 2
ocf_log(){
true
}
ocf_run(){
echo $@
}
sleep(){
true
}
pid=$$
pgrp=$(ps -o pgid= ${pid} | tr -d '[[:space:]]')
echo "When used with default params"
expected="pkill -SIGTERM -g $pgrp"
run proc_kill $pid
echo -e "Actual:\n${output}"
for l in {0..4}; do
echo "Expected line $l: ${expected}"
[ "${lines[$l]}" = "${expected}" ]
done
echo "When used with correct params"
expected="pkill -SIGSTOP -g $pgrp"
run proc_kill $pid foo SIGSTOP 2
echo "Expected line 1: ${expected}"
echo -e "Actual:\n${output}"
[ "${lines[1]}" = "${expected}" ]
echo "When misused with wrong params"
expected="pkill -1 -g $pgrp"
echo "Expected line 4: ${expected}"
run proc_kill $pid foo 1
echo "Actual: ${output}"
[ "${lines[4]}" = "${expected}" ]
}