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