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