listen: implement systemd socket activation
This commit is contained in:
		
							
								
								
									
										3
									
								
								INSTALL
									
									
									
									
									
								
							
							
						
						
									
										3
									
								
								INSTALL
									
									
									
									
									
								
							@@ -139,6 +139,9 @@ For the sticker database.
 | 
				
			|||||||
libcdio - http://www.gnu.org/software/libcdio/
 | 
					libcdio - http://www.gnu.org/software/libcdio/
 | 
				
			||||||
For playing audio CDs.
 | 
					For playing audio CDs.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					libsystemd-daemon - http://freedesktop.org/wiki/Software/systemd/
 | 
				
			||||||
 | 
					For systemd activation.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pkg-config
 | 
					pkg-config
 | 
				
			||||||
----------
 | 
					----------
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -34,6 +34,7 @@ src_mpd_LDADD = \
 | 
				
			|||||||
	$(FILTER_LIBS) \
 | 
						$(FILTER_LIBS) \
 | 
				
			||||||
	$(ENCODER_LIBS) \
 | 
						$(ENCODER_LIBS) \
 | 
				
			||||||
	$(MIXER_LIBS) \
 | 
						$(MIXER_LIBS) \
 | 
				
			||||||
 | 
						$(SYSTEMD_DAEMON_LIBS) \
 | 
				
			||||||
	$(GLIB_LIBS)
 | 
						$(GLIB_LIBS)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
mpd_headers = \
 | 
					mpd_headers = \
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										1
									
								
								NEWS
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								NEWS
									
									
									
									
									
								
							@@ -34,6 +34,7 @@ ver 0.17 (2011/??/??)
 | 
				
			|||||||
* cue: show CUE track numbers
 | 
					* cue: show CUE track numbers
 | 
				
			||||||
* allow port specification in "bind_to_address" settings
 | 
					* allow port specification in "bind_to_address" settings
 | 
				
			||||||
* support floating point samples
 | 
					* support floating point samples
 | 
				
			||||||
 | 
					* systemd socket activation
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
ver 0.16.8 (2012/??/??)
 | 
					ver 0.16.8 (2012/??/??)
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										12
									
								
								configure.ac
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								configure.ac
									
									
									
									
									
								
							@@ -353,6 +353,11 @@ AC_ARG_ENABLE(sqlite,
 | 
				
			|||||||
		[enable support for the SQLite database]),,
 | 
							[enable support for the SQLite database]),,
 | 
				
			||||||
	[enable_sqlite=auto])
 | 
						[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,
 | 
					AC_ARG_ENABLE(tcp,
 | 
				
			||||||
	AS_HELP_STRING([--disable-tcp],
 | 
						AS_HELP_STRING([--disable-tcp],
 | 
				
			||||||
		[disable support for clients connecting via TCP (default: enable)]),,
 | 
							[disable support for clients connecting via TCP (default: enable)]),,
 | 
				
			||||||
@@ -495,6 +500,13 @@ if
 | 
				
			|||||||
	AC_MSG_ERROR([No client interfaces configured!])
 | 
						AC_MSG_ERROR([No client interfaces configured!])
 | 
				
			||||||
fi
 | 
					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 ---------------------------------------------------------------------------
 | 
				
			||||||
dnl LIBC Features
 | 
					dnl LIBC Features
 | 
				
			||||||
dnl ---------------------------------------------------------------------------
 | 
					dnl ---------------------------------------------------------------------------
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										41
									
								
								doc/user.xml
									
									
									
									
									
								
							
							
						
						
									
										41
									
								
								doc/user.xml
									
									
									
									
									
								
							@@ -99,6 +99,47 @@ cd mpd-version</programlisting>
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
      <programlisting>make install</programlisting>
 | 
					      <programlisting>make install</programlisting>
 | 
				
			||||||
    </section>
 | 
					    </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>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  <chapter>
 | 
					  <chapter>
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										36
									
								
								src/listen.c
									
									
									
									
									
								
							
							
						
						
									
										36
									
								
								src/listen.c
									
									
									
									
									
								
							@@ -28,6 +28,10 @@
 | 
				
			|||||||
#include <string.h>
 | 
					#include <string.h>
 | 
				
			||||||
#include <assert.h>
 | 
					#include <assert.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifdef ENABLE_SYSTEMD_DAEMON
 | 
				
			||||||
 | 
					#include <systemd/sd-daemon.h>
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#undef G_LOG_DOMAIN
 | 
					#undef G_LOG_DOMAIN
 | 
				
			||||||
#define G_LOG_DOMAIN "listen"
 | 
					#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
 | 
					bool
 | 
				
			||||||
listen_global_init(GError **error_r)
 | 
					listen_global_init(GError **error_r)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
@@ -72,6 +100,14 @@ listen_global_init(GError **error_r)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	listen_socket = server_socket_new(listen_callback, NULL);
 | 
						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) {
 | 
						if (param != NULL) {
 | 
				
			||||||
		/* "bind_to_address" is configured, create listeners
 | 
							/* "bind_to_address" is configured, create listeners
 | 
				
			||||||
		   for all values */
 | 
							   for all values */
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user