1 /* $NetBSD: sysconf.c,v 1.32 2008/06/25 11:46:12 ad Exp $ */
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Sean Eric Fagan of Cygnus Support.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
36 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid
[] = "@(#)sysconf.c 8.2 (Berkeley) 3/20/94";
40 __RCSID("$NetBSD: sysconf.c,v 1.32 2008/06/25 11:46:12 ad Exp $");
42 #endif /* LIBC_SCCS and not lint */
44 #include "namespace.h"
45 #include <sys/param.h>
46 #include <sys/sysctl.h>
48 #include <sys/resource.h>
58 __weak_alias(sysconf
,__sysconf
)
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.
78 int mib
[CTL_MAXNAME
], value
;
80 struct clockinfo tmpclock
;
85 /* Default length of the MIB */
96 return (getrlimit(RLIMIT_NPROC
, &rl
) ? -1 : (long)rl
.rlim_cur
);
99 * For applications compiled when CLK_TCK was a compile-time
105 * Has to be handled specially because it returns a
106 * struct clockinfo instead of an integer. Also, since
107 * this might be called often by some things that
108 * don't grok CLK_TCK can be a macro expanding to a
109 * function, cache the value.
113 mib
[1] = KERN_CLOCKRATE
;
114 len
= sizeof(struct clockinfo
);
115 clk_tck
= sysctl(mib
, 2, &tmpclock
, &len
, NULL
, 0)
116 == -1 ? -1 : tmpclock
.hz
;
119 case _SC_JOB_CONTROL
:
121 mib
[1] = KERN_JOB_CONTROL
;
123 case _SC_NGROUPS_MAX
:
125 mib
[1] = KERN_NGROUPS
;
128 return (getrlimit(RLIMIT_NOFILE
, &rl
) ? -1 : (long)rl
.rlim_cur
);
131 mib
[1] = USER_STREAM_MAX
;
135 mib
[1] = USER_TZNAME_MAX
;
139 mib
[1] = KERN_SAVED_IDS
;
143 mib
[1] = KERN_POSIX1
;
148 return _getpagesize();
153 case _SC_SYNCHRONIZED_IO
:
155 mib
[1] = KERN_SYNCHRONIZED_IO
;
157 case _SC_MAPPED_FILES
:
159 mib
[1] = KERN_MAPPED_FILES
;
163 mib
[1] = KERN_MEMLOCK
;
165 case _SC_MEMLOCK_RANGE
:
167 mib
[1] = KERN_MEMLOCK_RANGE
;
169 case _SC_MEMORY_PROTECTION
:
171 mib
[1] = KERN_MEMORY_PROTECTION
;
173 case _SC_MONOTONIC_CLOCK
:
175 mib
[1] = KERN_MONOTONIC_CLOCK
;
179 mib
[1] = KERN_POSIX_SEMAPHORES
;
183 mib
[1] = KERN_POSIX_TIMERS
;
187 case _SC_LOGIN_NAME_MAX
:
189 mib
[1] = KERN_LOGIN_NAME_MAX
;
193 mib
[1] = KERN_POSIX_THREADS
;
199 mib
[1] = KERN_POSIX_BARRIERS
;
203 mib
[1] = KERN_POSIX_SPIN_LOCKS
;
205 /* Historical; Threads option in 1003.1-2001 */
206 case _SC_READER_WRITER_LOCKS
:
208 mib
[1] = KERN_POSIX_READER_WRITER_LOCKS
;
212 case _SC_BC_BASE_MAX
:
214 mib
[1] = USER_BC_BASE_MAX
;
218 mib
[1] = USER_BC_DIM_MAX
;
220 case _SC_BC_SCALE_MAX
:
222 mib
[1] = USER_BC_SCALE_MAX
;
224 case _SC_BC_STRING_MAX
:
226 mib
[1] = USER_BC_STRING_MAX
;
228 case _SC_COLL_WEIGHTS_MAX
:
230 mib
[1] = USER_COLL_WEIGHTS_MAX
;
232 case _SC_EXPR_NEST_MAX
:
234 mib
[1] = USER_EXPR_NEST_MAX
;
238 mib
[1] = USER_LINE_MAX
;
242 mib
[1] = USER_RE_DUP_MAX
;
246 mib
[1] = USER_POSIX2_VERSION
;
250 mib
[1] = USER_POSIX2_C_BIND
;
254 mib
[1] = USER_POSIX2_C_DEV
;
256 case _SC_2_CHAR_TERM
:
258 mib
[1] = USER_POSIX2_CHAR_TERM
;
262 mib
[1] = USER_POSIX2_FORT_DEV
;
266 mib
[1] = USER_POSIX2_FORT_RUN
;
268 case _SC_2_LOCALEDEF
:
270 mib
[1] = USER_POSIX2_LOCALEDEF
;
274 mib
[1] = USER_POSIX2_SW_DEV
;
278 mib
[1] = USER_POSIX2_UPE
;
284 mib
[1] = KERN_IOV_MAX
;
288 mib
[1] = KERN_SYSVIPC
;
289 mib
[2] = KERN_SYSVIPC_SHM
;
293 /* 1003.1-2001, XSI Option Group */
294 case _SC_AIO_LISTIO_MAX
:
295 if (sysctlgetmibinfo("kern.aio_listio_max", &mib
[0], &mib_len
,
296 NULL
, NULL
, NULL
, SYSCTL_VERSION
))
300 if (sysctlgetmibinfo("kern.aio_max", &mib
[0], &mib_len
,
301 NULL
, NULL
, NULL
, SYSCTL_VERSION
))
304 case _SC_ASYNCHRONOUS_IO
:
305 if (sysctlgetmibinfo("kern.posix_aio", &mib
[0], &mib_len
,
306 NULL
, NULL
, NULL
, SYSCTL_VERSION
))
309 case _SC_MESSAGE_PASSING
:
310 if (sysctlgetmibinfo("kern.posix_msg", &mib
[0], &mib_len
,
311 NULL
, NULL
, NULL
, SYSCTL_VERSION
))
314 case _SC_MQ_OPEN_MAX
:
315 if (sysctlgetmibinfo("kern.mqueue.mq_open_max", &mib
[0],
316 &mib_len
, NULL
, NULL
, NULL
, SYSCTL_VERSION
))
319 case _SC_MQ_PRIO_MAX
:
320 if (sysctlgetmibinfo("kern.mqueue.mq_prio_max", &mib
[0],
321 &mib_len
, NULL
, NULL
, NULL
, SYSCTL_VERSION
))
324 case _SC_PRIORITY_SCHEDULING
:
325 if (sysctlgetmibinfo("kern.posix_sched", &mib
[0], &mib_len
,
326 NULL
, NULL
, NULL
, SYSCTL_VERSION
))
331 mib
[1] = USER_ATEXIT_MAX
;
334 /* 1003.1-2001, TSF */
335 case _SC_GETGR_R_SIZE_MAX
:
336 return _GETGR_R_SIZE_MAX
;
337 case _SC_GETPW_R_SIZE_MAX
:
338 return _GETPW_R_SIZE_MAX
;
341 case _SC_HOST_NAME_MAX
:
342 return MAXHOSTNAMELEN
;
344 return _PASSWORD_LEN
;
346 return _POSIX_REGEXP
;
349 case _SC_SYMLOOP_MAX
:
352 yesno
: if (sysctl(mib
, mib_len
, &value
, &len
, NULL
, 0) == -1)
359 case _SC_NPROCESSORS_CONF
:
363 case _SC_NPROCESSORS_ONLN
:
365 mib
[1] = HW_NCPUONLINE
;
369 case _SC_SCHED_RT_TS
:
370 if (sysctlgetmibinfo("kern.sched.rtts", &mib
[0], &mib_len
,
371 NULL
, NULL
, NULL
, SYSCTL_VERSION
))
374 case _SC_SCHED_PRI_MIN
:
375 if (sysctlgetmibinfo("kern.sched.pri_min", &mib
[0], &mib_len
,
376 NULL
, NULL
, NULL
, SYSCTL_VERSION
))
379 case _SC_SCHED_PRI_MAX
:
380 if (sysctlgetmibinfo("kern.sched.pri_max", &mib
[0], &mib_len
,
381 NULL
, NULL
, NULL
, SYSCTL_VERSION
))
384 case _SC_THREAD_DESTRUCTOR_ITERATIONS
:
385 return _POSIX_THREAD_DESTRUCTOR_ITERATIONS
;
386 case _SC_THREAD_KEYS_MAX
:
387 return _POSIX_THREAD_KEYS_MAX
;
388 case _SC_THREAD_STACK_MIN
:
389 return _getpagesize();
390 case _SC_THREAD_THREADS_MAX
:
391 if (sysctlgetmibinfo("kern.maxproc", &mib
[0], &mib_len
,
392 NULL
, NULL
, NULL
, SYSCTL_VERSION
)) /* XXX */
395 case _SC_THREAD_ATTR_STACKADDR
:
396 return _POSIX_THREAD_ATTR_STACKADDR
;
397 case _SC_THREAD_ATTR_STACKSIZE
:
398 return _POSIX_THREAD_ATTR_STACKSIZE
;
399 case _SC_THREAD_SAFE_FUNCTIONS
:
400 return _POSIX_THREAD_SAFE_FUNCTIONS
;
401 case _SC_THREAD_PRIORITY_SCHEDULING
:
402 case _SC_THREAD_PRIO_INHERIT
:
403 case _SC_THREAD_PRIO_PROTECT
:
404 case _SC_THREAD_PROCESS_SHARED
:
406 case _SC_TTY_NAME_MAX
:
407 return pathconf(_PATH_DEV
, _PC_NAME_MAX
);
412 return (sysctl(mib
, mib_len
, &value
, &len
, NULL
, 0) == -1 ? -1 : value
);