3 # Ensure that rm gives the expected diagnostic when failing to remove a file
4 # owned by some other user in a directory with the sticky bit set.
6 # Copyright (C) 2002, 2003, 2004, 2006, 2007 Free Software Foundation, Inc.
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 if test "$VERBOSE" = yes; then
28 # FIXME-someday: when run as root we don't need all of the
29 # searching below. root can simply create the required
30 # dir/files and run the test as someone else.
32 PRIV_CHECK_ARG=require-non-root . $srcdir/../priv-check
37 $PERL -e 1 > /dev/null 2>&1 || {
38 echo 1>&2 "$0: configure didn't find a usable version of Perl," \
39 "so can't run this test"
46 exec $PERL -Tw -- - << \EOP
50 (my $ME = $ENV{ARGV_0}) =~ s|.*/||;
52 my $verbose = $ENV{VERBOSE} && $ENV{VERBOSE} eq 'yes';
54 # Ensure that the diagnostics are in English.
57 # Set up a safe, well-known environment
58 delete @ENV{qw(BASH_ENV CDPATH ENV PATH)};
61 my @dir_list = qw(/tmp /var/tmp /usr/tmp);
62 my $rm = '../../src/rm';
64 # Find a directory with the sticky bit set.
67 foreach my $dir (@dir_list)
69 if (-d $dir && -k _ && -r _ && -w _ && -x _)
73 # Find a non-directory there that is owned by some other user.
74 opendir DIR_HANDLE, $dir
75 or die "$ME: couldn't open $dir: $!\n";
77 foreach my $f (readdir DIR_HANDLE)
79 # Consider only names containing "safe" characters.
84 my $target_file = "$dir/$f";
86 and warn "$ME: considering $target_file\n";
88 # Skip files owned by self, symlinks, and directories.
89 # It's not technically necessary to skip symlinks, but it's simpler.
90 # SVR4-like systems (e.g., Solaris 9) let you unlink files that
91 # you can write, so skip writable files too.
92 -l $target_file || -o _ || -d _ || -w _
97 # Invoke rm on this file and ensure that we get the
98 # expected exit code and diagnostic.
99 my $cmd = "$rm -f -- $target_file";
100 open RM, "$cmd 2>&1 |"
101 or die "$ME: cannot execute `$cmd'\n";
109 my $status = $rc >> 8;
111 or die "$ME: unexpected exit status from `$cmd';\n"
112 . " got $status, expected 1\n";
116 # Terminated by a signal.
117 my $sig_num = $rc & 0x7F;
118 die "$ME: command `$cmd' died with signal $sig_num\n";
121 my $exp = "rm: cannot remove `$target_file':";
123 or die "$ME: no output from `$cmd';\n"
124 . "expected something like `$exp ...'\n";
126 # Transform the actual diagnostic so that it starts with "rm:".
127 # Depending on your system, it might be "rm:" already, or
129 $line =~ s,^\Q$rm\E:,rm:,;
131 my $regex = quotemeta $exp;
133 or die "$ME: unexpected diagnostic from `$cmd';\n"
135 . " expected $exp ...\n";
148 warn "$ME: couldn't find a directory with the sticky bit set;"
149 . " skipping this test\n";
155 warn "$ME: couldn't find a file not owned by you\n"
156 . " in any of the following directories:\n @dir_list\n"
157 . "...so, skipping this test\n";