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