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