1 /* $NetBSD: sysctl.c,v 1.23 2004/03/26 22:54:42 he 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.23 2004/03/26 22:54:42 he 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 <errno.h> 47 #include <paths.h> 48 #include <stdio.h> 49 #include <string.h> 50 #include <unistd.h> 51 #include "extern.h" 52 53 #ifdef __weak_alias 54 __weak_alias(sysctl,_sysctl) 55 #endif 56 57 /* 58 * handles requests off the user subtree 59 */ 60 static int user_sysctl(int *, u_int, void *, size_t *, const void *, size_t); 61 62 /* 63 * copies out individual nodes taking target version into account 64 */ 65 static size_t __cvt_node_out(uint, const struct sysctlnode *, void **, 66 size_t *); 67 68 #include <stdlib.h> 69 70 int 71 sysctl(name, namelen, oldp, oldlenp, newp, newlen) 72 int *name; 73 unsigned int namelen; 74 void *oldp; 75 const void *newp; 76 size_t *oldlenp, newlen; 77 { 78 size_t oldlen, savelen; 79 int error; 80 81 if (name[0] != CTL_USER) 82 /* LINTED will fix when sysctl interface gets corrected */ 83 /* XXX when will that be? */ 84 return (__sysctl(name, namelen, oldp, oldlenp, 85 (void *)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 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, _PATH_STDPATH), 151 }, }, 152 sysc_init_field(_sysctl_desc, NULL), 153 }, 154 _INT("bc_base_max", USER_BC_BASE_MAX, BC_BASE_MAX, NULL), 155 _INT("bc_dim_max", USER_BC_DIM_MAX, BC_DIM_MAX, NULL), 156 _INT("bc_scale_max", USER_BC_SCALE_MAX, BC_SCALE_MAX, NULL), 157 _INT("bc_string_max", USER_BC_STRING_MAX, BC_STRING_MAX, NULL), 158 _INT("coll_weights_max", USER_COLL_WEIGHTS_MAX, 159 COLL_WEIGHTS_MAX, NULL), 160 _INT("expr_nest_max", USER_EXPR_NEST_MAX, EXPR_NEST_MAX, NULL), 161 _INT("line_max", USER_LINE_MAX, LINE_MAX, NULL), 162 _INT("re_dup_max", USER_RE_DUP_MAX, RE_DUP_MAX, NULL), 163 _INT("posix2_version", USER_POSIX2_VERSION, _POSIX2_VERSION, NULL), 164 #ifdef POSIX2_C_BIND 165 _INT("posix2_c_bind", USER_POSIX2_C_BIND, 1, NULL), 166 #else 167 _INT("posix2_c_bind", USER_POSIX2_C_BIND, 0, NULL), 168 #endif 169 #ifdef POSIX2_C_DEV 170 _INT("posix2_c_dev", USER_POSIX2_C_DEV, 1, NULL), 171 #else 172 _INT("posix2_c_dev", USER_POSIX2_C_DEV, 0, NULL), 173 #endif 174 #ifdef POSIX2_CHAR_TERM 175 _INT("posix2_char_term", USER_POSIX2_CHAR_TERM, 1, NULL), 176 #else 177 _INT("posix2_char_term", USER_POSIX2_CHAR_TERM, 0, NULL), 178 #endif 179 #ifdef POSIX2_FORT_DEV 180 _INT("posix2_fort_dev", USER_POSIX2_FORT_DEV, 1, NULL), 181 #else 182 _INT("posix2_fort_dev", USER_POSIX2_FORT_DEV, 0, NULL), 183 #endif 184 #ifdef POSIX2_FORT_RUN 185 _INT("posix2_fort_run", USER_POSIX2_FORT_RUN, 1, NULL), 186 #else 187 _INT("posix2_fort_run", USER_POSIX2_FORT_RUN, 0, NULL), 188 #endif 189 #ifdef POSIX2_LOCALEDEF 190 _INT("posix2_localedef", USER_POSIX2_LOCALEDEF, 1, NULL), 191 #else 192 _INT("posix2_localedef", USER_POSIX2_LOCALEDEF, 0, NULL), 193 #endif 194 #ifdef POSIX2_SW_DEV 195 _INT("posix2_sw_dev", USER_POSIX2_SW_DEV, 1, NULL), 196 #else 197 _INT("posix2_sw_dev", USER_POSIX2_SW_DEV, 0, NULL), 198 #endif 199 #ifdef POSIX2_UPE 200 _INT("posix2_upe", USER_POSIX2_UPE, 1, NULL), 201 #else 202 _INT("posix2_upe", USER_POSIX2_UPE, 0, NULL), 203 #endif 204 _INT("stream_max", USER_STREAM_MAX, FOPEN_MAX, NULL), 205 _INT("tzname_max", USER_TZNAME_MAX, NAME_MAX, NULL), 206 _INT("atexit_max", USER_ATEXIT_MAX, -1, NULL), 207 #endif /* !lint */ 208 }; 209 #undef _INT 210 211 static const int clen = sizeof(sysctl_usermib) / 212 sizeof(sysctl_usermib[0]); 213 214 const struct sysctlnode *node; 215 int ni; 216 size_t l, sz; 217 218 /* 219 * none of these nodes are writable and they're all terminal (for now) 220 */ 221 if (namelen != 1) 222 return (EINVAL); 223 224 l = *oldlenp; 225 if (name[0] == CTL_QUERY) { 226 uint v; 227 node = newp; 228 if (node == NULL) { 229 v = SYSCTL_VERS_0; 230 newlen = sizeof(struct sysctlnode); 231 } 232 else if (SYSCTL_VERS(node->sysctl_flags) == SYSCTL_VERS_0 && 233 newlen == sizeof(struct sysctlnode0)) 234 v = SYSCTL_VERS_0; 235 else if (SYSCTL_VERS(node->sysctl_flags) == SYSCTL_VERS_1 && 236 newlen == sizeof(struct sysctlnode)) 237 v = SYSCTL_VERS_1; 238 else 239 return (EINVAL); 240 241 sz = 0; 242 for (ni = 0; ni < clen; ni++) 243 sz += __cvt_node_out(v, &sysctl_usermib[ni], &oldp, &l); 244 *oldlenp = sz; 245 return (0); 246 } 247 248 if (name[0] == CTL_DESCRIBE) { 249 char buf[128]; 250 struct sysctldesc *d1 = (void *)&buf[0], *d2 = oldp; 251 size_t d; 252 253 node = newp; 254 if (node != NULL && 255 (SYSCTL_VERS(node->sysctl_flags) < SYSCTL_VERS_1 || 256 newlen != sizeof(struct sysctlnode))) 257 return (EINVAL); 258 259 sz = 0; 260 for (ni = 0; ni < clen; ni++) { 261 memset(&buf[0], 0, sizeof(buf)); 262 if (node != NULL && 263 node->sysctl_num != sysctl_usermib[ni].sysctl_num) 264 continue; 265 d1->descr_num = sysctl_usermib[ni].sysctl_num; 266 d1->descr_ver = sysctl_usermib[ni].sysctl_ver; 267 if (sysctl_usermib[ni].sysctl_desc == NULL) 268 d1->descr_len = 1; 269 else { 270 strncpy(d1->descr_str, 271 sysctl_usermib[ni].sysctl_desc, 272 sizeof(buf) - sizeof(*d1)); 273 buf[sizeof(buf) - 1] = '\0'; 274 d1->descr_len = strlen(d1->descr_str) + 1; 275 } 276 d = (size_t)__sysc_desc_adv(NULL, d1->descr_len); 277 if (d2 != NULL) 278 memcpy(d2, d1, d); 279 sz += d; 280 if (node != NULL) 281 break; 282 } 283 *oldlenp = sz; 284 if (sz == 0 && node != NULL) 285 return (ENOENT); 286 return (0); 287 288 } 289 290 /* 291 * none of these nodes are writable 292 */ 293 if (newp != NULL || newlen != 0) 294 return (EPERM); 295 296 node = &sysctl_usermib[0]; 297 for (ni = 0; ni < clen; ni++) 298 if (name[0] == node[ni].sysctl_num) 299 break; 300 if (ni == clen) 301 return (EOPNOTSUPP); 302 303 node = &node[ni]; 304 if (node->sysctl_flags & CTLFLAG_IMMEDIATE) { 305 switch (SYSCTL_TYPE(node->sysctl_flags)) { 306 case CTLTYPE_INT: 307 newp = &node->sysctl_idata; 308 break; 309 case CTLTYPE_QUAD: 310 newp = &node->sysctl_qdata; 311 break; 312 default: 313 return (EINVAL); 314 } 315 } 316 else 317 newp = node->sysctl_data; 318 319 l = MIN(l, node->sysctl_size); 320 if (oldp != NULL) 321 memcpy(oldp, newp, l); 322 *oldlenp = node->sysctl_size; 323 324 return (0); 325 } 326 327 static size_t 328 __cvt_node_out(uint v, const struct sysctlnode *n, void **o, size_t *l) 329 { 330 struct sysctlnode0 node0; 331 const void *src = n; 332 size_t sz; 333 334 switch (v) { 335 case SYSCTL_VERS_0: 336 memset(&node0, 0, sizeof(node0)); 337 node0.sysctl0_flags = n->sysctl_flags; 338 node0.sysctl0_num = n->sysctl_num; 339 node0.sysctl0_size = n->sysctl_size; 340 memcpy(node0.sysctl0_name, n->sysctl_name, SYSCTL_NAMELEN); 341 node0.sysctl0_csize = n->sysctl_csize; 342 node0.sysctl0_clen = n->sysctl_clen; 343 node0.sysctl0_child = NULL; 344 node0.sysctl0_alias = n->sysctl_alias; 345 node0.sysctl0_idata = n->sysctl_idata; 346 node0.sysctl0_qdata = n->sysctl_qdata; 347 node0.sysctl0_data = n->sysctl_data; 348 node0.sysctl0_func = NULL; 349 node0.sysctl0_parent = NULL; 350 node0.sysctl0_ver = n->sysctl_ver; 351 node0.sysctl0_flags &= ~SYSCTL_VERS_MASK; 352 node0.sysctl0_flags |= SYSCTL_VERS_0; 353 src = &node0; 354 sz = sizeof(node0); 355 break; 356 357 #if (SYSCTL_VERSION != SYSCTL_VERS_1) 358 #error __cvt_node_out: no support for SYSCTL_VERSION 359 #endif /* (SYSCTL_VERSION != SYSCTL_VERS_1) */ 360 361 case SYSCTL_VERSION: 362 sz = sizeof(struct sysctlnode); 363 break; 364 default: 365 sz = 0; 366 break; 367 } 368 369 if (sz > 0 && *o != NULL && *l >= sz) { 370 memcpy(*o, src, sz); 371 *o = sz + (caddr_t)*o; 372 *l -= sz; 373 } 374 375 return(sz); 376 } 377