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