[LTP] [PATCH] inotify11: Fix race condition between fork() and inotify init

Martin Doucha mdoucha@suse.cz
Thu Sep 8 16:50:11 CEST 2022


When the child process runs a few churn cycles before the parent completes
inotify initialization, the parent will get stuck reading the inotify
file descriptor until it eventually times out. Create inotify watch
befory fork()ing to prevent this race condition.

Signed-off-by: Martin Doucha <mdoucha@suse.cz>
---
 testcases/kernel/syscalls/inotify/inotify11.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/testcases/kernel/syscalls/inotify/inotify11.c b/testcases/kernel/syscalls/inotify/inotify11.c
index e426f9e1c..dd32ea7fd 100644
--- a/testcases/kernel/syscalls/inotify/inotify11.c
+++ b/testcases/kernel/syscalls/inotify/inotify11.c
@@ -61,15 +61,16 @@ static void verify_inotify(void)
 	struct inotify_event *event;
 	int inotify_fd;
 
+	inotify_fd = SAFE_MYINOTIFY_INIT();
+	SAFE_MYINOTIFY_ADD_WATCH(inotify_fd, ".", IN_DELETE);
+
 	pid = SAFE_FORK();
 	if (pid == 0) {
+		SAFE_CLOSE(inotify_fd);
 		churn();
 		return;
 	}
 
-	inotify_fd = SAFE_MYINOTIFY_INIT();
-	SAFE_MYINOTIFY_ADD_WATCH(inotify_fd, ".", IN_DELETE);
-
 	while (!opened && nevents < CHURN_FILES) {
 		int i, fd, len;
 
-- 
2.37.2



More information about the ltp mailing list