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