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