5b63504
commit 699a741b488010d56cc358a5f7b4d8a8f4886347
5b63504
Author: Mark Wielaard <mark@klomp.org>
5b63504
Date:   Sat Dec 23 23:16:24 2017 +0100
5b63504
5b63504
    tests: Try to use coredumpctl to extract core files.
5b63504
    
5b63504
    If systemd-coredump is installed we have to use coredumpctl to extract
5b63504
    the core file to test. Unfortunately systemd-coredump/coredumpctl seem
5b63504
    to be somewhat fragile if multiple core dumps are generated/extracted
5b63504
    at the same time. So use a lock file to only run one core dump test at
5b63504
    a time (under make -j).
5b63504
    
5b63504
    Signed-off-by: Mark Wielaard <mark@klomp.org>
5b63504
5b63504
diff --git a/tests/Makefile.am b/tests/Makefile.am
5b63504
index fca0072..64cb5bd 100644
5b63504
--- a/tests/Makefile.am
5b63504
+++ b/tests/Makefile.am
5b63504
@@ -515,6 +515,9 @@ dwarf_default_lower_bound_LDADD = $(libdw)
5b63504
 system_elf_libelf_test_CPPFLAGS =
5b63504
 system_elf_libelf_test_LDADD = $(libelf)
5b63504
 
5b63504
+# A lock file used to make sure only one test dumps core at a time
5b63504
+CLEANFILES += core-dump-backtrace.lock
5b63504
+
5b63504
 if GCOV
5b63504
 check: check-am coverage
5b63504
 .PHONY: coverage
5b63504
diff --git a/tests/backtrace-subr.sh b/tests/backtrace-subr.sh
5b63504
index c1f3156..e04a7ea 100644
5b63504
--- a/tests/backtrace-subr.sh
5b63504
+++ b/tests/backtrace-subr.sh
5b63504
@@ -137,19 +137,46 @@ check_native()
5b63504
 # Backtrace core file.
5b63504
 check_native_core()
5b63504
 {
5b63504
+# systemd-coredump/coredumpctl doesn't seem to like concurrent core dumps
5b63504
+# use a lock file (fd 200) tests/core-dump-backtrace.lock
5b63504
+(
5b63504
   child=$1
5b63504
 
5b63504
   # Disable valgrind while dumping core.
5b63504
   SAVED_VALGRIND_CMD="$VALGRIND_CMD"
5b63504
   unset VALGRIND_CMD
5b63504
 
5b63504
+  # Wait for lock for 10 seconds or skip.
5b63504
+  flock -x -w 10 200 || exit 77;
5b63504
+
5b63504
   # Skip the test if we cannot adjust core ulimit.
5b63504
-  core="core.`ulimit -c unlimited || exit 77; set +ex; testrun ${abs_builddir}/$child --gencore; true`"
5b63504
+  pid="`ulimit -c unlimited || exit 77; set +ex; testrun ${abs_builddir}/$child --gencore; true`"
5b63504
+  core="core.$pid"
5b63504
   # see if /proc/sys/kernel/core_uses_pid is set to 0
5b63504
   if [ -f core ]; then
5b63504
     mv core "$core"
5b63504
   fi
5b63504
-  if [ ! -f "$core" ]; then echo "No $core file generated"; exit 77; fi
5b63504
+  type -P coredumpctl && have_coredumpctl=1 || have_coredumpctl=0
5b63504
+  if [ ! -f "$core" -a $have_coredumpctl -eq 1 ]; then
5b63504
+    # Maybe systemd-coredump took it. But give it some time to dump first...
5b63504
+    sleep 1
5b63504
+    coredumpctl --output="$core" dump $pid || rm -f $core
5b63504
+
5b63504
+    # Try a couple of times after waiting some more if something went wrong...
5b63504
+    if [ ! -f "$core" ]; then
5b63504
+      sleep 2
5b63504
+      coredumpctl --output="$core" dump $pid || rm -f $core
5b63504
+    fi
5b63504
+
5b63504
+    if [ ! -f "$core" ]; then
5b63504
+      sleep 3
5b63504
+      coredumpctl --output="$core" dump $pid || rm -f $core
5b63504
+    fi
5b63504
+  fi
5b63504
+  if [ ! -f "$core" ]; then
5b63504
+    echo "No $core file generated";
5b63504
+    exit 77;
5b63504
+  fi
5b63504
 
5b63504
   if [ "x$SAVED_VALGRIND_CMD" != "x" ]; then
5b63504
     VALGRIND_CMD="$SAVED_VALGRIND_CMD"
5b63504
@@ -163,4 +190,6 @@ check_native_core()
5b63504
   cat $core.{bt,err}
5b63504
   check_native_unsupported $core.err $child-$core
5b63504
   check_all $core.{bt,err} $child-$core
5b63504
+  rm $core{,.{bt,err}}
5b63504
+) 200>${abs_builddir}/core-dump-backtrace.lock
5b63504
 }
5b63504
5b63504
commit 61e33d72788c58467668b2f2ad44d5b95ebbee80
5b63504
Author: Mark Wielaard <mark@klomp.org>
5b63504
Date:   Fri Feb 16 20:34:25 2018 +0100
5b63504
5b63504
    tests: Accept any core if no core with the "correct" pid can be found.
5b63504
    
5b63504
    In some containers our view of pids is confused. We see the container
5b63504
    pid namespace, but the core is generated using the host pid namespace.
5b63504
    Since tests are run in a new fresh directory any core here is most like
5b63504
    is ours.
5b63504
    
5b63504
    Signed-off-by: Mark Wielaard <mark@klomp.org>
5b63504
5b63504
diff --git a/tests/backtrace-subr.sh b/tests/backtrace-subr.sh
5b63504
index e04a7ea..ff42c6f 100644
5b63504
--- a/tests/backtrace-subr.sh
5b63504
+++ b/tests/backtrace-subr.sh
5b63504
@@ -174,6 +174,13 @@ check_native_core()
5b63504
     fi
5b63504
   fi
5b63504
   if [ ! -f "$core" ]; then
5b63504
+    # In some containers our view of pids is confused. Since tests are
5b63504
+    # run in a new fresh directory any core here is most like is ours.
5b63504
+    if ls core.[0-9]* 1> /dev/null 2>&1; then
5b63504
+      mv core.[0-9]* "$core"
5b63504
+    fi
5b63504
+  fi
5b63504
+  if [ ! -f "$core" ]; then
5b63504
     echo "No $core file generated";
5b63504
     exit 77;
5b63504
   fi