rnestler / reboot-arch-btw

Checks if your ArchLinux needs a reboot due to a kernel update
GNU General Public License v3.0
24 stars 4 forks source link

Pacman hook #33

Open dbrgn opened 4 years ago

dbrgn commented 4 years ago

This hook runs reboot-arch-btw when linux is upgraded:

danilo@x2000:~$ cat /usr/share/libalpm/hooks/99-reboot-arch-btw.hook
[Trigger]
Type = Package
Target = linux
Operation = Upgrade

[Action]
Description = Check whether a reboot is required
Depends = reboot-arch-btw
When = PostTransaction
Exec = /usr/bin/reboot-arch-btw

Unfortunately DBUS cannot be accessed from the hook right now:

hook

rnestler commented 4 years ago

Hmm. That is probably because it will run as root which won't have access to the dbus session of the user.

rnestler commented 4 years ago

But I should definitely fix the panic :slightly_smiling_face:

rnestler commented 4 years ago

@dbrgn Can you try setting $DBUS_SESSION_BUS_ADDRESS to something like unix:path=/run/user/1000/bus where 1000 is your UID?

dbrgn commented 4 years ago

We can't set env vars in the hook files. Someone on Arch IRC suggested to either hardcode the UID (or make it configurable), or to try to detect users with an active login session (could maybe be detected through DBUS as well).

emansom commented 1 year ago

Checking for D-Bus service presence of rfjakob/systembus-notify, and utilizing it to alert the user when available would resolve this issue.

rnestler commented 1 year ago

I found a solution:

/etc/pacman.d/scripts/reboot-arch-btw-hook.sh

#!/bin/bash

sudo -u $SUDO_USER DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$SUDO_UID/bus /usr/bin/reboot-arch-btw

/etc/pacman.d/hooks/99-reboot-arch-btw.hook

[Trigger]
Operation = Upgrade
Type = Package
Target = *

[Action]
Description = Check whether a reboot is required
Depends = reboot-arch-btw
When = PostTransaction
Exec = /etc/pacman.d/scripts/reboot-arch-btw-hook.sh

This works perfectly fine if you run upgrades with sudo pacman -Syu. If you run upgrades in a cron job or similar you probably need to hard-code the user-name and user-id in the script above.

Edit: If you hardcode it, it also works as a one-liner in the .hook file: Exec = /usr/bin/sudo -u your-user DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/your-uid/bus /usr/bin/reboot-arch-btw

dbrgn commented 1 year ago

@rnestler great, it works! I'm not sure if this is something that could be part of the AUR package? (Or maybe just a separate package that depends on reboot-arch-btw.)

rnestler commented 1 year ago

great, it works! I'm not sure if this is something that could be part of the AUR package? (Or maybe just a separate package that depends on reboot-arch-btw.)

It's hard to write it in a generic way such that it works for all use cases.

Something like this might work:

#!/bin/bash

# go through all logged in users and check if we find a D-BUS bus for them
for user in $(users|tr ' ' '\n'|sort -u); do
    uid=$(id -u "$user")
    if [[ -f "/run/user/$uid/bus" ]]; then
        /usr/bin/sudo -u "$user" DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$UID/bus /usr/bin/reboot-arch-btw
        exit 0
    fi
done

# if not run with notifications disabled
/usr/bin/reboot-arch-btw --disable-notification
rnestler commented 1 year ago

Checking for D-Bus service presence of rfjakob/systembus-notify, and utilizing it to alert the user when available would resolve this issue.

@emansom This looks interesting as well, but I don't use systembus-notify myself, so PR welcome :)