Files
mesh-catalog/modules/clipmenu/files/bin/clipmenud-session
T

42 lines
2.0 KiB
Bash

#!/bin/sh
# clipmenud as the operator's user unit starts it, written by the mesh (module clipmenu). Replaced at every push.
#
# The account's service manager starts before the X server does, and has a DISPLAY only once the session's
# xinitrc has handed it over (module xorg). So this waits, quietly, until the display is known, and exits
# only when the session ends with it: a quick exit would run the unit into its start limit. DISPLAY, when
# the manager has it, wins. Without it, exactly one X socket names the display; none or several (a
# second server beside the session) wait for the session's own to arrive.
# A running process does not see what the session hands the manager later, so each pass asks the manager.
until [ -n "$DISPLAY" ]; do
for pair in $(systemctl --user show-environment 2>/dev/null | grep -E '^(DISPLAY|XAUTHORITY)='); do
export "$pair"
done
[ -n "$DISPLAY" ] && break
set -- /tmp/.X11-unix/X*
if [ $# -eq 1 ] && [ -S "$1" ]; then
DISPLAY=":${1##*/X}"
export DISPLAY
else
sleep 1
fi
done
if [ -z "$XAUTHORITY" ] && [ -r "$HOME/.Xauthority" ]; then
XAUTHORITY="$HOME/.Xauthority"
export XAUTHORITY
fi
# clipmenud allows one daemon per account: it takes an exclusive flock on its session_lock file and exits
# with status 2 when that is held. After a delivery without a new login, the session's older clipmenud (from
# its xinitrc) still holds it, and a start would run the unit round its restart loop. So this waits, quietly,
# until the lock is free: it looks at the same file clipmenud locks (the directory is clipmenud's own rule)
# and lets go at once. Exit status 1 is "held" for util-linux and BusyBox flock alike (no -E: BusyBox lacks it); any other outcome starts clipmenud, which says its own
# error. Another daemon winning the gap between look and start costs one restart, not a loop.
lock="${CM_DIR:-${XDG_RUNTIME_DIR-${TMPDIR-/tmp}}}/clipmenu.6.$USER/session_lock"
while [ -e "$lock" ]; do
flock -n "$lock" true
[ $? -eq 1 ] || break
sleep 2
done
PATH="/usr/local/lib/mesh-clipmenu:$PATH"
export PATH
exec clipmenud