[systemd-devel] Synchronization Between Services at Shutdown

Lennart Poettering lennart at poettering.net
Thu Apr 2 02:06:50 PDT 2015


On Wed, 01.04.15 18:22, Kurt von Laven (kurt at endlessm.com) wrote:

> Hello folks,
> 
> I am trying to ensure that my system DBus service has time to handle a DBus
> method call before it shuts down. My DBus service is implemented as a
> system systemd service; let's call it service A. It runs a single-threaded,
> synchronous event loop that listens for DBus method calls and handles them
> appropriately. This event loop also listens for SIGTERM. When SIGTERM is
> received, service A currently terminates the event loop, performs any
> necessary writes to disk, and releases its resources. I am assuming that
> when the system is shut down cleanly, systemd initially sends SIGTERM to
> systemd services and only sends SIGKILL to abort it abruptly if it takes
> too long to shut down when asked nicely.
> 
> I have another system systemd service, call it service B, that runs an
> event loop listening for SIGTERM. When service B receives a SIGTERM, it
> makes the aforementioned DBus method call and then flushes the system bus.
> Service B specifies Requires=
> <http://www.freedesktop.org/software/systemd/man/systemd.unit.html#Requires=>Service
> A and After=
> <http://www.freedesktop.org/software/systemd/man/systemd.unit.html#Before=>Service
> A. Service A makes no mention of service B in its unit configuration file.
> Does systemd make any sort of guarantee that service A will get a chance to
> handle the DBus method call before receiving SIGTERM, or does my code as
> written have a race condition?

The general rule in systemd is that the shutdown order is the reverse
startup order. If you declare After=foo.service in a service
bar.service, then this will hence have two effects: 1) when both are
started foo.service is started before bar.service. And 2) when both
are stopped bar.service is stopped before foo.service. 

If you want to ensure that bus communication still works in your
shutdown code, you hence need to make sure you place
After=dbus.service in your services, so that you are shut down before
dbus is.

Lennart

-- 
Lennart Poettering, Red Hat


More information about the systemd-devel mailing list