cb192ff80019d95b1932502c501f4882e9fbb533
[lttng-tools.git] / tests / unit / test_fd_tracker.c
1 /*
2 * Copyright (C) 2018 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #include <stdlib.h>
9 #include <inttypes.h>
10 #include <stdbool.h>
11 #include <assert.h>
12 #include <string.h>
13 #include <stdarg.h>
14 #include <tap/tap.h>
15 #include <sys/types.h>
16 #include <dirent.h>
17 #include <stdio.h>
18 #include <errno.h>
19 #include <unistd.h>
20 #include <fcntl.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23
24 #include <urcu.h>
25
26 #include <common/compat/directory-handle.h>
27 #include <common/error.h>
28 #include <common/fd-tracker/fd-tracker.h>
29
30 /* For error.h */
31 int lttng_opt_quiet = 1;
32 int lttng_opt_verbose;
33 int lttng_opt_mi;
34
35 /* Number of TAP tests in this file */
36 #define NUM_TESTS 61
37 /* 3 for stdin, stdout, and stderr */
38 #define STDIO_FD_COUNT 3
39 #define TRACKER_FD_LIMIT 50
40 #define TMP_DIR_PATTERN "/tmp/fd-tracker-XXXXXX"
41 #define TEST_UNLINK_DIRECTORY_NAME "unlinked_files"
42
43 /*
44 * Count of fds, beyond stdin, stderr, stdout that were open
45 * at the launch of the test. This allows the test to succeed when
46 * run by automake's test runner or valgrind which both open
47 * fds behind our back.
48 */
49 int unknown_fds_count;
50
51 const char file_contents[] = "Bacon ipsum dolor amet jerky drumstick sirloin "
52 "strip steak venison boudin filet mignon picanha doner shoulder. "
53 "Strip steak brisket alcatra, venison beef chuck cupim pastrami. "
54 "Landjaeger tri-tip salami leberkas ball tip, ham hock chuck sausage "
55 "flank jerky cupim. Pig bacon chuck pancetta andouille.";
56
57 void get_temporary_directories(char **_test_directory, char **_unlink_directory)
58 {
59 int ret;
60 char tmp_path_pattern[] = TMP_DIR_PATTERN;
61 char *output_dir;
62
63 output_dir = mkdtemp(tmp_path_pattern);
64 if (!output_dir) {
65 diag("Failed to create temporary path of the form %s",
66 TMP_DIR_PATTERN);
67 assert(0);
68 }
69
70 *_test_directory = strdup(output_dir);
71 assert(*_test_directory);
72 ret = asprintf(_unlink_directory, "%s/%s", output_dir,
73 TEST_UNLINK_DIRECTORY_NAME);
74 if (ret < 0) {
75 assert(0);
76 }
77 }
78
79 int fd_count(void)
80 {
81 DIR *dir;
82 struct dirent *entry;
83 int count = 0;
84
85 dir = opendir("/proc/self/fd");
86 if (!dir) {
87 perror("# Failed to enumerate /proc/self/fd/ to count the number of used file descriptors");
88 count = -1;
89 goto end;
90 }
91
92 while ((entry = readdir(dir)) != NULL) {
93 if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) {
94 continue;
95 }
96 count++;
97 }
98 /* Don't account for the file descriptor opened by opendir(). */
99 count--;
100 if (closedir(dir)) {
101 perror("# Failed to close test program's self/fd directory file descriptor");
102 }
103 end:
104 return count;
105 }
106
107 static
108 void check_fd_count(int expected_count)
109 {
110 int count = 0;
111
112 count = fd_count();
113 ok(count == expected_count, "Expected %d open file descriptors (%d are open)",
114 expected_count, count);
115 }
116
117 static
118 int noop_open(void *data, int *fds)
119 {
120 *fds = *((int *) data);
121 return 0;
122 }
123
124 static
125 int noop_close(void *data, int *fds)
126 {
127 return 0;
128 }
129
130 static
131 void track_std_fds(struct fd_tracker *tracker)
132 {
133 int i;
134 struct { int fd; const char *name; } files[] = {
135 { .fd = fileno(stdin), .name = "stdin" },
136 { .fd = fileno(stdout), .name = "stdout" },
137 { .fd = fileno(stderr), .name = "stderr" },
138 };
139
140 for (i = 0; i < sizeof(files) / sizeof(*files); i++) {
141 int out_fd, ret;
142
143 ret = fd_tracker_open_unsuspendable_fd(tracker, &out_fd,
144 &files[i].name, 1, noop_open, &files[i].fd);
145 assert(out_fd == files[i].fd);
146
147 ok(ret == 0, "Track unsuspendable fd %d (%s)", files[i].fd,
148 files[i].name);
149 }
150 }
151
152 static
153 void untrack_std_fds(struct fd_tracker *tracker)
154 {
155 int i;
156 struct { int fd; const char *name; } files[] = {
157 { .fd = fileno(stdin), .name = "stdin" },
158 { .fd = fileno(stdout), .name = "stdout" },
159 { .fd = fileno(stderr), .name = "stderr" },
160 };
161 unsigned int fds_set_to_minus_1 = 0;
162
163 for (i = 0; i < sizeof(files) / sizeof(*files); i++) {
164 int fd = files[i].fd;
165 int ret = fd_tracker_close_unsuspendable_fd(tracker,
166 &files[i].fd, 1, noop_close, NULL);
167
168 ok(ret == 0, "Untrack unsuspendable fd %d (%s)", fd,
169 files[i].name);
170 fds_set_to_minus_1 += (files[i].fd == -1);
171 }
172 }
173
174 /*
175 * Basic test opening and closing three unsuspendable fds.
176 */
177 static
178 void test_unsuspendable_basic(void)
179 {
180 int ret;
181 struct fd_tracker *tracker;
182 char *test_directory = NULL, *unlinked_files_directory = NULL;
183
184 get_temporary_directories(&test_directory, &unlinked_files_directory);
185
186 tracker = fd_tracker_create(unlinked_files_directory, TRACKER_FD_LIMIT);
187 ok(tracker, "Created an fd tracker with a limit of %d simulateously opened file descriptors",
188 TRACKER_FD_LIMIT);
189 if (!tracker) {
190 goto end;
191 }
192
193 track_std_fds(tracker);
194 untrack_std_fds(tracker);
195
196 fd_tracker_destroy(tracker);
197 ret = rmdir(test_directory);
198 ok(ret == 0, "Test directory is empty");
199 end:
200 free(test_directory);
201 free(unlinked_files_directory);
202 }
203
204 static
205 int error_open(void *data, int *fds)
206 {
207 return *((int *) data);
208 }
209
210 static
211 int error_close(void *data, int *fds)
212 {
213 return *((int *) data);
214 }
215
216 /*
217 * Validate that user callback return values are returned to the
218 * caller of the fd tracker.
219 */
220 static
221 void test_unsuspendable_cb_return(void)
222 {
223 int ret, stdout_fd = fileno(stdout), out_fd = 42;
224 struct fd_tracker *tracker;
225 int expected_error = -ENETDOWN;
226 char *test_directory = NULL, *unlinked_files_directory = NULL;
227
228 get_temporary_directories(&test_directory, &unlinked_files_directory);
229
230 tracker = fd_tracker_create(test_directory, TRACKER_FD_LIMIT);
231 assert(tracker);
232
233 /* The error_open callback should fail and return 'expected_error'. */
234 ret = fd_tracker_open_unsuspendable_fd(tracker, &out_fd,
235 NULL, 1, error_open, &expected_error);
236 ok(ret == expected_error, "fd_tracker_open_unsuspendable_fd() forwards the user callback's error code");
237 ok(out_fd == 42, "Output fd parameter is unaffected on error of fd_tracker_open_unsuspendable_fd()");
238
239 /*
240 * Track a valid fd since we don't want the tracker to fail with an
241 * invalid fd error for this test.
242 */
243 ret = fd_tracker_open_unsuspendable_fd(tracker, &out_fd,
244 NULL, 1, noop_open, &stdout_fd);
245 ok(out_fd == stdout_fd, "fd_tracker_open_unsuspendable_fd() sets the output fd parameter to the newly-tracked fd's value");
246 assert(!ret);
247
248 ret = fd_tracker_close_unsuspendable_fd(tracker,
249 &stdout_fd, 1, error_close, &expected_error);
250 ok(ret == expected_error, "fd_tracker_close_unsuspendable_fd() forwards the user callback's error code");
251 ret = fd_tracker_close_unsuspendable_fd(tracker,
252 &stdout_fd, 1, noop_close, &expected_error);
253 assert(!ret);
254
255 fd_tracker_destroy(tracker);
256 ret = rmdir(test_directory);
257 ok(ret == 0, "Test directory is empty");
258 free(test_directory);
259 free(unlinked_files_directory);
260 }
261
262 /*
263 * Validate that the tracker refuses to track two identical unsuspendable
264 * file descriptors.
265 */
266 static
267 void test_unsuspendable_duplicate(void)
268 {
269 int ret, stdout_fd = fileno(stdout), out_fd;
270 struct fd_tracker *tracker;
271 char *test_directory = NULL, *unlinked_files_directory = NULL;
272
273 get_temporary_directories(&test_directory, &unlinked_files_directory);
274
275 tracker = fd_tracker_create(unlinked_files_directory, TRACKER_FD_LIMIT);
276 assert(tracker);
277
278 ret = fd_tracker_open_unsuspendable_fd(tracker, &out_fd,
279 NULL, 1, noop_open, &stdout_fd);
280 assert(!ret);
281 ret = fd_tracker_open_unsuspendable_fd(tracker, &out_fd,
282 NULL, 1, noop_open, &stdout_fd);
283 ok(ret == -EEXIST, "EEXIST reported on open of an already tracked file descriptor");
284
285 ret = fd_tracker_close_unsuspendable_fd(tracker,
286 &stdout_fd, 1, noop_close, NULL);
287 assert(!ret);
288
289 fd_tracker_destroy(tracker);
290 ret = rmdir(test_directory);
291 ok(ret == 0, "Test directory is empty");
292 free(test_directory);
293 free(unlinked_files_directory);
294 }
295
296 static
297 int open_pipes(void *data, int *out_fds)
298 {
299 unsigned int i;
300 const unsigned int pipe_count = TRACKER_FD_LIMIT / 2;
301
302 for (i = 0; i < pipe_count; i++) {
303 int ret = pipe(&out_fds[i * 2]);
304
305 if (ret) {
306 return -errno;
307 }
308 }
309 return 0;
310 }
311
312 static
313 int close_pipes(void *data, int *fds)
314 {
315 int i;
316 int *pipes = fds;
317
318 for (i = 0; i < TRACKER_FD_LIMIT; i++) {
319 int ret = close(pipes[i]);
320
321 if (ret) {
322 return -errno;
323 }
324 }
325 return 0;
326 }
327
328 /*
329 * Validate that the tracker enforces the open file descriptor limit
330 * when unsuspendable file descritptors are being opened.
331 */
332 static
333 void test_unsuspendable_limit(void)
334 {
335 struct fd_tracker *tracker;
336 int ret, stdout_fd = fileno(stdout), out_fd;
337 int fds[TRACKER_FD_LIMIT];
338 char *test_directory = NULL, *unlinked_files_directory = NULL;
339
340 get_temporary_directories(&test_directory, &unlinked_files_directory);
341
342 /* This test assumes TRACKER_FD_LIMIT is a multiple of 2. */
343 assert((TRACKER_FD_LIMIT % 2 == 0) && TRACKER_FD_LIMIT);
344
345 tracker = fd_tracker_create(unlinked_files_directory, TRACKER_FD_LIMIT);
346 assert(tracker);
347
348 ret = fd_tracker_open_unsuspendable_fd(tracker, fds,
349 NULL, TRACKER_FD_LIMIT, open_pipes, NULL);
350 ok(ret == 0, "File descriptor tracker allowed the user to meet its limit with unsuspendable file descritptors (%d)",
351 TRACKER_FD_LIMIT);
352
353 ret = fd_tracker_open_unsuspendable_fd(tracker, &out_fd,
354 NULL, 1, noop_open, &stdout_fd);
355 ok(ret == -EMFILE, "EMFILE reported when exceeding the file descriptor limit while opening an unsuspendable fd");
356
357 ret = fd_tracker_close_unsuspendable_fd(tracker,
358 fds, TRACKER_FD_LIMIT, close_pipes, NULL);
359 assert(!ret);
360
361 fd_tracker_destroy(tracker);
362 ret = rmdir(test_directory);
363 ok(ret == 0, "Test directory is empty");
364 free(test_directory);
365 free(unlinked_files_directory);
366 }
367
368 /*
369 * Validate that the tracker refuses to track two identical unsuspendable
370 * file descriptors.
371 */
372 static
373 void test_unsuspendable_close_untracked(void)
374 {
375 int ret, stdout_fd = fileno(stdout), unknown_fds[2], out_fd;
376 struct fd_tracker *tracker;
377 char *test_directory = NULL, *unlinked_files_directory = NULL;
378
379 get_temporary_directories(&test_directory, &unlinked_files_directory);
380
381 tracker = fd_tracker_create(unlinked_files_directory, TRACKER_FD_LIMIT);
382 if (!tracker) {
383 goto end;;
384 }
385
386 ret = pipe(unknown_fds);
387 assert(!ret);
388 assert(close(unknown_fds[0]) == 0);
389 assert(close(unknown_fds[1]) == 0);
390
391 ret = fd_tracker_open_unsuspendable_fd(tracker, &out_fd,
392 NULL, 1, noop_open, &stdout_fd);
393 assert(!ret);
394
395 ret = fd_tracker_close_unsuspendable_fd(tracker,
396 unknown_fds, 1, noop_close, NULL);
397 ok(ret == -EINVAL, "EINVAL reported on close of an untracked file descriptor");
398
399 ret = fd_tracker_close_unsuspendable_fd(tracker,
400 &stdout_fd, 1, noop_close, NULL);
401 assert(!ret);
402
403 fd_tracker_destroy(tracker);
404 ret = rmdir(test_directory);
405 ok(ret == 0, "Test directory is empty");
406 end:
407 free(test_directory);
408 free(unlinked_files_directory);
409 }
410
411 static int open_files(struct fd_tracker *tracker,
412 struct lttng_directory_handle *directory,
413 unsigned int count,
414 struct fs_handle **handles,
415 char **file_paths)
416 {
417 int ret = 0;
418 unsigned int i;
419
420 for (i = 0; i < count; i++) {
421 int p_ret;
422 char *file_path;
423 struct fs_handle *handle;
424 mode_t mode = S_IWUSR | S_IRUSR;
425
426 p_ret = asprintf(&file_path, "file-%u", i);
427 assert(p_ret >= 0);
428 file_paths[i] = file_path;
429
430 handle = fd_tracker_open_fs_handle(tracker, directory, file_path,
431 O_RDWR | O_CREAT, &mode);
432 if (!handle) {
433 ret = -1;
434 break;
435 }
436 handles[i] = handle;
437 }
438 return ret;
439 }
440
441 static int open_same_file(struct fd_tracker *tracker,
442 struct lttng_directory_handle *directory,
443 const char *file,
444 unsigned int count,
445 struct fs_handle **handles)
446 {
447 int ret = 0;
448 unsigned int i;
449
450 for (i = 0; i < count; i++) {
451 struct fs_handle *handle;
452 mode_t mode = S_IWUSR | S_IRUSR;
453
454 handle = fd_tracker_open_fs_handle(tracker, directory, file,
455 O_RDWR | O_CREAT, &mode);
456 if (!handle) {
457 ret = -1;
458 break;
459 }
460 handles[i] = handle;
461 }
462 return ret;
463 }
464
465 static
466 int cleanup_files(struct fd_tracker *tracker, const char *dir,
467 unsigned int count, struct fs_handle **handles,
468 char **file_paths)
469 {
470 int ret = 0;
471 unsigned int i;
472
473 for (i = 0; i < count; i++) {
474 char *file_path = file_paths[i];
475
476 if (!file_path) {
477 break;
478 }
479 if (fs_handle_unlink(handles[i])) {
480 diag("Failed to unlink fs_handle to file %s", file_path);
481 ret = -1;
482 }
483 if (fs_handle_close(handles[i])) {
484 diag("Failed to close fs_handle to file %s", file_path);
485 ret = -1;
486 }
487 free(file_path);
488 }
489 return ret;
490 }
491
492 static
493 void test_suspendable_limit(void)
494 {
495 int ret;
496 const int files_to_create = TRACKER_FD_LIMIT * 10;
497 struct fd_tracker *tracker;
498 char *test_directory = NULL, *unlinked_files_directory = NULL;
499 char *output_files[files_to_create];
500 struct fs_handle *handles[files_to_create];
501 struct lttng_directory_handle *dir_handle = NULL;
502 int dir_handle_fd_count;
503
504 memset(output_files, 0, sizeof(output_files));
505 memset(handles, 0, sizeof(handles));
506
507 get_temporary_directories(&test_directory, &unlinked_files_directory);
508
509 tracker = fd_tracker_create(unlinked_files_directory, TRACKER_FD_LIMIT);
510 if (!tracker) {
511 goto end;
512 }
513
514 dir_handle = lttng_directory_handle_create(test_directory);
515 assert(dir_handle);
516 dir_handle_fd_count = !!lttng_directory_handle_uses_fd(dir_handle);
517
518 ret = open_files(tracker, dir_handle, files_to_create, handles,
519 output_files);
520 ok(!ret, "Created %d files with a limit of %d simultaneously-opened file descriptor",
521 files_to_create, TRACKER_FD_LIMIT);
522 check_fd_count(TRACKER_FD_LIMIT + STDIO_FD_COUNT + unknown_fds_count +
523 dir_handle_fd_count);
524
525 ret = cleanup_files(tracker, test_directory, files_to_create, handles,
526 output_files);
527 ok(!ret, "Close all opened filesystem handles");
528 ret = rmdir(test_directory);
529 ok(ret == 0, "Test directory is empty");
530 fd_tracker_destroy(tracker);
531 lttng_directory_handle_put(dir_handle);
532 end:
533 free(test_directory);
534 free(unlinked_files_directory);
535 }
536
537 static
538 void test_mixed_limit(void)
539 {
540 int ret;
541 const int files_to_create = TRACKER_FD_LIMIT;
542 struct fd_tracker *tracker;
543 char *test_directory = NULL, *unlinked_files_directory = NULL;
544 char *output_files[files_to_create];
545 struct fs_handle *handles[files_to_create];
546 struct lttng_directory_handle *dir_handle = NULL;
547 int dir_handle_fd_count;
548
549 memset(output_files, 0, sizeof(output_files));
550 memset(handles, 0, sizeof(handles));
551
552 get_temporary_directories(&test_directory, &unlinked_files_directory);
553
554 tracker = fd_tracker_create(unlinked_files_directory, TRACKER_FD_LIMIT);
555 if (!tracker) {
556 goto end;
557 }
558
559 dir_handle = lttng_directory_handle_create(test_directory);
560 assert(dir_handle);
561 dir_handle_fd_count = !!lttng_directory_handle_uses_fd(dir_handle);
562
563 ret = open_files(tracker, dir_handle, files_to_create, handles,
564 output_files);
565 ok(!ret, "Created %d files with a limit of %d simultaneously-opened file descriptor",
566 files_to_create, TRACKER_FD_LIMIT);
567 diag("Check file descriptor count after opening %u files", files_to_create);
568 check_fd_count(TRACKER_FD_LIMIT + STDIO_FD_COUNT + unknown_fds_count +
569 dir_handle_fd_count);
570
571 /*
572 * Open unsuspendable fds (stdin, stdout, stderr) and verify that the fd
573 * cap is still respected.
574 */
575 diag("Check file descriptor count after adding %d unsuspendable fds",
576 STDIO_FD_COUNT);
577 track_std_fds(tracker);
578 check_fd_count(TRACKER_FD_LIMIT + unknown_fds_count +
579 dir_handle_fd_count);
580 diag("Untrack unsuspendable file descriptors");
581 untrack_std_fds(tracker);
582 check_fd_count(TRACKER_FD_LIMIT + unknown_fds_count +
583 dir_handle_fd_count);
584
585 ret = cleanup_files(tracker, test_directory, files_to_create, handles,
586 output_files);
587 ok(!ret, "Close all opened filesystem handles");
588 ret = rmdir(test_directory);
589 ok(ret == 0, "Test directory is empty");
590 fd_tracker_destroy(tracker);
591 lttng_directory_handle_put(dir_handle);
592 end:
593 free(test_directory);
594 free(unlinked_files_directory);
595 }
596
597 /*
598 * Open more files than allowed by the fd tracker's cap and write,
599 * byte-by-byte, and in round-robin, a string. The goal is to force
600 * the fd tracker to suspend and resume the fs_handles often and
601 * verify that the fd cap is always respected.
602 *
603 * The content of the files is also verified at the end.
604 */
605 static
606 void test_suspendable_restore(void)
607 {
608 int ret;
609 const int files_to_create = TRACKER_FD_LIMIT * 10;
610 struct fd_tracker *tracker;
611 char *output_files[files_to_create];
612 struct fs_handle *handles[files_to_create];
613 size_t content_index;
614 int handle_index;
615 bool write_success = true;
616 bool fd_cap_respected = true;
617 bool content_ok = true;
618 struct lttng_directory_handle *dir_handle = NULL;
619 int dir_handle_fd_count;
620 char *test_directory = NULL, *unlinked_files_directory = NULL;
621
622 memset(output_files, 0, sizeof(output_files));
623 memset(handles, 0, sizeof(handles));
624
625 get_temporary_directories(&test_directory, &unlinked_files_directory);
626
627 tracker = fd_tracker_create(unlinked_files_directory, TRACKER_FD_LIMIT);
628 if (!tracker) {
629 goto end;
630 }
631
632 dir_handle = lttng_directory_handle_create(test_directory);
633 assert(dir_handle);
634 dir_handle_fd_count = !!lttng_directory_handle_uses_fd(dir_handle);
635
636 ret = open_files(tracker, dir_handle, files_to_create, handles,
637 output_files);
638 ok(!ret, "Created %d files with a limit of %d simultaneously-opened file descriptor",
639 files_to_create, TRACKER_FD_LIMIT);
640 diag("Check file descriptor count after opening %u files", files_to_create);
641 check_fd_count(TRACKER_FD_LIMIT + STDIO_FD_COUNT + unknown_fds_count +
642 dir_handle_fd_count);
643
644 for (content_index = 0; content_index < sizeof(file_contents); content_index++) {
645 for (handle_index = 0; handle_index < files_to_create; handle_index++) {
646 int fd;
647 struct fs_handle *handle = handles[handle_index];
648 const char *path = output_files[handle_index];
649
650 fd = fs_handle_get_fd(handle);
651 if (fd < 0) {
652 write_success = false;
653 diag("Failed to restore fs_handle to %s",
654 path);
655 goto skip_write;
656 }
657
658 do {
659 ret = write(fd, file_contents + content_index, 1);
660 } while (ret < 0 && errno == EINTR);
661
662 if (ret != 1) {
663 write_success = false;
664 PERROR("write() to %s failed", path);
665 goto skip_write;
666 }
667
668 if (fd_count() > (TRACKER_FD_LIMIT + STDIO_FD_COUNT +
669 unknown_fds_count +
670 dir_handle_fd_count)) {
671 fd_cap_respected = false;
672 }
673
674 fs_handle_put_fd(handle);
675 }
676 }
677 skip_write:
678 ok(write_success, "Wrote reference string to %d files",
679 files_to_create);
680 ok(fd_cap_respected, "FD tracker enforced the file descriptor cap");
681
682 /* Validate the contents of the files. */
683 for (handle_index = 0; handle_index < files_to_create; handle_index++) {
684 struct stat fd_stat;
685 const char *path = output_files[handle_index];
686 char read_buf[sizeof(file_contents)];
687 char *read_pos;
688 size_t to_read = sizeof(read_buf);
689 int fd;
690
691 fd = lttng_directory_handle_open_file(
692 dir_handle, path, O_RDONLY, 0);
693 assert(fd >= 0);
694 ret = fstat(fd, &fd_stat);
695 assert(!ret);
696 if (fd_stat.st_size != sizeof(file_contents)) {
697 diag("Content size of file %s doesn't match, got %" PRId64 ", expected %zu",
698 path, (int64_t) fd_stat.st_size,
699 sizeof(file_contents));
700 content_ok = false;
701 (void) close(fd);
702 break;
703 }
704
705 read_pos = read_buf;
706 do {
707 ret = read(fd, read_pos, to_read);
708 if (ret > 0) {
709 to_read -= ret;
710 read_pos += ret;
711 }
712 } while (to_read && (ret < 0 && errno == EINTR));
713 if (ret < 0) {
714 content_ok = false;
715 PERROR("Failed to read file %s", path);
716 (void) close(fd);
717 break;
718 }
719
720 if (strcmp(file_contents, read_buf)) {
721 content_ok = false;
722 diag("File content doesn't match the expectated string");
723 (void) close(fd);
724 break;
725 }
726 (void) close(fd);
727 }
728 ok(content_ok, "Files contain the expected content");
729 ret = cleanup_files(tracker, test_directory, files_to_create, handles,
730 output_files);
731 ok(!ret, "Close all opened filesystem handles");
732 ret = rmdir(test_directory);
733 ok(ret == 0, "Test directory is empty");
734 fd_tracker_destroy(tracker);
735 lttng_directory_handle_put(dir_handle);
736 end:
737 free(test_directory);
738 free(unlinked_files_directory);
739 }
740
741 static
742 void test_unlink(void)
743 {
744 int ret;
745 struct fd_tracker *tracker;
746 const int handles_to_open = 2;
747 struct fs_handle *handles[handles_to_open];
748 struct fs_handle *new_handle = NULL;
749 struct stat statbuf;
750 struct lttng_directory_handle *dir_handle = NULL;
751 const char file_name[] = "my_file";
752 char *test_directory = NULL, *unlinked_files_directory = NULL;
753 char *unlinked_file_zero = NULL, *unlinked_file_one = NULL;
754 int fd;
755
756 get_temporary_directories(&test_directory, &unlinked_files_directory);
757 ret = asprintf(&unlinked_file_zero, "%s/%u", unlinked_files_directory,
758 0);
759 assert(ret > 0);
760 ret = asprintf(&unlinked_file_one, "%s/%u", unlinked_files_directory,
761 1);
762 assert(ret > 0);
763
764 tracker = fd_tracker_create(unlinked_files_directory, 1);
765 if (!tracker) {
766 goto end;
767 }
768
769 dir_handle = lttng_directory_handle_create(test_directory);
770 assert(dir_handle);
771
772 /* Open two handles to the same file. */
773 ret = open_same_file(tracker, dir_handle, file_name, handles_to_open,
774 handles);
775 ok(!ret, "Successfully opened %i handles to %s/%s", handles_to_open,
776 test_directory, file_name);
777 if (ret) {
778 goto end;
779 }
780
781 /*
782 * Unlinking the first handle should cause the file to be renamed
783 * to '0'.
784 */
785 ret = fs_handle_unlink(handles[0]);
786 ok(!ret, "Successfully unlinked the first handle to %s/%s",
787 test_directory, file_name);
788
789 /*
790 * The original file should no longer exist on the file system, and a
791 * new file named '0' should exist.
792 */
793 ok(lttng_directory_handle_stat(dir_handle, file_name, &statbuf) == -1 &&
794 errno == ENOENT,
795 "%s no longer present on file system after unlink",
796 file_name);
797 ok(lttng_directory_handle_stat(
798 dir_handle, unlinked_file_zero, &statbuf) == 0,
799 "%s exists on file system after unlink",
800 unlinked_file_zero);
801
802 /*
803 * It should be possible to use the file descriptors of both handles.
804 * Since only one file descriptor can be opened at once, this should
805 * force the fd_tracker to suspend and restore the handles.
806 */
807 fd = fs_handle_get_fd(handles[0]);
808 ok(fd >= 0, "Got fd from first handle");
809
810 fd = fs_handle_get_fd(handles[1]);
811 ok (fd < 0, "fd tracker does not allow two fds to be used at once");
812
813 fs_handle_put_fd(handles[0]);
814 fd = fs_handle_get_fd(handles[1]);
815 ok(fd >= 0, "Got fd from second handle");
816 fs_handle_put_fd(handles[1]);
817
818 /* The second unlink should fail with -ENOENT. */
819 ret = fs_handle_unlink(handles[1]);
820 ok(ret == -ENOENT,
821 "ENOENT is reported when attempting to unlink the second handle to %s/%s",
822 test_directory, file_name);
823
824 /*
825 * Opening a new handle to 'my_file' should succeed.
826 */
827 ret = open_same_file(tracker, dir_handle, file_name, 1, &new_handle);
828 ok(!ret, "Successfully opened a new handle to previously unlinked file %s/%s",
829 test_directory, file_name);
830 assert(new_handle);
831
832 /*
833 * Unlinking the new handle should cause the file to be renamed
834 * to '1' since '0' already exists.
835 */
836 ret = fs_handle_unlink(new_handle);
837 ok(!ret, "Successfully unlinked the new handle handle to %s/%s",
838 test_directory, file_name);
839 ok(stat(unlinked_file_one, &statbuf) == 0,
840 "%s exists on file system after unlink",
841 unlinked_file_one);
842
843 ret = fs_handle_close(handles[0]);
844 ok(!ret, "Successfully closed the first handle");
845 ret = fs_handle_close(handles[1]);
846 ok(!ret, "Successfully closed the second handle");
847 ret = fs_handle_close(new_handle);
848 ok(!ret, "Successfully closed the third handle");
849
850 ok(lttng_directory_handle_stat(dir_handle, file_name, &statbuf) == -1 &&
851 errno == ENOENT,
852 "%s no longer present on file system after handle close",
853 file_name);
854 ok(lttng_directory_handle_stat(
855 dir_handle, unlinked_file_zero, &statbuf) == -1 &&
856 errno == ENOENT,
857 "%s no longer present on file system after handle close",
858 unlinked_file_zero);
859 ok(lttng_directory_handle_stat(dir_handle, unlinked_file_one,
860 &statbuf) == -1 &&
861 errno == ENOENT,
862 "%s no longer present on file system after handle close",
863 unlinked_file_one);
864
865 ret = rmdir(test_directory);
866 ok(ret == 0, "Test directory is empty");
867 end:
868 fd_tracker_destroy(tracker);
869 free(test_directory);
870 free(unlinked_files_directory);
871 free(unlinked_file_zero);
872 free(unlinked_file_one);
873 lttng_directory_handle_put(dir_handle);
874 }
875
876 int main(int argc, char **argv)
877 {
878 plan_tests(NUM_TESTS);
879 diag("File descriptor tracker unit tests");
880
881 rcu_register_thread();
882
883 unknown_fds_count = fd_count() - STDIO_FD_COUNT;
884 assert(unknown_fds_count >= 0);
885
886 diag("Unsuspendable - basic");
887 test_unsuspendable_basic();
888 diag("Unsuspendable - callback return values");
889 test_unsuspendable_cb_return();
890 diag("Unsuspendable - duplicate file descriptors");
891 test_unsuspendable_duplicate();
892 diag("Unsuspendable - closing an untracked file descriptor");
893 test_unsuspendable_close_untracked();
894 diag("Unsuspendable - check that file descritptor limit is enforced");
895 test_unsuspendable_limit();
896
897 diag("Suspendable - check that file descritptor limit is enforced");
898 test_suspendable_limit();
899 diag("Suspendable - restoration test");
900 test_suspendable_restore();
901
902 diag("Mixed - check that file descritptor limit is enforced");
903 test_mixed_limit();
904
905 diag("Suspendable - Unlinking test");
906 test_unlink();
907
908 rcu_barrier();
909 rcu_unregister_thread();
910 return exit_status();
911 }
This page took 0.048848 seconds and 4 git commands to generate.