[systemd-devel] Packaging systemd user-instance service files

Mantas Mikulėnas grawity at gmail.com
Fri Jul 17 14:17:51 PDT 2015


On Fri, Jul 17, 2015 at 9:38 PM, Ahmed S. Darwish <darwish.07 at gmail.com>
wrote:

> Hi everyone,
>
> I'm currently transforming a network daemon to become a native
> ``Type=notify'' systemd service. This daemon uses native PulseAudio
> simple APIs for output.
>
> Due to the PulseAudio dependency, the service needs to run under the
> user session. Otherwise, all PulseAudio APIs [e.g. pa_simple_new()]
> returns a ``Connection Refused'' error. [1]
>
> Given the above, I've built the following service file:
>
>     [Unit]
>     Description=AirTunes Synchronous Audio Service
>     [Service]
>     Type=notify
>     ExecStart=/usr/local/bin/shairport-sync
>     [Install]
>     WantedBy=default.target
>
> And the following snippet in the package Makefile.am:
>
>     cp scripts/shairport-sync.service /usr/lib/systemd/user/
>     systemctl --user daemon-reload
>     systemctl --user enable shairport-sync.service
>     systemctl --user start shairport-sync.service
>

So the real problem is not that it *doesn't work*, but rather that it
*shouldn't
be done*. That makefile is mixing entirely separate things – installation
(packaging), global configuration, and per-user configuration.

(Not to mention the dangerous assumptions that 1) there's exactly one user
logged in during installation, and 2) that they'll actually want to run the
program right now...)

As you can see, the service is properly installed under
> ``/usr/lib/systemd/user/'' to run under the systemd user instance.
>
> Now the problem is that the Makefile commands above run as root,
> and thus all the ``systemctl --user'' commands fail with:
>
>     Failed to get D-Bus connection: Connection refused
>
> So, the question is, can I start ``systemctl --user daemon-reload''
> and ``systemctl --user enable'' above in some form while the
> Makefile is run from root?
>

No, but you don't need to. Just install the file to /usr/lib/systemd/user,
and that's it. It'll be available to all users.

If you want to forcefully enable the service for all users, then also
symlink it into /usr/lib/systemd/user/default.target.wants/, which is
almost exactly what `systemctl enable` does (except system-wide). That'll
make it start on login for everyone.

But the general rule is, do not start user-session processes from system
tasks.

-- 
Mantas Mikulėnas <grawity at gmail.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freedesktop.org/archives/systemd-devel/attachments/20150718/30b1c18d/attachment.html>


More information about the systemd-devel mailing list