diff --git a/Makefile.am b/Makefile.am
index 029958adb..ec1bb0bbf 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1453,6 +1453,11 @@ systemdsystemunit_DATA = \
systemd/system/mpd.service
endif
+if HAVE_SYSTEMD_USER
+systemduserunit_DATA = \
+ systemd/user/mpd.service
+endif
+
#
# Test programs
diff --git a/NEWS b/NEWS
index 55be6e133..7c7b8b196 100644
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,7 @@ ver 0.19.20 (not yet released)
* decoder
- ffmpeg: fix crash bug
* fix unit test failure after recent "setprio" change
+* systemd: add user unit
ver 0.19.20 (2016/12/09)
* protocol
diff --git a/configure.ac b/configure.ac
index 84f9b686d..8662a6248 100644
--- a/configure.ac
+++ b/configure.ac
@@ -52,6 +52,22 @@ if test "x$with_systemdsystemunitdir" != xno; then
fi
AM_CONDITIONAL(HAVE_SYSTEMD, [test -n "$with_systemdsystemunitdir" -a "x$with_systemdsystemunitdir" != xno ])
+AC_ARG_WITH([systemduserunitdir],
+ AS_HELP_STRING([--with-systemduserunitdir=DIR], [Directory for systemd service files]),
+ [], [with_systemduserunitdir=no])
+if test "x$with_systemduserunitdir" = xyes; then
+ AC_MSG_CHECKING(for systemd)
+ with_systemduserunitdir=$($PKG_CONFIG --variable=systemduserunitdir systemd)
+ if test -z "$with_systemduserunitdir"; then
+ AC_MSG_ERROR([Failed to detect systemd])
+ fi
+ AC_MSG_RESULT([$with_systemduserunitdir])
+fi
+if test "x$with_systemduserunitdir" != xno; then
+ AC_SUBST([systemduserunitdir], [$with_systemduserunitdir])
+fi
+AM_CONDITIONAL(HAVE_SYSTEMD_USER, [test -n "$with_systemduserunitdir" -a "x$with_systemduserunitdir" != xno ])
+
dnl ---------------------------------------------------------------------------
dnl Declare Variables
dnl ---------------------------------------------------------------------------
@@ -1906,6 +1922,7 @@ dnl ---------------------------------------------------------------------------
AC_CONFIG_FILES(Makefile)
AC_CONFIG_FILES(doc/doxygen.conf)
AC_CONFIG_FILES(systemd/system/mpd.service)
+AC_CONFIG_FILES(systemd/user/mpd.service)
AC_OUTPUT
echo 'MPD is ready for compilation, type "make" to begin.'
diff --git a/doc/user.xml b/doc/user.xml
index caadb9331..c5589ade4 100644
--- a/doc/user.xml
+++ b/doc/user.xml
@@ -167,6 +167,33 @@ systemctl start mpd.socket
port settings.
+
+
+ systemd user unit
+
+
+ You can launch MPD as a
+ systemd user unit. The service file will
+ only be installed when MPD was
+ configured with
+ --with-systemduserunitdir=/usr/lib/systemd/user
+ or
+ --with-systemduserunitdir=$HOME/.local/share/systemd/user.
+
+
+
+ Once the user unit is installed, you can start and stop
+ MPD like any other service:
+
+
+ systemctl --user start mpd
+
+
+ To auto-start MPD upon login, type:
+
+
+ systemctl --user enable mpd
+
diff --git a/systemd/user/mpd.service.in b/systemd/user/mpd.service.in
new file mode 100644
index 000000000..fa0f43d41
--- /dev/null
+++ b/systemd/user/mpd.service.in
@@ -0,0 +1,29 @@
+[Unit]
+Description=Music Player Daemon
+After=network.target sound.target
+
+[Service]
+ExecStart=@prefix@/bin/mpd --no-daemon
+
+# allow MPD to use real-time priority 50
+LimitRTPRIO=50
+LimitRTTIME=infinity
+
+# disallow writing to /usr, /bin, /sbin, ...
+ProtectSystem=yes
+
+# more paranoid security settings
+NoNewPrivileges=yes
+ProtectKernelTunables=yes
+ProtectControlGroups=yes
+# AF_NETLINK is required by libsmbclient, or it will exit() .. *sigh*
+RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX AF_NETLINK
+RestrictNamespaces=yes
+
+# Note that "ProtectKernelModules=yes" is missing in the user unit
+# because systemd 232 is unable to reduce its own capabilities
+# ("Failed at step CAPABILITIES spawning /usr/bin/mpd: Operation not
+# permitted")
+
+[Install]
+WantedBy=default.target