Fabio Berton | a066f13 | 2015-10-15 15:11:46 -0300 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # |
| 3 | # dahdi This shell script takes care of loading and unloading \ |
| 4 | # DAHDI Telephony interfaces |
| 5 | # chkconfig: 2345 9 92 |
| 6 | # description: The DAHDI drivers allow you to use your linux \ |
| 7 | # computer to accept incoming data and voice interfaces |
| 8 | # |
| 9 | # config: /etc/dahdi/init.conf |
| 10 | |
| 11 | ### BEGIN INIT INFO |
| 12 | # Provides: dahdi |
| 13 | # Required-Start: $local_fs $remote_fs |
| 14 | # Required-Stop: $local_fs $remote_fs |
| 15 | # Should-Start: $network $syslog |
| 16 | # Should-Stop: $network $syslog |
| 17 | # Default-Start: 2 3 4 5 |
| 18 | # Default-Stop: 0 1 6 |
| 19 | # Short-Description: DAHDI kernel modules |
| 20 | # Description: dahdi - load and configure DAHDI modules |
| 21 | ### END INIT INFO |
| 22 | |
| 23 | initdir=/etc/init.d |
| 24 | |
| 25 | # Don't edit the following values. Edit /etc/dahdi/init.conf instead. |
| 26 | |
| 27 | DAHDI_CFG=/usr/sbin/dahdi_cfg |
| 28 | DAHDI_CFG_CMD=${DAHDI_CFG_CMD:-"$DAHDI_CFG"} # e.g: for a custom system.conf location |
| 29 | |
| 30 | FXOTUNE=/usr/sbin/fxotune |
| 31 | |
| 32 | # The default syncer Astribank. Usually set automatically to a sane |
| 33 | # value by xpp_sync(1) if you have an Astribank. You can set this to an |
| 34 | # explicit Astribank (e.g: 01). |
| 35 | XPP_SYNC=auto |
| 36 | |
| 37 | # The maximal timeout (seconds) to wait for udevd to finish generating |
| 38 | # device nodes after the modules have loaded and before running dahdi_cfg. |
| 39 | DAHDI_DEV_TIMEOUT=20 |
| 40 | |
| 41 | # A list of modules to unload when stopping. |
| 42 | # All of their dependencies will be unloaded as well. |
| 43 | DAHDI_UNLOAD_MODULES="dahdi" |
| 44 | |
| 45 | # |
| 46 | # Determine which kind of configuration we're using |
| 47 | # |
| 48 | system=debian # assume debian |
| 49 | if [ -f /etc/debian_version ]; then |
| 50 | system=debian |
| 51 | fi |
| 52 | |
| 53 | if [ -f /etc/gentoo-release ]; then |
| 54 | system=debian |
| 55 | fi |
| 56 | |
| 57 | if [ -f /etc/SuSE-release -o -f /etc/novell-release ] |
| 58 | then |
| 59 | system=debian |
| 60 | fi |
| 61 | |
| 62 | # Source function library. |
| 63 | if [ $system = debian ]; then |
| 64 | . $initdir/functions || exit 0 |
| 65 | fi |
| 66 | |
| 67 | DAHDI_MODULES_FILE="/etc/dahdi/modules" |
| 68 | |
| 69 | [ -r /etc/dahdi/init.conf ] && . /etc/dahdi/init.conf |
| 70 | |
| 71 | if [ $system = redhat ]; then |
| 72 | LOCKFILE=/var/lock/subsys/dahdi |
| 73 | fi |
| 74 | |
| 75 | # recursively unload a module and its dependencies, if possible. |
| 76 | # where's modprobe -r when you need it? |
| 77 | # inputs: module to unload. |
| 78 | # returns: the result from |
| 79 | unload_module() { |
| 80 | module="$1" |
| 81 | line=`lsmod 2>/dev/null | grep "^$1 "` |
| 82 | if [ "$line" = '' ]; then return; fi # module was not loaded |
| 83 | |
| 84 | set -- $line |
| 85 | # $1: the original module, $2: size, $3: refcount, $4: deps list |
| 86 | mods=`echo $4 | tr , ' '` |
| 87 | ec_modules="" |
| 88 | # xpp_usb keeps the xpds below busy if an xpp hardware is |
| 89 | # connected. Hence must be removed before them: |
| 90 | case "$module" in xpd_*) mods="xpp_usb $mods";; esac |
| 91 | |
| 92 | for mod in $mods; do |
| 93 | case "$mod" in |
| 94 | dahdi_echocan_*) |
| 95 | ec_modules="$mod $ec_modules" |
| 96 | ;; |
| 97 | *) |
| 98 | # run in a subshell, so it won't step over our vars: |
| 99 | (unload_module $mod) |
| 100 | ;; |
| 101 | esac |
| 102 | done |
| 103 | # Now that all the other dependencies are unloaded, we can unload the |
| 104 | # dahdi_echocan modules. The drivers that register spans may keep |
| 105 | # references on the echocan modules before they are unloaded. |
| 106 | for mod in $ec_modules; do |
| 107 | (unload_module $mod) |
| 108 | done |
| 109 | rmmod $module |
| 110 | } |
| 111 | |
| 112 | unload_modules() { |
| 113 | for module in $DAHDI_UNLOAD_MODULES; do |
| 114 | unload_module $module |
| 115 | done |
| 116 | } |
| 117 | |
| 118 | # In (xpp) hotplug mode, the init script is also executed from the |
| 119 | # hotplug hook. In that case it should not attempt to loade modules. |
| 120 | # |
| 121 | # This function only retunrs false (1) if we're in hotplug mode and |
| 122 | # coming from the hotplug hook script. |
| 123 | hotplug_should_load_modules() { |
| 124 | if [ "$XPP_HOTPLUG_DAHDI" = yes -a "$CALLED_FROM_ATRIBANK_HOOK" != '' ] |
| 125 | then |
| 126 | return 1 |
| 127 | fi |
| 128 | return 0 |
| 129 | } |
| 130 | |
| 131 | # In (xpp) hotplug mode: quit after we loaded modules. |
| 132 | # |
| 133 | # In hotplug mode, the main run should end here, whereas the rest of the |
| 134 | # script should be finished by the instance running from the hook. |
| 135 | # Note that we only get here if there are actually Astribanks on the |
| 136 | # system (otherwise noone will trigger the run of the hotplug hook |
| 137 | # script). |
| 138 | hotplug_exit_after_load() { |
| 139 | if [ "$XPP_HOTPLUG_DAHDI" = yes -a "$CALLED_FROM_ATRIBANK_HOOK" = '' ] |
| 140 | then |
| 141 | exit 0 |
| 142 | fi |
| 143 | } |
| 144 | |
| 145 | # Initialize the Xorcom Astribank (xpp/) using perl utiliites: |
| 146 | xpp_startup() { |
| 147 | if [ "$ASTERISK_SUPPORTS_DAHDI_HOTPLUG" = yes ]; then |
| 148 | aas_param='/sys/module/dahdi/parameters/auto_assign_spans' |
| 149 | aas=`cat "$aas_param" 2>/dev/null` |
| 150 | if [ "$aas" = 0 ]; then |
| 151 | echo 1>&2 "Don't wait for Astribanks (use Asterisk hotplug-support)" |
| 152 | return 0 |
| 153 | fi |
| 154 | fi |
| 155 | # do nothing if there are no astribank devices: |
| 156 | if ! /usr/share/dahdi/waitfor_xpds; then return 0; fi |
| 157 | |
| 158 | hotplug_exit_after_load |
| 159 | } |
| 160 | |
| 161 | |
| 162 | hpec_start() { |
| 163 | # HPEC license found |
| 164 | if ! echo /var/lib/digium/licenses/HPEC-*.lic | grep -v '\*' | grep -q .; then |
| 165 | return |
| 166 | fi |
| 167 | |
| 168 | # dahdihpec_enable not installed in /usr/sbin |
| 169 | if [ ! -f /usr/sbin/dahdihpec_enable ]; then |
| 170 | echo -n "Running dahdihpec_enable: Failed" |
| 171 | echo -n "." |
| 172 | echo " The dahdihpec_enable binary is not installed in /usr/sbin." |
| 173 | return |
| 174 | fi |
| 175 | |
| 176 | # dahdihpec_enable not set executable |
| 177 | if [ ! -x /usr/sbin/dahdihpec_enable ]; then |
| 178 | echo -n "Running dahdihpec_enable: Failed" |
| 179 | echo -n "." |
| 180 | echo " /usr/sbin/dahdihpec_enable is not set as executable." |
| 181 | return |
| 182 | fi |
| 183 | |
| 184 | # dahdihpec_enable properly installed |
| 185 | if [ $system = debian ]; then |
| 186 | echo -n "Running dahdihpec_enable: " |
| 187 | /usr/sbin/dahdihpec_enable 2> /dev/null |
| 188 | elif [ $system = redhat ]; then |
| 189 | action "Running dahdihpec_enable: " /usr/sbin/dahdihpec_enable |
| 190 | fi |
| 191 | if [ $? = 0 ]; then |
| 192 | echo -n "done" |
| 193 | echo "." |
| 194 | else |
| 195 | echo -n "Failed" |
| 196 | echo -n "." |
| 197 | echo " This can be caused if you had already run dahdihpec_enable, or if your HPEC license is no longer valid." |
| 198 | fi |
| 199 | } |
| 200 | |
| 201 | shutdown_dynamic() { |
| 202 | if ! grep -q ' DYN/' /proc/dahdi/* 2>/dev/null; then return; fi |
| 203 | |
| 204 | # we should only get here if we have dynamic spans. Right? |
| 205 | $DAHDI_CFG_CMD -s |
| 206 | } |
| 207 | |
| 208 | load_modules() { |
| 209 | # Some systems, e.g. Debian Lenny, add here -b, which will break |
| 210 | # loading of modules blacklisted in modprobe.d/* |
| 211 | unset MODPROBE_OPTIONS |
| 212 | modules=`sed -e 's/#.*$//' $DAHDI_MODULES_FILE 2>/dev/null` |
| 213 | #if [ "$modules" = '' ]; then |
| 214 | # what? |
| 215 | #fi |
| 216 | echo "Loading DAHDI hardware modules:" |
| 217 | modprobe dahdi |
| 218 | for line in $modules; do |
| 219 | if [ $system = debian ]; then |
| 220 | echo -n " ${line}: " |
| 221 | if modprobe $line 2> /dev/null; then |
| 222 | echo -n "done" |
| 223 | else |
| 224 | echo -n "error" |
| 225 | fi |
| 226 | elif [ $system = redhat ]; then |
| 227 | action " ${line}: " modprobe $line |
| 228 | fi |
| 229 | done |
| 230 | echo "" |
| 231 | } |
| 232 | |
| 233 | # Make sure that either dahdi is loaded or modprobe-able |
| 234 | dahdi_modules_loadable() { |
| 235 | modinfo dahdi >/dev/null 2>&1 || lsmod | grep -q -w ^dahdi |
| 236 | } |
| 237 | |
| 238 | if [ ! -x "$DAHDI_CFG" ]; then |
| 239 | echo "dahdi_cfg not executable" |
| 240 | exit 0 |
| 241 | fi |
| 242 | |
| 243 | RETVAL=0 |
| 244 | |
| 245 | # See how we were called. |
| 246 | case "$1" in |
| 247 | start) |
| 248 | if ! dahdi_modules_loadable; then |
| 249 | echo "No DAHDI modules on the system. Not starting" |
| 250 | exit 0 |
| 251 | fi |
| 252 | if hotplug_should_load_modules; then |
| 253 | load_modules |
| 254 | fi |
| 255 | |
| 256 | TMOUT=$DAHDI_DEV_TIMEOUT # max secs to wait |
| 257 | |
| 258 | while [ ! -d /dev/dahdi ] ; do |
| 259 | sleep 1 |
| 260 | TMOUT=`expr $TMOUT - 1` |
| 261 | if [ $TMOUT -eq 0 ] ; then |
| 262 | echo "Error: missing /dev/dahdi!" |
| 263 | exit 1 |
| 264 | fi |
| 265 | done |
| 266 | |
| 267 | xpp_startup |
| 268 | |
| 269 | # Assign all spans that weren't handled via udev + /etc/dahdi/assigned-spans.conf |
| 270 | /usr/share/dahdi/dahdi_auto_assign_compat |
| 271 | |
| 272 | if [ $system = debian ]; then |
| 273 | echo -n "Running dahdi_cfg: " |
| 274 | $DAHDI_CFG_CMD 2> /dev/null && echo -n "done" |
| 275 | echo "." |
| 276 | elif [ $system = redhat ]; then |
| 277 | action "Running dahdi_cfg: " $DAHDI_CFG_CMD |
| 278 | fi |
| 279 | RETVAL=$? |
| 280 | |
| 281 | if [ "$LOCKFILE" != '' ]; then |
| 282 | [ $RETVAL -eq 0 ] && touch $LOCKFILE |
| 283 | fi |
| 284 | |
| 285 | if [ -x "$FXOTUNE" ] && [ -r /etc/fxotune.conf ]; then |
| 286 | # Allowed to fail if e.g. Asterisk already uses channels: |
| 287 | $FXOTUNE -s || : |
| 288 | fi |
| 289 | |
| 290 | # Do not try to call xpp_sync if there are no Astribank devices |
| 291 | # installed. |
| 292 | if test -e /sys/bus/astribanks; then |
| 293 | # Set the right Astribanks ticker: |
| 294 | LC_ALL=C xpp_sync "$XPP_SYNC" |
| 295 | fi |
| 296 | |
| 297 | hpec_start |
| 298 | ;; |
| 299 | stop) |
| 300 | # Unload drivers |
| 301 | #shutdown_dynamic # FIXME: needs test from someone with dynamic spans |
| 302 | echo -n "Unloading DAHDI hardware modules: " |
| 303 | if unload_modules; then |
| 304 | echo "done" |
| 305 | else |
| 306 | echo "error" |
| 307 | fi |
| 308 | if [ "$LOCKFILE" != '' ]; then |
| 309 | [ $RETVAL -eq 0 ] && rm -f $LOCKFILE |
| 310 | fi |
| 311 | ;; |
| 312 | unload) |
| 313 | unload_modules |
| 314 | ;; |
| 315 | restart|force-reload) |
| 316 | $0 stop |
| 317 | $0 start |
| 318 | ;; |
| 319 | reload) |
| 320 | if [ $system = debian ]; then |
| 321 | echo -n "Rerunning dahdi_cfg: " |
| 322 | $DAHDI_CFG_CMD 2> /dev/null && echo -n "done" |
| 323 | echo "." |
| 324 | elif [ $system = redhat ]; then |
| 325 | action "Rerunning dahdi_cfg: " $DAHDI_CFG_CMD |
| 326 | fi |
| 327 | RETVAL=$? |
| 328 | ;; |
| 329 | status) |
| 330 | if [ -d /proc/dahdi ]; then |
| 331 | /usr/sbin/lsdahdi |
| 332 | RETVAL=0 |
| 333 | else |
| 334 | RETVAL=3 |
| 335 | fi |
| 336 | ;; |
| 337 | *) |
| 338 | echo "Usage: dahdi {start|stop|restart|status|reload|unload}" |
| 339 | exit 1 |
| 340 | esac |
| 341 | |
| 342 | exit $RETVAL |
| 343 | |