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 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid
[] = "@(#)sysconf.c 8.2 (Berkeley) 3/20/94";
39 #endif /* LIBC_SCCS and not lint */
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD: src/lib/libc/gen/sysconf.c,v 1.20 2002/11/17 08:54:29 dougb Exp $");
43 #include <sys/param.h>
45 #include <sys/sysctl.h>
46 #include <sys/resource.h>
47 #include <sys/socket.h>
52 #include <pthread.h> /* we just need the limits */
56 #include "../stdlib/atexit.h"
57 #include "../stdtime/tzfile.h"
59 #define _PATH_ZONEINFO TZDIR /* from tzfile.h */
63 * get configurable system variables.
66 * POSIX 1003.1 (ISO/IEC 9945-1, 4.8.1.3) states that the variable values
67 * not change during the lifetime of the calling process. This would seem
68 * to require that any change to system limits kill all running processes.
69 * A workaround might be to cache the values when they are first retrieved
70 * and then simply return the cached value on subsequent calls. This is
71 * less useful than returning up-to-date values, however.
79 int mib
[2], sverrno
, value
;
92 if (getrlimit(RLIMIT_NPROC
, &rl
) != 0)
94 if (rl
.rlim_cur
== RLIM_INFINITY
)
96 if (rl
.rlim_cur
> LONG_MAX
) {
100 return ((long)rl
.rlim_cur
);
103 case _SC_NGROUPS_MAX
:
105 mib
[1] = KERN_NGROUPS
;
108 case _SC_STREAM_MAX
: /* assume fds run out before memory does */
109 if (getrlimit(RLIMIT_NOFILE
, &rl
) != 0)
111 if (rl
.rlim_cur
== RLIM_INFINITY
)
113 if (rl
.rlim_cur
> LONG_MAX
) {
117 return ((long)rl
.rlim_cur
);
118 case _SC_JOB_CONTROL
:
119 return (_POSIX_JOB_CONTROL
);
121 /* XXX - must be 1 */
123 mib
[1] = KERN_SAVED_IDS
;
127 mib
[1] = KERN_POSIX1
;
129 case _SC_BC_BASE_MAX
:
130 return (BC_BASE_MAX
);
133 case _SC_BC_SCALE_MAX
:
134 return (BC_SCALE_MAX
);
135 case _SC_BC_STRING_MAX
:
136 return (BC_STRING_MAX
);
137 case _SC_COLL_WEIGHTS_MAX
:
138 return (COLL_WEIGHTS_MAX
);
139 case _SC_EXPR_NEST_MAX
:
140 return (EXPR_NEST_MAX
);
147 * This is something of a lie, but it would be silly at
148 * this point to try to deduce this from the contents
151 return (_POSIX2_VERSION
);
153 return (_POSIX2_C_BIND
);
155 return (_POSIX2_C_DEV
);
156 case _SC_2_CHAR_TERM
:
157 return (_POSIX2_CHAR_TERM
);
159 return (_POSIX2_FORT_DEV
);
161 return (_POSIX2_FORT_RUN
);
162 case _SC_2_LOCALEDEF
:
163 return (_POSIX2_LOCALEDEF
);
165 return (_POSIX2_SW_DEV
);
167 return (_POSIX2_UPE
);
169 path
= _PATH_ZONEINFO
;
173 value
= pathconf(path
, _PC_NAME_MAX
);
174 if (value
== -1 && errno
!= 0)
179 case _SC_ASYNCHRONOUS_IO
:
180 #if _POSIX_ASYNCHRONOUS_IO == 0
181 mib
[0] = CTL_P1003_1B
;
182 mib
[1] = CTL_P1003_1B_ASYNCHRONOUS_IO
;
185 return (_POSIX_ASYNCHRONOUS_IO
);
187 case _SC_MAPPED_FILES
:
188 return (_POSIX_MAPPED_FILES
);
190 return (_POSIX_MEMLOCK
);
191 case _SC_MEMLOCK_RANGE
:
192 return (_POSIX_MEMLOCK_RANGE
);
193 case _SC_MEMORY_PROTECTION
:
194 return (_POSIX_MEMORY_PROTECTION
);
195 case _SC_MESSAGE_PASSING
:
196 #if _POSIX_MESSAGE_PASSING == 0
197 mib
[0] = CTL_P1003_1B
;
198 mib
[1] = CTL_P1003_1B_MESSAGE_PASSING
;
201 return (_POSIX_MESSAGE_PASSING
);
203 case _SC_PRIORITIZED_IO
:
204 #if _POSIX_PRIORITIZED_IO == 0
205 mib
[0] = CTL_P1003_1B
;
206 mib
[1] = CTL_P1003_1B_PRIORITIZED_IO
;
209 return (_POSIX_PRIORITIZED_IO
);
211 case _SC_PRIORITY_SCHEDULING
:
212 #if _POSIX_PRIORITY_SCHEDULING == 0
213 mib
[0] = CTL_P1003_1B
;
214 mib
[1] = CTL_P1003_1B_PRIORITY_SCHEDULING
;
217 return (_POSIX_PRIORITY_SCHEDULING
);
219 case _SC_REALTIME_SIGNALS
:
220 #if _POSIX_REALTIME_SIGNALS == 0
221 mib
[0] = CTL_P1003_1B
;
222 mib
[1] = CTL_P1003_1B_REALTIME_SIGNALS
;
225 return (_POSIX_REALTIME_SIGNALS
);
228 #if _POSIX_SEMAPHORES == 0
229 mib
[0] = CTL_P1003_1B
;
230 mib
[1] = CTL_P1003_1B_SEMAPHORES
;
233 return (_POSIX_SEMAPHORES
);
236 return (_POSIX_FSYNC
);
238 case _SC_SHARED_MEMORY_OBJECTS
:
239 return (_POSIX_SHARED_MEMORY_OBJECTS
);
240 case _SC_SYNCHRONIZED_IO
:
241 #if _POSIX_SYNCHRONIZED_IO == 0
242 mib
[0] = CTL_P1003_1B
;
243 mib
[1] = CTL_P1003_1B_SYNCHRONIZED_IO
;
246 return (_POSIX_SYNCHRONIZED_IO
);
249 #if _POSIX_TIMERS == 0
250 mib
[0] = CTL_P1003_1B
;
251 mib
[1] = CTL_P1003_1B_TIMERS
;
254 return (_POSIX_TIMERS
);
256 case _SC_AIO_LISTIO_MAX
:
257 mib
[0] = CTL_P1003_1B
;
258 mib
[1] = CTL_P1003_1B_AIO_LISTIO_MAX
;
261 mib
[0] = CTL_P1003_1B
;
262 mib
[1] = CTL_P1003_1B_AIO_MAX
;
264 case _SC_AIO_PRIO_DELTA_MAX
:
265 mib
[0] = CTL_P1003_1B
;
266 mib
[1] = CTL_P1003_1B_AIO_PRIO_DELTA_MAX
;
268 case _SC_DELAYTIMER_MAX
:
269 mib
[0] = CTL_P1003_1B
;
270 mib
[1] = CTL_P1003_1B_DELAYTIMER_MAX
;
272 case _SC_MQ_OPEN_MAX
:
273 mib
[0] = CTL_P1003_1B
;
274 mib
[1] = CTL_P1003_1B_MQ_OPEN_MAX
;
277 defaultresult
= getpagesize();
278 mib
[0] = CTL_P1003_1B
;
279 mib
[1] = CTL_P1003_1B_PAGESIZE
;
282 mib
[0] = CTL_P1003_1B
;
283 mib
[1] = CTL_P1003_1B_RTSIG_MAX
;
285 case _SC_SEM_NSEMS_MAX
:
286 mib
[0] = CTL_P1003_1B
;
287 mib
[1] = CTL_P1003_1B_SEM_NSEMS_MAX
;
289 case _SC_SEM_VALUE_MAX
:
290 mib
[0] = CTL_P1003_1B
;
291 mib
[1] = CTL_P1003_1B_SEM_VALUE_MAX
;
293 case _SC_SIGQUEUE_MAX
:
294 mib
[0] = CTL_P1003_1B
;
295 mib
[1] = CTL_P1003_1B_SIGQUEUE_MAX
;
298 mib
[0] = CTL_P1003_1B
;
299 mib
[1] = CTL_P1003_1B_TIMER_MAX
;
301 yesno
: if (sysctl(mib
, 2, &value
, &len
, NULL
, 0) == -1)
304 return (defaultresult
);
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
:
314 #error "don't know how to determine _SC_2_PBS"
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''.
325 return (_POSIX2_PBS
);
327 case _SC_ADVISORY_INFO
:
328 #if _POSIX_ADVISORY_INFO == 0
329 #error "_POSIX_ADVISORY_INFO"
331 return (_POSIX_ADVISORY_INFO
);
334 #if _POSIX_BARRIERS == 0
335 #error "_POSIX_BARRIERS"
337 return (_POSIX_BARRIERS
);
339 case _SC_CLOCK_SELECTION
:
340 #if _POSIX_CLOCK_SELECTION == 0
341 #error "_POSIX_CLOCK_SELECTION"
343 return (_POSIX_CLOCK_SELECTION
);
346 #if _POSIX_CPUTIME == 0
347 #error "_POSIX_CPUTIME"
349 return (_POSIX_CPUTIME
);
352 case _SC_FILE_LOCKING
:
354 * XXX - The standard doesn't tell us how to define
355 * _POSIX_FILE_LOCKING, so we can't answer this one.
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"
363 case _SC_HOST_NAME_MAX
:
364 return (MAXHOSTNAMELEN
- 1); /* does not include \0 */
365 case _SC_LOGIN_NAME_MAX
:
367 case _SC_MONOTONIC_CLOCK
:
368 #if _POSIX_MONOTONIC_CLOCK == 0
369 #error "_POSIX_MONOTONIC_CLOCK"
371 return (_POSIX_MONOTONIC_CLOCK
);
373 #if _POSIX_MESSAGE_PASSING > -1
374 case _SC_MQ_PRIO_MAX
:
375 return (MQ_PRIO_MAX
);
377 case _SC_READER_WRITER_LOCKS
:
378 return (_POSIX_READER_WRITER_LOCKS
);
380 return (_POSIX_REGEXP
);
382 return (_POSIX_SHELL
);
384 return (_POSIX_SPAWN
);
386 return (_POSIX_SPIN_LOCKS
);
387 case _SC_SPORADIC_SERVER
:
388 #if _POSIX_SPORADIC_SERVER == 0
389 #error "_POSIX_SPORADIC_SERVER"
391 return (_POSIX_SPORADIC_SERVER
);
393 case _SC_THREAD_ATTR_STACKADDR
:
394 return (_POSIX_THREAD_ATTR_STACKADDR
);
395 case _SC_THREAD_ATTR_STACKSIZE
:
396 return (_POSIX_THREAD_ATTR_STACKSIZE
);
397 case _SC_THREAD_CPUTIME
:
398 return (_POSIX_THREAD_CPUTIME
);
399 case _SC_THREAD_DESTRUCTOR_ITERATIONS
:
400 return (PTHREAD_DESTRUCTOR_ITERATIONS
);
401 case _SC_THREAD_KEYS_MAX
:
402 return (PTHREAD_KEYS_MAX
);
403 case _SC_THREAD_PRIO_INHERIT
:
404 return (_POSIX_THREAD_PRIO_INHERIT
);
405 case _SC_THREAD_PRIO_PROTECT
:
406 return (_POSIX_THREAD_PRIO_PROTECT
);
407 case _SC_THREAD_PRIORITY_SCHEDULING
:
408 return (_POSIX_THREAD_PRIORITY_SCHEDULING
);
409 case _SC_THREAD_PROCESS_SHARED
:
410 return (_POSIX_THREAD_PROCESS_SHARED
);
411 case _SC_THREAD_SAFE_FUNCTIONS
:
412 return (_POSIX_THREAD_SAFE_FUNCTIONS
);
413 case _SC_THREAD_STACK_MIN
:
414 return (PTHREAD_STACK_MIN
);
415 case _SC_THREAD_THREADS_MAX
:
416 return (PTHREAD_THREADS_MAX
); /* XXX wrong type! */
418 return (_POSIX_TIMEOUTS
);
420 return (_POSIX_THREADS
);
422 #if _POSIX_TRACE == 0
423 #error "_POSIX_TRACE"
424 /* While you're implementing this, also do the ones below. */
426 return (_POSIX_TRACE
);
428 #if _POSIX_TRACE > -1
429 case _SC_TRACE_EVENT_FILTER
:
430 return (_POSIX_TRACE_EVENT_FILTER
);
431 case _SC_TRACE_INHERIT
:
432 return (_POSIX_TRACE_INHERIT
);
434 return (_POSIX_TRACE_LOG
);
436 case _SC_TTY_NAME_MAX
:
439 case _SC_TYPED_MEMORY_OBJECTS
:
440 #if _POSIX_TYPED_MEMORY_OBJECTS == 0
441 #error "_POSIX_TYPED_MEMORY_OBJECTS"
443 return (_POSIX_TYPED_MEMORY_OBJECTS
);
445 case _SC_V6_ILP32_OFF32
:
446 #if _V6_ILP32_OFF32 == 0
447 if (sizeof(int) * CHAR_BIT
== 32 &&
448 sizeof(int) == sizeof(long) &&
449 sizeof(long) == sizeof(void *) &&
450 sizeof(void *) == sizeof(off_t
))
455 return (_V6_ILP32_OFF32
);
457 case _SC_V6_ILP32_OFFBIG
:
458 #if _V6_ILP32_OFFBIG == 0
459 if (sizeof(int) * CHAR_BIT
== 32 &&
460 sizeof(int) == sizeof(long) &&
461 sizeof(long) == sizeof(void *) &&
462 sizeof(off_t
) * CHAR_BIT
>= 64)
467 return (_V6_ILP32_OFFBIG
);
469 case _SC_V6_LP64_OFF64
:
470 #if _V6_LP64_OFF64 == 0
471 if (sizeof(int) * CHAR_BIT
== 32 &&
472 sizeof(long) * CHAR_BIT
== 64 &&
473 sizeof(long) == sizeof(void *) &&
474 sizeof(void *) == sizeof(off_t
))
479 return (_V6_LP64_OFF64
);
481 case _SC_V6_LPBIG_OFFBIG
:
482 #if _V6_LPBIG_OFFBIG == 0
483 if (sizeof(int) * CHAR_BIT
>= 32 &&
484 sizeof(long) * CHAR_BIT
>= 64 &&
485 sizeof(void *) * CHAR_BIT
>= 64 &&
486 sizeof(off_t
) * CHAR_BIT
>= 64)
491 return (_V6_LPBIG_OFFBIG
);
494 return (ATEXIT_SIZE
);
497 mib
[1] = KERN_IOV_MAX
;
499 case _SC_XOPEN_CRYPT
:
500 return (_XOPEN_CRYPT
);
501 case _SC_XOPEN_ENH_I18N
:
502 return (_XOPEN_ENH_I18N
);
503 case _SC_XOPEN_LEGACY
:
504 return (_XOPEN_LEGACY
);
505 case _SC_XOPEN_REALTIME
:
506 #if _XOPEN_REALTIME == 0
508 value
= sysconf(_SC_ASYNCHRONOUS_IO
) > 0 &&
509 sysconf(_SC_MEMLOCK
) > 0 &&
510 sysconf(_SC_MEMLOCK_RANGE
) > 0 &&
511 sysconf(_SC_MESSAGE_PASSING
) > 0 &&
512 sysconf(_SC_PRIORITY_SCHEDULING
) > 0 &&
513 sysconf(_SC_REALTIME_SIGNALS
) > 0 &&
514 sysconf(_SC_SEMAPHORES
) > 0 &&
515 sysconf(_SC_SHARED_MEMORY_OBJECTS
) > 0 &&
516 sysconf(_SC_SYNCHRONIZED_IO
) > 0 &&
517 sysconf(_SC_TIMERS
) > 0;
524 return (_XOPEN_REALTIME
);
526 case _SC_XOPEN_REALTIME_THREADS
:
527 #if _XOPEN_REALTIME_THREADS == 0
528 #error "_XOPEN_REALTIME_THREADS"
530 return (_XOPEN_REALTIME_THREADS
);
534 if (sysctlbyname("kern.ipc.shmmin", &value
, &len
, NULL
,
541 case _SC_XOPEN_STREAMS
:
542 return (_XOPEN_STREAMS
);
544 return (_XOPEN_UNIX
);
545 #ifdef _XOPEN_VERSION
546 case _SC_XOPEN_VERSION
:
547 return (_XOPEN_VERSION
);
549 #ifdef _XOPEN_XCU_VERSION
550 case _SC_XOPEN_XCU_VERSION
:
551 return (_XOPEN_XCU_VERSION
);
553 case _SC_SYMLOOP_MAX
:
554 return (MAXSYMLINKS
);
555 case _SC_RAW_SOCKETS
:
556 return (_POSIX_RAW_SOCKETS
);
560 value
= socket(PF_INET6
, SOCK_DGRAM
, 0);
568 return (_POSIX_IPV6
);
571 case _SC_NPROCESSORS_CONF
:
572 case _SC_NPROCESSORS_ONLN
:
581 return (sysctl(mib
, 2, &value
, &len
, NULL
, 0) == -1 ? -1 : value
);