hotplug modularization: split logic to scriptlets
authorOron Peled <oron.peled@xorcom.com>
Thu, 16 Jan 2014 10:31:17 +0000 (12:31 +0200)
committerTzafrir Cohen <tzafrir.cohen@xorcom.com>
Thu, 23 Jan 2014 10:35:59 +0000 (12:35 +0200)
* 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 <tzafrir.cohen@xorcom.com>
Acked-by: Russ Meyerriecks <rmeyerriecks@digium.com>

Makefile
hotplug/dahdi_handle_device
hotplug/dahdi_span_config
hotplug/handle_device.d/10-span-types [new file with mode: 0755]
hotplug/handle_device.d/20-span-assignments [new file with mode: 0755]
hotplug/span_config.d/10-dahdi-cfg [new file with mode: 0755]
hotplug/span_config.d/20-fxotune [new file with mode: 0755]
hotplug/span_config.d/50-asterisk [new file with mode: 0755]

index f3ce6df..45fc7b7 100644 (file)
--- 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
 
index 34af0c3..c67fce9 100755 (executable)
@@ -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...
index bde20fb..0f610fb 100755 (executable)
@@ -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 (executable)
index 0000000..d840c3d
--- /dev/null
@@ -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 (executable)
index 0000000..f078aca
--- /dev/null
@@ -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 (executable)
index 0000000..9ca2efe
--- /dev/null
@@ -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 (executable)
index 0000000..199c82c
--- /dev/null
@@ -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 (executable)
index 0000000..2e7adbd
--- /dev/null
@@ -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"