Skip to content

Instantly share code, notes, and snippets.

@mvo5
Created April 18, 2017 15:48
Show Gist options
  • Save mvo5/bd980109566c0c74af2c00255bb68712 to your computer and use it in GitHub Desktop.
Save mvo5/bd980109566c0c74af2c00255bb68712 to your computer and use it in GitHub Desktop.
#!/bin/bash
# yes - bash - because <() is really nice to have in a while read loop
set -e
SESSIONS="$(loginctl list-sessions --no-legend)"
SEAT=""
while read session; do
maybe="$( echo $session|cut -f4 -d" " )"
if [ -z "$maybe" ]; then
continue
fi
if [ -n "$SEAT" ] && [ "$maybe" != "$SEAT" ]; then
echo "cannot continue, found multiple seats: $SEAT and $maybe"
exit 1
fi
SEAT="$maybe"
done < <(echo "$SESSIONS")
if [ -z "$SEAT" ]; then
echo "cannot find a seat"
exit 1
fi
SESSION_ID="$(echo $SESSIONS | cut -f1 -d" ")"
SCOPE="$(loginctl show-session -pScope --value "${SESSION_ID}")"
SESSION_UID="$(loginctl show-session -pUser --value "${SESSION_ID}")"
CGROUP="$(systemctl show -pControlGroup --value "$SCOPE")"
# this is where it gets ugly
cat /sys/fs/cgroup/pids/"$CGROUP"/cgroup.procs |while read pid; do
ENV="$(cat /proc/"$pid"/environ | tr '\0' '\n')"
if echo "$ENV" | grep -q '^XAUTHORITY='; then
# found a env with the right env, copy it
envstr=""
while read line; do
envstr="--setenv=$line $envstr"
done < <(echo "$ENV")
systemd-run --scope --uid="$SESSION_UID" $envstr xdg-open "$1"
break
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment