1#! /bin/sh
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or http://www.opensolaris.org/os/licensing.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23# Use is subject to license terms.
24#
25# This is the audio_clean program.
26# 
27# Following is the syntax for calling the script:
28#	scriptname [-s|-f|-i|-I] devicename [-A|-D] [username] [zonename]
29#           [zonepath]
30#
31# $1:	-s for standard cleanup by a user
32#	-f for forced cleanup by an administrator
33#	-i for boot-time initialization (when the system is booted with -r)
34#	-I to suppress error/warning messages; the script is run in the '-i'
35#	mode
36#
37# $2:	devicename - device to be allocated/deallocated, e.g., sr0
38#
39# $3:	-A if cleanup is for allocation, or -D if cleanup is for deallocation.
40#
41# $4:	username - run the script as this user, rather than as the caller.
42#
43# $5:	zonename - zone in which device to be allocated/deallocated
44#
45# $6:	zonepath - root path of zonename
46#
47# Unless the clean script is being called for boot-time
48# initialization, it may communicate with the user via stdin and
49# stdout.  To communicate with the user via CDE dialogs, create a
50# script or link with the same name, but with ".windowing" appended.
51# For example, if the clean script specified in device_allocate is
52# /etc/security/xyz_clean, that script must use stdin/stdout.  If a
53# script named /etc/security/xyz_clean.windowing exists, it must use
54# dialogs.  To present dialogs to the user, the dtksh script
55# /etc/security/lib/wdwmsg may be used.
56#
57# This particular script, audio_clean, will work using stdin/stdout, or
58# using dialogs.  A symbolic link audio_clean.windowing points to
59# audio_clean.
60
61
62trap "" INT TERM QUIT TSTP ABRT
63
64USAGE="usage: $0 [-s|-f|-i|-I] devicename [-A|-D][username][zonename][zonepath]"
65PATH="/usr/bin:/usr/sbin"
66WDWMSG="/etc/security/lib/wdwmsg"
67MODE="allocate"
68
69if [ `basename $0` != `basename $0 .windowing` ]; then
70  WINDOWING="yes"
71else
72  WINDOWING="no"
73fi
74
75#
76# 		*** Shell Function Declarations ***
77#
78
79msg() {
80  	if [ "$WINDOWING" = "yes" ]; then
81	  if [ $MODE = "allocate" ]; then
82	    TITLE="Audio Device Allocation"
83	    else
84	    TITLE="Audio Device Dellocation"
85	  fi
86	  $WDWMSG "$*" "$TITLE" OK 
87	else  
88	  echo "$*"
89	fi
90}
91
92fail_msg() {
93	if [ "$MODE" = "allocate" ]; then
94		msg "$0: Allocate of $DEVICE failed."
95	else
96		msg "$0: Deallocate of $DEVICE failed."
97	fi
98	exit 1
99}
100
101#
102# 	Main program
103#
104
105# Check syntax, parse arguments.
106
107while getopts ifsI c
108do
109	case $c in
110	i)
111		FLAG=$c;;
112	f)
113		FLAG=$c;;
114	s)
115		FLAG=$c;;
116	I)
117		FLAG=i
118		silent=y;;
119	\?)  msg $USAGE
120      	     exit 1;;
121	esac
122done
123
124shift `expr $OPTIND - 1`
125
126DEVICE=$1
127if [ "$2" = "-A" ]; then
128	MODE="allocate"
129elif [ "$2" = "-D" ]; then
130	MODE="deallocate"
131fi
132if [ "$MODE" != "allocate" -a "$MODE" != "deallocate" ]; then
133	msg $USAGE
134	exit 1
135fi
136ZONENAME=$4
137ZONEPATH=$5
138SAVEDIR=/etc/security/audio
139MAP=`dminfo -v -n $DEVICE`
140DEVICE=`echo $MAP | cut -f1 -d:`
141TYPE=`echo $MAP | cut -f2 -d:`
142FILES=`echo $MAP | cut -f3 -d:`
143
144if [ ! -d ${SAVEDIR} ]
145then
146    /usr/bin/mkdir -m 0755 -p ${SAVEDIR} || fail_msg
147    /usr/bin/chown root:sys ${SAVEDIR} || fail_msg
148fi
149
150for d in $FILES
151do
152    x="`expr $d : '/dev/mixer[0-9][0-9]*'`"
153    if [ "$x" -ne 0 ] ; then
154	DEVNM=$d
155	break
156    fi
157done
158SAVEFILE="${SAVEDIR}/`basename ${DEVNM}`"
159
160if [ "${FLAG}" = "i" -a ! -r "${SAVEFILE}" ]
161then
162    /usr/bin/audioctl save-controls -d ${DEVNM} -f ${SAVEFILE} || fail_msg
163else
164    /usr/bin/audioctl load-controls -d ${DEVNM} ${SAVEFILE} || fail_msg
165fi
166
167exit 0
168