1 /* $NetBSD: sysconf.c,v 1.25 2007/10/15 14:12:56 ad 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.25 2007/10/15 14:12:56 ad 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(int name) 73 { 74 struct rlimit rl; 75 size_t len; 76 int mib[CTL_MAXNAME], value; 77 unsigned int mib_len; 78 struct clockinfo tmpclock; 79 static int clk_tck; 80 81 len = sizeof(value); 82 83 /* Default length of the MIB */ 84 mib_len = 2; 85 86 switch (name) { 87 88 /* 1003.1 */ 89 case _SC_ARG_MAX: 90 mib[0] = CTL_KERN; 91 mib[1] = KERN_ARGMAX; 92 break; 93 case _SC_CHILD_MAX: 94 return (getrlimit(RLIMIT_NPROC, &rl) ? -1 : (long)rl.rlim_cur); 95 case _O_SC_CLK_TCK: 96 /* 97 * For applications compiled when CLK_TCK was a compile-time 98 * constant. 99 */ 100 return 100; 101 case _SC_CLK_TCK: 102 /* 103 * Has to be handled specially because it returns a 104 * struct clockinfo instead of an integer. Also, since 105 * this might be called often by some things that 106 * don't grok CLK_TCK can be a macro expanding to a 107 * function, cache the value. 108 */ 109 if (clk_tck == 0) { 110 mib[0] = CTL_KERN; 111 mib[1] = KERN_CLOCKRATE; 112 len = sizeof(struct clockinfo); 113 clk_tck = sysctl(mib, 2, &tmpclock, &len, NULL, 0) 114 == -1 ? -1 : tmpclock.hz; 115 } 116 return(clk_tck); 117 case _SC_JOB_CONTROL: 118 mib[0] = CTL_KERN; 119 mib[1] = KERN_JOB_CONTROL; 120 goto yesno; 121 case _SC_NGROUPS_MAX: 122 mib[0] = CTL_KERN; 123 mib[1] = KERN_NGROUPS; 124 break; 125 case _SC_OPEN_MAX: 126 return (getrlimit(RLIMIT_NOFILE, &rl) ? -1 : (long)rl.rlim_cur); 127 case _SC_STREAM_MAX: 128 mib[0] = CTL_USER; 129 mib[1] = USER_STREAM_MAX; 130 break; 131 case _SC_TZNAME_MAX: 132 mib[0] = CTL_USER; 133 mib[1] = USER_TZNAME_MAX; 134 break; 135 case _SC_SAVED_IDS: 136 mib[0] = CTL_KERN; 137 mib[1] = KERN_SAVED_IDS; 138 goto yesno; 139 case _SC_VERSION: 140 mib[0] = CTL_KERN; 141 mib[1] = KERN_POSIX1; 142 break; 143 144 /* 1003.1b */ 145 case _SC_PAGESIZE: 146 mib[0] = CTL_HW; 147 mib[1] = HW_PAGESIZE; 148 break; 149 case _SC_FSYNC: 150 mib[0] = CTL_KERN; 151 mib[1] = KERN_FSYNC; 152 goto yesno; 153 case _SC_SYNCHRONIZED_IO: 154 mib[0] = CTL_KERN; 155 mib[1] = KERN_SYNCHRONIZED_IO; 156 goto yesno; 157 case _SC_MAPPED_FILES: 158 mib[0] = CTL_KERN; 159 mib[1] = KERN_MAPPED_FILES; 160 goto yesno; 161 case _SC_MEMLOCK: 162 mib[0] = CTL_KERN; 163 mib[1] = KERN_MEMLOCK; 164 goto yesno; 165 case _SC_MEMLOCK_RANGE: 166 mib[0] = CTL_KERN; 167 mib[1] = KERN_MEMLOCK_RANGE; 168 goto yesno; 169 case _SC_MEMORY_PROTECTION: 170 mib[0] = CTL_KERN; 171 mib[1] = KERN_MEMORY_PROTECTION; 172 goto yesno; 173 case _SC_MONOTONIC_CLOCK: 174 mib[0] = CTL_KERN; 175 mib[1] = KERN_MONOTONIC_CLOCK; 176 goto yesno; 177 case _SC_SEMAPHORES: 178 mib[0] = CTL_KERN; 179 mib[1] = KERN_POSIX_SEMAPHORES; 180 goto yesno; 181 case _SC_TIMERS: 182 mib[0] = CTL_KERN; 183 mib[1] = KERN_POSIX_TIMERS; 184 goto yesno; 185 186 /* 1003.1c */ 187 case _SC_LOGIN_NAME_MAX: 188 mib[0] = CTL_KERN; 189 mib[1] = KERN_LOGIN_NAME_MAX; 190 break; 191 case _SC_THREADS: 192 mib[0] = CTL_KERN; 193 mib[1] = KERN_POSIX_THREADS; 194 goto yesno; 195 196 /* 1003.1j */ 197 case _SC_BARRIERS: 198 mib[0] = CTL_KERN; 199 mib[1] = KERN_POSIX_BARRIERS; 200 goto yesno; 201 case _SC_SPIN_LOCKS: 202 mib[0] = CTL_KERN; 203 mib[1] = KERN_POSIX_SPIN_LOCKS; 204 goto yesno; 205 /* Historical; Threads option in 1003.1-2001 */ 206 case _SC_READER_WRITER_LOCKS: 207 mib[0] = CTL_KERN; 208 mib[1] = KERN_POSIX_READER_WRITER_LOCKS; 209 goto yesno; 210 211 /* 1003.2 */ 212 case _SC_BC_BASE_MAX: 213 mib[0] = CTL_USER; 214 mib[1] = USER_BC_BASE_MAX; 215 break; 216 case _SC_BC_DIM_MAX: 217 mib[0] = CTL_USER; 218 mib[1] = USER_BC_DIM_MAX; 219 break; 220 case _SC_BC_SCALE_MAX: 221 mib[0] = CTL_USER; 222 mib[1] = USER_BC_SCALE_MAX; 223 break; 224 case _SC_BC_STRING_MAX: 225 mib[0] = CTL_USER; 226 mib[1] = USER_BC_STRING_MAX; 227 break; 228 case _SC_COLL_WEIGHTS_MAX: 229 mib[0] = CTL_USER; 230 mib[1] = USER_COLL_WEIGHTS_MAX; 231 break; 232 case _SC_EXPR_NEST_MAX: 233 mib[0] = CTL_USER; 234 mib[1] = USER_EXPR_NEST_MAX; 235 break; 236 case _SC_LINE_MAX: 237 mib[0] = CTL_USER; 238 mib[1] = USER_LINE_MAX; 239 break; 240 case _SC_RE_DUP_MAX: 241 mib[0] = CTL_USER; 242 mib[1] = USER_RE_DUP_MAX; 243 break; 244 case _SC_2_VERSION: 245 mib[0] = CTL_USER; 246 mib[1] = USER_POSIX2_VERSION; 247 break; 248 case _SC_2_C_BIND: 249 mib[0] = CTL_USER; 250 mib[1] = USER_POSIX2_C_BIND; 251 goto yesno; 252 case _SC_2_C_DEV: 253 mib[0] = CTL_USER; 254 mib[1] = USER_POSIX2_C_DEV; 255 goto yesno; 256 case _SC_2_CHAR_TERM: 257 mib[0] = CTL_USER; 258 mib[1] = USER_POSIX2_CHAR_TERM; 259 goto yesno; 260 case _SC_2_FORT_DEV: 261 mib[0] = CTL_USER; 262 mib[1] = USER_POSIX2_FORT_DEV; 263 goto yesno; 264 case _SC_2_FORT_RUN: 265 mib[0] = CTL_USER; 266 mib[1] = USER_POSIX2_FORT_RUN; 267 goto yesno; 268 case _SC_2_LOCALEDEF: 269 mib[0] = CTL_USER; 270 mib[1] = USER_POSIX2_LOCALEDEF; 271 goto yesno; 272 case _SC_2_SW_DEV: 273 mib[0] = CTL_USER; 274 mib[1] = USER_POSIX2_SW_DEV; 275 goto yesno; 276 case _SC_2_UPE: 277 mib[0] = CTL_USER; 278 mib[1] = USER_POSIX2_UPE; 279 goto yesno; 280 281 /* XPG 4.2 */ 282 case _SC_IOV_MAX: 283 mib[0] = CTL_KERN; 284 mib[1] = KERN_IOV_MAX; 285 break; 286 case _SC_XOPEN_SHM: 287 mib[0] = CTL_KERN; 288 mib[1] = KERN_SYSVIPC; 289 mib[2] = KERN_SYSVIPC_SHM; 290 mib_len = 3; 291 goto yesno; 292 293 /* 1003.1-2001, XSI Option Group */ 294 case _SC_AIO_LISTIO_MAX: 295 if (sysctlgetmibinfo("kern.aio_listio_max", &mib[0], &mib_len, 296 NULL, NULL, NULL, SYSCTL_VERSION)) 297 return -1; 298 break; 299 case _SC_AIO_MAX: 300 if (sysctlgetmibinfo("kern.aio_max", &mib[0], &mib_len, 301 NULL, NULL, NULL, SYSCTL_VERSION)) 302 return -1; 303 break; 304 case _SC_ASYNCHRONOUS_IO: 305 if (sysctlgetmibinfo("kern.posix_aio", &mib[0], &mib_len, 306 NULL, NULL, NULL, SYSCTL_VERSION)) 307 return -1; 308 goto yesno; 309 case _SC_MESSAGE_PASSING: 310 if (sysctlgetmibinfo("kern.posix_msg", &mib[0], &mib_len, 311 NULL, NULL, NULL, SYSCTL_VERSION)) 312 return -1; 313 goto yesno; 314 case _SC_MQ_OPEN_MAX: 315 if (sysctlgetmibinfo("kern.mq_open_max", &mib[0], &mib_len, 316 NULL, NULL, NULL, SYSCTL_VERSION)) 317 return -1; 318 break; 319 case _SC_MQ_PRIO_MAX: 320 if (sysctlgetmibinfo("kern.mq_prio_max", &mib[0], &mib_len, 321 NULL, NULL, NULL, SYSCTL_VERSION)) 322 return -1; 323 break; 324 case _SC_ATEXIT_MAX: 325 mib[0] = CTL_USER; 326 mib[1] = USER_ATEXIT_MAX; 327 break; 328 329 /* 1003.1-2001, TSF */ 330 case _SC_GETGR_R_SIZE_MAX: 331 return _GETGR_R_SIZE_MAX; 332 case _SC_GETPW_R_SIZE_MAX: 333 return _GETPW_R_SIZE_MAX; 334 335 yesno: if (sysctl(mib, mib_len, &value, &len, NULL, 0) == -1) 336 return (-1); 337 if (value == 0) 338 return (-1); 339 return (value); 340 /*NOTREACHED*/ 341 break; 342 343 /* Extensions */ 344 case _SC_NPROCESSORS_CONF: 345 mib[0] = CTL_HW; 346 mib[1] = HW_NCPU; 347 break; 348 case _SC_NPROCESSORS_ONLN: 349 mib[0] = CTL_HW; 350 mib[1] = HW_NCPUONLINE; 351 break; 352 353 default: 354 errno = EINVAL; 355 return (-1); 356 } 357 return (sysctl(mib, mib_len, &value, &len, NULL, 0) == -1 ? -1 : value); 358 } 359