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