Skip to content

Commit c3a97fc

Browse files
authored
Merge pull request #382 from Tarsnap/libcperciva-import
Libcperciva import
2 parents d503204 + e2d35cc commit c3a97fc

2 files changed

Lines changed: 90 additions & 36 deletions

File tree

tests/shared_test_functions.sh

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
# Look for ${cmd} in the ${PATH}, and ensure that it supports ${args}.
2323
# - has_pid(cmd):
2424
# Look for a ${cmd} in $(ps).
25-
# - wait_while(func):
26-
# Wait until ${func} returns non-zero.
25+
# - wait_while(timeout, func):
26+
# Wait up to ${timeout} milliseconds, or until ${func} returns non-zero.
2727
# - setup_check(description, check_prev):
2828
# Set up the below variables.
2929
# - expected_exitcode(expected, actual):
@@ -133,11 +133,14 @@ has_pid() {
133133
return 1
134134
}
135135

136-
## wait_while(func):
136+
## wait_while(timeout, func):
137137
# Wait while ${func} returns 0. If ${msleep} is defined, use that to wait
138-
# 100ms; otherwise, wait in 1 second increments.
138+
# 100ms; otherwise, wait in 1 second increments. If ${timeout} is non-zero,
139+
# return 1 if ${timeout} milliseconds have passed.
139140
wait_while() {
140141
_wait_while_ms=0
142+
_wait_while_timeout=$1
143+
shift 1
141144

142145
# Check for the ending condition
143146
while "$@"; do
@@ -147,8 +150,17 @@ wait_while() {
147150
"${_wait_while_ms}" "$*" 1>&2
148151
fi
149152

153+
# Bail if we've exceeded the timeout
154+
if [ "${_wait_while_timeout}" -gt 0 ] && \
155+
[ "${_wait_while_ms}" -gt "${_wait_while_timeout}" ]; then
156+
if [ "${VERBOSE}" -ne 0 ]; then
157+
printf "Bail; timeout exceeded\n" 1>&2
158+
fi
159+
return 1
160+
fi
161+
150162
# Wait using the appropriate binary
151-
if [ -n "${msleep:-}" ]; then
163+
if [ -n "${msleep:-}" ]; then
152164
"${msleep}" 100
153165
_wait_while_ms=$((_wait_while_ms + 100))
154166
else

tests/shared_valgrind_functions.sh

Lines changed: 73 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,44 @@ _val_checkl() {
344344
fi
345345
}
346346

347+
## _get_pids (logfiles):
348+
# Extract a list of pids in the format %08d from ${logfiles}.
349+
_get_pids() {
350+
_get_pids_logfiles=$1
351+
352+
_get_pids_pids=""
353+
for _get_pids_logfile in ${_valgrind_check_logfiles} ; do
354+
# Get the pid.
355+
_get_pids_pid=$(printf "%s" "${_get_pids_logfile%%.log}" | \
356+
rev | cut -d "-" -f 1 | rev)
357+
# Zero-pad it and add it to the new list.
358+
_get_pids_pids=$(printf "%s %08d" \
359+
"${_get_pids_pids}" "${_get_pids_pid}")
360+
done
361+
362+
echo "${_get_pids_pids}"
363+
}
364+
365+
## _is_parent (logfile, pids):
366+
# If the parent pid of ${logfile} is in ${pids}, return 0; otherwise, return 1.
367+
_is_parent () {
368+
_is_parent_logfile=$1
369+
_is_parent_pids=$2
370+
371+
# Get the parent pid from the valgrind logfile
372+
ppid=$(grep "Parent PID:" "${_is_parent_logfile}" | \
373+
awk '{ print $4 }')
374+
ppid=$(printf "%08d" "${ppid}")
375+
376+
# If the parent is in the list of pids, this isn't the parent process.
377+
if [ "${_is_parent_pids#*"${ppid}"}" != "${_is_parent_pids}" ] ; then
378+
return 1
379+
fi
380+
381+
# Yes, this is the parent process.
382+
return 0
383+
}
384+
347385
## valgrind_check (exitfile):
348386
# Check for any memory leaks recorded in valgrind logfiles associated with a
349387
# test exitfile. Return the filename if there's a leak; otherwise return an
@@ -368,41 +406,45 @@ valgrind_check() {
368406
return
369407
fi
370408

409+
# Get a normalized list of pids.
410+
_valgrind_check_pids=$(_get_pids "${_valgrind_check_logfiles}")
411+
371412
# If the valgrind logfiles contain "-valgrind-parent-", then we only
372-
# want to check the parent (the lowest pid).
373-
for _valgrind_check_logfile in ${_valgrind_check_logfiles} ; do
374-
if [ "${_valgrind_check_logfile#*-valgrind-parent-}" != "${_valgrind_check_logfile}" ]; then
375-
# Only check the parent
376-
_val_checkl "${_valgrind_check_logfile}"
377-
return "$?"
378-
fi
379-
done
413+
# want to check the parent. The parent is the logfile whose "parent
414+
# pid" is not in the list of pids. (If one logfile contains
415+
# "-valgrind-parent-" then all of them should have it, so we can
416+
# simply check if that string occurs in the list of logfiles.)
417+
if [ "${_valgrind_check_logfiles#*-valgrind-parent-}" != \
418+
"${_valgrind_check_logfiles}" ]; then
419+
_valgrind_check_parent=1
420+
else
421+
_valgrind_check_parent=0
422+
fi
380423

381-
# If there's two files, there's a fork() -- likely within
382-
# daemonize() -- so only pay attention to the child.
383-
if [ "${_valgrind_check_num}" -eq "2" ]; then
384-
# Find both pids.
385-
_valgrind_check_val_pids=""
386-
for _valgrind_check_logfile in ${_valgrind_check_logfiles} ; do
387-
_valgrind_check_val_pid=$(head -n 1 "${_valgrind_check_logfile}" | cut -d "=" -f 3)
388-
_valgrind_check_val_pids="${_valgrind_check_val_pids} ${_valgrind_check_val_pid}"
389-
done
390-
391-
# Find the logfile which has a parent in the list of pids.
392-
for _valgrind_check_logfile in ${_valgrind_check_logfiles} ; do
393-
_valgrind_check_val_parent_pid=$(grep "Parent PID:" "${_valgrind_check_logfile}" | \
394-
awk '{ print $4 }')
395-
if [ "${_valgrind_check_val_pids#*"${_valgrind_check_val_parent_pid}"}" != \
396-
"${_valgrind_check_val_pids}" ]; then
424+
# Check the logfiles depending on whether it's the parent or not,
425+
# and whether we want to check the parent or children.
426+
for _valgrind_check_logfile in ${_valgrind_check_logfiles} ; do
427+
if _is_parent "${_valgrind_check_logfile}" \
428+
"${_valgrind_check_pids}" ; then
429+
# This is the parent.
430+
if [ "${_valgrind_check_parent}" -eq 1 ] ; then
397431
_val_checkl "${_valgrind_check_logfile}"
398-
return "$?"
432+
# Bail if there's a problem.
433+
if [ "$?" -ne 0 ]; then
434+
return
435+
fi
399436
fi
400-
done
401-
fi
402-
403-
# Programmer error; hard bail.
404-
echo "Programmer error: wrong number of valgrind logfiles!" 1>&2
405-
exit 1
437+
else
438+
# This is a child.
439+
if [ "${_valgrind_check_parent}" -eq 0 ] ; then
440+
_val_checkl "${_valgrind_check_logfile}"
441+
# Bail if there's a problem.
442+
if [ "$?" -ne 0 ]; then
443+
return
444+
fi
445+
fi
446+
fi
447+
done
406448
}
407449

408450
## valgrind_init():

0 commit comments

Comments
 (0)