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