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