bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] tail: fix a race condition


From: Jim Meyering
Subject: Re: [PATCH] tail: fix a race condition
Date: Tue, 20 Oct 2009 15:03:54 +0200

Giuseppe Scrivano wrote:
> I wrote a simple test case, taking some ideas from the old commit
> 11b626c20fbc65c668081996bfb9d1118f475eda.
>
> The test has a race (it is funny, considering that it checks for a race)
> and I used quite long time periods to minimize it, probably under high
> loads it can cause a false positive.

Thanks for doing that.
A couple suggestions:

>>From e86c16342e856bec744ea01b8a2f8ab1b8695d63 Mon Sep 17 00:00:00 2001
> From: Giuseppe Scrivano <address@hidden>
> Date: Tue, 20 Oct 2009 10:49:17 +0200
> Subject: [PATCH] tests: add a new test that checks for a possible `tail' race.
>
> If new data is available between the initial read and tail registers the 
> inotify
> watch descriptors, ensure that it is read before a new event happens on the
> file.
>
> * tests/Makefile.am (TESTS): Add tail-2/inotify-race.
> * tests/tail-2/inotify-race: New file.
> ---
>  tests/Makefile.am         |    1 +
>  tests/tail-2/inotify-race |   77 
> +++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 78 insertions(+), 0 deletions(-)
>  create mode 100755 tests/tail-2/inotify-race
>
> diff --git a/tests/Makefile.am b/tests/Makefile.am
> index 751db1c..bb11c23 100644
> --- a/tests/Makefile.am
> +++ b/tests/Makefile.am
> @@ -74,6 +74,7 @@ EXTRA_DIST += $(TESTS)
>
>  TESTS =                                              \
>    misc/help-version                          \
> +  tail-2/inotify-race                                \
>    misc/invalid-opt                           \
>    rm/ext3-perf                                       \
>    rm/cycle                                   \
> diff --git a/tests/tail-2/inotify-race b/tests/tail-2/inotify-race
> new file mode 100755
> index 0000000..32ebfcb
> --- /dev/null
> +++ b/tests/tail-2/inotify-race
> @@ -0,0 +1,77 @@
> +#!/bin/sh
> +# Ensure that when open creates a destination file,
> +# that file has properly restrictive permissions.
> +# Before coreutils-6.7, there was an interval in which
> +# a just-created file would have too-generous permissions.

Those comments are for the original, deleted test.

> +# Copyright (C) 2006, 2009 Free Software Foundation, Inc.
> +
> +# This program is free software; you can redistribute it and/or modify
> +# it under the terms of the GNU General Public License as published by
> +# the Free Software Foundation; either version 2 of the License, or
> +# (at your option) any later version.
> +
> +# This program is distributed in the hope that it will be useful,
> +# but WITHOUT ANY WARRANTY; without even the implied warranty of
> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +# GNU General Public License for more details.
> +
> +# You should have received a copy of the GNU General Public License
> +# along with this program; if not, write to the Free Software
> +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
> +# 02110-1301, USA.
> +
> +if test "$VERBOSE" = yes; then
> +  set -x
> +  tail --version
> +fi
> +
> +. $srcdir/test-lib.sh
> +
> +fail=0
> +
> +umask 2
> +touch file || framework_failure
> +
> +( gdb --version ) > gdb.out 2>&1
> +case `cat gdb.out` in
> +  'GNU gdb'*) ;;
> +  *) echo "$0: can't run gdb.  Skipping this test." 1>&2;
> +     (exit 77); exit 77;;
> +esac
> +
> +
> +# Use `xnanosleep' to sleep the debugged process for a specified
> +# amount of time.  `xnanosleep' is used by tail and the symbol is
> +# exported, so we can use it without problems.
> +
> +gdb -nx --batch-silent              \
> +    --eval-command='break tail_forever_inotify'    \
> +    --eval-command='run -f file'    \
> +    --eval-command='print xnanosleep (0.01)' \
> +    --eval-command='quit'    \
> +    $abs_top_builddir/src/tail < /dev/null > gdb.out 2>&1
>
> +if test -s gdb.out; then
> +  cat <<EOF 1>&2
> +$0: can't set breakpoints or use xnanosleep in tail.  Skipping this test.
> +EOF
> +  (exit 77); exit 77
> +fi
> +
> +(sleep 1s; echo "hello working tail" > file) &
> +
> +gdb -nx --batch-silent              \
> +    --eval-command='break tail_forever_inotify'    \
> +    --eval-command='run -f file'    \
> +    --eval-command='print xnanosleep (2.0)' \
> +    --eval-command='continue' \
> +    --eval-command='quit'    \
> +    $abs_top_builddir/src/tail < /dev/null > gdb.out &

You can simplify that and remove most of the race by removing the
xnanosleep and the preceding backgrounded sleep+echo shell.

Replace the print xnanosleep command with the gdb command,
"shell echo never-seen-with-tail-7.5 >> file" to append to the file being 
tailed.

> +
> +sleep 3s
> +kill $!
> +
> +test -s gdb.out || fail=1
> +
> +Exit $fail




reply via email to

[Prev in Thread] Current Thread [Next in Thread]