2 # Ensure that rm gives the expected diagnostic when failing to remove a file
3 # owned by some other user in a directory with the sticky bit set.
5 # Copyright (C) 2002-2024 Free Software Foundation, Inc.
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <https://www.gnu.org/licenses/>.
22 (my $ME = $0) =~ s
|.*/||;
27 and CuSkip
::skip
"$ME: can't run this test as root: skipping this test";
29 my $verbose = $ENV{VERBOSE
} && $ENV{VERBOSE
} eq 'yes';
31 # Ensure that the diagnostics are in English.
34 # Set up a safe, well-known environment
37 # Taint checking requires a sanitized $PATH. This script performs no $PATH
38 # search, so on most Unix-based systems, it is fine simply to clear $ENV{PATH}.
39 # However, on Cygwin, it's used to find cygwin1.dll, so set it.
40 $ENV{PATH
} = '/bin:/usr/bin';
42 my @dir_list = qw(/tmp /var/tmp /usr/tmp);
43 my $rm = "$ENV{abs_top_builddir}/src/rm";
45 # Untaint for upcoming popen.
46 $rm =~ m!^([-+\@\w./]+)$!
47 or CuSkip
::skip
"$ME: unusual absolute builddir name; skipping this test\n";
50 # Find a directory with the sticky bit set.
53 foreach my $dir (@dir_list)
55 if (-d
$dir && -k _
&& -r _
&& -w _
&& -x _
)
59 # Find a non-directory there that is owned by some other user.
60 opendir DIR_HANDLE
, $dir
61 or die "$ME: couldn't open $dir: $!\n";
63 foreach my $f (readdir DIR_HANDLE
)
65 # Consider only names containing "safe" characters.
70 my $target_file = "$dir/$f";
72 and warn "$ME: considering $target_file\n";
74 # Skip files owned by self, symlinks, and directories.
75 # It's not technically necessary to skip symlinks, but it's simpler.
76 # SVR4-like systems (e.g., Solaris 9) let you unlink files that
77 # you can write, so skip writable files too.
78 -l
$target_file || -o _
|| -d _
|| -w _
83 # Invoke rm on this file and ensure that we get the
84 # expected exit code and diagnostic.
85 my $cmd = "$rm -f -- $target_file";
86 open RM
, "$cmd 2>&1 |"
87 or die "$ME: cannot execute '$cmd'\n";
93 # This test opportunistically looks for files that can't
94 # be removed but those files may already have been removed
95 # by their owners by the time we get to them. It is a
96 # race condition. If so then the rm is successful and our
97 # test is thwarted. Detect this case and ignore.
100 next if ! -e
$target_file;
101 die "$ME: unexpected exit status from '$cmd';\n"
102 . " got 0, expected 1\n";
106 my $status = $rc >> 8;
108 or die "$ME: unexpected exit status from '$cmd';\n"
109 . " got $status, expected 1\n";
113 # Terminated by a signal.
114 my $sig_num = $rc & 0x7F;
115 die "$ME: command '$cmd' died with signal $sig_num\n";
118 my $exp = "rm: cannot remove '$target_file':";
120 or die "$ME: no output from '$cmd';\n"
121 . "expected something like '$exp ...'\n";
123 # Transform the actual diagnostic so that it starts with "rm:".
124 # Depending on your system, it might be "rm:" already, or
126 $line =~ s
,^\Q
$rm\E
:,rm
:,;
128 my $regex = quotemeta $exp;
130 or die "$ME: unexpected diagnostic from '$cmd';\n"
132 . " expected $exp ...\n";
144 or CuSkip
::skip
"$ME: couldn't find a directory with the sticky bit set;"
145 . " skipping this test\n";
148 or CuSkip
::skip
"$ME: couldn't find a file not owned by you\n"
149 . " in any of the following directories:\n @dir_list\n"
150 . "...so, skipping this test\n";