From df1152ee0f3b3761e9956c0084094400d1250705 Mon Sep 17 00:00:00 2001
From: Max Kellermann <max@duempel.org>
Date: Mon, 19 Sep 2011 07:42:11 +0200
Subject: [PATCH 1/6] configure.ac: fix solaris result display

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

diff --git a/NEWS b/NEWS
index 90d3b0fca..cc6874068 100644
--- a/NEWS
+++ b/NEWS
@@ -1,5 +1,7 @@
 ver 0.16.5 (2010/??/??)
-* configure.ac: disable assertions in the non-debugging build
+* configure.ac
+  - disable assertions in the non-debugging build
+  - show solaris plugin result correctly
 * pcm_format: fix 32-to-24 bit conversion (the "silence" bug)
 * input:
   - rewind: reduce heap usage
diff --git a/configure.ac b/configure.ac
index 9a9576271..e87cf114b 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1553,7 +1553,7 @@ results(pulse, [PulseAudio])
 results(mvp, [Media MVP])
 results(shout, [SHOUTcast])
 printf '\n\t'
-results(solaris, [Solaris])
+results(solaris_output, [Solaris])
 results(winmm_output, [WinMM])
 
 if

From 1d8840412fc1dd76a73ee13413cd7fc9a6ff229a Mon Sep 17 00:00:00 2001
From: Max Kellermann <max@duempel.org>
Date: Mon, 19 Sep 2011 07:39:27 +0200
Subject: [PATCH 2/6] configure.ac: add option --enable-solaris-output

Allow enabling the plugin explicitly without running Solaris, to test
the build.
---
 NEWS                               |  1 +
 configure.ac                       | 17 ++++++++++++++---
 src/output/solaris_output_plugin.c | 20 +++++++++++++++++++-
 3 files changed, 34 insertions(+), 4 deletions(-)

diff --git a/NEWS b/NEWS
index cc6874068..af7315360 100644
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,7 @@ ver 0.16.5 (2010/??/??)
 * configure.ac
   - disable assertions in the non-debugging build
   - show solaris plugin result correctly
+  - add option --enable-solaris-output
 * pcm_format: fix 32-to-24 bit conversion (the "silence" bug)
 * input:
   - rewind: reduce heap usage
diff --git a/configure.ac b/configure.ac
index e87cf114b..50e8d8c7a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -324,6 +324,11 @@ AC_ARG_ENABLE(sndfile,
 		[enable sndfile support]),,
 	enable_sndfile=auto)
 
+AC_ARG_ENABLE(solaris_output,
+	AS_HELP_STRING([--enable-solaris-output],
+		[enables the Solaris /dev/audio output]),,
+	[enable_solaris_output=auto])
+
 AC_ARG_ENABLE(sqlite,
 	AS_HELP_STRING([--enable-sqlite],
 		[enable support for the SQLite database]),,
@@ -1370,16 +1375,22 @@ fi
 AM_CONDITIONAL(HAVE_SHOUT, test x$enable_shout = xyes)
 
 dnl --------------------------------- Solaris ---------------------------------
-case "$host_os" in
+
+if test x$enable_solaris_output = xauto; then
+	case "$host_os" in
 	solaris*)
-		AC_DEFINE(ENABLE_SOLARIS_OUTPUT, 1, [Define to enable Solaris /dev/audio support])
 		enable_solaris_output=yes
 		;;
 
 	*)
 		enable_solaris_output=no
 		;;
-esac
+	esac
+fi
+
+if test x$enable_solaris_output = xyes; then
+	AC_DEFINE(ENABLE_SOLARIS_OUTPUT, 1, [Define to enable Solaris /dev/audio support])
+fi
 
 AM_CONDITIONAL(ENABLE_SOLARIS_OUTPUT, test x$enable_solaris_output = xyes)
 
diff --git a/src/output/solaris_output_plugin.c b/src/output/solaris_output_plugin.c
index 22c583805..6f6f507b3 100644
--- a/src/output/solaris_output_plugin.c
+++ b/src/output/solaris_output_plugin.c
@@ -23,7 +23,6 @@
 
 #include <glib.h>
 
-#include <sys/audio.h>
 #include <sys/stropts.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -31,6 +30,25 @@
 #include <fcntl.h>
 #include <errno.h>
 
+#ifdef __sun
+#include <sys/audio.h>
+#else
+
+/* some fake declarations that allow build this plugin on systems
+   other than Solaris, just to see if it compiles */
+
+#define AUDIO_GETINFO 0
+#define AUDIO_SETINFO 0
+#define AUDIO_ENCODING_LINEAR 0
+
+struct audio_info {
+	struct {
+		unsigned sample_rate, channels, precision, encoding;
+	} play;
+};
+
+#endif
+
 #undef G_LOG_DOMAIN
 #define G_LOG_DOMAIN "solaris_output"
 

From b03f9ece05c6f9a6410da48ec024f9d6bed0dc82 Mon Sep 17 00:00:00 2001
From: Max Kellermann <max@duempel.org>
Date: Mon, 19 Sep 2011 20:40:33 +0200
Subject: [PATCH 3/6] glib_socket.h: wrap g_io_channel_*_new() calls portably

The server_socket library (used by the httpd output plugin) didn't
check for WIN32, that's fixed now.
---
 Makefile.am               |  1 +
 src/client_new.c          |  7 ++-----
 src/event_pipe.c          |  7 ++-----
 src/glib_socket.h         | 40 +++++++++++++++++++++++++++++++++++++++
 src/output/httpd_client.c |  7 ++-----
 src/server_socket.c       |  3 ++-
 6 files changed, 49 insertions(+), 16 deletions(-)
 create mode 100644 src/glib_socket.h

diff --git a/Makefile.am b/Makefile.am
index 5e6747e6d..478614406 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -243,6 +243,7 @@ src_mpd_SOURCES = \
 	$(OUTPUT_API_SRC) $(OUTPUT_SRC) \
 	$(MIXER_API_SRC) $(MIXER_SRC) \
 	$(FILTER_SRC) \
+	src/glib_socket.h \
 	src/notify.c \
 	src/audio.c \
 	src/audio_check.c \
diff --git a/src/client_new.c b/src/client_new.c
index 781a36524..0ee21916b 100644
--- a/src/client_new.c
+++ b/src/client_new.c
@@ -22,6 +22,7 @@
 #include "fifo_buffer.h"
 #include "socket_util.h"
 #include "permission.h"
+#include "glib_socket.h"
 
 #include <assert.h>
 #include <sys/types.h>
@@ -82,11 +83,7 @@ void client_new(int fd, const struct sockaddr *sa, size_t sa_length, int uid)
 
 	client = g_new0(struct client, 1);
 
-#ifndef G_OS_WIN32
-	client->channel = g_io_channel_unix_new(fd);
-#else
-	client->channel = g_io_channel_win32_new_socket(fd);
-#endif
+	client->channel = g_io_channel_new_socket(fd);
 	/* GLib is responsible for closing the file descriptor */
 	g_io_channel_set_close_on_unref(client->channel, true);
 	/* NULL encoding means the stream is binary safe; the MPD
diff --git a/src/event_pipe.c b/src/event_pipe.c
index 5b519984f..edfd40eb5 100644
--- a/src/event_pipe.c
+++ b/src/event_pipe.c
@@ -21,6 +21,7 @@
 #include "event_pipe.h"
 #include "fd_util.h"
 #include "mpd_error.h"
+#include "glib_socket.h"
 
 #include <stdbool.h>
 #include <assert.h>
@@ -94,11 +95,7 @@ void event_pipe_init(void)
 	if (ret < 0)
 		MPD_ERROR("Couldn't open pipe: %s", strerror(errno));
 
-#ifndef G_OS_WIN32
-	channel = g_io_channel_unix_new(event_pipe[0]);
-#else
-	channel = g_io_channel_win32_new_fd(event_pipe[0]);
-#endif
+	channel = g_io_channel_new_socket(event_pipe[0]);
 	g_io_channel_set_encoding(channel, NULL, NULL);
 	g_io_channel_set_buffered(channel, false);
 
diff --git a/src/glib_socket.h b/src/glib_socket.h
new file mode 100644
index 000000000..46fab37dd
--- /dev/null
+++ b/src/glib_socket.h
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2003-2011 The Music Player Daemon Project
+ * http://www.musicpd.org
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef MPD_GLIB_SOCKET_H
+#define MPD_GLIB_SOCKET_H
+
+#include <glib.h>
+
+/**
+ * Portable wrapper for g_io_channel_unix_new() or
+ * g_io_channel_win32_new_socket().
+ */
+G_GNUC_MALLOC
+static inline GIOChannel *
+g_io_channel_new_socket(int fd)
+{
+#ifdef G_OS_WIN32
+	return g_io_channel_win32_new_socket(fd);
+#else
+	return g_io_channel_unix_new(fd);
+#endif
+}
+
+#endif
diff --git a/src/output/httpd_client.c b/src/output/httpd_client.c
index 1119a7834..995c1f659 100644
--- a/src/output/httpd_client.c
+++ b/src/output/httpd_client.c
@@ -24,6 +24,7 @@
 #include "page.h"
 #include "icy_server.h"
 #include "glib_compat.h"
+#include "glib_socket.h"
 
 #include <stdbool.h>
 #include <assert.h>
@@ -459,11 +460,7 @@ httpd_client_new(struct httpd_output *httpd, int fd, bool metadata_supported)
 
 	client->httpd = httpd;
 
-#ifndef G_OS_WIN32
-	client->channel = g_io_channel_unix_new(fd);
-#else
-	client->channel = g_io_channel_win32_new_socket(fd);
-#endif
+	client->channel = g_io_channel_new_socket(fd);
 
 	/* GLib is responsible for closing the file descriptor */
 	g_io_channel_set_close_on_unref(client->channel, true);
diff --git a/src/server_socket.c b/src/server_socket.c
index bb7a6f097..c10740676 100644
--- a/src/server_socket.c
+++ b/src/server_socket.c
@@ -22,6 +22,7 @@
 #include "socket_util.h"
 #include "fd_util.h"
 #include "glib_compat.h"
+#include "glib_socket.h"
 
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -218,7 +219,7 @@ server_socket_open(struct server_socket *ss, GError **error_r)
 
 		/* register in the GLib main loop */
 
-		GIOChannel *channel = g_io_channel_unix_new(s->fd);
+		GIOChannel *channel = g_io_channel_new_socket(s->fd);
 		s->source_id = g_io_add_watch(channel, G_IO_IN,
 					      server_socket_in_event, s);
 		g_io_channel_unref(channel);

From 46deb7ca8298084b6169ac6349b24797e35623a7 Mon Sep 17 00:00:00 2001
From: Max Kellermann <max@duempel.org>
Date: Mon, 19 Sep 2011 21:02:29 +0200
Subject: [PATCH 4/6] fd_util: include check.h, verify config.h was included

---
 src/fd_util.h | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/src/fd_util.h b/src/fd_util.h
index 5b80df2c7..647860a97 100644
--- a/src/fd_util.h
+++ b/src/fd_util.h
@@ -36,6 +36,8 @@
 #ifndef FD_UTIL_H
 #define FD_UTIL_H
 
+#include "check.h"
+
 #include <stdbool.h>
 #include <stddef.h>
 

From 77a56c7c5a1ab6bc0d76e0cf55f24071be7c4b5f Mon Sep 17 00:00:00 2001
From: Max Kellermann <max@duempel.org>
Date: Mon, 19 Sep 2011 21:03:08 +0200
Subject: [PATCH 5/6] fd_util: check HAVE_INOTIFY_INIT in header

Don't provide the prototype if inotify_init() was not detected.
---
 src/fd_util.h | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/src/fd_util.h b/src/fd_util.h
index 647860a97..3d465b200 100644
--- a/src/fd_util.h
+++ b/src/fd_util.h
@@ -122,6 +122,8 @@ recvmsg_cloexec(int sockfd, struct msghdr *msg, int flags);
 
 #endif
 
+#ifdef HAVE_INOTIFY_INIT
+
 /**
  * Wrapper for inotify_init(), which sets the CLOEXEC flag (atomically
  * if supported by the OS).
@@ -130,3 +132,5 @@ int
 inotify_init_cloexec(void);
 
 #endif
+
+#endif

From c476819cb1186770b1b7395851e394caac39cbc8 Mon Sep 17 00:00:00 2001
From: Max Kellermann <max@duempel.org>
Date: Mon, 19 Sep 2011 21:04:19 +0200
Subject: [PATCH 6/6] fd_util: add function close_socket()

Wrap close(), use closesocket() on WIN32/WinSock.
---
 NEWS                             |  1 +
 src/client_new.c                 |  5 +++--
 src/fd_util.c                    | 10 ++++++++++
 src/fd_util.h                    |  6 ++++++
 src/output/httpd_output_plugin.c |  4 ++--
 src/server_socket.c              |  2 +-
 src/socket_util.c                |  6 +++---
 7 files changed, 26 insertions(+), 8 deletions(-)

diff --git a/NEWS b/NEWS
index af7315360..2f1907cd7 100644
--- a/NEWS
+++ b/NEWS
@@ -9,6 +9,7 @@ ver 0.16.5 (2010/??/??)
 * decoder:
   - ffmpeg: higher precision timestamps
   - ffmpeg: don't require key frame for seeking
+* WIN32: close sockets properly
 
 
 ver 0.16.4 (2011/09/01)
diff --git a/src/client_new.c b/src/client_new.c
index 0ee21916b..e764a6550 100644
--- a/src/client_new.c
+++ b/src/client_new.c
@@ -19,6 +19,7 @@
 
 #include "config.h"
 #include "client_internal.h"
+#include "fd_util.h"
 #include "fifo_buffer.h"
 #include "socket_util.h"
 #include "permission.h"
@@ -67,7 +68,7 @@ void client_new(int fd, const struct sockaddr *sa, size_t sa_length, int uid)
 			      progname, hostaddr);
 
 			g_free(hostaddr);
-			close(fd);
+			close_socket(fd);
 			return;
 		}
 
@@ -77,7 +78,7 @@ void client_new(int fd, const struct sockaddr *sa, size_t sa_length, int uid)
 
 	if (client_list_is_full()) {
 		g_warning("Max Connections Reached!");
-		close(fd);
+		close_socket(fd);
 		return;
 	}
 
diff --git a/src/fd_util.c b/src/fd_util.c
index 1f3004d0c..4636c19e4 100644
--- a/src/fd_util.c
+++ b/src/fd_util.c
@@ -304,3 +304,13 @@ inotify_init_cloexec(void)
 }
 
 #endif
+
+int
+close_socket(int fd)
+{
+#ifdef WIN32
+	return closesocket(fd);
+#else
+	return close(fd);
+#endif
+}
diff --git a/src/fd_util.h b/src/fd_util.h
index 3d465b200..d74bb30d8 100644
--- a/src/fd_util.h
+++ b/src/fd_util.h
@@ -133,4 +133,10 @@ inotify_init_cloexec(void);
 
 #endif
 
+/**
+ * Portable wrapper for close(); use closesocket() on WIN32/WinSock.
+ */
+int
+close_socket(int fd);
+
 #endif
diff --git a/src/output/httpd_output_plugin.c b/src/output/httpd_output_plugin.c
index 40ad05c3d..2c140a300 100644
--- a/src/output/httpd_output_plugin.c
+++ b/src/output/httpd_output_plugin.c
@@ -201,7 +201,7 @@ httpd_listen_in_event(int fd, const struct sockaddr *address,
 			g_warning("libwrap refused connection (libwrap=%s) from %s",
 			      progname, hostaddr);
 			g_free(hostaddr);
-			close(fd);
+			close_socket(fd);
 			g_mutex_unlock(httpd->mutex);
 			return;
 		}
@@ -222,7 +222,7 @@ httpd_listen_in_event(int fd, const struct sockaddr *address,
 		     httpd->clients_cnt < httpd->clients_max))
 			httpd_client_add(httpd, fd);
 		else
-			close(fd);
+			close_socket(fd);
 	} else if (fd < 0 && errno != EINTR) {
 		g_warning("accept() failed: %s", g_strerror(errno));
 	}
diff --git a/src/server_socket.c b/src/server_socket.c
index c10740676..b4114d2d9 100644
--- a/src/server_socket.c
+++ b/src/server_socket.c
@@ -253,7 +253,7 @@ server_socket_close(struct server_socket *ss)
 			continue;
 
 		g_source_remove(s->source_id);
-		close(s->fd);
+		close_socket(s->fd);
 		s->fd = -1;
 	}
 }
diff --git a/src/socket_util.c b/src/socket_util.c
index 0909765ba..3f7075ed3 100644
--- a/src/socket_util.c
+++ b/src/socket_util.c
@@ -122,7 +122,7 @@ socket_bind_listen(int domain, int type, int protocol,
 	if (ret < 0) {
 		g_set_error(error, listen_quark(), errno,
 			    "setsockopt() failed: %s", g_strerror(errno));
-		close(fd);
+		close_socket(fd);
 		return -1;
 	}
 
@@ -130,7 +130,7 @@ socket_bind_listen(int domain, int type, int protocol,
 	if (ret < 0) {
 		g_set_error(error, listen_quark(), errno,
 			    "%s", g_strerror(errno));
-		close(fd);
+		close_socket(fd);
 		return -1;
 	}
 
@@ -138,7 +138,7 @@ socket_bind_listen(int domain, int type, int protocol,
 	if (ret < 0) {
 		g_set_error(error, listen_quark(), errno,
 			    "listen() failed: %s", g_strerror(errno));
-		close(fd);
+		close_socket(fd);
 		return -1;
 	}