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