f0d357
#!/bin/sh
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
#
050f80
# The code in this file might be run in an environment without bash.
050f80
# Any code added must be POSIX compliant.
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
{
f0d357
    [ -f /lib/dracut-lib.sh ] && kdump_sysloglvl=$(getarg rd.kdumploglvl)
DistroBaker 17a515
    [ -z "$kdump_sysloglvl" ] && return 1;
DistroBaker 17a515
f0d357
    if [ -f /lib/dracut-lib.sh ] && ! isdigit "$kdump_sysloglvl"; then
f0d357
        return 1
f0d357
    fi
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() {
f0d357
    ret=0
DistroBaker 17a515
DistroBaker 17a515
    if [ -s /proc/vmcore ];then
f0d357
        if ! get_kdump_loglvl; 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
f0d357
        if ! check_loglvl "$loglvl"; 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
f0d357
    if [ "$UID" -ne 0 ]; then
DistroBaker 5cac7c
        kdump_kmsgloglvl=0
DistroBaker 5cac7c
        kdump_sysloglvl=0
DistroBaker 5cac7c
    fi
DistroBaker 5cac7c
f0d357
    if [ "$kdump_sysloglvl" -gt 0 ]; then
f0d357
        if [ -d /run/systemd/journal ] \
f0d357
            && systemd-cat --version 1>/dev/null 2>&1 \
f0d357
            && systemctl --quiet is-active systemd-journald.socket 1>/dev/null 2>&1; then
DistroBaker 5cac7c
            readonly _systemdcatfile="/var/tmp/systemd-cat"
f0d357
            mkfifo "$_systemdcatfile" 1>/dev/null 2>&1
DistroBaker 5cac7c
            readonly _dlogfd=15
DistroBaker 5cac7c
            systemd-cat -t 'kdump' --level-prefix=true <"$_systemdcatfile" &
DistroBaker 5cac7c
            exec 15>"$_systemdcatfile"
f0d357
        elif ! [ -S /dev/log ] && [ -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
f0d357
    kdump_maxloglvl=0
f0d357
    for _dlog_lvl in $kdump_stdloglvl $kdump_sysloglvl $kdump_kmsgloglvl; do
f0d357
        [ $_dlog_lvl -gt $kdump_maxloglvl ] && kdump_maxloglvl=$_dlog_lvl
DistroBaker 5cac7c
    done
f0d357
    readonly kdump_maxloglvl
DistroBaker 5cac7c
    export kdump_maxloglvl
DistroBaker 5cac7c
f0d357
    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
f0d357
    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
f0d357
    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
f0d357
    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
#
f0d357
# @param $1: 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
#
f0d357
# @param $1: 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
    case "$1" in
f0d357
        1) set -- 3;;
f0d357
        2) set -- 4;;
f0d357
        3) set -- 6;;
f0d357
        4) set -- 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.
f0d357
    echo $((24 + $1))
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
#
f0d357
# @param $1: Numeric logging level.
f0d357
# @param $2: 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() {
f0d357
    [ "$1" -le $kdump_stdloglvl ] && printf -- 'kdump: %s\n' "$2" >&2
DistroBaker 5cac7c
f0d357
    if [ "$1" -le $kdump_sysloglvl ]; then
f0d357
        if [ "$_dlogfd" ]; then
f0d357
            printf -- "<%s>%s\n" "$(($(_dlvl2syslvl "$1") & 7))" "$2" 1>&$_dlogfd
DistroBaker 5cac7c
        else
f0d357
            logger -t "kdump[$$]" -p "$(_lvl2syspri "$1")" -- "$2"
DistroBaker 5cac7c
        fi
DistroBaker 5cac7c
    fi
DistroBaker 5cac7c
f0d357
    [ "$1" -le $kdump_kmsgloglvl ] && \
f0d357
        echo "<$(_dlvl2syslvl "$1")>kdump[$$] $2" >/dev/kmsg
DistroBaker 5cac7c
}
DistroBaker 5cac7c
DistroBaker 5cac7c
## @brief Internal helper function for _do_dlog()
DistroBaker 5cac7c
#
f0d357
# @param $1: Numeric logging level.
f0d357
# @param $2 [...]: 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
f0d357
    [ "$1" -le "$kdump_maxloglvl" ] || return 0
DistroBaker 5cac7c
f0d357
    if [ $# -gt 1 ]; then
f0d357
        _dlog_lvl=$1; shift
f0d357
        _do_dlog "$_dlog_lvl" "$*"
DistroBaker 5cac7c
    else
f0d357
        while read -r 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 "$@"
f0d357
    if [ -n "$debug" ]; then
f0d357
        set -x
f0d357
    fi
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 "$@"
f0d357
    if [ -n "$debug" ]; then
f0d357
        set -x
f0d357
    fi
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 "$@"
f0d357
    if [ -n "$debug" ]; then
f0d357
        set -x
f0d357
    fi
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 "$@"
f0d357
    if [ -n "$debug" ]; then
f0d357
        set -x
f0d357
    fi
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 "$@"
f0d357
    if [ -n "$debug" ]; then
f0d357
        set -x
f0d357
    fi
DistroBaker 5cac7c
}