From f40ece567f4d6a0b1c8249f32ab0a82f5594efca Mon Sep 17 00:00:00 2001 From: dubo-dubon-duponey Date: Wed, 27 Mar 2024 12:15:26 -0700 Subject: [PATCH] Backport --- context/runtime/boot/helpers.sh | 67 +++++++++++++++++++++++++++++++++ context/runtime/boot/mdns.sh | 41 ++++++++++++++------ 2 files changed, 97 insertions(+), 11 deletions(-) diff --git a/context/runtime/boot/helpers.sh b/context/runtime/boot/helpers.sh index 6b3e621..283ec8a 100644 --- a/context/runtime/boot/helpers.sh +++ b/context/runtime/boot/helpers.sh @@ -30,3 +30,70 @@ helpers::log::normalize(){ } helpers::log::normalize >/dev/null + +# shellcheck disable=SC2034 +readonly DC_COLOR_BLACK=0 +# shellcheck disable=SC2034 +readonly DC_COLOR_RED=1 +# shellcheck disable=SC2034 +readonly DC_COLOR_GREEN=2 +# shellcheck disable=SC2034 +readonly DC_COLOR_YELLOW=3 +# shellcheck disable=SC2034 +readonly DC_COLOR_BLUE=4 +# shellcheck disable=SC2034 +readonly DC_COLOR_MAGENTA=5 +# shellcheck disable=SC2034 +readonly DC_COLOR_CYAN=6 +# shellcheck disable=SC2034 +readonly DC_COLOR_WHITE=7 +# shellcheck disable=SC2034 +readonly DC_COLOR_DEFAULT=9 + +# shellcheck disable=SC2034 +readonly DC_LOGGER_DEBUG=4 +# shellcheck disable=SC2034 +readonly DC_LOGGER_INFO=3 +# shellcheck disable=SC2034 +readonly DC_LOGGER_WARNING=2 +# shellcheck disable=SC2034 +readonly DC_LOGGER_ERROR=1 + +export DC_LOGGER_STYLE_DEBUG=( setaf "$DC_COLOR_WHITE" ) +export DC_LOGGER_STYLE_INFO=( setaf "$DC_COLOR_GREEN" ) +export DC_LOGGER_STYLE_WARNING=( setaf "$DC_COLOR_YELLOW" ) +export DC_LOGGER_STYLE_ERROR=( setaf "$DC_COLOR_RED" ) + +_DC_PRIVATE_LOGGER_LEVEL="$DC_LOGGER_WARNING" + +helpers::logger::log(){ + local prefix="$1" + shift + + local level="DC_LOGGER_$prefix" + local style="DC_LOGGER_STYLE_${prefix}[@]" + + [ "$_DC_PRIVATE_LOGGER_LEVEL" -ge "${!level}" ] || return 0 + + # If you wonder about why that crazy shit: https://stackoverflow.com/questions/12674783/bash-double-process-substitution-gives-bad-file-descriptor + exec 3>&2 + [ ! "$TERM" ] || [ ! -t 2 ] || >&2 tput "${!style}" 2>/dev/null || true + >&2 printf "[%s] [%s] %s\n" "$(date 2>/dev/null || true)" "$prefix" "$*" + [ ! "$TERM" ] || [ ! -t 2 ] || >&2 tput op 2>/dev/null || true + exec 3>&- +} + +helpers::logger::set() { + local desired + desired="$(printf "DC_LOGGER_%s" "${1:-warning}" | tr '[:lower:]' '[:upper:]')" + _DC_PRIVATE_LOGGER_LEVEL="${!desired}" +} + +helpers::logger::slurp(){ + local level + level="$(printf "%s" "${1:-warning}" | tr '[:lower:]' '[:upper:]')" + shift + while read -r line; do + helpers::logger::log "$level" "$* $line"; + done &1 + } > >(helpers::logger::slurp "$log_level" "[avahi]") \ + && helpers::logger::log INFO "[avahi]" "Avahi stopped" \ + || helpers::logger::log ERROR "[avahi]" "Avahi stopped with exit code: $?" + } & + local tries=1 # Wait until the socket is there @@ -98,9 +105,10 @@ mdns::start::avahi(){ sleep 1s tries=$(( tries + 1)) [ $tries -lt 10 ] || { - printf >&2 "Failed starting avahi in a reasonable time. Something is quite wrong\n" + helpers::logger::log ERROR "[avahi]" "Failed starting avahi in a reasonable time. Something is quite wrong" return 1 } + helpers::logger::log DEBUG "[avahi]" "Avahi started successfully" done } @@ -109,6 +117,7 @@ mdns::start::dbus(){ # https://man7.org/linux/man-pages/man3/sd_bus_default.3.html # https://specifications.freedesktop.org/basedir-spec/latest/ar01s03.html + local log_level="$1" local dbussocket=/magnetar/runtime/dbus/system_bus_socket # Configuration file also has that ^ hardcoded, so, cannot use the variable... @@ -119,8 +128,17 @@ mdns::start::dbus(){ export DBUS_SYSTEM_BUS_ADDRESS=unix:path="$dbussocket" export DBUS_SESSION_BUS_ADDRESS=unix:path="$dbussocket" - # Start it, without a PID file - dbus-daemon --nopidfile --config-file "$XDG_CONFIG_DIRS"/dbus/main.conf + # Start it, without a PID file, no fork + # XXX somehow right now shairport-sync is not happy - disable custom config for now + # dbus-daemon --nofork --nopidfile --nosyslog --config-file "$XDG_CONFIG_DIRS"/dbus/main.conf + # shellcheck disable=SC2015 + { + { + dbus-daemon --system --nofork --nopidfile --nosyslog 2>&1 + } > >(helpers::logger::slurp "$log_level" "[dbus]") \ + && helpers::logger::log INFO "[dbus]" "DBUS stopped" \ + || helpers::logger::log ERROR "[dbus]" "DBUS stopped with exit code: $?" + } & local tries=1 # Wait until the socket is there @@ -128,8 +146,9 @@ mdns::start::dbus(){ sleep 1s tries=$(( tries + 1)) [ $tries -lt 10 ] || { - printf >&2 "Failed starting dbus in a reasonable time. Something is quite wrong\n" + helpers::logger::log ERROR "[dbus]" "Failed starting dbus in a reasonable time. Something is quite wrong" return 1 } done + helpers::logger::log DEBUG "[dbus]" "DBUS started successfully" }