sysconf.c revision 90039
1/*-
2 * Copyright (c) 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Sean Eric Fagan of Cygnus Support.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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.
23 *
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
34 * SUCH DAMAGE.
35 */
36
37#if defined(LIBC_SCCS) && !defined(lint)
38static 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: head/lib/libc/gen/sysconf.c 90039 2002-02-01 00:57:29Z obrien $");
42
43#include <sys/_posix.h>
44#include <sys/param.h>
45#include <sys/time.h>
46#include <sys/sysctl.h>
47#include <sys/resource.h>
48
49#include <errno.h>
50#include <time.h>
51#include <unistd.h>
52
53/*
54 * sysconf --
55 *	get configurable system variables.
56 *
57 * XXX
58 * POSIX 1003.1 (ISO/IEC 9945-1, 4.8.1.3) states that the variable values
59 * not change during the lifetime of the calling process.  This would seem
60 * to require that any change to system limits kill all running processes.
61 * A workaround might be to cache the values when they are first retrieved
62 * and then simply return the cached value on subsequent calls.  This is
63 * less useful than returning up-to-date values, however.
64 */
65long
66sysconf(name)
67	int name;
68{
69	struct rlimit rl;
70	size_t len;
71	int mib[2], value;
72	long defaultresult;
73
74	len = sizeof(value);
75	defaultresult = -1;
76
77	switch (name) {
78/* 1003.1 */
79	case _SC_ARG_MAX:
80		mib[0] = CTL_KERN;
81		mib[1] = KERN_ARGMAX;
82		break;
83	case _SC_CHILD_MAX:
84		return (getrlimit(RLIMIT_NPROC, &rl) ? -1 : rl.rlim_cur);
85	case _SC_CLK_TCK:
86		return (CLK_TCK);
87	case _SC_JOB_CONTROL:
88		mib[0] = CTL_KERN;
89		mib[1] = KERN_JOB_CONTROL;
90		goto yesno;
91	case _SC_NGROUPS_MAX:
92		mib[0] = CTL_KERN;
93		mib[1] = KERN_NGROUPS;
94		break;
95	case _SC_OPEN_MAX:
96		return (getrlimit(RLIMIT_NOFILE, &rl) ? -1 : rl.rlim_cur);
97	case _SC_STREAM_MAX:
98		mib[0] = CTL_USER;
99		mib[1] = USER_STREAM_MAX;
100		break;
101	case _SC_TZNAME_MAX:
102		mib[0] = CTL_USER;
103		mib[1] = USER_TZNAME_MAX;
104		break;
105	case _SC_SAVED_IDS:
106		mib[0] = CTL_KERN;
107		mib[1] = KERN_SAVED_IDS;
108		goto yesno;
109	case _SC_VERSION:
110		mib[0] = CTL_KERN;
111		mib[1] = KERN_POSIX1;
112		break;
113
114/* 1003.2 */
115	case _SC_BC_BASE_MAX:
116		mib[0] = CTL_USER;
117		mib[1] = USER_BC_BASE_MAX;
118		break;
119	case _SC_BC_DIM_MAX:
120		mib[0] = CTL_USER;
121		mib[1] = USER_BC_DIM_MAX;
122		break;
123	case _SC_BC_SCALE_MAX:
124		mib[0] = CTL_USER;
125		mib[1] = USER_BC_SCALE_MAX;
126		break;
127	case _SC_BC_STRING_MAX:
128		mib[0] = CTL_USER;
129		mib[1] = USER_BC_STRING_MAX;
130		break;
131	case _SC_COLL_WEIGHTS_MAX:
132		mib[0] = CTL_USER;
133		mib[1] = USER_COLL_WEIGHTS_MAX;
134		break;
135	case _SC_EXPR_NEST_MAX:
136		mib[0] = CTL_USER;
137		mib[1] = USER_EXPR_NEST_MAX;
138		break;
139	case _SC_LINE_MAX:
140		mib[0] = CTL_USER;
141		mib[1] = USER_LINE_MAX;
142		break;
143	case _SC_RE_DUP_MAX:
144		mib[0] = CTL_USER;
145		mib[1] = USER_RE_DUP_MAX;
146		break;
147	case _SC_2_VERSION:
148		mib[0] = CTL_USER;
149		mib[1] = USER_POSIX2_VERSION;
150		break;
151	case _SC_2_C_BIND:
152		mib[0] = CTL_USER;
153		mib[1] = USER_POSIX2_C_BIND;
154		goto yesno;
155	case _SC_2_C_DEV:
156		mib[0] = CTL_USER;
157		mib[1] = USER_POSIX2_C_DEV;
158		goto yesno;
159	case _SC_2_CHAR_TERM:
160		mib[0] = CTL_USER;
161		mib[1] = USER_POSIX2_CHAR_TERM;
162		goto yesno;
163	case _SC_2_FORT_DEV:
164		mib[0] = CTL_USER;
165		mib[1] = USER_POSIX2_FORT_DEV;
166		goto yesno;
167	case _SC_2_FORT_RUN:
168		mib[0] = CTL_USER;
169		mib[1] = USER_POSIX2_FORT_RUN;
170		goto yesno;
171	case _SC_2_LOCALEDEF:
172		mib[0] = CTL_USER;
173		mib[1] = USER_POSIX2_LOCALEDEF;
174		goto yesno;
175	case _SC_2_SW_DEV:
176		mib[0] = CTL_USER;
177		mib[1] = USER_POSIX2_SW_DEV;
178		goto yesno;
179	case _SC_2_UPE:
180		mib[0] = CTL_USER;
181		mib[1] = USER_POSIX2_UPE;
182		goto yesno;
183
184#ifdef _P1003_1B_VISIBLE
185	/* POSIX.1B */
186
187	case _SC_ASYNCHRONOUS_IO:
188		mib[0] = CTL_P1003_1B;
189		mib[1] = CTL_P1003_1B_ASYNCHRONOUS_IO;
190		goto yesno;
191	case _SC_MAPPED_FILES:
192		mib[0] = CTL_P1003_1B;
193		mib[1] = CTL_P1003_1B_MAPPED_FILES;
194		goto yesno;
195	case _SC_MEMLOCK:
196		mib[0] = CTL_P1003_1B;
197		mib[1] = CTL_P1003_1B_MEMLOCK;
198		goto yesno;
199	case _SC_MEMLOCK_RANGE:
200		mib[0] = CTL_P1003_1B;
201		mib[1] = CTL_P1003_1B_MEMLOCK_RANGE;
202		goto yesno;
203	case _SC_MEMORY_PROTECTION:
204		mib[0] = CTL_P1003_1B;
205		mib[1] = CTL_P1003_1B_MEMORY_PROTECTION;
206		goto yesno;
207	case _SC_MESSAGE_PASSING:
208		mib[0] = CTL_P1003_1B;
209		mib[1] = CTL_P1003_1B_MESSAGE_PASSING;
210		goto yesno;
211	case _SC_PRIORITIZED_IO:
212		mib[0] = CTL_P1003_1B;
213		mib[1] = CTL_P1003_1B_PRIORITIZED_IO;
214		goto yesno;
215	case _SC_PRIORITY_SCHEDULING:
216		mib[0] = CTL_P1003_1B;
217		mib[1] = CTL_P1003_1B_PRIORITY_SCHEDULING;
218		goto yesno;
219	case _SC_REALTIME_SIGNALS:
220		mib[0] = CTL_P1003_1B;
221		mib[1] = CTL_P1003_1B_REALTIME_SIGNALS;
222		goto yesno;
223	case _SC_SEMAPHORES:
224		mib[0] = CTL_P1003_1B;
225		mib[1] = CTL_P1003_1B_SEMAPHORES;
226		goto yesno;
227	case _SC_FSYNC:
228		mib[0] = CTL_P1003_1B;
229		mib[1] = CTL_P1003_1B_FSYNC;
230		goto yesno;
231	case _SC_SHARED_MEMORY_OBJECTS:
232		mib[0] = CTL_P1003_1B;
233		mib[1] = CTL_P1003_1B_SHARED_MEMORY_OBJECTS;
234		goto yesno;
235	case _SC_SYNCHRONIZED_IO:
236		mib[0] = CTL_P1003_1B;
237		mib[1] = CTL_P1003_1B_SYNCHRONIZED_IO;
238		goto yesno;
239	case _SC_TIMERS:
240		mib[0] = CTL_P1003_1B;
241		mib[1] = CTL_P1003_1B_TIMERS;
242		goto yesno;
243	case _SC_AIO_LISTIO_MAX:
244		mib[0] = CTL_P1003_1B;
245		mib[1] = CTL_P1003_1B_AIO_LISTIO_MAX;
246		goto yesno;
247	case _SC_AIO_MAX:
248		mib[0] = CTL_P1003_1B;
249		mib[1] = CTL_P1003_1B_AIO_MAX;
250		goto yesno;
251	case _SC_AIO_PRIO_DELTA_MAX:
252		mib[0] = CTL_P1003_1B;
253		mib[1] = CTL_P1003_1B_AIO_PRIO_DELTA_MAX;
254		goto yesno;
255	case _SC_DELAYTIMER_MAX:
256		mib[0] = CTL_P1003_1B;
257		mib[1] = CTL_P1003_1B_DELAYTIMER_MAX;
258		goto yesno;
259	case _SC_MQ_OPEN_MAX:
260		mib[0] = CTL_P1003_1B;
261		mib[1] = CTL_P1003_1B_MQ_OPEN_MAX;
262		goto yesno;
263	case _SC_PAGESIZE:
264		defaultresult = getpagesize();
265		mib[0] = CTL_P1003_1B;
266		mib[1] = CTL_P1003_1B_PAGESIZE;
267		goto yesno;
268	case _SC_RTSIG_MAX:
269		mib[0] = CTL_P1003_1B;
270		mib[1] = CTL_P1003_1B_RTSIG_MAX;
271		goto yesno;
272	case _SC_SEM_NSEMS_MAX:
273		mib[0] = CTL_P1003_1B;
274		mib[1] = CTL_P1003_1B_SEM_NSEMS_MAX;
275		goto yesno;
276	case _SC_SEM_VALUE_MAX:
277		mib[0] = CTL_P1003_1B;
278		mib[1] = CTL_P1003_1B_SEM_VALUE_MAX;
279		goto yesno;
280	case _SC_SIGQUEUE_MAX:
281		mib[0] = CTL_P1003_1B;
282		mib[1] = CTL_P1003_1B_SIGQUEUE_MAX;
283		goto yesno;
284	case _SC_TIMER_MAX:
285		mib[0] = CTL_P1003_1B;
286		mib[1] = CTL_P1003_1B_TIMER_MAX;
287		goto yesno;
288#endif /* _P1003_1B_VISIBLE */
289
290#if defined(_SC_NPROCESSORS_CONF) && defined(_SC_NPROCESSORS_ONLN)
291	case _SC_NPROCESSORS_CONF:
292	case _SC_NPROCESSORS_ONLN:
293		mib[0] = CTL_HW;
294		mib[1] = HW_NCPU;
295		break;
296#endif
297
298#ifdef _SC_IOV_MAX
299	case _SC_IOV_MAX:
300		mib[0] = CTL_KERN;
301		mib[1] = KERN_IOV_MAX;
302		break;
303#endif
304
305yesno:		if (sysctl(mib, 2, &value, &len, NULL, 0) == -1)
306			return (-1);
307		if (value == 0)
308			return (defaultresult);
309		return (value);
310		break;
311	default:
312		errno = EINVAL;
313		return (-1);
314	}
315	return (sysctl(mib, 2, &value, &len, NULL, 0) == -1 ? -1 : value);
316}
317