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