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)
109 if (rl
.rlim_cur
== RLIM_INFINITY
)
111 if (rl
.rlim_cur
> LONG_MAX
) {
115 return ((long)rl
.rlim_cur
);
116 case _SC_JOB_CONTROL
:
117 return (_POSIX_JOB_CONTROL
);
119 /* XXX - must be 1 */
121 mib
[1] = KERN_SAVED_IDS
;
125 mib
[1] = KERN_POSIX1
;
127 case _SC_BC_BASE_MAX
:
128 return (BC_BASE_MAX
);
131 case _SC_BC_SCALE_MAX
:
132 return (BC_SCALE_MAX
);
133 case _SC_BC_STRING_MAX
:
134 return (BC_STRING_MAX
);
135 case _SC_COLL_WEIGHTS_MAX
:
136 return (COLL_WEIGHTS_MAX
);
137 case _SC_EXPR_NEST_MAX
:
138 return (EXPR_NEST_MAX
);
145 * This is something of a lie, but it would be silly at
146 * this point to try to deduce this from the contents
149 return (_POSIX2_VERSION
);
151 return (_POSIX2_C_BIND
);
153 return (_POSIX2_C_DEV
);
154 case _SC_2_CHAR_TERM
:
155 return (_POSIX2_CHAR_TERM
);
157 return (_POSIX2_FORT_DEV
);
159 return (_POSIX2_FORT_RUN
);
160 case _SC_2_LOCALEDEF
:
161 return (_POSIX2_LOCALEDEF
);
163 return (_POSIX2_SW_DEV
);
165 return (_POSIX2_UPE
);
167 path
= _PATH_ZONEINFO
;
171 lvalue
= pathconf(path
, _PC_NAME_MAX
);
172 if (lvalue
== -1 && errno
!= 0)
177 case _SC_ASYNCHRONOUS_IO
:
178 #if _POSIX_ASYNCHRONOUS_IO == 0
179 mib
[0] = CTL_P1003_1B
;
180 mib
[1] = CTL_P1003_1B_ASYNCHRONOUS_IO
;
183 return (_POSIX_ASYNCHRONOUS_IO
);
185 case _SC_MAPPED_FILES
:
186 return (_POSIX_MAPPED_FILES
);
188 return (_POSIX_MEMLOCK
);
189 case _SC_MEMLOCK_RANGE
:
190 return (_POSIX_MEMLOCK_RANGE
);
191 case _SC_MEMORY_PROTECTION
:
192 return (_POSIX_MEMORY_PROTECTION
);
193 case _SC_MESSAGE_PASSING
:
194 #if _POSIX_MESSAGE_PASSING == 0
195 mib
[0] = CTL_P1003_1B
;
196 mib
[1] = CTL_P1003_1B_MESSAGE_PASSING
;
199 return (_POSIX_MESSAGE_PASSING
);
201 case _SC_PRIORITIZED_IO
:
202 #if _POSIX_PRIORITIZED_IO == 0
203 mib
[0] = CTL_P1003_1B
;
204 mib
[1] = CTL_P1003_1B_PRIORITIZED_IO
;
207 return (_POSIX_PRIORITIZED_IO
);
209 case _SC_PRIORITY_SCHEDULING
:
210 #if _POSIX_PRIORITY_SCHEDULING == 0
211 mib
[0] = CTL_P1003_1B
;
212 mib
[1] = CTL_P1003_1B_PRIORITY_SCHEDULING
;
215 return (_POSIX_PRIORITY_SCHEDULING
);
217 case _SC_REALTIME_SIGNALS
:
218 #if _POSIX_REALTIME_SIGNALS == 0
219 mib
[0] = CTL_P1003_1B
;
220 mib
[1] = CTL_P1003_1B_REALTIME_SIGNALS
;
223 return (_POSIX_REALTIME_SIGNALS
);
226 #if _POSIX_SEMAPHORES == 0
227 mib
[0] = CTL_P1003_1B
;
228 mib
[1] = CTL_P1003_1B_SEMAPHORES
;
231 return (_POSIX_SEMAPHORES
);
234 return (_POSIX_FSYNC
);
236 case _SC_SHARED_MEMORY_OBJECTS
:
237 return (_POSIX_SHARED_MEMORY_OBJECTS
);
238 case _SC_SYNCHRONIZED_IO
:
239 #if _POSIX_SYNCHRONIZED_IO == 0
240 mib
[0] = CTL_P1003_1B
;
241 mib
[1] = CTL_P1003_1B_SYNCHRONIZED_IO
;
244 return (_POSIX_SYNCHRONIZED_IO
);
247 #if _POSIX_TIMERS == 0
248 mib
[0] = CTL_P1003_1B
;
249 mib
[1] = CTL_P1003_1B_TIMERS
;
252 return (_POSIX_TIMERS
);
254 case _SC_AIO_LISTIO_MAX
:
255 #if defined(CTL_P1003_1B) && defined(CTL_P1003_1B_AIO_LISTIO_MAX)
256 mib
[0] = CTL_P1003_1B
;
257 mib
[1] = CTL_P1003_1B_AIO_LISTIO_MAX
;
260 mib
[1] = KERN_AIOMAX
;
264 #if defined(CTL_P1003_1B) && defined(CTL_P1003_1B_AIO_MAX)
265 mib
[0] = CTL_P1003_1B
;
266 mib
[1] = CTL_P1003_1B_AIO_MAX
;
269 mib
[1] = KERN_AIOMAX
;
272 case _SC_AIO_PRIO_DELTA_MAX
:
273 #if defined(CTL_P1003_1B) && defined(CTL_P1003_1B_AIO_PRIO_DELTA_MAX)
274 mib
[0] = CTL_P1003_1B
;
275 mib
[1] = CTL_P1003_1B_AIO_PRIO_DELTA_MAX
;
280 case _SC_DELAYTIMER_MAX
:
281 #if defined(CTL_P1003_1B) && defined(CTL_P1003_1B_DELAYTIMER_MAX)
282 mib
[0] = CTL_P1003_1B
;
283 mib
[1] = CTL_P1003_1B_DELAYTIMER_MAX
;
288 case _SC_MQ_OPEN_MAX
:
289 #if defined(CTL_P1003_1B) && defined(CTL_P1003_1B_MQ_OPEN_MAX)
290 mib
[0] = CTL_P1003_1B
;
291 mib
[1] = CTL_P1003_1B_MQ_OPEN_MAX
;
297 defaultresult
= getpagesize();
298 #if defined(CTL_P1003_1B) && defined(CTL_P1003_1B_PAGESIZE)
299 mib
[0] = CTL_P1003_1B
;
300 mib
[1] = CTL_P1003_1B_PAGESIZE
;
303 return defaultresult
;
306 #if defined(CTL_P1003_1B) && defined(CTL_P1003_1B_RTSIG_MAX)
307 mib
[0] = CTL_P1003_1B
;
308 mib
[1] = CTL_P1003_1B_RTSIG_MAX
;
313 case _SC_SEM_NSEMS_MAX
:
314 #if defined(CTL_P1003_1B) && defined(CTL_P1003_1B_RTSIG_MAX)
315 mib
[0] = CTL_P1003_1B
;
316 mib
[1] = CTL_P1003_1B_SEM_NSEMS_MAX
;
320 return (sysctlbyname("kern.sysv.semmns", &value
, &len
, NULL
, 0) == -1 ? -1 : value
);
323 case _SC_SEM_VALUE_MAX
:
324 #if SEM_VALUE_MAX == 0
325 mib
[0] = CTL_P1003_1B
;
326 mib
[1] = CTL_P1003_1B_SEM_VALUE_MAX
;
329 return (SEM_VALUE_MAX
);
331 case _SC_SIGQUEUE_MAX
:
332 #if defined(CTL_P1003_1B) && defined(CTL_P1003_1B_SIGQUEUE_MAX)
333 mib
[0] = CTL_P1003_1B
;
334 mib
[1] = CTL_P1003_1B_SIGQUEUE_MAX
;
340 #if defined(CTL_P1003_1B) && defined(CTL_P1003_1B_TIMER_MAX)
341 mib
[0] = CTL_P1003_1B
;
342 mib
[1] = CTL_P1003_1B_TIMER_MAX
;
349 if (sysctl(mib
, 2, &value
, &len
, NULL
, 0) == -1)
352 return (defaultresult
);
353 return ((long)value
);
356 case _SC_2_PBS_ACCOUNTING
:
357 case _SC_2_PBS_CHECKPOINT
:
358 case _SC_2_PBS_LOCATE
:
359 case _SC_2_PBS_MESSAGE
:
360 case _SC_2_PBS_TRACK
:
362 #error "don't know how to determine _SC_2_PBS"
364 * This probably requires digging through the filesystem
365 * to see if the appropriate package has been installed.
366 * Since we don't currently support this option at all,
367 * it's not worth the effort to write the code now.
368 * Figuring out which of the sub-options are supported
369 * would be even more difficult, so it's probably easier
370 * to always say ``no''.
373 return (_POSIX2_PBS
);
375 case _SC_ADVISORY_INFO
:
376 #if _POSIX_ADVISORY_INFO == 0
377 #error "_POSIX_ADVISORY_INFO"
379 return (_POSIX_ADVISORY_INFO
);
382 #if _POSIX_BARRIERS == 0
383 #error "_POSIX_BARRIERS"
385 return (_POSIX_BARRIERS
);
387 case _SC_CLOCK_SELECTION
:
388 #if _POSIX_CLOCK_SELECTION == 0
389 #error "_POSIX_CLOCK_SELECTION"
391 return (_POSIX_CLOCK_SELECTION
);
394 #if _POSIX_CPUTIME == 0
395 #error "_POSIX_CPUTIME"
397 return (_POSIX_CPUTIME
);
399 case _SC_FILE_LOCKING
:
400 case _SC_GETGR_R_SIZE_MAX
:
401 /* return sizeof(group) + group_name_max + group_passwd_max + max_group_member*(MAXLOGNAME+1); */
402 return 4096; /* INT_MAX is too big for Perl */
403 case _SC_GETPW_R_SIZE_MAX
:
405 case _SC_HOST_NAME_MAX
:
406 return (MAXHOSTNAMELEN
- 1); /* does not include \0 */
407 case _SC_LOGIN_NAME_MAX
:
409 case _SC_MONOTONIC_CLOCK
:
410 #if _POSIX_MONOTONIC_CLOCK == 0
411 #error "_POSIX_MONOTONIC_CLOCK"
413 return (_POSIX_MONOTONIC_CLOCK
);
415 case _SC_MQ_PRIO_MAX
:
417 case _SC_READER_WRITER_LOCKS
:
418 return (_POSIX_READER_WRITER_LOCKS
);
420 return (_POSIX_REGEXP
);
422 return (_POSIX_SHELL
);
424 return (_POSIX_SPAWN
);
426 return (_POSIX_SPIN_LOCKS
);
427 case _SC_SPORADIC_SERVER
:
428 #if _POSIX_SPORADIC_SERVER == 0
429 #error "_POSIX_SPORADIC_SERVER"
431 return (_POSIX_SPORADIC_SERVER
);
433 case _SC_THREAD_ATTR_STACKADDR
:
434 return (_POSIX_THREAD_ATTR_STACKADDR
);
435 case _SC_THREAD_ATTR_STACKSIZE
:
436 return (_POSIX_THREAD_ATTR_STACKSIZE
);
437 case _SC_THREAD_CPUTIME
:
438 return (_POSIX_THREAD_CPUTIME
);
439 case _SC_THREAD_DESTRUCTOR_ITERATIONS
:
440 return (PTHREAD_DESTRUCTOR_ITERATIONS
);
441 case _SC_THREAD_KEYS_MAX
:
442 return (PTHREAD_KEYS_MAX
);
443 case _SC_THREAD_PRIO_INHERIT
:
444 return (_POSIX_THREAD_PRIO_INHERIT
);
445 case _SC_THREAD_PRIO_PROTECT
:
446 return (_POSIX_THREAD_PRIO_PROTECT
);
447 case _SC_THREAD_PRIORITY_SCHEDULING
:
448 return (_POSIX_THREAD_PRIORITY_SCHEDULING
);
449 case _SC_THREAD_PROCESS_SHARED
:
450 return (_POSIX_THREAD_PROCESS_SHARED
);
451 case _SC_THREAD_SAFE_FUNCTIONS
:
452 return (_POSIX_THREAD_SAFE_FUNCTIONS
);
453 case _SC_THREAD_SPORADIC_SERVER
:
454 return (_POSIX_THREAD_SPORADIC_SERVER
);
455 case _SC_THREAD_STACK_MIN
:
456 return (PTHREAD_STACK_MIN
);
457 case _SC_THREAD_THREADS_MAX
:
458 #ifdef PTHREAD_THREADS_MAX
459 return (PTHREAD_THREADS_MAX
); /* XXX wrong type! */
464 return (_POSIX_TIMEOUTS
);
466 return (_POSIX_THREADS
);
468 #if _POSIX_TRACE == 0
469 #error "_POSIX_TRACE"
470 /* While you're implementing this, also do the ones below. */
472 return (_POSIX_TRACE
);
474 case _SC_TRACE_EVENT_FILTER
:
475 return (_POSIX_TRACE_EVENT_FILTER
);
476 case _SC_TRACE_INHERIT
:
477 return (_POSIX_TRACE_INHERIT
);
479 return (_POSIX_TRACE_LOG
);
480 case _SC_TTY_NAME_MAX
:
481 path
= "/"; // should be _PATH_DEV (PR-3624562)
483 case _SC_TYPED_MEMORY_OBJECTS
:
484 #if _POSIX_TYPED_MEMORY_OBJECTS == 0
485 #error "_POSIX_TYPED_MEMORY_OBJECTS"
487 return (_POSIX_TYPED_MEMORY_OBJECTS
);
489 case _SC_V6_ILP32_OFF32
:
490 #if _V6_ILP32_OFF32 == 0
491 if (sizeof(int) * CHAR_BIT
== 32 &&
492 sizeof(int) == sizeof(long) &&
493 sizeof(long) == sizeof(void *) &&
494 sizeof(void *) == sizeof(off_t
))
499 return (_V6_ILP32_OFF32
);
501 case _SC_V6_ILP32_OFFBIG
:
502 #if _V6_ILP32_OFFBIG == 0
503 if (sizeof(int) * CHAR_BIT
== 32 &&
504 sizeof(int) == sizeof(long) &&
505 sizeof(long) == sizeof(void *) &&
506 sizeof(off_t
) * CHAR_BIT
>= 64)
511 return (_V6_ILP32_OFFBIG
);
513 case _SC_V6_LP64_OFF64
:
514 #if _V6_LP64_OFF64 == 0
515 if (sizeof(int) * CHAR_BIT
== 32 &&
516 sizeof(long) * CHAR_BIT
== 64 &&
517 sizeof(long) == sizeof(void *) &&
518 sizeof(void *) == sizeof(off_t
))
523 return (_V6_LP64_OFF64
);
525 case _SC_V6_LPBIG_OFFBIG
:
526 #if _V6_LPBIG_OFFBIG == 0
527 if (sizeof(int) * CHAR_BIT
>= 32 &&
528 sizeof(long) * CHAR_BIT
>= 64 &&
529 sizeof(void *) * CHAR_BIT
>= 64 &&
530 sizeof(off_t
) * CHAR_BIT
>= 64)
535 return (_V6_LPBIG_OFFBIG
);
538 return (INT_MAX
); /* unlimited */
542 mib
[1] = KERN_IOV_MAX
;
547 case _SC_XOPEN_CRYPT
:
548 return (_XOPEN_CRYPT
);
549 case _SC_XOPEN_ENH_I18N
:
550 return (_XOPEN_ENH_I18N
);
551 case _SC_XOPEN_LEGACY
:
552 return (_XOPEN_LEGACY
);
553 case _SC_XOPEN_REALTIME
:
554 #if _XOPEN_REALTIME == 0
556 value
= sysconf(_SC_ASYNCHRONOUS_IO
) > 0 &&
557 sysconf(_SC_MEMLOCK
) > 0 &&
558 sysconf(_SC_MEMLOCK_RANGE
) > 0 &&
559 sysconf(_SC_MESSAGE_PASSING
) > 0 &&
560 sysconf(_SC_PRIORITY_SCHEDULING
) > 0 &&
561 sysconf(_SC_REALTIME_SIGNALS
) > 0 &&
562 sysconf(_SC_SEMAPHORES
) > 0 &&
563 sysconf(_SC_SHARED_MEMORY_OBJECTS
) > 0 &&
564 sysconf(_SC_SYNCHRONIZED_IO
) > 0 &&
565 sysconf(_SC_TIMERS
) > 0;
572 return (_XOPEN_REALTIME
);
574 case _SC_XOPEN_REALTIME_THREADS
:
575 #if _XOPEN_REALTIME_THREADS == 0
576 #error "_XOPEN_REALTIME_THREADS"
578 return (_XOPEN_REALTIME_THREADS
);
582 len
= sizeof(qdvalue
);
583 if (sysctlbyname("kern.sysv.shmmin", &qdvalue
, &len
, NULL
,
590 case _SC_XOPEN_STREAMS
:
591 return (_XOPEN_STREAMS
);
593 return (_XOPEN_UNIX
);
594 #ifdef _XOPEN_VERSION
595 case _SC_XOPEN_VERSION
:
596 return (_XOPEN_VERSION
);
598 #ifdef _XOPEN_XCU_VERSION
599 case _SC_XOPEN_XCU_VERSION
:
600 return (_XOPEN_XCU_VERSION
);
602 case _SC_SYMLOOP_MAX
:
603 return (MAXSYMLINKS
);
604 case _SC_RAW_SOCKETS
:
605 return (_POSIX_RAW_SOCKETS
);
609 value
= socket(PF_INET6
, SOCK_DGRAM
, 0);
617 return (_POSIX_IPV6
);
620 #ifdef _SC_NPROCESSORS_CONF
621 case _SC_NPROCESSORS_CONF
:
623 #ifdef _SC_NPROCESSORS_ONLN
624 case _SC_NPROCESSORS_ONLN
:
626 #if defined(_SC_NPROCESSORS_CONF) || defined(_SC_NPROCESSORS_ONLN)
631 case _SC_XBS5_ILP32_OFF32
:
632 return (_XBS5_ILP32_OFF32
);
633 case _SC_XBS5_ILP32_OFFBIG
:
634 return (_XBS5_ILP32_OFFBIG
);
635 case _SC_XBS5_LP64_OFF64
:
636 return (_XBS5_LP64_OFF64
);
637 case _SC_XBS5_LPBIG_OFFBIG
:
638 return (_XBS5_LPBIG_OFFBIG
);
639 case _SC_SS_REPL_MAX
:
640 return (_POSIX_SS_REPL_MAX
);
641 case _SC_TRACE_EVENT_NAME_MAX
:
642 return (_POSIX_TRACE_EVENT_NAME_MAX
);
643 case _SC_TRACE_NAME_MAX
:
644 return (_POSIX_TRACE_NAME_MAX
);
645 case _SC_TRACE_SYS_MAX
:
646 return (_POSIX_TRACE_SYS_MAX
);
647 case _SC_TRACE_USER_EVENT_MAX
:
648 return (_POSIX_TRACE_USER_EVENT_MAX
);
652 #ifdef _SC_PHYS_PAGES
659 len
= sizeof(memsize
);
660 if (sysctlbyname("hw.memsize", &memsize
, &len
, NULL
, 0) == -1)
663 len
= sizeof(pagesize
);
664 if (sysctlbyname("hw.pagesize", &pagesize
, &len
, NULL
, 0) == -1)
667 return (memsize
/ pagesize
);
670 len
= sizeof(lvalue
);
671 if (sysctlbyname("hw.availpages", &lvalue
, &len
, NULL
, 0) == -1)
682 if (sysctl(mib
, 2, &value
, &len
, NULL
, 0) == -1)
684 return ((long)value
);