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