1 /* $NetBSD: sysconf.c,v 1.35 2012/11/02 21:43:07 christos 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.35 2012/11/02 21:43:07 christos 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.
79 int mib
[CTL_MAXNAME
], value
;
81 struct clockinfo tmpclock
;
86 /* Default length of the MIB */
97 return (getrlimit(RLIMIT_NPROC
, &rl
) ? -1 : (long)rl
.rlim_cur
);
100 * For applications compiled when CLK_TCK was a compile-time
106 * Has to be handled specially because it returns a
107 * struct clockinfo instead of an integer. Also, since
108 * this might be called often by some things that
109 * don't grok CLK_TCK can be a macro expanding to a
110 * function, cache the value.
114 mib
[1] = KERN_CLOCKRATE
;
115 len
= sizeof(struct clockinfo
);
116 clk_tck
= sysctl(mib
, 2, &tmpclock
, &len
, NULL
, 0)
117 == -1 ? -1 : tmpclock
.hz
;
120 case _SC_JOB_CONTROL
:
122 mib
[1] = KERN_JOB_CONTROL
;
124 case _SC_NGROUPS_MAX
:
126 mib
[1] = KERN_NGROUPS
;
129 return (getrlimit(RLIMIT_NOFILE
, &rl
) ? -1 : (long)rl
.rlim_cur
);
132 mib
[1] = USER_STREAM_MAX
;
136 mib
[1] = USER_TZNAME_MAX
;
140 mib
[1] = KERN_SAVED_IDS
;
144 mib
[1] = KERN_POSIX1
;
149 return _getpagesize();
154 case _SC_SYNCHRONIZED_IO
:
156 mib
[1] = KERN_SYNCHRONIZED_IO
;
158 case _SC_MAPPED_FILES
:
160 mib
[1] = KERN_MAPPED_FILES
;
164 mib
[1] = KERN_MEMLOCK
;
166 case _SC_MEMLOCK_RANGE
:
168 mib
[1] = KERN_MEMLOCK_RANGE
;
170 case _SC_MEMORY_PROTECTION
:
172 mib
[1] = KERN_MEMORY_PROTECTION
;
174 case _SC_MONOTONIC_CLOCK
:
176 mib
[1] = KERN_MONOTONIC_CLOCK
;
180 mib
[1] = KERN_POSIX_SEMAPHORES
;
184 mib
[1] = KERN_POSIX_TIMERS
;
188 case _SC_LOGIN_NAME_MAX
:
190 mib
[1] = KERN_LOGIN_NAME_MAX
;
194 mib
[1] = KERN_POSIX_THREADS
;
200 mib
[1] = KERN_POSIX_BARRIERS
;
204 mib
[1] = KERN_POSIX_SPIN_LOCKS
;
206 /* Historical; Threads option in 1003.1-2001 */
207 case _SC_READER_WRITER_LOCKS
:
209 mib
[1] = KERN_POSIX_READER_WRITER_LOCKS
;
213 case _SC_BC_BASE_MAX
:
215 mib
[1] = USER_BC_BASE_MAX
;
219 mib
[1] = USER_BC_DIM_MAX
;
221 case _SC_BC_SCALE_MAX
:
223 mib
[1] = USER_BC_SCALE_MAX
;
225 case _SC_BC_STRING_MAX
:
227 mib
[1] = USER_BC_STRING_MAX
;
229 case _SC_COLL_WEIGHTS_MAX
:
231 mib
[1] = USER_COLL_WEIGHTS_MAX
;
233 case _SC_EXPR_NEST_MAX
:
235 mib
[1] = USER_EXPR_NEST_MAX
;
239 mib
[1] = USER_LINE_MAX
;
243 mib
[1] = USER_RE_DUP_MAX
;
247 mib
[1] = USER_POSIX2_VERSION
;
251 mib
[1] = USER_POSIX2_C_BIND
;
255 mib
[1] = USER_POSIX2_C_DEV
;
257 case _SC_2_CHAR_TERM
:
259 mib
[1] = USER_POSIX2_CHAR_TERM
;
263 mib
[1] = USER_POSIX2_FORT_DEV
;
267 mib
[1] = USER_POSIX2_FORT_RUN
;
269 case _SC_2_LOCALEDEF
:
271 mib
[1] = USER_POSIX2_LOCALEDEF
;
275 mib
[1] = USER_POSIX2_SW_DEV
;
279 mib
[1] = USER_POSIX2_UPE
;
285 mib
[1] = KERN_IOV_MAX
;
289 mib
[1] = KERN_SYSVIPC
;
290 mib
[2] = KERN_SYSVIPC_SHM
;
294 /* 1003.1-2001, XSI Option Group */
295 case _SC_AIO_LISTIO_MAX
:
296 if (sysctlgetmibinfo("kern.aio_listio_max", &mib
[0], &mib_len
,
297 NULL
, NULL
, NULL
, SYSCTL_VERSION
))
301 if (sysctlgetmibinfo("kern.aio_max", &mib
[0], &mib_len
,
302 NULL
, NULL
, NULL
, SYSCTL_VERSION
))
305 case _SC_ASYNCHRONOUS_IO
:
306 if (sysctlgetmibinfo("kern.posix_aio", &mib
[0], &mib_len
,
307 NULL
, NULL
, NULL
, SYSCTL_VERSION
))
310 case _SC_MESSAGE_PASSING
:
311 if (sysctlgetmibinfo("kern.posix_msg", &mib
[0], &mib_len
,
312 NULL
, NULL
, NULL
, SYSCTL_VERSION
))
315 case _SC_MQ_OPEN_MAX
:
316 if (sysctlgetmibinfo("kern.mqueue.mq_open_max", &mib
[0],
317 &mib_len
, NULL
, NULL
, NULL
, SYSCTL_VERSION
))
320 case _SC_MQ_PRIO_MAX
:
321 if (sysctlgetmibinfo("kern.mqueue.mq_prio_max", &mib
[0],
322 &mib_len
, NULL
, NULL
, NULL
, SYSCTL_VERSION
))
325 case _SC_PRIORITY_SCHEDULING
:
326 if (sysctlgetmibinfo("kern.posix_sched", &mib
[0], &mib_len
,
327 NULL
, NULL
, NULL
, SYSCTL_VERSION
))
332 mib
[1] = USER_ATEXIT_MAX
;
335 /* 1003.1-2001, TSF */
336 case _SC_GETGR_R_SIZE_MAX
:
337 return _GETGR_R_SIZE_MAX
;
338 case _SC_GETPW_R_SIZE_MAX
:
339 return _GETPW_R_SIZE_MAX
;
342 case _SC_HOST_NAME_MAX
:
343 return MAXHOSTNAMELEN
;
345 return _PASSWORD_LEN
;
347 return _POSIX_REGEXP
;
352 case _SC_SYMLOOP_MAX
:
355 yesno
: if (sysctl(mib
, mib_len
, &value
, &len
, NULL
, 0) == -1)
362 case _SC_NPROCESSORS_CONF
:
366 case _SC_NPROCESSORS_ONLN
:
368 mib
[1] = HW_NCPUONLINE
;
375 mib
[1] = HW_PHYSMEM64
;
376 return sysctl(mib
, 2, &mem
, &len
, NULL
, 0) == -1 ? -1 :
377 (long)(mem
/ _getpagesize());
380 case _SC_SCHED_RT_TS
:
381 if (sysctlgetmibinfo("kern.sched.rtts", &mib
[0], &mib_len
,
382 NULL
, NULL
, NULL
, SYSCTL_VERSION
))
385 case _SC_SCHED_PRI_MIN
:
386 if (sysctlgetmibinfo("kern.sched.pri_min", &mib
[0], &mib_len
,
387 NULL
, NULL
, NULL
, SYSCTL_VERSION
))
390 case _SC_SCHED_PRI_MAX
:
391 if (sysctlgetmibinfo("kern.sched.pri_max", &mib
[0], &mib_len
,
392 NULL
, NULL
, NULL
, SYSCTL_VERSION
))
395 case _SC_THREAD_DESTRUCTOR_ITERATIONS
:
396 return _POSIX_THREAD_DESTRUCTOR_ITERATIONS
;
397 case _SC_THREAD_KEYS_MAX
:
398 return _POSIX_THREAD_KEYS_MAX
;
399 case _SC_THREAD_STACK_MIN
:
400 return _getpagesize();
401 case _SC_THREAD_THREADS_MAX
:
402 if (sysctlgetmibinfo("kern.maxproc", &mib
[0], &mib_len
,
403 NULL
, NULL
, NULL
, SYSCTL_VERSION
)) /* XXX */
406 case _SC_THREAD_ATTR_STACKADDR
:
407 return _POSIX_THREAD_ATTR_STACKADDR
;
408 case _SC_THREAD_ATTR_STACKSIZE
:
409 return _POSIX_THREAD_ATTR_STACKSIZE
;
410 case _SC_THREAD_SAFE_FUNCTIONS
:
411 return _POSIX_THREAD_SAFE_FUNCTIONS
;
412 case _SC_THREAD_PRIORITY_SCHEDULING
:
413 case _SC_THREAD_PRIO_INHERIT
:
414 case _SC_THREAD_PRIO_PROTECT
:
415 case _SC_THREAD_PROCESS_SHARED
:
417 case _SC_TTY_NAME_MAX
:
418 return pathconf(_PATH_DEV
, _PC_NAME_MAX
);
423 return (sysctl(mib
, mib_len
, &value
, &len
, NULL
, 0) == -1 ? -1 : value
);