2 * Copyright (c) - 2018 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by as
6 * published by the Free Software Foundation; only version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <sys/types.h>
32 #include <sys/types.h>
36 #include <common/compat/directory-handle.h>
37 #include <common/error.h>
38 #include <common/fd-tracker/fd-tracker.h>
41 int lttng_opt_quiet
= 1;
42 int lttng_opt_verbose
;
45 /* Number of TAP tests in this file */
47 /* 3 for stdin, stdout, and stderr */
48 #define STDIO_FD_COUNT 3
49 #define TRACKER_FD_LIMIT 50
50 #define TMP_DIR_PATTERN "/tmp/fd-tracker-XXXXXX"
51 #define TEST_UNLINK_DIRECTORY_NAME "unlinked_files"
54 * Count of fds, beyond stdin, stderr, stdout that were open
55 * at the launch of the test. This allows the test to succeed when
56 * run by automake's test runner or valgrind which both open
57 * fds behind our back.
59 int unknown_fds_count
;
61 const char file_contents
[] = "Bacon ipsum dolor amet jerky drumstick sirloin "
62 "strip steak venison boudin filet mignon picanha doner shoulder. "
63 "Strip steak brisket alcatra, venison beef chuck cupim pastrami. "
64 "Landjaeger tri-tip salami leberkas ball tip, ham hock chuck sausage "
65 "flank jerky cupim. Pig bacon chuck pancetta andouille.";
67 void get_temporary_directories(char **_test_directory
, char **_unlink_directory
)
70 char tmp_path_pattern
[] = TMP_DIR_PATTERN
;
73 output_dir
= mkdtemp(tmp_path_pattern
);
75 diag("Failed to create temporary path of the form %s",
80 *_test_directory
= strdup(output_dir
);
81 assert(*_test_directory
);
82 ret
= asprintf(_unlink_directory
, "%s/%s", output_dir
,
83 TEST_UNLINK_DIRECTORY_NAME
);
95 dir
= opendir("/proc/self/fd");
97 perror("# Failed to enumerate /proc/self/fd/ to count the number of used file descriptors");
102 while ((entry
= readdir(dir
)) != NULL
) {
103 if (!strcmp(entry
->d_name
, ".") || !strcmp(entry
->d_name
, "..")) {
108 /* Don't account for the file descriptor opened by opendir(). */
111 perror("# Failed to close test program's self/fd directory file descriptor");
118 void check_fd_count(int expected_count
)
123 ok(count
== expected_count
, "Expected %d open file descriptors (%d are open)",
124 expected_count
, count
);
128 int noop_open(void *data
, int *fds
)
130 *fds
= *((int *) data
);
135 int noop_close(void *data
, int *fds
)
141 void track_std_fds(struct fd_tracker
*tracker
)
144 struct { int fd
; const char *name
; } files
[] = {
145 { .fd
= fileno(stdin
), .name
= "stdin" },
146 { .fd
= fileno(stdout
), .name
= "stdout" },
147 { .fd
= fileno(stderr
), .name
= "stderr" },
150 for (i
= 0; i
< sizeof(files
) / sizeof(*files
); i
++) {
153 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &out_fd
,
154 &files
[i
].name
, 1, noop_open
, &files
[i
].fd
);
155 assert(out_fd
== files
[i
].fd
);
157 ok(ret
== 0, "Track unsuspendable fd %d (%s)", files
[i
].fd
,
163 void untrack_std_fds(struct fd_tracker
*tracker
)
166 struct { int fd
; const char *name
; } files
[] = {
167 { .fd
= fileno(stdin
), .name
= "stdin" },
168 { .fd
= fileno(stdout
), .name
= "stdout" },
169 { .fd
= fileno(stderr
), .name
= "stderr" },
171 unsigned int fds_set_to_minus_1
= 0;
173 for (i
= 0; i
< sizeof(files
) / sizeof(*files
); i
++) {
174 int fd
= files
[i
].fd
;
175 int ret
= fd_tracker_close_unsuspendable_fd(tracker
,
176 &files
[i
].fd
, 1, noop_close
, NULL
);
178 ok(ret
== 0, "Untrack unsuspendable fd %d (%s)", fd
,
180 fds_set_to_minus_1
+= (files
[i
].fd
== -1);
185 * Basic test opening and closing three unsuspendable fds.
188 void test_unsuspendable_basic(void)
191 struct fd_tracker
*tracker
;
192 char *test_directory
= NULL
, *unlinked_files_directory
= NULL
;
194 get_temporary_directories(&test_directory
, &unlinked_files_directory
);
196 tracker
= fd_tracker_create(unlinked_files_directory
, TRACKER_FD_LIMIT
);
197 ok(tracker
, "Created an fd tracker with a limit of %d simulateously opened file descriptors",
203 track_std_fds(tracker
);
204 untrack_std_fds(tracker
);
206 fd_tracker_destroy(tracker
);
207 ret
= rmdir(test_directory
);
208 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 descritptors 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 descritptors (%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");
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
);
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
);
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
);
540 free(test_directory
);
541 free(unlinked_files_directory
);
545 void test_mixed_limit(void)
548 const int files_to_create
= TRACKER_FD_LIMIT
;
549 struct fd_tracker
*tracker
;
550 char *test_directory
= NULL
, *unlinked_files_directory
= NULL
;
551 char *output_files
[files_to_create
];
552 struct fs_handle
*handles
[files_to_create
];
553 struct lttng_directory_handle
*dir_handle
= NULL
;
554 int dir_handle_fd_count
;
556 memset(output_files
, 0, sizeof(output_files
));
557 memset(handles
, 0, sizeof(handles
));
559 get_temporary_directories(&test_directory
, &unlinked_files_directory
);
561 tracker
= fd_tracker_create(unlinked_files_directory
, TRACKER_FD_LIMIT
);
566 dir_handle
= lttng_directory_handle_create(test_directory
);
568 dir_handle_fd_count
= !!lttng_directory_handle_uses_fd(dir_handle
);
570 ret
= open_files(tracker
, dir_handle
, files_to_create
, handles
,
572 ok(!ret
, "Created %d files with a limit of %d simultaneously-opened file descriptor",
573 files_to_create
, TRACKER_FD_LIMIT
);
574 diag("Check file descriptor count after opening %u files", files_to_create
);
575 check_fd_count(TRACKER_FD_LIMIT
+ STDIO_FD_COUNT
+ unknown_fds_count
+
576 dir_handle_fd_count
);
579 * Open unsuspendable fds (stdin, stdout, stderr) and verify that the fd
580 * cap is still respected.
582 diag("Check file descriptor count after adding %d unsuspendable fds",
584 track_std_fds(tracker
);
585 check_fd_count(TRACKER_FD_LIMIT
+ unknown_fds_count
+
586 dir_handle_fd_count
);
587 diag("Untrack unsuspendable file descriptors");
588 untrack_std_fds(tracker
);
589 check_fd_count(TRACKER_FD_LIMIT
+ unknown_fds_count
+
590 dir_handle_fd_count
);
592 ret
= cleanup_files(tracker
, test_directory
, files_to_create
, handles
,
594 ok(!ret
, "Close all opened filesystem handles");
595 ret
= rmdir(test_directory
);
596 ok(ret
== 0, "Test directory is empty");
597 fd_tracker_destroy(tracker
);
598 lttng_directory_handle_put(dir_handle
);
599 free(test_directory
);
600 free(unlinked_files_directory
);
604 * Open more files than allowed by the fd tracker's cap and write,
605 * byte-by-byte, and in round-robin, a string. The goal is to force
606 * the fd tracker to suspend and resume the fs_handles often and
607 * verify that the fd cap is always respected.
609 * The content of the files is also verified at the end.
612 void test_suspendable_restore(void)
615 const int files_to_create
= TRACKER_FD_LIMIT
* 10;
616 struct fd_tracker
*tracker
;
617 char *output_files
[files_to_create
];
618 struct fs_handle
*handles
[files_to_create
];
619 size_t content_index
;
621 bool write_success
= true;
622 bool fd_cap_respected
= true;
623 bool content_ok
= true;
624 struct lttng_directory_handle
*dir_handle
= NULL
;
625 int dir_handle_fd_count
;
626 char *test_directory
= NULL
, *unlinked_files_directory
= NULL
;
628 memset(output_files
, 0, sizeof(output_files
));
629 memset(handles
, 0, sizeof(handles
));
631 get_temporary_directories(&test_directory
, &unlinked_files_directory
);
633 tracker
= fd_tracker_create(unlinked_files_directory
, TRACKER_FD_LIMIT
);
638 dir_handle
= lttng_directory_handle_create(test_directory
);
640 dir_handle_fd_count
= !!lttng_directory_handle_uses_fd(dir_handle
);
642 ret
= open_files(tracker
, dir_handle
, files_to_create
, handles
,
644 ok(!ret
, "Created %d files with a limit of %d simultaneously-opened file descriptor",
645 files_to_create
, TRACKER_FD_LIMIT
);
646 diag("Check file descriptor count after opening %u files", files_to_create
);
647 check_fd_count(TRACKER_FD_LIMIT
+ STDIO_FD_COUNT
+ unknown_fds_count
+
648 dir_handle_fd_count
);
650 for (content_index
= 0; content_index
< sizeof(file_contents
); content_index
++) {
651 for (handle_index
= 0; handle_index
< files_to_create
; handle_index
++) {
653 struct fs_handle
*handle
= handles
[handle_index
];
654 const char *path
= output_files
[handle_index
];
656 fd
= fs_handle_get_fd(handle
);
658 write_success
= false;
659 diag("Failed to restore fs_handle to %s",
665 ret
= write(fd
, file_contents
+ content_index
, 1);
666 } while (ret
< 0 && errno
== EINTR
);
669 write_success
= false;
670 PERROR("write() to %s failed", path
);
674 if (fd_count() > (TRACKER_FD_LIMIT
+ STDIO_FD_COUNT
+
676 dir_handle_fd_count
)) {
677 fd_cap_respected
= false;
680 fs_handle_put_fd(handle
);
684 ok(write_success
, "Wrote reference string to %d files",
686 ok(fd_cap_respected
, "FD tracker enforced the file descriptor cap");
688 /* Validate the contents of the files. */
689 for (handle_index
= 0; handle_index
< files_to_create
; handle_index
++) {
691 const char *path
= output_files
[handle_index
];
692 char read_buf
[sizeof(file_contents
)];
694 size_t to_read
= sizeof(read_buf
);
697 fd
= lttng_directory_handle_open_file(
698 dir_handle
, path
, O_RDONLY
, 0);
700 ret
= fstat(fd
, &fd_stat
);
702 if (fd_stat
.st_size
!= sizeof(file_contents
)) {
703 diag("Content size of file %s doesn't match, got %" PRId64
", expected %zu",
704 path
, (int64_t) fd_stat
.st_size
,
705 sizeof(file_contents
));
713 ret
= read(fd
, read_pos
, to_read
);
718 } while (to_read
&& (ret
< 0 && errno
== EINTR
));
721 PERROR("Failed to read file %s", path
);
726 if (strcmp(file_contents
, read_buf
)) {
728 diag("File content doesn't match the expectated string");
734 ok(content_ok
, "Files contain the expected content");
735 ret
= cleanup_files(tracker
, test_directory
, files_to_create
, handles
,
737 ok(!ret
, "Close all opened filesystem handles");
738 ret
= rmdir(test_directory
);
739 ok(ret
== 0, "Test directory is empty");
740 fd_tracker_destroy(tracker
);
741 lttng_directory_handle_put(dir_handle
);
742 free(test_directory
);
743 free(unlinked_files_directory
);
747 void test_unlink(void)
750 struct fd_tracker
*tracker
;
751 const int handles_to_open
= 2;
752 struct fs_handle
*handles
[handles_to_open
];
753 struct fs_handle
*new_handle
= NULL
;
755 struct lttng_directory_handle
*dir_handle
= NULL
;
756 const char file_name
[] = "my_file";
757 char *test_directory
= NULL
, *unlinked_files_directory
= NULL
;
758 char *unlinked_file_zero
= NULL
, *unlinked_file_one
= NULL
;
761 get_temporary_directories(&test_directory
, &unlinked_files_directory
);
762 ret
= asprintf(&unlinked_file_zero
, "%s/%u", unlinked_files_directory
,
765 ret
= asprintf(&unlinked_file_one
, "%s/%u", unlinked_files_directory
,
769 tracker
= fd_tracker_create(unlinked_files_directory
, 1);
774 dir_handle
= lttng_directory_handle_create(test_directory
);
777 /* Open two handles to the same file. */
778 ret
= open_same_file(tracker
, dir_handle
, file_name
, handles_to_open
,
780 ok(!ret
, "Successfully opened %i handles to %s/%s", handles_to_open
,
781 test_directory
, file_name
);
787 * Unlinking the first handle should cause the file to be renamed
790 ret
= fs_handle_unlink(handles
[0]);
791 ok(!ret
, "Successfully unlinked the first handle to %s/%s",
792 test_directory
, file_name
);
795 * The original file should no longer exist on the file system, and a
796 * new file named '0' should exist.
798 ok(lttng_directory_handle_stat(dir_handle
, file_name
, &statbuf
) == -1 &&
800 "%s no longer present on file system after unlink",
802 ok(lttng_directory_handle_stat(
803 dir_handle
, unlinked_file_zero
, &statbuf
) == 0,
804 "%s exists on file system after unlink",
808 * It should be possible to use the file descriptors of both handles.
809 * Since only one file descriptor can be opened at once, this should
810 * force the fd_tracker to suspend and restore the handles.
812 fd
= fs_handle_get_fd(handles
[0]);
813 ok(fd
>= 0, "Got fd from first handle");
815 fd
= fs_handle_get_fd(handles
[1]);
816 ok (fd
< 0, "fd tracker does not allow two fds to be used at once");
818 fs_handle_put_fd(handles
[0]);
819 fd
= fs_handle_get_fd(handles
[1]);
820 ok(fd
>= 0, "Got fd from second handle");
821 fs_handle_put_fd(handles
[1]);
823 /* The second unlink should fail with -ENOENT. */
824 ret
= fs_handle_unlink(handles
[1]);
826 "ENOENT is reported when attempting to unlink the second handle to %s/%s",
827 test_directory
, file_name
);
830 * Opening a new handle to 'my_file' should succeed.
832 ret
= open_same_file(tracker
, dir_handle
, file_name
, 1, &new_handle
);
833 ok(!ret
, "Successfully opened a new handle to previously unlinked file %s/%s",
834 test_directory
, file_name
);
838 * Unlinking the new handle should cause the file to be renamed
839 * to '1' since '0' already exists.
841 ret
= fs_handle_unlink(new_handle
);
842 ok(!ret
, "Successfully unlinked the new handle handle to %s/%s",
843 test_directory
, file_name
);
844 ok(stat(unlinked_file_one
, &statbuf
) == 0,
845 "%s exists on file system after unlink",
848 ret
= fs_handle_close(handles
[0]);
849 ok(!ret
, "Successfully closed the first handle");
850 ret
= fs_handle_close(handles
[1]);
851 ok(!ret
, "Successfully closed the second handle");
852 ret
= fs_handle_close(new_handle
);
853 ok(!ret
, "Successfully closed the third handle");
855 ok(lttng_directory_handle_stat(dir_handle
, file_name
, &statbuf
) == -1 &&
857 "%s no longer present on file system after handle close",
859 ok(lttng_directory_handle_stat(
860 dir_handle
, unlinked_file_zero
, &statbuf
) == -1 &&
862 "%s no longer present on file system after handle close",
864 ok(lttng_directory_handle_stat(dir_handle
, unlinked_file_one
,
867 "%s no longer present on file system after handle close",
870 ret
= rmdir(test_directory
);
871 ok(ret
== 0, "Test directory is empty");
872 fd_tracker_destroy(tracker
);
873 free(test_directory
);
874 free(unlinked_files_directory
);
875 free(unlinked_file_zero
);
876 free(unlinked_file_one
);
877 lttng_directory_handle_put(dir_handle
);
880 int main(int argc
, char **argv
)
882 plan_tests(NUM_TESTS
);
883 diag("File descriptor tracker unit tests");
885 rcu_register_thread();
887 unknown_fds_count
= fd_count() - STDIO_FD_COUNT
;
888 assert(unknown_fds_count
>= 0);
890 diag("Unsuspendable - basic");
891 test_unsuspendable_basic();
892 diag("Unsuspendable - callback return values");
893 test_unsuspendable_cb_return();
894 diag("Unsuspendable - duplicate file descriptors");
895 test_unsuspendable_duplicate();
896 diag("Unsuspendable - closing an untracked file descriptor");
897 test_unsuspendable_close_untracked();
898 diag("Unsuspendable - check that file descritptor limit is enforced");
899 test_unsuspendable_limit();
901 diag("Suspendable - check that file descritptor limit is enforced");
902 test_suspendable_limit();
903 diag("Suspendable - restoration test");
904 test_suspendable_restore();
906 diag("Mixed - check that file descritptor limit is enforced");
909 diag("Suspendable - Unlinking test");
913 rcu_unregister_thread();
914 return exit_status();