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