#include <fcntl.h>
#include <errno.h>
#include <string.h>
+#include <dirent.h>
#include <kit/kit.h>
#include "kit-test.h"
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
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 */
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;
&_test_spawn,
};
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
int
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 ();
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);
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:
out:
if (dir != NULL)
- closedir(dir);
+ closedir (dir);
return ret;
}
_polkit_policy_cache_new (const char *dirname, polkit_bool_t load_descriptions, PolKitError **error)
{
DIR *dir;
- int dfd;
struct dirent64 *d;
PolKitPolicyCache *pc;
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;
}
polkit_policy_file_unref (pf);
}
- closedir(dir);
+ closedir (dir);
return pc;
out:
polkit_policy_cache_ref (pc);
polkit_policy_cache_unref (pc);
polkit_policy_cache_unref (pc);
-
out:
return TRUE;
}
#include <stdio.h>
#include <stdlib.h>
+#include <syslog.h>
#include <polkit/polkit-test.h>
#include <polkit/polkit-private.h>
#include <polkit/polkit-private.h>
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 ();
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);
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: