Linux Test Project
 help / color / mirror / Atom feed
From: Martin Doucha <mdoucha@suse.cz>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH] inotify11: Fix race condition between fork() and inotify init
Date: Thu,  8 Sep 2022 16:50:11 +0200	[thread overview]
Message-ID: <20220908145011.21212-1-mdoucha@suse.cz> (raw)

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


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

             reply	other threads:[~2022-09-08 14:50 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-08 14:50 Martin Doucha [this message]
2022-09-08 15:15 ` [LTP] [PATCH] inotify11: Fix race condition between fork() and inotify init Amir Goldstein
2022-09-09  7:44   ` Petr Vorel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220908145011.21212-1-mdoucha@suse.cz \
    --to=mdoucha@suse.cz \
    --cc=ltp@lists.linux.it \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox