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