Skip to content

Commit 8979fc6

Browse files
authored
Merge pull request #3308 from BsAtHome/fix_runtests-shmem
Update runtests to test for shared memory segments
2 parents ad5ba69 + 3ed2edf commit 8979fc6

1 file changed

Lines changed: 146 additions & 86 deletions

File tree

scripts/runtests.in

Lines changed: 146 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,13 @@ unset DISPLAY
1313
export LC_ALL=C
1414
export LANGUAGES=
1515

16-
17-
case "$0" in
18-
*/*) MYDIR="${0%/*}" ;;
19-
*) MYDIR="`type -path $0`"; MYDIR="${MYDIR%/*}"
20-
esac
21-
MYDIR=$(cd $MYDIR; pwd);
22-
TOPDIR=$(cd $MYDIR/..; pwd)
16+
TOPDIR="$(realpath "$(dirname "$0")/..")"
2317

2418
prefix=@prefix@
25-
if test @RUN_IN_PLACE@ = yes; then
26-
. $TOPDIR/scripts/rip-environment >&/dev/null
19+
# Shellcheck does not know about substitutions
20+
# shellcheck disable=SC2050
21+
if [ "@RUN_IN_PLACE@" = yes ]; then
22+
. "$TOPDIR/scripts/rip-environment" >&/dev/null
2723
export HEADERS=@EMC2_HOME@/include
2824
export LIBDIR=${TOPDIR}/lib
2925
export REALTIME=realtime
@@ -42,136 +38,193 @@ export PYTHON_EXTRA_LIBS="@PYTHON_EXTRA_LIBS@"
4238
export PYTHON_EXTRA_LDFLAGS="@PYTHON_EXTRA_LDFLAGS@"
4339
export PYTHON_LIBS="@PYTHON_EXTRA_LIBS@"
4440

45-
export RUNTESTS="$(readlink -f $0)"
41+
RUNTESTS="$(readlink -f "$0")"
42+
export RUNTESTS
4643

4744
NUM=0
4845
FAIL=0; FAIL_NAMES=""
4946
XFAIL=0
47+
SHMERR=0
5048
SKIP=0
5149
VERBOSE=0
5250

5351
clean () {
54-
find $* \( -name "stderr" -or -name "result" \
52+
find "$*" \( -name "stderr" -or -name "result" \
5553
-or -name "*.var" -or -name "*.var.bak" \) \
5654
-print0 | xargs -0 rm -f
5755
}
5856

5957
run_shell_script () {
60-
testname=$(basename $1)
61-
testdir=$(dirname $1)
58+
testname=$(basename "$1")
59+
testdir=$(dirname "$1")
6260

63-
pushd $testdir > /dev/null
61+
pushd "$testdir" > /dev/null || exit 2
6462
if [ $VERBOSE -eq 1 ]; then
65-
(bash -x $testname | tee result) 3>&1 1>&2 2>&3 | tee stderr
63+
(bash -x "$testname" | tee result) 3>&1 1>&2 2>&3 | tee stderr
6664
else
67-
bash -x $testname > result 2> stderr
65+
bash -x "$testname" > result 2> stderr
6866
fi
6967
exitcode=$?
70-
popd > /dev/null
71-
return $exitcode
68+
popd > /dev/null || exit 2
69+
return "$exitcode"
7270
}
7371

7472
run_executable () {
75-
testname=$(basename $1)
76-
testdir=$(dirname $1)
73+
testname=$(basename "$1")
74+
testdir=$(dirname "$1")
7775

78-
pushd $testdir > /dev/null
76+
pushd "$testdir" > /dev/null || exit 2
7977
if [ $VERBOSE -eq 1 ]; then
80-
(./$testname | tee result) 3>&1 1>&2 2>&3 | tee stderr
78+
(./"$testname" | tee result) 3>&1 1>&2 2>&3 | tee stderr
8179
else
82-
./$testname > result 2> stderr
80+
./"$testname" > result 2> stderr
8381
fi
8482
exitcode=$?
85-
popd > /dev/null
86-
return $exitcode
83+
popd > /dev/null || exit 2
84+
return "$exitcode"
8785
}
8886

8987
run_without_overruns () {
90-
testname=$(basename $1)
91-
testdir=$(dirname $1)
88+
testname=$(basename "$1")
89+
testdir=$(dirname "$1")
9290
for i in $(seq 10); do
93-
if [ $i != 1 ]; then echo "--- $testdir: overrun detected in sampler, re-running test" 1>&2 ; fi
91+
if [ "$i" != 1 ]; then echo "--- $testdir: overrun detected in sampler, re-running test" 1>&2 ; fi
9492

95-
pushd $testdir > /dev/null
93+
pushd "$testdir" > /dev/null || exit 2
9694
if [ $VERBOSE -eq 1 ]; then
97-
(halrun -f $testname | tee result) 3>&1 1>&2 2>&3 | tee stderr
95+
(halrun -f "$testname" | tee result) 3>&1 1>&2 2>&3 | tee stderr
9896
else
99-
halrun -f $testname > result 2> stderr
97+
halrun -f "$testname" > result 2> stderr
10098
fi
10199
exitcode=$?
102-
popd > /dev/null
100+
popd > /dev/null || exit 2
103101

104-
if ! grep -q '^overrun$' $testdir/result; then return $exitcode; fi
102+
if ! grep -q '^overrun$' "$testdir/result"; then return "$exitcode"; fi
105103
done
106104
echo "--- $testdir: $i overruns detected, giving up" 1>&2
107105
return 1
108106
}
109107

110108
run_test() {
111-
testname=$1
112-
case $testname in
113-
*.hal) run_without_overruns $testname ;;
114-
*.sh) run_shell_script $testname ;;
115-
*) run_executable $testname ;;
109+
testname="$1"
110+
case "$testname" in
111+
*.hal) run_without_overruns "$testname" ;;
112+
*.sh) run_shell_script "$testname" ;;
113+
*) run_executable "$testname" ;;
116114
esac
117115
}
118116

119-
TMPDIR=`mktemp -d /tmp/runtest.XXXXXX`
120-
trap "rm -rf $TMPDIR" 0 1 2 3 9 15
117+
SHMEM_KEY=( "0x00000064" "0x48414c32" "0x130cf406" "0x434c522b" )
118+
SHMEM_USE=( "Emc motion" "Hal" "Hal scope" "Classicladder" )
119+
test_shmem() {
120+
# Test if there are any shared memory segments left. These will
121+
# interfere with performing tests as we cannot be assured of a clean
122+
# start for each test if a segment exists.
123+
mapfile -t keys < <(ipcs -m | grep -Ei "^\\s*($(IFS='|'; echo "${SHMEM_KEY[*]}"))" | awk '{print $1}')
124+
if [ "${#keys[@]}" -gt 0 ]; then
125+
echo "There are one or more shared memory segments currently allocated."
126+
echo "This indicates that there is a LinuxCNC instance running or it"
127+
echo "did not cleanup before exit."
128+
echo "You should run 'ipcs -m' and look for the following keys to cleanup:"
129+
for i in "${!SHMEM_KEY[@]}"; do
130+
for j in "${keys[@]}"; do
131+
if [ "$j" = "${SHMEM_KEY[$i]}" ]; then
132+
echo "${SHMEM_KEY[$i]} - ${SHMEM_USE[$i]} key"
133+
break
134+
fi
135+
done
136+
done
137+
echo
138+
echo "You should remove the key(s) with 'ipcrm -M <key>' if LinuxCNC is"
139+
echo "not running and no processes are attached to the segment(s)."
140+
return 1
141+
else
142+
return 0
143+
fi
144+
}
145+
146+
test_and_remove_shmem() {
147+
ret=0
148+
for i in "${!SHMEM_KEY[@]}"; do
149+
# ipcs returns the following columns:
150+
# key shmid owner perms bytes nattch status
151+
read -r -a SHM < <(ipcs -m | grep -Ei "^\\s*${SHMEM_KEY[$i]}")
152+
if [ "${#SHM[@]}" -ge 6 ]; then
153+
echo "*** SHMERR: Shared memory segment ${SHMEM_KEY[$i]} (${SHMEM_USE[$i]} key) was not removed. Removing..."
154+
ipcrm -M "${SHMEM_KEY[$i]}"
155+
# Check number of attached processes. It should be zero.
156+
if [ "${SHM[5]}" -ne 0 ]; then
157+
echo "*** SHMERR: Shared memory segment ${SHMEM_KEY[$i]} has at least one attached process."
158+
echo "*** SHMERR: Manual intervention is required to solve the situation."
159+
return 2
160+
fi
161+
ret=1
162+
fi
163+
done
164+
return $ret
165+
}
166+
167+
TMPDIR=$(mktemp -d /tmp/runtest.XXXXXX)
168+
trap 'rm -rf "$TMPDIR"' 0 1 2 3 15
121169

122170

123171
run_tests () {
124-
find $* -name test.hal -or -name test.sh -or -name test \
125-
| sort > $TMPDIR/alltests
172+
if ! test_shmem; then
173+
exit 1;
174+
fi
175+
176+
find "$*" -name test.hal -or -name test.sh -or -name test \
177+
| sort > "$TMPDIR/alltests"
126178

127-
while read testname; do
128-
testdir=$(dirname $testname)
129-
if [ -e $testdir/skip ]; then
130-
if ! [ -x $testdir/skip ] || ! $testdir/skip; then
179+
while read -r testname; do
180+
testdir=$(dirname "$testname")
181+
if [ -e "$testdir/skip" ]; then
182+
if ! [ -x "$testdir/skip" ] || ! "$testdir/skip"; then
131183
echo "Skipping disabled test: $testdir" 1>&2
132-
SKIP=$(($SKIP+1))
184+
SKIP=$((SKIP + 1))
133185
continue
134186
fi
135187
fi
136-
if $NOSUDO && [ -e $testdir/control ] && \
137-
grep Restrictions: $testdir/control | grep -q sudo; then
138-
if ! [ -x $testdir/skip ] || ! $testdir/skip; then
188+
if $NOSUDO && [ -e "$testdir/control" ] && \
189+
grep Restrictions: "$testdir/control" | grep -q sudo; then
190+
if ! [ -x "$testdir/skip" ] || ! "$testdir/skip"; then
139191
echo "Skipping sudo test: $testdir" 1>&2
140-
SKIP=$(($SKIP+1))
192+
SKIP=$((SKIP + 1))
141193
continue
142194
fi
143195
fi
144-
NUM=$(($NUM+1))
145-
export TEST_DIR=$(readlink -f $testdir)
196+
NUM=$((NUM + 1))
197+
TEST_DIR=$(readlink -f "$testdir")
198+
export TEST_DIR
146199
echo "Running test: $testdir" 1>&2
147200
if test -n "$SYSTEM_BUILD"; then
148201
# Tell `halcompile` where to install comps
149-
USER_MODULE_DIR=$(readlink -f $testdir) \
150-
PATH=$(readlink -f $testdir):$PATH \
151-
run_test $testname
202+
USER_MODULE_DIR=$(readlink -f "$testdir") \
203+
PATH=$(readlink -f "$testdir"):$PATH \
204+
run_test "$testname"
152205
else
153-
run_test $testname
206+
run_test "$testname"
154207
fi
155208
exitcode=$?
156-
if [ $exitcode -ne 0 ]; then
209+
if [ "$exitcode" -ne 0 ]; then
157210
reason="test run exited with $exitcode"
158211
else
159-
if [ -e $testdir/checkresult ]; then
160-
$testdir/checkresult $testdir/result
212+
if [ -e "$testdir/checkresult" ]; then
213+
"$testdir/checkresult" "$testdir/result"
161214
exitcode=$?
162215
reason="checkresult exited with $exitcode"
163-
elif [ -f $testdir/expected ]; then
164-
cmp -s $testdir/expected $testdir/result
216+
elif [ -f "$testdir/expected" ]; then
217+
cmp -s "$testdir/expected" "$testdir/result"
165218
exitcode=$?
166219
reason="result differed from expected"
167-
if [ $exitcode -ne 0 ]; then
168-
diff -u $testdir/expected $testdir/result > $TMPDIR/diff
169-
SIZE=$(wc -l < $TMPDIR/diff)
170-
if [ $SIZE -lt 40 ]; then
171-
cat $TMPDIR/diff
220+
if [ "$exitcode" -ne 0 ]; then
221+
diff -u "$testdir/expected" "$testdir/result" > "$TMPDIR/diff"
222+
SIZE=$(wc -l < "$TMPDIR/diff")
223+
if [ "$SIZE" -lt 40 ]; then
224+
cat "$TMPDIR/diff"
172225
else
173226
OMIT=$((SIZE-40))
174-
head -40 $TMPDIR/diff
227+
head -40 "$TMPDIR/diff"
175228
echo "($OMIT more lines omitted)"
176229
fi
177230
fi
@@ -180,44 +233,51 @@ run_tests () {
180233
reason="Neither expected nor checkresult existed"
181234
fi
182235
fi
183-
if [ $exitcode -ne 0 ]; then
236+
if [ "$exitcode" -ne 0 ]; then
184237
echo "*** $testdir: XFAIL: $reason"
185238
if test $PRINT = 1; then
186239
echo "************** result:"
187-
tail -500 $testdir/result | sed 's/^/ /'
240+
tail -500 "$testdir/result" | sed 's/^/ /'
188241
echo "************** stderr:"
189-
tail -500 $testdir/stderr | sed 's/^/ /'
242+
tail -500 "$testdir/stderr" | sed 's/^/ /'
190243
echo "**************"
191244
fi
192-
if [ -f $testdir/xfail ]; then
193-
XFAIL=$(($XFAIL+1))
245+
if [ -f "$testdir/xfail" ]; then
246+
XFAIL=$((XFAIL + 1))
194247
if [ $NOCLEAN -eq 0 ]; then
195-
rm -f $testdir/stderr $testdir/result \
196-
$testdir/*.var $testdir/*.var.bak
248+
rm -f "$testdir/stderr" "$testdir/result" \
249+
"$testdir"/*.var "$testdir"/*.var.bak
197250
fi
198251
else
199-
FAIL=$(($FAIL+1))
200-
FAIL_NAMES="$FAIL_NAMES
201-
$testdir"
252+
FAIL=$((FAIL + 1))
253+
FAIL_NAMES="$FAIL_NAMES"$'\n'"$testdir"
202254
fi
203255
if test $STOP = 1; then
204256
break
205257
fi
206258
else
207-
if [ -f $testdir/xfail ]; then
259+
if [ -f "$testdir/xfail" ]; then
208260
echo "*** $testdir: XPASS: Passed, but was expected to fail"
209261
else
210262
if [ $NOCLEAN -eq 0 ]; then
211-
rm -f $testdir/stderr $testdir/result \
212-
$testdir/*.var $testdir/*.var.bak
263+
rm -f "$testdir/stderr" "$testdir/result" \
264+
"$testdir"/*.var "$testdir"/*.var.bak
213265
fi
214266
fi
215267
fi
216-
done < $TMPDIR/alltests
268+
269+
if ! test_and_remove_shmem; then
270+
if [ $? -eq 2 ]; then
271+
# Cannot remove attached segments. Fail hard.
272+
exit 1
273+
fi
274+
SHMERR=$((SHMERR + 1))
275+
fi
276+
done < "$TMPDIR/alltests"
217277

218278
SUCC=$((NUM-FAIL-XFAIL))
219-
echo "Runtest: $NUM tests run, $SUCC successful, $FAIL failed + $XFAIL expected, $SKIP skipped"
220-
if [ $FAIL -ne 0 ]; then
279+
echo "Runtest: $NUM tests run, $SUCC successful, $FAIL failed + $XFAIL expected, $SKIP skipped, $SHMERR shmem errors"
280+
if [ "$FAIL" -ne 0 ]; then
221281
echo "Failed: $FAIL_NAMES"
222282
exit 1;
223283
else
@@ -268,10 +328,10 @@ done
268328
shift $((OPTIND-1))
269329

270330
if [ $# -eq 0 ]; then
271-
if [ -f test.hal -o -f test.sh ]; then
331+
if [ -f test.hal ] || [ -f test.sh ]; then
272332
set -- .
273333
else
274-
set -- $TOPDIR/tests
334+
set -- "$TOPDIR/tests"
275335
fi
276336
fi
277337

0 commit comments

Comments
 (0)