script/init.d/ethercat
branchstable-1.3
changeset 1744 7bc131b92039
parent 1740 fa0af75bfbe9
child 1745 07fd94c5119d
equal deleted inserted replaced
1743:1a7067207637 1744:7bc131b92039
    47 # Description:
    47 # Description:
    48 ### END INIT INFO
    48 ### END INIT INFO
    49 
    49 
    50 #------------------------------------------------------------------------------
    50 #------------------------------------------------------------------------------
    51 
    51 
    52 device="ecxml"
    52 MODPROBE=/sbin/modprobe
    53 
    53 RMMOD=/sbin/rmmod
    54 IFCONFIG=ifconfig
    54 MODINFO=/sbin/modinfo
    55 BRCTL=brctl
       
    56 ROUTE=route
       
    57 
    55 
    58 #------------------------------------------------------------------------------
    56 #------------------------------------------------------------------------------
    59 
    57 
    60 ETHERCAT_CONFIG=/etc/sysconfig/ethercat
    58 ETHERCAT_CONFIG=/etc/sysconfig/ethercat
    61 
    59 
    62 if [ ! -r $ETHERCAT_CONFIG ]; then
    60 if [ ! -r ${ETHERCAT_CONFIG} ]; then
    63     echo "$ETHERCAT_CONFIG not existing";
    61     echo ${ETHERCAT_CONFIG} not existing;
    64     if [ "$1" = "stop" ]; then
    62     if [ "${1}" = "stop" ]; then
    65 	exit 0
    63 	exit 0
    66     else
    64     else
    67 	exit 6
    65 	exit 6
    68     fi
    66     fi
    69 fi
    67 fi
    70 
    68 
    71 . $ETHERCAT_CONFIG
    69 . ${ETHERCAT_CONFIG}
    72 
    70 
    73 #------------------------------------------------------------------------------
    71 #------------------------------------------------------------------------------
    74 
    72 
    75 #
    73 function exit_success()
    76 #  Function for setting up the EoE bridge
    74 {
    77 #
    75     if [ -r /etc/rc.status ]; then
    78 build_eoe_bridge()
    76         rc_reset
    79 {
    77         rc_status -v
    80     if [ -z "$EOE_BRIDGE" ]; then return; fi
    78         rc_exit
    81 
    79     else
    82     EOEIF=`$IFCONFIG -a | grep -o -E "^eoe[0-9]+ "`
    80         echo " done"
    83 
    81         exit 0
    84     # add bridge, if it does not already exist
    82     fi
    85     if ! $BRCTL show | grep -E -q "^$EOE_BRIDGE"; then
    83 }
    86         if ! $BRCTL addbr $EOE_BRIDGE; then
    84 
    87 	    /bin/false
    85 #------------------------------------------------------------------------------
    88 	    rc_status -v
    86 
    89 	    rc_exit
    87 function exit_running()
    90 	fi
    88 {
    91     fi
    89     if [ -r /etc/rc.status ]; then
    92 
    90         rc_reset
    93     # check if specified interfaces are bridged
    91         rc_status -v
    94     for interf in $EOEIF $EOE_EXTRA_INTERFACES; do
    92         rc_exit
    95 	# interface is already part of the bridge (FIXME->show $EOE_BRIDGE)
    93     else
    96 	if $BRCTL show | grep -E -q $interf
    94         echo " running"
    97 	    then continue
    95         exit 0
    98 	fi
    96     fi
    99 	# clear IP address and open interface
    97 }
   100 	if ! $IFCONFIG $interf 0.0.0.0 up; then
    98 
   101 	    /bin/false
    99 #------------------------------------------------------------------------------
   102 	    rc_status -v
   100 
   103 	    rc_exit
   101 function exit_fail()
   104 	fi
   102 {
   105 	# add interface to the bridge
   103     if [ -r /etc/rc.status ]; then
   106 	if ! $BRCTL addif $EOE_BRIDGE $interf; then
   104         rc_failed
   107 	    /bin/false
   105         rc_exit
   108 	    rc_status -v
   106     else
   109 	    rc_exit
   107         echo " failed"
   110 	fi
   108         exit 1
   111     done
   109     fi
   112 
   110 }
   113     # configure IP on bridge
   111 
   114     if [ -n "$EOE_IP_ADDRESS" -a -n "$EOE_IP_NETMASK" ]; then
   112 #------------------------------------------------------------------------------
   115 	if ! $IFCONFIG $EOE_BRIDGE $EOE_IP_ADDRESS \
   113 
   116 	    netmask $EOE_IP_NETMASK; then
   114 function parse_mac_address()
   117 	    /bin/false
   115 {
   118 	    rc_status -v
   116     if [ -z "${1}" ]; then
   119 	    rc_exit
   117         MAC=""
   120 	fi
   118     elif echo ${1} | grep -qE '^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$'; then
   121     fi
   119         MAC=${1}
   122 
   120     else
   123     # open bridge
   121         echo Invalid MAC address \"${1}\" in ${ETHERCAT_CONFIG}
   124     if ! $IFCONFIG $EOE_BRIDGE up; then
   122         exit_fail
   125 	/bin/false
   123     fi 
   126 	rc_status -v
   124 }
   127 	rc_exit
   125 
   128     fi
   126 #------------------------------------------------------------------------------
   129 
   127 
   130     # install new default gateway
   128 if [ -r /etc/rc.status ]; then
   131     if [ -n "$EOE_GATEWAY" ]; then
   129     . /etc/rc.status
   132 	while $ROUTE -n | grep -E -q "^0.0.0.0"; do
   130     rc_reset
   133 	    if ! $ROUTE del default; then
   131 fi
   134 		echo "Failed to remove route!" 1>&2
   132 
   135 		/bin/false
   133 case "${1}" in
   136 		rc_status -v
   134 
   137 		rc_exit
   135 start)
   138 	    fi
   136     echo -n "Starting EtherCAT master "
   139 	done
   137 
   140 	if ! $ROUTE add default gw $EOE_GATEWAY; then
   138     # construct DEVICES and BACKUPS from configuration variables
   141 	    /bin/false
   139     DEVICES=""
   142 	    rc_status -v
   140     BACKUPS=""
   143 	    rc_exit
   141     MASTER_INDEX=0
   144 	fi
   142     while true; do
   145     fi
   143         DEVICE=$(eval echo "\${MASTER${MASTER_INDEX}_DEVICE}")
   146 }
   144         BACKUP=$(eval echo "\${MASTER${MASTER_INDEX}_BACKUP}")
   147 
   145         if [ -z "${DEVICE}" ]; then break; fi
   148 #------------------------------------------------------------------------------
   146 
   149 
   147         if [ ${MASTER_INDEX} -gt 0 ]; then
   150 . /etc/rc.status
   148             DEVICES=${DEVICES},
   151 rc_reset
   149             BACKUPS=${BACKUPS},
   152 
   150         fi
   153 case "$1" in
   151 
   154 
   152         parse_mac_address ${DEVICE}
   155     start)
   153         DEVICES=${DEVICES}${MAC}
   156 	echo -n "Starting EtherCAT master "
   154         
   157 
   155         parse_mac_address ${BACKUP}
   158 	if [ -z "$DEVICE_INDEX" ]; then
   156         BACKUPS=${BACKUPS}${MAC}
   159 	    echo "ERROR: DEVICE_INDEX not set!"
   157 
   160 	    /bin/false
   158         MASTER_INDEX=$(expr ${MASTER_INDEX} + 1)
   161 	    rc_status -v
   159     done
   162 	    rc_exit
   160 
   163 	fi
   161     # load master module
   164 
   162     if ! ${MODPROBE} ec_master main=${DEVICES} backup=${BACKUPS}; then
   165 	if [ -z "$EOE_INTERFACES" ]; then
   163         exit_fail
   166             # support legacy sysconfig files
   164     fi
   167 	    if [ -n "$EOE_DEVICES" ]; then
   165 
   168 		EOE_INTERFACES=$EOE_DEVICES
   166     # check for modules to replace
   169 	    else
   167     for MODULE in ${DEVICE_MODULES}; do
   170 		EOE_INTERFACES=0
   168         ECMODULE=ec_${MODULE}
   171 	    fi
   169         if ! ${MODINFO} ${ECMODULE} > /dev/null; then
   172 	fi
   170             continue # ec_* module not found
   173 
   171         fi
   174         # unload conflicting modules at first
   172         if lsmod | grep "^${MODULE} " > /dev/null; then
   175 	for mod in 8139too; do
   173             if ! ${RMMOD} ${MODULE}; then
   176 	    if lsmod | grep "^$mod " > /dev/null; then
   174                 exit_fail
   177 		if ! rmmod $mod; then
   175             fi
   178 		    /bin/false
   176         fi
   179 		    rc_status -v
   177         if ! ${MODPROBE} ${ECMODULE}; then
   180 		    rc_exit
   178             ${MODPROBE} ${MODULE} # try to restore module
   181 		fi
   179             exit_fail
   182 	    fi
   180         fi
   183 	done
   181     done
   184 
   182 
   185         # load master module
   183     exit_success
   186 	if ! modprobe ec_master ec_eoeif_count=$EOE_INTERFACES; then
   184     ;;
   187 	    modprobe 8139too
   185 
   188 	    /bin/false
   186 stop)
   189 	    rc_status -v
   187     echo -n "Shutting down EtherCAT master "
   190 	    rc_exit
   188 
   191 	fi
   189     # unload EtherCAT device modules
   192 
   190     for MODULE in ${DEVICE_MODULES} master; do
   193 	# remove stale device node
   191         ECMODULE=ec_${MODULE}
   194 	rm -f /dev/${device}0
   192         if ! lsmod | grep -q "^${ECMODULE} "; then
   195 
   193             continue # ec_* module not loaded
   196 	# get dynamic major number
   194         fi
   197 	major=$(awk "\$2==\"EtherCAT\" {print \$1}" /proc/devices)
   195         if ! ${RMMOD} ${ECMODULE}; then
   198 
   196             exit_fail
   199 	# create character device
   197         fi;
   200 	mknod /dev/${device}0 c $major 0
   198     done
   201 
   199 
   202 	# load device module
   200     sleep 1
   203 	if ! modprobe ec_8139too ec_device_index=$DEVICE_INDEX; then
   201 
   204 	    rmmod ec_master
   202     # reload previous modules
   205 	    modprobe 8139too
   203     for MODULE in ${DEVICE_MODULES}; do
   206 	    /bin/false
   204         if ! ${MODPROBE} ${MODULE}; then
   207 	    rc_status -v
   205             echo Warning: Failed to restore ${MODULE}.
   208 	    rc_exit
   206         fi
   209 	fi
   207     done
   210 
   208 
   211 	# build EoE bridge
   209     exit_success
   212 	build_eoe_bridge
   210     ;;
   213 
   211 
   214 	rc_status -v
   212 restart)
   215 	;;
   213     $0 stop || exit 1
   216 
   214     sleep 1
   217     stop)
   215     $0 start
   218 	echo -n "Shutting down EtherCAT master "
   216     ;;
   219 
   217 
   220 	# unload modules
   218 status)
   221 	for mod in ec_8139too ec_master; do
   219     echo -n "Checking for EtherCAT "
   222 	    if lsmod | grep "^$mod " > /dev/null; then
   220 
   223 		if ! rmmod $mod; then
   221     lsmod | grep -q "^ec_master "
   224 		    /bin/false
   222     MASTERS_RUNNING=$?
   225 		    rc_status -v
   223 
   226 		    rc_exit
   224     ! grep -q "(WAITING)" /sys/ethercat/master*/info
   227 		fi;
   225     MASTERS_IDLE=$?
   228 	    fi;
   226 
   229 	done
   227     # master module loaded and masters not waiting for devices?
   230 
   228     if [ ${MASTERS_RUNNING} -eq 0 -a ${MASTERS_IDLE} -eq 0 ]; then
   231 	# remove device node
   229         exit_running
   232 	rm -f /dev/${device}0
   230     else
   233 
   231         exit_fail
   234 	sleep 1
   232     fi
   235 
   233     ;;
   236 	# reload previous modules
   234 
   237 	if ! modprobe 8139too; then
   235 *)
   238 	    echo "Warning: Failed to restore 8139too module."
   236     echo "USAGE: $0 {start|stop|restart|status}"
   239 	fi
   237     ;;
   240 
       
   241 	rc_status -v
       
   242 	;;
       
   243 
       
   244     restart)
       
   245 	$0 stop || exit 1
       
   246 	sleep 1
       
   247 	$0 start
       
   248 	rc_status
       
   249 	;;
       
   250 
       
   251     status)
       
   252 	echo -n "Checking for EtherCAT "
       
   253 
       
   254 	lsmod | grep "^ec_master " > /dev/null
       
   255 	master_running=$?
       
   256 	lsmod | grep "^ec_8139too " > /dev/null
       
   257 	device_running=$?
       
   258 
       
   259 	# master module and device module loaded?
       
   260 	test $master_running -eq 0 -a $device_running -eq 0
       
   261 
       
   262 	rc_status -v
       
   263 	;;
       
   264 
       
   265     bridge)
       
   266 	echo -n "Building EoE bridge "
       
   267 	build_eoe_bridge
       
   268 	rc_status -v
       
   269 	;;
       
   270 
       
   271     *)
       
   272 	echo "USAGE: $0 {start|stop|restart|status|bridge}"
       
   273 	;;
       
   274 
   238 
   275 esac
   239 esac
   276 
   240 
   277 rc_exit
   241 if [ -r /etc/rc.status ]; then
   278 
   242     rc_exit
   279 #------------------------------------------------------------------------------
   243 else
       
   244     exit 1
       
   245 fi
       
   246 
       
   247 #------------------------------------------------------------------------------