Blame sysdeps/generic/eloop-threshold.h

Packit 6c4009
/* Threshold at which to diagnose ELOOP.  Generic version.
Packit 6c4009
   Copyright (C) 2012-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#ifndef _ELOOP_THRESHOLD_H
Packit 6c4009
#define _ELOOP_THRESHOLD_H      1
Packit 6c4009
Packit 6c4009
#include <limits.h>
Packit 6c4009
#include <sys/param.h>
Packit 6c4009
Packit 6c4009
/* POSIX specifies SYMLOOP_MAX as the "Maximum number of symbolic
Packit 6c4009
   links that can be reliably traversed in the resolution of a
Packit 6c4009
   pathname in the absence of a loop."  This makes it a minimum that
Packit 6c4009
   we should certainly accept.  But it leaves open the possibility
Packit 6c4009
   that more might sometimes work--just not "reliably".
Packit 6c4009
Packit 6c4009
   For example, Linux implements a complex policy whereby there is a
Packit 6c4009
   small limit on the number of direct symlink traversals (a symlink
Packit 6c4009
   to a symlink to a symlink), but larger limit on the total number of
Packit 6c4009
   symlink traversals overall.  Hence the SYMLOOP_MAX number should be
Packit 6c4009
   the small one, but the limit library functions enforce on users
Packit 6c4009
   should be the larger one.
Packit 6c4009
Packit 6c4009
   So, we use the larger of the reported SYMLOOP_MAX (if any) and our
Packit 6c4009
   own constant MIN_ELOOP_THRESHOLD, below.  This constant should be
Packit 6c4009
   large enough that it never rules out a file name and directory tree
Packit 6c4009
   that the underlying system (i.e. calls to 'open' et al) would
Packit 6c4009
   resolve successfully.  It should be small enough that actual loops
Packit 6c4009
   are detected without a huge number of iterations.  */
Packit 6c4009
Packit 6c4009
#ifndef MIN_ELOOP_THRESHOLD
Packit 6c4009
# define MIN_ELOOP_THRESHOLD    40
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/* Return the maximum number of symlink traversals to permit
Packit 6c4009
   before diagnosing ELOOP.  */
Packit 6c4009
static inline unsigned int __attribute__ ((const))
Packit 6c4009
__eloop_threshold (void)
Packit 6c4009
{
Packit 6c4009
#ifdef SYMLOOP_MAX
Packit 6c4009
  const int symloop_max = SYMLOOP_MAX;
Packit 6c4009
#else
Packit 6c4009
  /* The function is marked 'const' even though we use memory and
Packit 6c4009
     call a function, because sysconf is required to return the
Packit 6c4009
     same value in every call and so it must always be safe to
Packit 6c4009
     call __eloop_threshold exactly once and reuse the value.  */
Packit 6c4009
  static long int sysconf_symloop_max;
Packit 6c4009
  if (sysconf_symloop_max == 0)
Packit 6c4009
    sysconf_symloop_max = __sysconf (_SC_SYMLOOP_MAX);
Packit 6c4009
  const unsigned int symloop_max = (sysconf_symloop_max <= 0
Packit 6c4009
                                    ? _POSIX_SYMLOOP_MAX
Packit 6c4009
                                    : sysconf_symloop_max);
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  return MAX (symloop_max, MIN_ELOOP_THRESHOLD);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#endif  /* eloop-threshold.h */