Advertisement
Guest User

Untitled

a guest
Jan 31st, 2011
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.63 KB | None | 0 0
  1. #!/bin/sh
  2. #
  3. # alsa-base control script
  4. #
  5. # Description: Used to load and unload ALSA modules and
  6. # restore and store mixer levels. There is no
  7. # longer any need to run this script on bootup
  8. # or shutdown. It is now moved to /usr/sbin.
  9.  
  10. set -e
  11.  
  12. # Exit if alsa-base package is not installed
  13. [ -f /etc/modprobe.d/alsa-base.conf ] || exit 0
  14.  
  15. MYNAME=/usr/sbin/alsa
  16. PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
  17.  
  18. # Default values of variables in /etc/default/alsa
  19. force_unload_modules_before_suspend=""
  20.  
  21. [ -f /etc/default/alsa ] && . /etc/default/alsa
  22.  
  23. # $* MESSAGE
  24. warn() { echo "${MYNAME}: Warning: $* " >&2 ; }
  25.  
  26. #
  27. # Attempt to create /var/run/alsa if it is absent.
  28. # Return true if /var/run/alsa exists after this attempt,
  29. # otherwise false.
  30. #
  31. check_run_dir()
  32. {
  33. [ -d /var/run/alsa ] && return 0
  34. # We have no business creating /var/run if it doesn't exist
  35. if ! [ -d /var/run ] ; then
  36. warn "Could not create /var/run/alsa/ because /var/run/ is not present."
  37. return 1
  38. fi
  39. if ! mkdir --mode=755 /var/run/alsa ; then
  40. warn "Failed to create /var/run/alsa/."
  41. return 1
  42. fi
  43. [ -d /var/run/alsa ] && return 0
  44. return 1
  45. }
  46.  
  47. echo_procs_using_sound()
  48. {
  49. echo $( \
  50. lsof +D /dev -F rt \
  51. | awk '/^p/ {pid=$1} /^t/ {type=$1} /^r0x(74|e)..$/ && type == "tCHR" {print pid}' \
  52. | cut -c 2- \
  53. | uniq \
  54. )
  55. }
  56.  
  57. # $* [PID]...
  58. echo_with_command_names()
  59. {
  60. [ "$1" ] || return 0
  61. echo $( \
  62. ps --no-headers -o "%p %c" "$@" \
  63. | sed -e 's/\([0-9][0-9]*\) \(.*\)/\1(\2)/' \
  64. )
  65. }
  66.  
  67. kill_procs_using_sound()
  68. {
  69. procs_using_sound="$(echo_procs_using_sound)"
  70. if [ "$procs_using_sound" ] ; then
  71. echo -n "Terminating processes:"
  72. for attempt in 1 2 3 4 ; do
  73. echo -n " ${procs_using_sound}"
  74. kill $procs_using_sound || :
  75. sleep 1
  76. procs_using_sound="$(echo_procs_using_sound)"
  77. [ "$procs_using_sound" ] || break
  78. done
  79. # Either no more procs using sound or attempts ran out
  80. if [ "$procs_using_sound" ] ; then
  81. echo -n " (with SIGKILL:) ${procs_using_sound}"
  82. kill -9 $procs_using_sound || :
  83. sleep 1
  84. fi
  85. procs_using_sound="$(echo_procs_using_sound)"
  86. if [ "$procs_using_sound" ] ; then
  87. echo " (failed: processes still using sound devices: $(echo_with_command_names $procs_using_sound))."
  88. return 1
  89. fi
  90. echo "."
  91. fi
  92. return 0
  93. }
  94.  
  95. # $* MODULE-NAME [MODULE-NAME]... | "all"
  96. unload_modules()
  97. {
  98. procs_using_sound="$(echo_procs_using_sound)"
  99. if [ "$procs_using_sound" ] ; then
  100. warn "Processes using sound devices: $(echo_with_command_names $procs_using_sound)."
  101. fi
  102. if check_run_dir ; then
  103. :> /var/run/alsa/modules-removed
  104. else
  105. warn "Not keeping list of removed modules because /var/run/alsa is absent.
  106. It will not be possible automatically to reload these modules."
  107. fi
  108. echo -n "Unloading ALSA sound driver modules:"
  109. [ -d /proc/asound ] || { echo " (none loaded)." ; return 0 ; }
  110. echo_snd_modules_loaded()
  111. {
  112. lsmod \
  113. | sed -n -e 's/^\(snd[-_][^[:space:]]*\)[[:space:]].*/\1/p' \
  114. | sed -e 's/_/-/g'
  115. }
  116. for FSMBS in $* ; do
  117. MODULES_TO_REMOVE=""
  118. SND_MODULES_LOADED="$(echo_snd_modules_loaded)"
  119. case "$FSMBS" in
  120. all)
  121. MODULES_TO_REMOVE="$SND_MODULES_LOADED"
  122. ;;
  123. snd_*|snd-*)
  124. FSMBS="$(echo "$FSMBS" | sed -e 's/_/-/g')"
  125. for M in $SND_MODULES_LOADED ; do
  126. if [ "$FSMBS" = "$M" ] ; then
  127. MODULES_TO_REMOVE="$FSMBS"
  128. break
  129. fi
  130. done
  131. ;;
  132. esac
  133. [ "$MODULES_TO_REMOVE" ] || continue
  134. if [ -d /var/run/alsa ] ; then
  135. echo "$MODULES_TO_REMOVE" >> /var/run/alsa/modules-removed
  136. fi
  137. for M in $MODULES_TO_REMOVE ; do
  138. echo -n " ${M}"
  139. modprobe -r "$M" >/dev/null 2>&1 || :
  140. done
  141. done
  142. if [ -f /var/run/alsa/modules-removed ] ; then
  143. MODULES_STILL_LOADED="$(echo_snd_modules_loaded | grep -F -f /var/run/alsa/modules-removed)"
  144. MODULES_STILL_LOADED="$(echo $MODULES_STILL_LOADED)"
  145. else
  146. MODULES_STILL_LOADED=""
  147. fi
  148. if [ "$MODULES_STILL_LOADED" ] ; then
  149. echo " (failed: modules still loaded: ${MODULES_STILL_LOADED})."
  150. return 1
  151. else
  152. echo "."
  153. return 0
  154. fi
  155. }
  156.  
  157. # $* MODULE-NAME [MODULE-NAME]... | "all"
  158. force_unload_modules()
  159. {
  160. kill_procs_using_sound || :
  161. unload_modules "$@" || return 1
  162. return 0
  163. }
  164.  
  165. load_unloaded_modules()
  166. {
  167. LUM_RETURNSTATUS=0
  168. MODULES_TO_LOAD=""
  169. [ -d /var/run/alsa ] || warn "Directory /var/run/alsa is absent."
  170. echo -n "Loading ALSA sound driver modules:"
  171. [ -f /var/run/alsa/modules-removed ] && MODULES_TO_LOAD="$(echo $(cat /var/run/alsa/modules-removed))"
  172. [ "$MODULES_TO_LOAD" ] || { echo " (none to reload)." ; return $LUM_RETURNSTATUS ; }
  173. echo -n " $MODULES_TO_LOAD"
  174. for MDL in $MODULES_TO_LOAD ; do
  175. modprobe $MDL || LUM_RETURNSTATUS=1
  176. done
  177. case "$LUM_RETURNSTATUS" in
  178. 0) echo "." ;;
  179. *) echo " (failed)." ;;
  180. esac
  181. return $LUM_RETURNSTATUS
  182. }
  183.  
  184. case "$1" in
  185. unload)
  186. unload_modules all || exit $?
  187. ;;
  188. reload)
  189. EXITSTATUS=0
  190. unload_modules all || EXITSTATUS=1
  191. load_unloaded_modules || EXITSTATUS=1
  192. exit $EXITSTATUS
  193. ;;
  194. force-unload)
  195. force_unload_modules all || exit $?
  196. ;;
  197. force-reload)
  198. EXITSTATUS=0
  199. force_unload_modules all || EXITSTATUS=1
  200. load_unloaded_modules || EXITSTATUS=1
  201. exit $EXITSTATUS
  202. ;;
  203. suspend)
  204. case "$force_unload_modules_before_suspend" in
  205. ""|false) : ;;
  206. all|true) force_unload_modules all || exit $? ;;
  207. *) force_unload_modules $force_unload_modules_before_suspend || exit $? ;;
  208. esac
  209. ;;
  210. resume)
  211. case "$force_unload_modules_before_suspend" in
  212. ""|false) : ;;
  213. *) load_unloaded_modules || exit $? ;;
  214. esac
  215. ;;
  216. *)
  217. echo "Usage: $MYNAME {unload|reload|force-unload|force-reload|suspend|resume}" >&2
  218. exit 3
  219. ;;
  220. esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement