BASH   67
realtime
Guest on 11th July 2022 07:41:06 PM


  1. #! /bin/bash
  2. #
  3. # @configure_input@
  4. # on @DATE@
  5. #
  6.  
  7. export LANG=C
  8.  
  9. PIDOF=@PIDOF@
  10.  
  11. if ! type -path readlink > /dev/null 2>& 1; then
  12.     function readlink () {
  13.         perl -le 'print readlink(shift)' -- $1
  14.     }
  15. fi
  16.  
  17. RUN_IN_PLACE=@RUN_IN_PLACE@
  18. if [ $RUN_IN_PLACE = yes ]; then
  19.     # When using RIP make sure PATH includes the directory where rtapi_app
  20.     # resides
  21.     PATH=@EMC2_HOME@/bin:$PATH; export PATH
  22. fi
  23.  
  24. CheckConfig(){
  25.     prefix=@prefix@
  26.     exec_prefix=@exec_prefix@
  27.     sysconfdir=@sysconfdir@
  28.     if [ $RUN_IN_PLACE = yes ]; then
  29.         RTAPICONF=
  30.         # check in the emc2 scripts directory
  31.         # $0 is the command to run this script
  32.         # strip the script name to leave the path
  33.         SCRIPT_DIR=${0%/*}
  34.         # the path might be relative
  35.         # convert it to an absolute path
  36.         SCRIPT_DIR=$(cd $SCRIPT_DIR ; pwd -P)
  37.         # now look for rtapi.conf there
  38.         if [ -f $SCRIPT_DIR/rtapi.conf ] ; then
  39.             RTAPICONF=$SCRIPT_DIR/rtapi.conf
  40.         fi
  41.     else
  42.         if [ -f $sysconfdir/emc2/rtapi.conf ]; then
  43.                 RTAPICONF=$sysconfdir/emc2/rtapi.conf
  44.         fi
  45.     fi
  46.     if [ -z "$RTAPICONF" ] ; then
  47.         echo "Missing rtapi.conf.  Check your installation." 1>&2
  48.         exit 1
  49.     fi
  50.     INSMOD="@INSMOD@"
  51.     RMMOD="@RMMOD@"
  52.     FUSER="@FUSER@"
  53.  
  54.     # Import the config
  55.     source $RTAPICONF
  56.     if [ ! -e $RTLIB_DIR/.runinfo -a -e $RTLIB_DIR/emc2/.runinfo ]; then
  57.         # installed system: we don't want our modules mixed up with rtai
  58.         RTLIB_DIR=$RTLIB_DIR/emc2
  59.     fi
  60.     # Export the module path specified in the config.
  61.     export MODPATH
  62.     # Generate module lists for loading and unloading
  63.     # lists contain RTOS modules plus RTAPI and HAL
  64.     # unload list is in reverse order
  65.     # any module names that are symlinks are resolved to their real names
  66.     MODULES_LOAD=
  67.     MODULES_UNLOAD=
  68.     case $RTPREFIX in
  69.     sim) SHM_DEV=/dev/zero;;
  70.     *)
  71.         for  MOD in $MODULES ; do
  72.             eval MOD=\${MODPATH_$MOD}
  73.             if [ -z "$MOD" ]; then continue; fi
  74.             if [ -L $MOD ]; then
  75.                 MOD=${MOD%/*}/$(readlink $MOD)
  76.             fi
  77.             MODULES_LOAD="$MODULES_LOAD $MOD"
  78.             MOD="${MOD##*/}"
  79.             MOD="${MOD%$MODULE_EXT}"
  80.             MODULES_UNLOAD="$MOD $MODULES_UNLOAD"
  81.         done
  82.         MODULES_LOAD="$MODULES_LOAD $RTLIB_DIR/rtapi$MODULE_EXT $RTLIB_DIR/hal_lib$MODULE_EXT"
  83.         MODULES_UNLOAD="hal_lib rtapi $MODULES_UNLOAD"
  84.         case "$MODULES" in
  85.         *rtai_shm*)
  86.             SHM_DEV=/dev/rtai_shm
  87.         ;;
  88.         *mbuff*)
  89.             SHM_DEV=/dev/mbuff
  90.         ;;
  91.         esac
  92.     esac
  93. }
  94.  
  95. CheckStatus(){
  96.     case $RTPREFIX in
  97.     sim)
  98.         if [ -z "$($PIDOF rtapi_app)" ]; then
  99.             exit 1
  100.         else
  101.             exit 0
  102.         fi ;;
  103.     *)
  104.         # check loaded/unloaded status of modules
  105.         unset NOTLOADED
  106.         for MOD in $MODULES_UNLOAD ; do
  107.             if /sbin/lsmod | awk '{print $1}' | grep -x $MOD >/dev/null ; then
  108.                 echo "$MOD is loaded"
  109.             else
  110.                 echo "$MOD is not loaded"
  111.                 NOTLOADED=NOT
  112.             fi
  113.         done
  114.         if [ -z $NOTLOADED ]; then
  115.             exit 0
  116.         else
  117.             exit 1
  118.         fi
  119.     esac
  120. }
  121.  
  122. CheckMem(){
  123. # check for user space processes using shared memory
  124.     if [ -e /dev/mbuff ] ; then
  125.         # device file exists, check for processes using it
  126.         if $FUSER -s /dev/mbuff 2>/dev/null; then
  127.             # at least one process is using it
  128.             echo "ERROR:  Can't remove RTLinux modules, kill the following process(es) first"
  129.             $FUSER -v /dev/mbuff
  130.             exit 1
  131.         fi
  132.     elif [ -e /dev/rtai_shm ] ; then
  133.         # device file exists, check for processes using it
  134.         if $FUSER -s /dev/rtai_shm 2>/dev/null; then
  135.             # at least one process is using it
  136.             echo "ERROR:  Can't remove RTAI modules, kill the following process(es) first"
  137.             $FUSER -v /dev/rtai_shm
  138.             exit 1
  139.         fi
  140.     fi
  141. }
  142.  
  143. Load(){
  144.     case $RTPREFIX in
  145.     sim)
  146.     ;;
  147.     *)
  148.         for MOD in $MODULES_LOAD ; do
  149.             $INSMOD $MOD || return $?
  150.         done
  151.         if [ "$DEBUG" != "" ] && [ -w /proc/rtapi/debug ] ; then
  152.             echo "$DEBUG" > /proc/rtapi/debug
  153.         fi
  154.     esac
  155. }
  156.  
  157. CheckLoaded(){
  158.     # this abomination is needed because udev sometimes doesn't
  159.     # have the device ready for us in time.
  160.     n=0
  161.     while [ $n -lt 100 ]; do
  162.         [ -w $SHM_DEV ] && return 0
  163.         echo "." 1>&2
  164.         sleep .1
  165.         n=$(($n+1))
  166.     done
  167.     echo "Can't write to $SHM_DEV - aborting" 1>&2
  168.     exit 1
  169. }
  170.  
  171. Unload(){
  172.     case $RTPREFIX in
  173.     sim)
  174.         rtapi_app exit
  175.         ipcrm -M 0x48414c32 2>/dev/null
  176.         ;;
  177.     *)
  178.         for module in $MODULES_UNLOAD ; do
  179.             $RMMOD $module
  180.         done
  181.     esac
  182. }
  183.  
  184. CheckUnloaded(){
  185. # checks to see if all modules were unloaded
  186.     STATUS=
  187.     for module in $MODULES_UNLOAD ; do
  188.         # check to see if the module is installed
  189.         if /sbin/lsmod | awk '{print $1}' | grep -x $module >/dev/null ; then
  190.             echo "ERROR: Could not unload '$module'"
  191.             STATUS=error
  192.         fi
  193.     done
  194.     if [ -n "$STATUS" ] ; then
  195.         exit 1
  196.     fi
  197. }
  198.  
  199. CMD=$1
  200.  
  201. case "$CMD" in
  202.   start|load)
  203.         CheckConfig
  204.         Load || exit $?
  205.         CheckLoaded
  206.         ;;
  207.   restart|reload)
  208.         CheckConfig
  209.         CheckMem
  210.         Unload
  211.         CheckUnloaded
  212.         Load || exit $?
  213.         CheckLoaded
  214.         ;;
  215.   stop|unload)
  216.         CheckConfig
  217.         CheckMem
  218.         Unload || exit $?
  219.         ;;
  220.   status)
  221.         CheckConfig
  222.         CheckStatus
  223.         ;;
  224.   *)
  225.         echo "Usage: $0 {start|load|stop|unload|restart|reload|status}" >&2
  226.         exit 1
  227.         ;;
  228. esac
  229.  
  230. exit 0

Raw Paste

Login or Register to edit or fork this paste. It's free.