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