]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/unix/sysv/linux/sysconf.c
x86-64: Check FMA_Usable in ifunc-mathvec-avx2.h [BZ #21966]
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / sysconf.c
CommitLineData
ad0e8eb0 1/* Get file-specific information about a file. Linux version.
bfff8b1b 2 Copyright (C) 2003-2017 Free Software Foundation, Inc.
ad0e8eb0
UD
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
59ba27a6
PE
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
ad0e8eb0 18
bf067910 19#include <errno.h>
d024596d 20#include <fcntl.h>
4c771a3b 21#include <stdlib.h>
ad0e8eb0
UD
22#include <sysdep.h>
23#include <time.h>
24#include <unistd.h>
7c22c7ec 25#include <sys/resource.h>
ca06321d 26#include <sys/param.h>
5db7adc4 27#include <not-cancel.h>
7c22c7ec 28#include <ldsodefs.h>
ad0e8eb0 29
f96853be
UD
30/* Legacy value of ARG_MAX. The macro is now not defined since the
31 actual value varies based on the stack size. */
32#define legacy_ARG_MAX 131072
33
34
ad0e8eb0
UD
35static long int posix_sysconf (int name);
36
ad0e8eb0 37
3b85df27
UD
38#ifndef HAS_CPUCLOCK
39static long int
f50ef8f1 40has_cpuclock (int name)
3b85df27
UD
41{
42# if defined __NR_clock_getres || HP_TIMING_AVAIL
43 /* If we have HP_TIMING, we will fall back on that if the system
44 call does not work, so we support it either way. */
45# if !HP_TIMING_AVAIL
46 /* Check using the clock_getres system call. */
47 struct timespec ts;
48 INTERNAL_SYSCALL_DECL (err);
49 int r = INTERNAL_SYSCALL (clock_getres, err, 2,
50 (name == _SC_CPUTIME
51 ? CLOCK_PROCESS_CPUTIME_ID
52 : CLOCK_THREAD_CPUTIME_ID),
53 &ts);
54 if (INTERNAL_SYSCALL_ERROR_P (r, err))
55 return -1;
56# endif
57 return _POSIX_VERSION;
58# else
59 return -1;
60# endif
61}
f50ef8f1 62# define HAS_CPUCLOCK(name) has_cpuclock (name)
3b85df27
UD
63#endif
64
65
ad0e8eb0
UD
66/* Get the value of the system variable NAME. */
67long int
68__sysconf (int name)
69{
3bd1aa47
UD
70 const char *procfname = NULL;
71
ad0e8eb0
UD
72 switch (name)
73 {
5be0a688 74 struct rlimit rlimit;
ad0e8eb0
UD
75#ifdef __NR_clock_getres
76 case _SC_MONOTONIC_CLOCK:
77 /* Check using the clock_getres system call. */
78 {
79 struct timespec ts;
80 INTERNAL_SYSCALL_DECL (err);
d15851ec
RM
81 int r;
82 r = INTERNAL_SYSCALL (clock_getres, err, 2, CLOCK_MONOTONIC, &ts);
fbf86dda 83 return INTERNAL_SYSCALL_ERROR_P (r, err) ? -1 : _POSIX_VERSION;
ad0e8eb0
UD
84 }
85#endif
86
182ed12e
RM
87 case _SC_CPUTIME:
88 case _SC_THREAD_CPUTIME:
f50ef8f1 89 return HAS_CPUCLOCK (name);
182ed12e 90
7c22c7ec 91 case _SC_ARG_MAX:
637461d9
JM
92 /* Use getrlimit to get the stack limit. */
93 if (__getrlimit (RLIMIT_STACK, &rlimit) == 0)
94 return MAX (legacy_ARG_MAX, rlimit.rlim_cur / 4);
7c22c7ec 95
f96853be 96 return legacy_ARG_MAX;
7c22c7ec 97
d024596d 98 case _SC_NGROUPS_MAX:
3bd1aa47
UD
99 /* Try to read the information from the /proc/sys/kernel/ngroups_max
100 file. */
101 procfname = "/proc/sys/kernel/ngroups_max";
102 break;
103
104 case _SC_SIGQUEUE_MAX:
5be0a688
UD
105 if (__getrlimit (RLIMIT_SIGPENDING, &rlimit) == 0)
106 return rlimit.rlim_cur;
107
3bd1aa47
UD
108 /* The /proc/sys/kernel/rtsig-max file contains the answer. */
109 procfname = "/proc/sys/kernel/rtsig-max";
d024596d
UD
110 break;
111
ad0e8eb0 112 default:
d024596d 113 break;
ad0e8eb0 114 }
3bd1aa47
UD
115
116 if (procfname != NULL)
117 {
c2284574 118 int fd = __open_nocancel (procfname, O_RDONLY);
3bd1aa47
UD
119 if (fd != -1)
120 {
121 /* This is more than enough, the file contains a single integer. */
122 char buf[32];
123 ssize_t n;
124 n = TEMP_FAILURE_RETRY (read_not_cancel (fd, buf, sizeof (buf) - 1));
125 close_not_cancel_no_status (fd);
126
127 if (n > 0)
128 {
129 /* Terminate the string. */
130 buf[n] = '\0';
131
132 char *endp;
133 long int res = strtol (buf, &endp, 10);
134 if (endp != buf && (*endp == '\0' || *endp == '\n'))
135 return res;
136 }
137 }
138 }
139
d024596d 140 return posix_sysconf (name);
ad0e8eb0 141}
bf067910
UD
142
143/* Now the POSIX version. */
144#undef __sysconf
145#define __sysconf static posix_sysconf
146#include <sysdeps/posix/sysconf.c>