Tests: fd-tracker: fix: leak of test paths
[lttng-tools.git] / tests / unit / test_fd_tracker.c
CommitLineData
9ca3e8a2 1/*
9d16b343 2 * Copyright (C) 2018 Jérémie Galarneau <jeremie.galarneau@efficios.com>
9ca3e8a2 3 *
9d16b343 4 * SPDX-License-Identifier: GPL-2.0-only
9ca3e8a2 5 *
9ca3e8a2
JG
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>
f0faf175 22#include <sys/types.h>
9ca3e8a2
JG
23
24#include <urcu.h>
25
f7c3ffd7 26#include <common/compat/directory-handle.h>
9ca3e8a2 27#include <common/error.h>
f7c3ffd7 28#include <common/fd-tracker/fd-tracker.h>
9ca3e8a2
JG
29
30/* For error.h */
31int lttng_opt_quiet = 1;
32int lttng_opt_verbose;
33int lttng_opt_mi;
34
35/* Number of TAP tests in this file */
f7c3ffd7 36#define NUM_TESTS 61
9ca3e8a2
JG
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"
f7c3ffd7 41#define TEST_UNLINK_DIRECTORY_NAME "unlinked_files"
9ca3e8a2
JG
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 */
49int unknown_fds_count;
50
51const 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
f7c3ffd7
JG
57void 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
9ca3e8a2
JG
79int 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--;
e0efb2c8
JG
100 if (closedir(dir)) {
101 perror("# Failed to close test program's self/fd directory file descriptor");
102 }
9ca3e8a2
JG
103end:
104 return count;
105}
106
107static
108void 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
117static
118int noop_open(void *data, int *fds)
119{
120 *fds = *((int *) data);
121 return 0;
122}
123
124static
125int noop_close(void *data, int *fds)
126{
127 return 0;
128}
129
130static
131void 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
152static
153void 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 */
177static
178void test_unsuspendable_basic(void)
179{
f7c3ffd7 180 int ret;
9ca3e8a2 181 struct fd_tracker *tracker;
f7c3ffd7
JG
182 char *test_directory = NULL, *unlinked_files_directory = NULL;
183
184 get_temporary_directories(&test_directory, &unlinked_files_directory);
9ca3e8a2 185
f7c3ffd7 186 tracker = fd_tracker_create(unlinked_files_directory, TRACKER_FD_LIMIT);
9ca3e8a2
JG
187 ok(tracker, "Created an fd tracker with a limit of %d simulateously opened file descriptors",
188 TRACKER_FD_LIMIT);
189 if (!tracker) {
f6bef966 190 goto end;
9ca3e8a2
JG
191 }
192
193 track_std_fds(tracker);
194 untrack_std_fds(tracker);
195
196 fd_tracker_destroy(tracker);
f7c3ffd7
JG
197 ret = rmdir(test_directory);
198 ok(ret == 0, "Test directory is empty");
f6bef966 199end:
f7c3ffd7
JG
200 free(test_directory);
201 free(unlinked_files_directory);
9ca3e8a2
JG
202}
203
204static
205int error_open(void *data, int *fds)
206{
207 return *((int *) data);
208}
209
210static
211int 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 */
220static
221void 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;
f7c3ffd7 226 char *test_directory = NULL, *unlinked_files_directory = NULL;
9ca3e8a2 227
f7c3ffd7
JG
228 get_temporary_directories(&test_directory, &unlinked_files_directory);
229
230 tracker = fd_tracker_create(test_directory, TRACKER_FD_LIMIT);
9ca3e8a2
JG
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);
f7c3ffd7
JG
256 ret = rmdir(test_directory);
257 ok(ret == 0, "Test directory is empty");
258 free(test_directory);
259 free(unlinked_files_directory);
9ca3e8a2
JG
260}
261
262/*
263 * Validate that the tracker refuses to track two identical unsuspendable
264 * file descriptors.
265 */
266static
267void test_unsuspendable_duplicate(void)
268{
269 int ret, stdout_fd = fileno(stdout), out_fd;
270 struct fd_tracker *tracker;
f7c3ffd7
JG
271 char *test_directory = NULL, *unlinked_files_directory = NULL;
272
273 get_temporary_directories(&test_directory, &unlinked_files_directory);
9ca3e8a2 274
f7c3ffd7 275 tracker = fd_tracker_create(unlinked_files_directory, TRACKER_FD_LIMIT);
9ca3e8a2
JG
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);
f7c3ffd7
JG
290 ret = rmdir(test_directory);
291 ok(ret == 0, "Test directory is empty");
292 free(test_directory);
293 free(unlinked_files_directory);
9ca3e8a2
JG
294}
295
296static
297int 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
312static
313int 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 */
332static
333void 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];
f7c3ffd7
JG
338 char *test_directory = NULL, *unlinked_files_directory = NULL;
339
340 get_temporary_directories(&test_directory, &unlinked_files_directory);
9ca3e8a2
JG
341
342 /* This test assumes TRACKER_FD_LIMIT is a multiple of 2. */
343 assert((TRACKER_FD_LIMIT % 2 == 0) && TRACKER_FD_LIMIT);
344
f7c3ffd7 345 tracker = fd_tracker_create(unlinked_files_directory, TRACKER_FD_LIMIT);
9ca3e8a2
JG
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);
f7c3ffd7
JG
362 ret = rmdir(test_directory);
363 ok(ret == 0, "Test directory is empty");
364 free(test_directory);
365 free(unlinked_files_directory);
9ca3e8a2
JG
366}
367
368/*
369 * Validate that the tracker refuses to track two identical unsuspendable
370 * file descriptors.
371 */
372static
373void test_unsuspendable_close_untracked(void)
374{
375 int ret, stdout_fd = fileno(stdout), unknown_fds[2], out_fd;
376 struct fd_tracker *tracker;
f7c3ffd7 377 char *test_directory = NULL, *unlinked_files_directory = NULL;
9ca3e8a2 378
f7c3ffd7
JG
379 get_temporary_directories(&test_directory, &unlinked_files_directory);
380
381 tracker = fd_tracker_create(unlinked_files_directory, TRACKER_FD_LIMIT);
9ca3e8a2 382 if (!tracker) {
f6bef966 383 goto end;;
9ca3e8a2
JG
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);
f7c3ffd7
JG
404 ret = rmdir(test_directory);
405 ok(ret == 0, "Test directory is empty");
f6bef966 406end:
f7c3ffd7
JG
407 free(test_directory);
408 free(unlinked_files_directory);
9ca3e8a2
JG
409}
410
f7c3ffd7
JG
411static 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)
9ca3e8a2
JG
416{
417 int ret = 0;
418 unsigned int i;
419
420 for (i = 0; i < count; i++) {
f0faf175 421 int p_ret;
9ca3e8a2
JG
422 char *file_path;
423 struct fs_handle *handle;
424 mode_t mode = S_IWUSR | S_IRUSR;
425
f7c3ffd7 426 p_ret = asprintf(&file_path, "file-%u", i);
f0faf175 427 assert(p_ret >= 0);
9ca3e8a2
JG
428 file_paths[i] = file_path;
429
f7c3ffd7 430 handle = fd_tracker_open_fs_handle(tracker, directory, file_path,
9ca3e8a2
JG
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
f7c3ffd7
JG
441static 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)
f0faf175
JG
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
f7c3ffd7 454 handle = fd_tracker_open_fs_handle(tracker, directory, file,
f0faf175
JG
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
9ca3e8a2
JG
465static
466int 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 }
f7c3ffd7
JG
479 if (fs_handle_unlink(handles[i])) {
480 diag("Failed to unlink fs_handle to file %s", file_path);
481 ret = -1;
482 }
9ca3e8a2 483 if (fs_handle_close(handles[i])) {
f7c3ffd7 484 diag("Failed to close fs_handle to file %s", file_path);
9ca3e8a2
JG
485 ret = -1;
486 }
f7c3ffd7 487 free(file_path);
9ca3e8a2
JG
488 }
489 return ret;
490}
491
492static
493void test_suspendable_limit(void)
494{
495 int ret;
496 const int files_to_create = TRACKER_FD_LIMIT * 10;
497 struct fd_tracker *tracker;
f7c3ffd7 498 char *test_directory = NULL, *unlinked_files_directory = NULL;
9ca3e8a2
JG
499 char *output_files[files_to_create];
500 struct fs_handle *handles[files_to_create];
f7c3ffd7
JG
501 struct lttng_directory_handle *dir_handle = NULL;
502 int dir_handle_fd_count;
9ca3e8a2
JG
503
504 memset(output_files, 0, sizeof(output_files));
505 memset(handles, 0, sizeof(handles));
506
f7c3ffd7
JG
507 get_temporary_directories(&test_directory, &unlinked_files_directory);
508
509 tracker = fd_tracker_create(unlinked_files_directory, TRACKER_FD_LIMIT);
9ca3e8a2 510 if (!tracker) {
f6bef966 511 goto end;
9ca3e8a2
JG
512 }
513
f7c3ffd7
JG
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);
9ca3e8a2 517
f7c3ffd7 518 ret = open_files(tracker, dir_handle, files_to_create, handles,
9ca3e8a2
JG
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);
f7c3ffd7
JG
522 check_fd_count(TRACKER_FD_LIMIT + STDIO_FD_COUNT + unknown_fds_count +
523 dir_handle_fd_count);
9ca3e8a2 524
f7c3ffd7 525 ret = cleanup_files(tracker, test_directory, files_to_create, handles,
9ca3e8a2
JG
526 output_files);
527 ok(!ret, "Close all opened filesystem handles");
f7c3ffd7
JG
528 ret = rmdir(test_directory);
529 ok(ret == 0, "Test directory is empty");
9ca3e8a2 530 fd_tracker_destroy(tracker);
f7c3ffd7 531 lttng_directory_handle_put(dir_handle);
f6bef966 532end:
f7c3ffd7
JG
533 free(test_directory);
534 free(unlinked_files_directory);
9ca3e8a2
JG
535}
536
537static
538void test_mixed_limit(void)
539{
540 int ret;
541 const int files_to_create = TRACKER_FD_LIMIT;
542 struct fd_tracker *tracker;
f7c3ffd7 543 char *test_directory = NULL, *unlinked_files_directory = NULL;
9ca3e8a2
JG
544 char *output_files[files_to_create];
545 struct fs_handle *handles[files_to_create];
f7c3ffd7
JG
546 struct lttng_directory_handle *dir_handle = NULL;
547 int dir_handle_fd_count;
9ca3e8a2
JG
548
549 memset(output_files, 0, sizeof(output_files));
550 memset(handles, 0, sizeof(handles));
551
f7c3ffd7
JG
552 get_temporary_directories(&test_directory, &unlinked_files_directory);
553
554 tracker = fd_tracker_create(unlinked_files_directory, TRACKER_FD_LIMIT);
9ca3e8a2 555 if (!tracker) {
f6bef966 556 goto end;
9ca3e8a2
JG
557 }
558
f7c3ffd7
JG
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);
9ca3e8a2 562
f7c3ffd7 563 ret = open_files(tracker, dir_handle, files_to_create, handles,
9ca3e8a2
JG
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);
f7c3ffd7
JG
568 check_fd_count(TRACKER_FD_LIMIT + STDIO_FD_COUNT + unknown_fds_count +
569 dir_handle_fd_count);
9ca3e8a2
JG
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);
f7c3ffd7
JG
578 check_fd_count(TRACKER_FD_LIMIT + unknown_fds_count +
579 dir_handle_fd_count);
9ca3e8a2
JG
580 diag("Untrack unsuspendable file descriptors");
581 untrack_std_fds(tracker);
f7c3ffd7
JG
582 check_fd_count(TRACKER_FD_LIMIT + unknown_fds_count +
583 dir_handle_fd_count);
9ca3e8a2 584
f7c3ffd7 585 ret = cleanup_files(tracker, test_directory, files_to_create, handles,
9ca3e8a2
JG
586 output_files);
587 ok(!ret, "Close all opened filesystem handles");
f7c3ffd7
JG
588 ret = rmdir(test_directory);
589 ok(ret == 0, "Test directory is empty");
9ca3e8a2 590 fd_tracker_destroy(tracker);
f7c3ffd7 591 lttng_directory_handle_put(dir_handle);
f6bef966 592end:
f7c3ffd7
JG
593 free(test_directory);
594 free(unlinked_files_directory);
9ca3e8a2
JG
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 */
605static
606void test_suspendable_restore(void)
607{
608 int ret;
609 const int files_to_create = TRACKER_FD_LIMIT * 10;
610 struct fd_tracker *tracker;
9ca3e8a2
JG
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;
f7c3ffd7
JG
618 struct lttng_directory_handle *dir_handle = NULL;
619 int dir_handle_fd_count;
620 char *test_directory = NULL, *unlinked_files_directory = NULL;
9ca3e8a2
JG
621
622 memset(output_files, 0, sizeof(output_files));
623 memset(handles, 0, sizeof(handles));
624
f7c3ffd7
JG
625 get_temporary_directories(&test_directory, &unlinked_files_directory);
626
627 tracker = fd_tracker_create(unlinked_files_directory, TRACKER_FD_LIMIT);
9ca3e8a2 628 if (!tracker) {
f6bef966 629 goto end;
9ca3e8a2
JG
630 }
631
f7c3ffd7
JG
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);
9ca3e8a2 635
f7c3ffd7 636 ret = open_files(tracker, dir_handle, files_to_create, handles,
9ca3e8a2
JG
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);
f7c3ffd7
JG
641 check_fd_count(TRACKER_FD_LIMIT + STDIO_FD_COUNT + unknown_fds_count +
642 dir_handle_fd_count);
9ca3e8a2
JG
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
f7c3ffd7
JG
668 if (fd_count() > (TRACKER_FD_LIMIT + STDIO_FD_COUNT +
669 unknown_fds_count +
670 dir_handle_fd_count)) {
9ca3e8a2
JG
671 fd_cap_respected = false;
672 }
673
f7c3ffd7 674 fs_handle_put_fd(handle);
9ca3e8a2
JG
675 }
676 }
677skip_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
f7c3ffd7
JG
691 fd = lttng_directory_handle_open_file(
692 dir_handle, path, O_RDONLY, 0);
9ca3e8a2
JG
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");
f7c3ffd7 729 ret = cleanup_files(tracker, test_directory, files_to_create, handles,
9ca3e8a2
JG
730 output_files);
731 ok(!ret, "Close all opened filesystem handles");
f7c3ffd7
JG
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);
f6bef966 736end:
f7c3ffd7
JG
737 free(test_directory);
738 free(unlinked_files_directory);
9ca3e8a2
JG
739}
740
f0faf175
JG
741static
742void test_unlink(void)
743{
744 int ret;
745 struct fd_tracker *tracker;
746 const int handles_to_open = 2;
f0faf175 747 struct fs_handle *handles[handles_to_open];
ae63e134 748 struct fs_handle *new_handle = NULL;
f0faf175 749 struct stat statbuf;
f7c3ffd7
JG
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);
f0faf175 765 if (!tracker) {
f6bef966 766 goto end;
f0faf175 767 }
f7c3ffd7
JG
768
769 dir_handle = lttng_directory_handle_create(test_directory);
770 assert(dir_handle);
f0faf175
JG
771
772 /* Open two handles to the same file. */
f7c3ffd7
JG
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);
f0faf175 777 if (ret) {
f6bef966 778 goto end;
f0faf175
JG
779 }
780
781 /*
782 * Unlinking the first handle should cause the file to be renamed
f7c3ffd7 783 * to '0'.
f0faf175
JG
784 */
785 ret = fs_handle_unlink(handles[0]);
f7c3ffd7
JG
786 ok(!ret, "Successfully unlinked the first handle to %s/%s",
787 test_directory, file_name);
f0faf175
JG
788
789 /*
790 * The original file should no longer exist on the file system, and a
f7c3ffd7 791 * new file named '0' should exist.
f0faf175 792 */
f7c3ffd7
JG
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]);
f0faf175
JG
817
818 /* The second unlink should fail with -ENOENT. */
819 ret = fs_handle_unlink(handles[1]);
f7c3ffd7
JG
820 ok(ret == -ENOENT,
821 "ENOENT is reported when attempting to unlink the second handle to %s/%s",
822 test_directory, file_name);
f0faf175
JG
823
824 /*
825 * Opening a new handle to 'my_file' should succeed.
826 */
f7c3ffd7
JG
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);
f0faf175
JG
830 assert(new_handle);
831
832 /*
833 * Unlinking the new handle should cause the file to be renamed
f7c3ffd7 834 * to '1' since '0' already exists.
f0faf175
JG
835 */
836 ret = fs_handle_unlink(new_handle);
f7c3ffd7
JG
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);
f0faf175
JG
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
f7c3ffd7
JG
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");
f6bef966 867end:
f0faf175 868 fd_tracker_destroy(tracker);
f7c3ffd7
JG
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);
f0faf175
JG
874}
875
9ca3e8a2
JG
876int 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
f0faf175
JG
905 diag("Suspendable - Unlinking test");
906 test_unlink();
907
f7c3ffd7 908 rcu_barrier();
9ca3e8a2
JG
909 rcu_unregister_thread();
910 return exit_status();
911}
This page took 0.098734 seconds and 4 git commands to generate.