3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Sean Eric Fagan of Cygnus Support.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
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: lib/libc/gen/sysconf.c r168718 $");
39 #include <sys/param.h>
41 #include <sys/sysctl.h>
42 #include <sys/resource.h>
43 #include <sys/socket.h>
45 #include <sys/semaphore.h>
50 #include <pthread.h> /* we just need the limits */
54 #include "../../stdlib/FreeBSD/atexit.h"
55 #include "../../stdtime/FreeBSD/tzfile.h"
57 #define _PATH_ZONEINFO TZDIR /* from tzfile.h */
61 * get configurable system variables.
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.
76 quad_t qdvalue
; /* for kern.sysv.shmmin */
78 int mib
[3], sverrno
, value
;
79 long lvalue
, defaultresult
;
90 if (getrlimit(RLIMIT_NPROC
, &rl
) != 0)
92 if (rl
.rlim_cur
== RLIM_INFINITY
)
94 if (rl
.rlim_cur
> LONG_MAX
) {
98 return ((long)rl
.rlim_cur
);
101 case _SC_NGROUPS_MAX
:
103 mib
[1] = KERN_NGROUPS
;
106 case _SC_STREAM_MAX
: /* assume fds run out before memory does */
107 if (getrlimit(RLIMIT_NOFILE
, &rl
) != 0)
111 * In 64 bit world, sysconf is able to correctly represent
112 * rlim_t values (8 bytes).
114 if (rl
.rlim_cur
> RLIM_INFINITY
)
116 if (rl
.rlim_cur
> LONG_MAX
) {
120 lvalue
= rl
.rlim_cur
;
123 * In 32 bit world (watches), sysconf falls back to the
124 * old behavior: use (int) OPEN_MAX as max limit. Ideally
125 * we would use maxfilesperproc as max limit, which is a
126 * system value adjusted by the kernel based on available
127 * RAM size. This value defaults to OPEN_MAX for systems
128 * with less than 8G of RAM. So we use OPEN_MAX (defined
129 * in sys/syslimits.h) as max limit here in case
130 * applications with strict sandbox rules prevent libc
131 * from reading maxfilesperproc via sysctl.
133 if (rl
.rlim_cur
> OPEN_MAX
)
136 lvalue
= (long)rl
.rlim_cur
;
139 case _SC_JOB_CONTROL
:
140 return (_POSIX_JOB_CONTROL
);
142 /* XXX - must be 1 */
144 mib
[1] = KERN_SAVED_IDS
;
148 mib
[1] = KERN_POSIX1
;
150 case _SC_BC_BASE_MAX
:
151 return (BC_BASE_MAX
);
154 case _SC_BC_SCALE_MAX
:
155 return (BC_SCALE_MAX
);
156 case _SC_BC_STRING_MAX
:
157 return (BC_STRING_MAX
);
158 case _SC_COLL_WEIGHTS_MAX
:
159 return (COLL_WEIGHTS_MAX
);
160 case _SC_EXPR_NEST_MAX
:
161 return (EXPR_NEST_MAX
);
168 * This is something of a lie, but it would be silly at
169 * this point to try to deduce this from the contents
172 return (_POSIX2_VERSION
);
174 return (_POSIX2_C_BIND
);
176 return (_POSIX2_C_DEV
);
177 case _SC_2_CHAR_TERM
:
178 return (_POSIX2_CHAR_TERM
);
180 return (_POSIX2_FORT_DEV
);
182 return (_POSIX2_FORT_RUN
);
183 case _SC_2_LOCALEDEF
:
184 return (_POSIX2_LOCALEDEF
);
186 return (_POSIX2_SW_DEV
);
188 return (_POSIX2_UPE
);
190 path
= _PATH_ZONEINFO
;
194 lvalue
= pathconf(path
, _PC_NAME_MAX
);
195 if (lvalue
== -1 && errno
!= 0)
200 case _SC_ASYNCHRONOUS_IO
:
201 #if _POSIX_ASYNCHRONOUS_IO == 0
202 mib
[0] = CTL_P1003_1B
;
203 mib
[1] = CTL_P1003_1B_ASYNCHRONOUS_IO
;
206 return (_POSIX_ASYNCHRONOUS_IO
);
208 case _SC_MAPPED_FILES
:
209 return (_POSIX_MAPPED_FILES
);
211 return (_POSIX_MEMLOCK
);
212 case _SC_MEMLOCK_RANGE
:
213 return (_POSIX_MEMLOCK_RANGE
);
214 case _SC_MEMORY_PROTECTION
:
215 return (_POSIX_MEMORY_PROTECTION
);
216 case _SC_MESSAGE_PASSING
:
217 #if _POSIX_MESSAGE_PASSING == 0
218 mib
[0] = CTL_P1003_1B
;
219 mib
[1] = CTL_P1003_1B_MESSAGE_PASSING
;
222 return (_POSIX_MESSAGE_PASSING
);
224 case _SC_PRIORITIZED_IO
:
225 #if _POSIX_PRIORITIZED_IO == 0
226 mib
[0] = CTL_P1003_1B
;
227 mib
[1] = CTL_P1003_1B_PRIORITIZED_IO
;
230 return (_POSIX_PRIORITIZED_IO
);
232 case _SC_PRIORITY_SCHEDULING
:
233 #if _POSIX_PRIORITY_SCHEDULING == 0
234 mib
[0] = CTL_P1003_1B
;
235 mib
[1] = CTL_P1003_1B_PRIORITY_SCHEDULING
;
238 return (_POSIX_PRIORITY_SCHEDULING
);
240 case _SC_REALTIME_SIGNALS
:
241 #if _POSIX_REALTIME_SIGNALS == 0
242 mib
[0] = CTL_P1003_1B
;
243 mib
[1] = CTL_P1003_1B_REALTIME_SIGNALS
;
246 return (_POSIX_REALTIME_SIGNALS
);
249 #if _POSIX_SEMAPHORES == 0
250 mib
[0] = CTL_P1003_1B
;
251 mib
[1] = CTL_P1003_1B_SEMAPHORES
;
254 return (_POSIX_SEMAPHORES
);
257 return (_POSIX_FSYNC
);
259 case _SC_SHARED_MEMORY_OBJECTS
:
260 return (_POSIX_SHARED_MEMORY_OBJECTS
);
261 case _SC_SYNCHRONIZED_IO
:
262 #if _POSIX_SYNCHRONIZED_IO == 0
263 mib
[0] = CTL_P1003_1B
;
264 mib
[1] = CTL_P1003_1B_SYNCHRONIZED_IO
;
267 return (_POSIX_SYNCHRONIZED_IO
);
270 #if _POSIX_TIMERS == 0
271 mib
[0] = CTL_P1003_1B
;
272 mib
[1] = CTL_P1003_1B_TIMERS
;
275 return (_POSIX_TIMERS
);
277 case _SC_AIO_LISTIO_MAX
:
278 #if defined(CTL_P1003_1B) && defined(CTL_P1003_1B_AIO_LISTIO_MAX)
279 mib
[0] = CTL_P1003_1B
;
280 mib
[1] = CTL_P1003_1B_AIO_LISTIO_MAX
;
283 mib
[1] = KERN_AIOMAX
;
287 #if defined(CTL_P1003_1B) && defined(CTL_P1003_1B_AIO_MAX)
288 mib
[0] = CTL_P1003_1B
;
289 mib
[1] = CTL_P1003_1B_AIO_MAX
;
292 mib
[1] = KERN_AIOMAX
;
295 case _SC_AIO_PRIO_DELTA_MAX
:
296 #if defined(CTL_P1003_1B) && defined(CTL_P1003_1B_AIO_PRIO_DELTA_MAX)
297 mib
[0] = CTL_P1003_1B
;
298 mib
[1] = CTL_P1003_1B_AIO_PRIO_DELTA_MAX
;
303 case _SC_DELAYTIMER_MAX
:
304 #if defined(CTL_P1003_1B) && defined(CTL_P1003_1B_DELAYTIMER_MAX)
305 mib
[0] = CTL_P1003_1B
;
306 mib
[1] = CTL_P1003_1B_DELAYTIMER_MAX
;
311 case _SC_MQ_OPEN_MAX
:
312 #if defined(CTL_P1003_1B) && defined(CTL_P1003_1B_MQ_OPEN_MAX)
313 mib
[0] = CTL_P1003_1B
;
314 mib
[1] = CTL_P1003_1B_MQ_OPEN_MAX
;
320 defaultresult
= getpagesize();
321 #if defined(CTL_P1003_1B) && defined(CTL_P1003_1B_PAGESIZE)
322 mib
[0] = CTL_P1003_1B
;
323 mib
[1] = CTL_P1003_1B_PAGESIZE
;
326 return defaultresult
;
329 #if defined(CTL_P1003_1B) && defined(CTL_P1003_1B_RTSIG_MAX)
330 mib
[0] = CTL_P1003_1B
;
331 mib
[1] = CTL_P1003_1B_RTSIG_MAX
;
336 case _SC_SEM_NSEMS_MAX
:
337 #if defined(CTL_P1003_1B) && defined(CTL_P1003_1B_RTSIG_MAX)
338 mib
[0] = CTL_P1003_1B
;
339 mib
[1] = CTL_P1003_1B_SEM_NSEMS_MAX
;
343 return (sysctlbyname("kern.sysv.semmns", &value
, &len
, NULL
, 0) == -1 ? -1 : value
);
346 case _SC_SEM_VALUE_MAX
:
347 #if SEM_VALUE_MAX == 0
348 mib
[0] = CTL_P1003_1B
;
349 mib
[1] = CTL_P1003_1B_SEM_VALUE_MAX
;
352 return (SEM_VALUE_MAX
);
354 case _SC_SIGQUEUE_MAX
:
355 #if defined(CTL_P1003_1B) && defined(CTL_P1003_1B_SIGQUEUE_MAX)
356 mib
[0] = CTL_P1003_1B
;
357 mib
[1] = CTL_P1003_1B_SIGQUEUE_MAX
;
363 #if defined(CTL_P1003_1B) && defined(CTL_P1003_1B_TIMER_MAX)
364 mib
[0] = CTL_P1003_1B
;
365 mib
[1] = CTL_P1003_1B_TIMER_MAX
;
372 if (sysctl(mib
, 2, &value
, &len
, NULL
, 0) == -1)
375 return (defaultresult
);
376 return ((long)value
);
379 case _SC_2_PBS_ACCOUNTING
:
380 case _SC_2_PBS_CHECKPOINT
:
381 case _SC_2_PBS_LOCATE
:
382 case _SC_2_PBS_MESSAGE
:
383 case _SC_2_PBS_TRACK
:
385 #error "don't know how to determine _SC_2_PBS"
387 * This probably requires digging through the filesystem
388 * to see if the appropriate package has been installed.
389 * Since we don't currently support this option at all,
390 * it's not worth the effort to write the code now.
391 * Figuring out which of the sub-options are supported
392 * would be even more difficult, so it's probably easier
393 * to always say ``no''.
396 return (_POSIX2_PBS
);
398 case _SC_ADVISORY_INFO
:
399 #if _POSIX_ADVISORY_INFO == 0
400 #error "_POSIX_ADVISORY_INFO"
402 return (_POSIX_ADVISORY_INFO
);
405 #if _POSIX_BARRIERS == 0
406 #error "_POSIX_BARRIERS"
408 return (_POSIX_BARRIERS
);
410 case _SC_CLOCK_SELECTION
:
411 #if _POSIX_CLOCK_SELECTION == 0
412 #error "_POSIX_CLOCK_SELECTION"
414 return (_POSIX_CLOCK_SELECTION
);
417 #if _POSIX_CPUTIME == 0
418 #error "_POSIX_CPUTIME"
420 return (_POSIX_CPUTIME
);
422 case _SC_FILE_LOCKING
:
423 case _SC_GETGR_R_SIZE_MAX
:
424 /* return sizeof(group) + group_name_max + group_passwd_max + max_group_member*(MAXLOGNAME+1); */
425 return 4096; /* INT_MAX is too big for Perl */
426 case _SC_GETPW_R_SIZE_MAX
:
428 case _SC_HOST_NAME_MAX
:
429 return (MAXHOSTNAMELEN
- 1); /* does not include \0 */
430 case _SC_LOGIN_NAME_MAX
:
432 case _SC_MONOTONIC_CLOCK
:
433 #if _POSIX_MONOTONIC_CLOCK == 0
434 #error "_POSIX_MONOTONIC_CLOCK"
436 return (_POSIX_MONOTONIC_CLOCK
);
438 case _SC_MQ_PRIO_MAX
:
440 case _SC_READER_WRITER_LOCKS
:
441 return (_POSIX_READER_WRITER_LOCKS
);
443 return (_POSIX_REGEXP
);
445 return (_POSIX_SHELL
);
447 return (_POSIX_SPAWN
);
449 return (_POSIX_SPIN_LOCKS
);
450 case _SC_SPORADIC_SERVER
:
451 #if _POSIX_SPORADIC_SERVER == 0
452 #error "_POSIX_SPORADIC_SERVER"
454 return (_POSIX_SPORADIC_SERVER
);
456 case _SC_THREAD_ATTR_STACKADDR
:
457 return (_POSIX_THREAD_ATTR_STACKADDR
);
458 case _SC_THREAD_ATTR_STACKSIZE
:
459 return (_POSIX_THREAD_ATTR_STACKSIZE
);
460 case _SC_THREAD_CPUTIME
:
461 return (_POSIX_THREAD_CPUTIME
);
462 case _SC_THREAD_DESTRUCTOR_ITERATIONS
:
463 return (PTHREAD_DESTRUCTOR_ITERATIONS
);
464 case _SC_THREAD_KEYS_MAX
:
465 return (PTHREAD_KEYS_MAX
);
466 case _SC_THREAD_PRIO_INHERIT
:
467 return (_POSIX_THREAD_PRIO_INHERIT
);
468 case _SC_THREAD_PRIO_PROTECT
:
469 return (_POSIX_THREAD_PRIO_PROTECT
);
470 case _SC_THREAD_PRIORITY_SCHEDULING
:
471 return (_POSIX_THREAD_PRIORITY_SCHEDULING
);
472 case _SC_THREAD_PROCESS_SHARED
:
473 return (_POSIX_THREAD_PROCESS_SHARED
);
474 case _SC_THREAD_SAFE_FUNCTIONS
:
475 return (_POSIX_THREAD_SAFE_FUNCTIONS
);
476 case _SC_THREAD_SPORADIC_SERVER
:
477 return (_POSIX_THREAD_SPORADIC_SERVER
);
478 case _SC_THREAD_STACK_MIN
:
479 return (PTHREAD_STACK_MIN
);
480 case _SC_THREAD_THREADS_MAX
:
481 #ifdef PTHREAD_THREADS_MAX
482 return (PTHREAD_THREADS_MAX
); /* XXX wrong type! */
487 return (_POSIX_TIMEOUTS
);
489 return (_POSIX_THREADS
);
491 #if _POSIX_TRACE == 0
492 #error "_POSIX_TRACE"
493 /* While you're implementing this, also do the ones below. */
495 return (_POSIX_TRACE
);
497 case _SC_TRACE_EVENT_FILTER
:
498 return (_POSIX_TRACE_EVENT_FILTER
);
499 case _SC_TRACE_INHERIT
:
500 return (_POSIX_TRACE_INHERIT
);
502 return (_POSIX_TRACE_LOG
);
503 case _SC_TTY_NAME_MAX
:
504 path
= "/"; // should be _PATH_DEV (PR-3624562)
506 case _SC_TYPED_MEMORY_OBJECTS
:
507 #if _POSIX_TYPED_MEMORY_OBJECTS == 0
508 #error "_POSIX_TYPED_MEMORY_OBJECTS"
510 return (_POSIX_TYPED_MEMORY_OBJECTS
);
512 case _SC_V6_ILP32_OFF32
:
513 #if _V6_ILP32_OFF32 == 0
514 if (sizeof(int) * CHAR_BIT
== 32 &&
515 sizeof(int) == sizeof(long) &&
516 sizeof(long) == sizeof(void *) &&
517 sizeof(void *) == sizeof(off_t
))
522 return (_V6_ILP32_OFF32
);
524 case _SC_V6_ILP32_OFFBIG
:
525 #if _V6_ILP32_OFFBIG == 0
526 if (sizeof(int) * CHAR_BIT
== 32 &&
527 sizeof(int) == sizeof(long) &&
528 sizeof(long) == sizeof(void *) &&
529 sizeof(off_t
) * CHAR_BIT
>= 64)
534 return (_V6_ILP32_OFFBIG
);
536 case _SC_V6_LP64_OFF64
:
537 #if _V6_LP64_OFF64 == 0
538 if (sizeof(int) * CHAR_BIT
== 32 &&
539 sizeof(long) * CHAR_BIT
== 64 &&
540 sizeof(long) == sizeof(void *) &&
541 sizeof(void *) == sizeof(off_t
))
546 return (_V6_LP64_OFF64
);
548 case _SC_V6_LPBIG_OFFBIG
:
549 #if _V6_LPBIG_OFFBIG == 0
550 if (sizeof(int) * CHAR_BIT
>= 32 &&
551 sizeof(long) * CHAR_BIT
>= 64 &&
552 sizeof(void *) * CHAR_BIT
>= 64 &&
553 sizeof(off_t
) * CHAR_BIT
>= 64)
558 return (_V6_LPBIG_OFFBIG
);
561 return (INT_MAX
); /* unlimited */
565 mib
[1] = KERN_IOV_MAX
;
570 case _SC_XOPEN_CRYPT
:
571 return (_XOPEN_CRYPT
);
572 case _SC_XOPEN_ENH_I18N
:
573 return (_XOPEN_ENH_I18N
);
574 case _SC_XOPEN_LEGACY
:
575 return (_XOPEN_LEGACY
);
576 case _SC_XOPEN_REALTIME
:
577 #if _XOPEN_REALTIME == 0
579 value
= sysconf(_SC_ASYNCHRONOUS_IO
) > 0 &&
580 sysconf(_SC_MEMLOCK
) > 0 &&
581 sysconf(_SC_MEMLOCK_RANGE
) > 0 &&
582 sysconf(_SC_MESSAGE_PASSING
) > 0 &&
583 sysconf(_SC_PRIORITY_SCHEDULING
) > 0 &&
584 sysconf(_SC_REALTIME_SIGNALS
) > 0 &&
585 sysconf(_SC_SEMAPHORES
) > 0 &&
586 sysconf(_SC_SHARED_MEMORY_OBJECTS
) > 0 &&
587 sysconf(_SC_SYNCHRONIZED_IO
) > 0 &&
588 sysconf(_SC_TIMERS
) > 0;
595 return (_XOPEN_REALTIME
);
597 case _SC_XOPEN_REALTIME_THREADS
:
598 #if _XOPEN_REALTIME_THREADS == 0
599 #error "_XOPEN_REALTIME_THREADS"
601 return (_XOPEN_REALTIME_THREADS
);
605 len
= sizeof(qdvalue
);
606 if (sysctlbyname("kern.sysv.shmmin", &qdvalue
, &len
, NULL
,
613 case _SC_XOPEN_STREAMS
:
614 return (_XOPEN_STREAMS
);
616 return (_XOPEN_UNIX
);
617 #ifdef _XOPEN_VERSION
618 case _SC_XOPEN_VERSION
:
619 return (_XOPEN_VERSION
);
621 #ifdef _XOPEN_XCU_VERSION
622 case _SC_XOPEN_XCU_VERSION
:
623 return (_XOPEN_XCU_VERSION
);
625 case _SC_SYMLOOP_MAX
:
626 return (MAXSYMLINKS
);
627 case _SC_RAW_SOCKETS
:
628 return (_POSIX_RAW_SOCKETS
);
632 value
= socket(PF_INET6
, SOCK_DGRAM
, 0);
640 return (_POSIX_IPV6
);
643 #ifdef _SC_NPROCESSORS_CONF
644 case _SC_NPROCESSORS_CONF
:
646 #ifdef _SC_NPROCESSORS_ONLN
647 case _SC_NPROCESSORS_ONLN
:
649 #if defined(_SC_NPROCESSORS_CONF) || defined(_SC_NPROCESSORS_ONLN)
654 case _SC_XBS5_ILP32_OFF32
:
655 return (_XBS5_ILP32_OFF32
);
656 case _SC_XBS5_ILP32_OFFBIG
:
657 return (_XBS5_ILP32_OFFBIG
);
658 case _SC_XBS5_LP64_OFF64
:
659 return (_XBS5_LP64_OFF64
);
660 case _SC_XBS5_LPBIG_OFFBIG
:
661 return (_XBS5_LPBIG_OFFBIG
);
662 case _SC_SS_REPL_MAX
:
663 return (_POSIX_SS_REPL_MAX
);
664 case _SC_TRACE_EVENT_NAME_MAX
:
665 return (_POSIX_TRACE_EVENT_NAME_MAX
);
666 case _SC_TRACE_NAME_MAX
:
667 return (_POSIX_TRACE_NAME_MAX
);
668 case _SC_TRACE_SYS_MAX
:
669 return (_POSIX_TRACE_SYS_MAX
);
670 case _SC_TRACE_USER_EVENT_MAX
:
671 return (_POSIX_TRACE_USER_EVENT_MAX
);
675 #ifdef _SC_PHYS_PAGES
682 len
= sizeof(memsize
);
683 if (sysctlbyname("hw.memsize", &memsize
, &len
, NULL
, 0) == -1)
686 len
= sizeof(pagesize
);
687 if (sysctlbyname("hw.pagesize", &pagesize
, &len
, NULL
, 0) == -1)
690 return (memsize
/ pagesize
);
693 len
= sizeof(lvalue
);
694 if (sysctlbyname("hw.availpages", &lvalue
, &len
, NULL
, 0) == -1)
705 if (sysctl(mib
, 2, &value
, &len
, NULL
, 0) == -1)
707 return ((long)value
);