#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
-#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <urcu.h>
#include <common/compat/directory-handle.h>
+#include <common/compat/errno.h>
#include <common/error.h>
#include <common/fd-tracker/fd-tracker.h>
#define TMP_DIR_PATTERN "/tmp/fd-tracker-XXXXXX"
#define TEST_UNLINK_DIRECTORY_NAME "unlinked_files"
+#ifdef __linux__
+#define SELF_FD_DIR "/proc/self/fd"
+#else
+/* Most Unices have /dev/fd */
+#define SELF_FD_DIR "/dev/fd"
+#endif
+
/*
* Count of fds, beyond stdin, stderr, stdout that were open
* at the launch of the test. This allows the test to succeed when
{
DIR *dir;
struct dirent *entry;
- int count = 0;
+ int count = 0;
- dir = opendir("/proc/self/fd");
+ dir = opendir(SELF_FD_DIR);
if (!dir) {
- perror("# Failed to enumerate /proc/self/fd/ to count the number of used file descriptors");
- count = -1;
+ perror("# Failed to enumerate " SELF_FD_DIR " to count the number of used file descriptors");
+ count = -1;
goto end;
}
if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) {
continue;
}
- count++;
+ count++;
}
/* Don't account for the file descriptor opened by opendir(). */
- count--;
+ count--;
if (closedir(dir)) {
- perror("# Failed to close test program's self/fd directory file descriptor");
+ perror("# Failed to close test program's " SELF_FD_DIR " directory file descriptor");
}
end:
return count;
static
void check_fd_count(int expected_count)
{
- int count = 0;
+ int count = 0;
- count = fd_count();
+ count = fd_count();
ok(count == expected_count, "Expected %d open file descriptors (%d are open)",
expected_count, count);
}
static
int noop_open(void *data, int *fds)
{
- *fds = *((int *) data);
+ *fds = *((int *) data);
return 0;
}
void track_std_fds(struct fd_tracker *tracker)
{
int i;
- struct { int fd; const char *name; } files[] = {
+ struct { int fd; const char *name; } files[] = {
{ .fd = fileno(stdin), .name = "stdin" },
{ .fd = fileno(stdout), .name = "stdout" },
{ .fd = fileno(stderr), .name = "stderr" },
void untrack_std_fds(struct fd_tracker *tracker)
{
int i;
- struct { int fd; const char *name; } files[] = {
+ struct { int fd; const char *name; } files[] = {
{ .fd = fileno(stdin), .name = "stdin" },
{ .fd = fileno(stdout), .name = "stdout" },
{ .fd = fileno(stderr), .name = "stderr" },
get_temporary_directories(&test_directory, &unlinked_files_directory);
- tracker = fd_tracker_create(unlinked_files_directory, TRACKER_FD_LIMIT);
+ tracker = fd_tracker_create(unlinked_files_directory, TRACKER_FD_LIMIT);
ok(tracker, "Created an fd tracker with a limit of %d simulateously opened file descriptors",
TRACKER_FD_LIMIT);
if (!tracker) {
get_temporary_directories(&test_directory, &unlinked_files_directory);
- tracker = fd_tracker_create(test_directory, TRACKER_FD_LIMIT);
+ tracker = fd_tracker_create(test_directory, TRACKER_FD_LIMIT);
assert(tracker);
/* The error_open callback should fail and return 'expected_error'. */
get_temporary_directories(&test_directory, &unlinked_files_directory);
- tracker = fd_tracker_create(unlinked_files_directory, TRACKER_FD_LIMIT);
+ tracker = fd_tracker_create(unlinked_files_directory, TRACKER_FD_LIMIT);
assert(tracker);
ret = fd_tracker_open_unsuspendable_fd(tracker, &out_fd,
/*
* Validate that the tracker enforces the open file descriptor limit
- * when unsuspendable file descritptors are being opened.
+ * when unsuspendable file descriptors are being opened.
*/
static
void test_unsuspendable_limit(void)
/* This test assumes TRACKER_FD_LIMIT is a multiple of 2. */
assert((TRACKER_FD_LIMIT % 2 == 0) && TRACKER_FD_LIMIT);
- tracker = fd_tracker_create(unlinked_files_directory, TRACKER_FD_LIMIT);
+ tracker = fd_tracker_create(unlinked_files_directory, TRACKER_FD_LIMIT);
assert(tracker);
ret = fd_tracker_open_unsuspendable_fd(tracker, fds,
NULL, TRACKER_FD_LIMIT, open_pipes, NULL);
- ok(ret == 0, "File descriptor tracker allowed the user to meet its limit with unsuspendable file descritptors (%d)",
+ ok(ret == 0, "File descriptor tracker allowed the user to meet its limit with unsuspendable file descriptors (%d)",
TRACKER_FD_LIMIT);
ret = fd_tracker_open_unsuspendable_fd(tracker, &out_fd,
get_temporary_directories(&test_directory, &unlinked_files_directory);
- tracker = fd_tracker_create(unlinked_files_directory, TRACKER_FD_LIMIT);
+ tracker = fd_tracker_create(unlinked_files_directory, TRACKER_FD_LIMIT);
if (!tracker) {
goto end;;
}
for (i = 0; i < count; i++) {
int p_ret;
- char *file_path;
+ char *file_path;
struct fs_handle *handle;
mode_t mode = S_IWUSR | S_IRUSR;
p_ret = asprintf(&file_path, "file-%u", i);
assert(p_ret >= 0);
- file_paths[i] = file_path;
+ file_paths[i] = file_path;
handle = fd_tracker_open_fs_handle(tracker, directory, file_path,
O_RDWR | O_CREAT, &mode);
diag("Failed to unlink fs_handle to file %s", file_path);
ret = -1;
}
- if (fs_handle_close(handles[i])) {
+ if (fs_handle_close(handles[i])) {
diag("Failed to close fs_handle to file %s", file_path);
ret = -1;
}
get_temporary_directories(&test_directory, &unlinked_files_directory);
- tracker = fd_tracker_create(unlinked_files_directory, TRACKER_FD_LIMIT);
+ tracker = fd_tracker_create(unlinked_files_directory, TRACKER_FD_LIMIT);
if (!tracker) {
goto end;
}
get_temporary_directories(&test_directory, &unlinked_files_directory);
- tracker = fd_tracker_create(unlinked_files_directory, TRACKER_FD_LIMIT);
+ tracker = fd_tracker_create(unlinked_files_directory, TRACKER_FD_LIMIT);
if (!tracker) {
goto end;
}
fd = fs_handle_get_fd(handle);
if (fd < 0) {
write_success = false;
- diag("Failed to restore fs_handle to %s",
- path);
+ diag("Failed to restore fs_handle to %s",
+ path);
goto skip_write;
}
if (ret != 1) {
write_success = false;
- PERROR("write() to %s failed", path);
+ PERROR("write() to %s failed", path);
goto skip_write;
}
test_unsuspendable_duplicate();
diag("Unsuspendable - closing an untracked file descriptor");
test_unsuspendable_close_untracked();
- diag("Unsuspendable - check that file descritptor limit is enforced");
+ diag("Unsuspendable - check that file descriptor limit is enforced");
test_unsuspendable_limit();
- diag("Suspendable - check that file descritptor limit is enforced");
+ diag("Suspendable - check that file descriptor limit is enforced");
test_suspendable_limit();
diag("Suspendable - restoration test");
test_suspendable_restore();
- diag("Mixed - check that file descritptor limit is enforced");
+ diag("Mixed - check that file descriptor limit is enforced");
test_mixed_limit();
diag("Suspendable - Unlinking test");