Newer version of DAHDI hotplug scripts
authorOron Peled <oron.peled@xorcom.com>
Mon, 23 Sep 2013 11:59:14 +0000 (13:59 +0200)
committerTzafrir Cohen <tzafrir.cohen@xorcom.com>
Tue, 1 Oct 2013 18:51:46 +0000 (21:51 +0300)
A newer version of the scripts fully adapted to pinned spans:
* handle_device does not run dahdi_cfg.
* A separate UDEV rule script for that: span_config. Should also work
  for the non-pinned case.
* span_assignments, span_types: add actions 'auto' (manually enable all)
  and 'dumpconfig' (dump current status in the format of configuration
  file).
* Fixed name of span_types and span_assignments (no '-').
* spantype.conf renamed span-types.conf: configuration files do have a
  dash.
* Those two are useful programs, insstalled to /usr/sbin.

Signed-off-by: Tzafrir Cohen <tzafrir.cohen@xorcom.com>

Makefile
dahdi.rules
handle_device
span_assignments
span_config [new file with mode: 0755]
span_types

index 54bdefa..9bfa294 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -104,9 +104,8 @@ ifeq        (1,$(PBX_HDLC))
 endif
 MAN_PAGES:=$(wildcard $(BINS:%=doc/%.8))
 
-PINNED_DATA_SCRIPTS:=dahdi_cfg_device_args handle_device \
-       span_assignments span_types
-PINNED_UTILS:=dahdi_map
+PINNED_DATA_SCRIPTS:=handle_device span_config
+PINNED_UTILS:=dahdi_map span_assignments span_types
 PINNED_CONF:=pinned-spans.conf spantype.conf
 
 TEST_BINS:=patgen pattest patlooptest hdlcstress hdlctest hdlcgen hdlcverify timertest dahdi_maint
index ef08fd3..8674cd1 100644 (file)
@@ -1,3 +1,4 @@
 # DAHDI devices with ownership/permissions for running as non-root
 SUBSYSTEM=="dahdi",            OWNER="asterisk", GROUP="asterisk", MODE="0660"
 SUBSYSTEM=="dahdi_devices",    RUN="/usr/share/dahdi/handle_device"
+SUBSYSTEM=="dahdi_spans",      RUN="/usr/share/dahdi/span_config"
index 59d0f62..498ebf0 100755 (executable)
@@ -2,7 +2,8 @@
 #
 # /usr/share/dahdi/handle_device
 #
-# Called by UDEV when a span goes online/offline to assign spans
+# Called by UDEV when a dahdi device is added/removed
+#
 
 me=`basename $0`
 dir=`dirname $0`
@@ -22,26 +23,38 @@ PATH="$dir:/usr/sbin:/sbin:/usr/bin:/bin"
 
 set -e
 
-run_dahdi_cfg() {
-       args="-c $DAHDICONFDIR/system.conf $@"
-       echo "Running dahdi_cfg $args"
-       dahdi_cfg $args
-}
+#echo >&2 "$0($ACTION): DEBUG($# args): '$*'"
+
+# 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
 
-echo >&2 "$0($ACTION): DEBUG($# args): '$*'"
+# Can we pass a different value so we can use
+# alternate (testing) configuration?
+# Meanwhile, make it hard-coded.
+DAHDICONFDIR='/etc/dahdi'
+export DAHDICONFDIR
 
 case "$ACTION" in
 add)
        echo "$ACTION: $DEVPATH" | $LOGGER
-       # FIXME: need a way to add custom environment here:
-       #export DAHDICONFDIR="/tmp/xortel/dahdi"
-       span_types set "/sys/$DEVPATH" 2>&1 | $LOGGER
-       span_assignments add "/sys/$DEVPATH" 2>&1 | $LOGGER
-       dahdi_cfg_device_args | while read args; do
-                       run_dahdi_cfg $args 2>&1 | $LOGGER
-               done
+       # Can have alternate dahdi configuration directory for debugging
+       # export DAHDICONFDIR="/tmp/xortel/dahdi"
+
+       # Don't block udev for too long
+       (
+               span_types set "/sys$DEVPATH"
+               span_assignments add "/sys$DEVPATH"
+       ) 2>&1 < /dev/null | $LOGGER &
        ;;
 remove)
+       # Nothing to do yet...
        echo "$ACTION: $DEVPATH" | $LOGGER
        ;;
 *)
index 0f4e006..8f29d39 100755 (executable)
@@ -1,15 +1,23 @@
 #! /bin/sh
 #
-# /usr/share/dahdi/span_assignments:
+# /usr/sbin/span_assignments:
 #
 # this script can be used both from udev and
 # from the command line to assign/unassign and list
-# current assignments.
+# current span assignments.
+#
+# It uses a configuration file: $DAHDICONFDIR/pinned-spans.conf
+# (default DAHDICONFDIR=/etc/dahdi)
 #
 # The first argument is an action:
-#   "add" to assign (spans which are not already assigned)
-#   "remove" to unassign (spans which are not already unassigned)
-#   "list" to show all spans (with/without assignments)
+#   "auto"       - trigger driver auto_assign attribute for given devices
+#                 (no configuration file is used)
+#   "add"        - assign (spans which are not already assigned), according
+#                  to /etc/dahdi/pinned-spans.conf configuration file
+#   "remove"     - unassign spans which are not already unassigned
+#   "list"       - human-readable list of all spans (with/without assignments)
+#   "dumpconfig" - dump current assignments in a /etc/dahdi/pinned-spans.conf
+#                  compatible format
 #
 # Without further arguments, it operates on all existing spans
 # With one or more sysfs dahdi_devices it is limited to those.
@@ -26,7 +34,7 @@ DAHDICONFDIR="${DAHDICONFDIR:-/etc/dahdi}"
 pinned_spans_conf="$DAHDICONFDIR/pinned-spans.conf"
 
 usage() {
-       echo >&2 "Usage: $0 {add|remove|list} [devpath ...]"
+       echo >&2 "Usage: $0 {auto|add|remove|list|dumpconfig} [devpath ...]"
        exit 1
 }
 
@@ -66,22 +74,26 @@ show_devices() {
                                spanno='-'
                                basechan='-'
                        fi
-                       printf "%-4s %-12s %s\n" "$local_spanno:$spanno:$basechan" "[$hw_id]" "@$location"
+                       printf "%-8s %-14s %s\n" "$local_spanno:$spanno:$basechan" "[$hw_id]" "@$location"
                done | sort -n
        done
 }
 
 dump_config() {
-
+       echo '#'
+       echo "# Autogenerated by $0 on `date`"
+       echo "# Map devices + local spans to span + base channel number"
+       echo ''
        for device in $DEVICES
        do
                hw_id=`cat "$device/hardware_id"`
-               location=`cd "$device" && pwd -P | sed 's,/sys/devices/,,'`
+               location='@'`cd "$device" && pwd -P | sed 's,/sys/devices/,,'`
                if [ "$hw_id" != '' ]; then
                        id="$hw_id"
                else
-                       id="@$location"
+                       id="$location"
                fi
+               echo "# Device: [$hw_id] $location"
                for local_spanno in `cut -d: -f1 "$device/spantype"`
                do
                        span=`grep 2>/dev/null -Hw "$local_spanno" "$device/span-"*"/local_spanno" | \
@@ -90,11 +102,12 @@ dump_config() {
                                spanno=`echo $span | sed 's/^.*-//'`
                                name=`cat 2>/dev/null "$device/$span/name"`
                                basechan=`cat 2>/dev/null "$device/$span/basechan"`
+                               printf "%-30s %s\n" "$id" "$local_spanno:$spanno:$basechan"
                        else
-                               spanno='-'
+                               echo "#   Skipped unassigned local span $local_spanno"
                        fi
-                       printf "%-30s %s\n" "$id" "$local_spanno:$spanno:$basechan"
-               done | sort -n
+               done | sort
+               echo ''
        done
 }
 
@@ -168,18 +181,28 @@ match_device() {
 }
 
 assign_devices() {
+       if [ ! -f "$pinned_spans_conf" ]; then
+               echo >&2 "$0: Missing '$pinned_spans_conf'"
+               exit 1
+       fi
+       echo "using '$pinned_spans_conf'"
        for device in $DEVICES
        do
                match_device "$device"
        done
 }
 
+auto_assign_devices() {
+       for device in $DEVICES
+       do
+               echo "auto-assign $device"
+               echo 1 > "$device/auto_assign"
+       done
+}
+
 case "$action" in
-list)
-       show_devices
-       ;;
-dump)
-       dump_config
+auto)
+       auto_assign_devices
        ;;
 add)
        assign_devices
@@ -187,6 +210,12 @@ add)
 remove)
        unassign_all_spans
        ;;
+list)
+       show_devices
+       ;;
+dumpconfig)
+       dump_config
+       ;;
 *)
        usage
        ;;
diff --git a/span_config b/span_config
new file mode 100755 (executable)
index 0000000..8500d46
--- /dev/null
@@ -0,0 +1,70 @@
+#! /bin/sh
+#
+# /usr/share/dahdi/span_config
+#
+# Called by UDEV when a dahdi span is added/removed
+#
+
+me=`basename $0`
+dir=`dirname $0`
+LOGGER="logger -i -t '$me'"
+NAME=`basename "$DEVPATH" | tr -c 'A-Za-z0-9-' '_'`
+
+exec 2> /dev/null
+# Always redirect stderr somewhere, otherwise the shell script will die
+# when it tries to do I/O related stuff on closed file descriptor.
+# Our default is to throw it down the bit-bucket.
+#exec 2> /dev/console
+## If you wish to trace this script:
+#exec 2> "/tmp/${me}.$NAME" 1>&2
+
+# Our directory in the beginning, so we can use local lab setup
+PATH="$dir:/usr/sbin:/sbin:/usr/bin:/bin"
+
+set -e
+
+#echo >&2 "$0($ACTION): DEBUG($# args): '$*'"
+
+# Can we pass a different value so we can use
+# alternate (testing) configuration?
+# Meanwhile, make it hard-coded.
+DAHDICONFDIR='/etc/dahdi'
+export DAHDICONFDIR
+
+run_dahdi_cfg() {
+       span_devpath="$1"
+       # Sanity check
+       checkit=`"dahdi_cfg" --help 2>&1 | grep -- '-S' | wc -l`
+       if [ "$checkit" != 1 ]; then
+               $LOGGER "Bad dahdi_cfg (no -S support). Skipping"
+               exit 0
+       fi
+       spanno=`echo "$span_devpath" | sed 's,.*/span-,,'`
+       basechan=`cat "$span_devpath/basechan"`
+       channels=`cat "$span_devpath/channels"`
+       endchan=`expr "$basechan" + "$channels" - 1`
+       echo "dahdi_cfg: span $spanno <$basechan-$endchan>"
+       dahdi_cfg \
+               -c "$DAHDICONFDIR/system.conf" \
+               -S "$spanno" \
+               -C "$basechan-$endchan"
+       asterisk -rx "dahdi create channels $basechan $endchan"
+}
+
+case "$ACTION" in
+add)
+       echo "$ACTION: $DEVPATH" | $LOGGER
+       # Can have alternate dahdi configuration directory for debugging
+       # export DAHDICONFDIR="/tmp/xortel/dahdi"
+
+       run_dahdi_cfg "/sys$DEVPATH" 2>&1 | $LOGGER
+       ;;
+remove)
+       # Nothing to do yet...
+       echo "$ACTION: $DEVPATH" | $LOGGER
+       ;;
+*)
+       echo "UNHANDLED: $ACTION: $DEVPATH" | $LOGGER
+       ;;
+esac
+
index cf7c70d..74d18f1 100755 (executable)
@@ -1,34 +1,36 @@
 #! /bin/sh
 #
-# /usr/share/dahdi/span_types:
+# /usr/sbin/span_types
 #
-# this script can be used both from udev and
-# from the command line for spantype management.
+# This script can be used both from udev and
+# from the command line to manage PRI spans
+# type (E1/T1/J1).
 #
-# It use a configuration file /etc/dahdi/spantype.conf
-# (the format is documented inside this file)
+# Span types can be set only *BEFORE* span are assigned.
 #
-# The first argument is an action:
-#   "list" to show existing E1/T1/J1 types
-#   "dump" the same, but in a format (almost) suitable for
-#          the configuration file
-#          FIXME: we currently don't have the base channo in sysfs.
-#   "set"  actually write the setting to the driver
+# It reads a configuration file /etc/dahdi/span-types.conf
+# (the format is documented inside that file)
+#
+# A mandatory first argument is:
+#   list       - to show existing E1/T1/J1 types
+#   dumpconfig - the same, but in a format (almost) suitable
+#                for the configuration file
+#   set        - actually write the setting to the driver
 #
 # Examples:
 #    span_types list
-#    span_types dump
-#    span_types set    # all
+#    span_types dumpconfig
+#    span_types set         # all devices
 #    span_types set /sys/bus/dahdi_devices/devices/astribanks:xbus-00
 #
 
 
 devbase='/sys/bus/dahdi_devices/devices'
 DAHDICONFDIR="${DAHDICONFDIR:-/etc/dahdi}"
-spantype_conf="$DAHDICONFDIR/spantype.conf"
+spantypes_conf="$DAHDICONFDIR/span-types.conf"
 
 usage() {
-       echo >&2 "Usage: $0 {list|dump|set} [devpath ...]"
+       echo >&2 "Usage: $0 {list|dumpconfig|set} [devpath ...]"
        exit 1
 }
 
@@ -38,6 +40,11 @@ fi
 action="$1"
 shift
 
+if [ ! -d "$devbase" ]; then
+       echo >&2 "$0: Missing '$devbase' (DAHDI driver unloaded?)"
+       exit 1
+fi
+
 # Use given devices or otherwise, all existing devices
 if [ "$#" -gt 0 ]; then
        DEVICES="$@"
@@ -46,19 +53,28 @@ else
 fi
 
 show_spantypes() {
+       echo "# PRI span types (E1/T1/J1)"
        for device in $DEVICES
        do
                hw_id=`cat "$device/hardware_id"`
                location='@'`cd "$device" && pwd -P | sed 's,/sys/devices/,,'`
                cat "$device/spantype" | while read st; do
-                       printf "%-10s %-20s %s\n" "$st" "[$hw_id]" "$location"
+                       case "$st" in
+                       *:[ETJ]1)
+                               printf "%-10s %-20s %s\n" \
+                                       "$st" "[$hw_id]" "$location"
+                               ;;
+                       esac
                done | sort -n
        done
 }
 
 dump_config() {
+       echo '#'
+       echo "# Autogenerated by $0 on `date`"
+       echo "# Map PRI DAHDI devices to span types for E1/T1/J1"
+       echo ''
        fmt="%-65s %s\n"
-       echo "# Map of dahdi_devices to span types for E1/T1/J1"
        printf "$fmt" '# @location/hardware_id' 'span_type'
        for device in $DEVICES
        do
@@ -69,19 +85,24 @@ dump_config() {
                else
                        id="$location"
                fi
+               #echo "# Device: [$hw_id] $location"
                cat "$device/spantype" | while read st; do
                        case "$st" in
                        *:[ETJ]1)
                                printf "$fmt" "$id" "$st"
                                ;;
+                       *)
+                               #echo "#    Skipped local span `echo $st | sed 's/:/ -- /'`"
+                               ;;
                        esac
                done | sort -n
+               #echo ''
        done
 }
 
 # Allow comments and empty lines in config file
 filter_conf() {
-       sed -e 's/#.*//' -e '/^[ \t]*$/d' "$spantype_conf"
+       sed -e 's/#.*//' -e '/^[ \t]*$/d' "$spantypes_conf"
 }
 
 conf_spans() {
@@ -121,7 +142,7 @@ device_set_spantype() {
        hw_id=`attr_clean "$device/hardware_id"`
        location='@'`cd "$device" && pwd -P | sed 's,/sys/devices/,,'`
        spanspecs=`conf_spans "$hw_id" "$location"`
-       echo >&2 "MATCHED($device): $spanspecs"
+       #echo >&2 "MATCHED($device): $spanspecs"
        cut -d: -f1 "$attr_file" | while read spanno; do
                        for sp in $spanspecs
                        do
@@ -138,6 +159,10 @@ device_set_spantype() {
 }
 
 set_spantypes() {
+       if [ ! -f "$spantypes_conf" ]; then
+               echo >&2 "$0: Missing configuration '$spantypes_conf'"
+               exit 1
+       fi
        for device in $DEVICES
        do
                device_set_spantype "$device"
@@ -148,7 +173,7 @@ case "$action" in
 list)
        show_spantypes
        ;;
-dump)
+dumpconfig)
        dump_config
        ;;
 set)