1 /* $OpenBSD: sysconf.c,v 1.13 2011/04/25 20:10:10 sthen 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 <grp.h> 43 #include <pwd.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.1c */ 116 case _SC_LOGIN_NAME_MAX: 117 return (LOGIN_NAME_MAX); 118 119 case _SC_THREAD_SAFE_FUNCTIONS: 120 return (_POSIX_THREAD_SAFE_FUNCTIONS); 121 122 case _SC_GETGR_R_SIZE_MAX: 123 return (_GR_BUF_LEN); 124 125 case _SC_GETPW_R_SIZE_MAX: 126 return (_PW_BUF_LEN); 127 128 /* 1003.2 */ 129 case _SC_BC_BASE_MAX: 130 mib[0] = CTL_USER; 131 mib[1] = USER_BC_BASE_MAX; 132 break; 133 case _SC_BC_DIM_MAX: 134 mib[0] = CTL_USER; 135 mib[1] = USER_BC_DIM_MAX; 136 break; 137 case _SC_BC_SCALE_MAX: 138 mib[0] = CTL_USER; 139 mib[1] = USER_BC_SCALE_MAX; 140 break; 141 case _SC_BC_STRING_MAX: 142 mib[0] = CTL_USER; 143 mib[1] = USER_BC_STRING_MAX; 144 break; 145 case _SC_COLL_WEIGHTS_MAX: 146 mib[0] = CTL_USER; 147 mib[1] = USER_COLL_WEIGHTS_MAX; 148 break; 149 case _SC_EXPR_NEST_MAX: 150 mib[0] = CTL_USER; 151 mib[1] = USER_EXPR_NEST_MAX; 152 break; 153 case _SC_LINE_MAX: 154 mib[0] = CTL_USER; 155 mib[1] = USER_LINE_MAX; 156 break; 157 case _SC_RE_DUP_MAX: 158 mib[0] = CTL_USER; 159 mib[1] = USER_RE_DUP_MAX; 160 break; 161 case _SC_2_VERSION: 162 mib[0] = CTL_USER; 163 mib[1] = USER_POSIX2_VERSION; 164 break; 165 case _SC_2_C_BIND: 166 mib[0] = CTL_USER; 167 mib[1] = USER_POSIX2_C_BIND; 168 goto yesno; 169 case _SC_2_C_DEV: 170 mib[0] = CTL_USER; 171 mib[1] = USER_POSIX2_C_DEV; 172 goto yesno; 173 case _SC_2_CHAR_TERM: 174 mib[0] = CTL_USER; 175 mib[1] = USER_POSIX2_CHAR_TERM; 176 goto yesno; 177 case _SC_2_FORT_DEV: 178 mib[0] = CTL_USER; 179 mib[1] = USER_POSIX2_FORT_DEV; 180 goto yesno; 181 case _SC_2_FORT_RUN: 182 mib[0] = CTL_USER; 183 mib[1] = USER_POSIX2_FORT_RUN; 184 goto yesno; 185 case _SC_2_LOCALEDEF: 186 mib[0] = CTL_USER; 187 mib[1] = USER_POSIX2_LOCALEDEF; 188 goto yesno; 189 case _SC_2_SW_DEV: 190 mib[0] = CTL_USER; 191 mib[1] = USER_POSIX2_SW_DEV; 192 goto yesno; 193 case _SC_2_UPE: 194 mib[0] = CTL_USER; 195 mib[1] = USER_POSIX2_UPE; 196 goto yesno; 197 198 /* XPG 4.2 */ 199 case _SC_XOPEN_SHM: 200 mib[0] = CTL_KERN; 201 mib[1] = KERN_SYSVSHM; 202 203 yesno: if (sysctl(mib, namelen, &value, &len, NULL, 0) == -1) 204 return (-1); 205 if (value == 0) 206 return (-1); 207 return (value); 208 break; 209 case _SC_SEM_NSEMS_MAX: 210 case _SC_SEM_VALUE_MAX: 211 mib[0] = CTL_KERN; 212 mib[1] = KERN_SEMINFO; 213 mib[2] = name = _SC_SEM_NSEMS_MAX ? 214 KERN_SEMINFO_SEMMNS : KERN_SEMINFO_SEMVMX; 215 namelen = 3; 216 break; 217 218 /* Unsorted */ 219 case _SC_HOST_NAME_MAX: 220 return (MAXHOSTNAMELEN - 1); /* does not include \0 */ 221 222 /* Extensions */ 223 case _SC_PHYS_PAGES: 224 { 225 int64_t physmem; 226 227 mib[0] = CTL_HW; 228 mib[1] = HW_PHYSMEM64; 229 len = sizeof(physmem); 230 if (sysctl(mib, namelen, &physmem, &len, NULL, 0) == -1) 231 return (-1); 232 return (physmem / getpagesize()); 233 } 234 case _SC_AVPHYS_PAGES: 235 { 236 struct vmtotal vmtotal; 237 238 mib[0] = CTL_VM; 239 mib[1] = VM_METER; 240 len = sizeof(vmtotal); 241 if (sysctl(mib, namelen, &vmtotal, &len, NULL, 0) == -1) 242 return (-1); 243 return (vmtotal.t_free); 244 } 245 246 case _SC_NPROCESSORS_CONF: 247 mib[0] = CTL_HW; 248 mib[1] = HW_NCPU; 249 break; 250 case _SC_NPROCESSORS_ONLN: 251 mib[0] = CTL_HW; 252 mib[1] = HW_NCPU; 253 break; 254 255 default: 256 errno = EINVAL; 257 return (-1); 258 } 259 return (sysctl(mib, namelen, &value, &len, NULL, 0) == -1 ? -1 : value); 260 } 261