I need to understand the meaning of the following code from /lib/lsb/init-functions
:
base=${1##*/}
Also it would be helpful if it was possible to explain how pidofproc
function returns the values to status_of_proc
.
EDIT:
pidofproc
pidofproc () {
local pidfile base status specified pid OPTIND
pidfile=
specified=
OPTIND=1
while getopts p: opt ; do
case "$opt" in
p) pidfile="$OPTARG"
specified="specified"
;;
esac
done
shift $(($OPTIND - 1))
if [ $# -ne 1 ]; then
echo "$0: invalid arguments" >&2
return 4
fi
base=${1##*/}
if [ ! "$specified" ]; then
pidfile="/var/run/$base.pid"
fi
if [ -n "${pidfile:-}" -a -r "$pidfile" ]; then
read pid < "$pidfile"
if [ -n "${pid:-}" ]; then
if $(kill -0 "${pid:-}" 2> /dev/null); then
echo "$pid" || true
return 0
elif ps "${pid:-}" >/dev/null 2>&1; then
echo "$pid" || true
return 0 # program is running, but not owned by this user
else
return 1 # program is dead and /var/run pid file exists
fi
fi
fi
if [ -n "$specified" ]; then
if [ -e "$pidfile" -a ! -r "$pidfile" ]; then
return 4 # pidfile exists, but unreadable, return unknown
else
return 3 # pidfile specified, but contains no PID to test
fi
fi
if [ -x /bin/pidof ]; then
status="0"
/bin/pidof -o %PPID -x $1 || status="$?"
if [ "$status" = 1 ]; then
return 3 # program is not running
fi
return 0
fi
return 4 # Unable to determine status
}
status_of_proc
status_of_proc () {
local pidfile daemon name status OPTIND
pidfile=
OPTIND=1
while getopts p: opt ; do
case "$opt" in
p) pidfile="$OPTARG";;
esac
done
shift $(($OPTIND - 1))
if [ -n "$pidfile" ]; then
pidfile="-p $pidfile"
fi
daemon="$1"
name="$2"
status="0"
pidofproc $pidfile $daemon >/dev/null || status="$?"
if [ "$status" = 0 ]; then
log_success_msg "$name is running"
return 0
elif [ "$status" = 4 ]; then
log_failure_msg "could not access PID file for $name"
return $status
else
log_failure_msg "$name is not running"
return $status
fi
}
$base
to whatever$1
's value is after all characters up to the last occurring/
char is stripped from its head. Can we have copies ofpidofproc
and/orstatus_of_proc
? – mikeserv Dec 19 '14 at 05:51base
will contain the filename, it seems. Thankyou – Jackzz Dec 19 '14 at 06:00$pidfile
- which is a lockfile, essentially, and they write into,read
from it. wait, no... status empties that var... just a sec. – mikeserv Dec 19 '14 at 06:05