Skip to content

Commit

Permalink
Backport
Browse files Browse the repository at this point in the history
  • Loading branch information
dubo-dubon-duponey committed Mar 27, 2024
1 parent fc549a7 commit f40ece5
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 11 deletions.
67 changes: 67 additions & 0 deletions context/runtime/boot/helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 </dev/stdin
}
41 changes: 30 additions & 11 deletions context/runtime/boot/mdns.sh
Original file line number Diff line number Diff line change
Expand Up @@ -69,38 +69,46 @@ mdns::start::broadcaster(){

mdns::start::avahi(){
# Current issues with Avahi:
# - no way to change /run/avahi-daemon to another location - symlink works though
# - no way to change /run/avahi-daemon to another location - symlink works though (has to happen in the Dockerfile obviously)
# - daemonization writing to syslog is a problem
# - avahi insists that /run/avahi-daemon must belong to avahi:avahi
# which is absolutely ridiculous - https://github.com/lathiat/avahi/blob/778fadb71cb923eee74f3f1967db88b8c2586830/avahi-daemon/main.c#L1434
# Some variant of it: https://github.com/lathiat/avahi/issues/349
# - project is half-dead: https://github.com/lathiat/avahi/issues/388
# - project is half-dead anyway: https://github.com/lathiat/avahi/issues/388

local log_level="$1"
local args=()
# local avahisocket="$XDG_STATE_HOME/avahi-daemon/socket"
# XXX giving up on trying to be fancy with avahi
local avahisocket="/run/avahi-daemon/socket"

# Make sure we can write it
helpers::dir::writable "$(dirname "$avahisocket")" true
helpers::dir::writable "$(dirname "$avahisocket")"

# Cleanup leftovers on container restart
rm -f "$(dirname "$avahisocket")/pid"

[ "$LOG_LEVEL" != "debug" ] || args+=(--debug)
[ "$log_level" != "debug" ] || args+=(--debug)

# -D/--daemonize implies -s/--syslog that we do not want, so, just background it
avahi-daemon -f "$XDG_CONFIG_DIRS"/avahi/main.conf --no-drop-root --no-chroot "${args[@]}" &
# shellcheck disable=SC2015
{
{
avahi-daemon -f "$XDG_CONFIG_DIRS"/avahi/main.conf --no-drop-root --no-chroot "${args[@]}" 2>&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
until [ -e "$avahisocket" ]; do
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
}

Expand All @@ -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...

Expand All @@ -119,17 +128,27 @@ 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
until [ -e "$dbussocket" ]; do
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"
}

0 comments on commit f40ece5

Please sign in to comment.