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(). */
92 void check_fd_count(int expected_count
)
97 ok(count
== expected_count
, "Expected %d open file descriptors (%d are open)",
98 expected_count
, count
);
102 int noop_open(void *data
, int *fds
)
104 *fds
= *((int *) data
);
109 int noop_close(void *data
, int *fds
)
115 void track_std_fds(struct fd_tracker
*tracker
)
118 struct { int fd
; const char *name
; } files
[] = {
119 { .fd
= fileno(stdin
), .name
= "stdin" },
120 { .fd
= fileno(stdout
), .name
= "stdout" },
121 { .fd
= fileno(stderr
), .name
= "stderr" },
124 for (i
= 0; i
< sizeof(files
) / sizeof(*files
); i
++) {
127 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &out_fd
,
128 &files
[i
].name
, 1, noop_open
, &files
[i
].fd
);
129 assert(out_fd
== files
[i
].fd
);
131 ok(ret
== 0, "Track unsuspendable fd %d (%s)", files
[i
].fd
,
137 void untrack_std_fds(struct fd_tracker
*tracker
)
140 struct { int fd
; const char *name
; } files
[] = {
141 { .fd
= fileno(stdin
), .name
= "stdin" },
142 { .fd
= fileno(stdout
), .name
= "stdout" },
143 { .fd
= fileno(stderr
), .name
= "stderr" },
145 unsigned int fds_set_to_minus_1
= 0;
147 for (i
= 0; i
< sizeof(files
) / sizeof(*files
); i
++) {
148 int fd
= files
[i
].fd
;
149 int ret
= fd_tracker_close_unsuspendable_fd(tracker
,
150 &files
[i
].fd
, 1, noop_close
, NULL
);
152 ok(ret
== 0, "Untrack unsuspendable fd %d (%s)", fd
,
154 fds_set_to_minus_1
+= (files
[i
].fd
== -1);
159 * Basic test opening and closing three unsuspendable fds.
162 void test_unsuspendable_basic(void)
164 struct fd_tracker
*tracker
;
166 tracker
= fd_tracker_create(TRACKER_FD_LIMIT
);
167 ok(tracker
, "Created an fd tracker with a limit of %d simulateously opened file descriptors",
173 track_std_fds(tracker
);
174 untrack_std_fds(tracker
);
176 fd_tracker_destroy(tracker
);
180 int error_open(void *data
, int *fds
)
182 return *((int *) data
);
186 int error_close(void *data
, int *fds
)
188 return *((int *) data
);
192 * Validate that user callback return values are returned to the
193 * caller of the fd tracker.
196 void test_unsuspendable_cb_return(void)
198 int ret
, stdout_fd
= fileno(stdout
), out_fd
= 42;
199 struct fd_tracker
*tracker
;
200 int expected_error
= -ENETDOWN
;
202 tracker
= fd_tracker_create(TRACKER_FD_LIMIT
);
205 /* The error_open callback should fail and return 'expected_error'. */
206 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &out_fd
,
207 NULL
, 1, error_open
, &expected_error
);
208 ok(ret
== expected_error
, "fd_tracker_open_unsuspendable_fd() forwards the user callback's error code");
209 ok(out_fd
== 42, "Output fd parameter is unaffected on error of fd_tracker_open_unsuspendable_fd()");
212 * Track a valid fd since we don't want the tracker to fail with an
213 * invalid fd error for this test.
215 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &out_fd
,
216 NULL
, 1, noop_open
, &stdout_fd
);
217 ok(out_fd
== stdout_fd
, "fd_tracker_open_unsuspendable_fd() sets the output fd parameter to the newly-tracked fd's value");
220 ret
= fd_tracker_close_unsuspendable_fd(tracker
,
221 &stdout_fd
, 1, error_close
, &expected_error
);
222 ok(ret
== expected_error
, "fd_tracker_close_unsuspendable_fd() forwards the user callback's error code");
223 ret
= fd_tracker_close_unsuspendable_fd(tracker
,
224 &stdout_fd
, 1, noop_close
, &expected_error
);
227 fd_tracker_destroy(tracker
);
231 * Validate that the tracker refuses to track two identical unsuspendable
235 void test_unsuspendable_duplicate(void)
237 int ret
, stdout_fd
= fileno(stdout
), out_fd
;
238 struct fd_tracker
*tracker
;
240 tracker
= fd_tracker_create(TRACKER_FD_LIMIT
);
243 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &out_fd
,
244 NULL
, 1, noop_open
, &stdout_fd
);
246 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &out_fd
,
247 NULL
, 1, noop_open
, &stdout_fd
);
248 ok(ret
== -EEXIST
, "EEXIST reported on open of an already tracked file descriptor");
250 ret
= fd_tracker_close_unsuspendable_fd(tracker
,
251 &stdout_fd
, 1, noop_close
, NULL
);
254 fd_tracker_destroy(tracker
);
258 int open_pipes(void *data
, int *out_fds
)
261 const unsigned int pipe_count
= TRACKER_FD_LIMIT
/ 2;
263 for (i
= 0; i
< pipe_count
; i
++) {
264 int ret
= pipe(&out_fds
[i
* 2]);
274 int close_pipes(void *data
, int *fds
)
279 for (i
= 0; i
< TRACKER_FD_LIMIT
; i
++) {
280 int ret
= close(pipes
[i
]);
290 * Validate that the tracker enforces the open file descriptor limit
291 * when unsuspendable file descritptors are being opened.
294 void test_unsuspendable_limit(void)
296 struct fd_tracker
*tracker
;
297 int ret
, stdout_fd
= fileno(stdout
), out_fd
;
298 int fds
[TRACKER_FD_LIMIT
];
300 /* This test assumes TRACKER_FD_LIMIT is a multiple of 2. */
301 assert((TRACKER_FD_LIMIT
% 2 == 0) && TRACKER_FD_LIMIT
);
303 tracker
= fd_tracker_create(TRACKER_FD_LIMIT
);
306 ret
= fd_tracker_open_unsuspendable_fd(tracker
, fds
,
307 NULL
, TRACKER_FD_LIMIT
, open_pipes
, NULL
);
308 ok(ret
== 0, "File descriptor tracker allowed the user to meet its limit with unsuspendable file descritptors (%d)",
311 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &out_fd
,
312 NULL
, 1, noop_open
, &stdout_fd
);
313 ok(ret
== -EMFILE
, "EMFILE reported when exceeding the file descriptor limit while opening an unsuspendable fd");
315 ret
= fd_tracker_close_unsuspendable_fd(tracker
,
316 fds
, TRACKER_FD_LIMIT
, close_pipes
, NULL
);
319 fd_tracker_destroy(tracker
);
323 * Validate that the tracker refuses to track two identical unsuspendable
327 void test_unsuspendable_close_untracked(void)
329 int ret
, stdout_fd
= fileno(stdout
), unknown_fds
[2], out_fd
;
330 struct fd_tracker
*tracker
;
332 tracker
= fd_tracker_create(TRACKER_FD_LIMIT
);
337 ret
= pipe(unknown_fds
);
339 assert(close(unknown_fds
[0]) == 0);
340 assert(close(unknown_fds
[1]) == 0);
342 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &out_fd
,
343 NULL
, 1, noop_open
, &stdout_fd
);
346 ret
= fd_tracker_close_unsuspendable_fd(tracker
,
347 unknown_fds
, 1, noop_close
, NULL
);
348 ok(ret
== -EINVAL
, "EINVAL reported on close of an untracked file descriptor");
350 ret
= fd_tracker_close_unsuspendable_fd(tracker
,
351 &stdout_fd
, 1, noop_close
, NULL
);
354 fd_tracker_destroy(tracker
);
358 int open_files(struct fd_tracker
*tracker
, const char *dir
, unsigned int count
,
359 struct fs_handle
**handles
, char **file_paths
)
364 for (i
= 0; i
< count
; i
++) {
367 struct fs_handle
*handle
;
368 mode_t mode
= S_IWUSR
| S_IRUSR
;
370 p_ret
= asprintf(&file_path
, "%s/file-%u", dir
, i
);
372 file_paths
[i
] = file_path
;
374 handle
= fd_tracker_open_fs_handle(tracker
, file_path
,
375 O_RDWR
| O_CREAT
, &mode
);
386 int open_same_file(struct fd_tracker
*tracker
, const char *file_path
,
387 unsigned int count
, struct fs_handle
**handles
)
392 for (i
= 0; i
< count
; i
++) {
393 struct fs_handle
*handle
;
394 mode_t mode
= S_IWUSR
| S_IRUSR
;
396 handle
= fd_tracker_open_fs_handle(tracker
, file_path
,
397 O_RDWR
| O_CREAT
, &mode
);
408 int cleanup_files(struct fd_tracker
*tracker
, const char *dir
,
409 unsigned int count
, struct fs_handle
**handles
,
415 for (i
= 0; i
< count
; i
++) {
416 char *file_path
= file_paths
[i
];
421 (void) unlink(file_path
);
423 if (fs_handle_close(handles
[i
])) {
431 void test_suspendable_limit(void)
434 const int files_to_create
= TRACKER_FD_LIMIT
* 10;
435 struct fd_tracker
*tracker
;
436 char tmp_path_pattern
[] = TMP_DIR_PATTERN
;
437 const char *output_dir
;
438 char *output_files
[files_to_create
];
439 struct fs_handle
*handles
[files_to_create
];
441 memset(output_files
, 0, sizeof(output_files
));
442 memset(handles
, 0, sizeof(handles
));
444 tracker
= fd_tracker_create(TRACKER_FD_LIMIT
);
449 output_dir
= mkdtemp(tmp_path_pattern
);
451 diag("Failed to create temporary path of the form %s",
456 ret
= open_files(tracker
, output_dir
, files_to_create
, handles
,
458 ok(!ret
, "Created %d files with a limit of %d simultaneously-opened file descriptor",
459 files_to_create
, TRACKER_FD_LIMIT
);
460 check_fd_count(TRACKER_FD_LIMIT
+ STDIO_FD_COUNT
+ unknown_fds_count
);
462 ret
= cleanup_files(tracker
, output_dir
, files_to_create
, handles
,
464 ok(!ret
, "Close all opened filesystem handles");
465 (void) rmdir(output_dir
);
466 fd_tracker_destroy(tracker
);
470 void test_mixed_limit(void)
473 const int files_to_create
= TRACKER_FD_LIMIT
;
474 struct fd_tracker
*tracker
;
475 char tmp_path_pattern
[] = TMP_DIR_PATTERN
;
476 const char *output_dir
;
477 char *output_files
[files_to_create
];
478 struct fs_handle
*handles
[files_to_create
];
480 memset(output_files
, 0, sizeof(output_files
));
481 memset(handles
, 0, sizeof(handles
));
483 tracker
= fd_tracker_create(TRACKER_FD_LIMIT
);
488 output_dir
= mkdtemp(tmp_path_pattern
);
490 diag("Failed to create temporary path of the form %s",
495 ret
= open_files(tracker
, output_dir
, files_to_create
, handles
,
497 ok(!ret
, "Created %d files with a limit of %d simultaneously-opened file descriptor",
498 files_to_create
, TRACKER_FD_LIMIT
);
499 diag("Check file descriptor count after opening %u files", files_to_create
);
500 check_fd_count(TRACKER_FD_LIMIT
+ STDIO_FD_COUNT
+ unknown_fds_count
);
503 * Open unsuspendable fds (stdin, stdout, stderr) and verify that the fd
504 * cap is still respected.
506 diag("Check file descriptor count after adding %d unsuspendable fds",
508 track_std_fds(tracker
);
509 check_fd_count(TRACKER_FD_LIMIT
+ unknown_fds_count
);
510 diag("Untrack unsuspendable file descriptors");
511 untrack_std_fds(tracker
);
512 check_fd_count(TRACKER_FD_LIMIT
+ unknown_fds_count
);
514 ret
= cleanup_files(tracker
, output_dir
, files_to_create
, handles
,
516 ok(!ret
, "Close all opened filesystem handles");
517 (void) rmdir(output_dir
);
518 fd_tracker_destroy(tracker
);
522 * Open more files than allowed by the fd tracker's cap and write,
523 * byte-by-byte, and in round-robin, a string. The goal is to force
524 * the fd tracker to suspend and resume the fs_handles often and
525 * verify that the fd cap is always respected.
527 * The content of the files is also verified at the end.
530 void test_suspendable_restore(void)
533 const int files_to_create
= TRACKER_FD_LIMIT
* 10;
534 struct fd_tracker
*tracker
;
535 char tmp_path_pattern
[] = TMP_DIR_PATTERN
;
536 const char *output_dir
;
537 char *output_files
[files_to_create
];
538 struct fs_handle
*handles
[files_to_create
];
539 size_t content_index
;
541 bool write_success
= true;
542 bool fd_cap_respected
= true;
543 bool content_ok
= true;
545 memset(output_files
, 0, sizeof(output_files
));
546 memset(handles
, 0, sizeof(handles
));
548 tracker
= fd_tracker_create(TRACKER_FD_LIMIT
);
553 output_dir
= mkdtemp(tmp_path_pattern
);
555 diag("Failed to create temporary path of the form %s",
560 ret
= open_files(tracker
, output_dir
, files_to_create
, handles
,
562 ok(!ret
, "Created %d files with a limit of %d simultaneously-opened file descriptor",
563 files_to_create
, TRACKER_FD_LIMIT
);
564 diag("Check file descriptor count after opening %u files", files_to_create
);
565 check_fd_count(TRACKER_FD_LIMIT
+ STDIO_FD_COUNT
+ unknown_fds_count
);
567 for (content_index
= 0; content_index
< sizeof(file_contents
); content_index
++) {
568 for (handle_index
= 0; handle_index
< files_to_create
; handle_index
++) {
570 struct fs_handle
*handle
= handles
[handle_index
];
571 const char *path
= output_files
[handle_index
];
573 fd
= fs_handle_get_fd(handle
);
575 write_success
= false;
576 diag("Failed to restore fs_handle to %s",
582 ret
= write(fd
, file_contents
+ content_index
, 1);
583 } while (ret
< 0 && errno
== EINTR
);
586 write_success
= false;
587 PERROR("write() to %s failed", path
);
592 (TRACKER_FD_LIMIT
+ STDIO_FD_COUNT
+
593 unknown_fds_count
)) {
594 fd_cap_respected
= false;
597 fs_handle_put_fd(handle
);
601 ok(write_success
, "Wrote reference string to %d files",
603 ok(fd_cap_respected
, "FD tracker enforced the file descriptor cap");
605 /* Validate the contents of the files. */
606 for (handle_index
= 0; handle_index
< files_to_create
; handle_index
++) {
608 const char *path
= output_files
[handle_index
];
609 char read_buf
[sizeof(file_contents
)];
611 size_t to_read
= sizeof(read_buf
);
614 fd
= open(path
, O_RDONLY
);
616 ret
= fstat(fd
, &fd_stat
);
618 if (fd_stat
.st_size
!= sizeof(file_contents
)) {
619 diag("Content size of file %s doesn't match, got %" PRId64
", expected %zu",
620 path
, (int64_t) fd_stat
.st_size
,
621 sizeof(file_contents
));
629 ret
= read(fd
, read_pos
, to_read
);
634 } while (to_read
&& (ret
< 0 && errno
== EINTR
));
637 PERROR("Failed to read file %s", path
);
642 if (strcmp(file_contents
, read_buf
)) {
644 diag("File content doesn't match the expectated string");
650 ok(content_ok
, "Files contain the expected content");
651 ret
= cleanup_files(tracker
, output_dir
, files_to_create
, handles
,
653 ok(!ret
, "Close all opened filesystem handles");
654 (void) rmdir(output_dir
);
655 fd_tracker_destroy(tracker
);
659 void test_unlink(void)
662 struct fd_tracker
*tracker
;
663 const int handles_to_open
= 2;
664 char tmp_path_pattern
[] = TMP_DIR_PATTERN
;
665 const char *output_dir
;
666 struct fs_handle
*handles
[handles_to_open
];
667 struct fs_handle
*new_handle
;
669 char *unlinked_file_path
;
670 char *unlinked_file_path_suffix
;
673 tracker
= fd_tracker_create(1);
677 output_dir
= mkdtemp(tmp_path_pattern
);
679 diag("Failed to create temporary path of the form %s",
683 ret
= asprintf(&file_path
, "%s/my_file", output_dir
);
685 diag("Failed to allocate path string");
688 ret
= asprintf(&unlinked_file_path
, "%s-deleted", file_path
);
690 diag("Failed to allocate path string");
693 ret
= asprintf(&unlinked_file_path_suffix
, "%s-1", unlinked_file_path
);
695 diag("Failed to allocate path string");
699 /* Open two handles to the same file. */
700 ret
= open_same_file(tracker
, file_path
, handles_to_open
, handles
);
701 ok(!ret
, "Successfully %i handles to %s", handles_to_open
);
707 * Unlinking the first handle should cause the file to be renamed
708 * to 'my_file-deleted'.
710 ret
= fs_handle_unlink(handles
[0]);
711 ok(!ret
, "Successfully unlinked the first handle to %s", file_path
);
714 * The original file should no longer exist on the file system, and a
715 * new file, with the '-deleted' suffix should exist.
717 ok(stat(file_path
, &statbuf
) == -1 && errno
== ENOENT
, "%s no longer present on file system after unlink", file_path
);
718 ok(stat(unlinked_file_path
, &statbuf
) == 0, "%s exists on file system after unlink", unlinked_file_path
);
720 /* The second unlink should fail with -ENOENT. */
721 ret
= fs_handle_unlink(handles
[1]);
722 ok(ret
== -ENOENT
, "ENOENT is reported when attempting to unlink the second handle to %s", file_path
);
725 * Opening a new handle to 'my_file' should succeed.
727 ret
= open_same_file(tracker
, file_path
, 1, &new_handle
);
728 ok(!ret
, "Successfully opened a new handle to previously unlinked file %s", file_path
);
732 * Unlinking the new handle should cause the file to be renamed
733 * to 'my_file-deleted-1' since 'my_file-deleted' already exists.
735 ret
= fs_handle_unlink(new_handle
);
736 ok(!ret
, "Successfully unlinked the new handle handle to %s", file_path
);
737 ok(stat(unlinked_file_path_suffix
, &statbuf
) == 0, "%s exists on file system after unlink", unlinked_file_path_suffix
);
739 ret
= fs_handle_close(handles
[0]);
740 ok(!ret
, "Successfully closed the first handle");
741 ret
= fs_handle_close(handles
[1]);
742 ok(!ret
, "Successfully closed the second handle");
743 ret
= fs_handle_close(new_handle
);
744 ok(!ret
, "Successfully closed the third handle");
746 ok(stat(file_path
, &statbuf
) == -1 && errno
== ENOENT
, "%s no longer present on file system after handle close", file_path
);
747 ok(stat(unlinked_file_path
, &statbuf
) == -1 && errno
== ENOENT
, "%s no longer present on file system after handle close", unlinked_file_path
);
748 ok(stat(unlinked_file_path_suffix
, &statbuf
) == -1 && errno
== ENOENT
, "%s no longer present on file system after handle close", unlinked_file_path_suffix
);
750 (void) rmdir(output_dir
);
751 fd_tracker_destroy(tracker
);
754 int main(int argc
, char **argv
)
756 plan_tests(NUM_TESTS
);
757 diag("File descriptor tracker unit tests");
759 rcu_register_thread();
761 unknown_fds_count
= fd_count() - STDIO_FD_COUNT
;
762 assert(unknown_fds_count
>= 0);
764 diag("Unsuspendable - basic");
765 test_unsuspendable_basic();
766 diag("Unsuspendable - callback return values");
767 test_unsuspendable_cb_return();
768 diag("Unsuspendable - duplicate file descriptors");
769 test_unsuspendable_duplicate();
770 diag("Unsuspendable - closing an untracked file descriptor");
771 test_unsuspendable_close_untracked();
772 diag("Unsuspendable - check that file descritptor limit is enforced");
773 test_unsuspendable_limit();
775 diag("Suspendable - check that file descritptor limit is enforced");
776 test_suspendable_limit();
777 diag("Suspendable - restoration test");
778 test_suspendable_restore();
780 diag("Mixed - check that file descritptor limit is enforced");
783 diag("Suspendable - Unlinking test");
786 rcu_unregister_thread();
787 return exit_status();