Blame sysdeps/unix/sysv/linux/sysconf.c

Packit 6c4009
/* Get file-specific information about a file.  Linux version.
Packit 6c4009
   Copyright (C) 2003-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
#include <errno.h>
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <sysdep.h>
Packit 6c4009
#include <time.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <sys/resource.h>
Packit 6c4009
#include <sys/param.h>
Packit 6c4009
#include <not-cancel.h>
Packit 6c4009
#include <ldsodefs.h>
Packit 6c4009
Packit 6c4009
/* Legacy value of ARG_MAX.  The macro is now not defined since the
Packit 6c4009
   actual value varies based on the stack size.  */
Packit 6c4009
#define legacy_ARG_MAX 131072
Packit 6c4009
Packit 6c4009
Packit 6c4009
static long int posix_sysconf (int name);
Packit 6c4009
Packit 6c4009
Packit Service c82647
#ifndef HAS_CPUCLOCK
Packit Service c82647
static long int
Packit Service c82647
has_cpuclock (int name)
Packit Service c82647
{
Packit Service c82647
# if defined __NR_clock_getres || HP_TIMING_AVAIL
Packit Service c82647
  /* If we have HP_TIMING, we will fall back on that if the system
Packit Service c82647
     call does not work, so we support it either way.  */
Packit Service c82647
#  if !HP_TIMING_AVAIL
Packit Service c82647
  /* Check using the clock_getres system call.  */
Packit Service c82647
  struct timespec ts;
Packit Service c82647
  INTERNAL_SYSCALL_DECL (err);
Packit Service c82647
  int r = INTERNAL_SYSCALL (clock_getres, err, 2,
Packit Service c82647
			    (name == _SC_CPUTIME
Packit Service c82647
			     ? CLOCK_PROCESS_CPUTIME_ID
Packit Service c82647
			     : CLOCK_THREAD_CPUTIME_ID),
Packit Service c82647
			    &ts);
Packit Service c82647
  if (INTERNAL_SYSCALL_ERROR_P (r, err))
Packit Service c82647
    return -1;
Packit Service c82647
#  endif
Packit Service c82647
  return _POSIX_VERSION;
Packit Service c82647
# else
Packit Service c82647
  return -1;
Packit Service c82647
# endif
Packit Service c82647
}
Packit Service c82647
# define HAS_CPUCLOCK(name) has_cpuclock (name)
Packit Service c82647
#endif
Packit Service c82647
Packit Service c82647
Packit 6c4009
/* Get the value of the system variable NAME.  */
Packit 6c4009
long int
Packit 6c4009
__sysconf (int name)
Packit 6c4009
{
Packit 6c4009
  const char *procfname = NULL;
Packit 6c4009
Packit 6c4009
  switch (name)
Packit 6c4009
    {
Packit Service c82647
      struct rlimit rlimit;
Packit Service c82647
#ifdef __NR_clock_getres
Packit 6c4009
    case _SC_MONOTONIC_CLOCK:
Packit Service c82647
      /* Check using the clock_getres system call.  */
Packit Service c82647
      {
Packit Service c82647
	struct timespec ts;
Packit Service c82647
	INTERNAL_SYSCALL_DECL (err);
Packit Service c82647
	int r;
Packit Service c82647
	r = INTERNAL_SYSCALL (clock_getres, err, 2, CLOCK_MONOTONIC, &ts);
Packit Service c82647
	return INTERNAL_SYSCALL_ERROR_P (r, err) ? -1 : _POSIX_VERSION;
Packit Service c82647
      }
Packit Service c82647
#endif
Packit Service c82647
Packit 6c4009
    case _SC_CPUTIME:
Packit 6c4009
    case _SC_THREAD_CPUTIME:
Packit Service c82647
      return HAS_CPUCLOCK (name);
Packit 6c4009
Packit 6c4009
    case _SC_ARG_MAX:
Packit Service c82647
      /* Use getrlimit to get the stack limit.  */
Packit Service c82647
      if (__getrlimit (RLIMIT_STACK, &rlimit) == 0)
Packit Service c82647
	return MAX (legacy_ARG_MAX, rlimit.rlim_cur / 4);
Packit 6c4009
Packit Service c82647
      return legacy_ARG_MAX;
Packit 6c4009
Packit 6c4009
    case _SC_NGROUPS_MAX:
Packit 6c4009
      /* Try to read the information from the /proc/sys/kernel/ngroups_max
Packit 6c4009
	 file.  */
Packit 6c4009
      procfname = "/proc/sys/kernel/ngroups_max";
Packit 6c4009
      break;
Packit 6c4009
Packit 6c4009
    case _SC_SIGQUEUE_MAX:
Packit Service c82647
      if (__getrlimit (RLIMIT_SIGPENDING, &rlimit) == 0)
Packit Service c82647
	return rlimit.rlim_cur;
Packit 6c4009
Packit Service c82647
      /* The /proc/sys/kernel/rtsig-max file contains the answer.  */
Packit Service c82647
      procfname = "/proc/sys/kernel/rtsig-max";
Packit 6c4009
      break;
Packit 6c4009
Packit 6c4009
    default:
Packit 6c4009
      break;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (procfname != NULL)
Packit 6c4009
    {
Packit 6c4009
      int fd = __open_nocancel (procfname, O_RDONLY);
Packit 6c4009
      if (fd != -1)
Packit 6c4009
	{
Packit 6c4009
	  /* This is more than enough, the file contains a single integer.  */
Packit 6c4009
	  char buf[32];
Packit 6c4009
	  ssize_t n;
Packit 6c4009
	  n = TEMP_FAILURE_RETRY (__read_nocancel (fd, buf, sizeof (buf) - 1));
Packit 6c4009
	  __close_nocancel_nostatus (fd);
Packit 6c4009
Packit 6c4009
	  if (n > 0)
Packit 6c4009
	    {
Packit 6c4009
	      /* Terminate the string.  */
Packit 6c4009
	      buf[n] = '\0';
Packit 6c4009
Packit 6c4009
	      char *endp;
Packit 6c4009
	      long int res = strtol (buf, &endp, 10);
Packit 6c4009
	      if (endp != buf && (*endp == '\0' || *endp == '\n'))
Packit 6c4009
		return res;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return posix_sysconf (name);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Now the POSIX version.  */
Packit 6c4009
#undef __sysconf
Packit 6c4009
#define __sysconf static posix_sysconf
Packit 6c4009
#include <sysdeps/posix/sysconf.c>