PolicyKit: Branch 'master'

David Zeuthen david at kemper.freedesktop.org
Sun Nov 11 15:41:46 PST 2007


 src/kit/kit-file.c                        |   39 ++++++++++++++++++++++++++++++
 src/kit/kit-file.h                        |    2 +
 src/kit/kit-spawn.c                       |    2 -
 src/kit/kit-test.c                        |   21 +++++++++++++++-
 src/polkit-dbus/polkit-read-auth-helper.c |    2 -
 src/polkit/polkit-policy-cache.c          |    9 ++----
 src/polkit/polkit-test.c                  |   22 ++++++++++++++++
 7 files changed, 88 insertions(+), 9 deletions(-)

New commits:
commit 109229bb5577c7b3ad778dd08edc7b312bad2c4c
Author: David Zeuthen <davidz at redhat.com>
Date:   Sun Nov 11 18:38:14 2007 -0500

    also check for file descriptor leaks

diff --git a/src/kit/kit-file.c b/src/kit/kit-file.c
index cc8ddd0..832e058 100644
--- a/src/kit/kit-file.c
+++ b/src/kit/kit-file.c
@@ -35,6 +35,7 @@
 #include <fcntl.h>
 #include <errno.h>
 #include <string.h>
+#include <dirent.h>
 
 #include <kit/kit.h>
 #include "kit-test.h"
@@ -263,6 +264,44 @@ out:
         return ret;
 }
 
+/**
+ * _kit_get_num_fd:
+ *
+ * Determines the number of open file descriptors
+ *
+ * Returns: Number of open file descriptors
+ */
+size_t 
+_kit_get_num_fd (void)
+{
+        DIR *dir;
+        char buf[128];
+        ssize_t num;
+        struct dirent64 *d;
+
+        num = -1;
+
+        snprintf (buf, sizeof (buf), "/proc/%d/fd", getpid ());
+
+        dir = opendir (buf);
+        if (dir == NULL) {
+                kit_warning ("error calling opendir on %s: %m\n", buf);
+                goto out;
+        }
+
+        num = -2;
+        while ((d = readdir64 (dir)) != NULL) {
+                if (d->d_name == NULL)
+                        continue;
+                num++;
+        }
+
+out:
+        if (dir != NULL)
+                closedir (dir);
+        return num;
+}
+
 
 #ifdef KIT_BUILD_TESTS
 
diff --git a/src/kit/kit-file.h b/src/kit/kit-file.h
index 2b31d53..b769e7c 100644
--- a/src/kit/kit-file.h
+++ b/src/kit/kit-file.h
@@ -37,6 +37,8 @@ KIT_BEGIN_DECLS
 kit_bool_t kit_file_get_contents (const char *path, char **out_contents, size_t *out_contents_size);
 kit_bool_t kit_file_set_contents (const char *path, mode_t mode, const char *contents, size_t contents_size);
 
+size_t _kit_get_num_fd (void);
+
 KIT_END_DECLS
 
 #endif /* KIT_FILE_H */
diff --git a/src/kit/kit-spawn.c b/src/kit/kit-spawn.c
index fb48a14..fb4be3e 100644
--- a/src/kit/kit-spawn.c
+++ b/src/kit/kit-spawn.c
@@ -140,7 +140,7 @@ again:
                         goto out;
         }
 
-        kit_debug ("Wrote %d bytes from '%s'", num_written, str);
+        //kit_debug ("Wrote %d bytes from '%s'", num_written, str);
 
 out:
         return num_written;
diff --git a/src/kit/kit-test.c b/src/kit/kit-test.c
index 9e226be..9299bcf 100644
--- a/src/kit/kit-test.c
+++ b/src/kit/kit-test.c
@@ -47,6 +47,10 @@ static KitTest *tests[] = {
         &_test_spawn,
 };
 
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
 int 
 main (int argc, char *argv[])
 {
@@ -63,6 +67,8 @@ main (int argc, char *argv[])
                 int m;
                 int total_allocs;
                 int delta;
+                int num_fd;
+                int num_fd_after;
                 KitTest *test = tests[n];
 
                 _kit_memory_reset ();
@@ -70,12 +76,14 @@ main (int argc, char *argv[])
                 if (test->setup != NULL)
                         test->setup ();
 
+                num_fd = _kit_get_num_fd ();
                 printf ("Running: %s\n", test->name);
                 if (!test->run ()) {
                         printf ("Failed\n");
                         ret = 1;
                         goto test_done;
                 }
+                num_fd_after = _kit_get_num_fd ();
 
                 total_allocs = _kit_memory_get_total_allocations ();
                 printf ("  Unit test made %d allocations in total\n", total_allocs);
@@ -85,24 +93,35 @@ main (int argc, char *argv[])
                         printf ("  Unit test leaked %d allocations\n", delta);
                         ret = 1;
                 }
+                if (num_fd != num_fd_after) {
+                        printf ("  Unit test leaked %d file descriptors\n", num_fd_after - num_fd);
+                        ret = 1;
+                }
                 
                 for (m = 0; m < total_allocs; m++) {
                         printf ("  Failing allocation %d of %d\n", m + 1, total_allocs);
                         
                         _kit_memory_reset ();
                         _kit_memory_fail_nth_alloc (m);
-                        
+
+                        num_fd = _kit_get_num_fd ();
                         if (!test->run ()) {
                                 printf ("  Failed\n");
                                 ret = 1;
                                 continue;
                         }
+                        num_fd_after = _kit_get_num_fd ();
                         
                         delta = _kit_memory_get_current_allocations ();
                         if (delta != 0) {
                                 printf ("  Unit test leaked %d allocations\n", delta);
                                 ret = 1;
                         }
+                        if (num_fd != num_fd_after) {
+                                printf ("  Unit test leaked %d file descriptors\n", num_fd_after - num_fd);
+                                ret = 1;
+                        }
+
                 }
 
         test_done:
diff --git a/src/polkit-dbus/polkit-read-auth-helper.c b/src/polkit-dbus/polkit-read-auth-helper.c
index 79be560..edc19c6 100644
--- a/src/polkit-dbus/polkit-read-auth-helper.c
+++ b/src/polkit-dbus/polkit-read-auth-helper.c
@@ -340,7 +340,7 @@ dump_auths_all (const char *root)
 
 out:
         if (dir != NULL)
-                closedir(dir);
+                closedir (dir);
         return ret;
 }
 
diff --git a/src/polkit/polkit-policy-cache.c b/src/polkit/polkit-policy-cache.c
index dc49d28..4e344c1 100644
--- a/src/polkit/polkit-policy-cache.c
+++ b/src/polkit/polkit-policy-cache.c
@@ -94,7 +94,6 @@ PolKitPolicyCache *
 _polkit_policy_cache_new (const char *dirname, polkit_bool_t load_descriptions, PolKitError **error)
 {
         DIR *dir;
-        int dfd;
         struct dirent64 *d;
         PolKitPolicyCache *pc;
 
@@ -109,15 +108,14 @@ _polkit_policy_cache_new (const char *dirname, polkit_bool_t load_descriptions,
         pc->refcount = 1;
 
         dir = opendir (dirname);
-        if (dir == NULL || (dfd = dirfd (dir)) == -1) {
+        if (dir == NULL) {
                 polkit_error_set_error (error, POLKIT_ERROR_POLICY_FILE_INVALID,
                                         "Cannot load policy files from directory %s: %m",
                                         dirname);
                 goto out;
         }
 
-
-        while ((d = readdir64(dir)) != NULL) {
+        while ((d = readdir64 (dir)) != NULL) {
                 char *path;
                 PolKitPolicyFile *pf;
                 PolKitError *pk_error;
@@ -167,7 +165,7 @@ _polkit_policy_cache_new (const char *dirname, polkit_bool_t load_descriptions,
                 }
                 polkit_policy_file_unref (pf);
         }
-        closedir(dir);
+        closedir (dir);
 
         return pc;
 out:
@@ -502,7 +500,6 @@ _run_test (void)
         polkit_policy_cache_ref (pc);
         polkit_policy_cache_unref (pc);
         polkit_policy_cache_unref (pc);
-
 out:
         return TRUE;
 }
diff --git a/src/polkit/polkit-test.c b/src/polkit/polkit-test.c
index 5ab195e..de87063 100644
--- a/src/polkit/polkit-test.c
+++ b/src/polkit/polkit-test.c
@@ -25,6 +25,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <syslog.h>
 #include <polkit/polkit-test.h>
 #include <polkit/polkit-private.h>
 #include <polkit/polkit-private.h>
@@ -68,11 +69,20 @@ main (int argc, char *argv[])
 
         num_tests = sizeof (tests) / sizeof (PolKitTest*);
 
+        /* Some of the code will log to syslog because .policy files
+         * etc. may be malformed. Since this will open a socket to the
+         * system logger preempt this so the fd-leak checking don't
+         * freak out.
+         */
+        syslog (LOG_INFO, "libpolkit: initiating test; bogus alerts may be written to syslog");
+
         printf ("Running %d unit tests\n", num_tests);
         for (n = 0; n < num_tests; n++) {
                 int m;
                 int total_allocs;
                 int delta;
+                int num_fd;
+                int num_fd_after;
                 PolKitTest *test = tests[n];
 
                 _kit_memory_reset ();
@@ -80,12 +90,14 @@ main (int argc, char *argv[])
                 if (test->setup != NULL)
                         test->setup ();
 
+                num_fd = _kit_get_num_fd ();
                 printf ("Running: %s\n", test->name);
                 if (!test->run ()) {
                         printf ("Failed\n");
                         ret = 1;
                         goto test_done;
                 }
+                num_fd_after = _kit_get_num_fd ();
 
                 total_allocs = _kit_memory_get_total_allocations ();
                 printf ("  Unit test made %d allocations in total\n", total_allocs);
@@ -95,6 +107,10 @@ main (int argc, char *argv[])
                         printf ("  Unit test leaked %d allocations\n", delta);
                         ret = 1;
                 }
+                if (num_fd != num_fd_after) {
+                        printf ("  Unit test leaked %d file descriptors\n", num_fd_after - num_fd);
+                        ret = 1;
+                }
                 
                 for (m = 0; m < total_allocs; m++) {
                         printf ("  Failing allocation %d of %d\n", m + 1, total_allocs);
@@ -102,17 +118,23 @@ main (int argc, char *argv[])
                         _kit_memory_reset ();
                         _kit_memory_fail_nth_alloc (m);
                         
+                        num_fd = _kit_get_num_fd ();
                         if (!test->run ()) {
                                 printf ("  Failed\n");
                                 ret = 1;
                                 continue;
                         }
+                        num_fd_after = _kit_get_num_fd ();
                         
                         delta = _kit_memory_get_current_allocations ();
                         if (delta != 0) {
                                 printf ("  Unit test leaked %d allocations\n", delta);
                                 ret = 1;
                         }
+                        if (num_fd != num_fd_after) {
+                                printf ("  Unit test leaked %d file descriptors\n", num_fd_after - num_fd);
+                                ret = 1;
+                        }
                 }
 
         test_done:


More information about the hal-commit mailing list