sysconf.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* Get file-specific information about a file. Linux version.
  2. Copyright (C) 2003-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. #include <errno.h>
  16. #include <fcntl.h>
  17. #include <stdlib.h>
  18. #include <sysdep.h>
  19. #include <time.h>
  20. #include <unistd.h>
  21. #include <sys/resource.h>
  22. #include <sys/param.h>
  23. #include <not-cancel.h>
  24. #include <ldsodefs.h>
  25. /* Legacy value of ARG_MAX. The macro is now not defined since the
  26. actual value varies based on the stack size. */
  27. #define legacy_ARG_MAX 131072
  28. static long int posix_sysconf (int name);
  29. #ifndef HAS_CPUCLOCK
  30. static long int
  31. has_cpuclock (int name)
  32. {
  33. # if defined __NR_clock_getres || HP_TIMING_AVAIL
  34. /* If we have HP_TIMING, we will fall back on that if the system
  35. call does not work, so we support it either way. */
  36. # if !HP_TIMING_AVAIL
  37. /* Check using the clock_getres system call. */
  38. struct timespec ts;
  39. INTERNAL_SYSCALL_DECL (err);
  40. int r = INTERNAL_SYSCALL (clock_getres, err, 2,
  41. (name == _SC_CPUTIME
  42. ? CLOCK_PROCESS_CPUTIME_ID
  43. : CLOCK_THREAD_CPUTIME_ID),
  44. &ts);
  45. if (INTERNAL_SYSCALL_ERROR_P (r, err))
  46. return -1;
  47. # endif
  48. return _POSIX_VERSION;
  49. # else
  50. return -1;
  51. # endif
  52. }
  53. # define HAS_CPUCLOCK(name) has_cpuclock (name)
  54. #endif
  55. /* Get the value of the system variable NAME. */
  56. long int
  57. __sysconf (int name)
  58. {
  59. const char *procfname = NULL;
  60. switch (name)
  61. {
  62. struct rlimit rlimit;
  63. #ifdef __NR_clock_getres
  64. case _SC_MONOTONIC_CLOCK:
  65. /* Check using the clock_getres system call. */
  66. {
  67. struct timespec ts;
  68. INTERNAL_SYSCALL_DECL (err);
  69. int r;
  70. r = INTERNAL_SYSCALL (clock_getres, err, 2, CLOCK_MONOTONIC, &ts);
  71. return INTERNAL_SYSCALL_ERROR_P (r, err) ? -1 : _POSIX_VERSION;
  72. }
  73. #endif
  74. case _SC_CPUTIME:
  75. case _SC_THREAD_CPUTIME:
  76. return HAS_CPUCLOCK (name);
  77. case _SC_ARG_MAX:
  78. /* Use getrlimit to get the stack limit. */
  79. if (__getrlimit (RLIMIT_STACK, &rlimit) == 0)
  80. return MAX (legacy_ARG_MAX, rlimit.rlim_cur / 4);
  81. return legacy_ARG_MAX;
  82. case _SC_NGROUPS_MAX:
  83. /* Try to read the information from the /proc/sys/kernel/ngroups_max
  84. file. */
  85. procfname = "/proc/sys/kernel/ngroups_max";
  86. break;
  87. case _SC_SIGQUEUE_MAX:
  88. if (__getrlimit (RLIMIT_SIGPENDING, &rlimit) == 0)
  89. return rlimit.rlim_cur;
  90. /* The /proc/sys/kernel/rtsig-max file contains the answer. */
  91. procfname = "/proc/sys/kernel/rtsig-max";
  92. break;
  93. default:
  94. break;
  95. }
  96. if (procfname != NULL)
  97. {
  98. int fd = __open_nocancel (procfname, O_RDONLY);
  99. if (fd != -1)
  100. {
  101. /* This is more than enough, the file contains a single integer. */
  102. char buf[32];
  103. ssize_t n;
  104. n = TEMP_FAILURE_RETRY (__read_nocancel (fd, buf, sizeof (buf) - 1));
  105. __close_nocancel_nostatus (fd);
  106. if (n > 0)
  107. {
  108. /* Terminate the string. */
  109. buf[n] = '\0';
  110. char *endp;
  111. long int res = strtol (buf, &endp, 10);
  112. if (endp != buf && (*endp == '\0' || *endp == '\n'))
  113. return res;
  114. }
  115. }
  116. }
  117. return posix_sysconf (name);
  118. }
  119. /* Now the POSIX version. */
  120. #undef __sysconf
  121. #define __sysconf static posix_sysconf
  122. #include <sysdeps/posix/sysconf.c>