summaryrefslogtreecommitdiff
path: root/scripts/realtime.in
blob: 28f70ffabe31d979b624bbe344db3de3ebed9f28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#! /bin/bash
#
# @configure_input@ 
# on @DATE@
#

export LANG=C

PIDOF=@PIDOF@

CheckKernel() {
    case "@KERNEL_VERS@" in
    "") ;;
    *)
	if [ `uname -r` != "@KERNEL_VERS@" ]; then
	    cat 1>&2 << EOF
RTAPI requires the real-time kernel @KERNEL_VERS@ to run.  Before running
this realtime application, reboot and choose this kernel at the boot menu.
EOF
	    exit 1
	fi
    esac
}

RUN_IN_PLACE=@RUN_IN_PLACE@
if [ $RUN_IN_PLACE = yes ]; then
    # When using RIP make sure PATH includes the directory where rtapi_app
    # resides
    PATH=@EMC2_HOME@/bin:$PATH; export PATH
fi

CheckConfig(){
    prefix=@prefix@
    exec_prefix=@exec_prefix@
    sysconfdir=@sysconfdir@
    if [ $RUN_IN_PLACE = yes ]; then
        RTAPICONF=
        # check in the LinuxCNC scripts directory
        # $0 is the command to run this script
        # strip the script name to leave the path
        SCRIPT_DIR=${0%/*}
        # the path might be relative
        # convert it to an absolute path
        SCRIPT_DIR=$(cd $SCRIPT_DIR ; pwd -P)
        # now look for rtapi.conf there
        if [ -f $SCRIPT_DIR/rtapi.conf ] ; then
            RTAPICONF=$SCRIPT_DIR/rtapi.conf
        fi
    else
        if [ -f $sysconfdir/linuxcnc/rtapi.conf ]; then
                RTAPICONF=$sysconfdir/linuxcnc/rtapi.conf
        fi
    fi
    if [ -z "$RTAPICONF" ] ; then
	echo "Missing rtapi.conf.  Check your installation." 1>&2
	exit 1
    fi
    INSMOD="@INSMOD@"
    RMMOD="@RMMOD@"
    LSMOD="@LSMOD@"
    FUSER="@FUSER@"

    # Import the config
    source $RTAPICONF
    if [ ! -e $RTLIB_DIR/.runinfo -a -e $RTLIB_DIR/emc2/.runinfo ]; then
        # installed system: we don't want our modules mixed up with rtai
        RTLIB_DIR=$RTLIB_DIR/emc2
    fi
    # Export the module path specified in the config.
    export MODPATH
    # Generate module lists for loading and unloading
    # lists contain RTOS modules plus RTAPI and HAL
    # unload list is in reverse order
    # any module names that are symlinks are resolved to their real names
    MODULES_LOAD=
    MODULES_UNLOAD=
    case $RTPREFIX in
    sim) SHM_DEV=/dev/zero;;
    *)
        for  MOD in $MODULES ; do
            eval MOD=\${MODPATH_$MOD}
            if [ -z "$MOD" ]; then continue; fi
            if [ -L $MOD ]; then
                MOD=${MOD%/*}/$(readlink $MOD)
            fi
            MODULES_LOAD="$MODULES_LOAD $MOD"
            MOD="${MOD##*/}"
            MOD="${MOD%$MODULE_EXT}"
            MODULES_UNLOAD="$MOD $MODULES_UNLOAD"
        done
        MODULES_LOAD="$MODULES_LOAD $RTLIB_DIR/rtapi$MODULE_EXT $RTLIB_DIR/hal_lib$MODULE_EXT"
        MODULES_UNLOAD="hal_lib rtapi $MODULES_UNLOAD"
        case "$MODULES" in
        *rtai_shm*)
            SHM_DEV=/dev/rtai_shm
        ;;
        *mbuff*)
            SHM_DEV=/dev/mbuff
        ;;
        esac
    esac
}

CheckStatus(){
    case $RTPREFIX in
    sim)
        if [ -z "$($PIDOF rtapi_app)" ]; then
            exit 1
        else
            exit 0
        fi ;;
    *)
        # check loaded/unloaded status of modules
        unset NOTLOADED
        for MOD in $MODULES_UNLOAD ; do
            if @LSMOD@ | awk '{print $1}' | grep -x $MOD >/dev/null ; then
                echo "$MOD is loaded"
            else
                echo "$MOD is not loaded"
                NOTLOADED=NOT
            fi
        done
        if [ -z $NOTLOADED ]; then
            exit 0
        else
            exit 1
        fi
    esac
}

CheckMem(){
# check for user space processes using shared memory
    if [ -e /dev/mbuff ] ; then
	# device file exists, check for processes using it
	if $FUSER -s /dev/mbuff 2>/dev/null; then
	    # at least one process is using it
	    echo "ERROR:  Can't remove RTLinux modules, kill the following process(es) first"
	    $FUSER -v /dev/mbuff
	    exit 1
	fi
    elif [ -e /dev/rtai_shm ] ; then
	# device file exists, check for processes using it
	if $FUSER -s /dev/rtai_shm 2>/dev/null; then
	    # at least one process is using it
	    echo "ERROR:  Can't remove RTAI modules, kill the following process(es) first"
	    $FUSER -v /dev/rtai_shm
	    exit 1
	fi
    fi
}

Load(){
    CheckKernel
    case $RTPREFIX in
    sim)
    ;;
    *)
        for MOD in $MODULES_LOAD ; do
            $INSMOD $MOD || return $?
        done
        if [ "$DEBUG" != "" ] && [ -w /proc/rtapi/debug ] ; then
            echo "$DEBUG" > /proc/rtapi/debug
        fi
    esac
}

CheckLoaded(){
    # this abomination is needed because udev sometimes doesn't
    # have the device ready for us in time.
    n=0
    while [ $n -lt 100 ]; do
        [ -w $SHM_DEV ] && return 0
        echo "." 1>&2
        sleep .1
        n=$(($n+1))
    done
    echo "Can't write to $SHM_DEV - aborting" 1>&2
    exit 1
}

Unload(){
    CheckKernel
    case $RTPREFIX in
    sim)
        rtapi_app exit 
        ipcrm -M 0x48414c32 2>/dev/null ;# HAL_KEY
        ipcrm -M 0x90280A48 2>/dev/null ;# RTAPI_KEY
        ipcrm -M 0x48484c34 2>/dev/null ;# UUID_KEY
        ;;
    *)
        for module in $MODULES_UNLOAD ; do
            $RMMOD $module
        done
    esac
}

CheckUnloaded(){
# checks to see if all modules were unloaded
    STATUS=
    for module in $MODULES_UNLOAD ; do
	# check to see if the module is installed
	if @LSMOD@ | awk '{print $1}' | grep -x $module >/dev/null ; then
	    echo "ERROR: Could not unload '$module'"
	    STATUS=error
	fi
    done
    if [ -n "$STATUS" ] ; then
	exit 1
    fi
}

CMD=$1

case "$CMD" in
  start|load)
	CheckConfig
	Load || exit $?
	CheckLoaded
	;;
  restart|force-reload)
	CheckConfig
	CheckMem
	Unload
	CheckUnloaded
	Load || exit $?
	CheckLoaded
	;;
  stop|unload)
	CheckConfig
	CheckMem
	Unload || exit $?
	;;
  status)
	CheckConfig
	CheckStatus
	;;
  *)
	echo "Usage: $0 {start|load|stop|unload|restart|force-reload|status}" >&2
	exit 1
	;;
esac

exit 0