4 #include <sys/resource.h>
6 #include <sys/sysinfo.h>
11 #define JT(x) (-256|(x))
13 #define JT_ARG_MAX JT(2)
14 #define JT_MQ_PRIO_MAX JT(3)
15 #define JT_PAGE_SIZE JT(4)
16 #define JT_SEM_VALUE_MAX JT(5)
17 #define JT_NPROCESSORS_CONF JT(6)
18 #define JT_NPROCESSORS_ONLN JT(7)
19 #define JT_PHYS_PAGES JT(8)
20 #define JT_AVPHYS_PAGES JT(9)
21 #define JT_ZERO JT(10)
22 #define JT_DELAYTIMER_MAX JT(11)
23 #define JT_MINSIGSTKSZ JT(12)
24 #define JT_SIGSTKSZ JT(13)
26 #define RLIM(x) (-32768|(RLIMIT_ ## x))
28 long sysconf(int name
)
30 static const short values
[] = {
31 [_SC_ARG_MAX
] = JT_ARG_MAX
,
32 [_SC_CHILD_MAX
] = RLIM(NPROC
),
34 [_SC_NGROUPS_MAX
] = 32,
35 [_SC_OPEN_MAX
] = RLIM(NOFILE
),
36 [_SC_STREAM_MAX
] = -1,
37 [_SC_TZNAME_MAX
] = TZNAME_MAX
,
38 [_SC_JOB_CONTROL
] = 1,
40 [_SC_REALTIME_SIGNALS
] = VER
,
41 [_SC_PRIORITY_SCHEDULING
] = -1,
43 [_SC_ASYNCHRONOUS_IO
] = VER
,
44 [_SC_PRIORITIZED_IO
] = -1,
45 [_SC_SYNCHRONIZED_IO
] = -1,
47 [_SC_MAPPED_FILES
] = VER
,
49 [_SC_MEMLOCK_RANGE
] = VER
,
50 [_SC_MEMORY_PROTECTION
] = VER
,
51 [_SC_MESSAGE_PASSING
] = VER
,
52 [_SC_SEMAPHORES
] = VER
,
53 [_SC_SHARED_MEMORY_OBJECTS
] = VER
,
54 [_SC_AIO_LISTIO_MAX
] = -1,
56 [_SC_AIO_PRIO_DELTA_MAX
] = JT_ZERO
, /* ?? */
57 [_SC_DELAYTIMER_MAX
] = JT_DELAYTIMER_MAX
,
58 [_SC_MQ_OPEN_MAX
] = -1,
59 [_SC_MQ_PRIO_MAX
] = JT_MQ_PRIO_MAX
,
61 [_SC_PAGE_SIZE
] = JT_PAGE_SIZE
,
62 [_SC_RTSIG_MAX
] = _NSIG
- 1 - 31 - 3,
63 [_SC_SEM_NSEMS_MAX
] = SEM_NSEMS_MAX
,
64 [_SC_SEM_VALUE_MAX
] = JT_SEM_VALUE_MAX
,
65 [_SC_SIGQUEUE_MAX
] = -1,
67 [_SC_BC_BASE_MAX
] = _POSIX2_BC_BASE_MAX
,
68 [_SC_BC_DIM_MAX
] = _POSIX2_BC_DIM_MAX
,
69 [_SC_BC_SCALE_MAX
] = _POSIX2_BC_SCALE_MAX
,
70 [_SC_BC_STRING_MAX
] = _POSIX2_BC_STRING_MAX
,
71 [_SC_COLL_WEIGHTS_MAX
] = COLL_WEIGHTS_MAX
,
72 [_SC_EXPR_NEST_MAX
] = -1,
74 [_SC_RE_DUP_MAX
] = RE_DUP_MAX
,
75 [_SC_2_VERSION
] = VER
,
78 [_SC_2_FORT_DEV
] = -1,
79 [_SC_2_FORT_RUN
] = -1,
81 [_SC_2_LOCALEDEF
] = -1,
82 [_SC_IOV_MAX
] = IOV_MAX
,
84 [_SC_THREAD_SAFE_FUNCTIONS
] = VER
,
85 [_SC_GETGR_R_SIZE_MAX
] = -1,
86 [_SC_GETPW_R_SIZE_MAX
] = -1,
87 [_SC_LOGIN_NAME_MAX
] = 256,
88 [_SC_TTY_NAME_MAX
] = TTY_NAME_MAX
,
89 [_SC_THREAD_DESTRUCTOR_ITERATIONS
] = PTHREAD_DESTRUCTOR_ITERATIONS
,
90 [_SC_THREAD_KEYS_MAX
] = PTHREAD_KEYS_MAX
,
91 [_SC_THREAD_STACK_MIN
] = PTHREAD_STACK_MIN
,
92 [_SC_THREAD_THREADS_MAX
] = -1,
93 [_SC_THREAD_ATTR_STACKADDR
] = VER
,
94 [_SC_THREAD_ATTR_STACKSIZE
] = VER
,
95 [_SC_THREAD_PRIORITY_SCHEDULING
] = VER
,
96 [_SC_THREAD_PRIO_INHERIT
] = -1,
97 [_SC_THREAD_PRIO_PROTECT
] = -1,
98 [_SC_THREAD_PROCESS_SHARED
] = VER
,
99 [_SC_NPROCESSORS_CONF
] = JT_NPROCESSORS_CONF
,
100 [_SC_NPROCESSORS_ONLN
] = JT_NPROCESSORS_ONLN
,
101 [_SC_PHYS_PAGES
] = JT_PHYS_PAGES
,
102 [_SC_AVPHYS_PAGES
] = JT_AVPHYS_PAGES
,
103 [_SC_ATEXIT_MAX
] = -1,
105 [_SC_XOPEN_VERSION
] = _XOPEN_VERSION
,
106 [_SC_XOPEN_XCU_VERSION
] = _XOPEN_VERSION
,
107 [_SC_XOPEN_UNIX
] = 1,
108 [_SC_XOPEN_CRYPT
] = -1,
109 [_SC_XOPEN_ENH_I18N
] = 1,
111 [_SC_2_CHAR_TERM
] = -1,
113 [_SC_XOPEN_XPG2
] = -1,
114 [_SC_XOPEN_XPG3
] = -1,
115 [_SC_XOPEN_XPG4
] = -1,
117 [_SC_XBS5_ILP32_OFF32
] = -1,
118 [_SC_XBS5_ILP32_OFFBIG
] = sizeof(long)==4 ? 1 : -1,
119 [_SC_XBS5_LP64_OFF64
] = sizeof(long)==8 ? 1 : -1,
120 [_SC_XBS5_LPBIG_OFFBIG
] = -1,
121 [_SC_XOPEN_LEGACY
] = -1,
122 [_SC_XOPEN_REALTIME
] = -1,
123 [_SC_XOPEN_REALTIME_THREADS
] = -1,
124 [_SC_ADVISORY_INFO
] = VER
,
125 [_SC_BARRIERS
] = VER
,
126 [_SC_CLOCK_SELECTION
] = VER
,
128 [_SC_THREAD_CPUTIME
] = VER
,
129 [_SC_MONOTONIC_CLOCK
] = VER
,
130 [_SC_READER_WRITER_LOCKS
] = VER
,
131 [_SC_SPIN_LOCKS
] = VER
,
135 [_SC_SPORADIC_SERVER
] = -1,
136 [_SC_THREAD_SPORADIC_SERVER
] = -1,
137 [_SC_TIMEOUTS
] = VER
,
138 [_SC_TYPED_MEMORY_OBJECTS
] = -1,
140 [_SC_2_PBS_ACCOUNTING
] = -1,
141 [_SC_2_PBS_LOCATE
] = -1,
142 [_SC_2_PBS_MESSAGE
] = -1,
143 [_SC_2_PBS_TRACK
] = -1,
144 [_SC_SYMLOOP_MAX
] = SYMLOOP_MAX
,
145 [_SC_STREAMS
] = JT_ZERO
,
146 [_SC_2_PBS_CHECKPOINT
] = -1,
147 [_SC_V6_ILP32_OFF32
] = -1,
148 [_SC_V6_ILP32_OFFBIG
] = sizeof(long)==4 ? 1 : -1,
149 [_SC_V6_LP64_OFF64
] = sizeof(long)==8 ? 1 : -1,
150 [_SC_V6_LPBIG_OFFBIG
] = -1,
151 [_SC_HOST_NAME_MAX
] = HOST_NAME_MAX
,
153 [_SC_TRACE_EVENT_FILTER
] = -1,
154 [_SC_TRACE_INHERIT
] = -1,
155 [_SC_TRACE_LOG
] = -1,
158 [_SC_RAW_SOCKETS
] = VER
,
159 [_SC_V7_ILP32_OFF32
] = -1,
160 [_SC_V7_ILP32_OFFBIG
] = sizeof(long)==4 ? 1 : -1,
161 [_SC_V7_LP64_OFF64
] = sizeof(long)==8 ? 1 : -1,
162 [_SC_V7_LPBIG_OFFBIG
] = -1,
163 [_SC_SS_REPL_MAX
] = -1,
164 [_SC_TRACE_EVENT_NAME_MAX
] = -1,
165 [_SC_TRACE_NAME_MAX
] = -1,
166 [_SC_TRACE_SYS_MAX
] = -1,
167 [_SC_TRACE_USER_EVENT_MAX
] = -1,
168 [_SC_XOPEN_STREAMS
] = JT_ZERO
,
169 [_SC_THREAD_ROBUST_PRIO_INHERIT
] = -1,
170 [_SC_THREAD_ROBUST_PRIO_PROTECT
] = -1,
172 [_SC_MINSIGSTKSZ
] = JT_MINSIGSTKSZ
,
173 [_SC_SIGSTKSZ
] = JT_SIGSTKSZ
,
176 if (name
>= sizeof(values
)/sizeof(values
[0]) || !values
[name
]) {
179 } else if (values
[name
] >= -1) {
181 } else if (values
[name
] < -256) {
183 getrlimit(values
[name
]&16383, &lim
);
184 if (lim
.rlim_cur
== RLIM_INFINITY
)
186 return lim
.rlim_cur
> LONG_MAX
? LONG_MAX
: lim
.rlim_cur
;
189 switch ((unsigned char)values
[name
]) {
191 return _POSIX_VERSION
;
192 case JT_ARG_MAX
& 255:
194 case JT_MQ_PRIO_MAX
& 255:
196 case JT_PAGE_SIZE
& 255:
198 case JT_SEM_VALUE_MAX
& 255:
199 return SEM_VALUE_MAX
;
200 case JT_DELAYTIMER_MAX
& 255:
201 return DELAYTIMER_MAX
;
202 case JT_NPROCESSORS_CONF
& 255:
203 case JT_NPROCESSORS_ONLN
& 255: ;
204 unsigned char set
[128] = {1};
206 __syscall(SYS_sched_getaffinity
, 0, sizeof set
, set
);
207 for (i
=cnt
=0; i
<sizeof set
; i
++)
208 for (; set
[i
]; set
[i
]&=set
[i
]-1, cnt
++);
210 case JT_PHYS_PAGES
& 255:
211 case JT_AVPHYS_PAGES
& 255: ;
212 unsigned long long mem
;
215 if (!si
.mem_unit
) si
.mem_unit
= 1;
216 if (name
==_SC_PHYS_PAGES
) mem
= si
.totalram
;
217 else mem
= si
.freeram
+ si
.bufferram
;
220 return (mem
> LONG_MAX
) ? LONG_MAX
: mem
;
221 case JT_MINSIGSTKSZ
& 255:
222 case JT_SIGSTKSZ
& 255: ;
223 long val
= __getauxval(AT_MINSIGSTKSZ
);
224 if (val
< MINSIGSTKSZ
) val
= MINSIGSTKSZ
;
225 if (values
[name
] == JT_SIGSTKSZ
)
226 val
+= SIGSTKSZ
- MINSIGSTKSZ
;