2 * Copyright (C) 2018 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
8 #include <common/compat/directory-handle.hpp>
9 #include <common/compat/errno.hpp>
10 #include <common/error.hpp>
11 #include <common/fd-tracker/fd-tracker.hpp>
12 #include <common/fs-handle.hpp>
23 #include <sys/types.h>
29 int lttng_opt_quiet
= 1;
30 int lttng_opt_verbose
;
33 /* Number of TAP tests in this file */
35 /* 3 for stdin, stdout, and stderr */
36 #define STDIO_FD_COUNT 3
37 #define TRACKER_FD_LIMIT 50
38 #define TMP_DIR_PATTERN "/tmp/fd-tracker-XXXXXX"
39 #define TEST_UNLINK_DIRECTORY_NAME "unlinked_files"
42 #define SELF_FD_DIR "/proc/self/fd"
44 /* Most Unices have /dev/fd */
45 #define SELF_FD_DIR "/dev/fd"
49 * Count of fds, beyond stdin, stderr, stdout that were open
50 * at the launch of the test. This allows the test to succeed when
51 * run by automake's test runner or valgrind which both open
52 * fds behind our back.
54 int unknown_fds_count
;
56 const char file_contents
[] = "Bacon ipsum dolor amet jerky drumstick sirloin "
57 "strip steak venison boudin filet mignon picanha doner shoulder. "
58 "Strip steak brisket alcatra, venison beef chuck cupim pastrami. "
59 "Landjaeger tri-tip salami leberkas ball tip, ham hock chuck sausage "
60 "flank jerky cupim. Pig bacon chuck pancetta andouille.";
62 static void get_temporary_directories(char **_test_directory
, char **_unlink_directory
)
65 char tmp_path_pattern
[] = TMP_DIR_PATTERN
;
68 output_dir
= mkdtemp(tmp_path_pattern
);
70 diag("Failed to create temporary path of the form %s", TMP_DIR_PATTERN
);
74 *_test_directory
= strdup(output_dir
);
75 LTTNG_ASSERT(*_test_directory
);
76 ret
= asprintf(_unlink_directory
, "%s/%s", output_dir
, TEST_UNLINK_DIRECTORY_NAME
);
82 static int fd_count(void)
88 dir
= opendir(SELF_FD_DIR
);
90 perror("# Failed to enumerate " SELF_FD_DIR
91 " to count the number of used file descriptors");
96 while ((entry
= readdir(dir
)) != NULL
) {
97 if (!strcmp(entry
->d_name
, ".") || !strcmp(entry
->d_name
, "..")) {
102 /* Don't account for the file descriptor opened by opendir(). */
105 perror("# Failed to close test program's " SELF_FD_DIR
106 " directory file descriptor");
112 static void check_fd_count(int expected_count
)
117 ok(count
== expected_count
,
118 "Expected %d open file descriptors (%d are open)",
123 static int noop_open(void *data
, int *fds
)
125 *fds
= *((int *) data
);
129 static int noop_close(void *data
__attribute__((unused
)), int *fds
__attribute__((unused
)))
134 static void track_std_fds(struct fd_tracker
*tracker
)
141 { .fd
= fileno(stdin
), .name
= "stdin" },
142 { .fd
= fileno(stdout
), .name
= "stdout" },
143 { .fd
= fileno(stderr
), .name
= "stderr" },
146 for (i
= 0; i
< sizeof(files
) / sizeof(*files
); i
++) {
149 ret
= fd_tracker_open_unsuspendable_fd(
150 tracker
, &out_fd
, &files
[i
].name
, 1, noop_open
, &files
[i
].fd
);
151 LTTNG_ASSERT(out_fd
== files
[i
].fd
);
153 ok(ret
== 0, "Track unsuspendable fd %d (%s)", files
[i
].fd
, files
[i
].name
);
157 static void untrack_std_fds(struct fd_tracker
*tracker
)
164 { .fd
= fileno(stdin
), .name
= "stdin" },
165 { .fd
= fileno(stdout
), .name
= "stdout" },
166 { .fd
= fileno(stderr
), .name
= "stderr" },
169 for (i
= 0; i
< sizeof(files
) / sizeof(*files
); i
++) {
170 int fd
= files
[i
].fd
;
171 int ret
= fd_tracker_close_unsuspendable_fd(
172 tracker
, &files
[i
].fd
, 1, noop_close
, NULL
);
174 ok(ret
== 0, "Untrack unsuspendable fd %d (%s)", fd
, files
[i
].name
);
179 * Basic test opening and closing three unsuspendable fds.
181 static void test_unsuspendable_basic(void)
184 struct fd_tracker
*tracker
;
185 char *test_directory
= NULL
, *unlinked_files_directory
= NULL
;
187 get_temporary_directories(&test_directory
, &unlinked_files_directory
);
189 tracker
= fd_tracker_create(unlinked_files_directory
, TRACKER_FD_LIMIT
);
191 "Created an fd tracker with a limit of %d simulateously opened file descriptors",
197 track_std_fds(tracker
);
198 untrack_std_fds(tracker
);
200 fd_tracker_destroy(tracker
);
201 ret
= rmdir(test_directory
);
202 ok(ret
== 0, "Test directory is empty");
204 free(test_directory
);
205 free(unlinked_files_directory
);
208 static int error_open(void *data
, int *fds
__attribute__((unused
)))
210 return *((int *) data
);
213 static int error_close(void *data
, int *fds
__attribute__((unused
)))
215 return *((int *) data
);
219 * Validate that user callback return values are returned to the
220 * caller of the fd tracker.
222 static void test_unsuspendable_cb_return(void)
224 int ret
, stdout_fd
= fileno(stdout
), out_fd
= 42;
225 struct fd_tracker
*tracker
;
226 int expected_error
= -ENETDOWN
;
227 char *test_directory
= NULL
, *unlinked_files_directory
= NULL
;
229 get_temporary_directories(&test_directory
, &unlinked_files_directory
);
231 tracker
= fd_tracker_create(test_directory
, TRACKER_FD_LIMIT
);
232 LTTNG_ASSERT(tracker
);
234 /* The error_open callback should fail and return 'expected_error'. */
235 ret
= fd_tracker_open_unsuspendable_fd(
236 tracker
, &out_fd
, NULL
, 1, error_open
, &expected_error
);
237 ok(ret
== expected_error
,
238 "fd_tracker_open_unsuspendable_fd() forwards the user callback's error code");
240 "Output fd parameter is unaffected on error of fd_tracker_open_unsuspendable_fd()");
243 * Track a valid fd since we don't want the tracker to fail with an
244 * invalid fd error for this test.
246 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &out_fd
, NULL
, 1, noop_open
, &stdout_fd
);
247 ok(out_fd
== stdout_fd
,
248 "fd_tracker_open_unsuspendable_fd() sets the output fd parameter to the newly-tracked fd's value");
251 ret
= fd_tracker_close_unsuspendable_fd(
252 tracker
, &stdout_fd
, 1, error_close
, &expected_error
);
253 ok(ret
== expected_error
,
254 "fd_tracker_close_unsuspendable_fd() forwards the user callback's error code");
255 ret
= fd_tracker_close_unsuspendable_fd(
256 tracker
, &stdout_fd
, 1, noop_close
, &expected_error
);
259 fd_tracker_destroy(tracker
);
260 ret
= rmdir(test_directory
);
261 ok(ret
== 0, "Test directory is empty");
262 free(test_directory
);
263 free(unlinked_files_directory
);
267 * Validate that the tracker refuses to track two identical unsuspendable
270 static void test_unsuspendable_duplicate(void)
272 int ret
, stdout_fd
= fileno(stdout
), out_fd
;
273 struct fd_tracker
*tracker
;
274 char *test_directory
= NULL
, *unlinked_files_directory
= NULL
;
276 get_temporary_directories(&test_directory
, &unlinked_files_directory
);
278 tracker
= fd_tracker_create(unlinked_files_directory
, TRACKER_FD_LIMIT
);
279 LTTNG_ASSERT(tracker
);
281 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &out_fd
, NULL
, 1, noop_open
, &stdout_fd
);
283 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &out_fd
, NULL
, 1, noop_open
, &stdout_fd
);
284 ok(ret
== -EEXIST
, "EEXIST reported on open of an already tracked file descriptor");
286 ret
= fd_tracker_close_unsuspendable_fd(tracker
, &stdout_fd
, 1, noop_close
, NULL
);
289 fd_tracker_destroy(tracker
);
290 ret
= rmdir(test_directory
);
291 ok(ret
== 0, "Test directory is empty");
292 free(test_directory
);
293 free(unlinked_files_directory
);
296 static int open_pipes(void *data
__attribute__((unused
)), int *out_fds
)
299 const unsigned int pipe_count
= TRACKER_FD_LIMIT
/ 2;
301 for (i
= 0; i
< pipe_count
; i
++) {
302 int ret
= pipe(&out_fds
[i
* 2]);
311 static int close_pipes(void *data
__attribute__((unused
)), int *fds
)
316 for (i
= 0; i
< TRACKER_FD_LIMIT
; i
++) {
317 int ret
= close(pipes
[i
]);
327 * Validate that the tracker enforces the open file descriptor limit
328 * when unsuspendable file descriptors are being opened.
330 static void test_unsuspendable_limit(void)
332 struct fd_tracker
*tracker
;
333 int ret
, stdout_fd
= fileno(stdout
), out_fd
;
334 int fds
[TRACKER_FD_LIMIT
];
335 char *test_directory
= NULL
, *unlinked_files_directory
= NULL
;
337 get_temporary_directories(&test_directory
, &unlinked_files_directory
);
339 /* This test assumes TRACKER_FD_LIMIT is a multiple of 2. */
340 LTTNG_ASSERT((TRACKER_FD_LIMIT
% 2 == 0) && TRACKER_FD_LIMIT
);
342 tracker
= fd_tracker_create(unlinked_files_directory
, TRACKER_FD_LIMIT
);
343 LTTNG_ASSERT(tracker
);
345 ret
= fd_tracker_open_unsuspendable_fd(
346 tracker
, fds
, NULL
, TRACKER_FD_LIMIT
, open_pipes
, NULL
);
348 "File descriptor tracker allowed the user to meet its limit with unsuspendable file descriptors (%d)",
351 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &out_fd
, NULL
, 1, noop_open
, &stdout_fd
);
353 "EMFILE reported when exceeding the file descriptor limit while opening an unsuspendable fd");
355 ret
= fd_tracker_close_unsuspendable_fd(tracker
, fds
, TRACKER_FD_LIMIT
, close_pipes
, NULL
);
358 fd_tracker_destroy(tracker
);
359 ret
= rmdir(test_directory
);
360 ok(ret
== 0, "Test directory is empty");
361 free(test_directory
);
362 free(unlinked_files_directory
);
366 * Validate that the tracker refuses to track two identical unsuspendable
369 static void test_unsuspendable_close_untracked(void)
371 int ret
, stdout_fd
= fileno(stdout
), unknown_fds
[2], out_fd
;
372 struct fd_tracker
*tracker
;
373 char *test_directory
= NULL
, *unlinked_files_directory
= NULL
;
375 get_temporary_directories(&test_directory
, &unlinked_files_directory
);
377 tracker
= fd_tracker_create(unlinked_files_directory
, TRACKER_FD_LIMIT
);
383 ret
= pipe(unknown_fds
);
385 ret
= close(unknown_fds
[0]);
386 LTTNG_ASSERT(ret
== 0);
387 ret
= close(unknown_fds
[1]);
388 LTTNG_ASSERT(ret
== 0);
390 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &out_fd
, NULL
, 1, noop_open
, &stdout_fd
);
393 ret
= fd_tracker_close_unsuspendable_fd(tracker
, unknown_fds
, 1, noop_close
, NULL
);
394 ok(ret
== -EINVAL
, "EINVAL reported on close of an untracked file descriptor");
396 ret
= fd_tracker_close_unsuspendable_fd(tracker
, &stdout_fd
, 1, noop_close
, NULL
);
399 fd_tracker_destroy(tracker
);
400 ret
= rmdir(test_directory
);
401 ok(ret
== 0, "Test directory is empty");
403 free(test_directory
);
404 free(unlinked_files_directory
);
407 static int open_files(struct fd_tracker
*tracker
,
408 struct lttng_directory_handle
*directory
,
410 struct fs_handle
**handles
,
416 for (i
= 0; i
< count
; i
++) {
419 struct fs_handle
*handle
;
420 mode_t mode
= S_IWUSR
| S_IRUSR
;
422 p_ret
= asprintf(&file_path
, "file-%u", i
);
423 LTTNG_ASSERT(p_ret
>= 0);
424 file_paths
[i
] = file_path
;
426 handle
= fd_tracker_open_fs_handle(
427 tracker
, directory
, file_path
, O_RDWR
| O_CREAT
, &mode
);
437 static int open_same_file(struct fd_tracker
*tracker
,
438 struct lttng_directory_handle
*directory
,
441 struct fs_handle
**handles
)
446 for (i
= 0; i
< count
; i
++) {
447 struct fs_handle
*handle
;
448 mode_t mode
= S_IWUSR
| S_IRUSR
;
450 handle
= fd_tracker_open_fs_handle(
451 tracker
, directory
, file
, O_RDWR
| O_CREAT
, &mode
);
461 static int cleanup_files(struct fd_tracker
*tracker
__attribute__((unused
)),
462 const char *dir
__attribute__((unused
)),
464 struct fs_handle
**handles
,
470 for (i
= 0; i
< count
; i
++) {
471 char *file_path
= file_paths
[i
];
476 if (fs_handle_unlink(handles
[i
])) {
477 diag("Failed to unlink fs_handle to file %s", file_path
);
480 if (fs_handle_close(handles
[i
])) {
481 diag("Failed to close fs_handle to file %s", file_path
);
489 static void test_suspendable_limit(void)
492 const int files_to_create
= TRACKER_FD_LIMIT
* 10;
493 struct fd_tracker
*tracker
;
494 char *test_directory
= NULL
, *unlinked_files_directory
= NULL
;
495 char *output_files
[files_to_create
];
496 struct fs_handle
*handles
[files_to_create
];
497 struct lttng_directory_handle
*dir_handle
= NULL
;
498 int dir_handle_fd_count
;
500 memset(output_files
, 0, sizeof(output_files
));
501 memset(handles
, 0, sizeof(handles
));
503 get_temporary_directories(&test_directory
, &unlinked_files_directory
);
505 tracker
= fd_tracker_create(unlinked_files_directory
, TRACKER_FD_LIMIT
);
510 dir_handle
= lttng_directory_handle_create(test_directory
);
511 LTTNG_ASSERT(dir_handle
);
512 dir_handle_fd_count
= !!lttng_directory_handle_uses_fd(dir_handle
);
514 ret
= open_files(tracker
, dir_handle
, files_to_create
, handles
, output_files
);
516 "Created %d files with a limit of %d simultaneously-opened file descriptor",
519 check_fd_count(TRACKER_FD_LIMIT
+ STDIO_FD_COUNT
+ unknown_fds_count
+ dir_handle_fd_count
);
521 ret
= cleanup_files(tracker
, test_directory
, files_to_create
, handles
, output_files
);
522 ok(!ret
, "Close all opened filesystem handles");
523 ret
= rmdir(test_directory
);
524 ok(ret
== 0, "Test directory is empty");
525 fd_tracker_destroy(tracker
);
526 lttng_directory_handle_put(dir_handle
);
528 free(test_directory
);
529 free(unlinked_files_directory
);
532 static void test_mixed_limit(void)
535 const int files_to_create
= TRACKER_FD_LIMIT
;
536 struct fd_tracker
*tracker
;
537 char *test_directory
= NULL
, *unlinked_files_directory
= NULL
;
538 char *output_files
[files_to_create
];
539 struct fs_handle
*handles
[files_to_create
];
540 struct lttng_directory_handle
*dir_handle
= NULL
;
541 int dir_handle_fd_count
;
543 memset(output_files
, 0, sizeof(output_files
));
544 memset(handles
, 0, sizeof(handles
));
546 get_temporary_directories(&test_directory
, &unlinked_files_directory
);
548 tracker
= fd_tracker_create(unlinked_files_directory
, TRACKER_FD_LIMIT
);
553 dir_handle
= lttng_directory_handle_create(test_directory
);
554 LTTNG_ASSERT(dir_handle
);
555 dir_handle_fd_count
= !!lttng_directory_handle_uses_fd(dir_handle
);
557 ret
= open_files(tracker
, dir_handle
, files_to_create
, handles
, output_files
);
559 "Created %d files with a limit of %d simultaneously-opened file descriptor",
562 diag("Check file descriptor count after opening %u files", files_to_create
);
563 check_fd_count(TRACKER_FD_LIMIT
+ STDIO_FD_COUNT
+ unknown_fds_count
+ dir_handle_fd_count
);
566 * Open unsuspendable fds (stdin, stdout, stderr) and verify that the fd
567 * cap is still respected.
569 diag("Check file descriptor count after adding %d unsuspendable fds", STDIO_FD_COUNT
);
570 track_std_fds(tracker
);
571 check_fd_count(TRACKER_FD_LIMIT
+ unknown_fds_count
+ dir_handle_fd_count
);
572 diag("Untrack unsuspendable file descriptors");
573 untrack_std_fds(tracker
);
574 check_fd_count(TRACKER_FD_LIMIT
+ unknown_fds_count
+ dir_handle_fd_count
);
576 ret
= cleanup_files(tracker
, test_directory
, files_to_create
, handles
, output_files
);
577 ok(!ret
, "Close all opened filesystem handles");
578 ret
= rmdir(test_directory
);
579 ok(ret
== 0, "Test directory is empty");
580 fd_tracker_destroy(tracker
);
581 lttng_directory_handle_put(dir_handle
);
583 free(test_directory
);
584 free(unlinked_files_directory
);
588 * Open more files than allowed by the fd tracker's cap and write,
589 * byte-by-byte, and in round-robin, a string. The goal is to force
590 * the fd tracker to suspend and resume the fs_handles often and
591 * verify that the fd cap is always respected.
593 * The content of the files is also verified at the end.
595 static void test_suspendable_restore(void)
598 const int files_to_create
= TRACKER_FD_LIMIT
* 10;
599 struct fd_tracker
*tracker
;
600 char *output_files
[files_to_create
];
601 struct fs_handle
*handles
[files_to_create
];
602 size_t content_index
;
604 bool write_success
= true;
605 bool fd_cap_respected
= true;
606 bool content_ok
= true;
607 struct lttng_directory_handle
*dir_handle
= NULL
;
608 int dir_handle_fd_count
;
609 char *test_directory
= NULL
, *unlinked_files_directory
= NULL
;
611 memset(output_files
, 0, sizeof(output_files
));
612 memset(handles
, 0, sizeof(handles
));
614 get_temporary_directories(&test_directory
, &unlinked_files_directory
);
616 tracker
= fd_tracker_create(unlinked_files_directory
, TRACKER_FD_LIMIT
);
621 dir_handle
= lttng_directory_handle_create(test_directory
);
622 LTTNG_ASSERT(dir_handle
);
623 dir_handle_fd_count
= !!lttng_directory_handle_uses_fd(dir_handle
);
625 ret
= open_files(tracker
, dir_handle
, files_to_create
, handles
, output_files
);
627 "Created %d files with a limit of %d simultaneously-opened file descriptor",
630 diag("Check file descriptor count after opening %u files", files_to_create
);
631 check_fd_count(TRACKER_FD_LIMIT
+ STDIO_FD_COUNT
+ unknown_fds_count
+ dir_handle_fd_count
);
633 for (content_index
= 0; content_index
< sizeof(file_contents
); content_index
++) {
634 for (handle_index
= 0; handle_index
< files_to_create
; handle_index
++) {
636 struct fs_handle
*handle
= handles
[handle_index
];
637 const char *path
= output_files
[handle_index
];
639 fd
= fs_handle_get_fd(handle
);
641 write_success
= false;
642 diag("Failed to restore fs_handle to %s", path
);
647 ret
= write(fd
, file_contents
+ content_index
, 1);
648 } while (ret
< 0 && errno
== EINTR
);
651 write_success
= false;
652 PERROR("write() to %s failed", path
);
656 if (fd_count() > (TRACKER_FD_LIMIT
+ STDIO_FD_COUNT
+ unknown_fds_count
+
657 dir_handle_fd_count
)) {
658 fd_cap_respected
= false;
661 fs_handle_put_fd(handle
);
665 ok(write_success
, "Wrote reference string to %d files", files_to_create
);
666 ok(fd_cap_respected
, "FD tracker enforced the file descriptor cap");
668 /* Validate the contents of the files. */
669 for (handle_index
= 0; handle_index
< files_to_create
; handle_index
++) {
671 const char *path
= output_files
[handle_index
];
672 char read_buf
[sizeof(file_contents
)];
674 size_t to_read
= sizeof(read_buf
);
677 fd
= lttng_directory_handle_open_file(dir_handle
, path
, O_RDONLY
, 0);
678 LTTNG_ASSERT(fd
>= 0);
679 ret
= fstat(fd
, &fd_stat
);
681 if (fd_stat
.st_size
!= sizeof(file_contents
)) {
682 diag("Content size of file %s doesn't match, got %" PRId64
", expected %zu",
684 (int64_t) fd_stat
.st_size
,
685 sizeof(file_contents
));
693 ret
= read(fd
, read_pos
, to_read
);
698 } while (to_read
&& (ret
< 0 && errno
== EINTR
));
701 PERROR("Failed to read file %s", path
);
706 if (strcmp(file_contents
, read_buf
)) {
708 diag("File content doesn't match the expectated string");
714 ok(content_ok
, "Files contain the expected content");
715 ret
= cleanup_files(tracker
, test_directory
, files_to_create
, handles
, output_files
);
716 ok(!ret
, "Close all opened filesystem handles");
717 ret
= rmdir(test_directory
);
718 ok(ret
== 0, "Test directory is empty");
719 fd_tracker_destroy(tracker
);
720 lttng_directory_handle_put(dir_handle
);
722 free(test_directory
);
723 free(unlinked_files_directory
);
726 static void test_unlink(void)
729 struct fd_tracker
*tracker
;
730 const int handles_to_open
= 2;
731 struct fs_handle
*handles
[handles_to_open
];
732 struct fs_handle
*new_handle
= NULL
;
734 struct lttng_directory_handle
*dir_handle
= NULL
;
735 const char file_name
[] = "my_file";
736 char *test_directory
= NULL
, *unlinked_files_directory
= NULL
;
737 char *unlinked_file_zero
= NULL
, *unlinked_file_one
= NULL
;
740 get_temporary_directories(&test_directory
, &unlinked_files_directory
);
741 ret
= asprintf(&unlinked_file_zero
, "%s/%u", unlinked_files_directory
, 0);
742 LTTNG_ASSERT(ret
> 0);
743 ret
= asprintf(&unlinked_file_one
, "%s/%u", unlinked_files_directory
, 1);
744 LTTNG_ASSERT(ret
> 0);
746 tracker
= fd_tracker_create(unlinked_files_directory
, 1);
751 dir_handle
= lttng_directory_handle_create(test_directory
);
752 LTTNG_ASSERT(dir_handle
);
754 /* Open two handles to the same file. */
755 ret
= open_same_file(tracker
, dir_handle
, file_name
, handles_to_open
, handles
);
757 "Successfully opened %i handles to %s/%s",
766 * Unlinking the first handle should cause the file to be renamed
769 ret
= fs_handle_unlink(handles
[0]);
770 ok(!ret
, "Successfully unlinked the first handle to %s/%s", test_directory
, file_name
);
773 * The original file should no longer exist on the file system, and a
774 * new file named '0' should exist.
776 ok(lttng_directory_handle_stat(dir_handle
, file_name
, &statbuf
) == -1 && errno
== ENOENT
,
777 "%s no longer present on file system after unlink",
779 ok(lttng_directory_handle_stat(dir_handle
, unlinked_file_zero
, &statbuf
) == 0,
780 "%s exists on file system after unlink",
784 * It should be possible to use the file descriptors of both handles.
785 * Since only one file descriptor can be opened at once, this should
786 * force the fd_tracker to suspend and restore the handles.
788 fd
= fs_handle_get_fd(handles
[0]);
789 ok(fd
>= 0, "Got fd from first handle");
791 fd
= fs_handle_get_fd(handles
[1]);
792 ok(fd
< 0, "fd tracker does not allow two fds to be used at once");
794 fs_handle_put_fd(handles
[0]);
795 fd
= fs_handle_get_fd(handles
[1]);
796 ok(fd
>= 0, "Got fd from second handle");
797 fs_handle_put_fd(handles
[1]);
799 /* The second unlink should fail with -ENOENT. */
800 ret
= fs_handle_unlink(handles
[1]);
802 "ENOENT is reported when attempting to unlink the second handle to %s/%s",
807 * Opening a new handle to 'my_file' should succeed.
809 ret
= open_same_file(tracker
, dir_handle
, file_name
, 1, &new_handle
);
811 "Successfully opened a new handle to previously unlinked file %s/%s",
814 LTTNG_ASSERT(new_handle
);
817 * Unlinking the new handle should cause the file to be renamed
818 * to '1' since '0' already exists.
820 ret
= fs_handle_unlink(new_handle
);
821 ok(!ret
, "Successfully unlinked the new handle handle to %s/%s", test_directory
, file_name
);
822 ok(stat(unlinked_file_one
, &statbuf
) == 0,
823 "%s exists on file system after unlink",
826 ret
= fs_handle_close(handles
[0]);
827 ok(!ret
, "Successfully closed the first handle");
828 ret
= fs_handle_close(handles
[1]);
829 ok(!ret
, "Successfully closed the second handle");
830 ret
= fs_handle_close(new_handle
);
831 ok(!ret
, "Successfully closed the third handle");
833 ok(lttng_directory_handle_stat(dir_handle
, file_name
, &statbuf
) == -1 && errno
== ENOENT
,
834 "%s no longer present on file system after handle close",
836 ok(lttng_directory_handle_stat(dir_handle
, unlinked_file_zero
, &statbuf
) == -1 &&
838 "%s no longer present on file system after handle close",
840 ok(lttng_directory_handle_stat(dir_handle
, unlinked_file_one
, &statbuf
) == -1 &&
842 "%s no longer present on file system after handle close",
845 ret
= rmdir(test_directory
);
846 ok(ret
== 0, "Test directory is empty");
848 fd_tracker_destroy(tracker
);
849 free(test_directory
);
850 free(unlinked_files_directory
);
851 free(unlinked_file_zero
);
852 free(unlinked_file_one
);
853 lttng_directory_handle_put(dir_handle
);
858 plan_tests(NUM_TESTS
);
859 diag("File descriptor tracker unit tests");
861 rcu_register_thread();
863 unknown_fds_count
= fd_count() - STDIO_FD_COUNT
;
864 LTTNG_ASSERT(unknown_fds_count
>= 0);
866 diag("Unsuspendable - basic");
867 test_unsuspendable_basic();
868 diag("Unsuspendable - callback return values");
869 test_unsuspendable_cb_return();
870 diag("Unsuspendable - duplicate file descriptors");
871 test_unsuspendable_duplicate();
872 diag("Unsuspendable - closing an untracked file descriptor");
873 test_unsuspendable_close_untracked();
874 diag("Unsuspendable - check that file descriptor limit is enforced");
875 test_unsuspendable_limit();
877 diag("Suspendable - check that file descriptor limit is enforced");
878 test_suspendable_limit();
879 diag("Suspendable - restoration test");
880 test_suspendable_restore();
882 diag("Mixed - check that file descriptor limit is enforced");
885 diag("Suspendable - Unlinking test");
889 rcu_unregister_thread();
890 return exit_status();