08 May 2010

Using D-Bus to shutdown the machine

I don't know why I don't like using /sbin/shutdown to switch off the computer :) But that is the fact! I am not using Gnome\KDE\XFCE\whatever_else_complete_DE. I'm running Archlinux with Openbox and several tools. So I wanted to allow non-privileged user to be able to shutdown the computer by a single click. Firstly I had to find out, what should I send over D-Bus (and whom to) to make my PC shutdown. Crawling the web I found an utility which allowed me to find what I needed in a few minutes. It's called DBus-Inspector. So having a message and destination it just remained to send it using dbus-send:
$ dbus-send --system --print-reply --dest=org.freedesktop.Hal \
          /org/freedesktop/Hal/devices/computer \
          org.freedesktop.Hal.Device.SystemPowerManagement.Shutdown
If you will try to execute it, you may receive and error saying that you don't have permissions to send messages to org.freedesktop.Hal.devices.computer interface. If you did, you should open hal.conf from system config directory of D-Bus (mine is /etc/dbus-1/system.d/hal.conf) and add the following policy:
 <policy group="shutdown">
   <allow send_destination="org.freedesktop.Hal"
          send_interface="org.freedesktop.Hal.Device.SystemPowerManagement"/>
 </policy>
Now all the members of group shutdown will be able to switch off the computer by D-Bus. You just have to create the group:
# groupadd shutdown
And add your user to this group:
# gpasswd -a user shutdown
Finally you may want to create a script or desktop entry:
[Desktop Entry]
Icon=system-shutdown
Name=Shutdown
Exec=dbus-send --system --print-reply --dest=org.freedesktop.Hal /org/freedesktop/Hal/devices/computer org.freedesktop.Hal.Device.SystemPowerManagement.Shutdown
Post a Comment