script/init.d/ethercat
changeset 1185 337ce4fc2383
parent 1184 75cd6681eb08
child 1186 ff481f097c97
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:
       
    48 ### END INIT INFO
       
    49 
       
    50 #------------------------------------------------------------------------------
       
    51 
       
    52 MODPROBE=/sbin/modprobe
       
    53 RMMOD=/sbin/rmmod
       
    54 MODINFO=/sbin/modinfo
       
    55 
       
    56 #------------------------------------------------------------------------------
       
    57 
       
    58 ETHERCAT_CONFIG=/etc/sysconfig/ethercat
       
    59 
       
    60 if [ ! -r ${ETHERCAT_CONFIG} ]; then
       
    61     echo ${ETHERCAT_CONFIG} not existing;
       
    62     if [ "${1}" = "stop" ]; then
       
    63 	exit 0
       
    64     else
       
    65 	exit 6
       
    66     fi
       
    67 fi
       
    68 
       
    69 . ${ETHERCAT_CONFIG}
       
    70 
       
    71 #------------------------------------------------------------------------------
       
    72 
       
    73 function exit_success()
       
    74 {
       
    75     if [ -r /etc/rc.status ]; then
       
    76         rc_reset
       
    77         rc_status -v
       
    78         rc_exit
       
    79     else
       
    80         echo " done"
       
    81         exit 0
       
    82     fi
       
    83 }
       
    84 
       
    85 #------------------------------------------------------------------------------
       
    86 
       
    87 function exit_running()
       
    88 {
       
    89     if [ -r /etc/rc.status ]; then
       
    90         rc_reset
       
    91         rc_status -v
       
    92         rc_exit
       
    93     else
       
    94         echo " running"
       
    95         exit 0
       
    96     fi
       
    97 }
       
    98 
       
    99 #------------------------------------------------------------------------------
       
   100 
       
   101 function exit_fail()
       
   102 {
       
   103     if [ -r /etc/rc.status ]; then
       
   104         rc_failed
       
   105         rc_status -v
       
   106         rc_exit
       
   107     else
       
   108         echo " failed"
       
   109         exit 1
       
   110     fi
       
   111 }
       
   112 
       
   113 #------------------------------------------------------------------------------
       
   114 
       
   115 function exit_dead()
       
   116 {
       
   117     if [ -r /etc/rc.status ]; then
       
   118         rc_failed
       
   119         rc_status -v
       
   120         rc_exit
       
   121     else
       
   122         echo " dead"
       
   123         exit 1
       
   124     fi
       
   125 }
       
   126 
       
   127 #------------------------------------------------------------------------------
       
   128 
       
   129 function parse_mac_address()
       
   130 {
       
   131     if [ -z "${1}" ]; then
       
   132         MAC=""
       
   133     elif echo ${1} | grep -qE '^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$'; then
       
   134         MAC=${1}
       
   135     else
       
   136         echo Invalid MAC address \"${1}\" in ${ETHERCAT_CONFIG}
       
   137         exit_fail
       
   138     fi 
       
   139 }
       
   140 
       
   141 #------------------------------------------------------------------------------
       
   142 
       
   143 if [ -r /etc/rc.status ]; then
       
   144     . /etc/rc.status
       
   145     rc_reset
       
   146 fi
       
   147 
       
   148 case "${1}" in
       
   149 
       
   150 start)
       
   151     echo -n "Starting EtherCAT master "
       
   152 
       
   153     # construct DEVICES and BACKUPS from configuration variables
       
   154     DEVICES=""
       
   155     BACKUPS=""
       
   156     MASTER_INDEX=0
       
   157     while true; do
       
   158         DEVICE=$(eval echo "\${MASTER${MASTER_INDEX}_DEVICE}")
       
   159         BACKUP=$(eval echo "\${MASTER${MASTER_INDEX}_BACKUP}")
       
   160         if [ -z "${DEVICE}" ]; then break; fi
       
   161 
       
   162         if [ ${MASTER_INDEX} -gt 0 ]; then
       
   163             DEVICES=${DEVICES},
       
   164             BACKUPS=${BACKUPS},
       
   165         fi
       
   166 
       
   167         parse_mac_address ${DEVICE}
       
   168         DEVICES=${DEVICES}${MAC}
       
   169         
       
   170         parse_mac_address ${BACKUP}
       
   171         BACKUPS=${BACKUPS}${MAC}
       
   172 
       
   173         MASTER_INDEX=$(expr ${MASTER_INDEX} + 1)
       
   174     done
       
   175 
       
   176     # load master module
       
   177     if ! ${MODPROBE} ${MODPROBE_FLAGS} ec_master \
       
   178             main_devices=${DEVICES} backup_devices=${BACKUPS}; then
       
   179         exit_fail
       
   180     fi
       
   181 
       
   182     # check for modules to replace
       
   183     for MODULE in ${DEVICE_MODULES}; do
       
   184         ECMODULE=ec_${MODULE}
       
   185         if ! ${MODINFO} ${ECMODULE} > /dev/null; then
       
   186             continue # ec_* module not found
       
   187         fi
       
   188         if lsmod | grep "^${MODULE} " > /dev/null; then
       
   189             if ! ${RMMOD} ${MODULE}; then
       
   190                 exit_fail
       
   191             fi
       
   192         fi
       
   193         if ! ${MODPROBE} ${MODPROBE_FLAGS} ${ECMODULE}; then
       
   194             ${MODPROBE} ${MODPROBE_FLAGS} ${MODULE} # try to restore module
       
   195             exit_fail
       
   196         fi
       
   197     done
       
   198 
       
   199     exit_success
       
   200     ;;
       
   201 
       
   202 stop)
       
   203     echo -n "Shutting down EtherCAT master "
       
   204 
       
   205     # unload EtherCAT device modules
       
   206     for MODULE in ${DEVICE_MODULES} master; do
       
   207         ECMODULE=ec_${MODULE}
       
   208         if ! lsmod | grep -q "^${ECMODULE} "; then
       
   209             continue # ec_* module not loaded
       
   210         fi
       
   211         if ! ${RMMOD} ${ECMODULE}; then
       
   212             exit_fail
       
   213         fi;
       
   214     done
       
   215 
       
   216     sleep 1
       
   217 
       
   218     # reload previous modules
       
   219     for MODULE in ${DEVICE_MODULES}; do
       
   220         if ! ${MODPROBE} ${MODPROBE_FLAGS} ${MODULE}; then
       
   221             echo Warning: Failed to restore ${MODULE}.
       
   222         fi
       
   223     done
       
   224 
       
   225     exit_success
       
   226     ;;
       
   227 
       
   228 restart)
       
   229     $0 stop || exit 1
       
   230     sleep 1
       
   231     $0 start
       
   232     ;;
       
   233 
       
   234 status)
       
   235     echo -n "Checking for EtherCAT "
       
   236 
       
   237     lsmod | grep -q "^ec_master "
       
   238     MASTERS_RUNNING=$?
       
   239 
       
   240     ! grep -q "(WAITING)" /sys/ethercat/master*/info
       
   241     MASTERS_IDLE=$?
       
   242 
       
   243     # master module loaded and masters not waiting for devices?
       
   244     if [ ${MASTERS_RUNNING} -eq 0 -a ${MASTERS_IDLE} -eq 0 ]; then
       
   245         exit_running
       
   246     else
       
   247         exit_dead
       
   248     fi
       
   249     ;;
       
   250 
       
   251 *)
       
   252     echo "USAGE: $0 {start|stop|restart|status}"
       
   253     ;;
       
   254 
       
   255 esac
       
   256 
       
   257 if [ -r /etc/rc.status ]; then
       
   258     rc_exit
       
   259 else
       
   260     exit 1
       
   261 fi
       
   262 
       
   263 #------------------------------------------------------------------------------