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