1 /* Threshold at which to diagnose ELOOP. Generic version.
2 Copyright (C) 2012-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19 #ifndef _ELOOP_THRESHOLD_H
20 #define _ELOOP_THRESHOLD_H 1
21
22 #include <limits.h>
23 #include <sys/param.h>
24
25 /* POSIX specifies SYMLOOP_MAX as the "Maximum number of symbolic
26 links that can be reliably traversed in the resolution of a
27 pathname in the absence of a loop." This makes it a minimum that
28 we should certainly accept. But it leaves open the possibility
29 that more might sometimes work--just not "reliably".
30
31 For example, Linux implements a complex policy whereby there is a
32 small limit on the number of direct symlink traversals (a symlink
33 to a symlink to a symlink), but larger limit on the total number of
34 symlink traversals overall. Hence the SYMLOOP_MAX number should be
35 the small one, but the limit library functions enforce on users
36 should be the larger one.
37
38 So, we use the larger of the reported SYMLOOP_MAX (if any) and our
39 own constant MIN_ELOOP_THRESHOLD, below. This constant should be
40 large enough that it never rules out a file name and directory tree
41 that the underlying system (i.e. calls to 'open' et al) would
42 resolve successfully. It should be small enough that actual loops
43 are detected without a huge number of iterations. */
44
45 #ifndef MIN_ELOOP_THRESHOLD
46 # define MIN_ELOOP_THRESHOLD 40
47 #endif
48
49 /* Return the maximum number of symlink traversals to permit
50 before diagnosing ELOOP. */
51 static inline unsigned int __attribute__ ((const))
__eloop_threshold(void)52 __eloop_threshold (void)
53 {
54 #ifdef SYMLOOP_MAX
55 const int symloop_max = SYMLOOP_MAX;
56 #else
57 /* The function is marked 'const' even though we use memory and
58 call a function, because sysconf is required to return the
59 same value in every call and so it must always be safe to
60 call __eloop_threshold exactly once and reuse the value. */
61 static long int sysconf_symloop_max;
62 if (sysconf_symloop_max == 0)
63 sysconf_symloop_max = __sysconf (_SC_SYMLOOP_MAX);
64 const unsigned int symloop_max = (sysconf_symloop_max <= 0
65 ? _POSIX_SYMLOOP_MAX
66 : sysconf_symloop_max);
67 #endif
68
69 return MAX (symloop_max, MIN_ELOOP_THRESHOLD);
70 }
71
72 #endif /* eloop-threshold.h */
73