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
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 * $DragonFly: src/lib/libc/gen/sysconf.c,v 1.5 2008/02/21 12:47:54 hasso Exp $
37 #include <sys/param.h>
39 #include <sys/sysctl.h>
40 #include <sys/resource.h>
41 #include <sys/socket.h>
46 #include <pthread.h> /* we just need the limits */
50 #include "../stdlib/atexit.h"
51 #include "../stdtime/tzfile.h"
53 #define _PATH_ZONEINFO TZDIR /* from tzfile.h */
57 * get configurable system variables.
60 * POSIX 1003.1 (ISO/IEC 9945-1, 4.8.1.3) states that the variable values
61 * not change during the lifetime of the calling process. This would seem
62 * to require that any change to system limits kill all running processes.
63 * A workaround might be to cache the values when they are first retrieved
64 * and then simply return the cached value on subsequent calls. This is
65 * less useful than returning up-to-date values, however.
72 int mib
[2], sverrno
, value
;
73 long lvalue
, defaultresult
;
84 if (getrlimit(RLIMIT_NPROC
, &rl
) != 0)
86 if (rl
.rlim_cur
== RLIM_INFINITY
)
88 if (rl
.rlim_cur
> LONG_MAX
) {
92 return ((long)rl
.rlim_cur
);
97 mib
[1] = KERN_NGROUPS
;
100 if (getrlimit(RLIMIT_NOFILE
, &rl
) != 0)
102 if (rl
.rlim_cur
== RLIM_INFINITY
)
104 if (rl
.rlim_cur
> LONG_MAX
) {
108 return ((long)rl
.rlim_cur
);
110 if (getrlimit(RLIMIT_NOFILE
, &rl
) != 0)
112 if (rl
.rlim_cur
== RLIM_INFINITY
)
114 if (rl
.rlim_cur
> LONG_MAX
) {
119 * struct __sFILE currently has a limitation that
120 * file descriptors must fit in a signed short.
121 * This doesn't precisely capture the letter of POSIX
122 * but approximates the spirit.
124 if (rl
.rlim_cur
> SHRT_MAX
)
127 return ((long)rl
.rlim_cur
);
128 case _SC_JOB_CONTROL
:
129 return (_POSIX_JOB_CONTROL
);
131 /* XXX - must be 1 */
133 mib
[1] = KERN_SAVED_IDS
;
137 mib
[1] = KERN_POSIX1
;
139 case _SC_BC_BASE_MAX
:
140 return (BC_BASE_MAX
);
143 case _SC_BC_SCALE_MAX
:
144 return (BC_SCALE_MAX
);
145 case _SC_BC_STRING_MAX
:
146 return (BC_STRING_MAX
);
147 case _SC_COLL_WEIGHTS_MAX
:
148 return (COLL_WEIGHTS_MAX
);
149 case _SC_EXPR_NEST_MAX
:
150 return (EXPR_NEST_MAX
);
157 * This is something of a lie, but it would be silly at
158 * this point to try to deduce this from the contents
161 return (_POSIX2_VERSION
);
163 return (_POSIX2_C_BIND
);
165 return (_POSIX2_C_DEV
);
166 case _SC_2_CHAR_TERM
:
167 return (_POSIX2_CHAR_TERM
);
169 return (_POSIX2_FORT_DEV
);
171 return (_POSIX2_FORT_RUN
);
172 case _SC_2_LOCALEDEF
:
173 return (_POSIX2_LOCALEDEF
);
175 return (_POSIX2_SW_DEV
);
177 return (_POSIX2_UPE
);
179 path
= _PATH_ZONEINFO
;
183 lvalue
= pathconf(path
, _PC_NAME_MAX
);
184 if (lvalue
== -1 && errno
!= 0)
189 case _SC_ASYNCHRONOUS_IO
:
190 #if _POSIX_ASYNCHRONOUS_IO == 0
191 mib
[0] = CTL_P1003_1B
;
192 mib
[1] = CTL_P1003_1B_ASYNCHRONOUS_IO
;
195 return (_POSIX_ASYNCHRONOUS_IO
);
197 case _SC_MAPPED_FILES
:
198 return (_POSIX_MAPPED_FILES
);
200 return (_POSIX_MEMLOCK
);
201 case _SC_MEMLOCK_RANGE
:
202 return (_POSIX_MEMLOCK_RANGE
);
203 case _SC_MEMORY_PROTECTION
:
204 return (_POSIX_MEMORY_PROTECTION
);
205 case _SC_MESSAGE_PASSING
:
206 #if _POSIX_MESSAGE_PASSING == 0
207 mib
[0] = CTL_P1003_1B
;
208 mib
[1] = CTL_P1003_1B_MESSAGE_PASSING
;
211 return (_POSIX_MESSAGE_PASSING
);
213 case _SC_PRIORITIZED_IO
:
214 #if _POSIX_PRIORITIZED_IO == 0
215 mib
[0] = CTL_P1003_1B
;
216 mib
[1] = CTL_P1003_1B_PRIORITIZED_IO
;
219 return (_POSIX_PRIORITIZED_IO
);
221 case _SC_PRIORITY_SCHEDULING
:
222 #if _POSIX_PRIORITY_SCHEDULING == 0
223 mib
[0] = CTL_P1003_1B
;
224 mib
[1] = CTL_P1003_1B_PRIORITY_SCHEDULING
;
227 return (_POSIX_PRIORITY_SCHEDULING
);
229 case _SC_REALTIME_SIGNALS
:
230 #if _POSIX_REALTIME_SIGNALS == 0
231 mib
[0] = CTL_P1003_1B
;
232 mib
[1] = CTL_P1003_1B_REALTIME_SIGNALS
;
235 return (_POSIX_REALTIME_SIGNALS
);
238 #if _POSIX_SEMAPHORES == 0
239 mib
[0] = CTL_P1003_1B
;
240 mib
[1] = CTL_P1003_1B_SEMAPHORES
;
243 return (_POSIX_SEMAPHORES
);
246 return (_POSIX_FSYNC
);
248 case _SC_SHARED_MEMORY_OBJECTS
:
249 return (_POSIX_SHARED_MEMORY_OBJECTS
);
250 case _SC_SYNCHRONIZED_IO
:
251 #if _POSIX_SYNCHRONIZED_IO == 0
252 mib
[0] = CTL_P1003_1B
;
253 mib
[1] = CTL_P1003_1B_SYNCHRONIZED_IO
;
256 return (_POSIX_SYNCHRONIZED_IO
);
259 #if _POSIX_TIMERS == 0
260 mib
[0] = CTL_P1003_1B
;
261 mib
[1] = CTL_P1003_1B_TIMERS
;
264 return (_POSIX_TIMERS
);
266 case _SC_AIO_LISTIO_MAX
:
267 mib
[0] = CTL_P1003_1B
;
268 mib
[1] = CTL_P1003_1B_AIO_LISTIO_MAX
;
271 mib
[0] = CTL_P1003_1B
;
272 mib
[1] = CTL_P1003_1B_AIO_MAX
;
274 case _SC_AIO_PRIO_DELTA_MAX
:
275 mib
[0] = CTL_P1003_1B
;
276 mib
[1] = CTL_P1003_1B_AIO_PRIO_DELTA_MAX
;
278 case _SC_DELAYTIMER_MAX
:
279 mib
[0] = CTL_P1003_1B
;
280 mib
[1] = CTL_P1003_1B_DELAYTIMER_MAX
;
282 case _SC_MQ_OPEN_MAX
:
283 mib
[0] = CTL_P1003_1B
;
284 mib
[1] = CTL_P1003_1B_MQ_OPEN_MAX
;
287 defaultresult
= getpagesize();
288 mib
[0] = CTL_P1003_1B
;
289 mib
[1] = CTL_P1003_1B_PAGESIZE
;
292 mib
[0] = CTL_P1003_1B
;
293 mib
[1] = CTL_P1003_1B_RTSIG_MAX
;
295 case _SC_SEM_NSEMS_MAX
:
296 mib
[0] = CTL_P1003_1B
;
297 mib
[1] = CTL_P1003_1B_SEM_NSEMS_MAX
;
299 case _SC_SEM_VALUE_MAX
:
300 mib
[0] = CTL_P1003_1B
;
301 mib
[1] = CTL_P1003_1B_SEM_VALUE_MAX
;
303 case _SC_SIGQUEUE_MAX
:
304 mib
[0] = CTL_P1003_1B
;
305 mib
[1] = CTL_P1003_1B_SIGQUEUE_MAX
;
308 mib
[0] = CTL_P1003_1B
;
309 mib
[1] = CTL_P1003_1B_TIMER_MAX
;
312 if (sysctl(mib
, 2, &value
, &len
, NULL
, 0) == -1)
315 return (defaultresult
);
316 return ((long)value
);
319 case _SC_2_PBS_ACCOUNTING
:
320 case _SC_2_PBS_CHECKPOINT
:
321 case _SC_2_PBS_LOCATE
:
322 case _SC_2_PBS_MESSAGE
:
323 case _SC_2_PBS_TRACK
:
325 #error "don't know how to determine _SC_2_PBS"
327 * This probably requires digging through the filesystem
328 * to see if the appropriate package has been installed.
329 * Since we don't currently support this option at all,
330 * it's not worth the effort to write the code now.
331 * Figuring out which of the sub-options are supported
332 * would be even more difficult, so it's probably easier
333 * to always say ``no''.
336 return (_POSIX2_PBS
);
338 case _SC_ADVISORY_INFO
:
339 #if _POSIX_ADVISORY_INFO == 0
340 #error "_POSIX_ADVISORY_INFO"
342 return (_POSIX_ADVISORY_INFO
);
345 #if _POSIX_BARRIERS == 0
346 #error "_POSIX_BARRIERS"
348 return (_POSIX_BARRIERS
);
350 case _SC_CLOCK_SELECTION
:
351 #if _POSIX_CLOCK_SELECTION == 0
352 #error "_POSIX_CLOCK_SELECTION"
354 return (_POSIX_CLOCK_SELECTION
);
357 #if _POSIX_CPUTIME == 0
358 #error "_POSIX_CPUTIME"
360 return (_POSIX_CPUTIME
);
363 case _SC_FILE_LOCKING
:
365 * XXX - The standard doesn't tell us how to define
366 * _POSIX_FILE_LOCKING, so we can't answer this one.
369 #if _POSIX_THREAD_SAFE_FUNCTIONS > -1
370 case _SC_GETGR_R_SIZE_MAX
:
371 case _SC_GETPW_R_SIZE_MAX
:
372 #error "somebody needs to implement this"
374 case _SC_HOST_NAME_MAX
:
375 return (MAXHOSTNAMELEN
- 1); /* does not include \0 */
376 case _SC_LOGIN_NAME_MAX
:
378 case _SC_MONOTONIC_CLOCK
:
379 #if _POSIX_MONOTONIC_CLOCK == 0
380 #error "_POSIX_MONOTONIC_CLOCK"
382 return (_POSIX_MONOTONIC_CLOCK
);
384 #if _POSIX_MESSAGE_PASSING > -1
385 case _SC_MQ_PRIO_MAX
:
386 return (MQ_PRIO_MAX
);
388 case _SC_READER_WRITER_LOCKS
:
389 return (_POSIX_READER_WRITER_LOCKS
);
391 return (_POSIX_REGEXP
);
393 return (_POSIX_SHELL
);
395 return (_POSIX_SPAWN
);
397 return (_POSIX_SPIN_LOCKS
);
398 case _SC_SPORADIC_SERVER
:
399 #if _POSIX_SPORADIC_SERVER == 0
400 #error "_POSIX_SPORADIC_SERVER"
402 return (_POSIX_SPORADIC_SERVER
);
404 case _SC_THREAD_ATTR_STACKADDR
:
405 return (_POSIX_THREAD_ATTR_STACKADDR
);
406 case _SC_THREAD_ATTR_STACKSIZE
:
407 return (_POSIX_THREAD_ATTR_STACKSIZE
);
408 case _SC_THREAD_CPUTIME
:
409 return (_POSIX_THREAD_CPUTIME
);
410 case _SC_THREAD_DESTRUCTOR_ITERATIONS
:
411 return (PTHREAD_DESTRUCTOR_ITERATIONS
);
412 case _SC_THREAD_KEYS_MAX
:
413 return (PTHREAD_KEYS_MAX
);
414 case _SC_THREAD_PRIO_INHERIT
:
415 return (_POSIX_THREAD_PRIO_INHERIT
);
416 case _SC_THREAD_PRIO_PROTECT
:
417 return (_POSIX_THREAD_PRIO_PROTECT
);
418 case _SC_THREAD_PRIORITY_SCHEDULING
:
419 return (_POSIX_THREAD_PRIORITY_SCHEDULING
);
420 case _SC_THREAD_PROCESS_SHARED
:
421 return (_POSIX_THREAD_PROCESS_SHARED
);
422 case _SC_THREAD_SAFE_FUNCTIONS
:
423 return (_POSIX_THREAD_SAFE_FUNCTIONS
);
424 case _SC_THREAD_STACK_MIN
:
425 return (PTHREAD_STACK_MIN
);
426 case _SC_THREAD_THREADS_MAX
:
427 return (PTHREAD_THREADS_MAX
); /* XXX wrong type! */
429 return (_POSIX_TIMEOUTS
);
431 return (_POSIX_THREADS
);
433 #if _POSIX_TRACE == 0
434 #error "_POSIX_TRACE"
435 /* While you're implementing this, also do the ones below. */
437 return (_POSIX_TRACE
);
439 #if _POSIX_TRACE > -1
440 case _SC_TRACE_EVENT_FILTER
:
441 return (_POSIX_TRACE_EVENT_FILTER
);
442 case _SC_TRACE_INHERIT
:
443 return (_POSIX_TRACE_INHERIT
);
445 return (_POSIX_TRACE_LOG
);
447 case _SC_TTY_NAME_MAX
:
450 case _SC_TYPED_MEMORY_OBJECTS
:
451 #if _POSIX_TYPED_MEMORY_OBJECTS == 0
452 #error "_POSIX_TYPED_MEMORY_OBJECTS"
454 return (_POSIX_TYPED_MEMORY_OBJECTS
);
456 case _SC_V6_ILP32_OFF32
:
457 #if _V6_ILP32_OFF32 == 0
458 if (sizeof(int) * CHAR_BIT
== 32 &&
459 sizeof(int) == sizeof(long) &&
460 sizeof(long) == sizeof(void *) &&
461 sizeof(void *) == sizeof(off_t
))
466 return (_V6_ILP32_OFF32
);
468 case _SC_V6_ILP32_OFFBIG
:
469 #if _V6_ILP32_OFFBIG == 0
470 if (sizeof(int) * CHAR_BIT
== 32 &&
471 sizeof(int) == sizeof(long) &&
472 sizeof(long) == sizeof(void *) &&
473 sizeof(off_t
) * CHAR_BIT
>= 64)
478 return (_V6_ILP32_OFFBIG
);
480 case _SC_V6_LP64_OFF64
:
481 #if _V6_LP64_OFF64 == 0
482 if (sizeof(int) * CHAR_BIT
== 32 &&
483 sizeof(long) * CHAR_BIT
== 64 &&
484 sizeof(long) == sizeof(void *) &&
485 sizeof(void *) == sizeof(off_t
))
490 return (_V6_LP64_OFF64
);
492 case _SC_V6_LPBIG_OFFBIG
:
493 #if _V6_LPBIG_OFFBIG == 0
494 if (sizeof(int) * CHAR_BIT
>= 32 &&
495 sizeof(long) * CHAR_BIT
>= 64 &&
496 sizeof(void *) * CHAR_BIT
>= 64 &&
497 sizeof(off_t
) * CHAR_BIT
>= 64)
502 return (_V6_LPBIG_OFFBIG
);
505 return (ATEXIT_SIZE
);
508 mib
[1] = KERN_IOV_MAX
;
510 case _SC_XOPEN_CRYPT
:
511 return (_XOPEN_CRYPT
);
512 case _SC_XOPEN_ENH_I18N
:
513 return (_XOPEN_ENH_I18N
);
514 case _SC_XOPEN_LEGACY
:
515 return (_XOPEN_LEGACY
);
516 case _SC_XOPEN_REALTIME
:
517 #if _XOPEN_REALTIME == 0
519 value
= sysconf(_SC_ASYNCHRONOUS_IO
) > 0 &&
520 sysconf(_SC_MEMLOCK
) > 0 &&
521 sysconf(_SC_MEMLOCK_RANGE
) > 0 &&
522 sysconf(_SC_MESSAGE_PASSING
) > 0 &&
523 sysconf(_SC_PRIORITY_SCHEDULING
) > 0 &&
524 sysconf(_SC_REALTIME_SIGNALS
) > 0 &&
525 sysconf(_SC_SEMAPHORES
) > 0 &&
526 sysconf(_SC_SHARED_MEMORY_OBJECTS
) > 0 &&
527 sysconf(_SC_SYNCHRONIZED_IO
) > 0 &&
528 sysconf(_SC_TIMERS
) > 0;
535 return (_XOPEN_REALTIME
);
537 case _SC_XOPEN_REALTIME_THREADS
:
538 #if _XOPEN_REALTIME_THREADS == 0
539 #error "_XOPEN_REALTIME_THREADS"
541 return (_XOPEN_REALTIME_THREADS
);
544 len
= sizeof(lvalue
);
546 if (sysctlbyname("kern.ipc.shmmin", &lvalue
, &len
, NULL
,
553 case _SC_XOPEN_STREAMS
:
554 return (_XOPEN_STREAMS
);
556 return (_XOPEN_UNIX
);
557 #ifdef _XOPEN_VERSION
558 case _SC_XOPEN_VERSION
:
559 return (_XOPEN_VERSION
);
561 #ifdef _XOPEN_XCU_VERSION
562 case _SC_XOPEN_XCU_VERSION
:
563 return (_XOPEN_XCU_VERSION
);
565 case _SC_SYMLOOP_MAX
:
566 return (MAXSYMLINKS
);
567 case _SC_RAW_SOCKETS
:
568 return (_POSIX_RAW_SOCKETS
);
572 value
= socket(PF_INET6
, SOCK_DGRAM
, 0);
580 return (_POSIX_IPV6
);
583 case _SC_NPROCESSORS_CONF
:
584 case _SC_NPROCESSORS_ONLN
:
589 #ifdef _SC_PHYS_PAGES
591 len
= sizeof(lvalue
);
592 if (sysctlbyname("hw.availpages", &lvalue
, &len
, NULL
, 0) == -1)
602 if (sysctl(mib
, 2, &value
, &len
, NULL
, 0) == -1)
604 return ((long)value
);