Blame SOURCES/acpid.power.sh

535d37
#!/bin/sh
535d37
535d37
PATH=/usr/sbin:/usr/bin
535d37
535d37
# $1 = session number
535d37
function get_session_processes() {
535d37
	local uid=$(loginctl show-session $1 | grep '^User=' | sed -r -e 's/^User=(.*)$/\1/')
535d37
	systemd-cgls "/user.slice/user-${uid}.slice/session-${1}.scope"
535d37
}
535d37
535d37
# Check session status using systemd
535d37
session_ids=$(loginctl list-sessions 2>/dev/null | awk '{print $1}')
535d37
for session in ${session_ids} ; do
535d37
	session_status=$(loginctl show-session ${session})
535d37
	session_processes="$(get_session_processes ${session})"
535d37
	echo "${session_status}" | grep -e 'Active=yes' &> /dev/null &&
535d37
		echo "${session_processes}" | grep -e '\(gnome-settings-daemon\|cinnamon-settings-daemon\|kded[4-5]\|plasmashell\|xfce4-power-manager\|mate-power-manager\)' &> /dev/null && exit 0
535d37
done
535d37
535d37
# Get the ID of the first active X11 session: using ConsoleKit
535d37
uid_session=$(
535d37
ck-list-sessions 2>/dev/null | \
535d37
awk '
535d37
/^Session[0-9]+:$/ { uid = active = x11 = "" ; next }
535d37
{ gsub(/'\''/, "", $3) }
535d37
$1 == "unix-user" { uid = $3 }
535d37
$1 == "active" { active = $3 }
535d37
$1 == "x11-display" { x11 = $3 }
535d37
active == "TRUE" && x11 != "" {
535d37
	print uid
535d37
	exit
535d37
}')
535d37
535d37
# Check that there is a power manager, otherwise shut down.
535d37
[ "$uid_session" ] &&
535d37
ps axo uid,cmd | \
535d37
awk '
535d37
    $1 == '$uid_session' &&
535d37
        ($2 ~ /gnome-power-manager/ || $2 ~ /kpowersave/ ||
535d37
        $2 ~ /mate-power-manager/ || $2 ~ /xfce4-power-manager/ ||
535d37
        $2 ~ /\/usr\/libexec\/gnome-settings-daemon/ ||
535d37
        $2 ~ /\/usr\/libexec\/cinnamon-settings-daemon/ ||
535d37
        $2 ~ /kded[4-5]/ || $2 ~ /guidance-power-manager/ ||
535d37
        $2 ~ /plasmashell/) \
535d37
                { found = 1; exit }
535d37
    END { exit !found }
535d37
' ||
535d37
  shutdown -h now
535d37