From cf7e1b5b8fb53aef2a9103a32d6ad5ee0b595fa6 Mon Sep 17 00:00:00 2001 From: Ondrej Oprala Date: Thu, 9 Aug 2012 17:34:09 +0200 Subject: du: handle bind-mounted directory cycles gracefully Before this change, a directory cycle induced by a bind mount would be treated as a fatal error, i.e., probable disk corruption. However, such cycles are relatively common, and can be detected efficiently, so now du emits a descriptive warning and arranges to exit nonzero. * NEWS (Bug fixes): Mention it. * src/du.c: Include "mountlist.h". (di_mnt): New global set. (di_files): Rename global from di_set, now that there are two. (fill_mount_table): New function. (hash_ins): Add DI_SET parameter. (process_file): Look up each dir dev/ino pair in the new set. (main): Allocate, initialize, and free the new set. * tests/du/bind-mount-dir-cycle: Add a test for the fix. * tests/Makefile.am (TESTS): Add it. * THANKS.in: Update. This implements the proposal in http://bugs.gnu.org/11844. Originally reported in http://bugs.debian.org/563254 by Alan Jenkins and more recently as http://bugzilla.redhat.com/836557 Improved by: Jim Meyering --- tests/Makefile.am | 1 + tests/du/bind-mount-dir-cycle | 44 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100755 tests/du/bind-mount-dir-cycle (limited to 'tests') diff --git a/tests/Makefile.am b/tests/Makefile.am index 69078bdd2..acd816d6c 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -32,6 +32,7 @@ root_tests = \ cp/sparse-fiemap \ dd/skip-seek-past-dev \ df/problematic-chars \ + du/bind-mount-dir-cycle \ install/install-C-root \ ls/capability \ ls/nameless-uid \ diff --git a/tests/du/bind-mount-dir-cycle b/tests/du/bind-mount-dir-cycle new file mode 100755 index 000000000..8f9e197f9 --- /dev/null +++ b/tests/du/bind-mount-dir-cycle @@ -0,0 +1,44 @@ +#!/bin/sh +# Exercise du's new ability to handle bind-mount-induced dir cycles. + +# Copyright (C) 2012 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 3 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, see . + +. "${srcdir=.}/init.sh"; path_prepend_ ../src +print_ver_ rm +require_root_ + +cleanup_() +{ + # When you take the undesirable shortcut of making /etc/mtab a link + # to /proc/mounts, unmounting "$other_partition_tmpdir" would fail. + # So, here we unmount a/b instead. + umount a/b +} + +mkdir -p a/b || framework_failure_ +mount --bind a a/b \ + || skip_ "This test requires mount with a working --bind option." + +echo a > exp || framework_failure_ +echo "du: mount point 'a/b' already traversed" > exp-err || framework_failure_ + +du a > out 2> err && fail=1 +sed 's/^[0-9][0-9]* //' out > k && mv k out + +compare exp-err err || fail=1 +compare exp out || fail=1 + +Exit $fail -- cgit v1.2.3-54-g00ecf