1/* Get file-specific information about a file. Linux version.
2 Copyright (C) 2003-2020 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 <https://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 <sys/param.h>
27#include <not-cancel.h>
28#include <ldsodefs.h>
29
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
35static long int posix_sysconf (int name);
36
37
38/* Get the value of the system variable NAME. */
39long int
40__sysconf (int name)
41{
42 const char *procfname = NULL;
43
44 switch (name)
45 {
46 case _SC_MONOTONIC_CLOCK:
47 case _SC_CPUTIME:
48 case _SC_THREAD_CPUTIME:
49 return _POSIX_VERSION;
50
51 case _SC_ARG_MAX:
52 {
53 struct rlimit rlimit;
54 /* Use getrlimit to get the stack limit. */
55 if (__getrlimit (RLIMIT_STACK, &rlimit) == 0)
56 return MAX (legacy_ARG_MAX, rlimit.rlim_cur / 4);
57
58 return legacy_ARG_MAX;
59 }
60
61 case _SC_NGROUPS_MAX:
62 /* Try to read the information from the /proc/sys/kernel/ngroups_max
63 file. */
64 procfname = "/proc/sys/kernel/ngroups_max";
65 break;
66
67 case _SC_SIGQUEUE_MAX:
68 {
69 struct rlimit rlimit;
70 if (__getrlimit (RLIMIT_SIGPENDING, &rlimit) == 0)
71 return rlimit.rlim_cur;
72
73 /* The /proc/sys/kernel/rtsig-max file contains the answer. */
74 procfname = "/proc/sys/kernel/rtsig-max";
75 }
76 break;
77
78 default:
79 break;
80 }
81
82 if (procfname != NULL)
83 {
84 int fd = __open_nocancel (procfname, O_RDONLY);
85 if (fd != -1)
86 {
87 /* This is more than enough, the file contains a single integer. */
88 char buf[32];
89 ssize_t n;
90 n = TEMP_FAILURE_RETRY (__read_nocancel (fd, buf, sizeof (buf) - 1));
91 __close_nocancel_nostatus (fd);
92
93 if (n > 0)
94 {
95 /* Terminate the string. */
96 buf[n] = '\0';
97
98 char *endp;
99 long int res = strtol (buf, &endp, 10);
100 if (endp != buf && (*endp == '\0' || *endp == '\n'))
101 return res;
102 }
103 }
104 }
105
106 return posix_sysconf (name);
107}
108
109/* Now the POSIX version. */
110#undef __sysconf
111#define __sysconf static posix_sysconf
112#include <sysdeps/posix/sysconf.c>
113