From 7f826a7d3540ab99c6440bf094c39cbf26042b87 Mon Sep 17 00:00:00 2001 From: Oron Peled Date: Thu, 16 Jan 2014 12:31:17 +0200 Subject: [PATCH] hotplug modularization: split logic to scriptlets * Device related operations are ordered in /usr/share/dahdi/handle_device.d/ * Span related operations are ordered in /usr/share/dahdi/span_config.d/ * In the future, span_config.d/50-asterisk should be moved to Asterisk. Signed-off-by: Tzafrir Cohen Acked-by: Russ Meyerriecks --- Makefile | 10 ++- hotplug/dahdi_handle_device | 58 +++++++-------- hotplug/dahdi_span_config | 80 +++++++-------------- hotplug/handle_device.d/10-span-types | 5 ++ hotplug/handle_device.d/20-span-assignments | 8 +++ hotplug/span_config.d/10-dahdi-cfg | 28 ++++++++ hotplug/span_config.d/20-fxotune | 12 ++++ hotplug/span_config.d/50-asterisk | 9 +++ 8 files changed, 126 insertions(+), 84 deletions(-) create mode 100755 hotplug/handle_device.d/10-span-types create mode 100755 hotplug/handle_device.d/20-span-assignments create mode 100755 hotplug/span_config.d/10-dahdi-cfg create mode 100755 hotplug/span_config.d/20-fxotune create mode 100755 hotplug/span_config.d/50-asterisk diff --git a/Makefile b/Makefile index f3ce6df..45fc7b7 100644 --- a/Makefile +++ b/Makefile @@ -103,7 +103,15 @@ endif ifeq (1,$(PBX_HDLC)) BINS += sethdlc endif -ASSIGNED_DATA_SCRIPTS:=dahdi_handle_device dahdi_span_config +ASSIGNED_DATA_SCRIPTS:=\ + dahdi_handle_device \ + dahdi_span_config \ + span_config.d/10-dahdi-cfg \ + span_config.d/20-fxotune \ + span_config.d/50-asterisk \ + handle_device.d/10-span-types \ + handle_device.d/20-span-assignments + ASSIGNED_UTILS:=dahdi_span_assignments dahdi_span_types ASSIGNED_CONF:=assigned-spans.conf.sample span-types.conf.sample diff --git a/hotplug/dahdi_handle_device b/hotplug/dahdi_handle_device index 34af0c3..c67fce9 100755 --- a/hotplug/dahdi_handle_device +++ b/hotplug/dahdi_handle_device @@ -20,6 +20,7 @@ exec 2> /dev/null # Our directory in the beginning, so we can use local lab setup PATH="$dir:/usr/sbin:/sbin:/usr/bin:/bin" +export PATH set -e @@ -35,43 +36,44 @@ if [ "$DAHDI_UDEV_DISABLE_DEVICES" = 'yes' ]; then exit 0 fi +# Check if we can safely do our job +if [ ! -f /sys/module/dahdi/parameters/auto_assign_spans ]; then + echo "Old driver (no auto_assign_spans parameter). Skip $DEVPATH" | $LOGGER + exit 0 +fi +if [ `cat /sys/module/dahdi/parameters/auto_assign_spans` -eq 1 ]; then + echo "auto_assign_spans=1. Skip $DEVPATH" | $LOGGER + exit 0 +fi + # Can we pass a different value so we can use # alternate (testing) configuration? # Meanwhile, make it hard-coded. DAHDICONFDIR='/etc/dahdi' export DAHDICONFDIR +run_parts() { + # Have our internal "run-parts" (adapted from Fedora), + # as implementations differ + for i in `LC_ALL=C; ls -d $dir/handle_device.d/*[!~,] 2>/dev/null` ; do + [ -d "$i" ] && continue + [ ! -x "$i" ] && continue + # Don't run *.{rpmsave,rpmorig,rpmnew,swp,cfsaved} files + case "$i" in + *.cfsaved|*.rpmsave|*.rpmorig|*.rpmnew|*.swp|*,v) + continue + ;; + esac + echo "D: Running '$i'" + "$i" + done +} + case "$ACTION" in add) echo "$ACTION: $DEVPATH" | $LOGGER - - # Check if we can safely do our job - if [ ! -f /sys/module/dahdi/parameters/auto_assign_spans ]; then - if [ -f /sys/module/dahdi ]; then - $LOGGER "Old driver (no auto_assign_spans parameter). Skip $DEVPATH" - exit 0 - fi - fi - if [ `cat /sys/module/dahdi/parameters/auto_assign_spans` -eq 1 ]; then - echo "auto_assign_spans=1. Skip $DEVPATH" | $LOGGER - exit 0 - fi - - # Can have alternate dahdi configuration directory for debugging - # export DAHDICONFDIR="/tmp/xortel/dahdi" - - # Don't block udev for too long - ( - if [ -r "$DAHDICONFDIR/span-types.conf" ]; then - dahdi_span_types set "/sys$DEVPATH" - fi - if [ -r "$DAHDICONFDIR/assigned-spans.conf" ]; then - dahdi_span_assignments add "/sys$DEVPATH" - else - # No configuration. No order guaranteed - dahdi_span_assignments auto "/sys$DEVPATH" - fi - ) 2>&1 < /dev/null | $LOGGER & + # Background run -- don't block udev + run_parts 2>&1 < /dev/null | $LOGGER & ;; remove) # Nothing to do yet... diff --git a/hotplug/dahdi_span_config b/hotplug/dahdi_span_config index bde20fb..0f610fb 100755 --- a/hotplug/dahdi_span_config +++ b/hotplug/dahdi_span_config @@ -20,6 +20,7 @@ exec 2> /dev/null # Our directory in the beginning, so we can use local lab setup PATH="$dir:/usr/sbin:/sbin:/usr/bin:/bin" +export PATH set -e @@ -41,66 +42,35 @@ fi DAHDICONFDIR='/etc/dahdi' export DAHDICONFDIR -run_dahdi_cfg() { - echo "dahdi_cfg: span $spanno <$basechan-$endchan> ($DEVPATH)" - dahdi_cfg -c "$cfg_file" -S "$spanno" -C "$basechan-$endchan" -} - -configure_span() { - span_devpath="$1" - # Sanity check - checkit=`"dahdi_cfg" --help 2>&1 | grep -- '-S' | wc -l` - if [ "$checkit" != 1 ]; then - echo "Bad dahdi_cfg (no -S support). Skipping" - exit 0 - fi - - # Set variables - spanno=`echo "$span_devpath" | sed 's,.*/span-,,'` - basechan=`cat "$span_devpath/basechan"` - channels=`cat "$span_devpath/channels"` - endchan=`expr "$basechan" + "$channels" - 1` - - # Configure DAHDI - cfg_file="$DAHDICONFDIR/system.conf" - if [ -r "$cfg_file" ]; then - run_dahdi_cfg - else - echo "Using auto-generated config for dahdi_cfg" - cfg_file='-' - DAHDI_CONF_FILE="$cfg_file" dahdi_genconf system | run_dahdi_cfg - fi - fxotune_cfg='/etc/fxotune.conf' - if [ -r "$fxotune_cfg" ]; then - echo "fxotune: span $spanno <$basechan-$endchan> ($DEVPATH)" - fxotune -s -b "$basechan" -e "$endchan" - fi - - # Add to asterisk - asterisk -rx "dahdi create channels $basechan $endchan" +run_parts() { + # Have our internal "run-parts" (adapted from Fedora), + # as implementations differ + for i in `LC_ALL=C; ls -d $dir/span_config.d/*[!~,] 2>/dev/null` ; do + [ -d "$i" ] && continue + [ ! -x "$i" ] && continue + # Don't run *.{rpmsave,rpmorig,rpmnew,swp,cfsaved} files + case "$i" in + *.cfsaved|*.rpmsave|*.rpmorig|*.rpmnew|*.swp|*,v) + continue + ;; + esac + #echo "D: Running '$i'" + "$i" + done } case "$ACTION" in add) echo "$ACTION: $DEVPATH" | $LOGGER - - # Old driver. These scripts probably won't work anyway. - if [ ! -f /sys/module/dahdi/parameters/auto_assign_spans ]; then - if [ -f /sys/module/dahdi ]; then - $LOGGER "Old driver (no auto_assign_spans parameter). Skip $DEVPATH" - exit 0 - fi - fi - - if [ $(cat /sys/module/dahdi/parameters/auto_assign_spans) -eq 1 ]; then - $LOGGER "auto_assign_spans=1. Skip $DEVPATH" - exit 0 - fi - - # Can have alternate dahdi configuration directory for debugging - # export DAHDICONFDIR="/tmp/xortel/dahdi" - - configure_span "/sys$DEVPATH" 2>&1 | $LOGGER + # Set variables + span_devpath="/sys$DEVPATH" + SPANNO=`echo "$span_devpath" | sed 's,.*/span-,,'` + BASECHAN=`cat "$span_devpath/basechan"` + CHANNELS=`cat "$span_devpath/channels"` + ENDCHAN=`expr "$BASECHAN" + "$CHANNELS" - 1` + export SPANNO BASECHAN CHANNELS ENDCHAN + # Background run -- don't block udev + run_parts 2>&1 < /dev/null | $LOGGER & ;; remove|online|offline) # Nothing to do yet... diff --git a/hotplug/handle_device.d/10-span-types b/hotplug/handle_device.d/10-span-types new file mode 100755 index 0000000..d840c3d --- /dev/null +++ b/hotplug/handle_device.d/10-span-types @@ -0,0 +1,5 @@ +#! /bin/sh + +if [ -r "$DAHDICONFDIR/span-types.conf" ]; then + dahdi_span_types set "/sys$DEVPATH" +fi diff --git a/hotplug/handle_device.d/20-span-assignments b/hotplug/handle_device.d/20-span-assignments new file mode 100755 index 0000000..f078aca --- /dev/null +++ b/hotplug/handle_device.d/20-span-assignments @@ -0,0 +1,8 @@ +#! /bin/sh + +if [ -r "$DAHDICONFDIR/assigned-spans.conf" ]; then + dahdi_span_assignments add "/sys$DEVPATH" +else + # No configuration. No order guaranteed + dahdi_span_assignments auto "/sys$DEVPATH" +fi diff --git a/hotplug/span_config.d/10-dahdi-cfg b/hotplug/span_config.d/10-dahdi-cfg new file mode 100755 index 0000000..9ca2efe --- /dev/null +++ b/hotplug/span_config.d/10-dahdi-cfg @@ -0,0 +1,28 @@ +#! /bin/sh + +if [ "$ACTION" != 'add' ]; then + # Nothing to do here + exit 0 +fi + +# Sanity check +checkit=`"dahdi_cfg" --help 2>&1 | grep -- '-S' | wc -l` +if [ "$checkit" != 1 ]; then + echo "Bad dahdi_cfg (no -S support). Skipping" + exit 0 +fi + +run_dahdi_cfg() { + echo "dahdi_cfg: span $SPANNO <$BASECHAN-$ENDCHAN> ($DEVPATH)" + dahdi_cfg -c "$cfg_file" -S "$SPANNO" -C "$BASECHAN-$ENDCHAN" +} + +# Configure DAHDI +cfg_file="$DAHDICONFDIR/system.conf" +if [ -r "$cfg_file" ]; then + run_dahdi_cfg +else + echo "Using auto-generated config for dahdi_cfg" + cfg_file='-' + DAHDI_CONF_FILE="$cfg_file" dahdi_genconf system | run_dahdi_cfg +fi diff --git a/hotplug/span_config.d/20-fxotune b/hotplug/span_config.d/20-fxotune new file mode 100755 index 0000000..199c82c --- /dev/null +++ b/hotplug/span_config.d/20-fxotune @@ -0,0 +1,12 @@ +#! /bin/sh + +if [ "$ACTION" != 'add' ]; then + # Nothing to do here + exit 0 +fi + +fxotune_cfg='/etc/fxotune.conf' +if [ -r "$fxotune_cfg" ]; then + echo "fxotune: span $SPANNO <$BASECHAN-$ENDCHAN> ($DEVPATH)" + fxotune -s -b "$BASECHAN" -e "$ENDCHAN" +fi diff --git a/hotplug/span_config.d/50-asterisk b/hotplug/span_config.d/50-asterisk new file mode 100755 index 0000000..2e7adbd --- /dev/null +++ b/hotplug/span_config.d/50-asterisk @@ -0,0 +1,9 @@ +#! /bin/sh + +if [ "$ACTION" != 'add' ]; then + # Nothing to do here + exit 0 +fi + +# Add to asterisk +asterisk -rx "dahdi create channels $BASECHAN $ENDCHAN"