2 * Copyright (C) 2018 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
14 #include <sys/types.h>
20 #include <sys/types.h>
24 #include <common/compat/directory-handle.h>
25 #include <common/compat/errno.h>
26 #include <common/error.h>
27 #include <common/fd-tracker/fd-tracker.h>
30 int lttng_opt_quiet
= 1;
31 int lttng_opt_verbose
;
34 /* Number of TAP tests in this file */
36 /* 3 for stdin, stdout, and stderr */
37 #define STDIO_FD_COUNT 3
38 #define TRACKER_FD_LIMIT 50
39 #define TMP_DIR_PATTERN "/tmp/fd-tracker-XXXXXX"
40 #define TEST_UNLINK_DIRECTORY_NAME "unlinked_files"
43 #define SELF_FD_DIR "/proc/self/fd"
45 /* Most Unices have /dev/fd */
46 #define SELF_FD_DIR "/dev/fd"
50 * Count of fds, beyond stdin, stderr, stdout that were open
51 * at the launch of the test. This allows the test to succeed when
52 * run by automake's test runner or valgrind which both open
53 * fds behind our back.
55 int unknown_fds_count
;
57 const char file_contents
[] = "Bacon ipsum dolor amet jerky drumstick sirloin "
58 "strip steak venison boudin filet mignon picanha doner shoulder. "
59 "Strip steak brisket alcatra, venison beef chuck cupim pastrami. "
60 "Landjaeger tri-tip salami leberkas ball tip, ham hock chuck sausage "
61 "flank jerky cupim. Pig bacon chuck pancetta andouille.";
64 void get_temporary_directories(char **_test_directory
, char **_unlink_directory
)
67 char tmp_path_pattern
[] = TMP_DIR_PATTERN
;
70 output_dir
= mkdtemp(tmp_path_pattern
);
72 diag("Failed to create temporary path of the form %s",
77 *_test_directory
= strdup(output_dir
);
78 LTTNG_ASSERT(*_test_directory
);
79 ret
= asprintf(_unlink_directory
, "%s/%s", output_dir
,
80 TEST_UNLINK_DIRECTORY_NAME
);
93 dir
= opendir(SELF_FD_DIR
);
95 perror("# Failed to enumerate " SELF_FD_DIR
" to count the number of used file descriptors");
100 while ((entry
= readdir(dir
)) != NULL
) {
101 if (!strcmp(entry
->d_name
, ".") || !strcmp(entry
->d_name
, "..")) {
106 /* Don't account for the file descriptor opened by opendir(). */
109 perror("# Failed to close test program's " SELF_FD_DIR
" directory file descriptor");
116 void check_fd_count(int expected_count
)
121 ok(count
== expected_count
, "Expected %d open file descriptors (%d are open)",
122 expected_count
, count
);
126 int noop_open(void *data
, int *fds
)
128 *fds
= *((int *) data
);
133 int noop_close(void *data
, int *fds
)
139 void track_std_fds(struct fd_tracker
*tracker
)
142 struct { int fd
; const char *name
; } files
[] = {
143 { .fd
= fileno(stdin
), .name
= "stdin" },
144 { .fd
= fileno(stdout
), .name
= "stdout" },
145 { .fd
= fileno(stderr
), .name
= "stderr" },
148 for (i
= 0; i
< sizeof(files
) / sizeof(*files
); i
++) {
151 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &out_fd
,
152 &files
[i
].name
, 1, noop_open
, &files
[i
].fd
);
153 LTTNG_ASSERT(out_fd
== files
[i
].fd
);
155 ok(ret
== 0, "Track unsuspendable fd %d (%s)", files
[i
].fd
,
161 void untrack_std_fds(struct fd_tracker
*tracker
)
164 struct { int fd
; const char *name
; } files
[] = {
165 { .fd
= fileno(stdin
), .name
= "stdin" },
166 { .fd
= fileno(stdout
), .name
= "stdout" },
167 { .fd
= fileno(stderr
), .name
= "stderr" },
170 for (i
= 0; i
< sizeof(files
) / sizeof(*files
); i
++) {
171 int fd
= files
[i
].fd
;
172 int ret
= fd_tracker_close_unsuspendable_fd(tracker
,
173 &files
[i
].fd
, 1, noop_close
, NULL
);
175 ok(ret
== 0, "Untrack unsuspendable fd %d (%s)", fd
,
181 * Basic test opening and closing three unsuspendable fds.
184 void test_unsuspendable_basic(void)
187 struct fd_tracker
*tracker
;
188 char *test_directory
= NULL
, *unlinked_files_directory
= NULL
;
190 get_temporary_directories(&test_directory
, &unlinked_files_directory
);
192 tracker
= fd_tracker_create(unlinked_files_directory
, TRACKER_FD_LIMIT
);
193 ok(tracker
, "Created an fd tracker with a limit of %d simulateously opened file descriptors",
199 track_std_fds(tracker
);
200 untrack_std_fds(tracker
);
202 fd_tracker_destroy(tracker
);
203 ret
= rmdir(test_directory
);
204 ok(ret
== 0, "Test directory is empty");
206 free(test_directory
);
207 free(unlinked_files_directory
);
211 int error_open(void *data
, int *fds
)
213 return *((int *) data
);
217 int error_close(void *data
, int *fds
)
219 return *((int *) data
);
223 * Validate that user callback return values are returned to the
224 * caller of the fd tracker.
227 void test_unsuspendable_cb_return(void)
229 int ret
, stdout_fd
= fileno(stdout
), out_fd
= 42;
230 struct fd_tracker
*tracker
;
231 int expected_error
= -ENETDOWN
;
232 char *test_directory
= NULL
, *unlinked_files_directory
= NULL
;
234 get_temporary_directories(&test_directory
, &unlinked_files_directory
);
236 tracker
= fd_tracker_create(test_directory
, TRACKER_FD_LIMIT
);
237 LTTNG_ASSERT(tracker
);
239 /* The error_open callback should fail and return 'expected_error'. */
240 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &out_fd
,
241 NULL
, 1, error_open
, &expected_error
);
242 ok(ret
== expected_error
, "fd_tracker_open_unsuspendable_fd() forwards the user callback's error code");
243 ok(out_fd
== 42, "Output fd parameter is unaffected on error of fd_tracker_open_unsuspendable_fd()");
246 * Track a valid fd since we don't want the tracker to fail with an
247 * invalid fd error for this test.
249 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &out_fd
,
250 NULL
, 1, noop_open
, &stdout_fd
);
251 ok(out_fd
== stdout_fd
, "fd_tracker_open_unsuspendable_fd() sets the output fd parameter to the newly-tracked fd's value");
254 ret
= fd_tracker_close_unsuspendable_fd(tracker
,
255 &stdout_fd
, 1, error_close
, &expected_error
);
256 ok(ret
== expected_error
, "fd_tracker_close_unsuspendable_fd() forwards the user callback's error code");
257 ret
= fd_tracker_close_unsuspendable_fd(tracker
,
258 &stdout_fd
, 1, noop_close
, &expected_error
);
261 fd_tracker_destroy(tracker
);
262 ret
= rmdir(test_directory
);
263 ok(ret
== 0, "Test directory is empty");
264 free(test_directory
);
265 free(unlinked_files_directory
);
269 * Validate that the tracker refuses to track two identical unsuspendable
273 void test_unsuspendable_duplicate(void)
275 int ret
, stdout_fd
= fileno(stdout
), out_fd
;
276 struct fd_tracker
*tracker
;
277 char *test_directory
= NULL
, *unlinked_files_directory
= NULL
;
279 get_temporary_directories(&test_directory
, &unlinked_files_directory
);
281 tracker
= fd_tracker_create(unlinked_files_directory
, TRACKER_FD_LIMIT
);
282 LTTNG_ASSERT(tracker
);
284 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &out_fd
,
285 NULL
, 1, noop_open
, &stdout_fd
);
287 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &out_fd
,
288 NULL
, 1, noop_open
, &stdout_fd
);
289 ok(ret
== -EEXIST
, "EEXIST reported on open of an already tracked file descriptor");
291 ret
= fd_tracker_close_unsuspendable_fd(tracker
,
292 &stdout_fd
, 1, noop_close
, NULL
);
295 fd_tracker_destroy(tracker
);
296 ret
= rmdir(test_directory
);
297 ok(ret
== 0, "Test directory is empty");
298 free(test_directory
);
299 free(unlinked_files_directory
);
303 int open_pipes(void *data
, int *out_fds
)
306 const unsigned int pipe_count
= TRACKER_FD_LIMIT
/ 2;
308 for (i
= 0; i
< pipe_count
; i
++) {
309 int ret
= pipe(&out_fds
[i
* 2]);
319 int close_pipes(void *data
, int *fds
)
324 for (i
= 0; i
< TRACKER_FD_LIMIT
; i
++) {
325 int ret
= close(pipes
[i
]);
335 * Validate that the tracker enforces the open file descriptor limit
336 * when unsuspendable file descriptors are being opened.
339 void test_unsuspendable_limit(void)
341 struct fd_tracker
*tracker
;
342 int ret
, stdout_fd
= fileno(stdout
), out_fd
;
343 int fds
[TRACKER_FD_LIMIT
];
344 char *test_directory
= NULL
, *unlinked_files_directory
= NULL
;
346 get_temporary_directories(&test_directory
, &unlinked_files_directory
);
348 /* This test assumes TRACKER_FD_LIMIT is a multiple of 2. */
349 LTTNG_ASSERT((TRACKER_FD_LIMIT
% 2 == 0) && TRACKER_FD_LIMIT
);
351 tracker
= fd_tracker_create(unlinked_files_directory
, TRACKER_FD_LIMIT
);
352 LTTNG_ASSERT(tracker
);
354 ret
= fd_tracker_open_unsuspendable_fd(tracker
, fds
,
355 NULL
, TRACKER_FD_LIMIT
, open_pipes
, NULL
);
356 ok(ret
== 0, "File descriptor tracker allowed the user to meet its limit with unsuspendable file descriptors (%d)",
359 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &out_fd
,
360 NULL
, 1, noop_open
, &stdout_fd
);
361 ok(ret
== -EMFILE
, "EMFILE reported when exceeding the file descriptor limit while opening an unsuspendable fd");
363 ret
= fd_tracker_close_unsuspendable_fd(tracker
,
364 fds
, TRACKER_FD_LIMIT
, close_pipes
, NULL
);
367 fd_tracker_destroy(tracker
);
368 ret
= rmdir(test_directory
);
369 ok(ret
== 0, "Test directory is empty");
370 free(test_directory
);
371 free(unlinked_files_directory
);
375 * Validate that the tracker refuses to track two identical unsuspendable
379 void test_unsuspendable_close_untracked(void)
381 int ret
, stdout_fd
= fileno(stdout
), unknown_fds
[2], out_fd
;
382 struct fd_tracker
*tracker
;
383 char *test_directory
= NULL
, *unlinked_files_directory
= NULL
;
385 get_temporary_directories(&test_directory
, &unlinked_files_directory
);
387 tracker
= fd_tracker_create(unlinked_files_directory
, TRACKER_FD_LIMIT
);
392 ret
= pipe(unknown_fds
);
394 ret
= close(unknown_fds
[0]);
395 LTTNG_ASSERT(ret
== 0);
396 ret
= close(unknown_fds
[1]);
397 LTTNG_ASSERT(ret
== 0);
399 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &out_fd
,
400 NULL
, 1, noop_open
, &stdout_fd
);
403 ret
= fd_tracker_close_unsuspendable_fd(tracker
,
404 unknown_fds
, 1, noop_close
, NULL
);
405 ok(ret
== -EINVAL
, "EINVAL reported on close of an untracked file descriptor");
407 ret
= fd_tracker_close_unsuspendable_fd(tracker
,
408 &stdout_fd
, 1, noop_close
, NULL
);
411 fd_tracker_destroy(tracker
);
412 ret
= rmdir(test_directory
);
413 ok(ret
== 0, "Test directory is empty");
415 free(test_directory
);
416 free(unlinked_files_directory
);
419 static int open_files(struct fd_tracker
*tracker
,
420 struct lttng_directory_handle
*directory
,
422 struct fs_handle
**handles
,
428 for (i
= 0; i
< count
; i
++) {
431 struct fs_handle
*handle
;
432 mode_t mode
= S_IWUSR
| S_IRUSR
;
434 p_ret
= asprintf(&file_path
, "file-%u", i
);
435 LTTNG_ASSERT(p_ret
>= 0);
436 file_paths
[i
] = file_path
;
438 handle
= fd_tracker_open_fs_handle(tracker
, directory
, file_path
,
439 O_RDWR
| O_CREAT
, &mode
);
449 static int open_same_file(struct fd_tracker
*tracker
,
450 struct lttng_directory_handle
*directory
,
453 struct fs_handle
**handles
)
458 for (i
= 0; i
< count
; i
++) {
459 struct fs_handle
*handle
;
460 mode_t mode
= S_IWUSR
| S_IRUSR
;
462 handle
= fd_tracker_open_fs_handle(tracker
, directory
, file
,
463 O_RDWR
| O_CREAT
, &mode
);
474 int cleanup_files(struct fd_tracker
*tracker
, const char *dir
,
475 unsigned int count
, struct fs_handle
**handles
,
481 for (i
= 0; i
< count
; i
++) {
482 char *file_path
= file_paths
[i
];
487 if (fs_handle_unlink(handles
[i
])) {
488 diag("Failed to unlink fs_handle to file %s", file_path
);
491 if (fs_handle_close(handles
[i
])) {
492 diag("Failed to close fs_handle to file %s", file_path
);
501 void test_suspendable_limit(void)
504 const int files_to_create
= TRACKER_FD_LIMIT
* 10;
505 struct fd_tracker
*tracker
;
506 char *test_directory
= NULL
, *unlinked_files_directory
= NULL
;
507 char *output_files
[files_to_create
];
508 struct fs_handle
*handles
[files_to_create
];
509 struct lttng_directory_handle
*dir_handle
= NULL
;
510 int dir_handle_fd_count
;
512 memset(output_files
, 0, sizeof(output_files
));
513 memset(handles
, 0, sizeof(handles
));
515 get_temporary_directories(&test_directory
, &unlinked_files_directory
);
517 tracker
= fd_tracker_create(unlinked_files_directory
, TRACKER_FD_LIMIT
);
522 dir_handle
= lttng_directory_handle_create(test_directory
);
523 LTTNG_ASSERT(dir_handle
);
524 dir_handle_fd_count
= !!lttng_directory_handle_uses_fd(dir_handle
);
526 ret
= open_files(tracker
, dir_handle
, files_to_create
, handles
,
528 ok(!ret
, "Created %d files with a limit of %d simultaneously-opened file descriptor",
529 files_to_create
, TRACKER_FD_LIMIT
);
530 check_fd_count(TRACKER_FD_LIMIT
+ STDIO_FD_COUNT
+ unknown_fds_count
+
531 dir_handle_fd_count
);
533 ret
= cleanup_files(tracker
, test_directory
, files_to_create
, handles
,
535 ok(!ret
, "Close all opened filesystem handles");
536 ret
= rmdir(test_directory
);
537 ok(ret
== 0, "Test directory is empty");
538 fd_tracker_destroy(tracker
);
539 lttng_directory_handle_put(dir_handle
);
541 free(test_directory
);
542 free(unlinked_files_directory
);
546 void test_mixed_limit(void)
549 const int files_to_create
= TRACKER_FD_LIMIT
;
550 struct fd_tracker
*tracker
;
551 char *test_directory
= NULL
, *unlinked_files_directory
= NULL
;
552 char *output_files
[files_to_create
];
553 struct fs_handle
*handles
[files_to_create
];
554 struct lttng_directory_handle
*dir_handle
= NULL
;
555 int dir_handle_fd_count
;
557 memset(output_files
, 0, sizeof(output_files
));
558 memset(handles
, 0, sizeof(handles
));
560 get_temporary_directories(&test_directory
, &unlinked_files_directory
);
562 tracker
= fd_tracker_create(unlinked_files_directory
, TRACKER_FD_LIMIT
);
567 dir_handle
= lttng_directory_handle_create(test_directory
);
568 LTTNG_ASSERT(dir_handle
);
569 dir_handle_fd_count
= !!lttng_directory_handle_uses_fd(dir_handle
);
571 ret
= open_files(tracker
, dir_handle
, files_to_create
, handles
,
573 ok(!ret
, "Created %d files with a limit of %d simultaneously-opened file descriptor",
574 files_to_create
, TRACKER_FD_LIMIT
);
575 diag("Check file descriptor count after opening %u files", files_to_create
);
576 check_fd_count(TRACKER_FD_LIMIT
+ STDIO_FD_COUNT
+ unknown_fds_count
+
577 dir_handle_fd_count
);
580 * Open unsuspendable fds (stdin, stdout, stderr) and verify that the fd
581 * cap is still respected.
583 diag("Check file descriptor count after adding %d unsuspendable fds",
585 track_std_fds(tracker
);
586 check_fd_count(TRACKER_FD_LIMIT
+ unknown_fds_count
+
587 dir_handle_fd_count
);
588 diag("Untrack unsuspendable file descriptors");
589 untrack_std_fds(tracker
);
590 check_fd_count(TRACKER_FD_LIMIT
+ unknown_fds_count
+
591 dir_handle_fd_count
);
593 ret
= cleanup_files(tracker
, test_directory
, files_to_create
, handles
,
595 ok(!ret
, "Close all opened filesystem handles");
596 ret
= rmdir(test_directory
);
597 ok(ret
== 0, "Test directory is empty");
598 fd_tracker_destroy(tracker
);
599 lttng_directory_handle_put(dir_handle
);
601 free(test_directory
);
602 free(unlinked_files_directory
);
606 * Open more files than allowed by the fd tracker's cap and write,
607 * byte-by-byte, and in round-robin, a string. The goal is to force
608 * the fd tracker to suspend and resume the fs_handles often and
609 * verify that the fd cap is always respected.
611 * The content of the files is also verified at the end.
614 void test_suspendable_restore(void)
617 const int files_to_create
= TRACKER_FD_LIMIT
* 10;
618 struct fd_tracker
*tracker
;
619 char *output_files
[files_to_create
];
620 struct fs_handle
*handles
[files_to_create
];
621 size_t content_index
;
623 bool write_success
= true;
624 bool fd_cap_respected
= true;
625 bool content_ok
= true;
626 struct lttng_directory_handle
*dir_handle
= NULL
;
627 int dir_handle_fd_count
;
628 char *test_directory
= NULL
, *unlinked_files_directory
= NULL
;
630 memset(output_files
, 0, sizeof(output_files
));
631 memset(handles
, 0, sizeof(handles
));
633 get_temporary_directories(&test_directory
, &unlinked_files_directory
);
635 tracker
= fd_tracker_create(unlinked_files_directory
, TRACKER_FD_LIMIT
);
640 dir_handle
= lttng_directory_handle_create(test_directory
);
641 LTTNG_ASSERT(dir_handle
);
642 dir_handle_fd_count
= !!lttng_directory_handle_uses_fd(dir_handle
);
644 ret
= open_files(tracker
, dir_handle
, files_to_create
, handles
,
646 ok(!ret
, "Created %d files with a limit of %d simultaneously-opened file descriptor",
647 files_to_create
, TRACKER_FD_LIMIT
);
648 diag("Check file descriptor count after opening %u files", files_to_create
);
649 check_fd_count(TRACKER_FD_LIMIT
+ STDIO_FD_COUNT
+ unknown_fds_count
+
650 dir_handle_fd_count
);
652 for (content_index
= 0; content_index
< sizeof(file_contents
); content_index
++) {
653 for (handle_index
= 0; handle_index
< files_to_create
; handle_index
++) {
655 struct fs_handle
*handle
= handles
[handle_index
];
656 const char *path
= output_files
[handle_index
];
658 fd
= fs_handle_get_fd(handle
);
660 write_success
= false;
661 diag("Failed to restore fs_handle to %s",
667 ret
= write(fd
, file_contents
+ content_index
, 1);
668 } while (ret
< 0 && errno
== EINTR
);
671 write_success
= false;
672 PERROR("write() to %s failed", path
);
676 if (fd_count() > (TRACKER_FD_LIMIT
+ STDIO_FD_COUNT
+
678 dir_handle_fd_count
)) {
679 fd_cap_respected
= false;
682 fs_handle_put_fd(handle
);
686 ok(write_success
, "Wrote reference string to %d files",
688 ok(fd_cap_respected
, "FD tracker enforced the file descriptor cap");
690 /* Validate the contents of the files. */
691 for (handle_index
= 0; handle_index
< files_to_create
; handle_index
++) {
693 const char *path
= output_files
[handle_index
];
694 char read_buf
[sizeof(file_contents
)];
696 size_t to_read
= sizeof(read_buf
);
699 fd
= lttng_directory_handle_open_file(
700 dir_handle
, path
, O_RDONLY
, 0);
701 LTTNG_ASSERT(fd
>= 0);
702 ret
= fstat(fd
, &fd_stat
);
704 if (fd_stat
.st_size
!= sizeof(file_contents
)) {
705 diag("Content size of file %s doesn't match, got %" PRId64
", expected %zu",
706 path
, (int64_t) fd_stat
.st_size
,
707 sizeof(file_contents
));
715 ret
= read(fd
, read_pos
, to_read
);
720 } while (to_read
&& (ret
< 0 && errno
== EINTR
));
723 PERROR("Failed to read file %s", path
);
728 if (strcmp(file_contents
, read_buf
)) {
730 diag("File content doesn't match the expectated string");
736 ok(content_ok
, "Files contain the expected content");
737 ret
= cleanup_files(tracker
, test_directory
, files_to_create
, handles
,
739 ok(!ret
, "Close all opened filesystem handles");
740 ret
= rmdir(test_directory
);
741 ok(ret
== 0, "Test directory is empty");
742 fd_tracker_destroy(tracker
);
743 lttng_directory_handle_put(dir_handle
);
745 free(test_directory
);
746 free(unlinked_files_directory
);
750 void test_unlink(void)
753 struct fd_tracker
*tracker
;
754 const int handles_to_open
= 2;
755 struct fs_handle
*handles
[handles_to_open
];
756 struct fs_handle
*new_handle
= NULL
;
758 struct lttng_directory_handle
*dir_handle
= NULL
;
759 const char file_name
[] = "my_file";
760 char *test_directory
= NULL
, *unlinked_files_directory
= NULL
;
761 char *unlinked_file_zero
= NULL
, *unlinked_file_one
= NULL
;
764 get_temporary_directories(&test_directory
, &unlinked_files_directory
);
765 ret
= asprintf(&unlinked_file_zero
, "%s/%u", unlinked_files_directory
,
767 LTTNG_ASSERT(ret
> 0);
768 ret
= asprintf(&unlinked_file_one
, "%s/%u", unlinked_files_directory
,
770 LTTNG_ASSERT(ret
> 0);
772 tracker
= fd_tracker_create(unlinked_files_directory
, 1);
777 dir_handle
= lttng_directory_handle_create(test_directory
);
778 LTTNG_ASSERT(dir_handle
);
780 /* Open two handles to the same file. */
781 ret
= open_same_file(tracker
, dir_handle
, file_name
, handles_to_open
,
783 ok(!ret
, "Successfully opened %i handles to %s/%s", handles_to_open
,
784 test_directory
, file_name
);
790 * Unlinking the first handle should cause the file to be renamed
793 ret
= fs_handle_unlink(handles
[0]);
794 ok(!ret
, "Successfully unlinked the first handle to %s/%s",
795 test_directory
, file_name
);
798 * The original file should no longer exist on the file system, and a
799 * new file named '0' should exist.
801 ok(lttng_directory_handle_stat(dir_handle
, file_name
, &statbuf
) == -1 &&
803 "%s no longer present on file system after unlink",
805 ok(lttng_directory_handle_stat(
806 dir_handle
, unlinked_file_zero
, &statbuf
) == 0,
807 "%s exists on file system after unlink",
811 * It should be possible to use the file descriptors of both handles.
812 * Since only one file descriptor can be opened at once, this should
813 * force the fd_tracker to suspend and restore the handles.
815 fd
= fs_handle_get_fd(handles
[0]);
816 ok(fd
>= 0, "Got fd from first handle");
818 fd
= fs_handle_get_fd(handles
[1]);
819 ok (fd
< 0, "fd tracker does not allow two fds to be used at once");
821 fs_handle_put_fd(handles
[0]);
822 fd
= fs_handle_get_fd(handles
[1]);
823 ok(fd
>= 0, "Got fd from second handle");
824 fs_handle_put_fd(handles
[1]);
826 /* The second unlink should fail with -ENOENT. */
827 ret
= fs_handle_unlink(handles
[1]);
829 "ENOENT is reported when attempting to unlink the second handle to %s/%s",
830 test_directory
, file_name
);
833 * Opening a new handle to 'my_file' should succeed.
835 ret
= open_same_file(tracker
, dir_handle
, file_name
, 1, &new_handle
);
836 ok(!ret
, "Successfully opened a new handle to previously unlinked file %s/%s",
837 test_directory
, file_name
);
838 LTTNG_ASSERT(new_handle
);
841 * Unlinking the new handle should cause the file to be renamed
842 * to '1' since '0' already exists.
844 ret
= fs_handle_unlink(new_handle
);
845 ok(!ret
, "Successfully unlinked the new handle handle to %s/%s",
846 test_directory
, file_name
);
847 ok(stat(unlinked_file_one
, &statbuf
) == 0,
848 "%s exists on file system after unlink",
851 ret
= fs_handle_close(handles
[0]);
852 ok(!ret
, "Successfully closed the first handle");
853 ret
= fs_handle_close(handles
[1]);
854 ok(!ret
, "Successfully closed the second handle");
855 ret
= fs_handle_close(new_handle
);
856 ok(!ret
, "Successfully closed the third handle");
858 ok(lttng_directory_handle_stat(dir_handle
, file_name
, &statbuf
) == -1 &&
860 "%s no longer present on file system after handle close",
862 ok(lttng_directory_handle_stat(
863 dir_handle
, unlinked_file_zero
, &statbuf
) == -1 &&
865 "%s no longer present on file system after handle close",
867 ok(lttng_directory_handle_stat(dir_handle
, unlinked_file_one
,
870 "%s no longer present on file system after handle close",
873 ret
= rmdir(test_directory
);
874 ok(ret
== 0, "Test directory is empty");
876 fd_tracker_destroy(tracker
);
877 free(test_directory
);
878 free(unlinked_files_directory
);
879 free(unlinked_file_zero
);
880 free(unlinked_file_one
);
881 lttng_directory_handle_put(dir_handle
);
884 int main(int argc
, char **argv
)
886 plan_tests(NUM_TESTS
);
887 diag("File descriptor tracker unit tests");
889 rcu_register_thread();
891 unknown_fds_count
= fd_count() - STDIO_FD_COUNT
;
892 LTTNG_ASSERT(unknown_fds_count
>= 0);
894 diag("Unsuspendable - basic");
895 test_unsuspendable_basic();
896 diag("Unsuspendable - callback return values");
897 test_unsuspendable_cb_return();
898 diag("Unsuspendable - duplicate file descriptors");
899 test_unsuspendable_duplicate();
900 diag("Unsuspendable - closing an untracked file descriptor");
901 test_unsuspendable_close_untracked();
902 diag("Unsuspendable - check that file descriptor limit is enforced");
903 test_unsuspendable_limit();
905 diag("Suspendable - check that file descriptor limit is enforced");
906 test_suspendable_limit();
907 diag("Suspendable - restoration test");
908 test_suspendable_restore();
910 diag("Mixed - check that file descriptor limit is enforced");
913 diag("Suspendable - Unlinking test");
917 rcu_unregister_thread();
918 return exit_status();