home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / sbin / alsa < prev    next >
Encoding:
Text File  |  2010-10-23  |  5.4 KB  |  221 lines

  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.     [ -d /var/run/alsa ] && return 0
  33.     # We have no business creating /var/run if it doesn't exist
  34.     if ! [ -d /var/run ] ; then
  35.         warn "Could not create /var/run/alsa/ because /var/run/ is not present."
  36.         return 1
  37.     fi
  38.     if ! mkdir --mode=755 /var/run/alsa ; then
  39.         warn "Failed to create /var/run/alsa/."
  40.         return 1
  41.     fi
  42.     [ -d /var/run/alsa ] && return 0
  43.     return 1
  44. }
  45.  
  46. echo_procs_using_sound()
  47. {
  48.     echo $( \
  49.         lsof +D /dev -F rt \
  50.         | awk '/^p/ {pid=$1} /^t/ {type=$1} /^r0x(74|e)..$/ && type == "tCHR" {print pid}' \
  51.         | cut -c 2- \
  52.         | uniq \
  53.     )
  54. }
  55.  
  56. # $* [PID]...
  57. echo_with_command_names()
  58. {
  59.     [ "$1" ] || return 0
  60.     echo $( \
  61.         ps --no-headers -o "%p %c" "$@" \
  62.         | sed -e 's/\([0-9][0-9]*\) \(.*\)/\1(\2)/' \
  63.     )
  64. }
  65.  
  66. kill_procs_using_sound()
  67. {
  68.     procs_using_sound="$(echo_procs_using_sound)"
  69.     if [ "$procs_using_sound" ] ; then
  70.         echo -n "Terminating processes:"
  71.         for attempt in 1 2 3 4 ; do
  72.             echo -n " ${procs_using_sound}"
  73.             kill $procs_using_sound || :
  74.             sleep 1
  75.             procs_using_sound="$(echo_procs_using_sound)"
  76.             [ "$procs_using_sound" ] || break
  77.         done
  78.         # Either no more procs using sound or attempts ran out
  79.         if [ "$procs_using_sound" ] ; then
  80.             echo -n " (with SIGKILL:) ${procs_using_sound}"
  81.             kill -9 $procs_using_sound || :
  82.             sleep 1
  83.         fi
  84.         procs_using_sound="$(echo_procs_using_sound)"
  85.         if [ "$procs_using_sound" ] ; then
  86.             echo " (failed: processes still using sound devices: $(echo_with_command_names $procs_using_sound))."
  87.             return 1
  88.         fi
  89.         echo "."
  90.     fi
  91.     return 0
  92. }
  93.  
  94. # $* MODULE-NAME [MODULE-NAME]... | "all"
  95. unload_modules()
  96. {
  97.     procs_using_sound="$(echo_procs_using_sound)"
  98.     if [ "$procs_using_sound" ] ; then
  99.         warn "Processes using sound devices: $(echo_with_command_names $procs_using_sound)."
  100.     fi
  101.     if check_run_dir ; then
  102.         :> /var/run/alsa/modules-removed
  103.     else
  104.         warn "Not keeping list of removed modules because /var/run/alsa is absent.
  105. It will not be possible automatically to reload these modules."
  106.     fi
  107.     echo -n "Unloading ALSA sound driver modules:"
  108.     [ -d /proc/asound ] || { echo " (none loaded)." ; return 0 ; }
  109.     echo_snd_modules_loaded()
  110.     {
  111.         lsmod \
  112.         | sed -n -e 's/^\(snd[-_][^[:space:]]*\)[[:space:]].*/\1/p' \
  113.         | sed -e 's/_/-/g'
  114.     }
  115.     for FSMBS in $* ; do
  116.         MODULES_TO_REMOVE=""
  117.         SND_MODULES_LOADED="$(echo_snd_modules_loaded)"
  118.         case "$FSMBS" in
  119.           all)
  120.             MODULES_TO_REMOVE="$SND_MODULES_LOADED"
  121.             ;;
  122.           snd_*|snd-*)
  123.             FSMBS="$(echo "$FSMBS" | sed -e 's/_/-/g')"
  124.             for M in $SND_MODULES_LOADED ; do
  125.                 if [ "$FSMBS" = "$M" ] ; then
  126.                     MODULES_TO_REMOVE="$FSMBS"
  127.                     break
  128.                 fi
  129.             done
  130.             ;;
  131.         esac
  132.         [ "$MODULES_TO_REMOVE" ] || continue
  133.         if [ -d /var/run/alsa ] ; then
  134.             echo "$MODULES_TO_REMOVE" >> /var/run/alsa/modules-removed
  135.         fi
  136.         for M in $MODULES_TO_REMOVE ; do
  137.             echo -n " ${M}"
  138.             modprobe -r "$M" >/dev/null 2>&1 || :
  139.         done
  140.     done
  141.     if [ -f /var/run/alsa/modules-removed ] ; then
  142.         MODULES_STILL_LOADED="$(echo_snd_modules_loaded | grep -F -f /var/run/alsa/modules-removed)"
  143.         MODULES_STILL_LOADED="$(echo $MODULES_STILL_LOADED)"
  144.     else
  145.         MODULES_STILL_LOADED=""
  146.     fi
  147.     if [ "$MODULES_STILL_LOADED" ] ; then
  148.         echo " (failed: modules still loaded: ${MODULES_STILL_LOADED})."
  149.         return 1
  150.     else
  151.         echo "."
  152.         return 0
  153.     fi
  154. }
  155.  
  156. # $* MODULE-NAME [MODULE-NAME]... | "all"
  157. force_unload_modules()
  158. {
  159.     kill_procs_using_sound || :
  160.     unload_modules "$@" || return 1
  161.     return 0
  162. }
  163.  
  164. load_unloaded_modules()
  165. {
  166.     LUM_RETURNSTATUS=0
  167.     MODULES_TO_LOAD=""
  168.     [ -d /var/run/alsa ] || warn "Directory /var/run/alsa is absent."
  169.     echo -n "Loading ALSA sound driver modules:"
  170.     [ -f /var/run/alsa/modules-removed ] && MODULES_TO_LOAD="$(echo $(cat /var/run/alsa/modules-removed))"
  171.     [ "$MODULES_TO_LOAD" ] || { echo " (none to reload)." ; return $LUM_RETURNSTATUS ; }
  172.     echo -n " $MODULES_TO_LOAD"
  173.     for MDL in $MODULES_TO_LOAD ; do
  174.         modprobe $MDL || LUM_RETURNSTATUS=1
  175.     done
  176.     case "$LUM_RETURNSTATUS" in
  177.       0) echo "." ;;
  178.       *) echo " (failed)." ;;
  179.     esac
  180.     return $LUM_RETURNSTATUS
  181. }
  182.  
  183. case "$1" in
  184.   unload)
  185.     unload_modules all || exit $?
  186.     ;;
  187.   reload)
  188.     EXITSTATUS=0
  189.     unload_modules all || EXITSTATUS=1
  190.     load_unloaded_modules || EXITSTATUS=1
  191.     exit $EXITSTATUS
  192.     ;;
  193.   force-unload)
  194.     force_unload_modules all || exit $?
  195.     ;;
  196.   force-reload)
  197.     EXITSTATUS=0
  198.     force_unload_modules all || EXITSTATUS=1
  199.     load_unloaded_modules || EXITSTATUS=1
  200.     exit $EXITSTATUS
  201.     ;;
  202.   suspend)
  203.     case "$force_unload_modules_before_suspend" in
  204.       ""|false) : ;;
  205.       all|true) force_unload_modules all || exit $? ;;
  206.       *) force_unload_modules $force_unload_modules_before_suspend || exit $? ;;
  207.     esac
  208.     ;;
  209.   resume)
  210.     case "$force_unload_modules_before_suspend" in
  211.       ""|false) : ;;
  212.       *) load_unloaded_modules || exit $? ;;
  213.     esac
  214.     ;;
  215.   *)
  216.     echo "Usage: $MYNAME {unload|reload|force-unload|force-reload|suspend|resume}" >&2
  217.     exit 3
  218.     ;;
  219. esac
  220.