[systemd-devel] "systemd-inhibit" in service files?

Lennart Poettering lennart at poettering.net
Fri Nov 9 16:06:57 PST 2012


On Tue, 06.11.12 22:16, Manuel Reimer (Manuel.Spam at nurfuerspam.de) wrote:

> Hello,
> 
> if I want a daemon to, for example listen for the power button, then
> there are two possible solutions. Either configure systemd to not
> longer handle the power button or register an "inhibitor" to make
> systemd stop handling the power button.
> 
> Is it good practice to use "systemd-inhibit" in service files to use
> the "inhibitor" way to "free" the power button or is the preferred
> way to fix the daemon to inhibit on its own?

If I were you I'd always do that in the service code itself rather than
wrap it in a "system-inhibit" invocation. In fact, calling the bus call
for this is dead simple. Just one bus call, and that's it.

http://www.freedesktop.org/wiki/Software/systemd/inhibit

Yeah, that doc is long, inhibition is a surprisingly complex
topic... That said the actuall code bits you need are tiny. For your
usecase, just do this somewhere at the top of your service (in D-Bus
pseudo code):

fd = Inhibit("handle-power-key", "Power Key Handler", "I am handling the power key now!", "block");

And then you can just leak the fd this returns, if you want to handle
the key during the entire runtime of your service, since the kernel will
close the fd anyway if the process dies. 

Lennart

-- 
Lennart Poettering - Red Hat, Inc.


More information about the systemd-devel mailing list