2 * Copyright (C) 2018 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
15 #include <sys/types.h>
21 #include <sys/types.h>
25 #include <common/compat/directory-handle.h>
26 #include <common/compat/errno.h>
27 #include <common/error.h>
28 #include <common/fd-tracker/fd-tracker.h>
31 int lttng_opt_quiet
= 1;
32 int lttng_opt_verbose
;
35 /* Number of TAP tests in this file */
37 /* 3 for stdin, stdout, and stderr */
38 #define STDIO_FD_COUNT 3
39 #define TRACKER_FD_LIMIT 50
40 #define TMP_DIR_PATTERN "/tmp/fd-tracker-XXXXXX"
41 #define TEST_UNLINK_DIRECTORY_NAME "unlinked_files"
44 #define SELF_FD_DIR "/proc/self/fd"
46 /* Most Unices have /dev/fd */
47 #define SELF_FD_DIR "/dev/fd"
51 * Count of fds, beyond stdin, stderr, stdout that were open
52 * at the launch of the test. This allows the test to succeed when
53 * run by automake's test runner or valgrind which both open
54 * fds behind our back.
56 int unknown_fds_count
;
58 const char file_contents
[] = "Bacon ipsum dolor amet jerky drumstick sirloin "
59 "strip steak venison boudin filet mignon picanha doner shoulder. "
60 "Strip steak brisket alcatra, venison beef chuck cupim pastrami. "
61 "Landjaeger tri-tip salami leberkas ball tip, ham hock chuck sausage "
62 "flank jerky cupim. Pig bacon chuck pancetta andouille.";
65 void get_temporary_directories(char **_test_directory
, char **_unlink_directory
)
68 char tmp_path_pattern
[] = TMP_DIR_PATTERN
;
71 output_dir
= mkdtemp(tmp_path_pattern
);
73 diag("Failed to create temporary path of the form %s",
78 *_test_directory
= strdup(output_dir
);
79 assert(*_test_directory
);
80 ret
= asprintf(_unlink_directory
, "%s/%s", output_dir
,
81 TEST_UNLINK_DIRECTORY_NAME
);
94 dir
= opendir(SELF_FD_DIR
);
96 perror("# Failed to enumerate " SELF_FD_DIR
" to count the number of used file descriptors");
101 while ((entry
= readdir(dir
)) != NULL
) {
102 if (!strcmp(entry
->d_name
, ".") || !strcmp(entry
->d_name
, "..")) {
107 /* Don't account for the file descriptor opened by opendir(). */
110 perror("# Failed to close test program's " SELF_FD_DIR
" directory file descriptor");
117 void check_fd_count(int expected_count
)
122 ok(count
== expected_count
, "Expected %d open file descriptors (%d are open)",
123 expected_count
, count
);
127 int noop_open(void *data
, int *fds
)
129 *fds
= *((int *) data
);
134 int noop_close(void *data
, int *fds
)
140 void track_std_fds(struct fd_tracker
*tracker
)
143 struct { int fd
; const char *name
; } files
[] = {
144 { .fd
= fileno(stdin
), .name
= "stdin" },
145 { .fd
= fileno(stdout
), .name
= "stdout" },
146 { .fd
= fileno(stderr
), .name
= "stderr" },
149 for (i
= 0; i
< sizeof(files
) / sizeof(*files
); i
++) {
152 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &out_fd
,
153 &files
[i
].name
, 1, noop_open
, &files
[i
].fd
);
154 assert(out_fd
== files
[i
].fd
);
156 ok(ret
== 0, "Track unsuspendable fd %d (%s)", files
[i
].fd
,
162 void untrack_std_fds(struct fd_tracker
*tracker
)
165 struct { int fd
; const char *name
; } files
[] = {
166 { .fd
= fileno(stdin
), .name
= "stdin" },
167 { .fd
= fileno(stdout
), .name
= "stdout" },
168 { .fd
= fileno(stderr
), .name
= "stderr" },
170 unsigned int fds_set_to_minus_1
= 0;
172 for (i
= 0; i
< sizeof(files
) / sizeof(*files
); i
++) {
173 int fd
= files
[i
].fd
;
174 int ret
= fd_tracker_close_unsuspendable_fd(tracker
,
175 &files
[i
].fd
, 1, noop_close
, NULL
);
177 ok(ret
== 0, "Untrack unsuspendable fd %d (%s)", fd
,
179 fds_set_to_minus_1
+= (files
[i
].fd
== -1);
184 * Basic test opening and closing three unsuspendable fds.
187 void test_unsuspendable_basic(void)
190 struct fd_tracker
*tracker
;
191 char *test_directory
= NULL
, *unlinked_files_directory
= NULL
;
193 get_temporary_directories(&test_directory
, &unlinked_files_directory
);
195 tracker
= fd_tracker_create(unlinked_files_directory
, TRACKER_FD_LIMIT
);
196 ok(tracker
, "Created an fd tracker with a limit of %d simulateously opened file descriptors",
202 track_std_fds(tracker
);
203 untrack_std_fds(tracker
);
205 fd_tracker_destroy(tracker
);
206 ret
= rmdir(test_directory
);
207 ok(ret
== 0, "Test directory is empty");
209 free(test_directory
);
210 free(unlinked_files_directory
);
214 int error_open(void *data
, int *fds
)
216 return *((int *) data
);
220 int error_close(void *data
, int *fds
)
222 return *((int *) data
);
226 * Validate that user callback return values are returned to the
227 * caller of the fd tracker.
230 void test_unsuspendable_cb_return(void)
232 int ret
, stdout_fd
= fileno(stdout
), out_fd
= 42;
233 struct fd_tracker
*tracker
;
234 int expected_error
= -ENETDOWN
;
235 char *test_directory
= NULL
, *unlinked_files_directory
= NULL
;
237 get_temporary_directories(&test_directory
, &unlinked_files_directory
);
239 tracker
= fd_tracker_create(test_directory
, TRACKER_FD_LIMIT
);
242 /* The error_open callback should fail and return 'expected_error'. */
243 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &out_fd
,
244 NULL
, 1, error_open
, &expected_error
);
245 ok(ret
== expected_error
, "fd_tracker_open_unsuspendable_fd() forwards the user callback's error code");
246 ok(out_fd
== 42, "Output fd parameter is unaffected on error of fd_tracker_open_unsuspendable_fd()");
249 * Track a valid fd since we don't want the tracker to fail with an
250 * invalid fd error for this test.
252 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &out_fd
,
253 NULL
, 1, noop_open
, &stdout_fd
);
254 ok(out_fd
== stdout_fd
, "fd_tracker_open_unsuspendable_fd() sets the output fd parameter to the newly-tracked fd's value");
257 ret
= fd_tracker_close_unsuspendable_fd(tracker
,
258 &stdout_fd
, 1, error_close
, &expected_error
);
259 ok(ret
== expected_error
, "fd_tracker_close_unsuspendable_fd() forwards the user callback's error code");
260 ret
= fd_tracker_close_unsuspendable_fd(tracker
,
261 &stdout_fd
, 1, noop_close
, &expected_error
);
264 fd_tracker_destroy(tracker
);
265 ret
= rmdir(test_directory
);
266 ok(ret
== 0, "Test directory is empty");
267 free(test_directory
);
268 free(unlinked_files_directory
);
272 * Validate that the tracker refuses to track two identical unsuspendable
276 void test_unsuspendable_duplicate(void)
278 int ret
, stdout_fd
= fileno(stdout
), out_fd
;
279 struct fd_tracker
*tracker
;
280 char *test_directory
= NULL
, *unlinked_files_directory
= NULL
;
282 get_temporary_directories(&test_directory
, &unlinked_files_directory
);
284 tracker
= fd_tracker_create(unlinked_files_directory
, TRACKER_FD_LIMIT
);
287 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &out_fd
,
288 NULL
, 1, noop_open
, &stdout_fd
);
290 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &out_fd
,
291 NULL
, 1, noop_open
, &stdout_fd
);
292 ok(ret
== -EEXIST
, "EEXIST reported on open of an already tracked file descriptor");
294 ret
= fd_tracker_close_unsuspendable_fd(tracker
,
295 &stdout_fd
, 1, noop_close
, NULL
);
298 fd_tracker_destroy(tracker
);
299 ret
= rmdir(test_directory
);
300 ok(ret
== 0, "Test directory is empty");
301 free(test_directory
);
302 free(unlinked_files_directory
);
306 int open_pipes(void *data
, int *out_fds
)
309 const unsigned int pipe_count
= TRACKER_FD_LIMIT
/ 2;
311 for (i
= 0; i
< pipe_count
; i
++) {
312 int ret
= pipe(&out_fds
[i
* 2]);
322 int close_pipes(void *data
, int *fds
)
327 for (i
= 0; i
< TRACKER_FD_LIMIT
; i
++) {
328 int ret
= close(pipes
[i
]);
338 * Validate that the tracker enforces the open file descriptor limit
339 * when unsuspendable file descriptors are being opened.
342 void test_unsuspendable_limit(void)
344 struct fd_tracker
*tracker
;
345 int ret
, stdout_fd
= fileno(stdout
), out_fd
;
346 int fds
[TRACKER_FD_LIMIT
];
347 char *test_directory
= NULL
, *unlinked_files_directory
= NULL
;
349 get_temporary_directories(&test_directory
, &unlinked_files_directory
);
351 /* This test assumes TRACKER_FD_LIMIT is a multiple of 2. */
352 assert((TRACKER_FD_LIMIT
% 2 == 0) && TRACKER_FD_LIMIT
);
354 tracker
= fd_tracker_create(unlinked_files_directory
, TRACKER_FD_LIMIT
);
357 ret
= fd_tracker_open_unsuspendable_fd(tracker
, fds
,
358 NULL
, TRACKER_FD_LIMIT
, open_pipes
, NULL
);
359 ok(ret
== 0, "File descriptor tracker allowed the user to meet its limit with unsuspendable file descriptors (%d)",
362 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &out_fd
,
363 NULL
, 1, noop_open
, &stdout_fd
);
364 ok(ret
== -EMFILE
, "EMFILE reported when exceeding the file descriptor limit while opening an unsuspendable fd");
366 ret
= fd_tracker_close_unsuspendable_fd(tracker
,
367 fds
, TRACKER_FD_LIMIT
, close_pipes
, NULL
);
370 fd_tracker_destroy(tracker
);
371 ret
= rmdir(test_directory
);
372 ok(ret
== 0, "Test directory is empty");
373 free(test_directory
);
374 free(unlinked_files_directory
);
378 * Validate that the tracker refuses to track two identical unsuspendable
382 void test_unsuspendable_close_untracked(void)
384 int ret
, stdout_fd
= fileno(stdout
), unknown_fds
[2], out_fd
;
385 struct fd_tracker
*tracker
;
386 char *test_directory
= NULL
, *unlinked_files_directory
= NULL
;
388 get_temporary_directories(&test_directory
, &unlinked_files_directory
);
390 tracker
= fd_tracker_create(unlinked_files_directory
, TRACKER_FD_LIMIT
);
395 ret
= pipe(unknown_fds
);
397 assert(close(unknown_fds
[0]) == 0);
398 assert(close(unknown_fds
[1]) == 0);
400 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &out_fd
,
401 NULL
, 1, noop_open
, &stdout_fd
);
404 ret
= fd_tracker_close_unsuspendable_fd(tracker
,
405 unknown_fds
, 1, noop_close
, NULL
);
406 ok(ret
== -EINVAL
, "EINVAL reported on close of an untracked file descriptor");
408 ret
= fd_tracker_close_unsuspendable_fd(tracker
,
409 &stdout_fd
, 1, noop_close
, NULL
);
412 fd_tracker_destroy(tracker
);
413 ret
= rmdir(test_directory
);
414 ok(ret
== 0, "Test directory is empty");
416 free(test_directory
);
417 free(unlinked_files_directory
);
420 static int open_files(struct fd_tracker
*tracker
,
421 struct lttng_directory_handle
*directory
,
423 struct fs_handle
**handles
,
429 for (i
= 0; i
< count
; i
++) {
432 struct fs_handle
*handle
;
433 mode_t mode
= S_IWUSR
| S_IRUSR
;
435 p_ret
= asprintf(&file_path
, "file-%u", i
);
437 file_paths
[i
] = file_path
;
439 handle
= fd_tracker_open_fs_handle(tracker
, directory
, file_path
,
440 O_RDWR
| O_CREAT
, &mode
);
450 static int open_same_file(struct fd_tracker
*tracker
,
451 struct lttng_directory_handle
*directory
,
454 struct fs_handle
**handles
)
459 for (i
= 0; i
< count
; i
++) {
460 struct fs_handle
*handle
;
461 mode_t mode
= S_IWUSR
| S_IRUSR
;
463 handle
= fd_tracker_open_fs_handle(tracker
, directory
, file
,
464 O_RDWR
| O_CREAT
, &mode
);
475 int cleanup_files(struct fd_tracker
*tracker
, const char *dir
,
476 unsigned int count
, struct fs_handle
**handles
,
482 for (i
= 0; i
< count
; i
++) {
483 char *file_path
= file_paths
[i
];
488 if (fs_handle_unlink(handles
[i
])) {
489 diag("Failed to unlink fs_handle to file %s", file_path
);
492 if (fs_handle_close(handles
[i
])) {
493 diag("Failed to close fs_handle to file %s", file_path
);
502 void test_suspendable_limit(void)
505 const int files_to_create
= TRACKER_FD_LIMIT
* 10;
506 struct fd_tracker
*tracker
;
507 char *test_directory
= NULL
, *unlinked_files_directory
= NULL
;
508 char *output_files
[files_to_create
];
509 struct fs_handle
*handles
[files_to_create
];
510 struct lttng_directory_handle
*dir_handle
= NULL
;
511 int dir_handle_fd_count
;
513 memset(output_files
, 0, sizeof(output_files
));
514 memset(handles
, 0, sizeof(handles
));
516 get_temporary_directories(&test_directory
, &unlinked_files_directory
);
518 tracker
= fd_tracker_create(unlinked_files_directory
, TRACKER_FD_LIMIT
);
523 dir_handle
= lttng_directory_handle_create(test_directory
);
525 dir_handle_fd_count
= !!lttng_directory_handle_uses_fd(dir_handle
);
527 ret
= open_files(tracker
, dir_handle
, files_to_create
, handles
,
529 ok(!ret
, "Created %d files with a limit of %d simultaneously-opened file descriptor",
530 files_to_create
, TRACKER_FD_LIMIT
);
531 check_fd_count(TRACKER_FD_LIMIT
+ STDIO_FD_COUNT
+ unknown_fds_count
+
532 dir_handle_fd_count
);
534 ret
= cleanup_files(tracker
, test_directory
, files_to_create
, handles
,
536 ok(!ret
, "Close all opened filesystem handles");
537 ret
= rmdir(test_directory
);
538 ok(ret
== 0, "Test directory is empty");
539 fd_tracker_destroy(tracker
);
540 lttng_directory_handle_put(dir_handle
);
542 free(test_directory
);
543 free(unlinked_files_directory
);
547 void test_mixed_limit(void)
550 const int files_to_create
= TRACKER_FD_LIMIT
;
551 struct fd_tracker
*tracker
;
552 char *test_directory
= NULL
, *unlinked_files_directory
= NULL
;
553 char *output_files
[files_to_create
];
554 struct fs_handle
*handles
[files_to_create
];
555 struct lttng_directory_handle
*dir_handle
= NULL
;
556 int dir_handle_fd_count
;
558 memset(output_files
, 0, sizeof(output_files
));
559 memset(handles
, 0, sizeof(handles
));
561 get_temporary_directories(&test_directory
, &unlinked_files_directory
);
563 tracker
= fd_tracker_create(unlinked_files_directory
, TRACKER_FD_LIMIT
);
568 dir_handle
= lttng_directory_handle_create(test_directory
);
570 dir_handle_fd_count
= !!lttng_directory_handle_uses_fd(dir_handle
);
572 ret
= open_files(tracker
, dir_handle
, files_to_create
, handles
,
574 ok(!ret
, "Created %d files with a limit of %d simultaneously-opened file descriptor",
575 files_to_create
, TRACKER_FD_LIMIT
);
576 diag("Check file descriptor count after opening %u files", files_to_create
);
577 check_fd_count(TRACKER_FD_LIMIT
+ STDIO_FD_COUNT
+ unknown_fds_count
+
578 dir_handle_fd_count
);
581 * Open unsuspendable fds (stdin, stdout, stderr) and verify that the fd
582 * cap is still respected.
584 diag("Check file descriptor count after adding %d unsuspendable fds",
586 track_std_fds(tracker
);
587 check_fd_count(TRACKER_FD_LIMIT
+ unknown_fds_count
+
588 dir_handle_fd_count
);
589 diag("Untrack unsuspendable file descriptors");
590 untrack_std_fds(tracker
);
591 check_fd_count(TRACKER_FD_LIMIT
+ unknown_fds_count
+
592 dir_handle_fd_count
);
594 ret
= cleanup_files(tracker
, test_directory
, files_to_create
, handles
,
596 ok(!ret
, "Close all opened filesystem handles");
597 ret
= rmdir(test_directory
);
598 ok(ret
== 0, "Test directory is empty");
599 fd_tracker_destroy(tracker
);
600 lttng_directory_handle_put(dir_handle
);
602 free(test_directory
);
603 free(unlinked_files_directory
);
607 * Open more files than allowed by the fd tracker's cap and write,
608 * byte-by-byte, and in round-robin, a string. The goal is to force
609 * the fd tracker to suspend and resume the fs_handles often and
610 * verify that the fd cap is always respected.
612 * The content of the files is also verified at the end.
615 void test_suspendable_restore(void)
618 const int files_to_create
= TRACKER_FD_LIMIT
* 10;
619 struct fd_tracker
*tracker
;
620 char *output_files
[files_to_create
];
621 struct fs_handle
*handles
[files_to_create
];
622 size_t content_index
;
624 bool write_success
= true;
625 bool fd_cap_respected
= true;
626 bool content_ok
= true;
627 struct lttng_directory_handle
*dir_handle
= NULL
;
628 int dir_handle_fd_count
;
629 char *test_directory
= NULL
, *unlinked_files_directory
= NULL
;
631 memset(output_files
, 0, sizeof(output_files
));
632 memset(handles
, 0, sizeof(handles
));
634 get_temporary_directories(&test_directory
, &unlinked_files_directory
);
636 tracker
= fd_tracker_create(unlinked_files_directory
, TRACKER_FD_LIMIT
);
641 dir_handle
= lttng_directory_handle_create(test_directory
);
643 dir_handle_fd_count
= !!lttng_directory_handle_uses_fd(dir_handle
);
645 ret
= open_files(tracker
, dir_handle
, files_to_create
, handles
,
647 ok(!ret
, "Created %d files with a limit of %d simultaneously-opened file descriptor",
648 files_to_create
, TRACKER_FD_LIMIT
);
649 diag("Check file descriptor count after opening %u files", files_to_create
);
650 check_fd_count(TRACKER_FD_LIMIT
+ STDIO_FD_COUNT
+ unknown_fds_count
+
651 dir_handle_fd_count
);
653 for (content_index
= 0; content_index
< sizeof(file_contents
); content_index
++) {
654 for (handle_index
= 0; handle_index
< files_to_create
; handle_index
++) {
656 struct fs_handle
*handle
= handles
[handle_index
];
657 const char *path
= output_files
[handle_index
];
659 fd
= fs_handle_get_fd(handle
);
661 write_success
= false;
662 diag("Failed to restore fs_handle to %s",
668 ret
= write(fd
, file_contents
+ content_index
, 1);
669 } while (ret
< 0 && errno
== EINTR
);
672 write_success
= false;
673 PERROR("write() to %s failed", path
);
677 if (fd_count() > (TRACKER_FD_LIMIT
+ STDIO_FD_COUNT
+
679 dir_handle_fd_count
)) {
680 fd_cap_respected
= false;
683 fs_handle_put_fd(handle
);
687 ok(write_success
, "Wrote reference string to %d files",
689 ok(fd_cap_respected
, "FD tracker enforced the file descriptor cap");
691 /* Validate the contents of the files. */
692 for (handle_index
= 0; handle_index
< files_to_create
; handle_index
++) {
694 const char *path
= output_files
[handle_index
];
695 char read_buf
[sizeof(file_contents
)];
697 size_t to_read
= sizeof(read_buf
);
700 fd
= lttng_directory_handle_open_file(
701 dir_handle
, path
, O_RDONLY
, 0);
703 ret
= fstat(fd
, &fd_stat
);
705 if (fd_stat
.st_size
!= sizeof(file_contents
)) {
706 diag("Content size of file %s doesn't match, got %" PRId64
", expected %zu",
707 path
, (int64_t) fd_stat
.st_size
,
708 sizeof(file_contents
));
716 ret
= read(fd
, read_pos
, to_read
);
721 } while (to_read
&& (ret
< 0 && errno
== EINTR
));
724 PERROR("Failed to read file %s", path
);
729 if (strcmp(file_contents
, read_buf
)) {
731 diag("File content doesn't match the expectated string");
737 ok(content_ok
, "Files contain the expected content");
738 ret
= cleanup_files(tracker
, test_directory
, files_to_create
, handles
,
740 ok(!ret
, "Close all opened filesystem handles");
741 ret
= rmdir(test_directory
);
742 ok(ret
== 0, "Test directory is empty");
743 fd_tracker_destroy(tracker
);
744 lttng_directory_handle_put(dir_handle
);
746 free(test_directory
);
747 free(unlinked_files_directory
);
751 void test_unlink(void)
754 struct fd_tracker
*tracker
;
755 const int handles_to_open
= 2;
756 struct fs_handle
*handles
[handles_to_open
];
757 struct fs_handle
*new_handle
= NULL
;
759 struct lttng_directory_handle
*dir_handle
= NULL
;
760 const char file_name
[] = "my_file";
761 char *test_directory
= NULL
, *unlinked_files_directory
= NULL
;
762 char *unlinked_file_zero
= NULL
, *unlinked_file_one
= NULL
;
765 get_temporary_directories(&test_directory
, &unlinked_files_directory
);
766 ret
= asprintf(&unlinked_file_zero
, "%s/%u", unlinked_files_directory
,
769 ret
= asprintf(&unlinked_file_one
, "%s/%u", unlinked_files_directory
,
773 tracker
= fd_tracker_create(unlinked_files_directory
, 1);
778 dir_handle
= lttng_directory_handle_create(test_directory
);
781 /* Open two handles to the same file. */
782 ret
= open_same_file(tracker
, dir_handle
, file_name
, handles_to_open
,
784 ok(!ret
, "Successfully opened %i handles to %s/%s", handles_to_open
,
785 test_directory
, file_name
);
791 * Unlinking the first handle should cause the file to be renamed
794 ret
= fs_handle_unlink(handles
[0]);
795 ok(!ret
, "Successfully unlinked the first handle to %s/%s",
796 test_directory
, file_name
);
799 * The original file should no longer exist on the file system, and a
800 * new file named '0' should exist.
802 ok(lttng_directory_handle_stat(dir_handle
, file_name
, &statbuf
) == -1 &&
804 "%s no longer present on file system after unlink",
806 ok(lttng_directory_handle_stat(
807 dir_handle
, unlinked_file_zero
, &statbuf
) == 0,
808 "%s exists on file system after unlink",
812 * It should be possible to use the file descriptors of both handles.
813 * Since only one file descriptor can be opened at once, this should
814 * force the fd_tracker to suspend and restore the handles.
816 fd
= fs_handle_get_fd(handles
[0]);
817 ok(fd
>= 0, "Got fd from first handle");
819 fd
= fs_handle_get_fd(handles
[1]);
820 ok (fd
< 0, "fd tracker does not allow two fds to be used at once");
822 fs_handle_put_fd(handles
[0]);
823 fd
= fs_handle_get_fd(handles
[1]);
824 ok(fd
>= 0, "Got fd from second handle");
825 fs_handle_put_fd(handles
[1]);
827 /* The second unlink should fail with -ENOENT. */
828 ret
= fs_handle_unlink(handles
[1]);
830 "ENOENT is reported when attempting to unlink the second handle to %s/%s",
831 test_directory
, file_name
);
834 * Opening a new handle to 'my_file' should succeed.
836 ret
= open_same_file(tracker
, dir_handle
, file_name
, 1, &new_handle
);
837 ok(!ret
, "Successfully opened a new handle to previously unlinked file %s/%s",
838 test_directory
, file_name
);
842 * Unlinking the new handle should cause the file to be renamed
843 * to '1' since '0' already exists.
845 ret
= fs_handle_unlink(new_handle
);
846 ok(!ret
, "Successfully unlinked the new handle handle to %s/%s",
847 test_directory
, file_name
);
848 ok(stat(unlinked_file_one
, &statbuf
) == 0,
849 "%s exists on file system after unlink",
852 ret
= fs_handle_close(handles
[0]);
853 ok(!ret
, "Successfully closed the first handle");
854 ret
= fs_handle_close(handles
[1]);
855 ok(!ret
, "Successfully closed the second handle");
856 ret
= fs_handle_close(new_handle
);
857 ok(!ret
, "Successfully closed the third handle");
859 ok(lttng_directory_handle_stat(dir_handle
, file_name
, &statbuf
) == -1 &&
861 "%s no longer present on file system after handle close",
863 ok(lttng_directory_handle_stat(
864 dir_handle
, unlinked_file_zero
, &statbuf
) == -1 &&
866 "%s no longer present on file system after handle close",
868 ok(lttng_directory_handle_stat(dir_handle
, unlinked_file_one
,
871 "%s no longer present on file system after handle close",
874 ret
= rmdir(test_directory
);
875 ok(ret
== 0, "Test directory is empty");
877 fd_tracker_destroy(tracker
);
878 free(test_directory
);
879 free(unlinked_files_directory
);
880 free(unlinked_file_zero
);
881 free(unlinked_file_one
);
882 lttng_directory_handle_put(dir_handle
);
885 int main(int argc
, char **argv
)
887 plan_tests(NUM_TESTS
);
888 diag("File descriptor tracker unit tests");
890 rcu_register_thread();
892 unknown_fds_count
= fd_count() - STDIO_FD_COUNT
;
893 assert(unknown_fds_count
>= 0);
895 diag("Unsuspendable - basic");
896 test_unsuspendable_basic();
897 diag("Unsuspendable - callback return values");
898 test_unsuspendable_cb_return();
899 diag("Unsuspendable - duplicate file descriptors");
900 test_unsuspendable_duplicate();
901 diag("Unsuspendable - closing an untracked file descriptor");
902 test_unsuspendable_close_untracked();
903 diag("Unsuspendable - check that file descriptor limit is enforced");
904 test_unsuspendable_limit();
906 diag("Suspendable - check that file descriptor limit is enforced");
907 test_suspendable_limit();
908 diag("Suspendable - restoration test");
909 test_suspendable_restore();
911 diag("Mixed - check that file descriptor limit is enforced");
914 diag("Suspendable - Unlinking test");
918 rcu_unregister_thread();
919 return exit_status();