Sync usage with man page.
[netbsd-mini2440.git] / lib / libc / gen / sysconf.c
blob9278c119a5a74813b4a6c4886e6cbaedc3be5bce
1 /* $NetBSD: sysconf.c,v 1.32 2008/06/25 11:46:12 ad Exp $ */
3 /*-
4 * Copyright (c) 1993
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
12 * are met:
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
32 * SUCH DAMAGE.
35 #include <sys/cdefs.h>
36 #if defined(LIBC_SCCS) && !defined(lint)
37 #if 0
38 static char sccsid[] = "@(#)sysconf.c 8.2 (Berkeley) 3/20/94";
39 #else
40 __RCSID("$NetBSD: sysconf.c,v 1.32 2008/06/25 11:46:12 ad Exp $");
41 #endif
42 #endif /* LIBC_SCCS and not lint */
44 #include "namespace.h"
45 #include <sys/param.h>
46 #include <sys/sysctl.h>
47 #include <sys/time.h>
48 #include <sys/resource.h>
50 #include <errno.h>
51 #include <limits.h>
52 #include <time.h>
53 #include <unistd.h>
54 #include <paths.h>
55 #include <pwd.h>
57 #ifdef __weak_alias
58 __weak_alias(sysconf,__sysconf)
59 #endif
62 * sysconf --
63 * get configurable system variables.
65 * XXX
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.
73 long
74 sysconf(int name)
76 struct rlimit rl;
77 size_t len;
78 int mib[CTL_MAXNAME], value;
79 unsigned int mib_len;
80 struct clockinfo tmpclock;
81 static int clk_tck;
83 len = sizeof(value);
85 /* Default length of the MIB */
86 mib_len = 2;
88 switch (name) {
90 /* 1003.1 */
91 case _SC_ARG_MAX:
92 mib[0] = CTL_KERN;
93 mib[1] = KERN_ARGMAX;
94 break;
95 case _SC_CHILD_MAX:
96 return (getrlimit(RLIMIT_NPROC, &rl) ? -1 : (long)rl.rlim_cur);
97 case _O_SC_CLK_TCK:
99 * For applications compiled when CLK_TCK was a compile-time
100 * constant.
102 return 100;
103 case _SC_CLK_TCK:
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.
111 if (clk_tck == 0) {
112 mib[0] = CTL_KERN;
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;
118 return(clk_tck);
119 case _SC_JOB_CONTROL:
120 mib[0] = CTL_KERN;
121 mib[1] = KERN_JOB_CONTROL;
122 goto yesno;
123 case _SC_NGROUPS_MAX:
124 mib[0] = CTL_KERN;
125 mib[1] = KERN_NGROUPS;
126 break;
127 case _SC_OPEN_MAX:
128 return (getrlimit(RLIMIT_NOFILE, &rl) ? -1 : (long)rl.rlim_cur);
129 case _SC_STREAM_MAX:
130 mib[0] = CTL_USER;
131 mib[1] = USER_STREAM_MAX;
132 break;
133 case _SC_TZNAME_MAX:
134 mib[0] = CTL_USER;
135 mib[1] = USER_TZNAME_MAX;
136 break;
137 case _SC_SAVED_IDS:
138 mib[0] = CTL_KERN;
139 mib[1] = KERN_SAVED_IDS;
140 goto yesno;
141 case _SC_VERSION:
142 mib[0] = CTL_KERN;
143 mib[1] = KERN_POSIX1;
144 break;
146 /* 1003.1b */
147 case _SC_PAGESIZE:
148 return _getpagesize();
149 case _SC_FSYNC:
150 mib[0] = CTL_KERN;
151 mib[1] = KERN_FSYNC;
152 goto yesno;
153 case _SC_SYNCHRONIZED_IO:
154 mib[0] = CTL_KERN;
155 mib[1] = KERN_SYNCHRONIZED_IO;
156 goto yesno;
157 case _SC_MAPPED_FILES:
158 mib[0] = CTL_KERN;
159 mib[1] = KERN_MAPPED_FILES;
160 goto yesno;
161 case _SC_MEMLOCK:
162 mib[0] = CTL_KERN;
163 mib[1] = KERN_MEMLOCK;
164 goto yesno;
165 case _SC_MEMLOCK_RANGE:
166 mib[0] = CTL_KERN;
167 mib[1] = KERN_MEMLOCK_RANGE;
168 goto yesno;
169 case _SC_MEMORY_PROTECTION:
170 mib[0] = CTL_KERN;
171 mib[1] = KERN_MEMORY_PROTECTION;
172 goto yesno;
173 case _SC_MONOTONIC_CLOCK:
174 mib[0] = CTL_KERN;
175 mib[1] = KERN_MONOTONIC_CLOCK;
176 goto yesno;
177 case _SC_SEMAPHORES:
178 mib[0] = CTL_KERN;
179 mib[1] = KERN_POSIX_SEMAPHORES;
180 goto yesno;
181 case _SC_TIMERS:
182 mib[0] = CTL_KERN;
183 mib[1] = KERN_POSIX_TIMERS;
184 goto yesno;
186 /* 1003.1c */
187 case _SC_LOGIN_NAME_MAX:
188 mib[0] = CTL_KERN;
189 mib[1] = KERN_LOGIN_NAME_MAX;
190 break;
191 case _SC_THREADS:
192 mib[0] = CTL_KERN;
193 mib[1] = KERN_POSIX_THREADS;
194 goto yesno;
196 /* 1003.1j */
197 case _SC_BARRIERS:
198 mib[0] = CTL_KERN;
199 mib[1] = KERN_POSIX_BARRIERS;
200 goto yesno;
201 case _SC_SPIN_LOCKS:
202 mib[0] = CTL_KERN;
203 mib[1] = KERN_POSIX_SPIN_LOCKS;
204 goto yesno;
205 /* Historical; Threads option in 1003.1-2001 */
206 case _SC_READER_WRITER_LOCKS:
207 mib[0] = CTL_KERN;
208 mib[1] = KERN_POSIX_READER_WRITER_LOCKS;
209 goto yesno;
211 /* 1003.2 */
212 case _SC_BC_BASE_MAX:
213 mib[0] = CTL_USER;
214 mib[1] = USER_BC_BASE_MAX;
215 break;
216 case _SC_BC_DIM_MAX:
217 mib[0] = CTL_USER;
218 mib[1] = USER_BC_DIM_MAX;
219 break;
220 case _SC_BC_SCALE_MAX:
221 mib[0] = CTL_USER;
222 mib[1] = USER_BC_SCALE_MAX;
223 break;
224 case _SC_BC_STRING_MAX:
225 mib[0] = CTL_USER;
226 mib[1] = USER_BC_STRING_MAX;
227 break;
228 case _SC_COLL_WEIGHTS_MAX:
229 mib[0] = CTL_USER;
230 mib[1] = USER_COLL_WEIGHTS_MAX;
231 break;
232 case _SC_EXPR_NEST_MAX:
233 mib[0] = CTL_USER;
234 mib[1] = USER_EXPR_NEST_MAX;
235 break;
236 case _SC_LINE_MAX:
237 mib[0] = CTL_USER;
238 mib[1] = USER_LINE_MAX;
239 break;
240 case _SC_RE_DUP_MAX:
241 mib[0] = CTL_USER;
242 mib[1] = USER_RE_DUP_MAX;
243 break;
244 case _SC_2_VERSION:
245 mib[0] = CTL_USER;
246 mib[1] = USER_POSIX2_VERSION;
247 break;
248 case _SC_2_C_BIND:
249 mib[0] = CTL_USER;
250 mib[1] = USER_POSIX2_C_BIND;
251 goto yesno;
252 case _SC_2_C_DEV:
253 mib[0] = CTL_USER;
254 mib[1] = USER_POSIX2_C_DEV;
255 goto yesno;
256 case _SC_2_CHAR_TERM:
257 mib[0] = CTL_USER;
258 mib[1] = USER_POSIX2_CHAR_TERM;
259 goto yesno;
260 case _SC_2_FORT_DEV:
261 mib[0] = CTL_USER;
262 mib[1] = USER_POSIX2_FORT_DEV;
263 goto yesno;
264 case _SC_2_FORT_RUN:
265 mib[0] = CTL_USER;
266 mib[1] = USER_POSIX2_FORT_RUN;
267 goto yesno;
268 case _SC_2_LOCALEDEF:
269 mib[0] = CTL_USER;
270 mib[1] = USER_POSIX2_LOCALEDEF;
271 goto yesno;
272 case _SC_2_SW_DEV:
273 mib[0] = CTL_USER;
274 mib[1] = USER_POSIX2_SW_DEV;
275 goto yesno;
276 case _SC_2_UPE:
277 mib[0] = CTL_USER;
278 mib[1] = USER_POSIX2_UPE;
279 goto yesno;
281 /* XPG 4.2 */
282 case _SC_IOV_MAX:
283 mib[0] = CTL_KERN;
284 mib[1] = KERN_IOV_MAX;
285 break;
286 case _SC_XOPEN_SHM:
287 mib[0] = CTL_KERN;
288 mib[1] = KERN_SYSVIPC;
289 mib[2] = KERN_SYSVIPC_SHM;
290 mib_len = 3;
291 goto yesno;
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))
297 return -1;
298 break;
299 case _SC_AIO_MAX:
300 if (sysctlgetmibinfo("kern.aio_max", &mib[0], &mib_len,
301 NULL, NULL, NULL, SYSCTL_VERSION))
302 return -1;
303 break;
304 case _SC_ASYNCHRONOUS_IO:
305 if (sysctlgetmibinfo("kern.posix_aio", &mib[0], &mib_len,
306 NULL, NULL, NULL, SYSCTL_VERSION))
307 return -1;
308 goto yesno;
309 case _SC_MESSAGE_PASSING:
310 if (sysctlgetmibinfo("kern.posix_msg", &mib[0], &mib_len,
311 NULL, NULL, NULL, SYSCTL_VERSION))
312 return -1;
313 goto yesno;
314 case _SC_MQ_OPEN_MAX:
315 if (sysctlgetmibinfo("kern.mqueue.mq_open_max", &mib[0],
316 &mib_len, NULL, NULL, NULL, SYSCTL_VERSION))
317 return -1;
318 break;
319 case _SC_MQ_PRIO_MAX:
320 if (sysctlgetmibinfo("kern.mqueue.mq_prio_max", &mib[0],
321 &mib_len, NULL, NULL, NULL, SYSCTL_VERSION))
322 return -1;
323 break;
324 case _SC_PRIORITY_SCHEDULING:
325 if (sysctlgetmibinfo("kern.posix_sched", &mib[0], &mib_len,
326 NULL, NULL, NULL, SYSCTL_VERSION))
327 return -1;
328 goto yesno;
329 case _SC_ATEXIT_MAX:
330 mib[0] = CTL_USER;
331 mib[1] = USER_ATEXIT_MAX;
332 break;
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;
340 /* Unsorted */
341 case _SC_HOST_NAME_MAX:
342 return MAXHOSTNAMELEN;
343 case _SC_PASS_MAX:
344 return _PASSWORD_LEN;
345 case _SC_REGEXP:
346 return _POSIX_REGEXP;
347 case _SC_SHELL:
348 return _POSIX_SHELL;
349 case _SC_SYMLOOP_MAX:
350 return MAXSYMLINKS;
352 yesno: if (sysctl(mib, mib_len, &value, &len, NULL, 0) == -1)
353 return (-1);
354 if (value == 0)
355 return (-1);
356 return (value);
358 /* Extensions */
359 case _SC_NPROCESSORS_CONF:
360 mib[0] = CTL_HW;
361 mib[1] = HW_NCPU;
362 break;
363 case _SC_NPROCESSORS_ONLN:
364 mib[0] = CTL_HW;
365 mib[1] = HW_NCPUONLINE;
366 break;
368 /* Native */
369 case _SC_SCHED_RT_TS:
370 if (sysctlgetmibinfo("kern.sched.rtts", &mib[0], &mib_len,
371 NULL, NULL, NULL, SYSCTL_VERSION))
372 return -1;
373 break;
374 case _SC_SCHED_PRI_MIN:
375 if (sysctlgetmibinfo("kern.sched.pri_min", &mib[0], &mib_len,
376 NULL, NULL, NULL, SYSCTL_VERSION))
377 return -1;
378 break;
379 case _SC_SCHED_PRI_MAX:
380 if (sysctlgetmibinfo("kern.sched.pri_max", &mib[0], &mib_len,
381 NULL, NULL, NULL, SYSCTL_VERSION))
382 return -1;
383 break;
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 */
393 return -1;
394 goto yesno;
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:
405 return -1;
406 case _SC_TTY_NAME_MAX:
407 return pathconf(_PATH_DEV, _PC_NAME_MAX);
408 default:
409 errno = EINVAL;
410 return (-1);
412 return (sysctl(mib, mib_len, &value, &len, NULL, 0) == -1 ? -1 : value);