script/init.d/ethercat
branchstable-1.3
changeset 1744 7bc131b92039
parent 1740 fa0af75bfbe9
child 1745 07fd94c5119d
--- a/script/init.d/ethercat	Fri Aug 10 15:08:44 2007 +0000
+++ b/script/init.d/ethercat	Fri Aug 10 15:27:08 2007 +0000
@@ -49,231 +49,199 @@
 
 #------------------------------------------------------------------------------
 
-device="ecxml"
-
-IFCONFIG=ifconfig
-BRCTL=brctl
-ROUTE=route
+MODPROBE=/sbin/modprobe
+RMMOD=/sbin/rmmod
+MODINFO=/sbin/modinfo
 
 #------------------------------------------------------------------------------
 
 ETHERCAT_CONFIG=/etc/sysconfig/ethercat
 
-if [ ! -r $ETHERCAT_CONFIG ]; then
-    echo "$ETHERCAT_CONFIG not existing";
-    if [ "$1" = "stop" ]; then
+if [ ! -r ${ETHERCAT_CONFIG} ]; then
+    echo ${ETHERCAT_CONFIG} not existing;
+    if [ "${1}" = "stop" ]; then
 	exit 0
     else
 	exit 6
     fi
 fi
 
-. $ETHERCAT_CONFIG
-
-#------------------------------------------------------------------------------
-
-#
-#  Function for setting up the EoE bridge
-#
-build_eoe_bridge()
-{
-    if [ -z "$EOE_BRIDGE" ]; then return; fi
-
-    EOEIF=`$IFCONFIG -a | grep -o -E "^eoe[0-9]+ "`
-
-    # add bridge, if it does not already exist
-    if ! $BRCTL show | grep -E -q "^$EOE_BRIDGE"; then
-        if ! $BRCTL addbr $EOE_BRIDGE; then
-	    /bin/false
-	    rc_status -v
-	    rc_exit
-	fi
-    fi
-
-    # check if specified interfaces are bridged
-    for interf in $EOEIF $EOE_EXTRA_INTERFACES; do
-	# interface is already part of the bridge (FIXME->show $EOE_BRIDGE)
-	if $BRCTL show | grep -E -q $interf
-	    then continue
-	fi
-	# clear IP address and open interface
-	if ! $IFCONFIG $interf 0.0.0.0 up; then
-	    /bin/false
-	    rc_status -v
-	    rc_exit
-	fi
-	# add interface to the bridge
-	if ! $BRCTL addif $EOE_BRIDGE $interf; then
-	    /bin/false
-	    rc_status -v
-	    rc_exit
-	fi
-    done
-
-    # configure IP on bridge
-    if [ -n "$EOE_IP_ADDRESS" -a -n "$EOE_IP_NETMASK" ]; then
-	if ! $IFCONFIG $EOE_BRIDGE $EOE_IP_ADDRESS \
-	    netmask $EOE_IP_NETMASK; then
-	    /bin/false
-	    rc_status -v
-	    rc_exit
-	fi
-    fi
-
-    # open bridge
-    if ! $IFCONFIG $EOE_BRIDGE up; then
-	/bin/false
-	rc_status -v
-	rc_exit
-    fi
-
-    # install new default gateway
-    if [ -n "$EOE_GATEWAY" ]; then
-	while $ROUTE -n | grep -E -q "^0.0.0.0"; do
-	    if ! $ROUTE del default; then
-		echo "Failed to remove route!" 1>&2
-		/bin/false
-		rc_status -v
-		rc_exit
-	    fi
-	done
-	if ! $ROUTE add default gw $EOE_GATEWAY; then
-	    /bin/false
-	    rc_status -v
-	    rc_exit
-	fi
-    fi
-}
-
-#------------------------------------------------------------------------------
-
-. /etc/rc.status
-rc_reset
-
-case "$1" in
-
-    start)
-	echo -n "Starting EtherCAT master "
-
-	if [ -z "$DEVICE_INDEX" ]; then
-	    echo "ERROR: DEVICE_INDEX not set!"
-	    /bin/false
-	    rc_status -v
-	    rc_exit
-	fi
-
-	if [ -z "$EOE_INTERFACES" ]; then
-            # support legacy sysconfig files
-	    if [ -n "$EOE_DEVICES" ]; then
-		EOE_INTERFACES=$EOE_DEVICES
-	    else
-		EOE_INTERFACES=0
-	    fi
-	fi
-
-        # unload conflicting modules at first
-	for mod in 8139too; do
-	    if lsmod | grep "^$mod " > /dev/null; then
-		if ! rmmod $mod; then
-		    /bin/false
-		    rc_status -v
-		    rc_exit
-		fi
-	    fi
-	done
-
-        # load master module
-	if ! modprobe ec_master ec_eoeif_count=$EOE_INTERFACES; then
-	    modprobe 8139too
-	    /bin/false
-	    rc_status -v
-	    rc_exit
-	fi
-
-	# remove stale device node
-	rm -f /dev/${device}0
-
-	# get dynamic major number
-	major=$(awk "\$2==\"EtherCAT\" {print \$1}" /proc/devices)
-
-	# create character device
-	mknod /dev/${device}0 c $major 0
-
-	# load device module
-	if ! modprobe ec_8139too ec_device_index=$DEVICE_INDEX; then
-	    rmmod ec_master
-	    modprobe 8139too
-	    /bin/false
-	    rc_status -v
-	    rc_exit
-	fi
-
-	# build EoE bridge
-	build_eoe_bridge
-
-	rc_status -v
-	;;
-
-    stop)
-	echo -n "Shutting down EtherCAT master "
-
-	# unload modules
-	for mod in ec_8139too ec_master; do
-	    if lsmod | grep "^$mod " > /dev/null; then
-		if ! rmmod $mod; then
-		    /bin/false
-		    rc_status -v
-		    rc_exit
-		fi;
-	    fi;
-	done
-
-	# remove device node
-	rm -f /dev/${device}0
-
-	sleep 1
-
-	# reload previous modules
-	if ! modprobe 8139too; then
-	    echo "Warning: Failed to restore 8139too module."
-	fi
-
-	rc_status -v
-	;;
-
-    restart)
-	$0 stop || exit 1
-	sleep 1
-	$0 start
-	rc_status
-	;;
-
-    status)
-	echo -n "Checking for EtherCAT "
-
-	lsmod | grep "^ec_master " > /dev/null
-	master_running=$?
-	lsmod | grep "^ec_8139too " > /dev/null
-	device_running=$?
-
-	# master module and device module loaded?
-	test $master_running -eq 0 -a $device_running -eq 0
-
-	rc_status -v
-	;;
-
-    bridge)
-	echo -n "Building EoE bridge "
-	build_eoe_bridge
-	rc_status -v
-	;;
-
-    *)
-	echo "USAGE: $0 {start|stop|restart|status|bridge}"
-	;;
+. ${ETHERCAT_CONFIG}
+
+#------------------------------------------------------------------------------
+
+function exit_success()
+{
+    if [ -r /etc/rc.status ]; then
+        rc_reset
+        rc_status -v
+        rc_exit
+    else
+        echo " done"
+        exit 0
+    fi
+}
+
+#------------------------------------------------------------------------------
+
+function exit_running()
+{
+    if [ -r /etc/rc.status ]; then
+        rc_reset
+        rc_status -v
+        rc_exit
+    else
+        echo " running"
+        exit 0
+    fi
+}
+
+#------------------------------------------------------------------------------
+
+function exit_fail()
+{
+    if [ -r /etc/rc.status ]; then
+        rc_failed
+        rc_exit
+    else
+        echo " failed"
+        exit 1
+    fi
+}
+
+#------------------------------------------------------------------------------
+
+function parse_mac_address()
+{
+    if [ -z "${1}" ]; then
+        MAC=""
+    elif echo ${1} | grep -qE '^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$'; then
+        MAC=${1}
+    else
+        echo Invalid MAC address \"${1}\" in ${ETHERCAT_CONFIG}
+        exit_fail
+    fi 
+}
+
+#------------------------------------------------------------------------------
+
+if [ -r /etc/rc.status ]; then
+    . /etc/rc.status
+    rc_reset
+fi
+
+case "${1}" in
+
+start)
+    echo -n "Starting EtherCAT master "
+
+    # construct DEVICES and BACKUPS from configuration variables
+    DEVICES=""
+    BACKUPS=""
+    MASTER_INDEX=0
+    while true; do
+        DEVICE=$(eval echo "\${MASTER${MASTER_INDEX}_DEVICE}")
+        BACKUP=$(eval echo "\${MASTER${MASTER_INDEX}_BACKUP}")
+        if [ -z "${DEVICE}" ]; then break; fi
+
+        if [ ${MASTER_INDEX} -gt 0 ]; then
+            DEVICES=${DEVICES},
+            BACKUPS=${BACKUPS},
+        fi
+
+        parse_mac_address ${DEVICE}
+        DEVICES=${DEVICES}${MAC}
+        
+        parse_mac_address ${BACKUP}
+        BACKUPS=${BACKUPS}${MAC}
+
+        MASTER_INDEX=$(expr ${MASTER_INDEX} + 1)
+    done
+
+    # load master module
+    if ! ${MODPROBE} ec_master main=${DEVICES} backup=${BACKUPS}; then
+        exit_fail
+    fi
+
+    # check for modules to replace
+    for MODULE in ${DEVICE_MODULES}; do
+        ECMODULE=ec_${MODULE}
+        if ! ${MODINFO} ${ECMODULE} > /dev/null; then
+            continue # ec_* module not found
+        fi
+        if lsmod | grep "^${MODULE} " > /dev/null; then
+            if ! ${RMMOD} ${MODULE}; then
+                exit_fail
+            fi
+        fi
+        if ! ${MODPROBE} ${ECMODULE}; then
+            ${MODPROBE} ${MODULE} # try to restore module
+            exit_fail
+        fi
+    done
+
+    exit_success
+    ;;
+
+stop)
+    echo -n "Shutting down EtherCAT master "
+
+    # unload EtherCAT device modules
+    for MODULE in ${DEVICE_MODULES} master; do
+        ECMODULE=ec_${MODULE}
+        if ! lsmod | grep -q "^${ECMODULE} "; then
+            continue # ec_* module not loaded
+        fi
+        if ! ${RMMOD} ${ECMODULE}; then
+            exit_fail
+        fi;
+    done
+
+    sleep 1
+
+    # reload previous modules
+    for MODULE in ${DEVICE_MODULES}; do
+        if ! ${MODPROBE} ${MODULE}; then
+            echo Warning: Failed to restore ${MODULE}.
+        fi
+    done
+
+    exit_success
+    ;;
+
+restart)
+    $0 stop || exit 1
+    sleep 1
+    $0 start
+    ;;
+
+status)
+    echo -n "Checking for EtherCAT "
+
+    lsmod | grep -q "^ec_master "
+    MASTERS_RUNNING=$?
+
+    ! grep -q "(WAITING)" /sys/ethercat/master*/info
+    MASTERS_IDLE=$?
+
+    # master module loaded and masters not waiting for devices?
+    if [ ${MASTERS_RUNNING} -eq 0 -a ${MASTERS_IDLE} -eq 0 ]; then
+        exit_running
+    else
+        exit_fail
+    fi
+    ;;
+
+*)
+    echo "USAGE: $0 {start|stop|restart|status}"
+    ;;
 
 esac
 
-rc_exit
-
-#------------------------------------------------------------------------------
+if [ -r /etc/rc.status ]; then
+    rc_exit
+else
+    exit 1
+fi
+
+#------------------------------------------------------------------------------