Remove building with NOCRYPTO option
[minix3.git] / lib / libc / gen / sysconf.c
blob3ff5d84fddc5c595b9e4ccf7fd932093515082a7
1 /* $NetBSD: sysconf.c,v 1.36 2013/12/19 19:11:50 rmind Exp $ */
3 /*-
4 * Copyright (c) 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Sean Eric Fagan of Cygnus Support.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
35 #include <sys/cdefs.h>
36 #if defined(LIBC_SCCS) && !defined(lint)
37 #if 0
38 static char sccsid[] = "@(#)sysconf.c 8.2 (Berkeley) 3/20/94";
39 #else
40 __RCSID("$NetBSD: sysconf.c,v 1.36 2013/12/19 19:11:50 rmind Exp $");
41 #endif
42 #endif /* LIBC_SCCS and not lint */
44 #include "namespace.h"
45 #include <sys/param.h>
46 #include <sys/sysctl.h>
47 #include <sys/time.h>
48 #include <sys/resource.h>
50 #include <errno.h>
51 #include <limits.h>
52 #include <time.h>
53 #include <unistd.h>
54 #include <paths.h>
55 #include <pwd.h>
57 #ifdef __weak_alias
58 __weak_alias(sysconf,__sysconf)
59 #endif
62 * sysconf --
63 * get configurable system variables.
65 * XXX
66 * POSIX 1003.1 (ISO/IEC 9945-1, 4.8.1.3) states that the variable values
67 * not change during the lifetime of the calling process. This would seem
68 * to require that any change to system limits kill all running processes.
69 * A workaround might be to cache the values when they are first retrieved
70 * and then simply return the cached value on subsequent calls. This is
71 * less useful than returning up-to-date values, however.
73 long
74 sysconf(int name)
76 struct rlimit rl;
77 size_t len;
78 uint64_t mem;
79 int mib[CTL_MAXNAME], value;
80 unsigned int mib_len;
81 struct clockinfo tmpclock;
82 static int clk_tck;
84 len = sizeof(value);
86 /* Default length of the MIB */
87 mib_len = 2;
89 switch (name) {
91 /* 1003.1 */
92 case _SC_ARG_MAX:
93 mib[0] = CTL_KERN;
94 mib[1] = KERN_ARGMAX;
95 break;
96 case _SC_CHILD_MAX:
97 return (getrlimit(RLIMIT_NPROC, &rl) ? -1 : (long)rl.rlim_cur);
98 case _O_SC_CLK_TCK:
100 * For applications compiled when CLK_TCK was a compile-time
101 * constant.
103 return 100;
104 case _SC_CLK_TCK:
106 * Has to be handled specially because it returns a
107 * struct clockinfo instead of an integer. Also, since
108 * this might be called often by some things that
109 * don't grok CLK_TCK can be a macro expanding to a
110 * function, cache the value.
112 if (clk_tck == 0) {
113 mib[0] = CTL_KERN;
114 mib[1] = KERN_CLOCKRATE;
115 len = sizeof(struct clockinfo);
116 clk_tck = sysctl(mib, 2, &tmpclock, &len, NULL, 0)
117 == -1 ? -1 : tmpclock.hz;
119 return(clk_tck);
120 case _SC_JOB_CONTROL:
121 mib[0] = CTL_KERN;
122 mib[1] = KERN_JOB_CONTROL;
123 goto yesno;
124 case _SC_NGROUPS_MAX:
125 mib[0] = CTL_KERN;
126 mib[1] = KERN_NGROUPS;
127 break;
128 case _SC_OPEN_MAX:
129 return (getrlimit(RLIMIT_NOFILE, &rl) ? -1 : (long)rl.rlim_cur);
130 case _SC_STREAM_MAX:
131 mib[0] = CTL_USER;
132 mib[1] = USER_STREAM_MAX;
133 break;
134 case _SC_TZNAME_MAX:
135 mib[0] = CTL_USER;
136 mib[1] = USER_TZNAME_MAX;
137 break;
138 case _SC_SAVED_IDS:
139 mib[0] = CTL_KERN;
140 mib[1] = KERN_SAVED_IDS;
141 goto yesno;
142 case _SC_VERSION:
143 mib[0] = CTL_KERN;
144 mib[1] = KERN_POSIX1;
145 break;
147 /* 1003.1b */
148 case _SC_PAGESIZE:
149 return _getpagesize();
150 case _SC_FSYNC:
151 mib[0] = CTL_KERN;
152 mib[1] = KERN_FSYNC;
153 goto yesno;
154 case _SC_SYNCHRONIZED_IO:
155 mib[0] = CTL_KERN;
156 mib[1] = KERN_SYNCHRONIZED_IO;
157 goto yesno;
158 case _SC_MAPPED_FILES:
159 mib[0] = CTL_KERN;
160 mib[1] = KERN_MAPPED_FILES;
161 goto yesno;
162 case _SC_MEMLOCK:
163 mib[0] = CTL_KERN;
164 mib[1] = KERN_MEMLOCK;
165 goto yesno;
166 case _SC_MEMLOCK_RANGE:
167 mib[0] = CTL_KERN;
168 mib[1] = KERN_MEMLOCK_RANGE;
169 goto yesno;
170 case _SC_MEMORY_PROTECTION:
171 mib[0] = CTL_KERN;
172 mib[1] = KERN_MEMORY_PROTECTION;
173 goto yesno;
174 case _SC_MONOTONIC_CLOCK:
175 mib[0] = CTL_KERN;
176 mib[1] = KERN_MONOTONIC_CLOCK;
177 goto yesno;
178 case _SC_SEMAPHORES:
179 mib[0] = CTL_KERN;
180 mib[1] = KERN_POSIX_SEMAPHORES;
181 goto yesno;
182 case _SC_TIMERS:
183 mib[0] = CTL_KERN;
184 mib[1] = KERN_POSIX_TIMERS;
185 goto yesno;
187 /* 1003.1c */
188 case _SC_LOGIN_NAME_MAX:
189 mib[0] = CTL_KERN;
190 mib[1] = KERN_LOGIN_NAME_MAX;
191 break;
192 case _SC_THREADS:
193 mib[0] = CTL_KERN;
194 mib[1] = KERN_POSIX_THREADS;
195 goto yesno;
197 /* 1003.1j */
198 case _SC_BARRIERS:
199 mib[0] = CTL_KERN;
200 mib[1] = KERN_POSIX_BARRIERS;
201 goto yesno;
202 case _SC_SPIN_LOCKS:
203 mib[0] = CTL_KERN;
204 mib[1] = KERN_POSIX_SPIN_LOCKS;
205 goto yesno;
206 /* Historical; Threads option in 1003.1-2001 */
207 case _SC_READER_WRITER_LOCKS:
208 mib[0] = CTL_KERN;
209 mib[1] = KERN_POSIX_READER_WRITER_LOCKS;
210 goto yesno;
212 /* 1003.2 */
213 case _SC_BC_BASE_MAX:
214 mib[0] = CTL_USER;
215 mib[1] = USER_BC_BASE_MAX;
216 break;
217 case _SC_BC_DIM_MAX:
218 mib[0] = CTL_USER;
219 mib[1] = USER_BC_DIM_MAX;
220 break;
221 case _SC_BC_SCALE_MAX:
222 mib[0] = CTL_USER;
223 mib[1] = USER_BC_SCALE_MAX;
224 break;
225 case _SC_BC_STRING_MAX:
226 mib[0] = CTL_USER;
227 mib[1] = USER_BC_STRING_MAX;
228 break;
229 case _SC_COLL_WEIGHTS_MAX:
230 mib[0] = CTL_USER;
231 mib[1] = USER_COLL_WEIGHTS_MAX;
232 break;
233 case _SC_EXPR_NEST_MAX:
234 mib[0] = CTL_USER;
235 mib[1] = USER_EXPR_NEST_MAX;
236 break;
237 case _SC_LINE_MAX:
238 mib[0] = CTL_USER;
239 mib[1] = USER_LINE_MAX;
240 break;
241 case _SC_RE_DUP_MAX:
242 mib[0] = CTL_USER;
243 mib[1] = USER_RE_DUP_MAX;
244 break;
245 case _SC_2_VERSION:
246 mib[0] = CTL_USER;
247 mib[1] = USER_POSIX2_VERSION;
248 break;
249 case _SC_2_C_BIND:
250 mib[0] = CTL_USER;
251 mib[1] = USER_POSIX2_C_BIND;
252 goto yesno;
253 case _SC_2_C_DEV:
254 mib[0] = CTL_USER;
255 mib[1] = USER_POSIX2_C_DEV;
256 goto yesno;
257 case _SC_2_CHAR_TERM:
258 mib[0] = CTL_USER;
259 mib[1] = USER_POSIX2_CHAR_TERM;
260 goto yesno;
261 case _SC_2_FORT_DEV:
262 mib[0] = CTL_USER;
263 mib[1] = USER_POSIX2_FORT_DEV;
264 goto yesno;
265 case _SC_2_FORT_RUN:
266 mib[0] = CTL_USER;
267 mib[1] = USER_POSIX2_FORT_RUN;
268 goto yesno;
269 case _SC_2_LOCALEDEF:
270 mib[0] = CTL_USER;
271 mib[1] = USER_POSIX2_LOCALEDEF;
272 goto yesno;
273 case _SC_2_SW_DEV:
274 mib[0] = CTL_USER;
275 mib[1] = USER_POSIX2_SW_DEV;
276 goto yesno;
277 case _SC_2_UPE:
278 mib[0] = CTL_USER;
279 mib[1] = USER_POSIX2_UPE;
280 goto yesno;
282 /* XPG 4.2 */
283 case _SC_IOV_MAX:
284 mib[0] = CTL_KERN;
285 mib[1] = KERN_IOV_MAX;
286 break;
287 case _SC_XOPEN_SHM:
288 mib[0] = CTL_KERN;
289 mib[1] = KERN_SYSVIPC;
290 mib[2] = KERN_SYSVIPC_SHM;
291 mib_len = 3;
292 goto yesno;
294 /* 1003.1-2001, XSI Option Group */
295 case _SC_AIO_LISTIO_MAX:
296 if (sysctlgetmibinfo("kern.aio_listio_max", &mib[0], &mib_len,
297 NULL, NULL, NULL, SYSCTL_VERSION))
298 return -1;
299 break;
300 case _SC_AIO_MAX:
301 if (sysctlgetmibinfo("kern.aio_max", &mib[0], &mib_len,
302 NULL, NULL, NULL, SYSCTL_VERSION))
303 return -1;
304 break;
305 case _SC_ASYNCHRONOUS_IO:
306 if (sysctlgetmibinfo("kern.posix_aio", &mib[0], &mib_len,
307 NULL, NULL, NULL, SYSCTL_VERSION))
308 return -1;
309 goto yesno;
310 case _SC_MESSAGE_PASSING:
311 if (sysctlgetmibinfo("kern.posix_msg", &mib[0], &mib_len,
312 NULL, NULL, NULL, SYSCTL_VERSION))
313 return -1;
314 goto yesno;
315 case _SC_MQ_OPEN_MAX:
316 if (sysctlgetmibinfo("kern.mqueue.mq_open_max", &mib[0],
317 &mib_len, NULL, NULL, NULL, SYSCTL_VERSION))
318 return -1;
319 break;
320 case _SC_MQ_PRIO_MAX:
321 if (sysctlgetmibinfo("kern.mqueue.mq_prio_max", &mib[0],
322 &mib_len, NULL, NULL, NULL, SYSCTL_VERSION))
323 return -1;
324 break;
325 case _SC_PRIORITY_SCHEDULING:
326 if (sysctlgetmibinfo("kern.posix_sched", &mib[0], &mib_len,
327 NULL, NULL, NULL, SYSCTL_VERSION))
328 return -1;
329 goto yesno;
330 case _SC_ATEXIT_MAX:
331 mib[0] = CTL_USER;
332 mib[1] = USER_ATEXIT_MAX;
333 break;
335 /* 1003.1-2001, TSF */
336 case _SC_GETGR_R_SIZE_MAX:
337 return _GETGR_R_SIZE_MAX;
338 case _SC_GETPW_R_SIZE_MAX:
339 return _GETPW_R_SIZE_MAX;
341 /* Unsorted */
342 case _SC_HOST_NAME_MAX:
343 return MAXHOSTNAMELEN;
344 case _SC_PASS_MAX:
345 return _PASSWORD_LEN;
346 case _SC_REGEXP:
347 return _POSIX_REGEXP;
348 case _SC_SHARED_MEMORY_OBJECTS:
349 return _POSIX_SHARED_MEMORY_OBJECTS;
350 case _SC_SHELL:
351 return _POSIX_SHELL;
352 #ifndef __minix
353 case _SC_SPAWN:
354 return _POSIX_SPAWN;
355 #endif /* !__minix */
356 case _SC_SYMLOOP_MAX:
357 return MAXSYMLINKS;
359 yesno: if (sysctl(mib, mib_len, &value, &len, NULL, 0) == -1)
360 return (-1);
361 if (value == 0)
362 return (-1);
363 return (value);
365 /* Extensions */
366 case _SC_NPROCESSORS_CONF:
367 mib[0] = CTL_HW;
368 mib[1] = HW_NCPU;
369 break;
370 case _SC_NPROCESSORS_ONLN:
371 mib[0] = CTL_HW;
372 mib[1] = HW_NCPUONLINE;
373 break;
375 /* Linux/Solaris */
376 case _SC_PHYS_PAGES:
377 len = sizeof(mem);
378 mib[0] = CTL_HW;
379 mib[1] = HW_PHYSMEM64;
380 return sysctl(mib, 2, &mem, &len, NULL, 0) == -1 ? -1 :
381 (long)(mem / _getpagesize());
383 /* Native */
384 case _SC_SCHED_RT_TS:
385 if (sysctlgetmibinfo("kern.sched.rtts", &mib[0], &mib_len,
386 NULL, NULL, NULL, SYSCTL_VERSION))
387 return -1;
388 break;
389 case _SC_SCHED_PRI_MIN:
390 if (sysctlgetmibinfo("kern.sched.pri_min", &mib[0], &mib_len,
391 NULL, NULL, NULL, SYSCTL_VERSION))
392 return -1;
393 break;
394 case _SC_SCHED_PRI_MAX:
395 if (sysctlgetmibinfo("kern.sched.pri_max", &mib[0], &mib_len,
396 NULL, NULL, NULL, SYSCTL_VERSION))
397 return -1;
398 break;
399 case _SC_THREAD_DESTRUCTOR_ITERATIONS:
400 return _POSIX_THREAD_DESTRUCTOR_ITERATIONS;
401 case _SC_THREAD_KEYS_MAX:
402 return _POSIX_THREAD_KEYS_MAX;
403 case _SC_THREAD_STACK_MIN:
404 return _getpagesize();
405 case _SC_THREAD_THREADS_MAX:
406 if (sysctlgetmibinfo("kern.maxproc", &mib[0], &mib_len,
407 NULL, NULL, NULL, SYSCTL_VERSION)) /* XXX */
408 return -1;
409 goto yesno;
410 case _SC_THREAD_ATTR_STACKADDR:
411 return _POSIX_THREAD_ATTR_STACKADDR;
412 case _SC_THREAD_ATTR_STACKSIZE:
413 return _POSIX_THREAD_ATTR_STACKSIZE;
414 case _SC_THREAD_SAFE_FUNCTIONS:
415 return _POSIX_THREAD_SAFE_FUNCTIONS;
416 case _SC_THREAD_PRIORITY_SCHEDULING:
417 case _SC_THREAD_PRIO_INHERIT:
418 case _SC_THREAD_PRIO_PROTECT:
419 case _SC_THREAD_PROCESS_SHARED:
420 return -1;
421 case _SC_TTY_NAME_MAX:
422 return pathconf(_PATH_DEV, _PC_NAME_MAX);
423 default:
424 errno = EINVAL;
425 return (-1);
427 return (sysctl(mib, mib_len, &value, &len, NULL, 0) == -1 ? -1 : value);