xref: /netbsd-src/lib/libc/gen/sysconf.c (revision 21e37cc72a480a47828990a439cde7ac9ffaf0c6)
1 /*	$NetBSD: sysconf.c,v 1.19 2003/08/07 16:42:57 agc Exp $	*/
2 
3 /*-
4  * Copyright (c) 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Sean Eric Fagan of Cygnus Support.
9  *
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.
21  *
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.
33  */
34 
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.19 2003/08/07 16:42:57 agc Exp $");
41 #endif
42 #endif /* LIBC_SCCS and not lint */
43 
44 #include "namespace.h"
45 #include <sys/param.h>
46 #include <sys/sysctl.h>
47 #include <sys/time.h>
48 #include <sys/resource.h>
49 
50 #include <errno.h>
51 #include <time.h>
52 #include <unistd.h>
53 
54 #ifdef __weak_alias
55 __weak_alias(sysconf,__sysconf)
56 #endif
57 
58 /*
59  * sysconf --
60  *	get configurable system variables.
61  *
62  * XXX
63  * POSIX 1003.1 (ISO/IEC 9945-1, 4.8.1.3) states that the variable values
64  * not change during the lifetime of the calling process.  This would seem
65  * to require that any change to system limits kill all running processes.
66  * A workaround might be to cache the values when they are first retrieved
67  * and then simply return the cached value on subsequent calls.  This is
68  * less useful than returning up-to-date values, however.
69  */
70 long
71 sysconf(name)
72 	int name;
73 {
74 	struct rlimit rl;
75 	size_t len;
76 	int mib[2], value;
77 	struct clockinfo tmpclock;
78 	static int clk_tck;
79 
80 	len = sizeof(value);
81 
82 	switch (name) {
83 /* 1003.1 */
84 	case _SC_ARG_MAX:
85 		mib[0] = CTL_KERN;
86 		mib[1] = KERN_ARGMAX;
87 		break;
88 	case _SC_CHILD_MAX:
89 		return (getrlimit(RLIMIT_NPROC, &rl) ? -1 : (long)rl.rlim_cur);
90 	case _O_SC_CLK_TCK:
91 		/*
92 		 * For applications compiled when CLK_TCK was a compile-time
93 		 * constant.
94 		 */
95 		return 100;
96 	case _SC_CLK_TCK:
97 		/*
98 		 * Has to be handled specially because it returns a
99 		 * struct clockinfo instead of an integer. Also, since
100 		 * this might be called often by some things that
101 		 * don't grok CLK_TCK can be a macro expanding to a
102 		 * function, cache the value.
103 		 */
104 		if (clk_tck == 0) {
105 			mib[0] = CTL_KERN;
106 			mib[1] = KERN_CLOCKRATE;
107 			len = sizeof(struct clockinfo);
108 			clk_tck = sysctl(mib, 2, &tmpclock, &len, NULL, 0)
109 			    == -1 ? -1 : tmpclock.hz;
110 		}
111 		return(clk_tck);
112 	case _SC_JOB_CONTROL:
113 		mib[0] = CTL_KERN;
114 		mib[1] = KERN_JOB_CONTROL;
115 		goto yesno;
116 	case _SC_NGROUPS_MAX:
117 		mib[0] = CTL_KERN;
118 		mib[1] = KERN_NGROUPS;
119 		break;
120 	case _SC_OPEN_MAX:
121 		return (getrlimit(RLIMIT_NOFILE, &rl) ? -1 : (long)rl.rlim_cur);
122 	case _SC_STREAM_MAX:
123 		mib[0] = CTL_USER;
124 		mib[1] = USER_STREAM_MAX;
125 		break;
126 	case _SC_TZNAME_MAX:
127 		mib[0] = CTL_USER;
128 		mib[1] = USER_TZNAME_MAX;
129 		break;
130 	case _SC_SAVED_IDS:
131 		mib[0] = CTL_KERN;
132 		mib[1] = KERN_SAVED_IDS;
133 		goto yesno;
134 	case _SC_VERSION:
135 		mib[0] = CTL_KERN;
136 		mib[1] = KERN_POSIX1;
137 		break;
138 
139 /* 1003.1b */
140 	case _SC_PAGESIZE:
141 		mib[0] = CTL_HW;
142 		mib[1] = HW_PAGESIZE;
143 		break;
144 	case _SC_FSYNC:
145 		mib[0] = CTL_KERN;
146 		mib[1] = KERN_FSYNC;
147 		goto yesno;
148 	case _SC_SYNCHRONIZED_IO:
149 		mib[0] = CTL_KERN;
150 		mib[1] = KERN_SYNCHRONIZED_IO;
151 		goto yesno;
152 	case _SC_MAPPED_FILES:
153 		mib[0] = CTL_KERN;
154 		mib[1] = KERN_MAPPED_FILES;
155 		goto yesno;
156 	case _SC_MEMLOCK:
157 		mib[0] = CTL_KERN;
158 		mib[1] = KERN_MEMLOCK;
159 		goto yesno;
160 	case _SC_MEMLOCK_RANGE:
161 		mib[0] = CTL_KERN;
162 		mib[1] = KERN_MEMLOCK_RANGE;
163 		goto yesno;
164 	case _SC_MEMORY_PROTECTION:
165 		mib[0] = CTL_KERN;
166 		mib[1] = KERN_MEMORY_PROTECTION;
167 		goto yesno;
168 	case _SC_MONOTONIC_CLOCK:
169 		mib[0] = CTL_KERN;
170 		mib[1] = KERN_MONOTONIC_CLOCK;
171 		goto yesno;
172 	case _SC_SEMAPHORES:
173 		mib[0] = CTL_KERN;
174 		mib[1] = KERN_POSIX_SEMAPHORES;
175 		goto yesno;
176 	case _SC_TIMERS:
177 		mib[0] = CTL_KERN;
178 		mib[1] = KERN_POSIX_TIMERS;
179 		goto yesno;
180 
181 /* 1003.1c */
182 	case _SC_LOGIN_NAME_MAX:
183 		mib[0] = CTL_KERN;
184 		mib[1] = KERN_LOGIN_NAME_MAX;
185 		break;
186 	case _SC_THREADS:
187 		mib[0] = CTL_KERN;
188 		mib[1] = KERN_POSIX_THREADS;
189 		goto yesno;
190 
191 /* 1003.1j */
192 	case _SC_BARRIERS:
193 		mib[0] = CTL_KERN;
194 		mib[1] = KERN_POSIX_BARRIERS;
195 		goto yesno;
196 	case _SC_SPIN_LOCKS:
197 		mib[0] = CTL_KERN;
198 		mib[1] = KERN_POSIX_SPIN_LOCKS;
199 		goto yesno;
200 	/* Historical; Threads option in 1003.1-2001 */
201 	case _SC_READER_WRITER_LOCKS:
202 		mib[0] = CTL_KERN;
203 		mib[1] = KERN_POSIX_READER_WRITER_LOCKS;
204 		goto yesno;
205 
206 /* 1003.2 */
207 	case _SC_BC_BASE_MAX:
208 		mib[0] = CTL_USER;
209 		mib[1] = USER_BC_BASE_MAX;
210 		break;
211 	case _SC_BC_DIM_MAX:
212 		mib[0] = CTL_USER;
213 		mib[1] = USER_BC_DIM_MAX;
214 		break;
215 	case _SC_BC_SCALE_MAX:
216 		mib[0] = CTL_USER;
217 		mib[1] = USER_BC_SCALE_MAX;
218 		break;
219 	case _SC_BC_STRING_MAX:
220 		mib[0] = CTL_USER;
221 		mib[1] = USER_BC_STRING_MAX;
222 		break;
223 	case _SC_COLL_WEIGHTS_MAX:
224 		mib[0] = CTL_USER;
225 		mib[1] = USER_COLL_WEIGHTS_MAX;
226 		break;
227 	case _SC_EXPR_NEST_MAX:
228 		mib[0] = CTL_USER;
229 		mib[1] = USER_EXPR_NEST_MAX;
230 		break;
231 	case _SC_LINE_MAX:
232 		mib[0] = CTL_USER;
233 		mib[1] = USER_LINE_MAX;
234 		break;
235 	case _SC_RE_DUP_MAX:
236 		mib[0] = CTL_USER;
237 		mib[1] = USER_RE_DUP_MAX;
238 		break;
239 	case _SC_2_VERSION:
240 		mib[0] = CTL_USER;
241 		mib[1] = USER_POSIX2_VERSION;
242 		break;
243 	case _SC_2_C_BIND:
244 		mib[0] = CTL_USER;
245 		mib[1] = USER_POSIX2_C_BIND;
246 		goto yesno;
247 	case _SC_2_C_DEV:
248 		mib[0] = CTL_USER;
249 		mib[1] = USER_POSIX2_C_DEV;
250 		goto yesno;
251 	case _SC_2_CHAR_TERM:
252 		mib[0] = CTL_USER;
253 		mib[1] = USER_POSIX2_CHAR_TERM;
254 		goto yesno;
255 	case _SC_2_FORT_DEV:
256 		mib[0] = CTL_USER;
257 		mib[1] = USER_POSIX2_FORT_DEV;
258 		goto yesno;
259 	case _SC_2_FORT_RUN:
260 		mib[0] = CTL_USER;
261 		mib[1] = USER_POSIX2_FORT_RUN;
262 		goto yesno;
263 	case _SC_2_LOCALEDEF:
264 		mib[0] = CTL_USER;
265 		mib[1] = USER_POSIX2_LOCALEDEF;
266 		goto yesno;
267 	case _SC_2_SW_DEV:
268 		mib[0] = CTL_USER;
269 		mib[1] = USER_POSIX2_SW_DEV;
270 		goto yesno;
271 	case _SC_2_UPE:
272 		mib[0] = CTL_USER;
273 		mib[1] = USER_POSIX2_UPE;
274 		goto yesno;
275 
276 /* XPG 4.2 */
277 	case _SC_IOV_MAX:
278 		mib[0] = CTL_KERN;
279 		mib[1] = KERN_IOV_MAX;
280 		break;
281 	case _SC_XOPEN_SHM:
282 		mib[0] = CTL_KERN;
283 		mib[1] = KERN_SYSVSHM;
284 		goto yesno;
285 
286 /* 1003.1-2001, XSI Option Group */
287 	case _SC_ATEXIT_MAX:
288 		mib[0] = CTL_USER;
289 		mib[1] = USER_ATEXIT_MAX;
290 		break;
291 
292 yesno:		if (sysctl(mib, 2, &value, &len, NULL, 0) == -1)
293 			return (-1);
294 		if (value == 0)
295 			return (-1);
296 		return (value);
297 		/*NOTREACHED*/
298 		break;
299 	default:
300 		errno = EINVAL;
301 		return (-1);
302 	}
303 	return (sysctl(mib, 2, &value, &len, NULL, 0) == -1 ? -1 : value);
304 }
305