diff mbox series

inotify11: Fix race condition between fork() and inotify init

Message ID 20220908145011.21212-1-mdoucha@suse.cz
State Accepted
Headers show
Series inotify11: Fix race condition between fork() and inotify init | expand

Commit Message

Martin Doucha Sept. 8, 2022, 2:50 p.m. UTC
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(-)

Comments

Amir Goldstein Sept. 8, 2022, 3:15 p.m. UTC | #1
On Thu, Sep 8, 2022 at 5:50 PM Martin Doucha <mdoucha@suse.cz> wrote:
>
> 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>

LGTM

Reviewed-by: Amir Goldstein <amir73il@gmail.com>


> ---
>  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
Petr Vorel Sept. 9, 2022, 7:44 a.m. UTC | #2
Hi all,

thanks, merged!

Kind regards,
Petr
diff mbox series

Patch

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;