Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > 4899a4bb718038f3a1f380b15e2ca880 > files > 10

dahdi-tools-2.2.0-3mdv2010.0.i586.rpm

#!/bin/sh
#
# dahdi         This shell script takes care of loading and unloading \
#               DAHDI Telephony interfaces
# chkconfig: - 9 92
# description: The DAHDI drivers allow you to use your linux \
# computer to accept incoming data and voice interfaces
#
# config: /etc/dahdi/init.conf

### BEGIN INIT INFO
# Provides:        dahdi
# Required-Start:  $local_fs $remote_fs
# Required-Stop:
# Should-Start:    $network $syslog
# Should-Stop:
# Default-Start:   2 3 4 5
# Default-Stop:    0 1 6
# Description:     dahdi - load and configure DAHDI modules
### END INIT INFO

initdir=/etc/init.d

# Don't edit the following values. Edit /etc/dahdi/init.conf instead.

DAHDI_CFG=/usr/sbin/dahdi_cfg
DAHDI_CFG_CMD="$DAHDI_CFG" # e.g: for a custom system.conf location

FXOTUNE=/usr/sbin/fxotune

# The default syncer Astribank. Usually set automatically to a sane
# value by xpp_sync(1) if you have an Astribank. You can set this to an
# explicit Astribank (e.g: 01).
XPP_SYNC=auto

# The maximal timeout (seconds) to wait for udevd to finish generating
# device nodes after the modules have loaded and before running dahdi_cfg.
DAHDI_DEV_TIMEOUT=20

#
# Determine which kind of configuration we're using
#
system=redhat  # assume redhat
if [ -f /etc/debian_version ]; then
    system=debian
fi

if [ -f /etc/gentoo-release ]; then
    system=debian
fi

# Source function library.
if [ $system = redhat ]; then
    . $initdir/functions || exit 0
fi

DAHDI_MODULES_FILE="/etc/dahdi/modules"

[ -r /etc/dahdi/init.conf ] && . /etc/dahdi/init.conf

if [ $system = redhat ]; then
	LOCKFILE=/var/lock/subsys/dahdi
fi

# recursively unload a module and its dependencies, if possible.
# where's modprobe -r when you need it?
# inputs: module to unload.
# returns: the result from
unload_module() {
	module="$1"
	line=`lsmod 2>/dev/null | grep "^$1 "`
	if [ "$line" = '' ]; then return; fi # module was not loaded

	set -- $line
	# $1: the original module, $2: size, $3: refcount, $4: deps list
	mods=`echo $4 | tr , ' '`
	# xpp_usb keeps the xpds below busy if an xpp hardware is
	# connected. Hence must be removed before them:
	case "$module" in xpd_*) mods="xpp_usb $mods";; esac
	for mod in $mods; do
		# run in a subshell, so it won't step over our vars:
		(unload_module $mod)
		# TODO: the following is probably the error handling we want:
		# if [ $? != 0 ]; then return 1; fi
	done
	rmmod $module
}

# Initialize the Xorcom Astribank (xpp/) using perl utiliites:
xpp_startup() {
	# do nothing if there are no astribank devices:
	if ! /usr/share/dahdi/waitfor_xpds; then return 0; fi

	# overriding locales for the above two, as perl can be noisy
	# when locales are missing.
	# No register all the devices if they didn't auto-register:
	LC_ALL=C dahdi_registration on
}


hpec_start() {
	# HPEC license found
	if ! echo /var/lib/digium/licenses/HPEC-*.lic | grep -v '\*' | grep -q .; then
		return
	fi

	# dahdihpec_enable not installed in /usr/sbin
	if [ ! -f /usr/sbin/dahdihpec_enable ]; then
		gprintf "Running dahdihpec_enable: Failed"
		gprintf "."
		gprintf "  The dahdihpec_enable binary is not installed in /usr/sbin.\n"
		return
	fi

	# dahdihpec_enable not set executable
	if [ ! -x /usr/sbin/dahdihpec_enable ]; then
		gprintf "Running dahdihpec_enable: Failed"
		gprintf "."
		gprintf "  /usr/sbin/dahdihpec_enable is not set as executable.\n"
		return
	fi

	# dahdihpec_enable properly installed
	if [ $system = debian ]; then
		gprintf "Running dahdihpec_enable: "
		/usr/sbin/dahdihpec_enable 2> /dev/null
	elif [ $system = redhat ]; then
		action "Running dahdihpec_enable: " /usr/sbin/dahdihpec_enable
	fi
	if [ $? = 0 ]; then
		gprintf "done"
		gprintf ".\n"
	else
		gprintf "Failed"
		gprintf "."
		gprintf "  This can be caused if you had already run dahdihpec_enable, or if your HPEC license is no longer valid.\n"
	fi
}

shutdown_dynamic() {
	if ! grep -q ' DYN/' /proc/dahdi/* 2>/dev/null; then return; fi

	# we should only get here if we have dynamic spans. Right?
	$DAHDI_CFG_CMD -s
}

if [ ! -x "$DAHDI_CFG" ]; then
       gprintf "dahdi_cfg not executable\n"
       exit 0
fi

if [ ! -f /etc/dahdi/system.conf ]; then
	gprintf "/etc/dahdi/system.conf not found. Nothing to do.\n"
       exit 0
fi

RETVAL=0

# See how we were called.
case "$1" in
  start)
  	# Some systems, e.g. Debian Lenny, add here -b, which will break
	# loading of modules blacklisted in modprobe.d/*
	unset MODPROBE_OPTIONS
  	modules=`sed -e 's/#.*$//' $DAHDI_MODULES_FILE 2>/dev/null`
	#if [ "$modules" = '' ]; then
		# what?
	#fi
	gprintf "Loading DAHDI hardware modules:\n"
	modprobe dahdi
	for line in $modules; do
		if [ $system = debian ]; then
			gprintf "   %s: " "${line}"
			if modprobe $line 2> /dev/null; then
				gprintf "done"
			else
				gprintf "error"
			fi
		elif [ $system = redhat ]; then
			action "  %s: " "${line}" modprobe $line
		fi
	done
	echo ""

	TMOUT=$DAHDI_DEV_TIMEOUT # max secs to wait

	while [ ! -d /dev/dahdi ] ; do
 		sleep 1
		TMOUT=`expr $TMOUT - 1`
		if [ $TMOUT -eq 0 ] ; then
			gprintf "Error: missing /dev/dahdi!\n"
			exit 1
		fi
	done

	xpp_startup

	if [ ! -e /proc/dahdi/1 ]; then
		gprintf "No hardware timing source found in /proc/dahdi, loading dahdi_dummy\n"
		modprobe dahdi_dummy 2> /dev/null
	fi

	if [ $system = debian ]; then
	    gprintf "Running dahdi_cfg: "
	    $DAHDI_CFG_CMD 2> /dev/null && gprintf "done"
	    gprintf ".\n"
	elif [ $system = redhat ]; then
	    action "Running dahdi_cfg: " $DAHDI_CFG_CMD
	fi
	RETVAL=$?

	if [ "$LOCKFILE" != '' ]; then
		[ $RETVAL -eq 0 ] && touch $LOCKFILE
	fi

	if [ -x "$FXOTUNE" ] && [ -r /etc/fxotune.conf ]; then
		# Allowed to fail if e.g. Asterisk already uses channels:
		$FXOTUNE -s || :
	fi

	# Set the right Astribanks ticker:
	LC_ALL=C xpp_sync "$XPP_SYNC"

	hpec_start
	;;
  stop)
	# Unload drivers
	#shutdown_dynamic # FIXME: needs test from someone with dynamic spans
	gprintf "Unloading DAHDI hardware modules: "
	if unload_module dahdi; then
		gprintf "done\n"
	else
		gprintf "error\n"
	fi
	if [ "$LOCKFILE" != '' ]; then
		[ $RETVAL -eq 0 ] && rm -f $LOCKFILE
	fi
	;;
  unload)
	unload_module dahdi
	;;
  restart|force-reload)
	$0 stop
	$0 start
	;;
  reload)
	if [ $system = debian ]; then
	    gprintf "Rerunning dahdi_cfg: "
	    $DAHDI_CFG_CMD 2> /dev/null && gprintf "done"
	    gprintf ".\n"
	elif [ $system = redhat ]; then
	    action "Rerunning dahdi_cfg: " $DAHDI_CFG_CMD
	fi
	RETVAL=$?
	;;
  status)
	if [ -d /proc/dahdi ]; then
		/usr/sbin/lsdahdi
		RETVAL=0
	else
		RETVAL=3
	fi
	;;
  *)
	gprintf "Usage: dahdi {start|stop|restart|status|reload|unload}\n"
	exit 1
esac

exit $RETVAL