]> git.ipfire.org Git - thirdparty/lldpd.git/commitdiff
When running on valgrind, don't chroot or drop privileges
authorVincent Bernat <bernat@luffy.cx>
Mon, 30 Apr 2012 23:50:03 +0000 (01:50 +0200)
committerVincent Bernat <bernat@luffy.cx>
Tue, 1 May 2012 00:38:30 +0000 (02:38 +0200)
Valgrind documentation says:

  You are encouraged to copy the valgrind/*.h headers into your
  project's include directory, so your program doesn't have a
  compile-time dependency on Valgrind being installed. The Valgrind
  headers, unlike most of the rest of the code, are under a BSD-style
  license so you may include them without worrying about license
  incompatibility.

I don't like copying headers around. Therefore, there is an autoconf
check for valgrind headers.

configure.ac
src/lldpd.h
src/priv.c

index 32ca90b4b2efff0d4add255f54a5a9d5be45ebfc..3c85065b99bc6e0ef850326bce40781b7ec099c9 100644 (file)
@@ -102,6 +102,7 @@ AC_CHECK_HEADERS([ \
 @%:@include <arpa/inet.h>
 @%:@include <linux/if.h>
 ])
+AC_CHECK_HEADERS([valgrind/valgrind.h])
 
 AC_CACHE_SAVE
 
index 97c8531b760be438e44a0aaab3ca4ad65d327c3d..0ecf4e936d83dc1a3c3fd14330293d5141c3e115 100644 (file)
 #  include <config.h>
 #endif
 
+#ifdef HAVE_VALGRIND_VALGRIND_H
+# include <valgrind/valgrind.h>
+#else
+# define RUNNING_ON_VALGRIND 0
+#endif
+
 #define _GNU_SOURCE 1
 #include <stdlib.h>
 #include <stddef.h>
index 217cac9e3d392c38caa2d604a517372cb4c4606f..9e57532f622c21eb0a1176a5c7838e54210da30c 100644 (file)
@@ -531,17 +531,21 @@ priv_init(char *chrootdir)
        switch (monitored) {
        case 0:
                /* We are in the children, drop privileges */
-               if (chroot(chrootdir) == -1)
-                       fatal("[priv]: unable to chroot");
-               if (chdir("/") != 0)
-                       fatal("[priv]: unable to chdir");
-               gidset[0] = gid;
-               if (setresgid(gid, gid, gid) == -1)
-                       fatal("[priv]: setresgid() failed");
-               if (setgroups(1, gidset) == -1)
-                       fatal("[priv]: setgroups() failed");
-               if (setresuid(uid, uid, uid) == -1)
-                       fatal("[priv]: setresuid() failed");
+               if (RUNNING_ON_VALGRIND)
+                       LLOG_WARNX("[priv]: running on valgrind, keep privileges");
+               else {
+                       if (chroot(chrootdir) == -1)
+                               fatal("[priv]: unable to chroot");
+                       if (chdir("/") != 0)
+                               fatal("[priv]: unable to chdir");
+                       gidset[0] = gid;
+                       if (setresgid(gid, gid, gid) == -1)
+                               fatal("[priv]: setresgid() failed");
+                       if (setgroups(1, gidset) == -1)
+                               fatal("[priv]: setgroups() failed");
+                       if (setresuid(uid, uid, uid) == -1)
+                               fatal("[priv]: setresuid() failed");
+               }
                remote = pair[0];
                close(pair[1]);
                priv_ping();