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