From 5d0fda7b6a83f6583a86c1b9af06bdf9de4d42fc Mon Sep 17 00:00:00 2001
From: use <user@localhost>
Date: Sat, 16 Jul 2011 21:28:16 +0200
Subject: [PATCH] Small patch to mute the sound card Master channel on startup
 to prevent unwanted playing of sounds.

---
 config/chroot_local-hooks/52-update-rc.d           |    1 +
 .../etc/init.d/tails-alsa-mute                     |  146 ++++++++++++++++++++
 2 files changed, 147 insertions(+), 0 deletions(-)
 create mode 100755 config/chroot_local-includes/etc/init.d/tails-alsa-mute

diff --git a/config/chroot_local-hooks/52-update-rc.d b/config/chroot_local-hooks/52-update-rc.d
index 30c1362..efaa8d2 100755
--- a/config/chroot_local-hooks/52-update-rc.d
+++ b/config/chroot_local-hooks/52-update-rc.d
@@ -8,6 +8,7 @@ update-rc.d tails-kexec                    stop 85 0 6 .
 update-rc.d tails-wifi start 17 S .
 update-rc.d memlockd start 22 2 3 4 5 .
 update-rc.d tails-sdmem-on-media-removal start 23 2 3 4 5 . stop 01 0 6
+update-rc.d tails-alsa-mute defaults
 
 # we run Tor ourselves after HTP via NetworkManager hooks
 update-rc.d tor disable
diff --git a/config/chroot_local-includes/etc/init.d/tails-alsa-mute b/config/chroot_local-includes/etc/init.d/tails-alsa-mute
new file mode 100755
index 0000000..4c9ec7b
--- /dev/null
+++ b/config/chroot_local-includes/etc/init.d/tails-alsa-mute
@@ -0,0 +1,146 @@
+#!/bin/sh
+#
+# alsa-mute initscript
+#
+### BEGIN INIT INFO
+# Provides:          alsa-mute
+# Required-Start:    $remote_fs udev alsa-utils
+# Required-Stop:     $remote_fs
+# Default-Start:     S
+# Default-Stop:      0 1 6
+# Short-Description: Mute channels in ALSA
+# Description:       This script mutes and zeroes channel levels on
+#                    bootup. Muting the channels prevents
+#                    accidential sound playing for Tails users.
+#                    To hear sounds, just unmute and raise the volume
+#                    levels of the channels you need.
+#                    This script is just a copy of Debians alsa-utils initscript
+#                    with unneccessary parts removed and a call of the
+#                    mute procedure on start). The alsa functions are
+#                    outsourced to this script so we don't need to
+#                    patch alsa-utils and update every time alsa-utils changes.
+### END INIT INFO
+
+# Don't use set -e; check exit status instead
+
+# Exit silently if package is no longer installed
+[ -x /usr/sbin/alsactl ] || exit 0
+
+PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
+MYNAME=/etc/init.d/alsa-utils
+
+. /lib/lsb/init-functions
+
+# $1 EXITSTATUS
+# [$2 MESSAGE]
+log_action_end_msg_and_exit()
+{
+	log_action_end_msg "$1" ${2:+"$2"}
+	exit $1
+}
+
+# $1 PROGRAM
+executable()
+{
+	# If which is not available then we must be running before
+	# /usr is mounted on a system that has which in /usr/bin/.
+	# Conclude that $1 is not executable.
+	[ -x /bin/which ] || [ -x /usr/bin/which ] || return 1
+	which "$1" >/dev/null 2>&1
+}
+
+executable amixer || { echo "${MYNAME}: Error: No amixer program available." >&2 ; exit 1 ; }
+
+bugout() { echo "${MYNAME}: Programming error" >&2 ; exit 123 ; }
+
+echo_card_indices()
+{
+	if [ -f /proc/asound/cards ] ; then
+		sed -n -e's/^[[:space:]]*\([0-7]\)[[:space:]].*/\1/p' /proc/asound/cards
+	fi
+}
+
+filter_amixer_output()
+{
+	sed \
+		-e '/Unable to find simple control/d' \
+		-e '/Unknown playback setup/d' \
+		-e '/^$/d'
+}
+
+# The following functions try to set many controls.
+# No card has all the controls and so some of the attempts are bound to fail.
+# Because of this, the functions can't return useful status values.
+
+# $1 <control>
+# $CARDOPT
+mute_and_zero_level()
+{
+	{ [ "$1" ] && [ "$CARDOPT" ] ; } || bugout
+	amixer $CARDOPT -q set "$1" "0%" mute 2>&1 | filter_amixer_output || :
+	return 0
+}
+
+# Please see /etc/init.d/alsa-utils if you need to mute
+# more channels.
+
+# $1 <card ID>
+mute_and_zero_levels_on_card()
+{
+	CARDOPT="-c $1"
+	mute_and_zero_level "Master"
+	return 0
+}
+
+# $1 <card ID> | "all"
+mute_and_zero_levels()
+{
+	TTZML_RETURNSTATUS=0
+	case "$1" in
+	  all)
+		for CARD in $(echo_card_indices) ; do
+			mute_and_zero_levels_on_card "$CARD" || TTZML_RETURNSTATUS=1
+		done
+		;;
+	  *)
+		mute_and_zero_levels_on_card "$1" || TTZML_RETURNSTATUS=1
+		;;
+	esac
+	return $TTZML_RETURNSTATUS
+}
+
+
+# $1 <card ID> | "all"
+card_OK()
+{
+	[ "$1" ] || bugout
+	if [ "$1" = all ] ; then
+		[ -d /proc/asound ]
+		return $?
+	else
+		[ -d "/proc/asound/card$1" ] || [ -d "/proc/asound/$1" ]
+		return $?
+	fi
+}
+
+# If a card identifier is provided in $2 then regard it as an error
+# if that card is not present; otherwise don't regard it as an error.
+
+case "$1" in
+  start)
+	EXITSTATUS=0
+	TARGET_CARD="$2"
+	case "$TARGET_CARD" in
+	  ""|all) TARGET_CARD=all ; log_action_begin_msg "Muting ALSA" ;;
+	  *) log_action_begin_msg "Muting ALSA card ${TARGET_CARD}" ;;
+	esac
+	card_OK "$TARGET_CARD" || log_action_end_msg_and_exit "$( [ ! "$2" ] ; echo $? ; )" "none loaded"
+	mute_and_zero_levels "$TARGET_CARD" || EXITSTATUS=1
+	log_action_end_msg_and_exit "$EXITSTATUS"
+	;;
+  *)
+	echo "Usage: $MYNAME {start [CARD]}" >&2
+	exit 3
+	;;
+esac
+
-- 
1.7.5.4

