Blob Blame History Raw
From c45acb6e734f183d16af7f80cb62c1e26d679c52 Mon Sep 17 00:00:00 2001
From: Lennart Poettering <lennart@poettering.net>
Date: Wed, 20 Nov 2013 22:11:10 +0100
Subject: [PATCH] valgrind: make running PID 1 in valgrind useful

Since valgrind only generates useful output on exit() (rather than
exec()) we need to explicitly exit when valgrind is detected.

Conflicts:
	src/core/main.c
---
 configure.ac    |  2 +-
 src/core/main.c | 13 +++++++++++++
 2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/configure.ac b/configure.ac
index 9904e25..6232cf8 100644
--- a/configure.ac
+++ b/configure.ac
@@ -814,7 +814,7 @@ AC_DEFINE_UNQUOTED(TELINIT, ["$TELINIT"], [Path to telinit])
 
 AC_SUBST(TELINIT)
 
-AC_CHECK_HEADERS_ONCE([valgrind/memcheck.h])
+AC_CHECK_HEADERS_ONCE([valgrind/memcheck.h valgrind/valgrind.h])
 
 # ------------------------------------------------------------------------------
 have_myhostname=no
diff --git a/src/core/main.c b/src/core/main.c
index 00fd394..58c3a9e 100644
--- a/src/core/main.c
+++ b/src/core/main.c
@@ -34,6 +34,10 @@
 #include <sys/prctl.h>
 #include <sys/mount.h>
 
+#ifdef HAVE_VALGRIND_VALGRIND_H
+#include <valgrind/valgrind.h>
+#endif
+
 #include "manager.h"
 #include "log.h"
 #include "load-fragment.h"
@@ -1855,6 +1859,15 @@ finish:
         if (fds)
                 fdset_free(fds);
 
+#ifdef HAVE_VALGRIND_VALGRIND_H
+        /* If we are PID 1 and running under valgrind, then let's exit
+         * here explicitly. valgrind will only generate nice output on
+         * exit(), not on exec(), hence let's do the former not the
+         * latter here. */
+        if (getpid() == 1 && RUNNING_ON_VALGRIND)
+                return 0;
+#endif
+
         if (shutdown_verb) {
                 const char * command_line[] = {
                         SYSTEMD_SHUTDOWN_BINARY_PATH,