
___x_cmd_service_status(){
    while [ $# -gt 0 ]; do
        case "$1" in
            -h|--help)      ___x_cmd help -m service status "$@" ; return 0 ;;
            *)              break
        esac
    done

    local name="$1"

    local x_
    ___x_cmd_service_status_ "$name" || return $?
    printf "%s\n" "$x_"
}

___x_cmd_service_status_(){
    local name="$1"
    [ -n "$name" ] ||   N=service M="Please provide service name." arg:ret:64

    local x_pid=""
    local x_timestamp=""

    local pidpath="$___X_CMD_SERVICE_FP_DATA/$name/PID"

    [ -f "$pidpath" ] || {
        x_=notfound
        return 0
    }

    {
        read -r x_pid
        read -r x_timestamp
    } <"$pidpath"

    # On Cygwin/Windows, exec changes the process starttime (a Cygwin quirk where
    # exec is implemented via fork+exec), so the fingerprint captured at launch
    # time never matches the one seen at check time, even for a perfectly healthy
    # process. Fall back to a simple PID-alive (rough) check on those systems.
    ___x_cmd os name_
    if [ "$x_" = "win" ]; then
        ___x_cmd ps fingerprint check "$x_pid"
    else
        ___x_cmd ps fingerprint check "$x_pid" "$x_timestamp"
    fi

    case $? in
        130)        x_=""; return 130 ;;
        0)          x_=alive ;;
        1)          x_=stop ;;
        *)          service:error "Unexpected return code from 'x ps fingerprint check $x_pid $x_timestamp'"; return 1 ;;
    esac
}

# There is a new state called paused.

___x_cmd_service_isalive(){
    while [ $# -gt 0 ]; do
        case "$1" in
            -h|--help)      ___x_cmd help -m service isalive "$@" ; return 0 ;;
            *)              break
        esac
    done

    local name="$1"
    local x_
    ___x_cmd_service_status_ "$name"

    [ "$x_" = alive ]
}

___x_cmd_service_pid_(){
    local name="$1"
    local fp_pid="$___X_CMD_SERVICE_FP_DATA/$name/PID"

    [ -f "$fp_pid" ] || return 1
    read -r x_ <"$fp_pid"
}
