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