]> git.ipfire.org Git - thirdparty/dracut.git/blobdiff - dracut-logger.sh
fix off by one
[thirdparty/dracut.git] / dracut-logger.sh
index 2e3c1ed8eee9a77db36697d71e22199ae546f5ca..e72716ff3d2c247df10de1b440b06764101ff588 100755 (executable)
@@ -1,6 +1,4 @@
 #!/bin/bash
-# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
-# ex: ts=8 sw=4 et filetype=sh
 #
 # logging faciality module for dracut both at build- and boot-time
 #
@@ -138,20 +136,26 @@ dlog_init() {
                 errmsg="'$logfile' is not a writable file"
             fi
         fi
-        if type -P systemd-cat &>/dev/null && (( $UID  == 0 )) ; then
-            readonly _dlogdir="$(mktemp --tmpdir="$TMPDIR/" -d -t dracut-log.XXXXXX)"
-            readonly _systemdcatfile="$_dlogdir/systemd-cat"
-            mkfifo "$_systemdcatfile"
-            readonly _dlogfd=15
-            systemd-cat -t 'dracut' <"$_systemdcatfile" &
-            exec 15>"$_systemdcatfile"
-        fi
+    fi
+
+    if (( $UID  != 0 )); then
+        kmsgloglvl=0
+        sysloglvl=0
     fi
 
     if (( $sysloglvl > 0 )); then
-        if ! [ -S /dev/log -a -w /dev/log ] || ! command -v logger >/dev/null
-        then
+        if [[ -d /run/systemd/journal ]] \
+            && type -P systemd-cat &>/dev/null \
+            && systemctl --quiet is-active systemd-journald.socket &>/dev/null \
+            && { echo "dracut-$DRACUT_VERSION" | systemd-cat -t 'dracut' &>/dev/null; } ; then
+            readonly _systemdcatfile="$DRACUT_TMPDIR/systemd-cat"
+            mkfifo "$_systemdcatfile"
+            readonly _dlogfd=15
+            systemd-cat -t 'dracut' --level-prefix=true <"$_systemdcatfile" &
+            exec 15>"$_systemdcatfile"
+        elif ! [ -S /dev/log -a -w /dev/log ] || ! command -v logger >/dev/null; then
             # We cannot log to syslog, so turn this facility off.
+            kmsgloglvl=$sysloglvl
             sysloglvl=0
             ret=1
             errmsg="No '/dev/log' or 'logger' included for syslog logging"
@@ -175,34 +179,34 @@ dlog_init() {
     export maxloglvl
 
 
-    if (($stdloglvl < 6)) && (($kmsgloglvl < 6)) && (($fileloglvl < 6)); then
+    if (($stdloglvl < 6)) && (($kmsgloglvl < 6)) && (($fileloglvl < 6)) && (($sysloglvl < 6)); then
         unset dtrace
         dtrace() { :; };
     fi
 
-    if (($stdloglvl < 5)) && (($kmsgloglvl < 5)) && (($fileloglvl < 5)); then
+    if (($stdloglvl < 5)) && (($kmsgloglvl < 5)) && (($fileloglvl < 5)) && (($sysloglvl < 5)); then
         unset ddebug
         ddebug() { :; };
     fi
 
-    if (($stdloglvl < 4)) && (($kmsgloglvl < 4)) && (($fileloglvl < 4)); then
+    if (($stdloglvl < 4)) && (($kmsgloglvl < 4)) && (($fileloglvl < 4)) && (($sysloglvl < 4)); then
         unset dinfo
         dinfo() { :; };
     fi
 
-    if (($stdloglvl < 3)) && (($kmsgloglvl < 3)) && (($fileloglvl < 3)); then
+    if (($stdloglvl < 3)) && (($kmsgloglvl < 3)) && (($fileloglvl < 3)) && (($sysloglvl < 3)); then
         unset dwarn
         dwarn() { :; };
         unset dwarning
         dwarning() { :; };
     fi
 
-    if (($stdloglvl < 2)) && (($kmsgloglvl < 2)) && (($fileloglvl < 2)); then
+    if (($stdloglvl < 2)) && (($kmsgloglvl < 2)) && (($fileloglvl < 2)) && (($sysloglvl < 2)); then
         unset derror
         derror() { :; };
     fi
 
-    if (($stdloglvl < 1)) && (($kmsgloglvl < 1)) && (($fileloglvl < 1)); then
+    if (($stdloglvl < 1)) && (($kmsgloglvl < 1)) && (($fileloglvl < 1)) && (($sysloglvl < 1)); then
         unset dfatal
         dfatal() { :; };
     fi
@@ -259,9 +263,9 @@ _lvl2syspri() {
 # Conversion is done as follows:
 #
 # <tt>
-#   FATAL(1) -> LOG_EMERG (0)
+#   none     -> LOG_EMERG (0)
 #   none     -> LOG_ALERT (1)
-#   none     -> LOG_CRIT (2)
+#   FATAL(1) -> LOG_CRIT (2)
 #   ERROR(2) -> LOG_ERR (3)
 #   WARN(3)  -> LOG_WARNING (4)
 #   none     -> LOG_NOTICE (5)
@@ -275,7 +279,7 @@ _dlvl2syslvl() {
     local lvl
 
     case "$1" in
-        1) lvl=0;;
+        1) lvl=2;;
         2) lvl=3;;
         3) lvl=4;;
         4) lvl=6;;
@@ -319,19 +323,20 @@ _do_dlog() {
     local msg="$*"
     local lmsg="$lvlc: $*"
 
-    (( $lvl <= $stdloglvl )) && echo "$msg" >&2
+    (( $lvl <= $stdloglvl )) && printf -- 'dracut: %s\n' "$msg" >&2
+
     if (( $lvl <= $sysloglvl )); then
-        logger -t "dracut[$$]" -p $(_lvl2syspri $lvl) "$msg"
+        if [[ "$_dlogfd" ]]; then
+            printf -- "<%s>%s\n" "$(($(_dlvl2syslvl $lvl) & 7))" "$msg" >&$_dlogfd
+        else
+            logger -t "dracut[$$]" -p $(_lvl2syspri $lvl) -- "$msg"
+        fi
     fi
 
     if (( $lvl <= $fileloglvl )) && [[ -w "$logfile" ]] && [[ -f "$logfile" ]]; then
         echo "$lmsg" >>"$logfile"
     fi
 
-    if (( $lvl <= $fileloglvl )) && [[ "$_dlogfd" ]]; then
-        echo "<$(_dlvl2syslvl $lvl)>$msg" >&$_dlogfd
-    fi
-
     (( $lvl <= $kmsgloglvl )) && \
         echo "<$(_dlvl2syslvl $lvl)>dracut[$$] $msg" >/dev/kmsg
 }
@@ -359,7 +364,7 @@ dlog() {
     if (( $# > 1 )); then
         _do_dlog "$@"
     else
-        while read line; do
+        while read line || [ -n "$line" ]; do
             _do_dlog "$1" "$line"
         done
     fi