1#!/bin/sh
2# Show that we've eliminated most of ls' failing getxattr syscalls,
3# regardless of how many files are in a directory we list.
4# This test is skipped on systems that lack LD_PRELOAD support; that's fine.
5# Similarly, on a system that lacks getxattr altogether, skipping it is fine.
6
7# Copyright (C) 2012-2023 Free Software Foundation, Inc.
8
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18
19# You should have received a copy of the GNU General Public License
20# along with this program.  If not, see <https://www.gnu.org/licenses/>.
21
22. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
23print_ver_ ls
24require_gcc_shared_
25
26# Replace each getxattr and lgetxattr call with a call to these stubs.
27# Count those and write the total number of calls to the file "x"
28# via a global destructor.
29cat > k.c <<'EOF' || framework_failure_
30#include <errno.h>
31#include <stdio.h>
32#include <sys/types.h>
33
34static unsigned long int n_calls;
35
36static void __attribute__ ((destructor))
37print_call_count (void)
38{
39  FILE *fp = fopen ("x", "w"); if (!fp) return;
40  fprintf (fp, "%lu\n", n_calls); fclose (fp);
41}
42
43static ssize_t incr () { ++n_calls; errno = ENOTSUP; return -1; }
44ssize_t getxattr (const char *path, const char *name, void *value, size_t size)
45{ return incr (); }
46ssize_t lgetxattr(const char *path, const char *name, void *value, size_t size)
47{ return incr (); }
48EOF
49
50# Then compile/link it:
51gcc_shared_ k.c k.so \
52  || framework_failure_ 'failed to build shared library'
53
54# Create a few files:
55seq 20 | xargs touch || framework_failure_
56
57# Finally, run the test:
58LD_PRELOAD=$LD_PRELOAD:./k.so ls --color=always -l . || fail=1
59
60test -f x || skip_ "internal test failure: maybe LD_PRELOAD doesn't work?"
61
62# Ensure that there were no more than 3 *getxattr calls.
63n_calls=$(cat x)
64test "$n_calls" -le 3 || fail=1
65
66Exit $fail
67