dahdi_handle_device: Auto assign only the device being added.
[dahdi/tools.git] / dahdi_handle_device
1 #! /bin/sh
2 #
3 # /usr/share/dahdi/dahdi_handle_device
4 #
5 # Called by UDEV when a dahdi device is added/removed
6 #
7
8 me=`basename $0`
9 dir=`dirname $0`
10 LOGGER="logger -i -t '$me'"
11 NAME=`basename "$DEVPATH" | tr -c 'A-Za-z0-9-' '_'`
12
13 # Always redirect stderr somewhere, otherwise the shell script will die
14 # when it tries to do I/O related stuff on closed file descriptor.
15 # Our default is to throw it down the bit-bucket.
16 exec 2> /dev/null
17 # If you wish to trace this script:
18 #exec 2> "/tmp/${me}.$NAME" 1>&2
19 #exec 2> /dev/console
20
21 # Our directory in the beginning, so we can use local lab setup
22 PATH="$dir:/usr/sbin:/sbin:/usr/bin:/bin"
23
24 set -e
25
26 #echo >&2 "$0($ACTION): DEBUG($# args): '$*'"
27
28 # Do we have a configuration?
29 if [ -f /etc/dahdi/init.conf ]; then
30         . /etc/dahdi/init.conf
31 fi
32
33 if [ "$DAHDI_UDEV_DISABLE_DEVICES" = 'yes' ]; then
34         echo "DAHDI_UDEV_DISABLE_DEVICES=yes. Skip $DEVPATH" | $LOGGER
35         exit 0
36 fi
37
38 # Can we pass a different value so we can use
39 # alternate (testing) configuration?
40 # Meanwhile, make it hard-coded.
41 DAHDICONFDIR='/etc/dahdi'
42 export DAHDICONFDIR
43
44 case "$ACTION" in
45 add)
46         echo "$ACTION: $DEVPATH" | $LOGGER
47
48         # Check if we can safely do our job
49         if [ ! -f /sys/module/dahdi/parameters/auto_assign_spans ]; then
50                 if [ -f /sys/module/dahdi ]; then
51                         $LOGGER "Old driver (no auto_assign_spans parameter). Skip $DEVPATH"
52                         exit 0
53                 fi
54         fi
55         if [ `cat /sys/module/dahdi/parameters/auto_assign_spans` -eq 1 ]; then
56                 echo "auto_assign_spans=1. Skip $DEVPATH" | $LOGGER
57                 exit 0
58         fi
59
60         # Can have alternate dahdi configuration directory for debugging
61         # export DAHDICONFDIR="/tmp/xortel/dahdi"
62
63         # Don't block udev for too long
64         (
65                 if [ -r "$DAHDICONFDIR/span-types.conf" ]; then
66                         dahdi_span_types set "/sys$DEVPATH"
67                 fi
68                 if [ -r "$DAHDICONFDIR/assigned-spans.conf" ]; then
69                         dahdi_span_assignments add "/sys$DEVPATH"
70                 else
71                         # No configuration. No order guaranteed
72                         dahdi_span_assignments auto "/sys$DEVPATH"
73                 fi
74         ) 2>&1 < /dev/null | $LOGGER &
75         ;;
76 remove)
77         # Nothing to do yet...
78         echo "$ACTION: $DEVPATH" | $LOGGER
79         ;;
80 *)
81         echo "UNHANDLED: $ACTION: $DEVPATH" | $LOGGER
82         ;;
83 esac