Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notification invisible when run with sudo su $SUDO_USER -c #1126

Closed
spikespaz opened this issue Nov 12, 2022 · 1 comment
Closed

Notification invisible when run with sudo su $SUDO_USER -c #1126

spikespaz opened this issue Nov 12, 2022 · 1 comment

Comments

@spikespaz
Copy link

spikespaz commented Nov 12, 2022

I have the following minimal example:

for i in 1 2 3 4; do                                                                              
  notify-send "Test $i" -h string:synchronous:disable-keyboard
  sleep 1
done

The notification shown does not replace the old one (as it should according to Dunst, and does work for my other scripts).

This is not the case if notify-send is run directly by the user.

I need to do this because the script's primary function requires superuser, and the script is marked as NOPASSWD in /etc/sudoers. Using sudo inline in the script only for the necessary commands is not an option.

What happens instead?

The first notification is shown correctly, any after that show up as a blurred rectangle in its place.

Here is a video where I first run the script, and then the notify-send commands individually, finally I copy and paste a contrived example from snippets of the script to demonstrate the commands should work as expected.

Note that for the first 30 seconds or so, I am waiting for the script to finish running. After that I start running things manually. Look in the top-right.

https://youtu.be/UvopfreWfa4

Here is the full script that I am working with:

#! /usr/bin/env bash

set -eu

# DISABLE_DEVICES=(
# 	# AT Translated Set 2 Keyboard
# 	/dev/input/event0
# 	# Sleep Button
# 	/dev/input/event9
# 	# Power Button
# 	/dev/input/event10
# 	# Lid Switch
# 	/dev/input/event8
# 	# ThinkPad Extra Buttons
# 	/dev/input/event15
# 	# SynPS/2 Synaptics Touchpad
# 	/dev/input/event19
# 	# TPPS/2 Elan TrackPoint
# 	/dev/input/event21
# )

# DISABLE_DEVICES=
: "${DISABLE_DURATION:=30}"
: "${NOTIFICATION_USER:="${SUDO_USER-$USER}"}"
: "${NOTIFICATION_COUNTDOWN:=28}"
: "${NOTIFICATION_TIMEOUT:=2000}"
: "${NOTIFICATION_TEXT_SIZE:=x-large}"
: "${NOTIFICATION_ICON_CATEGORY:=devices}"
: "${NOTIFICATION_ICON_NAME:=input-keyboard}"
: "${NOTIFICATION_URGENCY:=critical}"
: "${NOTIFICATION_TITLE:=Input/Keyboard}"

__NOTIFICATION_COUNTDOWN_TIMEOUT=2000

su "$NOTIFICATION_USER" -c "$(
cat <<- EOF
	notify-send \
		'$NOTIFICATION_TITLE' \
		"<b><span size='$NOTIFICATION_TEXT_SIZE'>Disabled</span></b>\\n${#DISABLE_DEVICES[@]} devices" \
		-u $NOTIFICATION_URGENCY \
		-t $NOTIFICATION_TIMEOUT \
		-c $NOTIFICATION_ICON_CATEGORY \
		-i $NOTIFICATION_ICON_NAME \
		-h string:synchronous:disable-keyboard
EOF
)"

pids=()

# for event in "${DISABLE_DEVICES[@]}"; do
# 	evtest --grab "$event" &> /dev/null &
# 	pids+=($!)
# done

sleep $((DISABLE_DURATION - NOTIFICATION_COUNTDOWN))

for i in $(seq 0 $NOTIFICATION_COUNTDOWN); do
	i=$((NOTIFICATION_COUNTDOWN - i))
	su "$NOTIFICATION_USER" -c "$(
	cat <<- EOF
		notify-send \
			'$NOTIFICATION_TITLE' \
			"Enabling ${#DISABLE_DEVICES[@]} devices in <b>$i seconds</b>" \
			-u $NOTIFICATION_URGENCY \
			-t $__NOTIFICATION_COUNTDOWN_TIMEOUT \
			-c $NOTIFICATION_ICON_CATEGORY \
			-i $NOTIFICATION_ICON_NAME \
			-h "int:value:$((100 * i / NOTIFICATION_COUNTDOWN))" \
			-h string:synchronous:disable-keyboard
	EOF
	)"
	sleep 1
done

# kill "${pids[@]}"

su "$NOTIFICATION_USER" -c "$(
cat <<- EOF
	notify-send \
		'$NOTIFICATION_TITLE' \
		"<b><span size='$NOTIFICATION_TEXT_SIZE'>Enabled</span></b>\\n${#DISABLE_DEVICES[@]} devices" \
		-u $NOTIFICATION_URGENCY \
		-t $NOTIFICATION_TIMEOUT \
		-c $NOTIFICATION_ICON_CATEGORY \
		-i $NOTIFICATION_ICON_NAME \
		-h string:synchronous:disable-keyboard
EOF
)"
@bebehei
Copy link
Member

bebehei commented Nov 22, 2022

Sending notifications from another user is a tedious task. Honestly, I think this is actually a total antipattern, too.

But nevertheless: Have you read the dunst FAQs? https://dunst-project.org/faq/#sending-notifications-from-another-user-root

I don't see any modification of the DBUS_SESSION_BUS_ADDRESS variable in your script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants