xref: /freebsd/lib/libc/gen/sysconf.c (revision 3f0164abf32b9b761e0a2cb4bdca3a8b84f156d4)
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 <sys/param.h>
40 #include <sys/time.h>
41 #include <sys/sysctl.h>
42 #include <sys/resource.h>
43 #include <sys/socket.h>
44 
45 #include <elf.h>
46 #include <errno.h>
47 #include <limits.h>
48 #include <paths.h>
49 #include <pthread.h>		/* we just need the limits */
50 #include <time.h>
51 #include <unistd.h>
52 
53 #include "../stdlib/atexit.h"
54 #include "tzfile.h"		/* from ../../../contrib/tzcode/stdtime */
55 #include "libc_private.h"
56 
57 #define	_PATH_ZONEINFO	TZDIR	/* from tzfile.h */
58 
59 /*
60  * sysconf --
61  *	get configurable system variables.
62  *
63  * XXX
64  * POSIX 1003.1 (ISO/IEC 9945-1, 4.8.1.3) states that the variable values
65  * not change during the lifetime of the calling process.  This would seem
66  * to require that any change to system limits kill all running processes.
67  * A workaround might be to cache the values when they are first retrieved
68  * and then simply return the cached value on subsequent calls.  This is
69  * less useful than returning up-to-date values, however.
70  */
71 long
72 sysconf(name)
73 	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 		defaultresult = getpagesize();
293 		mib[0] = CTL_P1003_1B;
294 		mib[1] = CTL_P1003_1B_PAGESIZE;
295 		goto yesno;
296 	case _SC_RTSIG_MAX:
297 		mib[0] = CTL_P1003_1B;
298 		mib[1] = CTL_P1003_1B_RTSIG_MAX;
299 		goto yesno;
300 	case _SC_SEM_NSEMS_MAX:
301 		mib[0] = CTL_P1003_1B;
302 		mib[1] = CTL_P1003_1B_SEM_NSEMS_MAX;
303 		goto yesno;
304 	case _SC_SEM_VALUE_MAX:
305 		mib[0] = CTL_P1003_1B;
306 		mib[1] = CTL_P1003_1B_SEM_VALUE_MAX;
307 		goto yesno;
308 	case _SC_SIGQUEUE_MAX:
309 		mib[0] = CTL_P1003_1B;
310 		mib[1] = CTL_P1003_1B_SIGQUEUE_MAX;
311 		goto yesno;
312 	case _SC_TIMER_MAX:
313 		mib[0] = CTL_P1003_1B;
314 		mib[1] = CTL_P1003_1B_TIMER_MAX;
315 yesno:
316 		len = sizeof(value);
317 		if (sysctl(mib, 2, &value, &len, NULL, 0) == -1)
318 			return (-1);
319 		if (value == 0)
320 			return (defaultresult);
321 		return ((long)value);
322 
323 	case _SC_2_PBS:
324 	case _SC_2_PBS_ACCOUNTING:
325 	case _SC_2_PBS_CHECKPOINT:
326 	case _SC_2_PBS_LOCATE:
327 	case _SC_2_PBS_MESSAGE:
328 	case _SC_2_PBS_TRACK:
329 #if _POSIX2_PBS == 0
330 #error "don't know how to determine _SC_2_PBS"
331 		/*
332 		 * This probably requires digging through the filesystem
333 		 * to see if the appropriate package has been installed.
334 		 * Since we don't currently support this option at all,
335 		 * it's not worth the effort to write the code now.
336 		 * Figuring out which of the sub-options are supported
337 		 * would be even more difficult, so it's probably easier
338 		 * to always say ``no''.
339 		 */
340 #else
341 		return (_POSIX2_PBS);
342 #endif
343 	case _SC_ADVISORY_INFO:
344 #if _POSIX_ADVISORY_INFO == 0
345 #error "_POSIX_ADVISORY_INFO"
346 #else
347 		return (_POSIX_ADVISORY_INFO);
348 #endif
349 	case _SC_BARRIERS:
350 #if _POSIX_BARRIERS == 0
351 #error "_POSIX_BARRIERS"
352 #else
353 		return (_POSIX_BARRIERS);
354 #endif
355 	case _SC_CLOCK_SELECTION:
356 #if _POSIX_CLOCK_SELECTION == 0
357 #error "_POSIX_CLOCK_SELECTION"
358 #else
359 		return (_POSIX_CLOCK_SELECTION);
360 #endif
361 	case _SC_CPUTIME:
362 		return (_POSIX_CPUTIME);
363 #ifdef notdef
364 	case _SC_FILE_LOCKING:
365 		/*
366 		 * XXX - The standard doesn't tell us how to define
367 		 * _POSIX_FILE_LOCKING, so we can't answer this one.
368 		 */
369 #endif
370 #if _POSIX_THREAD_SAFE_FUNCTIONS > -1
371 	case _SC_GETGR_R_SIZE_MAX:
372 	case _SC_GETPW_R_SIZE_MAX:
373 #error "somebody needs to implement this"
374 #endif
375 	case _SC_HOST_NAME_MAX:
376 		return (MAXHOSTNAMELEN - 1); /* does not include \0 */
377 	case _SC_LOGIN_NAME_MAX:
378 		return (MAXLOGNAME);
379 	case _SC_MONOTONIC_CLOCK:
380 #if _POSIX_MONOTONIC_CLOCK == 0
381 #error "_POSIX_MONOTONIC_CLOCK"
382 #else
383 		return (_POSIX_MONOTONIC_CLOCK);
384 #endif
385 #if _POSIX_MESSAGE_PASSING > -1
386 	case _SC_MQ_PRIO_MAX:
387 		return (MQ_PRIO_MAX);
388 #endif
389 	case _SC_READER_WRITER_LOCKS:
390 		return (_POSIX_READER_WRITER_LOCKS);
391 	case _SC_REGEXP:
392 		return (_POSIX_REGEXP);
393 	case _SC_SHELL:
394 		return (_POSIX_SHELL);
395 	case _SC_SPAWN:
396 		return (_POSIX_SPAWN);
397 	case _SC_SPIN_LOCKS:
398 		return (_POSIX_SPIN_LOCKS);
399 	case _SC_SPORADIC_SERVER:
400 #if _POSIX_SPORADIC_SERVER == 0
401 #error "_POSIX_SPORADIC_SERVER"
402 #else
403 		return (_POSIX_SPORADIC_SERVER);
404 #endif
405 	case _SC_THREAD_ATTR_STACKADDR:
406 		return (_POSIX_THREAD_ATTR_STACKADDR);
407 	case _SC_THREAD_ATTR_STACKSIZE:
408 		return (_POSIX_THREAD_ATTR_STACKSIZE);
409 	case _SC_THREAD_CPUTIME:
410 		return (_POSIX_THREAD_CPUTIME);
411 	case _SC_THREAD_DESTRUCTOR_ITERATIONS:
412 		return (PTHREAD_DESTRUCTOR_ITERATIONS);
413 	case _SC_THREAD_KEYS_MAX:
414 		return (PTHREAD_KEYS_MAX);
415 	case _SC_THREAD_PRIO_INHERIT:
416 		return (_POSIX_THREAD_PRIO_INHERIT);
417 	case _SC_THREAD_PRIO_PROTECT:
418 		return (_POSIX_THREAD_PRIO_PROTECT);
419 	case _SC_THREAD_PRIORITY_SCHEDULING:
420 		return (_POSIX_THREAD_PRIORITY_SCHEDULING);
421 	case _SC_THREAD_PROCESS_SHARED:
422 		return (_POSIX_THREAD_PROCESS_SHARED);
423 	case _SC_THREAD_SAFE_FUNCTIONS:
424 		return (_POSIX_THREAD_SAFE_FUNCTIONS);
425 	case _SC_THREAD_STACK_MIN:
426 		return (PTHREAD_STACK_MIN);
427 	case _SC_THREAD_THREADS_MAX:
428 		return (PTHREAD_THREADS_MAX); /* XXX wrong type! */
429 	case _SC_TIMEOUTS:
430 		return (_POSIX_TIMEOUTS);
431 	case _SC_THREADS:
432 		return (_POSIX_THREADS);
433 	case _SC_TRACE:
434 #if _POSIX_TRACE == 0
435 #error "_POSIX_TRACE"
436 		/* While you're implementing this, also do the ones below. */
437 #else
438 		return (_POSIX_TRACE);
439 #endif
440 #if _POSIX_TRACE > -1
441 	case _SC_TRACE_EVENT_FILTER:
442 		return (_POSIX_TRACE_EVENT_FILTER);
443 	case _SC_TRACE_INHERIT:
444 		return (_POSIX_TRACE_INHERIT);
445 	case _SC_TRACE_LOG:
446 		return (_POSIX_TRACE_LOG);
447 #endif
448 	case _SC_TTY_NAME_MAX:
449 		path = _PATH_DEV;
450 		goto do_NAME_MAX;
451 	case _SC_TYPED_MEMORY_OBJECTS:
452 #if _POSIX_TYPED_MEMORY_OBJECTS == 0
453 #error "_POSIX_TYPED_MEMORY_OBJECTS"
454 #else
455 		return (_POSIX_TYPED_MEMORY_OBJECTS);
456 #endif
457 	case _SC_V6_ILP32_OFF32:
458 #if _V6_ILP32_OFF32 == 0
459 		if (sizeof(int) * CHAR_BIT == 32 &&
460 		    sizeof(int) == sizeof(long) &&
461 		    sizeof(long) == sizeof(void *) &&
462 		    sizeof(void *) == sizeof(off_t))
463 			return 1;
464 		else
465 			return -1;
466 #else
467 		return (_V6_ILP32_OFF32);
468 #endif
469 	case _SC_V6_ILP32_OFFBIG:
470 #if _V6_ILP32_OFFBIG == 0
471 		if (sizeof(int) * CHAR_BIT == 32 &&
472 		    sizeof(int) == sizeof(long) &&
473 		    sizeof(long) == sizeof(void *) &&
474 		    sizeof(off_t) * CHAR_BIT >= 64)
475 			return 1;
476 		else
477 			return -1;
478 #else
479 		return (_V6_ILP32_OFFBIG);
480 #endif
481 	case _SC_V6_LP64_OFF64:
482 #if _V6_LP64_OFF64 == 0
483 		if (sizeof(int) * CHAR_BIT == 32 &&
484 		    sizeof(long) * CHAR_BIT == 64 &&
485 		    sizeof(long) == sizeof(void *) &&
486 		    sizeof(void *) == sizeof(off_t))
487 			return 1;
488 		else
489 			return -1;
490 #else
491 		return (_V6_LP64_OFF64);
492 #endif
493 	case _SC_V6_LPBIG_OFFBIG:
494 #if _V6_LPBIG_OFFBIG == 0
495 		if (sizeof(int) * CHAR_BIT >= 32 &&
496 		    sizeof(long) * CHAR_BIT >= 64 &&
497 		    sizeof(void *) * CHAR_BIT >= 64 &&
498 		    sizeof(off_t) * CHAR_BIT >= 64)
499 			return 1;
500 		else
501 			return -1;
502 #else
503 		return (_V6_LPBIG_OFFBIG);
504 #endif
505 	case _SC_ATEXIT_MAX:
506 		return (ATEXIT_SIZE);
507 	case _SC_IOV_MAX:
508 		mib[0] = CTL_KERN;
509 		mib[1] = KERN_IOV_MAX;
510 		break;
511 	case _SC_XOPEN_CRYPT:
512 		return (_XOPEN_CRYPT);
513 	case _SC_XOPEN_ENH_I18N:
514 		return (_XOPEN_ENH_I18N);
515 	case _SC_XOPEN_LEGACY:
516 		return (_XOPEN_LEGACY);
517 	case _SC_XOPEN_REALTIME:
518 #if _XOPEN_REALTIME == 0
519 		sverrno = errno;
520 		value = sysconf(_SC_ASYNCHRONOUS_IO) > 0 &&
521 			sysconf(_SC_MEMLOCK) > 0 &&
522 			sysconf(_SC_MEMLOCK_RANGE) > 0 &&
523 			sysconf(_SC_MESSAGE_PASSING) > 0 &&
524 			sysconf(_SC_PRIORITY_SCHEDULING) > 0 &&
525 			sysconf(_SC_REALTIME_SIGNALS) > 0 &&
526 			sysconf(_SC_SEMAPHORES) > 0 &&
527 			sysconf(_SC_SHARED_MEMORY_OBJECTS) > 0 &&
528 			sysconf(_SC_SYNCHRONIZED_IO) > 0 &&
529 			sysconf(_SC_TIMERS) > 0;
530 		errno = sverrno;
531 		if (value)
532 			return (200112L);
533 		else
534 			return (-1);
535 #else
536 		return (_XOPEN_REALTIME);
537 #endif
538 	case _SC_XOPEN_REALTIME_THREADS:
539 #if _XOPEN_REALTIME_THREADS == 0
540 #error "_XOPEN_REALTIME_THREADS"
541 #else
542 		return (_XOPEN_REALTIME_THREADS);
543 #endif
544 	case _SC_XOPEN_SHM:
545 		len = sizeof(lvalue);
546 		sverrno = errno;
547 		if (sysctlbyname("kern.ipc.shmmin", &lvalue, &len, NULL,
548 		    0) == -1) {
549 			errno = sverrno;
550 			return (-1);
551 		}
552 		errno = sverrno;
553 		return (1);
554 	case _SC_XOPEN_STREAMS:
555 		return (_XOPEN_STREAMS);
556 	case _SC_XOPEN_UNIX:
557 		return (_XOPEN_UNIX);
558 #ifdef _XOPEN_VERSION
559 	case _SC_XOPEN_VERSION:
560 		return (_XOPEN_VERSION);
561 #endif
562 #ifdef _XOPEN_XCU_VERSION
563 	case _SC_XOPEN_XCU_VERSION:
564 		return (_XOPEN_XCU_VERSION);
565 #endif
566 	case _SC_SYMLOOP_MAX:
567 		return (MAXSYMLINKS);
568 	case _SC_RAW_SOCKETS:
569 		return (_POSIX_RAW_SOCKETS);
570 	case _SC_IPV6:
571 #if _POSIX_IPV6 == 0
572 		sverrno = errno;
573 		value = socket(PF_INET6, SOCK_DGRAM, 0);
574 		errno = sverrno;
575 		if (value >= 0) {
576 			close(value);
577 			return (200112L);
578 		} else
579 			return (0);
580 #else
581 		return (_POSIX_IPV6);
582 #endif
583 
584 	case _SC_NPROCESSORS_CONF:
585 	case _SC_NPROCESSORS_ONLN:
586 		if (_elf_aux_info(AT_NCPUS, &value, sizeof(value)) == 0)
587 			return ((long)value);
588 		mib[0] = CTL_HW;
589 		mib[1] = HW_NCPU;
590 		break;
591 
592 #ifdef _SC_PHYS_PAGES
593 	case _SC_PHYS_PAGES:
594 		len = sizeof(lvalue);
595 		if (sysctlbyname("hw.availpages", &lvalue, &len, NULL, 0) == -1)
596 			return (-1);
597 		return (lvalue);
598 #endif
599 
600 #ifdef _SC_CPUSET_SIZE
601 	case _SC_CPUSET_SIZE:
602 		len = sizeof(value);
603 		if (sysctlbyname("kern.sched.cpusetsize", &value, &len, NULL,
604 		    0) == -1)
605 			return (-1);
606 		return ((long)value);
607 #endif
608 
609 	default:
610 		errno = EINVAL;
611 		return (-1);
612 	}
613 	len = sizeof(value);
614 	if (sysctl(mib, 2, &value, &len, NULL, 0) == -1)
615 		value = -1;
616 	return ((long)value);
617 }
618