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