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