blob: 10aefc2b1bd663434ac1b0827a4127701144ce8d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
#!/usr/bin/env bash
# Bash strict mode
set -euo pipefail
is_parent_volatile() {
local parent_pid="$PPID"
if [[ "$parent_pid" -eq 1 ]]; then
return 0
fi
local parent_exe
parent_exe="$(readlink -f "/proc/$parent_pid/exe" 2>/dev/null || true)"
if [[ -z "$parent_exe" ]]; then
return 0
fi
case "$parent_exe" in
/usr/bin/fuzzel|/usr/bin/nwg-drawer|/usr/bin/wofi)
return 0
;;
*)
return 1
;;
esac
}
# Elevate
if [[ $EUID -ne 0 ]]; then
if is_parent_volatile; then
pkexec /usr/lib/garuda/pkexec-gui "$@" || { exit "$?"; }
exit 0
else
exec pkexec /usr/lib/garuda/pkexec-gui "$@"
exit 1
fi
fi
# Launch dbus session if not already running
if [[ ! -v DBUS_SESSION_BUS_ADDRESS ]]; then
eval $(dbus-launch --sh-syntax --exit-with-session)
fi
# Restore some variables from parent process
while IFS= read -rd '' var; do export "$var"; done < <(grep --null-data -ae "^\(HOME\|XDG_CURRENT_DESKTOP\|WAYLAND_DISPLAY\|XDG_RUNTIME_DIR\|XDG_CONFIG_DIRS\|XDG_SESSION_TYPE\|XDG_SESSION_PATH\|XCURSOR_SIZE\|KDE_SESSION_VERSION\|LC_.*\|LANG\|LANGUAGE\|QT_WAYLAND_FORCE_DPI\|QT_QPA_PLATFORMTHEME\|QT_STYLE_OVERRIDE\)=.*\$" /proc/$PPID/environ)
# Temporary directory to store overlays
overlays_dir="$(mktemp -d)"
if [[ -z "$overlays_dir" ]]; then
echo "Failed to create temporary directory" >&2
exit 1
fi
mkdir "$overlays_dir"/{upper,work,home}
# Overlayfs mount
mount -t overlay overlay -o lowerdir="$HOME",upperdir="$overlays_dir/upper",workdir="$overlays_dir/work" "$overlays_dir/home"
# If not set, the default behavior of the program that is launched will be used automatically
if [[ -v XDG_CONFIG_DIRS ]]; then
# Replace any mention of the user's home directory with the overlay in XDG_CONFIG_DIRS
export XDG_CONFIG_DIRS="${XDG_CONFIG_DIRS//"$HOME"/"$overlays_dir/home"}"
fi
# Set HOME to the overlay
export HOME="$overlays_dir/home"
# Make sure wayland is configured correctly. This variable is usually not defined expliclty, so it will have to be generated
if [[ -v WAYLAND_DISPLAY ]]; then
export WAYLAND_DISPLAY="$XDG_RUNTIME_DIR/$WAYLAND_DISPLAY"
fi
# We do not want root writing to user's XDG_RUNTIME_DIR
export XDG_RUNTIME_DIR="/run/user/0"
# Run program and store exit code
"$@" || code=$?
# Cleanup
umount "$overlays_dir/home"
rm -rf "$overlays_dir"
exit ${code:-0}
|