1 /* $NetBSD: sysconf.c,v 1.29 2008/04/09 18:37:04 njoly 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.29 2008/04/09 18:37:04 njoly 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 return _getpagesize(); 147 case _SC_FSYNC: 148 mib[0] = CTL_KERN; 149 mib[1] = KERN_FSYNC; 150 goto yesno; 151 case _SC_SYNCHRONIZED_IO: 152 mib[0] = CTL_KERN; 153 mib[1] = KERN_SYNCHRONIZED_IO; 154 goto yesno; 155 case _SC_MAPPED_FILES: 156 mib[0] = CTL_KERN; 157 mib[1] = KERN_MAPPED_FILES; 158 goto yesno; 159 case _SC_MEMLOCK: 160 mib[0] = CTL_KERN; 161 mib[1] = KERN_MEMLOCK; 162 goto yesno; 163 case _SC_MEMLOCK_RANGE: 164 mib[0] = CTL_KERN; 165 mib[1] = KERN_MEMLOCK_RANGE; 166 goto yesno; 167 case _SC_MEMORY_PROTECTION: 168 mib[0] = CTL_KERN; 169 mib[1] = KERN_MEMORY_PROTECTION; 170 goto yesno; 171 case _SC_MONOTONIC_CLOCK: 172 mib[0] = CTL_KERN; 173 mib[1] = KERN_MONOTONIC_CLOCK; 174 goto yesno; 175 case _SC_SEMAPHORES: 176 mib[0] = CTL_KERN; 177 mib[1] = KERN_POSIX_SEMAPHORES; 178 goto yesno; 179 case _SC_TIMERS: 180 mib[0] = CTL_KERN; 181 mib[1] = KERN_POSIX_TIMERS; 182 goto yesno; 183 184 /* 1003.1c */ 185 case _SC_LOGIN_NAME_MAX: 186 mib[0] = CTL_KERN; 187 mib[1] = KERN_LOGIN_NAME_MAX; 188 break; 189 case _SC_THREADS: 190 mib[0] = CTL_KERN; 191 mib[1] = KERN_POSIX_THREADS; 192 goto yesno; 193 194 /* 1003.1j */ 195 case _SC_BARRIERS: 196 mib[0] = CTL_KERN; 197 mib[1] = KERN_POSIX_BARRIERS; 198 goto yesno; 199 case _SC_SPIN_LOCKS: 200 mib[0] = CTL_KERN; 201 mib[1] = KERN_POSIX_SPIN_LOCKS; 202 goto yesno; 203 /* Historical; Threads option in 1003.1-2001 */ 204 case _SC_READER_WRITER_LOCKS: 205 mib[0] = CTL_KERN; 206 mib[1] = KERN_POSIX_READER_WRITER_LOCKS; 207 goto yesno; 208 209 /* 1003.2 */ 210 case _SC_BC_BASE_MAX: 211 mib[0] = CTL_USER; 212 mib[1] = USER_BC_BASE_MAX; 213 break; 214 case _SC_BC_DIM_MAX: 215 mib[0] = CTL_USER; 216 mib[1] = USER_BC_DIM_MAX; 217 break; 218 case _SC_BC_SCALE_MAX: 219 mib[0] = CTL_USER; 220 mib[1] = USER_BC_SCALE_MAX; 221 break; 222 case _SC_BC_STRING_MAX: 223 mib[0] = CTL_USER; 224 mib[1] = USER_BC_STRING_MAX; 225 break; 226 case _SC_COLL_WEIGHTS_MAX: 227 mib[0] = CTL_USER; 228 mib[1] = USER_COLL_WEIGHTS_MAX; 229 break; 230 case _SC_EXPR_NEST_MAX: 231 mib[0] = CTL_USER; 232 mib[1] = USER_EXPR_NEST_MAX; 233 break; 234 case _SC_LINE_MAX: 235 mib[0] = CTL_USER; 236 mib[1] = USER_LINE_MAX; 237 break; 238 case _SC_RE_DUP_MAX: 239 mib[0] = CTL_USER; 240 mib[1] = USER_RE_DUP_MAX; 241 break; 242 case _SC_2_VERSION: 243 mib[0] = CTL_USER; 244 mib[1] = USER_POSIX2_VERSION; 245 break; 246 case _SC_2_C_BIND: 247 mib[0] = CTL_USER; 248 mib[1] = USER_POSIX2_C_BIND; 249 goto yesno; 250 case _SC_2_C_DEV: 251 mib[0] = CTL_USER; 252 mib[1] = USER_POSIX2_C_DEV; 253 goto yesno; 254 case _SC_2_CHAR_TERM: 255 mib[0] = CTL_USER; 256 mib[1] = USER_POSIX2_CHAR_TERM; 257 goto yesno; 258 case _SC_2_FORT_DEV: 259 mib[0] = CTL_USER; 260 mib[1] = USER_POSIX2_FORT_DEV; 261 goto yesno; 262 case _SC_2_FORT_RUN: 263 mib[0] = CTL_USER; 264 mib[1] = USER_POSIX2_FORT_RUN; 265 goto yesno; 266 case _SC_2_LOCALEDEF: 267 mib[0] = CTL_USER; 268 mib[1] = USER_POSIX2_LOCALEDEF; 269 goto yesno; 270 case _SC_2_SW_DEV: 271 mib[0] = CTL_USER; 272 mib[1] = USER_POSIX2_SW_DEV; 273 goto yesno; 274 case _SC_2_UPE: 275 mib[0] = CTL_USER; 276 mib[1] = USER_POSIX2_UPE; 277 goto yesno; 278 279 /* XPG 4.2 */ 280 case _SC_IOV_MAX: 281 mib[0] = CTL_KERN; 282 mib[1] = KERN_IOV_MAX; 283 break; 284 case _SC_XOPEN_SHM: 285 mib[0] = CTL_KERN; 286 mib[1] = KERN_SYSVIPC; 287 mib[2] = KERN_SYSVIPC_SHM; 288 mib_len = 3; 289 goto yesno; 290 291 /* 1003.1-2001, XSI Option Group */ 292 case _SC_AIO_LISTIO_MAX: 293 if (sysctlgetmibinfo("kern.aio_listio_max", &mib[0], &mib_len, 294 NULL, NULL, NULL, SYSCTL_VERSION)) 295 return -1; 296 break; 297 case _SC_AIO_MAX: 298 if (sysctlgetmibinfo("kern.aio_max", &mib[0], &mib_len, 299 NULL, NULL, NULL, SYSCTL_VERSION)) 300 return -1; 301 break; 302 case _SC_ASYNCHRONOUS_IO: 303 if (sysctlgetmibinfo("kern.posix_aio", &mib[0], &mib_len, 304 NULL, NULL, NULL, SYSCTL_VERSION)) 305 return -1; 306 goto yesno; 307 case _SC_MESSAGE_PASSING: 308 if (sysctlgetmibinfo("kern.posix_msg", &mib[0], &mib_len, 309 NULL, NULL, NULL, SYSCTL_VERSION)) 310 return -1; 311 goto yesno; 312 case _SC_MQ_OPEN_MAX: 313 if (sysctlgetmibinfo("kern.mqueue.mq_open_max", &mib[0], 314 &mib_len, NULL, NULL, NULL, SYSCTL_VERSION)) 315 return -1; 316 break; 317 case _SC_MQ_PRIO_MAX: 318 if (sysctlgetmibinfo("kern.mqueue.mq_prio_max", &mib[0], 319 &mib_len, NULL, NULL, NULL, SYSCTL_VERSION)) 320 return -1; 321 break; 322 case _SC_PRIORITY_SCHEDULING: 323 if (sysctlgetmibinfo("kern.posix_sched", &mib[0], &mib_len, 324 NULL, NULL, NULL, SYSCTL_VERSION)) 325 return -1; 326 goto yesno; 327 case _SC_ATEXIT_MAX: 328 mib[0] = CTL_USER; 329 mib[1] = USER_ATEXIT_MAX; 330 break; 331 332 /* 1003.1-2001, TSF */ 333 case _SC_GETGR_R_SIZE_MAX: 334 return _GETGR_R_SIZE_MAX; 335 case _SC_GETPW_R_SIZE_MAX: 336 return _GETPW_R_SIZE_MAX; 337 338 yesno: if (sysctl(mib, mib_len, &value, &len, NULL, 0) == -1) 339 return (-1); 340 if (value == 0) 341 return (-1); 342 return (value); 343 /*NOTREACHED*/ 344 break; 345 346 /* Extensions */ 347 case _SC_NPROCESSORS_CONF: 348 mib[0] = CTL_HW; 349 mib[1] = HW_NCPU; 350 break; 351 case _SC_NPROCESSORS_ONLN: 352 mib[0] = CTL_HW; 353 mib[1] = HW_NCPUONLINE; 354 break; 355 356 /* Native */ 357 case _SC_SCHED_RT_TS: 358 if (sysctlgetmibinfo("kern.sched.rtts", &mib[0], &mib_len, 359 NULL, NULL, NULL, SYSCTL_VERSION)) 360 return -1; 361 break; 362 case _SC_SCHED_PRI_MIN: 363 if (sysctlgetmibinfo("kern.sched.pri_min", &mib[0], &mib_len, 364 NULL, NULL, NULL, SYSCTL_VERSION)) 365 return -1; 366 break; 367 case _SC_SCHED_PRI_MAX: 368 if (sysctlgetmibinfo("kern.sched.pri_max", &mib[0], &mib_len, 369 NULL, NULL, NULL, SYSCTL_VERSION)) 370 return -1; 371 break; 372 373 default: 374 errno = EINVAL; 375 return (-1); 376 } 377 return (sysctl(mib, mib_len, &value, &len, NULL, 0) == -1 ? -1 : value); 378 } 379