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