eloop-threshold.h 2.9 KB

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