tests: compile and link shared object with $CC to make LD_PRELOAD work
[coreutils/ericb.git] / tests / ls / getxattr-speedup
blob967f7c95f732a2910e802782beb1db5249f5a7d9
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.
7 # Copyright (C) 2012 Free Software Foundation, Inc.
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.
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.
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22 . "${srcdir=.}/init.sh"; path_prepend_ ../src
23 print_ver_ ls
25 # Replace each getxattr and lgetxattr call with a call to these stubs.
26 # Count those and write the total number of calls to the file "x"
27 # via a global destructor.
28 cat > k.c <<'EOF' || framework_failure_
29 #include <errno.h>
30 #include <stdio.h>
31 #include <sys/types.h>
33 static unsigned long int n_calls;
35 static void __attribute__ ((destructor))
36 print_call_count (void)
38 FILE *fp = fopen ("x", "w"); if (!fp) return;
39 fprintf (fp, "%lu\n", n_calls); fclose (fp);
42 static ssize_t incr () { ++n_calls; errno = ENOTSUP; return -1; }
43 ssize_t getxattr (const char *path, const char *name, void *value, size_t size)
44 { return incr (); }
45 ssize_t lgetxattr(const char *path, const char *name, void *value, size_t size)
46 { return incr (); }
47 EOF
49 # Then compile/link it:
50 $CC -shared -fPIC -O2 k.c -o k.so \
51 || framework_failure_ 'failed to compile with -shared -fPIC'
53 # Create a few files:
54 seq 20 | xargs touch || framework_failure_
56 # Finally, run the test:
57 LD_PRELOAD=./k.so ls --color=always -l . || fail=1
59 test -f x || skip_ "internal test failure: maybe LD_PRELOAD doesn't work?"
61 # Ensure that there were no more than 3 *getxattr calls.
62 n_calls=$(cat x)
63 test "$n_calls" -le 3 || fail=1
65 Exit $fail