3

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
}
Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
Jackzz
  • 1,403
  • 3
  • 16
  • 21
  • maybe see this... but more directly, that sets the shell variable $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 of pidofproc and/or status_of_proc? – mikeserv Dec 19 '14 at 05:51
  • @mikeserv: So base will contain the filename, it seems. Thankyou – Jackzz Dec 19 '14 at 06:00
  • you're quite welcome. if you're still curious about p&p then please paste in their contents. – mikeserv Dec 19 '14 at 06:03
  • @mikeserv: I have posted the function body. Also I am not very familiar with shell scripting. – Jackzz Dec 19 '14 at 06:05
  • The two functions share the shell environment var $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
  • yes. But I cant find any pidfile that is unreadable or that doesnot contain info about pid. So how come it returns any value other than 0? – Jackzz Dec 19 '14 at 06:08
  • @mikeserv Beware that using the term "lockfile" risks sowing confusion. In the init daemon context, that term is used for this purpose: '"/var/lock/subsys/${lock_file}". The shell variable $pidfile is used to hold the name of the file that identifies one or more process id numbers associated with the daemon, while the daemon is running. Name is: /var/run/mydaemon.pid. Contents is typically just a single number (like 23232) but it can be multiple lines and multiple numbers per line. (Though, honestly, I don't know what sort of daemon would create such a multi-value pidfile). – IAM_AL_X Apr 25 '20 at 18:38

1 Answers1

3

The answer is here:

status="0"
pidofproc $pidfile $daemon >/dev/null || status="$?"

So status_of_proc calls pidofproc which sets $base. This variable value is set in the current shell and so its value persists when pidofproc returns to status_of_proc.

For example:

fn1() { unset var; fn2; echo "$var"; }
fn2() { var=set; }
fn1

OUTPUT

set

In the following [ test ] command pidofproc evaluates $pidfile and returns based on its results:

[ -e "$pidfile" -a ! -r "$pidfile" ]

So that translates to:

if $pidfile exists and it is not readable

The full text is here:

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
mikeserv
  • 58,310
  • I understood that. But what I would like to know is: in \var\run\ there is no .pid file that is either empty or unreadable. Then how does procofpid returns 1,3 or 4? Also for one of the daemons that has a readable pidfile, it returns 4. How will this happen? – Jackzz Dec 19 '14 at 06:15
  • $pidfile is set to: pidfile="/var/run/$base.pid" – mikeserv Dec 19 '14 at 06:22
  • Yes. I parsed all files in that folder that has .pid extension. – Jackzz Dec 19 '14 at 06:25
  • @Anzz - look again maybe? Closer to the mark, maybe? You might get 4 even with the readable $pidfile if for some reason the following does not execute correctly: /bin/pidof -o %PPID -x $1 || status="$?" – mikeserv Dec 19 '14 at 06:28
  • Sorry for repeated questions. But can you explain what that line does? – Jackzz Dec 19 '14 at 06:31
  • ok.. Now the script seems somewhat readable for me. :) – Jackzz Dec 19 '14 at 06:33
  • @Anzz - first you wanna find out if you have the command - do hash pidof. I almost never use it and so am not clear on what the -o option does, but I assume it has something to do with the PPID. Still, that is not a shell var - that must be some pidof syntax. Anyway, that calls pidof - probably to get the PID of whatever is in "$1". If that command is successful it does not set $status to 1. – mikeserv Dec 19 '14 at 06:35
  • 1
    ok..Thankyou. Now let me check it again with all these info. Thanks a lot.. – Jackzz Dec 19 '14 at 06:37