listen: implement systemd socket activation

This commit is contained in:
Max Kellermann 2012-02-13 20:48:51 +01:00
parent f529441400
commit 7338b16c18
6 changed files with 94 additions and 0 deletions

View File

@ -139,6 +139,9 @@ For the sticker database.
libcdio - http://www.gnu.org/software/libcdio/
For playing audio CDs.
libsystemd-daemon - http://freedesktop.org/wiki/Software/systemd/
For systemd activation.
pkg-config
----------

View File

@ -34,6 +34,7 @@ src_mpd_LDADD = \
$(FILTER_LIBS) \
$(ENCODER_LIBS) \
$(MIXER_LIBS) \
$(SYSTEMD_DAEMON_LIBS) \
$(GLIB_LIBS)
mpd_headers = \

1
NEWS
View File

@ -34,6 +34,7 @@ ver 0.17 (2011/??/??)
* cue: show CUE track numbers
* allow port specification in "bind_to_address" settings
* support floating point samples
* systemd socket activation
ver 0.16.8 (2012/??/??)

View File

@ -353,6 +353,11 @@ AC_ARG_ENABLE(sqlite,
[enable support for the SQLite database]),,
[enable_sqlite=auto])
AC_ARG_ENABLE(systemd-daemon,
AS_HELP_STRING([--enable-systemd-daemon],
[use the systemd daemon library (default=auto)]),,
[enable_systemd_daemon=auto])
AC_ARG_ENABLE(tcp,
AS_HELP_STRING([--disable-tcp],
[disable support for clients connecting via TCP (default: enable)]),,
@ -495,6 +500,13 @@ if
AC_MSG_ERROR([No client interfaces configured!])
fi
MPD_AUTO_PKG(systemd_daemon, SYSTEMD_DAEMON, libsystemd-daemon,
[systemd activation], [libsystemd-daemon not found])
AM_CONDITIONAL(ENABLE_SYSTEMD_DAEMON, test x$enable_systemd_daemon = xyes)
if test x$enable_systemd_daemon = xyes; then
AC_DEFINE([ENABLE_SYSTEMD_DAEMON], 1, [Define to use the systemd daemon library])
fi
dnl ---------------------------------------------------------------------------
dnl LIBC Features
dnl ---------------------------------------------------------------------------

View File

@ -99,6 +99,47 @@ cd mpd-version</programlisting>
<programlisting>make install</programlisting>
</section>
<section>
<title><filename>systemd</filename> socket activation</title>
<para>
Using <filename>systemd</filename>, you can launch
<filename>mpd</filename> on demand when the first client
attempts to connect. Create two files in
<filename>/etc/systemd/system/</filename>; first
<filename>mpd.socket</filename>:
</para>
<programlisting>[Socket]
ListenStream=/run/mpd.socket
ListenStream=6600
[Install]
WantedBy=sockets.target</programlisting>
<para>
Now create <filename>mpd.service</filename>:
</para>
<programlisting>[Unit]
Description=Music Player Daemon
After=sound.target
[Service]
ExecStart=/usr/bin/mpd --stdout --no-daemon</programlisting>
<para>
Start the socket:
</para>
<programlisting>systemctl enable mpd.socket
systemctl start mpd.socket</programlisting>
<para>
In this configuration, <filename>mpd</filename> will ignore
the <varname>bind_to_address</varname> and
<varname>port</varname> settings.
</para>
</section>
</chapter>
<chapter>

View File

@ -28,6 +28,10 @@
#include <string.h>
#include <assert.h>
#ifdef ENABLE_SYSTEMD_DAEMON
#include <systemd/sd-daemon.h>
#endif
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "listen"
@ -61,6 +65,30 @@ listen_add_config_param(unsigned int port,
}
}
static bool
listen_systemd_activation(GError **error_r)
{
#ifdef ENABLE_SYSTEMD_DAEMON
int n = sd_listen_fds(true);
if (n <= 0) {
if (n < 0)
g_warning("sd_listen_fds() failed: %s",
g_strerror(-n));
return false;
}
for (int i = SD_LISTEN_FDS_START, end = SD_LISTEN_FDS_START + n;
i != end; ++i)
if (!server_socket_add_fd(listen_socket, i, error_r))
return false;
return true;
#else
(void)error_r;
return false;
#endif
}
bool
listen_global_init(GError **error_r)
{
@ -72,6 +100,14 @@ listen_global_init(GError **error_r)
listen_socket = server_socket_new(listen_callback, NULL);
if (listen_systemd_activation(&error))
return true;
if (error != NULL) {
g_propagate_error(error_r, error);
return false;
}
if (param != NULL) {
/* "bind_to_address" is configured, create listeners
for all values */