54b5ae
#!/bin/bash
54b5ae
#
54b5ae
# This comes from the dracut-logger.sh
54b5ae
#
54b5ae
# The logger defined 4 logging levels:
54b5ae
#   - ddebug (4)
54b5ae
#     The DEBUG Level designates fine-grained informational events that are most
54b5ae
#     useful to debug an application.
54b5ae
#   - dinfo (3)
54b5ae
#     The INFO level designates informational messages that highlight the
54b5ae
#     progress of the application at coarse-grained level.
54b5ae
#   - dwarn (2)
54b5ae
#     The WARN level designates potentially harmful situations.
54b5ae
#   - derror (1)
54b5ae
#     The ERROR level designates error events that might still allow the
54b5ae
#     application to continue running.
54b5ae
#
54b5ae
# Logging is controlled by following global variables:
54b5ae
#   - @var kdump_stdloglvl - logging level to standard error (console output)
54b5ae
#   - @var kdump_sysloglvl - logging level to syslog (by logger command)
54b5ae
#   - @var kdump_kmsgloglvl - logging level to /dev/kmsg (only for boot-time)
54b5ae
#
54b5ae
# If any of the variables is not set, the function dlog_init() sets it to default:
54b5ae
#   - In the first kernel:
54b5ae
#     - @var kdump_stdloglvl = 3 (info)
54b5ae
#     - @var kdump_sysloglvl = 0 (no logging)
54b5ae
#     - @var kdump_kmsgloglvl = 0 (no logging)
54b5ae
#
54b5ae
#   -In the second kernel:
54b5ae
#    - @var kdump_stdloglvl = 0 (no logging)
54b5ae
#    - @var kdump_sysloglvl = 3 (info)
54b5ae
#    - @var kdump_kmsgloglvl = 0 (no logging)
54b5ae
#
54b5ae
# First of all you have to start with dlog_init() function which initializes
54b5ae
# required variables. Don't call any other logging function before that one!
54b5ae
#
54b5ae
54b5ae
# Define vairables for the log levels in this module.
54b5ae
kdump_stdloglvl=""
54b5ae
kdump_sysloglvl=""
54b5ae
kdump_kmsgloglvl=""
54b5ae
54b5ae
# The dracut-lib.sh is only available in the second kernel, and it won't
54b5ae
# be used in the first kernel because the dracut-lib.sh is invisible in
54b5ae
# the first kernel.
54b5ae
if [ -f /lib/dracut-lib.sh ]; then
54b5ae
    . /lib/dracut-lib.sh
54b5ae
fi
54b5ae
54b5ae
# @brief Get the log level from kernel command line.
54b5ae
# @retval 1 if something has gone wrong
54b5ae
# @retval 0 on success.
54b5ae
#
54b5ae
get_kdump_loglvl()
54b5ae
{
54b5ae
    (type -p getarg) && kdump_sysloglvl=$(getarg rd.kdumploglvl)
54b5ae
    [ -z "$kdump_sysloglvl" ] && return 1;
54b5ae
54b5ae
    (type -p isdigit) && isdigit $kdump_sysloglvl
54b5ae
    [ $? -ne 0 ] && return 1;
54b5ae
54b5ae
    return 0
54b5ae
}
54b5ae
54b5ae
# @brief Check the log level.
54b5ae
# @retval 1 if something has gone wrong
54b5ae
# @retval 0 on success.
54b5ae
#
54b5ae
check_loglvl()
54b5ae
{
54b5ae
    case "$1" in
54b5ae
        0|1|2|3|4)
54b5ae
            return 0
54b5ae
           ;;
54b5ae
        *)
54b5ae
            return 1
54b5ae
            ;;
54b5ae
        esac
54b5ae
}
54b5ae
54b5ae
# @brief Initializes Logger.
54b5ae
# @retval 1 if something has gone wrong
54b5ae
# @retval 0 on success.
54b5ae
#
54b5ae
dlog_init() {
54b5ae
    local ret=0; local errmsg
54b5ae
54b5ae
    if [ -s /proc/vmcore ];then
54b5ae
        get_kdump_loglvl
54b5ae
        if [ $? -ne 0 ];then
54b5ae
            logger -t "kdump[$$]" -p warn -- "Kdump is using the default log level(3)."
54b5ae
            kdump_sysloglvl=3
54b5ae
        fi
54b5ae
        kdump_stdloglvl=0
54b5ae
        kdump_kmsgloglvl=0
54b5ae
    else
54b5ae
        kdump_stdloglvl=$KDUMP_STDLOGLVL
54b5ae
        kdump_sysloglvl=$KDUMP_SYSLOGLVL
54b5ae
        kdump_kmsgloglvl=$KDUMP_KMSGLOGLVL
54b5ae
    fi
54b5ae
54b5ae
    [ -z "$kdump_stdloglvl" ] && kdump_stdloglvl=3
54b5ae
    [ -z "$kdump_sysloglvl" ] && kdump_sysloglvl=0
54b5ae
    [ -z "$kdump_kmsgloglvl" ] && kdump_kmsgloglvl=0
54b5ae
54b5ae
    for loglvl in "$kdump_stdloglvl" "$kdump_kmsgloglvl" "$kdump_sysloglvl"; do
54b5ae
        check_loglvl "$loglvl"
54b5ae
        if [ $? -ne 0 ]; then
54b5ae
            echo "Illegal log level: $kdump_stdloglvl $kdump_kmsgloglvl $kdump_sysloglvl"
54b5ae
            return 1
54b5ae
        fi
54b5ae
    done
54b5ae
54b5ae
    # Skip initialization if it's already done.
54b5ae
    [ -n "$kdump_maxloglvl" ] && return 0
54b5ae
54b5ae
    if [[ $UID -ne 0 ]]; then
54b5ae
        kdump_kmsgloglvl=0
54b5ae
        kdump_sysloglvl=0
54b5ae
    fi
54b5ae
54b5ae
    if [[ $kdump_sysloglvl -gt 0 ]]; then
54b5ae
        if [[ -d /run/systemd/journal ]] \
54b5ae
            && type -P systemd-cat &>/dev/null \
54b5ae
            && systemctl --quiet is-active systemd-journald.socket &>/dev/null; then
54b5ae
            readonly _systemdcatfile="/var/tmp/systemd-cat"
54b5ae
            mkfifo "$_systemdcatfile" &>/dev/null
54b5ae
            readonly _dlogfd=15
54b5ae
            systemd-cat -t 'kdump' --level-prefix=true <"$_systemdcatfile" &
54b5ae
            exec 15>"$_systemdcatfile"
54b5ae
        elif ! [ -S /dev/log -a -w /dev/log ] || ! command -v logger >/dev/null; then
54b5ae
            # We cannot log to syslog, so turn this facility off.
54b5ae
            kdump_kmsgloglvl=$kdump_sysloglvl
54b5ae
            kdump_sysloglvl=0
54b5ae
            ret=1
54b5ae
            errmsg="No '/dev/log' or 'logger' included for syslog logging"
54b5ae
        fi
54b5ae
    fi
54b5ae
54b5ae
    local lvl; local maxloglvl_l=0
54b5ae
    for lvl in $kdump_stdloglvl $kdump_sysloglvl $kdump_kmsgloglvl; do
54b5ae
        [[ $lvl -gt $maxloglvl_l ]] && maxloglvl_l=$lvl
54b5ae
    done
54b5ae
    readonly kdump_maxloglvl=$maxloglvl_l
54b5ae
    export kdump_maxloglvl
54b5ae
54b5ae
    if [[ $kdump_stdloglvl -lt 4 ]] && [[ $kdump_kmsgloglvl -lt 4 ]] && [[ $kdump_sysloglvl -lt 4 ]]; then
54b5ae
        unset ddebug
54b5ae
        ddebug() { :; };
54b5ae
    fi
54b5ae
54b5ae
    if [[ $kdump_stdloglvl -lt 3 ]] && [[ $kdump_kmsgloglvl -lt 3 ]] && [[ $kdump_sysloglvl -lt 3 ]]; then
54b5ae
        unset dinfo
54b5ae
        dinfo() { :; };
54b5ae
    fi
54b5ae
54b5ae
    if [[ $kdump_stdloglvl -lt 2 ]] && [[ $kdump_kmsgloglvl -lt 2 ]] && [[ $kdump_sysloglvl -lt 2 ]]; then
54b5ae
        unset dwarn
54b5ae
        dwarn() { :; };
54b5ae
        unset dwarning
54b5ae
        dwarning() { :; };
54b5ae
    fi
54b5ae
54b5ae
    if [[ $kdump_stdloglvl -lt 1 ]] && [[ $kdump_kmsgloglvl -lt 1 ]] && [[ $kdump_sysloglvl -lt 1 ]]; then
54b5ae
        unset derror
54b5ae
        derror() { :; };
54b5ae
    fi
54b5ae
54b5ae
    [ -n "$errmsg" ] && derror "$errmsg"
54b5ae
54b5ae
    return $ret
54b5ae
}
54b5ae
54b5ae
## @brief Converts numeric level to logger priority defined by POSIX.2.
54b5ae
#
54b5ae
# @param lvl Numeric logging level in range from 1 to 4.
54b5ae
# @retval 1 if @a lvl is out of range.
54b5ae
# @retval 0 if @a lvl is correct.
54b5ae
# @result Echoes logger priority.
54b5ae
_lvl2syspri() {
54b5ae
    case "$1" in
54b5ae
        1) echo error;;
54b5ae
        2) echo warning;;
54b5ae
        3) echo info;;
54b5ae
        4) echo debug;;
54b5ae
        *) return 1;;
54b5ae
    esac
54b5ae
}
54b5ae
54b5ae
## @brief Converts logger numeric level to syslog log level
54b5ae
#
54b5ae
# @param lvl Numeric logging level in range from 1 to 4.
54b5ae
# @retval 1 if @a lvl is out of range.
54b5ae
# @retval 0 if @a lvl is correct.
54b5ae
# @result Echoes kernel console numeric log level
54b5ae
#
54b5ae
# Conversion is done as follows:
54b5ae
#
54b5ae
# <tt>
54b5ae
#   none     -> LOG_EMERG (0)
54b5ae
#   none     -> LOG_ALERT (1)
54b5ae
#   none     -> LOG_CRIT (2)
54b5ae
#   ERROR(1) -> LOG_ERR (3)
54b5ae
#   WARN(2)  -> LOG_WARNING (4)
54b5ae
#   none     -> LOG_NOTICE (5)
54b5ae
#   INFO(3)  -> LOG_INFO (6)
54b5ae
#   DEBUG(4) -> LOG_DEBUG (7)
54b5ae
# </tt>
54b5ae
#
54b5ae
# @see /usr/include/sys/syslog.h
54b5ae
_dlvl2syslvl() {
54b5ae
    local lvl
54b5ae
54b5ae
    case "$1" in
54b5ae
        1) lvl=3;;
54b5ae
        2) lvl=4;;
54b5ae
        3) lvl=6;;
54b5ae
        4) lvl=7;;
54b5ae
        *) return 1;;
54b5ae
    esac
54b5ae
54b5ae
    # The number is constructed by multiplying the facility by 8 and then
54b5ae
    # adding the level.
54b5ae
    # About The Syslog Protocol, please refer to the RFC5424 for more details.
54b5ae
    echo $((24+$lvl))
54b5ae
}
54b5ae
54b5ae
## @brief Prints to stderr, to syslog and/or /dev/kmsg given message with
54b5ae
#  given level (priority).
54b5ae
#
54b5ae
# @param lvl Numeric logging level.
54b5ae
# @param msg Message.
54b5ae
# @retval 0 It's always returned, even if logging failed.
54b5ae
#
54b5ae
# @note This function is not supposed to be called manually. Please use
54b5ae
# dinfo(), ddebug(), or others instead which wrap this one.
54b5ae
#
54b5ae
# This is core logging function which logs given message to standard error
54b5ae
# and/or syslog (with POSIX shell command <tt>logger</tt>) and/or to /dev/kmsg.
54b5ae
# The format is following:
54b5ae
#
54b5ae
# <tt>X: some message</tt>
54b5ae
#
54b5ae
# where @c X is the first letter of logging level. See module description for
54b5ae
# details on that.
54b5ae
#
54b5ae
# Message to syslog is sent with tag @c kdump. Priorities are mapped as
54b5ae
# following:
54b5ae
#   - @c ERROR to @c error
54b5ae
#   - @c WARN to @c warning
54b5ae
#   - @c INFO to @c info
54b5ae
#   - @c DEBUG to @c debug
54b5ae
_do_dlog() {
54b5ae
    local lvl="$1"; shift
54b5ae
    local msg="$*"
54b5ae
54b5ae
    [[ $lvl -le $kdump_stdloglvl ]] && printf -- 'kdump: %s\n' "$msg" >&2
54b5ae
54b5ae
    if [[ $lvl -le $kdump_sysloglvl ]]; then
54b5ae
        if [[ "$_dlogfd" ]]; then
54b5ae
            printf -- "<%s>%s\n" "$(($(_dlvl2syslvl $lvl) & 7))" "$msg" >&$_dlogfd
54b5ae
        else
54b5ae
            logger -t "kdump[$$]" -p $(_lvl2syspri $lvl) -- "$msg"
54b5ae
        fi
54b5ae
    fi
54b5ae
54b5ae
    [[ $lvl -le $kdump_kmsgloglvl ]] && \
54b5ae
        echo "<$(_dlvl2syslvl $lvl)>kdump[$$] $msg" >/dev/kmsg
54b5ae
}
54b5ae
54b5ae
## @brief Internal helper function for _do_dlog()
54b5ae
#
54b5ae
# @param lvl Numeric logging level.
54b5ae
# @param msg Message.
54b5ae
# @retval 0 It's always returned, even if logging failed.
54b5ae
#
54b5ae
# @note This function is not supposed to be called manually. Please use
54b5ae
# dinfo(), ddebug(), or others instead which wrap this one.
54b5ae
#
54b5ae
# This function calls _do_dlog() either with parameter msg, or if
54b5ae
# none is given, it will read standard input and will use every line as
54b5ae
# a message.
54b5ae
#
54b5ae
# This enables:
54b5ae
# dwarn "This is a warning"
54b5ae
# echo "This is a warning" | dwarn
54b5ae
dlog() {
54b5ae
    [ -z "$kdump_maxloglvl" ] && return 0
54b5ae
    [[ $1 -le $kdump_maxloglvl ]] || return 0
54b5ae
54b5ae
    if [[ $# -gt 1 ]]; then
54b5ae
        _do_dlog "$@"
54b5ae
    else
54b5ae
        while read line || [ -n "$line" ]; do
54b5ae
            _do_dlog "$1" "$line"
54b5ae
        done
54b5ae
    fi
54b5ae
}
54b5ae
54b5ae
## @brief Logs message at DEBUG level (4)
54b5ae
#
54b5ae
# @param msg Message.
54b5ae
# @retval 0 It's always returned, even if logging failed.
54b5ae
ddebug() {
54b5ae
    set +x
54b5ae
    dlog 4 "$@"
54b5ae
    [ -n "$debug" ] && set -x || :
54b5ae
}
54b5ae
54b5ae
## @brief Logs message at INFO level (3)
54b5ae
#
54b5ae
# @param msg Message.
54b5ae
# @retval 0 It's always returned, even if logging failed.
54b5ae
dinfo() {
54b5ae
    set +x
54b5ae
    dlog 3 "$@"
54b5ae
    [ -n "$debug" ] && set -x || :
54b5ae
}
54b5ae
54b5ae
## @brief Logs message at WARN level (2)
54b5ae
#
54b5ae
# @param msg Message.
54b5ae
# @retval 0 It's always returned, even if logging failed.
54b5ae
dwarn() {
54b5ae
    set +x
54b5ae
    dlog 2 "$@"
54b5ae
    [ -n "$debug" ] && set -x || :
54b5ae
}
54b5ae
54b5ae
## @brief It's an alias to dwarn() function.
54b5ae
#
54b5ae
# @param msg Message.
54b5ae
# @retval 0 It's always returned, even if logging failed.
54b5ae
dwarning() {
54b5ae
    set +x
54b5ae
    dwarn "$@"
54b5ae
    [ -n "$debug" ] && set -x || :
54b5ae
}
54b5ae
54b5ae
## @brief Logs message at ERROR level (1)
54b5ae
#
54b5ae
# @param msg Message.
54b5ae
# @retval 0 It's always returned, even if logging failed.
54b5ae
derror() {
54b5ae
    set +x
54b5ae
    dlog 1 "$@"
54b5ae
    [ -n "$debug" ] && set -x || :
54b5ae
}