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>
33 #include <common/fd-tracker/fd-tracker.h>
34 #include <common/error.h>
37 int lttng_opt_quiet
= 1;
38 int lttng_opt_verbose
;
41 /* Number of TAP tests in this file */
43 /* 3 for stdin, stdout, and stderr */
44 #define STDIO_FD_COUNT 3
45 #define TRACKER_FD_LIMIT 50
46 #define TMP_DIR_PATTERN "/tmp/fd-tracker-XXXXXX"
49 * Count of fds, beyond stdin, stderr, stdout that were open
50 * at the launch of the test. This allows the test to succeed when
51 * run by automake's test runner or valgrind which both open
52 * fds behind our back.
54 int unknown_fds_count
;
56 const char file_contents
[] = "Bacon ipsum dolor amet jerky drumstick sirloin "
57 "strip steak venison boudin filet mignon picanha doner shoulder. "
58 "Strip steak brisket alcatra, venison beef chuck cupim pastrami. "
59 "Landjaeger tri-tip salami leberkas ball tip, ham hock chuck sausage "
60 "flank jerky cupim. Pig bacon chuck pancetta andouille.";
68 dir
= opendir("/proc/self/fd");
70 perror("# Failed to enumerate /proc/self/fd/ to count the number of used file descriptors");
75 while ((entry
= readdir(dir
)) != NULL
) {
76 if (!strcmp(entry
->d_name
, ".") || !strcmp(entry
->d_name
, "..")) {
81 /* Don't account for the file descriptor opened by opendir(). */
89 void check_fd_count(int expected_count
)
94 ok(count
== expected_count
, "Expected %d open file descriptors (%d are open)",
95 expected_count
, count
);
99 int noop_open(void *data
, int *fds
)
101 *fds
= *((int *) data
);
106 int noop_close(void *data
, int *fds
)
112 void track_std_fds(struct fd_tracker
*tracker
)
115 struct { int fd
; const char *name
; } files
[] = {
116 { .fd
= fileno(stdin
), .name
= "stdin" },
117 { .fd
= fileno(stdout
), .name
= "stdout" },
118 { .fd
= fileno(stderr
), .name
= "stderr" },
121 for (i
= 0; i
< sizeof(files
) / sizeof(*files
); i
++) {
124 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &out_fd
,
125 &files
[i
].name
, 1, noop_open
, &files
[i
].fd
);
126 assert(out_fd
== files
[i
].fd
);
128 ok(ret
== 0, "Track unsuspendable fd %d (%s)", files
[i
].fd
,
134 void untrack_std_fds(struct fd_tracker
*tracker
)
137 struct { int fd
; const char *name
; } files
[] = {
138 { .fd
= fileno(stdin
), .name
= "stdin" },
139 { .fd
= fileno(stdout
), .name
= "stdout" },
140 { .fd
= fileno(stderr
), .name
= "stderr" },
142 unsigned int fds_set_to_minus_1
= 0;
144 for (i
= 0; i
< sizeof(files
) / sizeof(*files
); i
++) {
145 int fd
= files
[i
].fd
;
146 int ret
= fd_tracker_close_unsuspendable_fd(tracker
,
147 &files
[i
].fd
, 1, noop_close
, NULL
);
149 ok(ret
== 0, "Untrack unsuspendable fd %d (%s)", fd
,
151 fds_set_to_minus_1
+= (files
[i
].fd
== -1);
156 * Basic test opening and closing three unsuspendable fds.
159 void test_unsuspendable_basic(void)
161 struct fd_tracker
*tracker
;
163 tracker
= fd_tracker_create(TRACKER_FD_LIMIT
);
164 ok(tracker
, "Created an fd tracker with a limit of %d simulateously opened file descriptors",
170 track_std_fds(tracker
);
171 untrack_std_fds(tracker
);
173 fd_tracker_destroy(tracker
);
177 int error_open(void *data
, int *fds
)
179 return *((int *) data
);
183 int error_close(void *data
, int *fds
)
185 return *((int *) data
);
189 * Validate that user callback return values are returned to the
190 * caller of the fd tracker.
193 void test_unsuspendable_cb_return(void)
195 int ret
, stdout_fd
= fileno(stdout
), out_fd
= 42;
196 struct fd_tracker
*tracker
;
197 int expected_error
= -ENETDOWN
;
199 tracker
= fd_tracker_create(TRACKER_FD_LIMIT
);
202 /* The error_open callback should fail and return 'expected_error'. */
203 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &out_fd
,
204 NULL
, 1, error_open
, &expected_error
);
205 ok(ret
== expected_error
, "fd_tracker_open_unsuspendable_fd() forwards the user callback's error code");
206 ok(out_fd
== 42, "Output fd parameter is unaffected on error of fd_tracker_open_unsuspendable_fd()");
209 * Track a valid fd since we don't want the tracker to fail with an
210 * invalid fd error for this test.
212 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &out_fd
,
213 NULL
, 1, noop_open
, &stdout_fd
);
214 ok(out_fd
== stdout_fd
, "fd_tracker_open_unsuspendable_fd() sets the output fd parameter to the newly-tracked fd's value");
217 ret
= fd_tracker_close_unsuspendable_fd(tracker
,
218 &stdout_fd
, 1, error_close
, &expected_error
);
219 ok(ret
== expected_error
, "fd_tracker_close_unsuspendable_fd() forwards the user callback's error code");
220 ret
= fd_tracker_close_unsuspendable_fd(tracker
,
221 &stdout_fd
, 1, noop_close
, &expected_error
);
224 fd_tracker_destroy(tracker
);
228 * Validate that the tracker refuses to track two identical unsuspendable
232 void test_unsuspendable_duplicate(void)
234 int ret
, stdout_fd
= fileno(stdout
), out_fd
;
235 struct fd_tracker
*tracker
;
237 tracker
= fd_tracker_create(TRACKER_FD_LIMIT
);
240 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &out_fd
,
241 NULL
, 1, noop_open
, &stdout_fd
);
243 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &out_fd
,
244 NULL
, 1, noop_open
, &stdout_fd
);
245 ok(ret
== -EEXIST
, "EEXIST reported on open of an already tracked file descriptor");
247 ret
= fd_tracker_close_unsuspendable_fd(tracker
,
248 &stdout_fd
, 1, noop_close
, NULL
);
251 fd_tracker_destroy(tracker
);
255 int open_pipes(void *data
, int *out_fds
)
258 const unsigned int pipe_count
= TRACKER_FD_LIMIT
/ 2;
260 for (i
= 0; i
< pipe_count
; i
++) {
261 int ret
= pipe(&out_fds
[i
* 2]);
271 int close_pipes(void *data
, int *fds
)
276 for (i
= 0; i
< TRACKER_FD_LIMIT
; i
++) {
277 int ret
= close(pipes
[i
]);
287 * Validate that the tracker enforces the open file descriptor limit
288 * when unsuspendable file descritptors are being opened.
291 void test_unsuspendable_limit(void)
293 struct fd_tracker
*tracker
;
294 int ret
, stdout_fd
= fileno(stdout
), out_fd
;
295 int fds
[TRACKER_FD_LIMIT
];
297 /* This test assumes TRACKER_FD_LIMIT is a multiple of 2. */
298 assert((TRACKER_FD_LIMIT
% 2 == 0) && TRACKER_FD_LIMIT
);
300 tracker
= fd_tracker_create(TRACKER_FD_LIMIT
);
303 ret
= fd_tracker_open_unsuspendable_fd(tracker
, fds
,
304 NULL
, TRACKER_FD_LIMIT
, open_pipes
, NULL
);
305 ok(ret
== 0, "File descriptor tracker allowed the user to meet its limit with unsuspendable file descritptors (%d)",
308 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &out_fd
,
309 NULL
, 1, noop_open
, &stdout_fd
);
310 ok(ret
== -EMFILE
, "EMFILE reported when exceeding the file descriptor limit while opening an unsuspendable fd");
312 ret
= fd_tracker_close_unsuspendable_fd(tracker
,
313 fds
, TRACKER_FD_LIMIT
, close_pipes
, NULL
);
316 fd_tracker_destroy(tracker
);
320 * Validate that the tracker refuses to track two identical unsuspendable
324 void test_unsuspendable_close_untracked(void)
326 int ret
, stdout_fd
= fileno(stdout
), unknown_fds
[2], out_fd
;
327 struct fd_tracker
*tracker
;
329 tracker
= fd_tracker_create(TRACKER_FD_LIMIT
);
334 ret
= pipe(unknown_fds
);
336 assert(close(unknown_fds
[0]) == 0);
337 assert(close(unknown_fds
[1]) == 0);
339 ret
= fd_tracker_open_unsuspendable_fd(tracker
, &out_fd
,
340 NULL
, 1, noop_open
, &stdout_fd
);
343 ret
= fd_tracker_close_unsuspendable_fd(tracker
,
344 unknown_fds
, 1, noop_close
, NULL
);
345 ok(ret
== -EINVAL
, "EINVAL reported on close of an untracked file descriptor");
347 ret
= fd_tracker_close_unsuspendable_fd(tracker
,
348 &stdout_fd
, 1, noop_close
, NULL
);
351 fd_tracker_destroy(tracker
);
355 int open_files(struct fd_tracker
*tracker
, const char *dir
, unsigned int count
,
356 struct fs_handle
**handles
, char **file_paths
)
361 for (i
= 0; i
< count
; i
++) {
364 struct fs_handle
*handle
;
365 mode_t mode
= S_IWUSR
| S_IRUSR
;
367 ret
= asprintf(&file_path
, "%s/file-%u", dir
, i
);
369 file_paths
[i
] = file_path
;
371 handle
= fd_tracker_open_fs_handle(tracker
, file_path
,
372 O_RDWR
| O_CREAT
, &mode
);
383 int cleanup_files(struct fd_tracker
*tracker
, const char *dir
,
384 unsigned int count
, struct fs_handle
**handles
,
390 for (i
= 0; i
< count
; i
++) {
391 char *file_path
= file_paths
[i
];
396 (void) unlink(file_path
);
398 if (fs_handle_close(handles
[i
])) {
406 void test_suspendable_limit(void)
409 const int files_to_create
= TRACKER_FD_LIMIT
* 10;
410 struct fd_tracker
*tracker
;
411 char tmp_path_pattern
[] = TMP_DIR_PATTERN
;
412 const char *output_dir
;
413 char *output_files
[files_to_create
];
414 struct fs_handle
*handles
[files_to_create
];
416 memset(output_files
, 0, sizeof(output_files
));
417 memset(handles
, 0, sizeof(handles
));
419 tracker
= fd_tracker_create(TRACKER_FD_LIMIT
);
424 output_dir
= mkdtemp(tmp_path_pattern
);
426 diag("Failed to create temporary path of the form %s",
431 ret
= open_files(tracker
, output_dir
, files_to_create
, handles
,
433 ok(!ret
, "Created %d files with a limit of %d simultaneously-opened file descriptor",
434 files_to_create
, TRACKER_FD_LIMIT
);
435 check_fd_count(TRACKER_FD_LIMIT
+ STDIO_FD_COUNT
+ unknown_fds_count
);
437 ret
= cleanup_files(tracker
, output_dir
, files_to_create
, handles
,
439 ok(!ret
, "Close all opened filesystem handles");
440 (void) rmdir(output_dir
);
441 fd_tracker_destroy(tracker
);
445 void test_mixed_limit(void)
448 const int files_to_create
= TRACKER_FD_LIMIT
;
449 struct fd_tracker
*tracker
;
450 char tmp_path_pattern
[] = TMP_DIR_PATTERN
;
451 const char *output_dir
;
452 char *output_files
[files_to_create
];
453 struct fs_handle
*handles
[files_to_create
];
455 memset(output_files
, 0, sizeof(output_files
));
456 memset(handles
, 0, sizeof(handles
));
458 tracker
= fd_tracker_create(TRACKER_FD_LIMIT
);
463 output_dir
= mkdtemp(tmp_path_pattern
);
465 diag("Failed to create temporary path of the form %s",
470 ret
= open_files(tracker
, output_dir
, files_to_create
, handles
,
472 ok(!ret
, "Created %d files with a limit of %d simultaneously-opened file descriptor",
473 files_to_create
, TRACKER_FD_LIMIT
);
474 diag("Check file descriptor count after opening %u files", files_to_create
);
475 check_fd_count(TRACKER_FD_LIMIT
+ STDIO_FD_COUNT
+ unknown_fds_count
);
478 * Open unsuspendable fds (stdin, stdout, stderr) and verify that the fd
479 * cap is still respected.
481 diag("Check file descriptor count after adding %d unsuspendable fds",
483 track_std_fds(tracker
);
484 check_fd_count(TRACKER_FD_LIMIT
+ unknown_fds_count
);
485 diag("Untrack unsuspendable file descriptors");
486 untrack_std_fds(tracker
);
487 check_fd_count(TRACKER_FD_LIMIT
+ unknown_fds_count
);
489 ret
= cleanup_files(tracker
, output_dir
, files_to_create
, handles
,
491 ok(!ret
, "Close all opened filesystem handles");
492 (void) rmdir(output_dir
);
493 fd_tracker_destroy(tracker
);
497 * Open more files than allowed by the fd tracker's cap and write,
498 * byte-by-byte, and in round-robin, a string. The goal is to force
499 * the fd tracker to suspend and resume the fs_handles often and
500 * verify that the fd cap is always respected.
502 * The content of the files is also verified at the end.
505 void test_suspendable_restore(void)
508 const int files_to_create
= TRACKER_FD_LIMIT
* 10;
509 struct fd_tracker
*tracker
;
510 char tmp_path_pattern
[] = TMP_DIR_PATTERN
;
511 const char *output_dir
;
512 char *output_files
[files_to_create
];
513 struct fs_handle
*handles
[files_to_create
];
514 size_t content_index
;
516 bool write_success
= true;
517 bool fd_cap_respected
= true;
518 bool content_ok
= true;
520 memset(output_files
, 0, sizeof(output_files
));
521 memset(handles
, 0, sizeof(handles
));
523 tracker
= fd_tracker_create(TRACKER_FD_LIMIT
);
528 output_dir
= mkdtemp(tmp_path_pattern
);
530 diag("Failed to create temporary path of the form %s",
535 ret
= open_files(tracker
, output_dir
, files_to_create
, handles
,
537 ok(!ret
, "Created %d files with a limit of %d simultaneously-opened file descriptor",
538 files_to_create
, TRACKER_FD_LIMIT
);
539 diag("Check file descriptor count after opening %u files", files_to_create
);
540 check_fd_count(TRACKER_FD_LIMIT
+ STDIO_FD_COUNT
+ unknown_fds_count
);
542 for (content_index
= 0; content_index
< sizeof(file_contents
); content_index
++) {
543 for (handle_index
= 0; handle_index
< files_to_create
; handle_index
++) {
545 struct fs_handle
*handle
= handles
[handle_index
];
546 const char *path
= output_files
[handle_index
];
548 fd
= fs_handle_get_fd(handle
);
550 write_success
= false;
551 diag("Failed to restore fs_handle to %s",
557 ret
= write(fd
, file_contents
+ content_index
, 1);
558 } while (ret
< 0 && errno
== EINTR
);
561 write_success
= false;
562 PERROR("write() to %s failed", path
);
567 (TRACKER_FD_LIMIT
+ STDIO_FD_COUNT
+
568 unknown_fds_count
)) {
569 fd_cap_respected
= false;
572 fs_handle_put_fd(handle
);
576 ok(write_success
, "Wrote reference string to %d files",
578 ok(fd_cap_respected
, "FD tracker enforced the file descriptor cap");
580 /* Validate the contents of the files. */
581 for (handle_index
= 0; handle_index
< files_to_create
; handle_index
++) {
583 const char *path
= output_files
[handle_index
];
584 char read_buf
[sizeof(file_contents
)];
586 size_t to_read
= sizeof(read_buf
);
589 fd
= open(path
, O_RDONLY
);
591 ret
= fstat(fd
, &fd_stat
);
593 if (fd_stat
.st_size
!= sizeof(file_contents
)) {
594 diag("Content size of file %s doesn't match, got %" PRId64
", expected %zu",
595 path
, (int64_t) fd_stat
.st_size
,
596 sizeof(file_contents
));
604 ret
= read(fd
, read_pos
, to_read
);
609 } while (to_read
&& (ret
< 0 && errno
== EINTR
));
612 PERROR("Failed to read file %s", path
);
617 if (strcmp(file_contents
, read_buf
)) {
619 diag("File content doesn't match the expectated string");
625 ok(content_ok
, "Files contain the expected content");
626 ret
= cleanup_files(tracker
, output_dir
, files_to_create
, handles
,
628 ok(!ret
, "Close all opened filesystem handles");
629 (void) rmdir(output_dir
);
630 fd_tracker_destroy(tracker
);
633 int main(int argc
, char **argv
)
635 plan_tests(NUM_TESTS
);
636 diag("File descriptor tracker unit tests");
638 unknown_fds_count
= fd_count() - STDIO_FD_COUNT
;
639 assert(unknown_fds_count
>= 0);
641 diag("Unsuspendable - basic");
642 test_unsuspendable_basic();
643 diag("Unsuspendable - callback return values");
644 test_unsuspendable_cb_return();
645 diag("Unsuspendable - duplicate file descriptors");
646 test_unsuspendable_duplicate();
647 diag("Unsuspendable - closing an untracked file descriptor");
648 test_unsuspendable_close_untracked();
649 diag("Unsuspendable - check that file descritptor limit is enforced");
650 test_unsuspendable_limit();
652 diag("Suspendable - check that file descritptor limit is enforced");
653 test_suspendable_limit();
654 diag("Suspendable - restoration test");
655 test_suspendable_restore();
657 diag("Mixed - check that file descritptor limit is enforced");
660 return exit_status();
This page took 0.043826 seconds and 5 git commands to generate.