1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2015 SUSE Linux. All Rights Reserved.
4 * Author: Jan Kara <jack@suse.cz>
5 *
6 * DESCRIPTION
7 * Test for inotify mark destruction race.
8 *
9 * Kernels prior to 4.2 have a race when inode is being deleted while
10 * inotify group watching that inode is being torn down. When the race is
11 * hit, the kernel crashes or loops.
12 *
13 * The problem has been fixed by commit:
14 * 8f2f3eb59dff "fsnotify: fix oops in fsnotify_clear_marks_by_group_flags()".
15 */
16
17 #include "config.h"
18
19 #include <stdio.h>
20 #include <unistd.h>
21 #include <stdlib.h>
22 #include <fcntl.h>
23 #include <time.h>
24 #include <signal.h>
25 #include <sys/time.h>
26 #include <sys/wait.h>
27 #include <sys/syscall.h>
28
29 #include "tst_test.h"
30 #include "inotify.h"
31
32 #if defined(HAVE_SYS_INOTIFY_H)
33 #include <sys/inotify.h>
34
35 /* Number of test loops to run the test for */
36 #define TEARDOWNS 400
37
38 /* Number of files to test (must be > 1) */
39 #define FILES 5
40
41 char names[FILES][PATH_MAX];
42
setup(void)43 static void setup(void)
44 {
45 int i;
46
47 for (i = 0; i < FILES; i++)
48 sprintf(names[i], "fname_%d", i);
49 }
50
verify_inotify(void)51 static void verify_inotify(void)
52 {
53 int inotify_fd, fd;
54 pid_t pid;
55 int i, tests;
56
57 pid = SAFE_FORK();
58 if (pid == 0) {
59 while (1) {
60 for (i = 0; i < FILES; i++) {
61 fd = SAFE_OPEN(names[i], O_CREAT | O_RDWR, 0600);
62 SAFE_CLOSE(fd);
63 }
64 for (i = 0; i < FILES; i++)
65 SAFE_UNLINK(names[i]);
66 }
67 }
68
69 for (tests = 0; tests < TEARDOWNS; tests++) {
70 inotify_fd = SAFE_MYINOTIFY_INIT1(O_NONBLOCK);
71
72 for (i = 0; i < FILES; i++) {
73 /*
74 * Both failure and success are fine since
75 * files are being deleted in parallel - this
76 * is what provokes the race we want to test
77 * for...
78 */
79 myinotify_add_watch(inotify_fd, names[i], IN_MODIFY);
80 }
81 SAFE_CLOSE(inotify_fd);
82 }
83 /* We survived for given time - test succeeded */
84 tst_res(TPASS, "kernel survived inotify beating");
85
86 /* Kill the child creating / deleting files and wait for it */
87 SAFE_KILL(pid, SIGKILL);
88 SAFE_WAIT(NULL);
89 }
90
91 static struct tst_test test = {
92 .timeout = 600,
93 .needs_tmpdir = 1,
94 .forks_child = 1,
95 .setup = setup,
96 .test_all = verify_inotify,
97 };
98
99 #else
100 TST_TEST_TCONF("system doesn't have required inotify support");
101 #endif
102