O que faz / lib / lsb / init-functions?

3

Eu preciso entender o significado do código a seguir de /lib/lsb/init-functions :

 base=${1##*/}

Além disso, seria útil se fosse possível explicar como pidofproc function retorna os valores para status_of_proc .

EDITAR:

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
}
    
por Jackzz 19.12.2014 / 06:46

1 resposta

2

A resposta está aqui:

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

Portanto, status_of_proc chama pidofproc , que define $base . Esse valor variável é definido no shell atual e, portanto, seu valor persiste quando pidofproc retorna para status_of_proc .

Por exemplo:

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

OUTPUT

set

A seguir, [ test ] comando pidofproc avalia $pidfile e retorna com base em seus resultados:

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

Então, isso se traduz em:

if $pidfile exists and it is not readable

O texto completo está aqui:

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
    
por 19.12.2014 / 07:11