hotplug: call handle_device.d/ actions for remove
[dahdi/tools.git] / hotplug / 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 export PATH
24
25 set -e
26
27 #echo >&2 "$0($ACTION): DEBUG($# args): '$*'"
28
29 # Do we have a configuration?
30 if [ -f /etc/dahdi/init.conf ]; then
31         . /etc/dahdi/init.conf
32 fi
33
34 if [ "$DAHDI_UDEV_DISABLE_DEVICES" = 'yes' ]; then
35         echo "DAHDI_UDEV_DISABLE_DEVICES=yes. Skip $DEVPATH" | $LOGGER
36         exit 0
37 fi
38
39 # Can we pass a different value so we can use
40 # alternate (testing) configuration?
41 # Meanwhile, make it hard-coded.
42 DAHDICONFDIR='/etc/dahdi'
43 export DAHDICONFDIR
44
45 run_parts() {
46         # Have our internal "run-parts" (adapted from Fedora),
47         # as implementations differ
48         for i in `LC_ALL=C; ls -d $dir/handle_device.d/*[!~,] 2>/dev/null` ; do
49                 [ -d "$i" ] && continue
50                 [ ! -x "$i" ] && continue
51                 # Don't run *.{rpmsave,rpmorig,rpmnew,swp,cfsaved} files
52                 case "$i" in
53                 *.cfsaved|*.rpmsave|*.rpmorig|*.rpmnew|*.swp|*,v)
54                         continue
55                         ;;
56                 esac
57                 echo "D: Running '$i'"
58                 "$i"
59         done
60 }
61
62 case "$ACTION" in
63 add)
64         echo "$ACTION: $DEVPATH" | $LOGGER
65
66         # Check if we can safely do our job
67         if [ ! -f /sys/module/dahdi/parameters/auto_assign_spans ]; then
68                 echo "Old driver (no auto_assign_spans parameter). Skip $DEVPATH" | $LOGGER
69                 exit 0
70         fi
71         if [ `cat /sys/module/dahdi/parameters/auto_assign_spans` -ne 0 ]; then
72                 echo "auto_assign_spans=1. Skip $DEVPATH" | $LOGGER
73                 exit 0
74         fi
75
76         # Background run -- don't block udev
77         run_parts 2>&1 < /dev/null | $LOGGER &
78         ;;
79 remove)
80         echo "$ACTION: $DEVPATH" | $LOGGER
81         # Background run -- don't block udev
82         run_parts 2>&1 < /dev/null | $LOGGER &
83         ;;
84 *)
85         echo "UNHANDLED: $ACTION: $DEVPATH" | $LOGGER
86         ;;
87 esac