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>
35 #include <common/fd-tracker/fd-tracker.h>
36 #include <common/error.h>
39 int lttng_opt_quiet
= 1;
40 int lttng_opt_verbose
;
43 /* Number of TAP tests in this file */
45 /* 3 for stdin, stdout, and stderr */
46 #define STDIO_FD_COUNT 3
47 #define TRACKER_FD_LIMIT 50
48 #define TMP_DIR_PATTERN "/tmp/fd-tracker-XXXXXX"
51 * Count of fds, beyond stdin, stderr, stdout that were open
52 * at the launch of the test. This allows the test to succeed when
53 * run by automake's test runner or valgrind which both open
54 * fds behind our back.
56 int unknown_fds_count
;
58 const char file_contents
[] = "Bacon ipsum dolor amet jerky drumstick sirloin "
59 "strip steak venison boudin filet mignon picanha doner shoulder. "
60 "Strip steak brisket alcatra, venison beef chuck cupim pastrami. "
61 "Landjaeger tri-tip salami leberkas ball tip, ham hock chuck sausage "
62 "flank jerky cupim. Pig bacon chuck pancetta andouille.";
70 dir
= opendir("/proc/self/fd");
72 perror("# Failed to enumerate /proc/self/fd/ to count the number of used file descriptors");
77 while ((entry
= readdir(dir
)) != NULL
) {
78 if (!strcmp(entry
->d_name
, ".") || !strcmp(entry
->d_name
, "..")) {
83 /* Don't account for the file descriptor opened by opendir(). */
91 void check_fd_count(int expected_count
)
96 ok(count
== expected_count
, "Expected %d open file descriptors (%d are open)",
97 expected_count
, count
);
101 int noop_open(void *data
, int *fds
)
103 *fds
= *((int *) data
);
108 int noop_close(void *data
, int *fds
)
114 void track_std_fds(struct fd_tracker
*tracker
)
117 struct { int fd
; const char *name
; } files
[] = {
118 { .fd
= fileno(stdin
), .name
= "stdin" },
119 { .fd
= fileno(stdout
), .name
= "stdout" },
120 { .fd
= fileno(stderr
), .name
= "stderr" },
123 for (i
= 0; i
< sizeof(files
) / sizeof(*files
); i
++) {
126 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &out_fd
,
127 &files
[i
].name
, 1, noop_open
, &files
[i
].fd
);
128 assert(out_fd
== files
[i
].fd
);
130 ok(ret
== 0, "Track unsuspendable fd %d (%s)", files
[i
].fd
,
136 void untrack_std_fds(struct fd_tracker
*tracker
)
139 struct { int fd
; const char *name
; } files
[] = {
140 { .fd
= fileno(stdin
), .name
= "stdin" },
141 { .fd
= fileno(stdout
), .name
= "stdout" },
142 { .fd
= fileno(stderr
), .name
= "stderr" },
144 unsigned int fds_set_to_minus_1
= 0;
146 for (i
= 0; i
< sizeof(files
) / sizeof(*files
); i
++) {
147 int fd
= files
[i
].fd
;
148 int ret
= fd_tracker_close_unsuspendable_fd(tracker
,
149 &files
[i
].fd
, 1, noop_close
, NULL
);
151 ok(ret
== 0, "Untrack unsuspendable fd %d (%s)", fd
,
153 fds_set_to_minus_1
+= (files
[i
].fd
== -1);
158 * Basic test opening and closing three unsuspendable fds.
161 void test_unsuspendable_basic(void)
163 struct fd_tracker
*tracker
;
165 tracker
= fd_tracker_create(TRACKER_FD_LIMIT
);
166 ok(tracker
, "Created an fd tracker with a limit of %d simulateously opened file descriptors",
172 track_std_fds(tracker
);
173 untrack_std_fds(tracker
);
175 fd_tracker_destroy(tracker
);
179 int error_open(void *data
, int *fds
)
181 return *((int *) data
);
185 int error_close(void *data
, int *fds
)
187 return *((int *) data
);
191 * Validate that user callback return values are returned to the
192 * caller of the fd tracker.
195 void test_unsuspendable_cb_return(void)
197 int ret
, stdout_fd
= fileno(stdout
), out_fd
= 42;
198 struct fd_tracker
*tracker
;
199 int expected_error
= -ENETDOWN
;
201 tracker
= fd_tracker_create(TRACKER_FD_LIMIT
);
204 /* The error_open callback should fail and return 'expected_error'. */
205 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &out_fd
,
206 NULL
, 1, error_open
, &expected_error
);
207 ok(ret
== expected_error
, "fd_tracker_open_unsuspendable_fd() forwards the user callback's error code");
208 ok(out_fd
== 42, "Output fd parameter is unaffected on error of fd_tracker_open_unsuspendable_fd()");
211 * Track a valid fd since we don't want the tracker to fail with an
212 * invalid fd error for this test.
214 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &out_fd
,
215 NULL
, 1, noop_open
, &stdout_fd
);
216 ok(out_fd
== stdout_fd
, "fd_tracker_open_unsuspendable_fd() sets the output fd parameter to the newly-tracked fd's value");
219 ret
= fd_tracker_close_unsuspendable_fd(tracker
,
220 &stdout_fd
, 1, error_close
, &expected_error
);
221 ok(ret
== expected_error
, "fd_tracker_close_unsuspendable_fd() forwards the user callback's error code");
222 ret
= fd_tracker_close_unsuspendable_fd(tracker
,
223 &stdout_fd
, 1, noop_close
, &expected_error
);
226 fd_tracker_destroy(tracker
);
230 * Validate that the tracker refuses to track two identical unsuspendable
234 void test_unsuspendable_duplicate(void)
236 int ret
, stdout_fd
= fileno(stdout
), out_fd
;
237 struct fd_tracker
*tracker
;
239 tracker
= fd_tracker_create(TRACKER_FD_LIMIT
);
242 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &out_fd
,
243 NULL
, 1, noop_open
, &stdout_fd
);
245 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &out_fd
,
246 NULL
, 1, noop_open
, &stdout_fd
);
247 ok(ret
== -EEXIST
, "EEXIST reported on open of an already tracked file descriptor");
249 ret
= fd_tracker_close_unsuspendable_fd(tracker
,
250 &stdout_fd
, 1, noop_close
, NULL
);
253 fd_tracker_destroy(tracker
);
257 int open_pipes(void *data
, int *out_fds
)
260 const unsigned int pipe_count
= TRACKER_FD_LIMIT
/ 2;
262 for (i
= 0; i
< pipe_count
; i
++) {
263 int ret
= pipe(&out_fds
[i
* 2]);
273 int close_pipes(void *data
, int *fds
)
278 for (i
= 0; i
< TRACKER_FD_LIMIT
; i
++) {
279 int ret
= close(pipes
[i
]);
289 * Validate that the tracker enforces the open file descriptor limit
290 * when unsuspendable file descritptors are being opened.
293 void test_unsuspendable_limit(void)
295 struct fd_tracker
*tracker
;
296 int ret
, stdout_fd
= fileno(stdout
), out_fd
;
297 int fds
[TRACKER_FD_LIMIT
];
299 /* This test assumes TRACKER_FD_LIMIT is a multiple of 2. */
300 assert((TRACKER_FD_LIMIT
% 2 == 0) && TRACKER_FD_LIMIT
);
302 tracker
= fd_tracker_create(TRACKER_FD_LIMIT
);
305 ret
= fd_tracker_open_unsuspendable_fd(tracker
, fds
,
306 NULL
, TRACKER_FD_LIMIT
, open_pipes
, NULL
);
307 ok(ret
== 0, "File descriptor tracker allowed the user to meet its limit with unsuspendable file descritptors (%d)",
310 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &out_fd
,
311 NULL
, 1, noop_open
, &stdout_fd
);
312 ok(ret
== -EMFILE
, "EMFILE reported when exceeding the file descriptor limit while opening an unsuspendable fd");
314 ret
= fd_tracker_close_unsuspendable_fd(tracker
,
315 fds
, TRACKER_FD_LIMIT
, close_pipes
, NULL
);
318 fd_tracker_destroy(tracker
);
322 * Validate that the tracker refuses to track two identical unsuspendable
326 void test_unsuspendable_close_untracked(void)
328 int ret
, stdout_fd
= fileno(stdout
), unknown_fds
[2], out_fd
;
329 struct fd_tracker
*tracker
;
331 tracker
= fd_tracker_create(TRACKER_FD_LIMIT
);
336 ret
= pipe(unknown_fds
);
338 assert(close(unknown_fds
[0]) == 0);
339 assert(close(unknown_fds
[1]) == 0);
341 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &out_fd
,
342 NULL
, 1, noop_open
, &stdout_fd
);
345 ret
= fd_tracker_close_unsuspendable_fd(tracker
,
346 unknown_fds
, 1, noop_close
, NULL
);
347 ok(ret
== -EINVAL
, "EINVAL reported on close of an untracked file descriptor");
349 ret
= fd_tracker_close_unsuspendable_fd(tracker
,
350 &stdout_fd
, 1, noop_close
, NULL
);
353 fd_tracker_destroy(tracker
);
357 int open_files(struct fd_tracker
*tracker
, const char *dir
, unsigned int count
,
358 struct fs_handle
**handles
, char **file_paths
)
363 for (i
= 0; i
< count
; i
++) {
366 struct fs_handle
*handle
;
367 mode_t mode
= S_IWUSR
| S_IRUSR
;
369 ret
= asprintf(&file_path
, "%s/file-%u", dir
, i
);
371 file_paths
[i
] = file_path
;
373 handle
= fd_tracker_open_fs_handle(tracker
, file_path
,
374 O_RDWR
| O_CREAT
, &mode
);
385 int cleanup_files(struct fd_tracker
*tracker
, const char *dir
,
386 unsigned int count
, struct fs_handle
**handles
,
392 for (i
= 0; i
< count
; i
++) {
393 char *file_path
= file_paths
[i
];
398 (void) unlink(file_path
);
400 if (fs_handle_close(handles
[i
])) {
408 void test_suspendable_limit(void)
411 const int files_to_create
= TRACKER_FD_LIMIT
* 10;
412 struct fd_tracker
*tracker
;
413 char tmp_path_pattern
[] = TMP_DIR_PATTERN
;
414 const char *output_dir
;
415 char *output_files
[files_to_create
];
416 struct fs_handle
*handles
[files_to_create
];
418 memset(output_files
, 0, sizeof(output_files
));
419 memset(handles
, 0, sizeof(handles
));
421 tracker
= fd_tracker_create(TRACKER_FD_LIMIT
);
426 output_dir
= mkdtemp(tmp_path_pattern
);
428 diag("Failed to create temporary path of the form %s",
433 ret
= open_files(tracker
, output_dir
, files_to_create
, handles
,
435 ok(!ret
, "Created %d files with a limit of %d simultaneously-opened file descriptor",
436 files_to_create
, TRACKER_FD_LIMIT
);
437 check_fd_count(TRACKER_FD_LIMIT
+ STDIO_FD_COUNT
+ unknown_fds_count
);
439 ret
= cleanup_files(tracker
, output_dir
, files_to_create
, handles
,
441 ok(!ret
, "Close all opened filesystem handles");
442 (void) rmdir(output_dir
);
443 fd_tracker_destroy(tracker
);
447 void test_mixed_limit(void)
450 const int files_to_create
= TRACKER_FD_LIMIT
;
451 struct fd_tracker
*tracker
;
452 char tmp_path_pattern
[] = TMP_DIR_PATTERN
;
453 const char *output_dir
;
454 char *output_files
[files_to_create
];
455 struct fs_handle
*handles
[files_to_create
];
457 memset(output_files
, 0, sizeof(output_files
));
458 memset(handles
, 0, sizeof(handles
));
460 tracker
= fd_tracker_create(TRACKER_FD_LIMIT
);
465 output_dir
= mkdtemp(tmp_path_pattern
);
467 diag("Failed to create temporary path of the form %s",
472 ret
= open_files(tracker
, output_dir
, files_to_create
, handles
,
474 ok(!ret
, "Created %d files with a limit of %d simultaneously-opened file descriptor",
475 files_to_create
, TRACKER_FD_LIMIT
);
476 diag("Check file descriptor count after opening %u files", files_to_create
);
477 check_fd_count(TRACKER_FD_LIMIT
+ STDIO_FD_COUNT
+ unknown_fds_count
);
480 * Open unsuspendable fds (stdin, stdout, stderr) and verify that the fd
481 * cap is still respected.
483 diag("Check file descriptor count after adding %d unsuspendable fds",
485 track_std_fds(tracker
);
486 check_fd_count(TRACKER_FD_LIMIT
+ unknown_fds_count
);
487 diag("Untrack unsuspendable file descriptors");
488 untrack_std_fds(tracker
);
489 check_fd_count(TRACKER_FD_LIMIT
+ unknown_fds_count
);
491 ret
= cleanup_files(tracker
, output_dir
, files_to_create
, handles
,
493 ok(!ret
, "Close all opened filesystem handles");
494 (void) rmdir(output_dir
);
495 fd_tracker_destroy(tracker
);
499 * Open more files than allowed by the fd tracker's cap and write,
500 * byte-by-byte, and in round-robin, a string. The goal is to force
501 * the fd tracker to suspend and resume the fs_handles often and
502 * verify that the fd cap is always respected.
504 * The content of the files is also verified at the end.
507 void test_suspendable_restore(void)
510 const int files_to_create
= TRACKER_FD_LIMIT
* 10;
511 struct fd_tracker
*tracker
;
512 char tmp_path_pattern
[] = TMP_DIR_PATTERN
;
513 const char *output_dir
;
514 char *output_files
[files_to_create
];
515 struct fs_handle
*handles
[files_to_create
];
516 size_t content_index
;
518 bool write_success
= true;
519 bool fd_cap_respected
= true;
520 bool content_ok
= true;
522 memset(output_files
, 0, sizeof(output_files
));
523 memset(handles
, 0, sizeof(handles
));
525 tracker
= fd_tracker_create(TRACKER_FD_LIMIT
);
530 output_dir
= mkdtemp(tmp_path_pattern
);
532 diag("Failed to create temporary path of the form %s",
537 ret
= open_files(tracker
, output_dir
, files_to_create
, handles
,
539 ok(!ret
, "Created %d files with a limit of %d simultaneously-opened file descriptor",
540 files_to_create
, TRACKER_FD_LIMIT
);
541 diag("Check file descriptor count after opening %u files", files_to_create
);
542 check_fd_count(TRACKER_FD_LIMIT
+ STDIO_FD_COUNT
+ unknown_fds_count
);
544 for (content_index
= 0; content_index
< sizeof(file_contents
); content_index
++) {
545 for (handle_index
= 0; handle_index
< files_to_create
; handle_index
++) {
547 struct fs_handle
*handle
= handles
[handle_index
];
548 const char *path
= output_files
[handle_index
];
550 fd
= fs_handle_get_fd(handle
);
552 write_success
= false;
553 diag("Failed to restore fs_handle to %s",
559 ret
= write(fd
, file_contents
+ content_index
, 1);
560 } while (ret
< 0 && errno
== EINTR
);
563 write_success
= false;
564 PERROR("write() to %s failed", path
);
569 (TRACKER_FD_LIMIT
+ STDIO_FD_COUNT
+
570 unknown_fds_count
)) {
571 fd_cap_respected
= false;
574 fs_handle_put_fd(handle
);
578 ok(write_success
, "Wrote reference string to %d files",
580 ok(fd_cap_respected
, "FD tracker enforced the file descriptor cap");
582 /* Validate the contents of the files. */
583 for (handle_index
= 0; handle_index
< files_to_create
; handle_index
++) {
585 const char *path
= output_files
[handle_index
];
586 char read_buf
[sizeof(file_contents
)];
588 size_t to_read
= sizeof(read_buf
);
591 fd
= open(path
, O_RDONLY
);
593 ret
= fstat(fd
, &fd_stat
);
595 if (fd_stat
.st_size
!= sizeof(file_contents
)) {
596 diag("Content size of file %s doesn't match, got %" PRId64
", expected %zu",
597 path
, (int64_t) fd_stat
.st_size
,
598 sizeof(file_contents
));
606 ret
= read(fd
, read_pos
, to_read
);
611 } while (to_read
&& (ret
< 0 && errno
== EINTR
));
614 PERROR("Failed to read file %s", path
);
619 if (strcmp(file_contents
, read_buf
)) {
621 diag("File content doesn't match the expectated string");
627 ok(content_ok
, "Files contain the expected content");
628 ret
= cleanup_files(tracker
, output_dir
, files_to_create
, handles
,
630 ok(!ret
, "Close all opened filesystem handles");
631 (void) rmdir(output_dir
);
632 fd_tracker_destroy(tracker
);
635 int main(int argc
, char **argv
)
637 plan_tests(NUM_TESTS
);
638 diag("File descriptor tracker unit tests");
640 rcu_register_thread();
642 unknown_fds_count
= fd_count() - STDIO_FD_COUNT
;
643 assert(unknown_fds_count
>= 0);
645 diag("Unsuspendable - basic");
646 test_unsuspendable_basic();
647 diag("Unsuspendable - callback return values");
648 test_unsuspendable_cb_return();
649 diag("Unsuspendable - duplicate file descriptors");
650 test_unsuspendable_duplicate();
651 diag("Unsuspendable - closing an untracked file descriptor");
652 test_unsuspendable_close_untracked();
653 diag("Unsuspendable - check that file descritptor limit is enforced");
654 test_unsuspendable_limit();
656 diag("Suspendable - check that file descritptor limit is enforced");
657 test_suspendable_limit();
658 diag("Suspendable - restoration test");
659 test_suspendable_restore();
661 diag("Mixed - check that file descritptor limit is enforced");
664 rcu_unregister_thread();
665 return exit_status();
This page took 0.044086 seconds and 5 git commands to generate.