sysconf.c revision 1.20 1 /* $NetBSD: sysconf.c,v 1.20 2004/11/10 04:02:52 lukem 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.20 2004/11/10 04:02:52 lukem 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
84 /* 1003.1 */
85 case _SC_ARG_MAX:
86 mib[0] = CTL_KERN;
87 mib[1] = KERN_ARGMAX;
88 break;
89 case _SC_CHILD_MAX:
90 return (getrlimit(RLIMIT_NPROC, &rl) ? -1 : (long)rl.rlim_cur);
91 case _O_SC_CLK_TCK:
92 /*
93 * For applications compiled when CLK_TCK was a compile-time
94 * constant.
95 */
96 return 100;
97 case _SC_CLK_TCK:
98 /*
99 * Has to be handled specially because it returns a
100 * struct clockinfo instead of an integer. Also, since
101 * this might be called often by some things that
102 * don't grok CLK_TCK can be a macro expanding to a
103 * function, cache the value.
104 */
105 if (clk_tck == 0) {
106 mib[0] = CTL_KERN;
107 mib[1] = KERN_CLOCKRATE;
108 len = sizeof(struct clockinfo);
109 clk_tck = sysctl(mib, 2, &tmpclock, &len, NULL, 0)
110 == -1 ? -1 : tmpclock.hz;
111 }
112 return(clk_tck);
113 case _SC_JOB_CONTROL:
114 mib[0] = CTL_KERN;
115 mib[1] = KERN_JOB_CONTROL;
116 goto yesno;
117 case _SC_NGROUPS_MAX:
118 mib[0] = CTL_KERN;
119 mib[1] = KERN_NGROUPS;
120 break;
121 case _SC_OPEN_MAX:
122 return (getrlimit(RLIMIT_NOFILE, &rl) ? -1 : (long)rl.rlim_cur);
123 case _SC_STREAM_MAX:
124 mib[0] = CTL_USER;
125 mib[1] = USER_STREAM_MAX;
126 break;
127 case _SC_TZNAME_MAX:
128 mib[0] = CTL_USER;
129 mib[1] = USER_TZNAME_MAX;
130 break;
131 case _SC_SAVED_IDS:
132 mib[0] = CTL_KERN;
133 mib[1] = KERN_SAVED_IDS;
134 goto yesno;
135 case _SC_VERSION:
136 mib[0] = CTL_KERN;
137 mib[1] = KERN_POSIX1;
138 break;
139
140 /* 1003.1b */
141 case _SC_PAGESIZE:
142 mib[0] = CTL_HW;
143 mib[1] = HW_PAGESIZE;
144 break;
145 case _SC_FSYNC:
146 mib[0] = CTL_KERN;
147 mib[1] = KERN_FSYNC;
148 goto yesno;
149 case _SC_SYNCHRONIZED_IO:
150 mib[0] = CTL_KERN;
151 mib[1] = KERN_SYNCHRONIZED_IO;
152 goto yesno;
153 case _SC_MAPPED_FILES:
154 mib[0] = CTL_KERN;
155 mib[1] = KERN_MAPPED_FILES;
156 goto yesno;
157 case _SC_MEMLOCK:
158 mib[0] = CTL_KERN;
159 mib[1] = KERN_MEMLOCK;
160 goto yesno;
161 case _SC_MEMLOCK_RANGE:
162 mib[0] = CTL_KERN;
163 mib[1] = KERN_MEMLOCK_RANGE;
164 goto yesno;
165 case _SC_MEMORY_PROTECTION:
166 mib[0] = CTL_KERN;
167 mib[1] = KERN_MEMORY_PROTECTION;
168 goto yesno;
169 case _SC_MONOTONIC_CLOCK:
170 mib[0] = CTL_KERN;
171 mib[1] = KERN_MONOTONIC_CLOCK;
172 goto yesno;
173 case _SC_SEMAPHORES:
174 mib[0] = CTL_KERN;
175 mib[1] = KERN_POSIX_SEMAPHORES;
176 goto yesno;
177 case _SC_TIMERS:
178 mib[0] = CTL_KERN;
179 mib[1] = KERN_POSIX_TIMERS;
180 goto yesno;
181
182 /* 1003.1c */
183 case _SC_LOGIN_NAME_MAX:
184 mib[0] = CTL_KERN;
185 mib[1] = KERN_LOGIN_NAME_MAX;
186 break;
187 case _SC_THREADS:
188 mib[0] = CTL_KERN;
189 mib[1] = KERN_POSIX_THREADS;
190 goto yesno;
191
192 /* 1003.1j */
193 case _SC_BARRIERS:
194 mib[0] = CTL_KERN;
195 mib[1] = KERN_POSIX_BARRIERS;
196 goto yesno;
197 case _SC_SPIN_LOCKS:
198 mib[0] = CTL_KERN;
199 mib[1] = KERN_POSIX_SPIN_LOCKS;
200 goto yesno;
201 /* Historical; Threads option in 1003.1-2001 */
202 case _SC_READER_WRITER_LOCKS:
203 mib[0] = CTL_KERN;
204 mib[1] = KERN_POSIX_READER_WRITER_LOCKS;
205 goto yesno;
206
207 /* 1003.2 */
208 case _SC_BC_BASE_MAX:
209 mib[0] = CTL_USER;
210 mib[1] = USER_BC_BASE_MAX;
211 break;
212 case _SC_BC_DIM_MAX:
213 mib[0] = CTL_USER;
214 mib[1] = USER_BC_DIM_MAX;
215 break;
216 case _SC_BC_SCALE_MAX:
217 mib[0] = CTL_USER;
218 mib[1] = USER_BC_SCALE_MAX;
219 break;
220 case _SC_BC_STRING_MAX:
221 mib[0] = CTL_USER;
222 mib[1] = USER_BC_STRING_MAX;
223 break;
224 case _SC_COLL_WEIGHTS_MAX:
225 mib[0] = CTL_USER;
226 mib[1] = USER_COLL_WEIGHTS_MAX;
227 break;
228 case _SC_EXPR_NEST_MAX:
229 mib[0] = CTL_USER;
230 mib[1] = USER_EXPR_NEST_MAX;
231 break;
232 case _SC_LINE_MAX:
233 mib[0] = CTL_USER;
234 mib[1] = USER_LINE_MAX;
235 break;
236 case _SC_RE_DUP_MAX:
237 mib[0] = CTL_USER;
238 mib[1] = USER_RE_DUP_MAX;
239 break;
240 case _SC_2_VERSION:
241 mib[0] = CTL_USER;
242 mib[1] = USER_POSIX2_VERSION;
243 break;
244 case _SC_2_C_BIND:
245 mib[0] = CTL_USER;
246 mib[1] = USER_POSIX2_C_BIND;
247 goto yesno;
248 case _SC_2_C_DEV:
249 mib[0] = CTL_USER;
250 mib[1] = USER_POSIX2_C_DEV;
251 goto yesno;
252 case _SC_2_CHAR_TERM:
253 mib[0] = CTL_USER;
254 mib[1] = USER_POSIX2_CHAR_TERM;
255 goto yesno;
256 case _SC_2_FORT_DEV:
257 mib[0] = CTL_USER;
258 mib[1] = USER_POSIX2_FORT_DEV;
259 goto yesno;
260 case _SC_2_FORT_RUN:
261 mib[0] = CTL_USER;
262 mib[1] = USER_POSIX2_FORT_RUN;
263 goto yesno;
264 case _SC_2_LOCALEDEF:
265 mib[0] = CTL_USER;
266 mib[1] = USER_POSIX2_LOCALEDEF;
267 goto yesno;
268 case _SC_2_SW_DEV:
269 mib[0] = CTL_USER;
270 mib[1] = USER_POSIX2_SW_DEV;
271 goto yesno;
272 case _SC_2_UPE:
273 mib[0] = CTL_USER;
274 mib[1] = USER_POSIX2_UPE;
275 goto yesno;
276
277 /* XPG 4.2 */
278 case _SC_IOV_MAX:
279 mib[0] = CTL_KERN;
280 mib[1] = KERN_IOV_MAX;
281 break;
282 case _SC_XOPEN_SHM:
283 mib[0] = CTL_KERN;
284 mib[1] = KERN_SYSVSHM;
285 goto yesno;
286
287 /* 1003.1-2001, XSI Option Group */
288 case _SC_ATEXIT_MAX:
289 mib[0] = CTL_USER;
290 mib[1] = USER_ATEXIT_MAX;
291 break;
292
293 /* 1003.1-2001, TSF */
294 case _SC_GETGR_R_SIZE_MAX:
295 return _GETGR_R_SIZE_MAX;
296 case _SC_GETPW_R_SIZE_MAX:
297 return _GETPW_R_SIZE_MAX;
298
299 yesno: if (sysctl(mib, 2, &value, &len, NULL, 0) == -1)
300 return (-1);
301 if (value == 0)
302 return (-1);
303 return (value);
304 /*NOTREACHED*/
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