stat.h: Fix missing declaration of struct timespec
[glibc.git] / sysdeps / unix / sysv / linux / sysconf.c
blob009ca9dfeaabbc8120a0a0c53c9cb1948256f77f
1 /* Get file-specific information about a file. Linux version.
2 Copyright (C) 2003-2024 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, see
17 <https://www.gnu.org/licenses/>. */
19 #include <assert.h>
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 <sys/param.h>
28 #include <not-cancel.h>
29 #include <ldsodefs.h>
30 #include <sysconf-sigstksz.h>
32 /* Legacy value of ARG_MAX. The macro is now not defined since the
33 actual value varies based on the stack size. */
34 #define legacy_ARG_MAX 131072
36 /* Newer kernels (4.13) limit the maximum command line arguments lengths to
37 6MiB. */
38 #define maximum_ARG_MAX (6 * 1024 * 1024)
40 static long int posix_sysconf (int name);
43 /* Get the value of the system variable NAME. */
44 long int
45 __sysconf (int name)
47 const char *procfname = NULL;
49 switch (name)
51 case _SC_MONOTONIC_CLOCK:
52 case _SC_CPUTIME:
53 case _SC_THREAD_CPUTIME:
54 return _POSIX_VERSION;
56 case _SC_ARG_MAX:
58 struct rlimit rlimit;
59 /* Use getrlimit to get the stack limit. */
60 if (__getrlimit (RLIMIT_STACK, &rlimit) == 0)
62 const long int limit = MAX (legacy_ARG_MAX, rlimit.rlim_cur / 4);
63 return MIN (limit, maximum_ARG_MAX);
66 return legacy_ARG_MAX;
69 case _SC_NGROUPS_MAX:
70 /* Try to read the information from the /proc/sys/kernel/ngroups_max
71 file. */
72 procfname = "/proc/sys/kernel/ngroups_max";
73 break;
75 case _SC_SIGQUEUE_MAX:
77 struct rlimit rlimit;
78 if (__getrlimit (RLIMIT_SIGPENDING, &rlimit) == 0)
79 return rlimit.rlim_cur;
81 /* The /proc/sys/kernel/rtsig-max file contains the answer. */
82 procfname = "/proc/sys/kernel/rtsig-max";
84 break;
86 case _SC_MINSIGSTKSZ:
87 assert (GLRO(dl_minsigstacksize) != 0);
88 return GLRO(dl_minsigstacksize);
90 case _SC_SIGSTKSZ:
91 return sysconf_sigstksz ();
93 default:
94 break;
97 if (procfname != NULL)
99 int fd = __open_nocancel (procfname, O_RDONLY | O_CLOEXEC);
100 if (fd != -1)
102 /* This is more than enough, the file contains a single integer. */
103 char buf[32];
104 ssize_t n;
105 n = TEMP_FAILURE_RETRY (__read_nocancel (fd, buf, sizeof (buf) - 1));
106 __close_nocancel_nostatus (fd);
108 if (n > 0)
110 /* Terminate the string. */
111 buf[n] = '\0';
113 char *endp;
114 long int res = strtol (buf, &endp, 10);
115 if (endp != buf && (*endp == '\0' || *endp == '\n'))
116 return res;
121 return posix_sysconf (name);
124 /* Now the POSIX version. */
125 #undef __sysconf
126 #define __sysconf static posix_sysconf
127 #include <sysdeps/posix/sysconf.c>