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