• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2015 SUSE Linux.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it would be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  *
12  * Further, this software is distributed without any warranty that it is
13  * free of the rightful claim of any third person regarding infringement
14  * or the like.  Any license provided herein, whether implied or
15  * otherwise, applies only to this software file.  Patent licenses, if
16  * any, provided herein do not apply to combinations of this program with
17  * other software, or any other product whatsoever.
18  *
19  * Started by Jan Kara <jack@suse.cz>
20  *
21  * DESCRIPTION
22  * Test for inotify mark destruction race.
23  *
24  * Kernels prior to 4.2 have a race when inode is being deleted while
25  * inotify group watching that inode is being torn down. When the race is
26  * hit, the kernel crashes or loops.
27  *
28  * The problem has been fixed by commit:
29  *  8f2f3eb59dff "fsnotify: fix oops in fsnotify_clear_marks_by_group_flags()".
30  */
31 
32 #include "config.h"
33 
34 #include <stdio.h>
35 #include <unistd.h>
36 #include <stdlib.h>
37 #include <fcntl.h>
38 #include <time.h>
39 #include <signal.h>
40 #include <sys/time.h>
41 #include <sys/wait.h>
42 #include <sys/syscall.h>
43 
44 #include "test.h"
45 #include "linux_syscall_numbers.h"
46 #include "inotify.h"
47 #include "safe_macros.h"
48 
49 char *TCID = "inotify06";
50 int TST_TOTAL = 1;
51 
52 #if defined(HAVE_SYS_INOTIFY_H)
53 #include <sys/inotify.h>
54 
55 /* Number of test loops to run the test for */
56 #define TEARDOWNS 400
57 
58 /* Number of files to test (must be > 1) */
59 #define FILES 5
60 
61 char names[FILES][PATH_MAX];
62 
cleanup(void)63 static void cleanup(void)
64 {
65 	tst_rmdir();
66 }
67 
setup(void)68 static void setup(void)
69 {
70 	int i;
71 
72 	tst_sig(FORK, DEF_HANDLER, cleanup);
73 
74 	TEST_PAUSE;
75 
76 	tst_tmpdir();
77 
78 	for (i = 0; i < FILES; i++)
79 		sprintf(names[i], "fname_%d", i);
80 }
81 
verify_inotify(void)82 static void verify_inotify(void)
83 {
84 	int inotify_fd, fd;
85 	pid_t pid;
86 	int i, tests;
87 
88 	pid = tst_fork();
89 	if (pid == 0) {
90 		while (1) {
91 			for (i = 0; i < FILES; i++) {
92 				fd = SAFE_OPEN(NULL, names[i], O_CREAT | O_RDWR, 0600);
93 				SAFE_CLOSE(NULL, fd);
94 			}
95 			for (i = 0; i < FILES; i++)
96 				SAFE_UNLINK(NULL, names[i]);
97 		}
98 	} else if (pid == -1)
99 			tst_brkm(TBROK | TERRNO, cleanup, "fork() failed");
100 
101 	for (tests = 0; tests < TEARDOWNS; tests++) {
102 		inotify_fd = ltp_syscall(__NR_inotify_init1, O_NONBLOCK);
103 		if (inotify_fd < 0)
104 			tst_brkm(TBROK | TERRNO, cleanup, "inotify_init failed");
105 
106 		for (i = 0; i < FILES; i++) {
107 			/*
108 			 * Both failure and success are fine since
109 			 * files are being deleted in parallel - this
110 			 * is what provokes the race we want to test
111 			 * for...
112 			 */
113 			myinotify_add_watch(inotify_fd, names[i], IN_MODIFY);
114 		}
115 		SAFE_CLOSE(cleanup, inotify_fd);
116 	}
117 	/* We survived for given time - test succeeded */
118 	tst_resm(TPASS, "kernel survived inotify beating");
119 
120 	/* Kill the child creating / deleting files and wait for it */
121 	kill(pid, SIGKILL);
122 	wait(NULL);
123 }
124 
main(int ac,char ** av)125 int main(int ac, char **av)
126 {
127 	int lc;
128 
129 	tst_parse_opts(ac, av, NULL, NULL);
130 
131 	setup();
132 
133 	for (lc = 0; TEST_LOOPING(lc); lc++)
134 		verify_inotify();
135 
136 	cleanup();
137 	tst_exit();
138 }
139 
140 #else
141 
main(void)142 int main(void)
143 {
144 	tst_brkm(TCONF, NULL, "system doesn't have required inotify support");
145 }
146 
147 #endif
148