script/init.d/ethercat.in
changeset 1185 337ce4fc2383
parent 1090 ee122eadda7f
child 1190 5f2e38dddd4e
equal deleted inserted replaced
1184:75cd6681eb08 1185:337ce4fc2383
       
     1 #!/bin/sh
       
     2 
       
     3 #------------------------------------------------------------------------------
       
     4 #
       
     5 #  Init script for EtherCAT
       
     6 #
       
     7 #  $Id$
       
     8 #
       
     9 #  Copyright (C) 2006  Florian Pose, Ingenieurgemeinschaft IgH
       
    10 #
       
    11 #  This file is part of the IgH EtherCAT Master.
       
    12 #
       
    13 #  The IgH EtherCAT Master is free software; you can redistribute it
       
    14 #  and/or modify it under the terms of the GNU General Public License
       
    15 #  as published by the Free Software Foundation; either version 2 of the
       
    16 #  License, or (at your option) any later version.
       
    17 #
       
    18 #  The IgH EtherCAT Master is distributed in the hope that it will be
       
    19 #  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    20 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    21 #  GNU General Public License for more details.
       
    22 #
       
    23 #  You should have received a copy of the GNU General Public License
       
    24 #  along with the IgH EtherCAT Master; if not, write to the Free Software
       
    25 #  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
       
    26 #
       
    27 #  The right to use EtherCAT Technology is granted and comes free of
       
    28 #  charge under condition of compatibility of product made by
       
    29 #  Licensee. People intending to distribute/sell products based on the
       
    30 #  code, have to sign an agreement to guarantee that products using
       
    31 #  software based on IgH EtherCAT master stay compatible with the actual
       
    32 #  EtherCAT specification (which are released themselves as an open
       
    33 #  standard) as the (only) precondition to have the right to use EtherCAT
       
    34 #  Technology, IP and trade marks.
       
    35 #
       
    36 #------------------------------------------------------------------------------
       
    37 
       
    38 ### BEGIN INIT INFO
       
    39 # Provides:          ethercat
       
    40 # Required-Start:    $local_fs $syslog $network
       
    41 # Should-Start:      $time ntp
       
    42 # Required-Stop:     $local_fs $syslog $network
       
    43 # Should-Stop:       $time ntp
       
    44 # Default-Start:     3 5
       
    45 # Default-Stop:      0 1 2 6
       
    46 # Short-Description: EtherCAT master
       
    47 # Description:       EtherCAT master @VERSION@
       
    48 ### END INIT INFO
       
    49 
       
    50 #------------------------------------------------------------------------------
       
    51 
       
    52 MODPROBE=/sbin/modprobe
       
    53 RMMOD=/sbin/rmmod
       
    54 MODINFO=/sbin/modinfo
       
    55 ETHERCAT=@prefix@/bin/ethercat
       
    56 
       
    57 #------------------------------------------------------------------------------
       
    58 
       
    59 ETHERCAT_CONFIG=/etc/sysconfig/ethercat
       
    60 
       
    61 if [ ! -r ${ETHERCAT_CONFIG} ]; then
       
    62     echo ${ETHERCAT_CONFIG} not existing;
       
    63     if [ "${1}" = "stop" ]; then
       
    64 	exit 0
       
    65     else
       
    66 	exit 6
       
    67     fi
       
    68 fi
       
    69 
       
    70 . ${ETHERCAT_CONFIG}
       
    71 
       
    72 #------------------------------------------------------------------------------
       
    73 
       
    74 function exit_success()
       
    75 {
       
    76     if [ -r /etc/rc.status ]; then
       
    77         rc_reset
       
    78         rc_status -v
       
    79         rc_exit
       
    80     else
       
    81         echo " done"
       
    82         exit 0
       
    83     fi
       
    84 }
       
    85 
       
    86 #------------------------------------------------------------------------------
       
    87 
       
    88 function exit_fail()
       
    89 {
       
    90     if [ -r /etc/rc.status ]; then
       
    91         rc_failed
       
    92         rc_status -v
       
    93         rc_exit
       
    94     else
       
    95         echo " failed"
       
    96         exit 1
       
    97     fi
       
    98 }
       
    99 
       
   100 #------------------------------------------------------------------------------
       
   101 
       
   102 function print_running()
       
   103 {
       
   104     if [ -r /etc/rc.status ]; then
       
   105         rc_reset
       
   106         rc_status -v
       
   107     else
       
   108         echo " running"
       
   109     fi
       
   110 }
       
   111 
       
   112 #------------------------------------------------------------------------------
       
   113 
       
   114 function print_dead()
       
   115 {
       
   116     if [ -r /etc/rc.status ]; then
       
   117         rc_failed
       
   118         rc_status -v
       
   119     else
       
   120         echo " dead"
       
   121     fi
       
   122 }
       
   123 
       
   124 #------------------------------------------------------------------------------
       
   125 
       
   126 function parse_mac_address()
       
   127 {
       
   128     if [ -z "${1}" ]; then
       
   129         MAC=""
       
   130     elif echo ${1} | grep -qE '^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$'; then
       
   131         MAC=${1}
       
   132     else
       
   133         echo Invalid MAC address \"${1}\" in ${ETHERCAT_CONFIG}
       
   134         exit_fail
       
   135     fi 
       
   136 }
       
   137 
       
   138 #------------------------------------------------------------------------------
       
   139 
       
   140 if [ -r /etc/rc.status ]; then
       
   141     . /etc/rc.status
       
   142     rc_reset
       
   143 fi
       
   144 
       
   145 case "${1}" in
       
   146 
       
   147 start)
       
   148     echo -n "Starting EtherCAT master @VERSION@"
       
   149 
       
   150     # construct DEVICES and BACKUPS from configuration variables
       
   151     DEVICES=""
       
   152     BACKUPS=""
       
   153     MASTER_INDEX=0
       
   154     while true; do
       
   155         DEVICE=$(eval echo "\${MASTER${MASTER_INDEX}_DEVICE}")
       
   156         BACKUP=$(eval echo "\${MASTER${MASTER_INDEX}_BACKUP}")
       
   157         if [ -z "${DEVICE}" ]; then break; fi
       
   158 
       
   159         if [ ${MASTER_INDEX} -gt 0 ]; then
       
   160             DEVICES=${DEVICES},
       
   161             BACKUPS=${BACKUPS},
       
   162         fi
       
   163 
       
   164         parse_mac_address ${DEVICE}
       
   165         DEVICES=${DEVICES}${MAC}
       
   166         
       
   167         parse_mac_address ${BACKUP}
       
   168         BACKUPS=${BACKUPS}${MAC}
       
   169 
       
   170         MASTER_INDEX=$(expr ${MASTER_INDEX} + 1)
       
   171     done
       
   172 
       
   173     # load master module
       
   174     if ! ${MODPROBE} ${MODPROBE_FLAGS} ec_master \
       
   175             main_devices=${DEVICES} backup_devices=${BACKUPS}; then
       
   176         exit_fail
       
   177     fi
       
   178 
       
   179     # check for modules to replace
       
   180     for MODULE in ${DEVICE_MODULES}; do
       
   181         ECMODULE=ec_${MODULE}
       
   182         if ! ${MODINFO} ${ECMODULE} > /dev/null; then
       
   183             continue # ec_* module not found
       
   184         fi
       
   185         if lsmod | grep "^${MODULE} " > /dev/null; then
       
   186             if ! ${RMMOD} ${MODULE}; then
       
   187                 exit_fail
       
   188             fi
       
   189         fi
       
   190         if ! ${MODPROBE} ${MODPROBE_FLAGS} ${ECMODULE}; then
       
   191             ${MODPROBE} ${MODPROBE_FLAGS} ${MODULE} # try to restore module
       
   192             exit_fail
       
   193         fi
       
   194     done
       
   195 
       
   196     exit_success
       
   197     ;;
       
   198 
       
   199 stop)
       
   200     echo -n "Shutting down EtherCAT master @VERSION@"
       
   201 
       
   202     # unload EtherCAT device modules
       
   203     for MODULE in ${DEVICE_MODULES} master; do
       
   204         ECMODULE=ec_${MODULE}
       
   205         if ! lsmod | grep -q "^${ECMODULE} "; then
       
   206             continue # ec_* module not loaded
       
   207         fi
       
   208         if ! ${RMMOD} ${ECMODULE}; then
       
   209             exit_fail
       
   210         fi;
       
   211     done
       
   212 
       
   213     sleep 1
       
   214 
       
   215     # reload previous modules
       
   216     for MODULE in ${DEVICE_MODULES}; do
       
   217         if ! ${MODPROBE} ${MODPROBE_FLAGS} ${MODULE}; then
       
   218             echo Warning: Failed to restore ${MODULE}.
       
   219         fi
       
   220     done
       
   221 
       
   222     exit_success
       
   223     ;;
       
   224 
       
   225 restart)
       
   226     $0 stop || exit 1
       
   227     sleep 1
       
   228     $0 start
       
   229     ;;
       
   230 
       
   231 status)
       
   232     echo "Checking for EtherCAT master @VERSION@"
       
   233 
       
   234     # count masters in configuration file
       
   235     MASTER_COUNT=0
       
   236     while true; do
       
   237         DEVICE=$(eval echo "\${MASTER${MASTER_COUNT}_DEVICE}")
       
   238         if [ -z "${DEVICE}" ]; then break; fi
       
   239         MASTER_COUNT=$(expr ${MASTER_COUNT} + 1)
       
   240     done
       
   241 
       
   242     RESULT=0
       
   243 
       
   244     for i in `seq 0 $(expr ${MASTER_COUNT} - 1)`; do
       
   245         echo -n "Master${i}"
       
   246         
       
   247         # Check if the master is in idle or operation phase
       
   248         ${ETHERCAT} master --master ${i} 2>/dev/null | \
       
   249             grep -qE 'Phase:[[:space:]]*Idle|Phase:[[:space:]]*Operation'
       
   250         EXITCODE=$?
       
   251 
       
   252         if [ ${EXITCODE} -eq 0 ]; then
       
   253             print_running
       
   254         else
       
   255             print_dead
       
   256             RESULT=1
       
   257         fi
       
   258     done
       
   259 
       
   260     exit ${RESULT}
       
   261     ;;
       
   262 
       
   263 *)
       
   264     echo "USAGE: $0 {start|stop|restart|status}"
       
   265     ;;
       
   266 
       
   267 esac
       
   268 
       
   269 if [ -r /etc/rc.status ]; then
       
   270     rc_exit
       
   271 else
       
   272     exit 1
       
   273 fi
       
   274 
       
   275 #------------------------------------------------------------------------------