1 /* $NetBSD: sysctl.c,v 1.32 2012/03/20 16:36:05 matt Exp $ */ 2 3 /*- 4 * Copyright (c) 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 #if defined(LIBC_SCCS) && !defined(lint) 34 #if 0 35 static char sccsid[] = "@(#)sysctl.c 8.2 (Berkeley) 1/4/94"; 36 #else 37 __RCSID("$NetBSD: sysctl.c,v 1.32 2012/03/20 16:36:05 matt Exp $"); 38 #endif 39 #endif /* LIBC_SCCS and not lint */ 40 41 #include "namespace.h" 42 #include <sys/param.h> 43 #define __COMPAT_SYSCTL 44 #include <sys/sysctl.h> 45 46 #include <assert.h> 47 #include <errno.h> 48 #include <paths.h> 49 #include <stdio.h> 50 #include <string.h> 51 #include <unistd.h> 52 #include "extern.h" 53 54 #ifdef __weak_alias 55 __weak_alias(sysctl,_sysctl) 56 #endif 57 58 /* 59 * handles requests off the user subtree 60 */ 61 static int user_sysctl(const int *, u_int, void *, size_t *, 62 const void *, size_t); 63 64 /* 65 * copies out individual nodes taking target version into account 66 */ 67 static size_t __cvt_node_out(uint, const struct sysctlnode *, void **, 68 size_t *); 69 70 #include <stdlib.h> 71 72 #if defined(__minix) 73 int __sysctl(const int *name, unsigned int namelen, 74 void *oldp, size_t *oldlenp, 75 const void *newp, size_t newlen) 76 { 77 errno = ENOENT; 78 return -1; 79 } 80 #endif /* defined(__minix) */ 81 82 int 83 sysctl(const int *name, unsigned int namelen, 84 void *oldp, size_t *oldlenp, 85 const void *newp, size_t newlen) 86 { 87 size_t oldlen, savelen; 88 int error; 89 90 if (name[0] != CTL_USER) 91 return (__sysctl(name, namelen, oldp, oldlenp, 92 newp, newlen)); 93 94 oldlen = (oldlenp == NULL) ? 0 : *oldlenp; 95 savelen = oldlen; 96 error = user_sysctl(name + 1, namelen - 1, oldp, &oldlen, newp, newlen); 97 98 if (error != 0) { 99 errno = error; 100 return (-1); 101 } 102 103 if (oldlenp != NULL) { 104 *oldlenp = oldlen; 105 if (oldp != NULL && oldlen > savelen) { 106 errno = ENOMEM; 107 return (-1); 108 } 109 } 110 111 return (0); 112 } 113 114 static int 115 user_sysctl(const int *name, unsigned int namelen, 116 void *oldp, size_t *oldlenp, 117 const void *newp, size_t newlen) 118 { 119 #define _INT(s, n, v, d) { \ 120 .sysctl_flags = CTLFLAG_IMMEDIATE|CTLFLAG_PERMANENT| \ 121 CTLTYPE_INT|SYSCTL_VERSION, \ 122 sysc_init_field(_sysctl_size, sizeof(int)), \ 123 .sysctl_name = (s), \ 124 .sysctl_num = (n), \ 125 .sysctl_un = { .scu_idata = (v), }, \ 126 sysc_init_field(_sysctl_desc, (d)), \ 127 } 128 129 /* 130 * the nodes under the "user" node 131 */ 132 static const struct sysctlnode sysctl_usermib[] = { 133 #if defined(lint) 134 /* 135 * lint doesn't like my initializers 136 */ 137 0 138 #else /* !lint */ 139 { 140 .sysctl_flags = SYSCTL_VERSION|CTLFLAG_PERMANENT| 141 CTLTYPE_STRING, 142 sysc_init_field(_sysctl_size, sizeof(_PATH_STDPATH)), 143 .sysctl_name = "cs_path", 144 .sysctl_num = USER_CS_PATH, 145 /* 146 * XXX these nasty initializers (and the one in 147 * the _INT() macro) can go away once all ports 148 * are using gcc3, and become 149 * 150 * .sysctl_data = _PATH_STDPATH, 151 * .sysctl_desc = NULL, 152 */ 153 .sysctl_un = { .scu_data = { 154 sysc_init_field(_sud_data, 155 __UNCONST(_PATH_STDPATH)), 156 }, }, 157 sysc_init_field(_sysctl_desc, 158 "A value for the PATH environment variable " 159 "that finds all the standard utilities"), 160 }, 161 _INT("bc_base_max", USER_BC_BASE_MAX, BC_BASE_MAX, 162 "The maximum ibase/obase values in the bc(1) utility"), 163 _INT("bc_dim_max", USER_BC_DIM_MAX, BC_DIM_MAX, 164 "The maximum array size in the bc(1) utility"), 165 _INT("bc_scale_max", USER_BC_SCALE_MAX, BC_SCALE_MAX, 166 "The maximum scale value in the bc(1) utility"), 167 _INT("bc_string_max", USER_BC_STRING_MAX, BC_STRING_MAX, 168 "The maximum string length in the bc(1) utility"), 169 _INT("coll_weights_max", USER_COLL_WEIGHTS_MAX, 170 COLL_WEIGHTS_MAX, "The maximum number of weights that can " 171 "be assigned to any entry of the LC_COLLATE order keyword " 172 "in the locale definition file"), 173 _INT("expr_nest_max", USER_EXPR_NEST_MAX, EXPR_NEST_MAX, 174 "The maximum number of expressions that can be nested " 175 "within parenthesis by the expr(1) utility"), 176 _INT("line_max", USER_LINE_MAX, LINE_MAX, "The maximum length " 177 "in bytes of a text-processing utility's input line"), 178 _INT("re_dup_max", USER_RE_DUP_MAX, RE_DUP_MAX, "The maximum " 179 "number of repeated occurrences of a regular expression " 180 "permitted when using interval notation"), 181 _INT("posix2_version", USER_POSIX2_VERSION, _POSIX2_VERSION, 182 "The version of POSIX 1003.2 with which the system " 183 "attempts to comply"), 184 #ifdef _POSIX2_C_BIND 185 _INT("posix2_c_bind", USER_POSIX2_C_BIND, 1, 186 "Whether the system's C-language development facilities " 187 "support the C-Language Bindings Option"), 188 #else 189 _INT("posix2_c_bind", USER_POSIX2_C_BIND, 0, 190 "Whether the system's C-language development facilities " 191 "support the C-Language Bindings Option"), 192 #endif 193 #ifdef POSIX2_C_DEV 194 _INT("posix2_c_dev", USER_POSIX2_C_DEV, 1, 195 "Whether the system supports the C-Language Development " 196 "Utilities Option"), 197 #else 198 _INT("posix2_c_dev", USER_POSIX2_C_DEV, 0, 199 "Whether the system supports the C-Language Development " 200 "Utilities Option"), 201 #endif 202 #ifdef POSIX2_CHAR_TERM 203 _INT("posix2_char_term", USER_POSIX2_CHAR_TERM, 1, 204 "Whether the system supports at least one terminal type " 205 "capable of all operations described in POSIX 1003.2"), 206 #else 207 _INT("posix2_char_term", USER_POSIX2_CHAR_TERM, 0, 208 "Whether the system supports at least one terminal type " 209 "capable of all operations described in POSIX 1003.2"), 210 #endif 211 #ifdef POSIX2_FORT_DEV 212 _INT("posix2_fort_dev", USER_POSIX2_FORT_DEV, 1, 213 "Whether the system supports the FORTRAN Development " 214 "Utilities Option"), 215 #else 216 _INT("posix2_fort_dev", USER_POSIX2_FORT_DEV, 0, 217 "Whether the system supports the FORTRAN Development " 218 "Utilities Option"), 219 #endif 220 #ifdef POSIX2_FORT_RUN 221 _INT("posix2_fort_run", USER_POSIX2_FORT_RUN, 1, 222 "Whether the system supports the FORTRAN Runtime " 223 "Utilities Option"), 224 #else 225 _INT("posix2_fort_run", USER_POSIX2_FORT_RUN, 0, 226 "Whether the system supports the FORTRAN Runtime " 227 "Utilities Option"), 228 #endif 229 #ifdef POSIX2_LOCALEDEF 230 _INT("posix2_localedef", USER_POSIX2_LOCALEDEF, 1, 231 "Whether the system supports the creation of locales"), 232 #else 233 _INT("posix2_localedef", USER_POSIX2_LOCALEDEF, 0, 234 "Whether the system supports the creation of locales"), 235 #endif 236 #ifdef POSIX2_SW_DEV 237 _INT("posix2_sw_dev", USER_POSIX2_SW_DEV, 1, 238 "Whether the system supports the Software Development " 239 "Utilities Option"), 240 #else 241 _INT("posix2_sw_dev", USER_POSIX2_SW_DEV, 0, 242 "Whether the system supports the Software Development " 243 "Utilities Option"), 244 #endif 245 #ifdef POSIX2_UPE 246 _INT("posix2_upe", USER_POSIX2_UPE, 1, 247 "Whether the system supports the User Portability " 248 "Utilities Option"), 249 #else 250 _INT("posix2_upe", USER_POSIX2_UPE, 0, 251 "Whether the system supports the User Portability " 252 "Utilities Option"), 253 #endif 254 _INT("stream_max", USER_STREAM_MAX, FOPEN_MAX, 255 "The minimum maximum number of streams that a process " 256 "may have open at any one time"), 257 _INT("tzname_max", USER_TZNAME_MAX, NAME_MAX, 258 "The minimum maximum number of types supported for the " 259 "name of a timezone"), 260 _INT("atexit_max", USER_ATEXIT_MAX, -1, 261 "The maximum number of functions that may be registered " 262 "with atexit(3)"), 263 #endif /* !lint */ 264 }; 265 #undef _INT 266 267 static const int clen = sizeof(sysctl_usermib) / 268 sizeof(sysctl_usermib[0]); 269 270 const struct sysctlnode *node; 271 int ni; 272 size_t l, sz; 273 274 /* 275 * none of these nodes are writable and they're all terminal (for now) 276 */ 277 if (namelen != 1) 278 return (EINVAL); 279 280 l = *oldlenp; 281 if (name[0] == CTL_QUERY) { 282 uint v; 283 node = newp; 284 if (node == NULL) 285 return (EINVAL); 286 else if (SYSCTL_VERS(node->sysctl_flags) == SYSCTL_VERS_1 && 287 newlen == sizeof(struct sysctlnode)) 288 v = SYSCTL_VERS_1; 289 else 290 return (EINVAL); 291 292 sz = 0; 293 for (ni = 0; ni < clen; ni++) 294 sz += __cvt_node_out(v, &sysctl_usermib[ni], &oldp, &l); 295 *oldlenp = sz; 296 return (0); 297 } 298 299 if (name[0] == CTL_DESCRIBE) { 300 /* 301 * XXX make sure this is larger than the largest 302 * "user" description 303 */ 304 char buf[192]; 305 struct sysctldesc *d1 = (void *)&buf[0], *d2 = oldp; 306 size_t d; 307 308 node = newp; 309 if (node != NULL && 310 (SYSCTL_VERS(node->sysctl_flags) < SYSCTL_VERS_1 || 311 newlen != sizeof(struct sysctlnode))) 312 return (EINVAL); 313 314 sz = 0; 315 for (ni = 0; ni < clen; ni++) { 316 memset(&buf[0], 0, sizeof(buf)); 317 if (node != NULL && 318 node->sysctl_num != sysctl_usermib[ni].sysctl_num) 319 continue; 320 d1->descr_num = sysctl_usermib[ni].sysctl_num; 321 d1->descr_ver = sysctl_usermib[ni].sysctl_ver; 322 if (sysctl_usermib[ni].sysctl_desc == NULL) 323 d1->descr_len = 1; 324 else { 325 size_t dlen; 326 (void)strlcpy(d1->descr_str, 327 sysctl_usermib[ni].sysctl_desc, 328 sizeof(buf) - sizeof(*d1)); 329 dlen = strlen(d1->descr_str) + 1; 330 _DIAGASSERT(__type_fit(uint32_t, dlen)); 331 d1->descr_len = (uint32_t)dlen; 332 } 333 d = (size_t)__sysc_desc_adv(NULL, d1->descr_len); 334 if (d2 != NULL) 335 memcpy(d2, d1, d); 336 sz += d; 337 if (node != NULL) 338 break; 339 } 340 *oldlenp = sz; 341 if (sz == 0 && node != NULL) 342 return (ENOENT); 343 return (0); 344 345 } 346 347 /* 348 * none of these nodes are writable 349 */ 350 if (newp != NULL || newlen != 0) 351 return (EPERM); 352 353 node = &sysctl_usermib[0]; 354 for (ni = 0; ni < clen; ni++) 355 if (name[0] == node[ni].sysctl_num) 356 break; 357 if (ni == clen) 358 return (EOPNOTSUPP); 359 360 node = &node[ni]; 361 if (node->sysctl_flags & CTLFLAG_IMMEDIATE) { 362 switch (SYSCTL_TYPE(node->sysctl_flags)) { 363 case CTLTYPE_INT: 364 newp = &node->sysctl_idata; 365 break; 366 case CTLTYPE_QUAD: 367 newp = &node->sysctl_qdata; 368 break; 369 default: 370 return (EINVAL); 371 } 372 } 373 else 374 newp = node->sysctl_data; 375 376 l = MIN(l, node->sysctl_size); 377 if (oldp != NULL) 378 memcpy(oldp, newp, l); 379 *oldlenp = node->sysctl_size; 380 381 return (0); 382 } 383 384 static size_t 385 __cvt_node_out(uint v, const struct sysctlnode *n, void **o, size_t *l) 386 { 387 const void *src = n; 388 size_t sz; 389 390 switch (v) { 391 #if (SYSCTL_VERSION != SYSCTL_VERS_1) 392 #error __cvt_node_out: no support for SYSCTL_VERSION 393 #endif /* (SYSCTL_VERSION != SYSCTL_VERS_1) */ 394 395 case SYSCTL_VERSION: 396 sz = sizeof(struct sysctlnode); 397 break; 398 399 default: 400 sz = 0; 401 break; 402 } 403 404 if (sz > 0 && *o != NULL && *l >= sz) { 405 memcpy(*o, src, sz); 406 *o = sz + (caddr_t)*o; 407 *l -= sz; 408 } 409 410 return(sz); 411 } 412