Skip to content

Commit

Permalink
Added check for username
Browse files Browse the repository at this point in the history
Added iteration over WMs for single username call
  • Loading branch information
VTimofeenko committed Jan 14, 2021
1 parent 56cc1c1 commit 0f105f0
Showing 1 changed file with 33 additions and 12 deletions.
45 changes: 33 additions & 12 deletions root-notify-user
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
#!/bin/sh
# This script allows calling notify-send as root for it to appear on vt's desktop

USERNAME="$1"
shift

UID=$(id -u "${USERNAME}")
WM_PID=$(pgrep --exact --uid "${UID}" "i3")
ENV_FILE="/proc/${WM_PID}/environ"
DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS "${ENV_FILE}"|cut -d= -f2- | tr -d '\0')
DISPLAY=$(grep -z DISPLAY "${ENV_FILE}"|cut -d= -f2- | tr -d '\0')
sudo -u "${USERNAME}" DISPLAY="${DISPLAY}" DBUS_SESSION_BUS_ADDRESS="${DBUS_SESSION_BUS_ADDRESS}" notify-send "$@"
#!/bin/bash
# This script allows calling notify-send as root for it to appear on specific user's desktop
# Kept separate from the other script because it has explicit first parameter handling.

set -euo pipefail

USERNAME=${1:-}

if [ -z "${USERNAME}" ]; then
>&2 echo "No username passed. Exiting."
exit 1
else
shift
fi

MY_UID=$(id -u "${USERNAME}") || exit 1

readarray -t KNOWN_WMS < <(grep "^Exec" /usr/share/xsessions/* | sed 's/^.*Exec=//' | grep -v Xsession)

if [ ${#KNOWN_WMS[@]} -eq 0 ]; then
>&2 echo "Could not locate any window managers in /usr/share/xsessions/. Exiting."
exit 1
fi


for WM in "${KNOWN_WMS[@]}"; do
WM_PID=$(pgrep --exact "${WM}" --uid "${MY_UID}") || continue
ENV_FILE="/proc/${WM_PID}/environ"
DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS "${ENV_FILE}"|cut -d= -f2- | tr -d '\0')
DISPLAY=$(grep -z DISPLAY "${ENV_FILE}"|cut -d= -f2- | tr -d '\0')
USERNAME=$(stat -c '%U' "${ENV_FILE}")
sudo -u "${USERNAME}" DISPLAY="${DISPLAY}" DBUS_SESSION_BUS_ADDRESS="${DBUS_SESSION_BUS_ADDRESS}" notify-send "$@"
done

0 comments on commit 0f105f0

Please sign in to comment.