#!/bin/bash

# This sshr-lock callback script checks the validity of the lock
# by testing whether a process with the supplied PID exists
# and its name corresponds to the supplied process name.
# The callback argument contains the PID and the process name
# separated with colon (eg. "12345:recoveryISO.sh").

RET_LOCK_VALID=0
RET_LOCK_INVALID=1

pid_and_pname="${1}"

# split the argument by first ':' character found
pid=${pid_and_pname%%:*}
pname=${pid_and_pname#*:}

ps_pname=`ps -p ${pid} -o comm --no-headers`
ps_exitcode=$?

if [ $ps_exitcode -eq 0 ] && [ "$ps_pname" = "$pname" ]; then
  # lock valid -> print '0' to stdout and return 0
  echo ${RET_LOCK_VALID}
  exit ${RET_LOCK_VALID}
fi

exit ${RET_LOCK_INVALID}
