Updated to fedora-glibc-20080310T1651
[glibc.git] / sysdeps / unix / sysv / linux / sysconf.c
blobab9cddc3060f67a732c6ae410cd4de2ca2c2c3f5
1 /* Get file-specific information about a file. Linux version.
2 Copyright (C) 2003, 2004, 2006, 2008 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 <sys/resource.h>
27 #include <not-cancel.h>
28 #include <ldsodefs.h>
30 static long int posix_sysconf (int name);
33 /* Get the value of the system variable NAME. */
34 long int
35 __sysconf (int name)
37 const char *procfname = NULL;
39 switch (name)
41 #ifdef __NR_clock_getres
42 case _SC_MONOTONIC_CLOCK:
43 /* Check using the clock_getres system call. */
45 struct timespec ts;
46 INTERNAL_SYSCALL_DECL (err);
47 int r;
48 r = INTERNAL_SYSCALL (clock_getres, err, 2, CLOCK_MONOTONIC, &ts);
49 return INTERNAL_SYSCALL_ERROR_P (r, err) ? -1 : _POSIX_VERSION;
51 #endif
53 #if defined __NR_clock_getres || HP_TIMING_AVAIL
54 case _SC_CPUTIME:
55 case _SC_THREAD_CPUTIME:
57 /* If we have HP_TIMING, we will fall back on that if the system
58 call does not work, so we support it either way. */
59 # if !HP_TIMING_AVAIL
60 /* Check using the clock_getres system call. */
61 struct timespec ts;
62 INTERNAL_SYSCALL_DECL (err);
63 int r = INTERNAL_SYSCALL (clock_getres, err, 2,
64 (name == _SC_CPUTIME
65 ? CLOCK_PROCESS_CPUTIME_ID
66 : CLOCK_THREAD_CPUTIME_ID),
67 &ts);
68 if (INTERNAL_SYSCALL_ERROR_P (r, err))
69 return -1;
70 # endif
71 return _POSIX_VERSION;
73 #endif
75 case _SC_ARG_MAX:
76 #if __LINUX_KERNEL_VERSION < 0x020617
77 /* Determine whether this is a kernel 2.6.23 or later. Only
78 then do we have an argument limit determined by the stack
79 size. */
80 if (GLRO(dl_discover_osversion) () >= 0x020617)
81 #endif
83 /* Use getrlimit to get the stack limit. */
84 struct rlimit rlimit;
85 if (__getrlimit (RLIMIT_STACK, &rlimit) == 0)
86 return MAX (ARG_MAX, rlimit.rlim_cur / 4);
89 return ARG_MAX;
91 case _SC_NGROUPS_MAX:
92 /* Try to read the information from the /proc/sys/kernel/ngroups_max
93 file. */
94 procfname = "/proc/sys/kernel/ngroups_max";
95 break;
97 case _SC_SIGQUEUE_MAX:
98 /* The /proc/sys/kernel/rtsig-max file contains the answer. */
99 procfname = "/proc/sys/kernel/rtsig-max";
100 break;
102 default:
103 break;
106 if (procfname != NULL)
108 int fd = open_not_cancel_2 (procfname, O_RDONLY);
109 if (fd != -1)
111 /* This is more than enough, the file contains a single integer. */
112 char buf[32];
113 ssize_t n;
114 n = TEMP_FAILURE_RETRY (read_not_cancel (fd, buf, sizeof (buf) - 1));
115 close_not_cancel_no_status (fd);
117 if (n > 0)
119 /* Terminate the string. */
120 buf[n] = '\0';
122 char *endp;
123 long int res = strtol (buf, &endp, 10);
124 if (endp != buf && (*endp == '\0' || *endp == '\n'))
125 return res;
130 return posix_sysconf (name);
133 /* Now the POSIX version. */
134 #undef __sysconf
135 #define __sysconf static posix_sysconf
136 #include <sysdeps/posix/sysconf.c>