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