Blame SOURCES/acpid.power.sh

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