1 /*- 2 * Copyright (c) 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Sean Eric Fagan of Cygnus Support. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #if defined(LIBC_SCCS) && !defined(lint) 34 static char rcsid[] = "$OpenBSD: sysconf.c,v 1.7 2004/05/18 02:05:52 jfb Exp $"; 35 #endif /* LIBC_SCCS and not lint */ 36 37 #include <sys/param.h> 38 #include <sys/sem.h> 39 #include <sys/sysctl.h> 40 #include <sys/time.h> 41 #include <sys/resource.h> 42 43 #include <errno.h> 44 #include <unistd.h> 45 46 /* 47 * sysconf -- 48 * get configurable system variables. 49 * 50 * XXX 51 * POSIX 1003.1 (ISO/IEC 9945-1, 4.8.1.3) states that the variable values 52 * not change during the lifetime of the calling process. This would seem 53 * to require that any change to system limits kill all running processes. 54 * A workaround might be to cache the values when they are first retrieved 55 * and then simply return the cached value on subsequent calls. This is 56 * less useful than returning up-to-date values, however. 57 */ 58 long 59 sysconf(int name) 60 { 61 struct rlimit rl; 62 size_t len; 63 int mib[3], value, namelen; 64 65 len = sizeof(value); 66 namelen = 2; 67 68 switch (name) { 69 /* 1003.1 */ 70 case _SC_ARG_MAX: 71 mib[0] = CTL_KERN; 72 mib[1] = KERN_ARGMAX; 73 break; 74 case _SC_CHILD_MAX: 75 return (getrlimit(RLIMIT_NPROC, &rl) ? -1 : rl.rlim_cur); 76 case _SC_CLK_TCK: 77 return (CLK_TCK); 78 case _SC_JOB_CONTROL: 79 mib[0] = CTL_KERN; 80 mib[1] = KERN_JOB_CONTROL; 81 goto yesno; 82 case _SC_NGROUPS_MAX: 83 mib[0] = CTL_KERN; 84 mib[1] = KERN_NGROUPS; 85 break; 86 case _SC_OPEN_MAX: 87 return (getrlimit(RLIMIT_NOFILE, &rl) ? -1 : rl.rlim_cur); 88 case _SC_STREAM_MAX: 89 mib[0] = CTL_USER; 90 mib[1] = USER_STREAM_MAX; 91 break; 92 case _SC_TZNAME_MAX: 93 mib[0] = CTL_USER; 94 mib[1] = USER_TZNAME_MAX; 95 break; 96 case _SC_SAVED_IDS: 97 mib[0] = CTL_KERN; 98 mib[1] = KERN_SAVED_IDS; 99 goto yesno; 100 case _SC_VERSION: 101 mib[0] = CTL_KERN; 102 mib[1] = KERN_POSIX1; 103 break; 104 105 /* 1003.1b */ 106 case _SC_PAGESIZE: 107 mib[0] = CTL_HW; 108 mib[1] = HW_PAGESIZE; 109 break; 110 case _SC_FSYNC: 111 mib[0] = CTL_KERN; 112 mib[1] = KERN_FSYNC; 113 goto yesno; 114 115 /* 1003.2 */ 116 case _SC_BC_BASE_MAX: 117 mib[0] = CTL_USER; 118 mib[1] = USER_BC_BASE_MAX; 119 break; 120 case _SC_BC_DIM_MAX: 121 mib[0] = CTL_USER; 122 mib[1] = USER_BC_DIM_MAX; 123 break; 124 case _SC_BC_SCALE_MAX: 125 mib[0] = CTL_USER; 126 mib[1] = USER_BC_SCALE_MAX; 127 break; 128 case _SC_BC_STRING_MAX: 129 mib[0] = CTL_USER; 130 mib[1] = USER_BC_STRING_MAX; 131 break; 132 case _SC_COLL_WEIGHTS_MAX: 133 mib[0] = CTL_USER; 134 mib[1] = USER_COLL_WEIGHTS_MAX; 135 break; 136 case _SC_EXPR_NEST_MAX: 137 mib[0] = CTL_USER; 138 mib[1] = USER_EXPR_NEST_MAX; 139 break; 140 case _SC_LINE_MAX: 141 mib[0] = CTL_USER; 142 mib[1] = USER_LINE_MAX; 143 break; 144 case _SC_RE_DUP_MAX: 145 mib[0] = CTL_USER; 146 mib[1] = USER_RE_DUP_MAX; 147 break; 148 case _SC_2_VERSION: 149 mib[0] = CTL_USER; 150 mib[1] = USER_POSIX2_VERSION; 151 break; 152 case _SC_2_C_BIND: 153 mib[0] = CTL_USER; 154 mib[1] = USER_POSIX2_C_BIND; 155 goto yesno; 156 case _SC_2_C_DEV: 157 mib[0] = CTL_USER; 158 mib[1] = USER_POSIX2_C_DEV; 159 goto yesno; 160 case _SC_2_CHAR_TERM: 161 mib[0] = CTL_USER; 162 mib[1] = USER_POSIX2_CHAR_TERM; 163 goto yesno; 164 case _SC_2_FORT_DEV: 165 mib[0] = CTL_USER; 166 mib[1] = USER_POSIX2_FORT_DEV; 167 goto yesno; 168 case _SC_2_FORT_RUN: 169 mib[0] = CTL_USER; 170 mib[1] = USER_POSIX2_FORT_RUN; 171 goto yesno; 172 case _SC_2_LOCALEDEF: 173 mib[0] = CTL_USER; 174 mib[1] = USER_POSIX2_LOCALEDEF; 175 goto yesno; 176 case _SC_2_SW_DEV: 177 mib[0] = CTL_USER; 178 mib[1] = USER_POSIX2_SW_DEV; 179 goto yesno; 180 case _SC_2_UPE: 181 mib[0] = CTL_USER; 182 mib[1] = USER_POSIX2_UPE; 183 goto yesno; 184 185 /* XPG 4.2 */ 186 case _SC_XOPEN_SHM: 187 mib[0] = CTL_KERN; 188 mib[1] = KERN_SYSVSHM; 189 190 yesno: if (sysctl(mib, namelen, &value, &len, NULL, 0) == -1) 191 return (-1); 192 if (value == 0) 193 return (-1); 194 return (value); 195 break; 196 case _SC_SEM_NSEMS_MAX: 197 case _SC_SEM_VALUE_MAX: 198 mib[0] = CTL_KERN; 199 mib[1] = KERN_SEMINFO; 200 mib[2] = name = _SC_SEM_NSEMS_MAX ? 201 KERN_SEMINFO_SEMMNS : KERN_SEMINFO_SEMVMX; 202 namelen = 3; 203 break; 204 default: 205 errno = EINVAL; 206 return (-1); 207 } 208 return (sysctl(mib, namelen, &value, &len, NULL, 0) == -1 ? -1 : value); 209 } 210