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