From f61a5f52004249e54cfee5e478002daa016161f3 Mon Sep 17 00:00:00 2001
From: Max Kellermann <max@musicpd.org>
Date: Tue, 13 Dec 2016 08:31:21 +0100
Subject: [PATCH 1/9] configure.ac: prepare for 0.19.21

---
 NEWS         | 2 ++
 configure.ac | 4 ++--
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/NEWS b/NEWS
index 6285a3312..564c99487 100644
--- a/NEWS
+++ b/NEWS
@@ -1,3 +1,5 @@
+ver 0.19.20 (not yet released)
+
 ver 0.19.20 (2016/12/09)
 * protocol
   - "setprio" re-enqueues old song if priority has been raised
diff --git a/configure.ac b/configure.ac
index 67e4fd502..9277c5f71 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1,10 +1,10 @@
 AC_PREREQ(2.60)
 
-AC_INIT(mpd, 0.19.20, musicpd-dev-team@lists.sourceforge.net)
+AC_INIT(mpd, 0.19.21, musicpd-dev-team@lists.sourceforge.net)
 
 VERSION_MAJOR=0
 VERSION_MINOR=19
-VERSION_REVISION=20
+VERSION_REVISION=21
 VERSION_EXTRA=0
 
 AC_CONFIG_SRCDIR([src/Main.cxx])

From 7146f825b279b6882b5372d7a5bec38d69848259 Mon Sep 17 00:00:00 2001
From: Max Kellermann <max@musicpd.org>
Date: Tue, 13 Dec 2016 08:34:05 +0100
Subject: [PATCH 2/9] decoder/ffmpeg: fix double free bug

From the avformat_open_input() API documentation:

 "Note that a user-supplied AVFormatContext will be freed on failure."

https://bugs.musicpd.org/view.php?id=4607
---
 NEWS                                        | 2 ++
 src/decoder/plugins/FfmpegDecoderPlugin.cxx | 1 -
 2 files changed, 2 insertions(+), 1 deletion(-)

diff --git a/NEWS b/NEWS
index 564c99487..aa3eece70 100644
--- a/NEWS
+++ b/NEWS
@@ -1,4 +1,6 @@
 ver 0.19.20 (not yet released)
+* decoder
+  - ffmpeg: fix crash bug
 
 ver 0.19.20 (2016/12/09)
 * protocol
diff --git a/src/decoder/plugins/FfmpegDecoderPlugin.cxx b/src/decoder/plugins/FfmpegDecoderPlugin.cxx
index fc287328a..915ad3e41 100644
--- a/src/decoder/plugins/FfmpegDecoderPlugin.cxx
+++ b/src/decoder/plugins/FfmpegDecoderPlugin.cxx
@@ -71,7 +71,6 @@ FfmpegOpenInput(AVIOContext *pb,
 
 	int err = avformat_open_input(&context, filename, fmt, nullptr);
 	if (err < 0) {
-		avformat_free_context(context);
 		SetFfmpegError(error, err, "avformat_open_input() failed");
 		return nullptr;
 	}

From c2da6dd45b17e6bf31cd2590769f48a76ed9f683 Mon Sep 17 00:00:00 2001
From: Max Kellermann <max@musicpd.org>
Date: Tue, 13 Dec 2016 08:36:42 +0100
Subject: [PATCH 3/9] test/test_queue_priority: fix unit test failure after
 recent "setprio" change

---
 NEWS                         |  1 +
 test/test_queue_priority.cxx | 15 ---------------
 2 files changed, 1 insertion(+), 15 deletions(-)

diff --git a/NEWS b/NEWS
index aa3eece70..55be6e133 100644
--- a/NEWS
+++ b/NEWS
@@ -1,6 +1,7 @@
 ver 0.19.20 (not yet released)
 * decoder
   - ffmpeg: fix crash bug
+* fix unit test failure after recent "setprio" change
 
 ver 0.19.20 (2016/12/09)
 * protocol
diff --git a/test/test_queue_priority.cxx b/test/test_queue_priority.cxx
index 517cb028c..171585dec 100644
--- a/test/test_queue_priority.cxx
+++ b/test/test_queue_priority.cxx
@@ -164,21 +164,6 @@ QueuePriorityTest::TestPriority()
 
 	check_descending_priority(&queue, current_order + 1);
 
-	/* priority=60 for the old prio50 item; must not be moved,
-	   because it's before the current song, and it's status
-	   hasn't changed (it was already higher before) */
-
-	unsigned c_order = 0;
-	unsigned c_position = queue.OrderToPosition(c_order);
-	CPPUNIT_ASSERT_EQUAL(50u, unsigned(queue.items[c_position].priority));
-	queue.SetPriority(c_position, 60, current_order);
-
-	current_order = queue.PositionToOrder(current_position);
-	CPPUNIT_ASSERT_EQUAL(3u, current_order);
-
-	c_order = queue.PositionToOrder(c_position);
-	CPPUNIT_ASSERT_EQUAL(0u, c_order);
-
 	/* move the prio=20 item back */
 
 	a_order = queue.PositionToOrder(a_position);

From 3fc9d50adb9ed1586302ab47bf677f6e72ff7a88 Mon Sep 17 00:00:00 2001
From: Max Kellermann <max@musicpd.org>
Date: Tue, 13 Dec 2016 10:23:57 +0100
Subject: [PATCH 4/9] doc/user: fix --with-systemdsystemunitdir example

---
 doc/user.xml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/doc/user.xml b/doc/user.xml
index f4317f33d..caadb9331 100644
--- a/doc/user.xml
+++ b/doc/user.xml
@@ -151,7 +151,7 @@ apt-get install g++ \
         <application>systemd</application> unit files: a "service"
         unit and a "socket" unit.  These will only be installed when
         <application>MPD</application> was configured with
-        <parameter>--with-systemdsystemunitdir=/lib/systemd</parameter>.
+        <parameter>--with-systemdsystemunitdir=/lib/systemd/system</parameter>.
       </para>
 
       <para>

From 53e22b81ef026e873c992bca35e27b41580a0bf8 Mon Sep 17 00:00:00 2001
From: Max Kellermann <max@musicpd.org>
Date: Tue, 13 Dec 2016 08:42:05 +0100
Subject: [PATCH 5/9] systemd: add "system" sub directory

---
 .gitignore                          | 2 +-
 Makefile.am                         | 4 ++--
 configure.ac                        | 2 +-
 systemd/{ => system}/mpd.service.in | 0
 systemd/{ => system}/mpd.socket     | 0
 5 files changed, 4 insertions(+), 4 deletions(-)
 rename systemd/{ => system}/mpd.service.in (100%)
 rename systemd/{ => system}/mpd.socket (100%)

diff --git a/.gitignore b/.gitignore
index ba6d3b30e..3ee0de0b0 100644
--- a/.gitignore
+++ b/.gitignore
@@ -35,7 +35,7 @@ tags
 /mkinstalldirs
 /build
 /src/mpd
-/systemd/mpd.service
+/systemd/system/mpd.service
 /stamp-h1
 
 /src/dsd2pcm/dsd2pcm
diff --git a/Makefile.am b/Makefile.am
index 744b96603..029958adb 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1449,8 +1449,8 @@ FILTER_LIBS = \
 
 if HAVE_SYSTEMD
 systemdsystemunit_DATA = \
-	systemd/mpd.socket \
-	systemd/mpd.service
+	systemd/system/mpd.socket \
+	systemd/system/mpd.service
 endif
 
 
diff --git a/configure.ac b/configure.ac
index 9277c5f71..84f9b686d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1905,7 +1905,7 @@ dnl Generate files
 dnl ---------------------------------------------------------------------------
 AC_CONFIG_FILES(Makefile)
 AC_CONFIG_FILES(doc/doxygen.conf)
-AC_CONFIG_FILES(systemd/mpd.service)
+AC_CONFIG_FILES(systemd/system/mpd.service)
 AC_OUTPUT
 
 echo 'MPD is ready for compilation, type "make" to begin.'
diff --git a/systemd/mpd.service.in b/systemd/system/mpd.service.in
similarity index 100%
rename from systemd/mpd.service.in
rename to systemd/system/mpd.service.in
diff --git a/systemd/mpd.socket b/systemd/system/mpd.socket
similarity index 100%
rename from systemd/mpd.socket
rename to systemd/system/mpd.socket

From ab332d7b2eb6ebc1052885bdcebac9c2673a7dde Mon Sep 17 00:00:00 2001
From: Max Kellermann <max@musicpd.org>
Date: Tue, 13 Dec 2016 08:41:55 +0100
Subject: [PATCH 6/9] systemd: add user unit

The user unit omits the "ProtectKernelModules" setting which fails
with modular kernels:

 Failed at step CAPABILITIES spawning /usr/bin/mpd: Operation not permitted

It is unfortunate that systemd (version 232) is unable to reduce its
own capabilities, because this requires us to split system and user
units.

 https://bugs.musicpd.org/view.php?id=4608
---
 Makefile.am                 |  5 +++++
 NEWS                        |  1 +
 configure.ac                | 17 +++++++++++++++++
 doc/user.xml                | 27 +++++++++++++++++++++++++++
 systemd/user/mpd.service.in | 29 +++++++++++++++++++++++++++++
 5 files changed, 79 insertions(+)
 create mode 100644 systemd/user/mpd.service.in

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</programlisting>
         <varname>port</varname> settings.
       </para>
     </section>
+
+    <section id="systemd_user">
+      <title><filename>systemd</filename> user unit</title>
+
+      <para>
+        You can launch <application>MPD</application> as a
+        <filename>systemd</filename> user unit.  The service file will
+        only be installed when <application>MPD</application> was
+        configured with
+        <parameter>--with-systemduserunitdir=/usr/lib/systemd/user</parameter>
+        or
+        <parameter>--with-systemduserunitdir=$HOME/.local/share/systemd/user</parameter>.
+      </para>
+
+      <para>
+        Once the user unit is installed, you can start and stop
+        <application>MPD</application> like any other service:
+      </para>
+
+      <programlisting>systemctl --user start mpd</programlisting>
+
+      <para>
+        To auto-start <application>MPD</application> upon login, type:
+      </para>
+
+      <programlisting>systemctl --user enable mpd</programlisting>
+    </section>
   </chapter>
 
   <chapter id="config">
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

From eb2b567da61f33e0e0f1c1e022349a6d97d82b5d Mon Sep 17 00:00:00 2001
From: Max Kellermann <max@musicpd.org>
Date: Tue, 13 Dec 2016 10:45:53 +0100
Subject: [PATCH 7/9] NEWS: fix version number

---
 NEWS | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/NEWS b/NEWS
index 7c7b8b196..416eef5d7 100644
--- a/NEWS
+++ b/NEWS
@@ -1,4 +1,4 @@
-ver 0.19.20 (not yet released)
+ver 0.19.21 (not yet released)
 * decoder
   - ffmpeg: fix crash bug
 * fix unit test failure after recent "setprio" change

From 7e43fb79af7ceb3999dcbe309253b16406fe9429 Mon Sep 17 00:00:00 2001
From: Max Kellermann <max@musicpd.org>
Date: Tue, 13 Dec 2016 10:53:41 +0100
Subject: [PATCH 8/9] Makefile.am: fix mpd.socket path in EXTRA_DIST

---
 Makefile.am | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/Makefile.am b/Makefile.am
index ec1bb0bbf..a7746e2f7 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -2171,7 +2171,7 @@ EXTRA_DIST = $(doc_DATA) autogen.sh \
 	$(wildcard $(srcdir)/scripts/*.rb) \
 	$(man_MANS) $(DOCBOOK_FILES) doc/mpdconf.example doc/doxygen.conf \
 	$(wildcard $(srcdir)/doc/include/*.xml) \
-	systemd/mpd.socket \
+	systemd/system/mpd.socket \
 	android/AndroidManifest.xml \
 	android/build.py \
 	android/custom_rules.xml \

From 3bbcda917ce7cb2fa6dcc90d6c38333ca855c941 Mon Sep 17 00:00:00 2001
From: Max Kellermann <max.kellermann@gmail.com>
Date: Tue, 13 Dec 2016 10:54:04 +0100
Subject: [PATCH 9/9] release v0.19.21

---
 NEWS | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/NEWS b/NEWS
index 416eef5d7..f839258dc 100644
--- a/NEWS
+++ b/NEWS
@@ -1,4 +1,4 @@
-ver 0.19.21 (not yet released)
+ver 0.19.21 (2016/12/13)
 * decoder
   - ffmpeg: fix crash bug
 * fix unit test failure after recent "setprio" change