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