* sysdeps/unix/sysv/linux/Versions: Move sync_file_range to
[glibc.git] / sysdeps / unix / sysv / linux / sysconf.c
blobf9f6f1bfa566bb1d0470391cf97b4a810870aa95
1 /* Get file-specific information about a file. Linux version.
2 Copyright (C) 2003, 2004, 2006 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <stdlib.h>
23 #include <sysdep.h>
24 #include <time.h>
25 #include <unistd.h>
26 #include <not-cancel.h>
28 static long int posix_sysconf (int name);
31 /* Get the value of the system variable NAME. */
32 long int
33 __sysconf (int name)
35 const char *procfname = NULL;
37 switch (name)
39 #ifdef __NR_clock_getres
40 case _SC_MONOTONIC_CLOCK:
41 /* Check using the clock_getres system call. */
43 struct timespec ts;
44 INTERNAL_SYSCALL_DECL (err);
45 int r;
46 r = INTERNAL_SYSCALL (clock_getres, err, 2, CLOCK_MONOTONIC, &ts);
47 return INTERNAL_SYSCALL_ERROR_P (r, err) ? -1 : _POSIX_VERSION;
49 #endif
51 #if defined __NR_clock_getres || HP_TIMING_AVAIL
52 case _SC_CPUTIME:
53 case _SC_THREAD_CPUTIME:
55 /* If we have HP_TIMING, we will fall back on that if the system
56 call does not work, so we support it either way. */
57 # if !HP_TIMING_AVAIL
58 /* Check using the clock_getres system call. */
59 struct timespec ts;
60 INTERNAL_SYSCALL_DECL (err);
61 int r = INTERNAL_SYSCALL (clock_getres, err, 2,
62 (name == _SC_CPUTIME
63 ? CLOCK_PROCESS_CPUTIME_ID
64 : CLOCK_THREAD_CPUTIME_ID),
65 &ts);
66 if (INTERNAL_SYSCALL_ERROR_P (r, err))
67 return -1;
68 # endif
69 return _POSIX_VERSION;
71 #endif
73 case _SC_NGROUPS_MAX:
74 /* Try to read the information from the /proc/sys/kernel/ngroups_max
75 file. */
76 procfname = "/proc/sys/kernel/ngroups_max";
77 break;
79 case _SC_SIGQUEUE_MAX:
80 /* The /proc/sys/kernel/rtsig-max file contains the answer. */
81 procfname = "/proc/sys/kernel/rtsig-max";
82 break;
84 default:
85 break;
88 if (procfname != NULL)
90 int fd = open_not_cancel_2 (procfname, O_RDONLY);
91 if (fd != -1)
93 /* This is more than enough, the file contains a single integer. */
94 char buf[32];
95 ssize_t n;
96 n = TEMP_FAILURE_RETRY (read_not_cancel (fd, buf, sizeof (buf) - 1));
97 close_not_cancel_no_status (fd);
99 if (n > 0)
101 /* Terminate the string. */
102 buf[n] = '\0';
104 char *endp;
105 long int res = strtol (buf, &endp, 10);
106 if (endp != buf && (*endp == '\0' || *endp == '\n'))
107 return res;
112 return posix_sysconf (name);
115 /* Now the POSIX version. */
116 #undef __sysconf
117 #define __sysconf static posix_sysconf
118 #include <sysdeps/posix/sysconf.c>