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/fd-tracker/fd-tracker.h>
37 #include <common/error.h>
40 int lttng_opt_quiet
= 1;
41 int lttng_opt_verbose
;
44 /* Number of TAP tests in this file */
46 /* 3 for stdin, stdout, and stderr */
47 #define STDIO_FD_COUNT 3
48 #define TRACKER_FD_LIMIT 50
49 #define TMP_DIR_PATTERN "/tmp/fd-tracker-XXXXXX"
52 * Count of fds, beyond stdin, stderr, stdout that were open
53 * at the launch of the test. This allows the test to succeed when
54 * run by automake's test runner or valgrind which both open
55 * fds behind our back.
57 int unknown_fds_count
;
59 const char file_contents
[] = "Bacon ipsum dolor amet jerky drumstick sirloin "
60 "strip steak venison boudin filet mignon picanha doner shoulder. "
61 "Strip steak brisket alcatra, venison beef chuck cupim pastrami. "
62 "Landjaeger tri-tip salami leberkas ball tip, ham hock chuck sausage "
63 "flank jerky cupim. Pig bacon chuck pancetta andouille.";
71 dir
= opendir("/proc/self/fd");
73 perror("# Failed to enumerate /proc/self/fd/ to count the number of used file descriptors");
78 while ((entry
= readdir(dir
)) != NULL
) {
79 if (!strcmp(entry
->d_name
, ".") || !strcmp(entry
->d_name
, "..")) {
84 /* Don't account for the file descriptor opened by opendir(). */
87 perror("# Failed to close test program's self/fd directory file descriptor");
94 void check_fd_count(int expected_count
)
99 ok(count
== expected_count
, "Expected %d open file descriptors (%d are open)",
100 expected_count
, count
);
104 int noop_open(void *data
, int *fds
)
106 *fds
= *((int *) data
);
111 int noop_close(void *data
, int *fds
)
117 void track_std_fds(struct fd_tracker
*tracker
)
120 struct { int fd
; const char *name
; } files
[] = {
121 { .fd
= fileno(stdin
), .name
= "stdin" },
122 { .fd
= fileno(stdout
), .name
= "stdout" },
123 { .fd
= fileno(stderr
), .name
= "stderr" },
126 for (i
= 0; i
< sizeof(files
) / sizeof(*files
); i
++) {
129 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &out_fd
,
130 &files
[i
].name
, 1, noop_open
, &files
[i
].fd
);
131 assert(out_fd
== files
[i
].fd
);
133 ok(ret
== 0, "Track unsuspendable fd %d (%s)", files
[i
].fd
,
139 void untrack_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" },
147 unsigned int fds_set_to_minus_1
= 0;
149 for (i
= 0; i
< sizeof(files
) / sizeof(*files
); i
++) {
150 int fd
= files
[i
].fd
;
151 int ret
= fd_tracker_close_unsuspendable_fd(tracker
,
152 &files
[i
].fd
, 1, noop_close
, NULL
);
154 ok(ret
== 0, "Untrack unsuspendable fd %d (%s)", fd
,
156 fds_set_to_minus_1
+= (files
[i
].fd
== -1);
161 * Basic test opening and closing three unsuspendable fds.
164 void test_unsuspendable_basic(void)
166 struct fd_tracker
*tracker
;
168 tracker
= fd_tracker_create(TRACKER_FD_LIMIT
);
169 ok(tracker
, "Created an fd tracker with a limit of %d simulateously opened file descriptors",
175 track_std_fds(tracker
);
176 untrack_std_fds(tracker
);
178 fd_tracker_destroy(tracker
);
182 int error_open(void *data
, int *fds
)
184 return *((int *) data
);
188 int error_close(void *data
, int *fds
)
190 return *((int *) data
);
194 * Validate that user callback return values are returned to the
195 * caller of the fd tracker.
198 void test_unsuspendable_cb_return(void)
200 int ret
, stdout_fd
= fileno(stdout
), out_fd
= 42;
201 struct fd_tracker
*tracker
;
202 int expected_error
= -ENETDOWN
;
204 tracker
= fd_tracker_create(TRACKER_FD_LIMIT
);
207 /* The error_open callback should fail and return 'expected_error'. */
208 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &out_fd
,
209 NULL
, 1, error_open
, &expected_error
);
210 ok(ret
== expected_error
, "fd_tracker_open_unsuspendable_fd() forwards the user callback's error code");
211 ok(out_fd
== 42, "Output fd parameter is unaffected on error of fd_tracker_open_unsuspendable_fd()");
214 * Track a valid fd since we don't want the tracker to fail with an
215 * invalid fd error for this test.
217 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &out_fd
,
218 NULL
, 1, noop_open
, &stdout_fd
);
219 ok(out_fd
== stdout_fd
, "fd_tracker_open_unsuspendable_fd() sets the output fd parameter to the newly-tracked fd's value");
222 ret
= fd_tracker_close_unsuspendable_fd(tracker
,
223 &stdout_fd
, 1, error_close
, &expected_error
);
224 ok(ret
== expected_error
, "fd_tracker_close_unsuspendable_fd() forwards the user callback's error code");
225 ret
= fd_tracker_close_unsuspendable_fd(tracker
,
226 &stdout_fd
, 1, noop_close
, &expected_error
);
229 fd_tracker_destroy(tracker
);
233 * Validate that the tracker refuses to track two identical unsuspendable
237 void test_unsuspendable_duplicate(void)
239 int ret
, stdout_fd
= fileno(stdout
), out_fd
;
240 struct fd_tracker
*tracker
;
242 tracker
= fd_tracker_create(TRACKER_FD_LIMIT
);
245 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &out_fd
,
246 NULL
, 1, noop_open
, &stdout_fd
);
248 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &out_fd
,
249 NULL
, 1, noop_open
, &stdout_fd
);
250 ok(ret
== -EEXIST
, "EEXIST reported on open of an already tracked file descriptor");
252 ret
= fd_tracker_close_unsuspendable_fd(tracker
,
253 &stdout_fd
, 1, noop_close
, NULL
);
256 fd_tracker_destroy(tracker
);
260 int open_pipes(void *data
, int *out_fds
)
263 const unsigned int pipe_count
= TRACKER_FD_LIMIT
/ 2;
265 for (i
= 0; i
< pipe_count
; i
++) {
266 int ret
= pipe(&out_fds
[i
* 2]);
276 int close_pipes(void *data
, int *fds
)
281 for (i
= 0; i
< TRACKER_FD_LIMIT
; i
++) {
282 int ret
= close(pipes
[i
]);
292 * Validate that the tracker enforces the open file descriptor limit
293 * when unsuspendable file descritptors are being opened.
296 void test_unsuspendable_limit(void)
298 struct fd_tracker
*tracker
;
299 int ret
, stdout_fd
= fileno(stdout
), out_fd
;
300 int fds
[TRACKER_FD_LIMIT
];
302 /* This test assumes TRACKER_FD_LIMIT is a multiple of 2. */
303 assert((TRACKER_FD_LIMIT
% 2 == 0) && TRACKER_FD_LIMIT
);
305 tracker
= fd_tracker_create(TRACKER_FD_LIMIT
);
308 ret
= fd_tracker_open_unsuspendable_fd(tracker
, fds
,
309 NULL
, TRACKER_FD_LIMIT
, open_pipes
, NULL
);
310 ok(ret
== 0, "File descriptor tracker allowed the user to meet its limit with unsuspendable file descritptors (%d)",
313 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &out_fd
,
314 NULL
, 1, noop_open
, &stdout_fd
);
315 ok(ret
== -EMFILE
, "EMFILE reported when exceeding the file descriptor limit while opening an unsuspendable fd");
317 ret
= fd_tracker_close_unsuspendable_fd(tracker
,
318 fds
, TRACKER_FD_LIMIT
, close_pipes
, NULL
);
321 fd_tracker_destroy(tracker
);
325 * Validate that the tracker refuses to track two identical unsuspendable
329 void test_unsuspendable_close_untracked(void)
331 int ret
, stdout_fd
= fileno(stdout
), unknown_fds
[2], out_fd
;
332 struct fd_tracker
*tracker
;
334 tracker
= fd_tracker_create(TRACKER_FD_LIMIT
);
339 ret
= pipe(unknown_fds
);
341 assert(close(unknown_fds
[0]) == 0);
342 assert(close(unknown_fds
[1]) == 0);
344 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &out_fd
,
345 NULL
, 1, noop_open
, &stdout_fd
);
348 ret
= fd_tracker_close_unsuspendable_fd(tracker
,
349 unknown_fds
, 1, noop_close
, NULL
);
350 ok(ret
== -EINVAL
, "EINVAL reported on close of an untracked file descriptor");
352 ret
= fd_tracker_close_unsuspendable_fd(tracker
,
353 &stdout_fd
, 1, noop_close
, NULL
);
356 fd_tracker_destroy(tracker
);
360 int open_files(struct fd_tracker
*tracker
, const char *dir
, unsigned int count
,
361 struct fs_handle
**handles
, char **file_paths
)
366 for (i
= 0; i
< count
; i
++) {
369 struct fs_handle
*handle
;
370 mode_t mode
= S_IWUSR
| S_IRUSR
;
372 p_ret
= asprintf(&file_path
, "%s/file-%u", dir
, i
);
374 file_paths
[i
] = file_path
;
376 handle
= fd_tracker_open_fs_handle(tracker
, file_path
,
377 O_RDWR
| O_CREAT
, &mode
);
388 int open_same_file(struct fd_tracker
*tracker
, const char *file_path
,
389 unsigned int count
, struct fs_handle
**handles
)
394 for (i
= 0; i
< count
; i
++) {
395 struct fs_handle
*handle
;
396 mode_t mode
= S_IWUSR
| S_IRUSR
;
398 handle
= fd_tracker_open_fs_handle(tracker
, file_path
,
399 O_RDWR
| O_CREAT
, &mode
);
410 int cleanup_files(struct fd_tracker
*tracker
, const char *dir
,
411 unsigned int count
, struct fs_handle
**handles
,
417 for (i
= 0; i
< count
; i
++) {
418 char *file_path
= file_paths
[i
];
423 (void) unlink(file_path
);
425 if (fs_handle_close(handles
[i
])) {
433 void test_suspendable_limit(void)
436 const int files_to_create
= TRACKER_FD_LIMIT
* 10;
437 struct fd_tracker
*tracker
;
438 char tmp_path_pattern
[] = TMP_DIR_PATTERN
;
439 const char *output_dir
;
440 char *output_files
[files_to_create
];
441 struct fs_handle
*handles
[files_to_create
];
443 memset(output_files
, 0, sizeof(output_files
));
444 memset(handles
, 0, sizeof(handles
));
446 tracker
= fd_tracker_create(TRACKER_FD_LIMIT
);
451 output_dir
= mkdtemp(tmp_path_pattern
);
453 diag("Failed to create temporary path of the form %s",
458 ret
= open_files(tracker
, output_dir
, files_to_create
, handles
,
460 ok(!ret
, "Created %d files with a limit of %d simultaneously-opened file descriptor",
461 files_to_create
, TRACKER_FD_LIMIT
);
462 check_fd_count(TRACKER_FD_LIMIT
+ STDIO_FD_COUNT
+ unknown_fds_count
);
464 ret
= cleanup_files(tracker
, output_dir
, files_to_create
, handles
,
466 ok(!ret
, "Close all opened filesystem handles");
467 (void) rmdir(output_dir
);
468 fd_tracker_destroy(tracker
);
472 void test_mixed_limit(void)
475 const int files_to_create
= TRACKER_FD_LIMIT
;
476 struct fd_tracker
*tracker
;
477 char tmp_path_pattern
[] = TMP_DIR_PATTERN
;
478 const char *output_dir
;
479 char *output_files
[files_to_create
];
480 struct fs_handle
*handles
[files_to_create
];
482 memset(output_files
, 0, sizeof(output_files
));
483 memset(handles
, 0, sizeof(handles
));
485 tracker
= fd_tracker_create(TRACKER_FD_LIMIT
);
490 output_dir
= mkdtemp(tmp_path_pattern
);
492 diag("Failed to create temporary path of the form %s",
497 ret
= open_files(tracker
, output_dir
, files_to_create
, handles
,
499 ok(!ret
, "Created %d files with a limit of %d simultaneously-opened file descriptor",
500 files_to_create
, TRACKER_FD_LIMIT
);
501 diag("Check file descriptor count after opening %u files", files_to_create
);
502 check_fd_count(TRACKER_FD_LIMIT
+ STDIO_FD_COUNT
+ unknown_fds_count
);
505 * Open unsuspendable fds (stdin, stdout, stderr) and verify that the fd
506 * cap is still respected.
508 diag("Check file descriptor count after adding %d unsuspendable fds",
510 track_std_fds(tracker
);
511 check_fd_count(TRACKER_FD_LIMIT
+ unknown_fds_count
);
512 diag("Untrack unsuspendable file descriptors");
513 untrack_std_fds(tracker
);
514 check_fd_count(TRACKER_FD_LIMIT
+ unknown_fds_count
);
516 ret
= cleanup_files(tracker
, output_dir
, files_to_create
, handles
,
518 ok(!ret
, "Close all opened filesystem handles");
519 (void) rmdir(output_dir
);
520 fd_tracker_destroy(tracker
);
524 * Open more files than allowed by the fd tracker's cap and write,
525 * byte-by-byte, and in round-robin, a string. The goal is to force
526 * the fd tracker to suspend and resume the fs_handles often and
527 * verify that the fd cap is always respected.
529 * The content of the files is also verified at the end.
532 void test_suspendable_restore(void)
535 const int files_to_create
= TRACKER_FD_LIMIT
* 10;
536 struct fd_tracker
*tracker
;
537 char tmp_path_pattern
[] = TMP_DIR_PATTERN
;
538 const char *output_dir
;
539 char *output_files
[files_to_create
];
540 struct fs_handle
*handles
[files_to_create
];
541 size_t content_index
;
543 bool write_success
= true;
544 bool fd_cap_respected
= true;
545 bool content_ok
= true;
547 memset(output_files
, 0, sizeof(output_files
));
548 memset(handles
, 0, sizeof(handles
));
550 tracker
= fd_tracker_create(TRACKER_FD_LIMIT
);
555 output_dir
= mkdtemp(tmp_path_pattern
);
557 diag("Failed to create temporary path of the form %s",
562 ret
= open_files(tracker
, output_dir
, files_to_create
, handles
,
564 ok(!ret
, "Created %d files with a limit of %d simultaneously-opened file descriptor",
565 files_to_create
, TRACKER_FD_LIMIT
);
566 diag("Check file descriptor count after opening %u files", files_to_create
);
567 check_fd_count(TRACKER_FD_LIMIT
+ STDIO_FD_COUNT
+ unknown_fds_count
);
569 for (content_index
= 0; content_index
< sizeof(file_contents
); content_index
++) {
570 for (handle_index
= 0; handle_index
< files_to_create
; handle_index
++) {
572 struct fs_handle
*handle
= handles
[handle_index
];
573 const char *path
= output_files
[handle_index
];
575 fd
= fs_handle_get_fd(handle
);
577 write_success
= false;
578 diag("Failed to restore fs_handle to %s",
584 ret
= write(fd
, file_contents
+ content_index
, 1);
585 } while (ret
< 0 && errno
== EINTR
);
588 write_success
= false;
589 PERROR("write() to %s failed", path
);
594 (TRACKER_FD_LIMIT
+ STDIO_FD_COUNT
+
595 unknown_fds_count
)) {
596 fd_cap_respected
= false;
599 fs_handle_put_fd(handle
);
603 ok(write_success
, "Wrote reference string to %d files",
605 ok(fd_cap_respected
, "FD tracker enforced the file descriptor cap");
607 /* Validate the contents of the files. */
608 for (handle_index
= 0; handle_index
< files_to_create
; handle_index
++) {
610 const char *path
= output_files
[handle_index
];
611 char read_buf
[sizeof(file_contents
)];
613 size_t to_read
= sizeof(read_buf
);
616 fd
= open(path
, O_RDONLY
);
618 ret
= fstat(fd
, &fd_stat
);
620 if (fd_stat
.st_size
!= sizeof(file_contents
)) {
621 diag("Content size of file %s doesn't match, got %" PRId64
", expected %zu",
622 path
, (int64_t) fd_stat
.st_size
,
623 sizeof(file_contents
));
631 ret
= read(fd
, read_pos
, to_read
);
636 } while (to_read
&& (ret
< 0 && errno
== EINTR
));
639 PERROR("Failed to read file %s", path
);
644 if (strcmp(file_contents
, read_buf
)) {
646 diag("File content doesn't match the expectated string");
652 ok(content_ok
, "Files contain the expected content");
653 ret
= cleanup_files(tracker
, output_dir
, files_to_create
, handles
,
655 ok(!ret
, "Close all opened filesystem handles");
656 (void) rmdir(output_dir
);
657 fd_tracker_destroy(tracker
);
661 void test_unlink(void)
664 struct fd_tracker
*tracker
;
665 const int handles_to_open
= 2;
666 char tmp_path_pattern
[] = TMP_DIR_PATTERN
;
667 const char *output_dir
;
668 struct fs_handle
*handles
[handles_to_open
];
669 struct fs_handle
*new_handle
= NULL
;
671 char *unlinked_file_path
;
672 char *unlinked_file_path_suffix
;
675 tracker
= fd_tracker_create(1);
679 output_dir
= mkdtemp(tmp_path_pattern
);
681 diag("Failed to create temporary path of the form %s",
685 ret
= asprintf(&file_path
, "%s/my_file", output_dir
);
687 diag("Failed to allocate path string");
690 ret
= asprintf(&unlinked_file_path
, "%s-deleted", file_path
);
692 diag("Failed to allocate path string");
695 ret
= asprintf(&unlinked_file_path_suffix
, "%s-1", unlinked_file_path
);
697 diag("Failed to allocate path string");
701 /* Open two handles to the same file. */
702 ret
= open_same_file(tracker
, file_path
, handles_to_open
, handles
);
703 ok(!ret
, "Successfully %i handles to %s", handles_to_open
);
709 * Unlinking the first handle should cause the file to be renamed
710 * to 'my_file-deleted'.
712 ret
= fs_handle_unlink(handles
[0]);
713 ok(!ret
, "Successfully unlinked the first handle to %s", file_path
);
716 * The original file should no longer exist on the file system, and a
717 * new file, with the '-deleted' suffix should exist.
719 ok(stat(file_path
, &statbuf
) == -1 && errno
== ENOENT
, "%s no longer present on file system after unlink", file_path
);
720 ok(stat(unlinked_file_path
, &statbuf
) == 0, "%s exists on file system after unlink", unlinked_file_path
);
722 /* The second unlink should fail with -ENOENT. */
723 ret
= fs_handle_unlink(handles
[1]);
724 ok(ret
== -ENOENT
, "ENOENT is reported when attempting to unlink the second handle to %s", file_path
);
727 * Opening a new handle to 'my_file' should succeed.
729 ret
= open_same_file(tracker
, file_path
, 1, &new_handle
);
730 ok(!ret
, "Successfully opened a new handle to previously unlinked file %s", file_path
);
734 * Unlinking the new handle should cause the file to be renamed
735 * to 'my_file-deleted-1' since 'my_file-deleted' already exists.
737 ret
= fs_handle_unlink(new_handle
);
738 ok(!ret
, "Successfully unlinked the new handle handle to %s", file_path
);
739 ok(stat(unlinked_file_path_suffix
, &statbuf
) == 0, "%s exists on file system after unlink", unlinked_file_path_suffix
);
741 ret
= fs_handle_close(handles
[0]);
742 ok(!ret
, "Successfully closed the first handle");
743 ret
= fs_handle_close(handles
[1]);
744 ok(!ret
, "Successfully closed the second handle");
745 ret
= fs_handle_close(new_handle
);
746 ok(!ret
, "Successfully closed the third handle");
748 ok(stat(file_path
, &statbuf
) == -1 && errno
== ENOENT
, "%s no longer present on file system after handle close", file_path
);
749 ok(stat(unlinked_file_path
, &statbuf
) == -1 && errno
== ENOENT
, "%s no longer present on file system after handle close", unlinked_file_path
);
750 ok(stat(unlinked_file_path_suffix
, &statbuf
) == -1 && errno
== ENOENT
, "%s no longer present on file system after handle close", unlinked_file_path_suffix
);
752 (void) rmdir(output_dir
);
753 fd_tracker_destroy(tracker
);
756 int main(int argc
, char **argv
)
758 plan_tests(NUM_TESTS
);
759 diag("File descriptor tracker unit tests");
761 rcu_register_thread();
763 unknown_fds_count
= fd_count() - STDIO_FD_COUNT
;
764 assert(unknown_fds_count
>= 0);
766 diag("Unsuspendable - basic");
767 test_unsuspendable_basic();
768 diag("Unsuspendable - callback return values");
769 test_unsuspendable_cb_return();
770 diag("Unsuspendable - duplicate file descriptors");
771 test_unsuspendable_duplicate();
772 diag("Unsuspendable - closing an untracked file descriptor");
773 test_unsuspendable_close_untracked();
774 diag("Unsuspendable - check that file descritptor limit is enforced");
775 test_unsuspendable_limit();
777 diag("Suspendable - check that file descritptor limit is enforced");
778 test_suspendable_limit();
779 diag("Suspendable - restoration test");
780 test_suspendable_restore();
782 diag("Mixed - check that file descritptor limit is enforced");
785 diag("Suspendable - Unlinking test");
788 rcu_unregister_thread();
789 return exit_status();