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