eloop-threshold.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* Threshold at which to diagnose ELOOP. Generic version.
  2. Copyright (C) 2012-2023 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. <https://www.gnu.org/licenses/>. */
  15. #ifndef _ELOOP_THRESHOLD_H
  16. #define _ELOOP_THRESHOLD_H 1
  17. #include <limits.h>
  18. #ifdef _LIBC
  19. # include <sys/param.h>
  20. # define _GL_ATTRIBUTE_CONST __attribute__ ((const))
  21. #else
  22. # include <unistd.h>
  23. # include "minmax.h"
  24. # define __sysconf sysconf
  25. # if (!defined SYMLOOP_MAX \
  26. && ! (defined _SC_SYMLOOP_MAX && defined _POSIX_SYMLOOP_MAX))
  27. # define SYMLOOP_MAX 8
  28. # endif
  29. #endif
  30. /* POSIX specifies SYMLOOP_MAX as the "Maximum number of symbolic
  31. links that can be reliably traversed in the resolution of a
  32. pathname in the absence of a loop." This makes it a minimum that
  33. we should certainly accept. But it leaves open the possibility
  34. that more might sometimes work--just not "reliably".
  35. For example, Linux implements a complex policy whereby there is a
  36. small limit on the number of direct symlink traversals (a symlink
  37. to a symlink to a symlink), but larger limit on the total number of
  38. symlink traversals overall. Hence the SYMLOOP_MAX number should be
  39. the small one, but the limit library functions enforce on users
  40. should be the larger one.
  41. So, we use the larger of the reported SYMLOOP_MAX (if any) and our
  42. own constant MIN_ELOOP_THRESHOLD, below. This constant should be
  43. large enough that it never rules out a file name and directory tree
  44. that the underlying system (i.e. calls to 'open' et al) would
  45. resolve successfully. It should be small enough that actual loops
  46. are detected without a huge number of iterations. */
  47. #ifndef MIN_ELOOP_THRESHOLD
  48. # define MIN_ELOOP_THRESHOLD 40
  49. #endif
  50. /* Return the maximum number of symlink traversals to permit
  51. before diagnosing ELOOP. */
  52. static inline unsigned int _GL_ATTRIBUTE_CONST
  53. __eloop_threshold (void)
  54. {
  55. #ifdef SYMLOOP_MAX
  56. const int symloop_max = SYMLOOP_MAX;
  57. #else
  58. /* The function is marked 'const' even though we use memory and
  59. call a function, because sysconf is required to return the
  60. same value in every call and so it must always be safe to
  61. call __eloop_threshold exactly once and reuse the value. */
  62. static long int sysconf_symloop_max;
  63. if (sysconf_symloop_max == 0)
  64. sysconf_symloop_max = __sysconf (_SC_SYMLOOP_MAX);
  65. const unsigned int symloop_max = (sysconf_symloop_max <= 0
  66. ? _POSIX_SYMLOOP_MAX
  67. : sysconf_symloop_max);
  68. #endif
  69. return MAX (symloop_max, MIN_ELOOP_THRESHOLD);
  70. }
  71. #endif /* eloop-threshold.h */