1 /* $NetBSD: sysctl.c,v 1.31 2012/03/13 21:13:36 christos 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.31 2012/03/13 21:13:36 christos 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 int 73 sysctl(name, namelen, oldp, oldlenp, newp, newlen) 74 const int *name; 75 unsigned int namelen; 76 void *oldp; 77 const void *newp; 78 size_t *oldlenp, newlen; 79 { 80 size_t oldlen, savelen; 81 int error; 82 83 if (name[0] != CTL_USER) 84 return (__sysctl(name, namelen, oldp, oldlenp, 85 newp, newlen)); 86 87 oldlen = (oldlenp == NULL) ? 0 : *oldlenp; 88 savelen = oldlen; 89 error = user_sysctl(name + 1, namelen - 1, oldp, &oldlen, newp, newlen); 90 91 if (error != 0) { 92 errno = error; 93 return (-1); 94 } 95 96 if (oldlenp != NULL) { 97 *oldlenp = oldlen; 98 if (oldp != NULL && oldlen > savelen) { 99 errno = ENOMEM; 100 return (-1); 101 } 102 } 103 104 return (0); 105 } 106 107 static int 108 user_sysctl(name, namelen, oldp, oldlenp, newp, newlen) 109 const int *name; 110 unsigned int namelen; 111 void *oldp; 112 const void *newp; 113 size_t *oldlenp, newlen; 114 { 115 #define _INT(s, n, v, d) { \ 116 .sysctl_flags = CTLFLAG_IMMEDIATE|CTLFLAG_PERMANENT| \ 117 CTLTYPE_INT|SYSCTL_VERSION, \ 118 sysc_init_field(_sysctl_size, sizeof(int)), \ 119 .sysctl_name = (s), \ 120 .sysctl_num = (n), \ 121 .sysctl_un = { .scu_idata = (v), }, \ 122 sysc_init_field(_sysctl_desc, (d)), \ 123 } 124 125 /* 126 * the nodes under the "user" node 127 */ 128 static const struct sysctlnode sysctl_usermib[] = { 129 #if defined(lint) 130 /* 131 * lint doesn't like my initializers 132 */ 133 0 134 #else /* !lint */ 135 { 136 .sysctl_flags = SYSCTL_VERSION|CTLFLAG_PERMANENT| 137 CTLTYPE_STRING, 138 sysc_init_field(_sysctl_size, sizeof(_PATH_STDPATH)), 139 .sysctl_name = "cs_path", 140 .sysctl_num = USER_CS_PATH, 141 /* 142 * XXX these nasty initializers (and the one in 143 * the _INT() macro) can go away once all ports 144 * are using gcc3, and become 145 * 146 * .sysctl_data = _PATH_STDPATH, 147 * .sysctl_desc = NULL, 148 */ 149 .sysctl_un = { .scu_data = { 150 sysc_init_field(_sud_data, 151 __UNCONST(_PATH_STDPATH)), 152 }, }, 153 sysc_init_field(_sysctl_desc, 154 "A value for the PATH environment variable " 155 "that finds all the standard utilities"), 156 }, 157 _INT("bc_base_max", USER_BC_BASE_MAX, BC_BASE_MAX, 158 "The maximum ibase/obase values in the bc(1) utility"), 159 _INT("bc_dim_max", USER_BC_DIM_MAX, BC_DIM_MAX, 160 "The maximum array size in the bc(1) utility"), 161 _INT("bc_scale_max", USER_BC_SCALE_MAX, BC_SCALE_MAX, 162 "The maximum scale value in the bc(1) utility"), 163 _INT("bc_string_max", USER_BC_STRING_MAX, BC_STRING_MAX, 164 "The maximum string length in the bc(1) utility"), 165 _INT("coll_weights_max", USER_COLL_WEIGHTS_MAX, 166 COLL_WEIGHTS_MAX, "The maximum number of weights that can " 167 "be assigned to any entry of the LC_COLLATE order keyword " 168 "in the locale definition file"), 169 _INT("expr_nest_max", USER_EXPR_NEST_MAX, EXPR_NEST_MAX, 170 "The maximum number of expressions that can be nested " 171 "within parenthesis by the expr(1) utility"), 172 _INT("line_max", USER_LINE_MAX, LINE_MAX, "The maximum length " 173 "in bytes of a text-processing utility's input line"), 174 _INT("re_dup_max", USER_RE_DUP_MAX, RE_DUP_MAX, "The maximum " 175 "number of repeated occurrences of a regular expression " 176 "permitted when using interval notation"), 177 _INT("posix2_version", USER_POSIX2_VERSION, _POSIX2_VERSION, 178 "The version of POSIX 1003.2 with which the system " 179 "attempts to comply"), 180 #ifdef _POSIX2_C_BIND 181 _INT("posix2_c_bind", USER_POSIX2_C_BIND, 1, 182 "Whether the system's C-language development facilities " 183 "support the C-Language Bindings Option"), 184 #else 185 _INT("posix2_c_bind", USER_POSIX2_C_BIND, 0, 186 "Whether the system's C-language development facilities " 187 "support the C-Language Bindings Option"), 188 #endif 189 #ifdef POSIX2_C_DEV 190 _INT("posix2_c_dev", USER_POSIX2_C_DEV, 1, 191 "Whether the system supports the C-Language Development " 192 "Utilities Option"), 193 #else 194 _INT("posix2_c_dev", USER_POSIX2_C_DEV, 0, 195 "Whether the system supports the C-Language Development " 196 "Utilities Option"), 197 #endif 198 #ifdef POSIX2_CHAR_TERM 199 _INT("posix2_char_term", USER_POSIX2_CHAR_TERM, 1, 200 "Whether the system supports at least one terminal type " 201 "capable of all operations described in POSIX 1003.2"), 202 #else 203 _INT("posix2_char_term", USER_POSIX2_CHAR_TERM, 0, 204 "Whether the system supports at least one terminal type " 205 "capable of all operations described in POSIX 1003.2"), 206 #endif 207 #ifdef POSIX2_FORT_DEV 208 _INT("posix2_fort_dev", USER_POSIX2_FORT_DEV, 1, 209 "Whether the system supports the FORTRAN Development " 210 "Utilities Option"), 211 #else 212 _INT("posix2_fort_dev", USER_POSIX2_FORT_DEV, 0, 213 "Whether the system supports the FORTRAN Development " 214 "Utilities Option"), 215 #endif 216 #ifdef POSIX2_FORT_RUN 217 _INT("posix2_fort_run", USER_POSIX2_FORT_RUN, 1, 218 "Whether the system supports the FORTRAN Runtime " 219 "Utilities Option"), 220 #else 221 _INT("posix2_fort_run", USER_POSIX2_FORT_RUN, 0, 222 "Whether the system supports the FORTRAN Runtime " 223 "Utilities Option"), 224 #endif 225 #ifdef POSIX2_LOCALEDEF 226 _INT("posix2_localedef", USER_POSIX2_LOCALEDEF, 1, 227 "Whether the system supports the creation of locales"), 228 #else 229 _INT("posix2_localedef", USER_POSIX2_LOCALEDEF, 0, 230 "Whether the system supports the creation of locales"), 231 #endif 232 #ifdef POSIX2_SW_DEV 233 _INT("posix2_sw_dev", USER_POSIX2_SW_DEV, 1, 234 "Whether the system supports the Software Development " 235 "Utilities Option"), 236 #else 237 _INT("posix2_sw_dev", USER_POSIX2_SW_DEV, 0, 238 "Whether the system supports the Software Development " 239 "Utilities Option"), 240 #endif 241 #ifdef POSIX2_UPE 242 _INT("posix2_upe", USER_POSIX2_UPE, 1, 243 "Whether the system supports the User Portability " 244 "Utilities Option"), 245 #else 246 _INT("posix2_upe", USER_POSIX2_UPE, 0, 247 "Whether the system supports the User Portability " 248 "Utilities Option"), 249 #endif 250 _INT("stream_max", USER_STREAM_MAX, FOPEN_MAX, 251 "The minimum maximum number of streams that a process " 252 "may have open at any one time"), 253 _INT("tzname_max", USER_TZNAME_MAX, NAME_MAX, 254 "The minimum maximum number of types supported for the " 255 "name of a timezone"), 256 _INT("atexit_max", USER_ATEXIT_MAX, -1, 257 "The maximum number of functions that may be registered " 258 "with atexit(3)"), 259 #endif /* !lint */ 260 }; 261 #undef _INT 262 263 static const int clen = sizeof(sysctl_usermib) / 264 sizeof(sysctl_usermib[0]); 265 266 const struct sysctlnode *node; 267 int ni; 268 size_t l, sz; 269 270 /* 271 * none of these nodes are writable and they're all terminal (for now) 272 */ 273 if (namelen != 1) 274 return (EINVAL); 275 276 l = *oldlenp; 277 if (name[0] == CTL_QUERY) { 278 uint v; 279 node = newp; 280 if (node == NULL) 281 return (EINVAL); 282 else if (SYSCTL_VERS(node->sysctl_flags) == SYSCTL_VERS_1 && 283 newlen == sizeof(struct sysctlnode)) 284 v = SYSCTL_VERS_1; 285 else 286 return (EINVAL); 287 288 sz = 0; 289 for (ni = 0; ni < clen; ni++) 290 sz += __cvt_node_out(v, &sysctl_usermib[ni], &oldp, &l); 291 *oldlenp = sz; 292 return (0); 293 } 294 295 if (name[0] == CTL_DESCRIBE) { 296 /* 297 * XXX make sure this is larger than the largest 298 * "user" description 299 */ 300 char buf[192]; 301 struct sysctldesc *d1 = (void *)&buf[0], *d2 = oldp; 302 size_t d; 303 304 node = newp; 305 if (node != NULL && 306 (SYSCTL_VERS(node->sysctl_flags) < SYSCTL_VERS_1 || 307 newlen != sizeof(struct sysctlnode))) 308 return (EINVAL); 309 310 sz = 0; 311 for (ni = 0; ni < clen; ni++) { 312 memset(&buf[0], 0, sizeof(buf)); 313 if (node != NULL && 314 node->sysctl_num != sysctl_usermib[ni].sysctl_num) 315 continue; 316 d1->descr_num = sysctl_usermib[ni].sysctl_num; 317 d1->descr_ver = sysctl_usermib[ni].sysctl_ver; 318 if (sysctl_usermib[ni].sysctl_desc == NULL) 319 d1->descr_len = 1; 320 else { 321 size_t dlen; 322 (void)strlcpy(d1->descr_str, 323 sysctl_usermib[ni].sysctl_desc, 324 sizeof(buf) - sizeof(*d1)); 325 dlen = strlen(d1->descr_str) + 1; 326 _DIAGASSERT(__type_fit(uint32_t, dlen)); 327 d1->descr_len = (uint32_t)dlen; 328 } 329 d = (size_t)__sysc_desc_adv(NULL, d1->descr_len); 330 if (d2 != NULL) 331 memcpy(d2, d1, d); 332 sz += d; 333 if (node != NULL) 334 break; 335 } 336 *oldlenp = sz; 337 if (sz == 0 && node != NULL) 338 return (ENOENT); 339 return (0); 340 341 } 342 343 /* 344 * none of these nodes are writable 345 */ 346 if (newp != NULL || newlen != 0) 347 return (EPERM); 348 349 node = &sysctl_usermib[0]; 350 for (ni = 0; ni < clen; ni++) 351 if (name[0] == node[ni].sysctl_num) 352 break; 353 if (ni == clen) 354 return (EOPNOTSUPP); 355 356 node = &node[ni]; 357 if (node->sysctl_flags & CTLFLAG_IMMEDIATE) { 358 switch (SYSCTL_TYPE(node->sysctl_flags)) { 359 case CTLTYPE_INT: 360 newp = &node->sysctl_idata; 361 break; 362 case CTLTYPE_QUAD: 363 newp = &node->sysctl_qdata; 364 break; 365 default: 366 return (EINVAL); 367 } 368 } 369 else 370 newp = node->sysctl_data; 371 372 l = MIN(l, node->sysctl_size); 373 if (oldp != NULL) 374 memcpy(oldp, newp, l); 375 *oldlenp = node->sysctl_size; 376 377 return (0); 378 } 379 380 static size_t 381 __cvt_node_out(uint v, const struct sysctlnode *n, void **o, size_t *l) 382 { 383 const void *src = n; 384 size_t sz; 385 386 switch (v) { 387 #if (SYSCTL_VERSION != SYSCTL_VERS_1) 388 #error __cvt_node_out: no support for SYSCTL_VERSION 389 #endif /* (SYSCTL_VERSION != SYSCTL_VERS_1) */ 390 391 case SYSCTL_VERSION: 392 sz = sizeof(struct sysctlnode); 393 break; 394 395 default: 396 sz = 0; 397 break; 398 } 399 400 if (sz > 0 && *o != NULL && *l >= sz) { 401 memcpy(*o, src, sz); 402 *o = sz + (caddr_t)*o; 403 *l -= sz; 404 } 405 406 return(sz); 407 } 408