xref: /freebsd/lib/libc/gen/sysconf.c (revision 1f4bcc459a76b7aa664f3fd557684cd0ba6da352)
1 /*-
2  * Copyright (c) 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Sean Eric Fagan of Cygnus Support.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 4. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #if defined(LIBC_SCCS) && !defined(lint)
34 static char sccsid[] = "@(#)sysconf.c	8.2 (Berkeley) 3/20/94";
35 #endif /* LIBC_SCCS and not lint */
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38 
39 #include "namespace.h"
40 #include <sys/param.h>
41 #include <sys/time.h>
42 #include <sys/sysctl.h>
43 #include <sys/resource.h>
44 #include <sys/socket.h>
45 
46 #include <elf.h>
47 #include <errno.h>
48 #include <limits.h>
49 #include <paths.h>
50 #include <pthread.h>		/* we just need the limits */
51 #include <time.h>
52 #include <unistd.h>
53 #include "un-namespace.h"
54 
55 #include "../stdlib/atexit.h"
56 #include "tzfile.h"		/* from ../../../contrib/tzcode/stdtime */
57 #include "libc_private.h"
58 
59 #define	_PATH_ZONEINFO	TZDIR	/* from tzfile.h */
60 
61 /*
62  * sysconf --
63  *	get configurable system variables.
64  *
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.
72  */
73 long
74 sysconf(int name)
75 {
76 	struct rlimit rl;
77 	size_t len;
78 	int mib[2], sverrno, value;
79 	long lvalue, defaultresult;
80 	const char *path;
81 
82 	defaultresult = -1;
83 
84 	switch (name) {
85 	case _SC_ARG_MAX:
86 		mib[0] = CTL_KERN;
87 		mib[1] = KERN_ARGMAX;
88 		break;
89 	case _SC_CHILD_MAX:
90 		if (getrlimit(RLIMIT_NPROC, &rl) != 0)
91 			return (-1);
92 		if (rl.rlim_cur == RLIM_INFINITY)
93 			return (-1);
94 		if (rl.rlim_cur > LONG_MAX) {
95 			errno = EOVERFLOW;
96 			return (-1);
97 		}
98 		return ((long)rl.rlim_cur);
99 	case _SC_CLK_TCK:
100 		return (CLK_TCK);
101 	case _SC_NGROUPS_MAX:
102 		mib[0] = CTL_KERN;
103 		mib[1] = KERN_NGROUPS;
104 		break;
105 	case _SC_OPEN_MAX:
106 		if (getrlimit(RLIMIT_NOFILE, &rl) != 0)
107 			return (-1);
108 		if (rl.rlim_cur == RLIM_INFINITY)
109 			return (-1);
110 		if (rl.rlim_cur > LONG_MAX) {
111 			errno = EOVERFLOW;
112 			return (-1);
113 		}
114 		return ((long)rl.rlim_cur);
115 	case _SC_STREAM_MAX:
116 		if (getrlimit(RLIMIT_NOFILE, &rl) != 0)
117 			return (-1);
118 		if (rl.rlim_cur == RLIM_INFINITY)
119 			return (-1);
120 		if (rl.rlim_cur > LONG_MAX) {
121 			errno = EOVERFLOW;
122 			return (-1);
123 		}
124 		/*
125 		 * struct __sFILE currently has a limitation that
126 		 * file descriptors must fit in a signed short.
127 		 * This doesn't precisely capture the letter of POSIX
128 		 * but approximates the spirit.
129 		 */
130 		if (rl.rlim_cur > SHRT_MAX)
131 			return (SHRT_MAX);
132 
133 		return ((long)rl.rlim_cur);
134 	case _SC_JOB_CONTROL:
135 		return (_POSIX_JOB_CONTROL);
136 	case _SC_SAVED_IDS:
137 		/* XXX - must be 1 */
138 		mib[0] = CTL_KERN;
139 		mib[1] = KERN_SAVED_IDS;
140 		goto yesno;
141 	case _SC_VERSION:
142 		mib[0] = CTL_KERN;
143 		mib[1] = KERN_POSIX1;
144 		break;
145 	case _SC_BC_BASE_MAX:
146 		return (BC_BASE_MAX);
147 	case _SC_BC_DIM_MAX:
148 		return (BC_DIM_MAX);
149 	case _SC_BC_SCALE_MAX:
150 		return (BC_SCALE_MAX);
151 	case _SC_BC_STRING_MAX:
152 		return (BC_STRING_MAX);
153 	case _SC_COLL_WEIGHTS_MAX:
154 		return (COLL_WEIGHTS_MAX);
155 	case _SC_EXPR_NEST_MAX:
156 		return (EXPR_NEST_MAX);
157 	case _SC_LINE_MAX:
158 		return (LINE_MAX);
159 	case _SC_RE_DUP_MAX:
160 		return (RE_DUP_MAX);
161 	case _SC_2_VERSION:
162 		/*
163 		 * This is something of a lie, but it would be silly at
164 		 * this point to try to deduce this from the contents
165 		 * of the filesystem.
166 		 */
167 		return (_POSIX2_VERSION);
168 	case _SC_2_C_BIND:
169 		return (_POSIX2_C_BIND);
170 	case _SC_2_C_DEV:
171 		return (_POSIX2_C_DEV);
172 	case _SC_2_CHAR_TERM:
173 		return (_POSIX2_CHAR_TERM);
174 	case _SC_2_FORT_DEV:
175 		return (_POSIX2_FORT_DEV);
176 	case _SC_2_FORT_RUN:
177 		return (_POSIX2_FORT_RUN);
178 	case _SC_2_LOCALEDEF:
179 		return (_POSIX2_LOCALEDEF);
180 	case _SC_2_SW_DEV:
181 		return (_POSIX2_SW_DEV);
182 	case _SC_2_UPE:
183 		return (_POSIX2_UPE);
184 	case _SC_TZNAME_MAX:
185 		path = _PATH_ZONEINFO;
186 do_NAME_MAX:
187 		sverrno = errno;
188 		errno = 0;
189 		lvalue = pathconf(path, _PC_NAME_MAX);
190 		if (lvalue == -1 && errno != 0)
191 			return (-1);
192 		errno = sverrno;
193 		return (lvalue);
194 
195 	case _SC_ASYNCHRONOUS_IO:
196 #if _POSIX_ASYNCHRONOUS_IO == 0
197 		mib[0] = CTL_P1003_1B;
198 		mib[1] = CTL_P1003_1B_ASYNCHRONOUS_IO;
199 		break;
200 #else
201 		return (_POSIX_ASYNCHRONOUS_IO);
202 #endif
203 	case _SC_MAPPED_FILES:
204 		return (_POSIX_MAPPED_FILES);
205 	case _SC_MEMLOCK:
206 		return (_POSIX_MEMLOCK);
207 	case _SC_MEMLOCK_RANGE:
208 		return (_POSIX_MEMLOCK_RANGE);
209 	case _SC_MEMORY_PROTECTION:
210 		return (_POSIX_MEMORY_PROTECTION);
211 	case _SC_MESSAGE_PASSING:
212 #if _POSIX_MESSAGE_PASSING == 0
213 		mib[0] = CTL_P1003_1B;
214 		mib[1] = CTL_P1003_1B_MESSAGE_PASSING;
215 		goto yesno;
216 #else
217 		return (_POSIX_MESSAGE_PASSING);
218 #endif
219 	case _SC_PRIORITIZED_IO:
220 #if _POSIX_PRIORITIZED_IO == 0
221 		mib[0] = CTL_P1003_1B;
222 		mib[1] = CTL_P1003_1B_PRIORITIZED_IO;
223 		goto yesno;
224 #else
225 		return (_POSIX_PRIORITIZED_IO);
226 #endif
227 	case _SC_PRIORITY_SCHEDULING:
228 #if _POSIX_PRIORITY_SCHEDULING == 0
229 		mib[0] = CTL_P1003_1B;
230 		mib[1] = CTL_P1003_1B_PRIORITY_SCHEDULING;
231 		goto yesno;
232 #else
233 		return (_POSIX_PRIORITY_SCHEDULING);
234 #endif
235 	case _SC_REALTIME_SIGNALS:
236 #if _POSIX_REALTIME_SIGNALS == 0
237 		mib[0] = CTL_P1003_1B;
238 		mib[1] = CTL_P1003_1B_REALTIME_SIGNALS;
239 		goto yesno;
240 #else
241 		return (_POSIX_REALTIME_SIGNALS);
242 #endif
243 	case _SC_SEMAPHORES:
244 #if _POSIX_SEMAPHORES == 0
245 		mib[0] = CTL_P1003_1B;
246 		mib[1] = CTL_P1003_1B_SEMAPHORES;
247 		goto yesno;
248 #else
249 		return (_POSIX_SEMAPHORES);
250 #endif
251 	case _SC_FSYNC:
252 		return (_POSIX_FSYNC);
253 
254 	case _SC_SHARED_MEMORY_OBJECTS:
255 		return (_POSIX_SHARED_MEMORY_OBJECTS);
256 	case _SC_SYNCHRONIZED_IO:
257 #if _POSIX_SYNCHRONIZED_IO == 0
258 		mib[0] = CTL_P1003_1B;
259 		mib[1] = CTL_P1003_1B_SYNCHRONIZED_IO;
260 		goto yesno;
261 #else
262 		return (_POSIX_SYNCHRONIZED_IO);
263 #endif
264 	case _SC_TIMERS:
265 #if _POSIX_TIMERS == 0
266 		mib[0] = CTL_P1003_1B;
267 		mib[1] = CTL_P1003_1B_TIMERS;
268 		goto yesno;
269 #else
270 		return (_POSIX_TIMERS);
271 #endif
272 	case _SC_AIO_LISTIO_MAX:
273 		mib[0] = CTL_P1003_1B;
274 		mib[1] = CTL_P1003_1B_AIO_LISTIO_MAX;
275 		break;
276 	case _SC_AIO_MAX:
277 		mib[0] = CTL_P1003_1B;
278 		mib[1] = CTL_P1003_1B_AIO_MAX;
279 		break;
280 	case _SC_AIO_PRIO_DELTA_MAX:
281 		mib[0] = CTL_P1003_1B;
282 		mib[1] = CTL_P1003_1B_AIO_PRIO_DELTA_MAX;
283 		break;
284 	case _SC_DELAYTIMER_MAX:
285 		mib[0] = CTL_P1003_1B;
286 		mib[1] = CTL_P1003_1B_DELAYTIMER_MAX;
287 		goto yesno;
288 	case _SC_MQ_OPEN_MAX:
289 		mib[0] = CTL_P1003_1B;
290 		mib[1] = CTL_P1003_1B_MQ_OPEN_MAX;
291 		goto yesno;
292 	case _SC_PAGESIZE:
293 		defaultresult = getpagesize();
294 		mib[0] = CTL_P1003_1B;
295 		mib[1] = CTL_P1003_1B_PAGESIZE;
296 		goto yesno;
297 	case _SC_RTSIG_MAX:
298 		mib[0] = CTL_P1003_1B;
299 		mib[1] = CTL_P1003_1B_RTSIG_MAX;
300 		goto yesno;
301 	case _SC_SEM_NSEMS_MAX:
302 		mib[0] = CTL_P1003_1B;
303 		mib[1] = CTL_P1003_1B_SEM_NSEMS_MAX;
304 		goto yesno;
305 	case _SC_SEM_VALUE_MAX:
306 		mib[0] = CTL_P1003_1B;
307 		mib[1] = CTL_P1003_1B_SEM_VALUE_MAX;
308 		goto yesno;
309 	case _SC_SIGQUEUE_MAX:
310 		mib[0] = CTL_P1003_1B;
311 		mib[1] = CTL_P1003_1B_SIGQUEUE_MAX;
312 		goto yesno;
313 	case _SC_TIMER_MAX:
314 		mib[0] = CTL_P1003_1B;
315 		mib[1] = CTL_P1003_1B_TIMER_MAX;
316 yesno:
317 		len = sizeof(value);
318 		if (sysctl(mib, 2, &value, &len, NULL, 0) == -1)
319 			return (-1);
320 		if (value == 0)
321 			return (defaultresult);
322 		return ((long)value);
323 
324 	case _SC_2_PBS:
325 	case _SC_2_PBS_ACCOUNTING:
326 	case _SC_2_PBS_CHECKPOINT:
327 	case _SC_2_PBS_LOCATE:
328 	case _SC_2_PBS_MESSAGE:
329 	case _SC_2_PBS_TRACK:
330 #if _POSIX2_PBS == 0
331 #error "don't know how to determine _SC_2_PBS"
332 		/*
333 		 * This probably requires digging through the filesystem
334 		 * to see if the appropriate package has been installed.
335 		 * Since we don't currently support this option at all,
336 		 * it's not worth the effort to write the code now.
337 		 * Figuring out which of the sub-options are supported
338 		 * would be even more difficult, so it's probably easier
339 		 * to always say ``no''.
340 		 */
341 #else
342 		return (_POSIX2_PBS);
343 #endif
344 	case _SC_ADVISORY_INFO:
345 #if _POSIX_ADVISORY_INFO == 0
346 #error "_POSIX_ADVISORY_INFO"
347 #else
348 		return (_POSIX_ADVISORY_INFO);
349 #endif
350 	case _SC_BARRIERS:
351 #if _POSIX_BARRIERS == 0
352 #error "_POSIX_BARRIERS"
353 #else
354 		return (_POSIX_BARRIERS);
355 #endif
356 	case _SC_CLOCK_SELECTION:
357 #if _POSIX_CLOCK_SELECTION == 0
358 #error "_POSIX_CLOCK_SELECTION"
359 #else
360 		return (_POSIX_CLOCK_SELECTION);
361 #endif
362 	case _SC_CPUTIME:
363 		return (_POSIX_CPUTIME);
364 #ifdef notdef
365 	case _SC_FILE_LOCKING:
366 		/*
367 		 * XXX - The standard doesn't tell us how to define
368 		 * _POSIX_FILE_LOCKING, so we can't answer this one.
369 		 */
370 #endif
371 
372 	/*
373 	 * SUSv4tc1 says the following about _SC_GETGR_R_SIZE_MAX and
374 	 * _SC_GETPW_R_SIZE_MAX:
375 	 * Note that sysconf(_SC_GETGR_R_SIZE_MAX) may return -1 if
376 	 * there is no hard limit on the size of the buffer needed to
377 	 * store all the groups returned.
378 	 */
379 	case _SC_GETGR_R_SIZE_MAX:
380 	case _SC_GETPW_R_SIZE_MAX:
381 		return (-1);
382 	case _SC_HOST_NAME_MAX:
383 		return (MAXHOSTNAMELEN - 1); /* does not include \0 */
384 	case _SC_LOGIN_NAME_MAX:
385 		return (MAXLOGNAME);
386 	case _SC_MONOTONIC_CLOCK:
387 #if _POSIX_MONOTONIC_CLOCK == 0
388 #error "_POSIX_MONOTONIC_CLOCK"
389 #else
390 		return (_POSIX_MONOTONIC_CLOCK);
391 #endif
392 #if _POSIX_MESSAGE_PASSING > -1
393 	case _SC_MQ_PRIO_MAX:
394 		return (MQ_PRIO_MAX);
395 #endif
396 	case _SC_READER_WRITER_LOCKS:
397 		return (_POSIX_READER_WRITER_LOCKS);
398 	case _SC_REGEXP:
399 		return (_POSIX_REGEXP);
400 	case _SC_SHELL:
401 		return (_POSIX_SHELL);
402 	case _SC_SPAWN:
403 		return (_POSIX_SPAWN);
404 	case _SC_SPIN_LOCKS:
405 		return (_POSIX_SPIN_LOCKS);
406 	case _SC_SPORADIC_SERVER:
407 #if _POSIX_SPORADIC_SERVER == 0
408 #error "_POSIX_SPORADIC_SERVER"
409 #else
410 		return (_POSIX_SPORADIC_SERVER);
411 #endif
412 	case _SC_THREAD_ATTR_STACKADDR:
413 		return (_POSIX_THREAD_ATTR_STACKADDR);
414 	case _SC_THREAD_ATTR_STACKSIZE:
415 		return (_POSIX_THREAD_ATTR_STACKSIZE);
416 	case _SC_THREAD_CPUTIME:
417 		return (_POSIX_THREAD_CPUTIME);
418 	case _SC_THREAD_DESTRUCTOR_ITERATIONS:
419 		return (PTHREAD_DESTRUCTOR_ITERATIONS);
420 	case _SC_THREAD_KEYS_MAX:
421 		return (PTHREAD_KEYS_MAX);
422 	case _SC_THREAD_PRIO_INHERIT:
423 		return (_POSIX_THREAD_PRIO_INHERIT);
424 	case _SC_THREAD_PRIO_PROTECT:
425 		return (_POSIX_THREAD_PRIO_PROTECT);
426 	case _SC_THREAD_PRIORITY_SCHEDULING:
427 		return (_POSIX_THREAD_PRIORITY_SCHEDULING);
428 	case _SC_THREAD_PROCESS_SHARED:
429 		return (_POSIX_THREAD_PROCESS_SHARED);
430 	case _SC_THREAD_SAFE_FUNCTIONS:
431 		return (_POSIX_THREAD_SAFE_FUNCTIONS);
432 	case _SC_THREAD_STACK_MIN:
433 		return (PTHREAD_STACK_MIN);
434 	case _SC_THREAD_THREADS_MAX:
435 		return (PTHREAD_THREADS_MAX); /* XXX wrong type! */
436 	case _SC_TIMEOUTS:
437 		return (_POSIX_TIMEOUTS);
438 	case _SC_THREADS:
439 		return (_POSIX_THREADS);
440 	case _SC_TRACE:
441 #if _POSIX_TRACE == 0
442 #error "_POSIX_TRACE"
443 		/* While you're implementing this, also do the ones below. */
444 #else
445 		return (_POSIX_TRACE);
446 #endif
447 #if _POSIX_TRACE > -1
448 	case _SC_TRACE_EVENT_FILTER:
449 		return (_POSIX_TRACE_EVENT_FILTER);
450 	case _SC_TRACE_INHERIT:
451 		return (_POSIX_TRACE_INHERIT);
452 	case _SC_TRACE_LOG:
453 		return (_POSIX_TRACE_LOG);
454 #endif
455 	case _SC_TTY_NAME_MAX:
456 		path = _PATH_DEV;
457 		goto do_NAME_MAX;
458 	case _SC_TYPED_MEMORY_OBJECTS:
459 #if _POSIX_TYPED_MEMORY_OBJECTS == 0
460 #error "_POSIX_TYPED_MEMORY_OBJECTS"
461 #else
462 		return (_POSIX_TYPED_MEMORY_OBJECTS);
463 #endif
464 	case _SC_V6_ILP32_OFF32:
465 #if _V6_ILP32_OFF32 == 0
466 		if (sizeof(int) * CHAR_BIT == 32 &&
467 		    sizeof(int) == sizeof(long) &&
468 		    sizeof(long) == sizeof(void *) &&
469 		    sizeof(void *) == sizeof(off_t))
470 			return 1;
471 		else
472 			return -1;
473 #else
474 		return (_V6_ILP32_OFF32);
475 #endif
476 	case _SC_V6_ILP32_OFFBIG:
477 #if _V6_ILP32_OFFBIG == 0
478 		if (sizeof(int) * CHAR_BIT == 32 &&
479 		    sizeof(int) == sizeof(long) &&
480 		    sizeof(long) == sizeof(void *) &&
481 		    sizeof(off_t) * CHAR_BIT >= 64)
482 			return 1;
483 		else
484 			return -1;
485 #else
486 		return (_V6_ILP32_OFFBIG);
487 #endif
488 	case _SC_V6_LP64_OFF64:
489 #if _V6_LP64_OFF64 == 0
490 		if (sizeof(int) * CHAR_BIT == 32 &&
491 		    sizeof(long) * CHAR_BIT == 64 &&
492 		    sizeof(long) == sizeof(void *) &&
493 		    sizeof(void *) == sizeof(off_t))
494 			return 1;
495 		else
496 			return -1;
497 #else
498 		return (_V6_LP64_OFF64);
499 #endif
500 	case _SC_V6_LPBIG_OFFBIG:
501 #if _V6_LPBIG_OFFBIG == 0
502 		if (sizeof(int) * CHAR_BIT >= 32 &&
503 		    sizeof(long) * CHAR_BIT >= 64 &&
504 		    sizeof(void *) * CHAR_BIT >= 64 &&
505 		    sizeof(off_t) * CHAR_BIT >= 64)
506 			return 1;
507 		else
508 			return -1;
509 #else
510 		return (_V6_LPBIG_OFFBIG);
511 #endif
512 	case _SC_ATEXIT_MAX:
513 		return (ATEXIT_SIZE);
514 	case _SC_IOV_MAX:
515 		mib[0] = CTL_KERN;
516 		mib[1] = KERN_IOV_MAX;
517 		break;
518 	case _SC_XOPEN_CRYPT:
519 		return (_XOPEN_CRYPT);
520 	case _SC_XOPEN_ENH_I18N:
521 		return (_XOPEN_ENH_I18N);
522 	case _SC_XOPEN_LEGACY:
523 		return (_XOPEN_LEGACY);
524 	case _SC_XOPEN_REALTIME:
525 #if _XOPEN_REALTIME == 0
526 		sverrno = errno;
527 		value = sysconf(_SC_ASYNCHRONOUS_IO) > 0 &&
528 			sysconf(_SC_MEMLOCK) > 0 &&
529 			sysconf(_SC_MEMLOCK_RANGE) > 0 &&
530 			sysconf(_SC_MESSAGE_PASSING) > 0 &&
531 			sysconf(_SC_PRIORITY_SCHEDULING) > 0 &&
532 			sysconf(_SC_REALTIME_SIGNALS) > 0 &&
533 			sysconf(_SC_SEMAPHORES) > 0 &&
534 			sysconf(_SC_SHARED_MEMORY_OBJECTS) > 0 &&
535 			sysconf(_SC_SYNCHRONIZED_IO) > 0 &&
536 			sysconf(_SC_TIMERS) > 0;
537 		errno = sverrno;
538 		if (value)
539 			return (200112L);
540 		else
541 			return (-1);
542 #else
543 		return (_XOPEN_REALTIME);
544 #endif
545 	case _SC_XOPEN_REALTIME_THREADS:
546 #if _XOPEN_REALTIME_THREADS == 0
547 #error "_XOPEN_REALTIME_THREADS"
548 #else
549 		return (_XOPEN_REALTIME_THREADS);
550 #endif
551 	case _SC_XOPEN_SHM:
552 		len = sizeof(lvalue);
553 		sverrno = errno;
554 		if (sysctlbyname("kern.ipc.shmmin", &lvalue, &len, NULL,
555 		    0) == -1) {
556 			errno = sverrno;
557 			return (-1);
558 		}
559 		errno = sverrno;
560 		return (1);
561 	case _SC_XOPEN_STREAMS:
562 		return (_XOPEN_STREAMS);
563 	case _SC_XOPEN_UNIX:
564 		return (_XOPEN_UNIX);
565 #ifdef _XOPEN_VERSION
566 	case _SC_XOPEN_VERSION:
567 		return (_XOPEN_VERSION);
568 #endif
569 #ifdef _XOPEN_XCU_VERSION
570 	case _SC_XOPEN_XCU_VERSION:
571 		return (_XOPEN_XCU_VERSION);
572 #endif
573 	case _SC_SYMLOOP_MAX:
574 		return (MAXSYMLINKS);
575 	case _SC_RAW_SOCKETS:
576 		return (_POSIX_RAW_SOCKETS);
577 	case _SC_IPV6:
578 #if _POSIX_IPV6 == 0
579 		sverrno = errno;
580 		value = _socket(PF_INET6, SOCK_DGRAM, 0);
581 		errno = sverrno;
582 		if (value >= 0) {
583 			_close(value);
584 			return (200112L);
585 		} else
586 			return (0);
587 #else
588 		return (_POSIX_IPV6);
589 #endif
590 
591 	case _SC_NPROCESSORS_CONF:
592 	case _SC_NPROCESSORS_ONLN:
593 		if (_elf_aux_info(AT_NCPUS, &value, sizeof(value)) == 0)
594 			return ((long)value);
595 		mib[0] = CTL_HW;
596 		mib[1] = HW_NCPU;
597 		break;
598 
599 #ifdef _SC_PHYS_PAGES
600 	case _SC_PHYS_PAGES:
601 		len = sizeof(lvalue);
602 		if (sysctlbyname("hw.availpages", &lvalue, &len, NULL, 0) == -1)
603 			return (-1);
604 		return (lvalue);
605 #endif
606 
607 #ifdef _SC_CPUSET_SIZE
608 	case _SC_CPUSET_SIZE:
609 		len = sizeof(value);
610 		if (sysctlbyname("kern.sched.cpusetsize", &value, &len, NULL,
611 		    0) == -1)
612 			return (-1);
613 		return ((long)value);
614 #endif
615 
616 	default:
617 		errno = EINVAL;
618 		return (-1);
619 	}
620 	len = sizeof(value);
621 	if (sysctl(mib, 2, &value, &len, NULL, 0) == -1)
622 		value = -1;
623 	return ((long)value);
624 }
625