@@ -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