1 /* $NetBSD: kern_sysctl.c,v 1.215 2008/02/29 02:28:35 matt Exp $ */ 2 3 /*- 4 * Copyright (c) 2003, 2007, 2008 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Andrew Brown. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 /*- 40 * Copyright (c) 1982, 1986, 1989, 1993 41 * The Regents of the University of California. All rights reserved. 42 * 43 * This code is derived from software contributed to Berkeley by 44 * Mike Karels at Berkeley Software Design, Inc. 45 * 46 * Redistribution and use in source and binary forms, with or without 47 * modification, are permitted provided that the following conditions 48 * are met: 49 * 1. Redistributions of source code must retain the above copyright 50 * notice, this list of conditions and the following disclaimer. 51 * 2. Redistributions in binary form must reproduce the above copyright 52 * notice, this list of conditions and the following disclaimer in the 53 * documentation and/or other materials provided with the distribution. 54 * 3. Neither the name of the University nor the names of its contributors 55 * may be used to endorse or promote products derived from this software 56 * without specific prior written permission. 57 * 58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 68 * SUCH DAMAGE. 69 * 70 * @(#)kern_sysctl.c 8.9 (Berkeley) 5/20/95 71 */ 72 73 /* 74 * sysctl system call. 75 */ 76 77 #include <sys/cdefs.h> 78 __KERNEL_RCSID(0, "$NetBSD: kern_sysctl.c,v 1.215 2008/02/29 02:28:35 matt Exp $"); 79 80 #include "opt_defcorename.h" 81 #include "ksyms.h" 82 83 #include <sys/param.h> 84 #define __COMPAT_SYSCTL 85 #include <sys/sysctl.h> 86 #include <sys/systm.h> 87 #include <sys/buf.h> 88 #include <sys/ksyms.h> 89 #include <sys/malloc.h> 90 #include <sys/mount.h> 91 #include <sys/syscallargs.h> 92 #include <sys/kauth.h> 93 #include <sys/ktrace.h> 94 #include <machine/stdarg.h> 95 96 #define MAXDESCLEN 1024 97 MALLOC_DEFINE(M_SYSCTLNODE, "sysctlnode", "sysctl node structures"); 98 MALLOC_DEFINE(M_SYSCTLDATA, "sysctldata", "misc sysctl data"); 99 100 static int sysctl_mmap(SYSCTLFN_PROTO); 101 static int sysctl_alloc(struct sysctlnode *, int); 102 static int sysctl_realloc(struct sysctlnode *); 103 104 static int sysctl_cvt_in(struct lwp *, int *, const void *, size_t, 105 struct sysctlnode *); 106 static int sysctl_cvt_out(struct lwp *, int, const struct sysctlnode *, 107 void *, size_t, size_t *); 108 109 static int sysctl_log_add(struct sysctllog **, const struct sysctlnode *); 110 static int sysctl_log_realloc(struct sysctllog *); 111 112 struct sysctllog { 113 const struct sysctlnode *log_root; 114 int *log_num; 115 int log_size, log_left; 116 }; 117 118 /* 119 * the "root" of the new sysctl tree 120 */ 121 struct sysctlnode sysctl_root = { 122 .sysctl_flags = SYSCTL_VERSION| 123 CTLFLAG_ROOT|CTLFLAG_READWRITE| 124 CTLTYPE_NODE, 125 .sysctl_num = 0, 126 /* 127 * XXX once all ports are on gcc3, we can get rid of this 128 * ugliness and simply make it into 129 * 130 * .sysctl_size = sizeof(struct sysctlnode), 131 */ 132 sysc_init_field(_sysctl_size, sizeof(struct sysctlnode)), 133 .sysctl_name = "(root)", 134 }; 135 136 /* 137 * link set of functions that add nodes at boot time (see also 138 * sysctl_buildtree()) 139 */ 140 __link_set_decl(sysctl_funcs, sysctl_setup_func); 141 142 /* 143 * The `sysctl_treelock' is intended to serialize access to the sysctl 144 * tree. XXX This has serious problems; allocating memory and 145 * copying data out with the lock held is insane. 146 */ 147 krwlock_t sysctl_treelock; 148 149 /* 150 * Attributes stored in the kernel. 151 */ 152 char hostname[MAXHOSTNAMELEN]; 153 int hostnamelen; 154 155 char domainname[MAXHOSTNAMELEN]; 156 int domainnamelen; 157 158 long hostid; 159 160 #ifndef DEFCORENAME 161 #define DEFCORENAME "%n.core" 162 #endif 163 char defcorename[MAXPATHLEN] = DEFCORENAME; 164 165 /* 166 * ******************************************************************** 167 * Section 0: Some simple glue 168 * ******************************************************************** 169 * By wrapping copyin(), copyout(), and copyinstr() like this, we can 170 * stop caring about who's calling us and simplify some code a bunch. 171 * ******************************************************************** 172 */ 173 static inline int 174 sysctl_copyin(struct lwp *l, const void *uaddr, void *kaddr, size_t len) 175 { 176 int error; 177 178 if (l != NULL) { 179 error = copyin(uaddr, kaddr, len); 180 ktrmibio(-1, UIO_WRITE, uaddr, len, error); 181 } else { 182 error = kcopy(uaddr, kaddr, len); 183 } 184 185 return error; 186 } 187 188 static inline int 189 sysctl_copyout(struct lwp *l, const void *kaddr, void *uaddr, size_t len) 190 { 191 int error; 192 193 if (l != NULL) { 194 error = copyout(kaddr, uaddr, len); 195 ktrmibio(-1, UIO_READ, uaddr, len, error); 196 } else { 197 error = kcopy(kaddr, uaddr, len); 198 } 199 200 return error; 201 } 202 203 static inline int 204 sysctl_copyinstr(struct lwp *l, const void *uaddr, void *kaddr, 205 size_t len, size_t *done) 206 { 207 int error; 208 209 if (l != NULL) { 210 error = copyinstr(uaddr, kaddr, len, done); 211 ktrmibio(-1, UIO_WRITE, uaddr, len, error); 212 } else { 213 error = copystr(uaddr, kaddr, len, done); 214 } 215 216 return error; 217 } 218 219 /* 220 * ******************************************************************** 221 * Initialize sysctl subsystem. 222 * ******************************************************************** 223 */ 224 void 225 sysctl_init(void) 226 { 227 sysctl_setup_func * const *sysctl_setup, f; 228 229 rw_init(&sysctl_treelock); 230 231 /* 232 * dynamic mib numbers start here 233 */ 234 sysctl_root.sysctl_num = CREATE_BASE; 235 236 __link_set_foreach(sysctl_setup, sysctl_funcs) { 237 /* 238 * XXX - why do i have to coerce the pointers like this? 239 */ 240 f = (void*)*sysctl_setup; 241 (*f)(NULL); 242 } 243 244 /* 245 * setting this means no more permanent nodes can be added, 246 * trees that claim to be readonly at the root now are, and if 247 * the main tree is readonly, *everything* is. 248 */ 249 sysctl_root.sysctl_flags |= CTLFLAG_PERMANENT; 250 251 } 252 253 /* 254 * ******************************************************************** 255 * The main native sysctl system call itself. 256 * ******************************************************************** 257 */ 258 int 259 sys___sysctl(struct lwp *l, const struct sys___sysctl_args *uap, register_t *retval) 260 { 261 /* { 262 syscallarg(const int *) name; 263 syscallarg(u_int) namelen; 264 syscallarg(void *) old; 265 syscallarg(size_t *) oldlenp; 266 syscallarg(const void *) new; 267 syscallarg(size_t) newlen; 268 } */ 269 int error, nerror, name[CTL_MAXNAME]; 270 size_t oldlen, savelen, *oldlenp; 271 272 /* 273 * get oldlen 274 */ 275 oldlen = 0; 276 oldlenp = SCARG(uap, oldlenp); 277 if (oldlenp != NULL) { 278 error = copyin(oldlenp, &oldlen, sizeof(oldlen)); 279 if (error) 280 return (error); 281 } 282 savelen = oldlen; 283 284 /* 285 * top-level sysctl names may or may not be non-terminal, but 286 * we don't care 287 */ 288 if (SCARG(uap, namelen) > CTL_MAXNAME || SCARG(uap, namelen) < 1) 289 return (EINVAL); 290 error = copyin(SCARG(uap, name), &name, 291 SCARG(uap, namelen) * sizeof(int)); 292 if (error) 293 return (error); 294 295 ktrmib(name, SCARG(uap, namelen)); 296 297 sysctl_lock(SCARG(uap, new) != NULL); 298 299 /* 300 * do sysctl work (NULL means main built-in default tree) 301 */ 302 error = sysctl_dispatch(&name[0], SCARG(uap, namelen), 303 SCARG(uap, old), &oldlen, 304 SCARG(uap, new), SCARG(uap, newlen), 305 &name[0], l, NULL); 306 307 /* 308 * release the sysctl lock 309 */ 310 sysctl_unlock(); 311 312 /* 313 * set caller's oldlen to new value even in the face of an 314 * error (if this gets an error and they didn't have one, they 315 * get this one) 316 */ 317 if (oldlenp) { 318 nerror = copyout(&oldlen, oldlenp, sizeof(oldlen)); 319 if (error == 0) 320 error = nerror; 321 } 322 323 /* 324 * if the only problem is that we weren't given enough space, 325 * that's an ENOMEM error 326 */ 327 if (error == 0 && SCARG(uap, old) != NULL && savelen < oldlen) 328 error = ENOMEM; 329 330 return (error); 331 } 332 333 /* 334 * ******************************************************************** 335 * Section 1: How the tree is used 336 * ******************************************************************** 337 * Implementations of sysctl for emulations should typically need only 338 * these three functions in this order: lock the tree, dispatch 339 * request into it, unlock the tree. 340 * ******************************************************************** 341 */ 342 void 343 sysctl_lock(bool write) 344 { 345 346 if (write) { 347 rw_enter(&sysctl_treelock, RW_WRITER); 348 curlwp->l_pflag |= LP_SYSCTLWRITE; 349 } else { 350 rw_enter(&sysctl_treelock, RW_READER); 351 curlwp->l_pflag &= ~LP_SYSCTLWRITE; 352 } 353 } 354 355 void 356 sysctl_relock(void) 357 { 358 359 if ((curlwp->l_pflag & LP_SYSCTLWRITE) != 0) { 360 rw_enter(&sysctl_treelock, RW_WRITER); 361 } else { 362 rw_enter(&sysctl_treelock, RW_READER); 363 } 364 } 365 366 /* 367 * ******************************************************************** 368 * the main sysctl dispatch routine. scans the given tree and picks a 369 * function to call based on what it finds. 370 * ******************************************************************** 371 */ 372 int 373 sysctl_dispatch(SYSCTLFN_ARGS) 374 { 375 int error; 376 sysctlfn fn; 377 int ni; 378 379 KASSERT(rw_lock_held(&sysctl_treelock)); 380 381 if (rnode && SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) { 382 printf("sysctl_dispatch: rnode %p wrong version\n", rnode); 383 error = EINVAL; 384 goto out; 385 } 386 387 fn = NULL; 388 error = sysctl_locate(l, name, namelen, &rnode, &ni); 389 390 if (rnode->sysctl_func != NULL) { 391 /* 392 * the node we ended up at has a function, so call it. it can 393 * hand off to query or create if it wants to. 394 */ 395 fn = rnode->sysctl_func; 396 } else if (error == 0) { 397 /* 398 * we found the node they were looking for, so do a lookup. 399 */ 400 fn = (sysctlfn)sysctl_lookup; /* XXX may write to rnode */ 401 } else if (error == ENOENT && (ni + 1) == namelen && name[ni] < 0) { 402 /* 403 * prospective parent node found, but the terminal node was 404 * not. generic operations associate with the parent. 405 */ 406 switch (name[ni]) { 407 case CTL_QUERY: 408 fn = sysctl_query; 409 break; 410 case CTL_CREATE: 411 #if NKSYMS > 0 412 case CTL_CREATESYM: 413 #endif /* NKSYMS > 0 */ 414 if (newp == NULL) { 415 error = EINVAL; 416 break; 417 } 418 KASSERT(rw_write_held(&sysctl_treelock)); 419 fn = (sysctlfn)sysctl_create; /* we own the rnode */ 420 break; 421 case CTL_DESTROY: 422 if (newp == NULL) { 423 error = EINVAL; 424 break; 425 } 426 KASSERT(rw_write_held(&sysctl_treelock)); 427 fn = (sysctlfn)sysctl_destroy; /* we own the rnode */ 428 break; 429 case CTL_MMAP: 430 fn = (sysctlfn)sysctl_mmap; /* we own the rnode */ 431 break; 432 case CTL_DESCRIBE: 433 fn = sysctl_describe; 434 break; 435 default: 436 error = EOPNOTSUPP; 437 break; 438 } 439 } 440 441 /* 442 * after all of that, maybe we found someone who knows how to 443 * get us what we want? 444 */ 445 if (fn != NULL) 446 error = (*fn)(name + ni, namelen - ni, oldp, oldlenp, 447 newp, newlen, name, l, rnode); 448 else if (error == 0) 449 error = EOPNOTSUPP; 450 451 out: 452 return (error); 453 } 454 455 /* 456 * ******************************************************************** 457 * Releases the tree lock. 458 * ******************************************************************** 459 */ 460 void 461 sysctl_unlock(void) 462 { 463 464 rw_exit(&sysctl_treelock); 465 } 466 467 /* 468 * ******************************************************************** 469 * Section 2: The main tree interfaces 470 * ******************************************************************** 471 * This is how sysctl_dispatch() does its work, and you can too, by 472 * calling these routines from helpers (though typically only 473 * sysctl_lookup() will be used). The tree MUST BE LOCKED when these 474 * are called. 475 * ******************************************************************** 476 */ 477 478 /* 479 * sysctl_locate -- Finds the node matching the given mib under the 480 * given tree (via rv). If no tree is given, we fall back to the 481 * native tree. The current process (via l) is used for access 482 * control on the tree (some nodes may be traversable only by root) and 483 * on return, nip will show how many numbers in the mib were consumed. 484 */ 485 int 486 sysctl_locate(struct lwp *l, const int *name, u_int namelen, 487 const struct sysctlnode **rnode, int *nip) 488 { 489 const struct sysctlnode *node, *pnode; 490 int tn, si, ni, error, alias; 491 492 KASSERT(rw_lock_held(&sysctl_treelock)); 493 494 /* 495 * basic checks and setup 496 */ 497 if (*rnode == NULL) 498 *rnode = &sysctl_root; 499 if (nip) 500 *nip = 0; 501 if (namelen == 0) 502 return (0); 503 504 /* 505 * search starts from "root" 506 */ 507 pnode = *rnode; 508 if (SYSCTL_VERS(pnode->sysctl_flags) != SYSCTL_VERSION) { 509 printf("sysctl_locate: pnode %p wrong version\n", pnode); 510 return (EINVAL); 511 } 512 node = pnode->sysctl_child; 513 error = 0; 514 515 /* 516 * scan for node to which new node should be attached 517 */ 518 for (ni = 0; ni < namelen; ni++) { 519 /* 520 * walked off bottom of tree 521 */ 522 if (node == NULL) { 523 if (SYSCTL_TYPE(pnode->sysctl_flags) == CTLTYPE_NODE) 524 error = ENOENT; 525 else 526 error = ENOTDIR; 527 break; 528 } 529 /* 530 * can anyone traverse this node or only root? 531 */ 532 if (l != NULL && (pnode->sysctl_flags & CTLFLAG_PRIVATE) && 533 (error = kauth_authorize_system(l->l_cred, 534 KAUTH_SYSTEM_SYSCTL, KAUTH_REQ_SYSTEM_SYSCTL_PRVT, 535 NULL, NULL, NULL)) != 0) 536 return (error); 537 /* 538 * find a child node with the right number 539 */ 540 tn = name[ni]; 541 alias = 0; 542 543 si = 0; 544 /* 545 * Note: ANYNUMBER only matches positive integers. 546 * Since ANYNUMBER is only permitted on single-node 547 * sub-trees (eg proc), check before the loop and skip 548 * it if we can. 549 */ 550 if ((node[si].sysctl_flags & CTLFLAG_ANYNUMBER) && (tn >= 0)) 551 goto foundit; 552 for (; si < pnode->sysctl_clen; si++) { 553 if (node[si].sysctl_num == tn) { 554 if (node[si].sysctl_flags & CTLFLAG_ALIAS) { 555 if (alias++ == 4) 556 break; 557 else { 558 tn = node[si].sysctl_alias; 559 si = -1; 560 } 561 } else 562 goto foundit; 563 } 564 } 565 /* 566 * if we ran off the end, it obviously doesn't exist 567 */ 568 error = ENOENT; 569 break; 570 571 /* 572 * so far so good, move on down the line 573 */ 574 foundit: 575 pnode = &node[si]; 576 if (SYSCTL_TYPE(pnode->sysctl_flags) == CTLTYPE_NODE) 577 node = node[si].sysctl_child; 578 else 579 node = NULL; 580 } 581 582 *rnode = pnode; 583 if (nip) 584 *nip = ni; 585 586 return (error); 587 } 588 589 /* 590 * sysctl_query -- The auto-discovery engine. Copies out the structs 591 * describing nodes under the given node and handles overlay trees. 592 */ 593 int 594 sysctl_query(SYSCTLFN_ARGS) 595 { 596 int error, ni, elim, v; 597 size_t out, left, t; 598 const struct sysctlnode *enode, *onode; 599 struct sysctlnode qnode; 600 601 KASSERT(rw_lock_held(&sysctl_treelock)); 602 603 if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) { 604 printf("sysctl_query: rnode %p wrong version\n", rnode); 605 return (EINVAL); 606 } 607 608 if (SYSCTL_TYPE(rnode->sysctl_flags) != CTLTYPE_NODE) 609 return (ENOTDIR); 610 if (namelen != 1 || name[0] != CTL_QUERY) 611 return (EINVAL); 612 613 error = 0; 614 out = 0; 615 left = *oldlenp; 616 elim = 0; 617 enode = NULL; 618 619 /* 620 * translate the given request to a current node 621 */ 622 error = sysctl_cvt_in(l, &v, newp, newlen, &qnode); 623 if (error) 624 return (error); 625 626 /* 627 * if the request specifies a version, check it 628 */ 629 if (qnode.sysctl_ver != 0) { 630 enode = rnode; 631 if (qnode.sysctl_ver != enode->sysctl_ver && 632 qnode.sysctl_ver != sysctl_rootof(enode)->sysctl_ver) 633 return (EINVAL); 634 } 635 636 /* 637 * process has overlay tree 638 */ 639 if (l && l->l_proc->p_emul->e_sysctlovly) { 640 enode = l->l_proc->p_emul->e_sysctlovly; 641 elim = (name - oname); 642 error = sysctl_locate(l, oname, elim, &enode, NULL); 643 if (error == 0) { 644 /* ah, found parent in overlay */ 645 elim = enode->sysctl_clen; 646 enode = enode->sysctl_child; 647 } else { 648 error = 0; 649 elim = 0; 650 enode = NULL; 651 } 652 } 653 654 for (ni = 0; ni < rnode->sysctl_clen; ni++) { 655 onode = &rnode->sysctl_child[ni]; 656 if (enode && enode->sysctl_num == onode->sysctl_num) { 657 if (SYSCTL_TYPE(enode->sysctl_flags) != CTLTYPE_NODE) 658 onode = enode; 659 if (--elim > 0) 660 enode++; 661 else 662 enode = NULL; 663 } 664 error = sysctl_cvt_out(l, v, onode, oldp, left, &t); 665 if (error) 666 return (error); 667 if (oldp != NULL) 668 oldp = (char*)oldp + t; 669 out += t; 670 left -= MIN(left, t); 671 } 672 673 /* 674 * overlay trees *MUST* be entirely consumed 675 */ 676 KASSERT(enode == NULL); 677 678 *oldlenp = out; 679 680 return (error); 681 } 682 683 /* 684 * sysctl_create -- Adds a node (the description of which is taken 685 * from newp) to the tree, returning a copy of it in the space pointed 686 * to by oldp. In the event that the requested slot is already taken 687 * (either by name or by number), the offending node is returned 688 * instead. Yes, this is complex, but we want to make sure everything 689 * is proper. 690 */ 691 #ifdef SYSCTL_DEBUG_CREATE 692 int _sysctl_create(SYSCTLFN_ARGS); 693 int 694 _sysctl_create(SYSCTLFN_ARGS) 695 #else 696 int 697 sysctl_create(SYSCTLFN_ARGS) 698 #endif 699 { 700 struct sysctlnode nnode, *node, *pnode; 701 int error, ni, at, nm, type, sz, flags, anum, v; 702 void *own; 703 704 KASSERT(rw_write_held(&sysctl_treelock)); 705 706 error = 0; 707 own = NULL; 708 anum = -1; 709 710 if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) { 711 printf("sysctl_create: rnode %p wrong version\n", rnode); 712 return (EINVAL); 713 } 714 715 if (namelen != 1 || (name[namelen - 1] != CTL_CREATE 716 #if NKSYMS > 0 717 && name[namelen - 1] != CTL_CREATESYM 718 #endif /* NKSYMS > 0 */ 719 )) 720 return (EINVAL); 721 722 /* 723 * processes can only add nodes at securelevel 0, must be 724 * root, and can't add nodes to a parent that's not writeable 725 */ 726 if (l != NULL) { 727 #ifndef SYSCTL_DISALLOW_CREATE 728 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SYSCTL, 729 KAUTH_REQ_SYSTEM_SYSCTL_ADD, NULL, NULL, NULL); 730 if (error) 731 return (error); 732 if (!(rnode->sysctl_flags & CTLFLAG_READWRITE)) 733 #endif /* SYSCTL_DISALLOW_CREATE */ 734 return (EPERM); 735 } 736 737 /* 738 * nothing can add a node if: 739 * we've finished initial set up and 740 * the tree itself is not writeable or 741 * the entire sysctl system is not writeable 742 */ 743 if ((sysctl_root.sysctl_flags & CTLFLAG_PERMANENT) && 744 (!(sysctl_rootof(rnode)->sysctl_flags & CTLFLAG_READWRITE) || 745 !(sysctl_root.sysctl_flags & CTLFLAG_READWRITE))) 746 return (EPERM); 747 748 /* 749 * it must be a "node", not a "int" or something 750 */ 751 if (SYSCTL_TYPE(rnode->sysctl_flags) != CTLTYPE_NODE) 752 return (ENOTDIR); 753 if (rnode->sysctl_flags & CTLFLAG_ALIAS) { 754 printf("sysctl_create: attempt to add node to aliased " 755 "node %p\n", rnode); 756 return (EINVAL); 757 } 758 pnode = __UNCONST(rnode); /* we are adding children to this node */ 759 760 if (newp == NULL) 761 return (EINVAL); 762 error = sysctl_cvt_in(l, &v, newp, newlen, &nnode); 763 if (error) 764 return (error); 765 766 /* 767 * nodes passed in don't *have* parents 768 */ 769 if (nnode.sysctl_parent != NULL) 770 return (EINVAL); 771 772 /* 773 * if we are indeed adding it, it should be a "good" name and 774 * number 775 */ 776 nm = nnode.sysctl_num; 777 #if NKSYMS > 0 778 if (nm == CTL_CREATESYM) 779 nm = CTL_CREATE; 780 #endif /* NKSYMS > 0 */ 781 if (nm < 0 && nm != CTL_CREATE) 782 return (EINVAL); 783 sz = 0; 784 785 /* 786 * the name can't start with a digit 787 */ 788 if (nnode.sysctl_name[sz] >= '0' && 789 nnode.sysctl_name[sz] <= '9') 790 return (EINVAL); 791 792 /* 793 * the name must be only alphanumerics or - or _, longer than 794 * 0 bytes and less that SYSCTL_NAMELEN 795 */ 796 while (sz < SYSCTL_NAMELEN && nnode.sysctl_name[sz] != '\0') { 797 if ((nnode.sysctl_name[sz] >= '0' && 798 nnode.sysctl_name[sz] <= '9') || 799 (nnode.sysctl_name[sz] >= 'A' && 800 nnode.sysctl_name[sz] <= 'Z') || 801 (nnode.sysctl_name[sz] >= 'a' && 802 nnode.sysctl_name[sz] <= 'z') || 803 nnode.sysctl_name[sz] == '-' || 804 nnode.sysctl_name[sz] == '_') 805 sz++; 806 else 807 return (EINVAL); 808 } 809 if (sz == 0 || sz == SYSCTL_NAMELEN) 810 return (EINVAL); 811 812 /* 813 * various checks revolve around size vs type, etc 814 */ 815 type = SYSCTL_TYPE(nnode.sysctl_flags); 816 flags = SYSCTL_FLAGS(nnode.sysctl_flags); 817 sz = nnode.sysctl_size; 818 819 /* 820 * find out if there's a collision, and if so, let the caller 821 * know what they collided with 822 */ 823 node = pnode->sysctl_child; 824 at = 0; 825 if (node) { 826 if ((flags | node->sysctl_flags) & CTLFLAG_ANYNUMBER) 827 /* No siblings for a CTLFLAG_ANYNUMBER node */ 828 return EINVAL; 829 for (ni = 0; ni < pnode->sysctl_clen; ni++) { 830 if (nm == node[ni].sysctl_num || 831 strcmp(nnode.sysctl_name, node[ni].sysctl_name) == 0) { 832 /* 833 * ignore error here, since we 834 * are already fixed on EEXIST 835 */ 836 (void)sysctl_cvt_out(l, v, &node[ni], oldp, 837 *oldlenp, oldlenp); 838 return (EEXIST); 839 } 840 if (nm > node[ni].sysctl_num) 841 at++; 842 } 843 } 844 845 /* 846 * use sysctl_ver to add to the tree iff it hasn't changed 847 */ 848 if (nnode.sysctl_ver != 0) { 849 /* 850 * a specified value must match either the parent 851 * node's version or the root node's version 852 */ 853 if (nnode.sysctl_ver != sysctl_rootof(rnode)->sysctl_ver && 854 nnode.sysctl_ver != rnode->sysctl_ver) { 855 return (EINVAL); 856 } 857 } 858 859 /* 860 * only the kernel can assign functions to entries 861 */ 862 if (l != NULL && nnode.sysctl_func != NULL) 863 return (EPERM); 864 865 /* 866 * only the kernel can create permanent entries, and only then 867 * before the kernel is finished setting itself up 868 */ 869 if (l != NULL && (flags & ~SYSCTL_USERFLAGS)) 870 return (EPERM); 871 if ((flags & CTLFLAG_PERMANENT) & 872 (sysctl_root.sysctl_flags & CTLFLAG_PERMANENT)) 873 return (EPERM); 874 if ((flags & (CTLFLAG_OWNDATA | CTLFLAG_IMMEDIATE)) == 875 (CTLFLAG_OWNDATA | CTLFLAG_IMMEDIATE)) 876 return (EINVAL); 877 if ((flags & CTLFLAG_IMMEDIATE) && 878 type != CTLTYPE_INT && type != CTLTYPE_QUAD && type != CTLTYPE_BOOL) 879 return (EINVAL); 880 881 /* 882 * check size, or set it if unset and we can figure it out. 883 * kernel created nodes are allowed to have a function instead 884 * of a size (or a data pointer). 885 */ 886 switch (type) { 887 case CTLTYPE_NODE: 888 /* 889 * only *i* can assert the size of a node 890 */ 891 if (flags & CTLFLAG_ALIAS) { 892 anum = nnode.sysctl_alias; 893 if (anum < 0) 894 return (EINVAL); 895 nnode.sysctl_alias = 0; 896 } 897 if (sz != 0 || nnode.sysctl_data != NULL) 898 return (EINVAL); 899 if (nnode.sysctl_csize != 0 || 900 nnode.sysctl_clen != 0 || 901 nnode.sysctl_child != 0) 902 return (EINVAL); 903 if (flags & CTLFLAG_OWNDATA) 904 return (EINVAL); 905 sz = sizeof(struct sysctlnode); 906 break; 907 case CTLTYPE_INT: 908 /* 909 * since an int is an int, if the size is not given or 910 * is wrong, we can "int-uit" it. 911 */ 912 if (sz != 0 && sz != sizeof(int)) 913 return (EINVAL); 914 sz = sizeof(int); 915 break; 916 case CTLTYPE_STRING: 917 /* 918 * strings are a little more tricky 919 */ 920 if (sz == 0) { 921 if (l == NULL) { 922 if (nnode.sysctl_func == NULL) { 923 if (nnode.sysctl_data == NULL) 924 return (EINVAL); 925 else 926 sz = strlen(nnode.sysctl_data) + 927 1; 928 } 929 } else if (nnode.sysctl_data == NULL && 930 flags & CTLFLAG_OWNDATA) { 931 return (EINVAL); 932 } else { 933 char *vp, *e; 934 size_t s; 935 936 /* 937 * we want a rough idea of what the 938 * size is now 939 */ 940 vp = malloc(PAGE_SIZE, M_SYSCTLDATA, 941 M_WAITOK|M_CANFAIL); 942 if (vp == NULL) 943 return (ENOMEM); 944 e = nnode.sysctl_data; 945 do { 946 error = copyinstr(e, vp, PAGE_SIZE, &s); 947 if (error) { 948 if (error != ENAMETOOLONG) { 949 free(vp, M_SYSCTLDATA); 950 return (error); 951 } 952 e += PAGE_SIZE; 953 if ((e - 32 * PAGE_SIZE) > 954 (char*)nnode.sysctl_data) { 955 free(vp, M_SYSCTLDATA); 956 return (ERANGE); 957 } 958 } 959 } while (error != 0); 960 sz = s + (e - (char*)nnode.sysctl_data); 961 free(vp, M_SYSCTLDATA); 962 } 963 } 964 break; 965 case CTLTYPE_QUAD: 966 if (sz != 0 && sz != sizeof(u_quad_t)) 967 return (EINVAL); 968 sz = sizeof(u_quad_t); 969 break; 970 case CTLTYPE_BOOL: 971 /* 972 * since an bool is an bool, if the size is not given or 973 * is wrong, we can "intuit" it. 974 */ 975 if (sz != 0 && sz != sizeof(bool)) 976 return (EINVAL); 977 sz = sizeof(bool); 978 break; 979 case CTLTYPE_STRUCT: 980 if (sz == 0) { 981 if (l != NULL || nnode.sysctl_func == NULL) 982 return (EINVAL); 983 if (flags & CTLFLAG_OWNDATA) 984 return (EINVAL); 985 } 986 break; 987 default: 988 return (EINVAL); 989 } 990 991 /* 992 * at this point, if sz is zero, we *must* have a 993 * function to go with it and we can't own it. 994 */ 995 996 /* 997 * l ptr own 998 * 0 0 0 -> EINVAL (if no func) 999 * 0 0 1 -> own 1000 * 0 1 0 -> kptr 1001 * 0 1 1 -> kptr 1002 * 1 0 0 -> EINVAL 1003 * 1 0 1 -> own 1004 * 1 1 0 -> kptr, no own (fault on lookup) 1005 * 1 1 1 -> uptr, own 1006 */ 1007 if (type != CTLTYPE_NODE) { 1008 if (sz != 0) { 1009 if (flags & CTLFLAG_OWNDATA) { 1010 own = malloc(sz, M_SYSCTLDATA, 1011 M_WAITOK|M_CANFAIL); 1012 if (own == NULL) 1013 return ENOMEM; 1014 if (nnode.sysctl_data == NULL) 1015 memset(own, 0, sz); 1016 else { 1017 error = sysctl_copyin(l, 1018 nnode.sysctl_data, own, sz); 1019 if (error != 0) { 1020 free(own, M_SYSCTLDATA); 1021 return (error); 1022 } 1023 } 1024 } else if ((nnode.sysctl_data != NULL) && 1025 !(flags & CTLFLAG_IMMEDIATE)) { 1026 #if NKSYMS > 0 1027 if (name[namelen - 1] == CTL_CREATESYM) { 1028 char symname[128]; /* XXX enough? */ 1029 u_long symaddr; 1030 size_t symlen; 1031 1032 error = sysctl_copyinstr(l, 1033 nnode.sysctl_data, symname, 1034 sizeof(symname), &symlen); 1035 if (error) 1036 return (error); 1037 error = ksyms_getval(NULL, symname, 1038 &symaddr, KSYMS_EXTERN); 1039 if (error) 1040 return (error); /* EINVAL? */ 1041 nnode.sysctl_data = (void*)symaddr; 1042 } 1043 #endif /* NKSYMS > 0 */ 1044 /* 1045 * Ideally, we'd like to verify here 1046 * that this address is acceptable, 1047 * but... 1048 * 1049 * - it might be valid now, only to 1050 * become invalid later 1051 * 1052 * - it might be invalid only for the 1053 * moment and valid later 1054 * 1055 * - or something else. 1056 * 1057 * Since we can't get a good answer, 1058 * we'll just accept the address as 1059 * given, and fault on individual 1060 * lookups. 1061 */ 1062 } 1063 } else if (nnode.sysctl_func == NULL) 1064 return (EINVAL); 1065 } 1066 1067 /* 1068 * a process can't assign a function to a node, and the kernel 1069 * can't create a node that has no function or data. 1070 * (XXX somewhat redundant check) 1071 */ 1072 if (l != NULL || nnode.sysctl_func == NULL) { 1073 if (type != CTLTYPE_NODE && 1074 nnode.sysctl_data == NULL && 1075 !(flags & CTLFLAG_IMMEDIATE) && 1076 own == NULL) 1077 return (EINVAL); 1078 } 1079 1080 #ifdef SYSCTL_DISALLOW_KWRITE 1081 /* 1082 * a process can't create a writable node unless it refers to 1083 * new data. 1084 */ 1085 if (l != NULL && own == NULL && type != CTLTYPE_NODE && 1086 (flags & CTLFLAG_READWRITE) != CTLFLAG_READONLY && 1087 !(flags & CTLFLAG_IMMEDIATE)) 1088 return (EPERM); 1089 #endif /* SYSCTL_DISALLOW_KWRITE */ 1090 1091 /* 1092 * make sure there's somewhere to put the new stuff. 1093 */ 1094 if (pnode->sysctl_child == NULL) { 1095 if (flags & CTLFLAG_ANYNUMBER) 1096 error = sysctl_alloc(pnode, 1); 1097 else 1098 error = sysctl_alloc(pnode, 0); 1099 if (error) { 1100 if (own != NULL) 1101 free(own, M_SYSCTLDATA); 1102 return (error); 1103 } 1104 } 1105 node = pnode->sysctl_child; 1106 1107 /* 1108 * no collisions, so pick a good dynamic number if we need to. 1109 */ 1110 if (nm == CTL_CREATE) { 1111 nm = ++sysctl_root.sysctl_num; 1112 for (ni = 0; ni < pnode->sysctl_clen; ni++) { 1113 if (nm == node[ni].sysctl_num) { 1114 nm++; 1115 ni = -1; 1116 } else if (nm > node[ni].sysctl_num) 1117 at = ni + 1; 1118 } 1119 } 1120 1121 /* 1122 * oops...ran out of space 1123 */ 1124 if (pnode->sysctl_clen == pnode->sysctl_csize) { 1125 error = sysctl_realloc(pnode); 1126 if (error) { 1127 if (own != NULL) 1128 free(own, M_SYSCTLDATA); 1129 return (error); 1130 } 1131 node = pnode->sysctl_child; 1132 } 1133 1134 /* 1135 * insert new node data 1136 */ 1137 if (at < pnode->sysctl_clen) { 1138 int t; 1139 1140 /* 1141 * move the nodes that should come after the new one 1142 */ 1143 memmove(&node[at + 1], &node[at], 1144 (pnode->sysctl_clen - at) * sizeof(struct sysctlnode)); 1145 memset(&node[at], 0, sizeof(struct sysctlnode)); 1146 node[at].sysctl_parent = pnode; 1147 /* 1148 * and...reparent any children of any moved nodes 1149 */ 1150 for (ni = at; ni <= pnode->sysctl_clen; ni++) 1151 if (SYSCTL_TYPE(node[ni].sysctl_flags) == CTLTYPE_NODE) 1152 for (t = 0; t < node[ni].sysctl_clen; t++) 1153 node[ni].sysctl_child[t].sysctl_parent = 1154 &node[ni]; 1155 } 1156 node = &node[at]; 1157 pnode->sysctl_clen++; 1158 1159 strlcpy(node->sysctl_name, nnode.sysctl_name, 1160 sizeof(node->sysctl_name)); 1161 node->sysctl_num = nm; 1162 node->sysctl_size = sz; 1163 node->sysctl_flags = SYSCTL_VERSION|type|flags; /* XXX other trees */ 1164 node->sysctl_csize = 0; 1165 node->sysctl_clen = 0; 1166 if (own) { 1167 node->sysctl_data = own; 1168 node->sysctl_flags |= CTLFLAG_OWNDATA; 1169 } else if (flags & CTLFLAG_ALIAS) { 1170 node->sysctl_alias = anum; 1171 } else if (flags & CTLFLAG_IMMEDIATE) { 1172 switch (type) { 1173 case CTLTYPE_BOOL: 1174 node->sysctl_idata = nnode.sysctl_bdata; 1175 break; 1176 case CTLTYPE_INT: 1177 node->sysctl_idata = nnode.sysctl_idata; 1178 break; 1179 case CTLTYPE_QUAD: 1180 node->sysctl_qdata = nnode.sysctl_qdata; 1181 break; 1182 } 1183 } else { 1184 node->sysctl_data = nnode.sysctl_data; 1185 node->sysctl_flags &= ~CTLFLAG_OWNDATA; 1186 } 1187 node->sysctl_func = nnode.sysctl_func; 1188 node->sysctl_child = NULL; 1189 /* node->sysctl_parent should already be done */ 1190 1191 /* 1192 * update "version" on path to "root" 1193 */ 1194 for (; rnode->sysctl_parent != NULL; rnode = rnode->sysctl_parent) 1195 ; 1196 pnode = node; 1197 for (nm = rnode->sysctl_ver + 1; pnode != NULL; 1198 pnode = pnode->sysctl_parent) 1199 pnode->sysctl_ver = nm; 1200 1201 /* If this fails, the node is already added - the user won't know! */ 1202 error = sysctl_cvt_out(l, v, node, oldp, *oldlenp, oldlenp); 1203 1204 return (error); 1205 } 1206 1207 /* 1208 * ******************************************************************** 1209 * A wrapper around sysctl_create() that prints the thing we're trying 1210 * to add. 1211 * ******************************************************************** 1212 */ 1213 #ifdef SYSCTL_DEBUG_CREATE 1214 int 1215 sysctl_create(SYSCTLFN_ARGS) 1216 { 1217 const struct sysctlnode *node; 1218 int k, rc, ni, nl = namelen + (name - oname); 1219 1220 node = newp; 1221 1222 printf("namelen %d (", nl); 1223 for (ni = 0; ni < nl - 1; ni++) 1224 printf(" %d", oname[ni]); 1225 printf(" %d )\t[%s]\tflags %08x (%08x %d %zu)\n", 1226 k = node->sysctl_num, 1227 node->sysctl_name, 1228 node->sysctl_flags, 1229 SYSCTL_FLAGS(node->sysctl_flags), 1230 SYSCTL_TYPE(node->sysctl_flags), 1231 node->sysctl_size); 1232 1233 node = rnode; 1234 rc = _sysctl_create(SYSCTLFN_CALL(rnode)); 1235 1236 printf("sysctl_create("); 1237 for (ni = 0; ni < nl - 1; ni++) 1238 printf(" %d", oname[ni]); 1239 printf(" %d ) returned %d\n", k, rc); 1240 1241 return (rc); 1242 } 1243 #endif /* SYSCTL_DEBUG_CREATE */ 1244 1245 /* 1246 * sysctl_destroy -- Removes a node (as described by newp) from the 1247 * given tree, returning (if successful) a copy of the dead node in 1248 * oldp. Since we're removing stuff, there's not much to check. 1249 */ 1250 int 1251 sysctl_destroy(SYSCTLFN_ARGS) 1252 { 1253 struct sysctlnode *node, *pnode, onode, nnode; 1254 int ni, error, v; 1255 1256 KASSERT(rw_write_held(&sysctl_treelock)); 1257 1258 if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) { 1259 printf("sysctl_destroy: rnode %p wrong version\n", rnode); 1260 return (EINVAL); 1261 } 1262 1263 error = 0; 1264 1265 if (namelen != 1 || name[namelen - 1] != CTL_DESTROY) 1266 return (EINVAL); 1267 1268 /* 1269 * processes can only destroy nodes at securelevel 0, must be 1270 * root, and can't remove nodes from a parent that's not 1271 * writeable 1272 */ 1273 if (l != NULL) { 1274 #ifndef SYSCTL_DISALLOW_CREATE 1275 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SYSCTL, 1276 KAUTH_REQ_SYSTEM_SYSCTL_DELETE, NULL, NULL, NULL); 1277 if (error) 1278 return (error); 1279 if (!(rnode->sysctl_flags & CTLFLAG_READWRITE)) 1280 #endif /* SYSCTL_DISALLOW_CREATE */ 1281 return (EPERM); 1282 } 1283 1284 /* 1285 * nothing can remove a node if: 1286 * the node is permanent (checked later) or 1287 * the tree itself is not writeable or 1288 * the entire sysctl system is not writeable 1289 * 1290 * note that we ignore whether setup is complete or not, 1291 * because these rules always apply. 1292 */ 1293 if (!(sysctl_rootof(rnode)->sysctl_flags & CTLFLAG_READWRITE) || 1294 !(sysctl_root.sysctl_flags & CTLFLAG_READWRITE)) 1295 return (EPERM); 1296 1297 if (newp == NULL) 1298 return (EINVAL); 1299 error = sysctl_cvt_in(l, &v, newp, newlen, &nnode); 1300 if (error) 1301 return (error); 1302 memset(&onode, 0, sizeof(struct sysctlnode)); 1303 1304 node = rnode->sysctl_child; 1305 for (ni = 0; ni < rnode->sysctl_clen; ni++) { 1306 if (nnode.sysctl_num == node[ni].sysctl_num) { 1307 /* 1308 * if name specified, must match 1309 */ 1310 if (nnode.sysctl_name[0] != '\0' && 1311 strcmp(nnode.sysctl_name, node[ni].sysctl_name)) 1312 continue; 1313 /* 1314 * if version specified, must match 1315 */ 1316 if (nnode.sysctl_ver != 0 && 1317 nnode.sysctl_ver != node[ni].sysctl_ver) 1318 continue; 1319 /* 1320 * this must be the one 1321 */ 1322 break; 1323 } 1324 } 1325 if (ni == rnode->sysctl_clen) 1326 return (ENOENT); 1327 node = &node[ni]; 1328 pnode = node->sysctl_parent; 1329 1330 /* 1331 * if the kernel says permanent, it is, so there. nyah. 1332 */ 1333 if (SYSCTL_FLAGS(node->sysctl_flags) & CTLFLAG_PERMANENT) 1334 return (EPERM); 1335 1336 /* 1337 * can't delete non-empty nodes 1338 */ 1339 if (SYSCTL_TYPE(node->sysctl_flags) == CTLTYPE_NODE && 1340 node->sysctl_clen != 0) 1341 return (ENOTEMPTY); 1342 1343 /* 1344 * if the node "owns" data, release it now 1345 */ 1346 if (node->sysctl_flags & CTLFLAG_OWNDATA) { 1347 if (node->sysctl_data != NULL) 1348 free(node->sysctl_data, M_SYSCTLDATA); 1349 node->sysctl_data = NULL; 1350 } 1351 if (node->sysctl_flags & CTLFLAG_OWNDESC) { 1352 if (node->sysctl_desc != NULL) 1353 /*XXXUNCONST*/ 1354 free(__UNCONST(node->sysctl_desc), M_SYSCTLDATA); 1355 node->sysctl_desc = NULL; 1356 } 1357 1358 /* 1359 * if the node to be removed is not the last one on the list, 1360 * move the remaining nodes up, and reparent any grandchildren 1361 */ 1362 onode = *node; 1363 if (ni < pnode->sysctl_clen - 1) { 1364 int t; 1365 1366 memmove(&pnode->sysctl_child[ni], &pnode->sysctl_child[ni + 1], 1367 (pnode->sysctl_clen - ni - 1) * 1368 sizeof(struct sysctlnode)); 1369 for (; ni < pnode->sysctl_clen - 1; ni++) 1370 if (SYSCTL_TYPE(pnode->sysctl_child[ni].sysctl_flags) == 1371 CTLTYPE_NODE) 1372 for (t = 0; 1373 t < pnode->sysctl_child[ni].sysctl_clen; 1374 t++) 1375 pnode->sysctl_child[ni].sysctl_child[t]. 1376 sysctl_parent = 1377 &pnode->sysctl_child[ni]; 1378 ni = pnode->sysctl_clen - 1; 1379 node = &pnode->sysctl_child[ni]; 1380 } 1381 1382 /* 1383 * reset the space we just vacated 1384 */ 1385 memset(node, 0, sizeof(struct sysctlnode)); 1386 node->sysctl_parent = pnode; 1387 pnode->sysctl_clen--; 1388 1389 /* 1390 * if this parent just lost its last child, nuke the creche 1391 */ 1392 if (pnode->sysctl_clen == 0) { 1393 free(pnode->sysctl_child, M_SYSCTLNODE); 1394 pnode->sysctl_csize = 0; 1395 pnode->sysctl_child = NULL; 1396 } 1397 1398 /* 1399 * update "version" on path to "root" 1400 */ 1401 for (; rnode->sysctl_parent != NULL; rnode = rnode->sysctl_parent) 1402 ; 1403 for (ni = rnode->sysctl_ver + 1; pnode != NULL; 1404 pnode = pnode->sysctl_parent) 1405 pnode->sysctl_ver = ni; 1406 1407 error = sysctl_cvt_out(l, v, &onode, oldp, *oldlenp, oldlenp); 1408 1409 return (error); 1410 } 1411 1412 /* 1413 * sysctl_lookup -- Handles copyin/copyout of new and old values. 1414 * Partial reads are globally allowed. Only root can write to things 1415 * unless the node says otherwise. 1416 */ 1417 int 1418 sysctl_lookup(SYSCTLFN_ARGS) 1419 { 1420 int error, rw; 1421 size_t sz, len; 1422 void *d; 1423 1424 KASSERT(rw_lock_held(&sysctl_treelock)); 1425 1426 if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) { 1427 printf("sysctl_lookup: rnode %p wrong version\n", rnode); 1428 return (EINVAL); 1429 } 1430 1431 error = 0; 1432 1433 /* 1434 * you can't "look up" a node. you can "query" it, but you 1435 * can't "look it up". 1436 */ 1437 if (SYSCTL_TYPE(rnode->sysctl_flags) == CTLTYPE_NODE || namelen != 0) 1438 return (EINVAL); 1439 1440 /* 1441 * some nodes are private, so only root can look into them. 1442 */ 1443 if (l != NULL && (rnode->sysctl_flags & CTLFLAG_PRIVATE) && 1444 (error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SYSCTL, 1445 KAUTH_REQ_SYSTEM_SYSCTL_PRVT, NULL, NULL, NULL)) != 0) 1446 return (error); 1447 1448 /* 1449 * if a node wants to be writable according to different rules 1450 * other than "only root can write to stuff unless a flag is 1451 * set", then it needs its own function which should have been 1452 * called and not us. 1453 */ 1454 if (l != NULL && newp != NULL && 1455 !(rnode->sysctl_flags & CTLFLAG_ANYWRITE) && 1456 (error = kauth_authorize_generic(l->l_cred, 1457 KAUTH_GENERIC_ISSUSER, NULL)) != 0) 1458 return (error); 1459 1460 /* 1461 * is this node supposedly writable? 1462 */ 1463 rw = (rnode->sysctl_flags & CTLFLAG_READWRITE) ? 1 : 0; 1464 1465 /* 1466 * it appears not to be writable at this time, so if someone 1467 * tried to write to it, we must tell them to go away 1468 */ 1469 if (!rw && newp != NULL) 1470 return (EPERM); 1471 1472 /* 1473 * step one, copy out the stuff we have presently 1474 */ 1475 if (rnode->sysctl_flags & CTLFLAG_IMMEDIATE) { 1476 /* 1477 * note that we discard const here because we are 1478 * modifying the contents of the node (which is okay 1479 * because it's ours) 1480 */ 1481 switch (SYSCTL_TYPE(rnode->sysctl_flags)) { 1482 case CTLTYPE_BOOL: 1483 d = __UNCONST(&rnode->sysctl_bdata); 1484 break; 1485 case CTLTYPE_INT: 1486 d = __UNCONST(&rnode->sysctl_idata); 1487 break; 1488 case CTLTYPE_QUAD: 1489 d = __UNCONST(&rnode->sysctl_qdata); 1490 break; 1491 default: 1492 return (EINVAL); 1493 } 1494 } else 1495 d = rnode->sysctl_data; 1496 if (SYSCTL_TYPE(rnode->sysctl_flags) == CTLTYPE_STRING) 1497 sz = strlen(d) + 1; /* XXX@@@ possible fault here */ 1498 else 1499 sz = rnode->sysctl_size; 1500 if (oldp != NULL) 1501 error = sysctl_copyout(l, d, oldp, MIN(sz, *oldlenp)); 1502 if (error) 1503 return (error); 1504 *oldlenp = sz; 1505 1506 /* 1507 * are we done? 1508 */ 1509 if (newp == NULL || newlen == 0) 1510 return (0); 1511 1512 /* 1513 * hmm...not done. must now "copy in" new value. re-adjust 1514 * sz to maximum value (strings are "weird"). 1515 */ 1516 sz = rnode->sysctl_size; 1517 switch (SYSCTL_TYPE(rnode->sysctl_flags)) { 1518 case CTLTYPE_BOOL: { 1519 u_char tmp; 1520 /* 1521 * these data must be *exactly* the same size coming 1522 * in. bool may only be true or false. 1523 */ 1524 if (newlen != sz) 1525 return (EINVAL); 1526 error = sysctl_copyin(l, newp, &tmp, sz); 1527 if (error) 1528 break; 1529 *(bool *)d = tmp; 1530 break; 1531 } 1532 case CTLTYPE_INT: 1533 case CTLTYPE_QUAD: 1534 case CTLTYPE_STRUCT: 1535 /* 1536 * these data must be *exactly* the same size coming 1537 * in. 1538 */ 1539 if (newlen != sz) 1540 return (EINVAL); 1541 error = sysctl_copyin(l, newp, d, sz); 1542 break; 1543 case CTLTYPE_STRING: { 1544 /* 1545 * strings, on the other hand, can be shorter, and we 1546 * let userland be sloppy about the trailing nul. 1547 */ 1548 char *newbuf; 1549 1550 /* 1551 * too much new string? 1552 */ 1553 if (newlen > sz) 1554 return (EINVAL); 1555 1556 /* 1557 * temporary copy of new inbound string 1558 */ 1559 len = MIN(sz, newlen); 1560 newbuf = malloc(len, M_SYSCTLDATA, M_WAITOK|M_CANFAIL); 1561 if (newbuf == NULL) 1562 return (ENOMEM); 1563 error = sysctl_copyin(l, newp, newbuf, len); 1564 if (error) { 1565 free(newbuf, M_SYSCTLDATA); 1566 return (error); 1567 } 1568 1569 /* 1570 * did they null terminate it, or do we have space 1571 * left to do it ourselves? 1572 */ 1573 if (newbuf[len - 1] != '\0' && len == sz) { 1574 free(newbuf, M_SYSCTLDATA); 1575 return (EINVAL); 1576 } 1577 1578 /* 1579 * looks good, so pop it into place and zero the rest. 1580 */ 1581 if (len > 0) 1582 memcpy(d, newbuf, len); 1583 if (sz != len) 1584 memset((char*)d + len, 0, sz - len); 1585 free(newbuf, M_SYSCTLDATA); 1586 break; 1587 } 1588 default: 1589 return (EINVAL); 1590 } 1591 1592 return (error); 1593 } 1594 1595 /* 1596 * sysctl_mmap -- Dispatches sysctl mmap requests to those nodes that 1597 * purport to handle it. This interface isn't fully fleshed out yet, 1598 * unfortunately. 1599 */ 1600 static int 1601 sysctl_mmap(SYSCTLFN_ARGS) 1602 { 1603 const struct sysctlnode *node; 1604 struct sysctlnode nnode; 1605 int error; 1606 1607 if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) { 1608 printf("sysctl_mmap: rnode %p wrong version\n", rnode); 1609 return (EINVAL); 1610 } 1611 1612 /* 1613 * let's just pretend that didn't happen, m'kay? 1614 */ 1615 if (l == NULL) 1616 return (EPERM); 1617 1618 /* 1619 * is this a sysctlnode description of an mmap request? 1620 */ 1621 if (newp == NULL || newlen != sizeof(struct sysctlnode)) 1622 return (EINVAL); 1623 error = sysctl_copyin(l, newp, &nnode, sizeof(nnode)); 1624 if (error) 1625 return (error); 1626 1627 /* 1628 * does the node they asked for exist? 1629 */ 1630 if (namelen != 1) 1631 return (EOPNOTSUPP); 1632 node = rnode; 1633 error = sysctl_locate(l, &nnode.sysctl_num, 1, &node, NULL); 1634 if (error) 1635 return (error); 1636 1637 /* 1638 * does this node that we have found purport to handle mmap? 1639 */ 1640 if (node->sysctl_func == NULL || 1641 !(node->sysctl_flags & CTLFLAG_MMAP)) 1642 return (EOPNOTSUPP); 1643 1644 /* 1645 * well...okay, they asked for it. 1646 */ 1647 return ((*node->sysctl_func)(SYSCTLFN_CALL(node))); 1648 } 1649 1650 int 1651 sysctl_describe(SYSCTLFN_ARGS) 1652 { 1653 struct sysctldesc *d; 1654 void *bf; 1655 size_t sz, left, tot; 1656 int i, error, v = -1; 1657 struct sysctlnode *node; 1658 struct sysctlnode dnode; 1659 1660 if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) { 1661 printf("sysctl_query: rnode %p wrong version\n", rnode); 1662 return (EINVAL); 1663 } 1664 1665 if (SYSCTL_TYPE(rnode->sysctl_flags) != CTLTYPE_NODE) 1666 return (ENOTDIR); 1667 if (namelen != 1 || name[0] != CTL_DESCRIBE) 1668 return (EINVAL); 1669 1670 /* 1671 * get ready... 1672 */ 1673 error = 0; 1674 d = bf = malloc(MAXDESCLEN, M_TEMP, M_WAITOK|M_CANFAIL); 1675 if (bf == NULL) 1676 return ENOMEM; 1677 tot = 0; 1678 node = rnode->sysctl_child; 1679 left = *oldlenp; 1680 1681 /* 1682 * no request -> all descriptions at this level 1683 * request with desc unset -> just this node 1684 * request with desc set -> set descr for this node 1685 */ 1686 if (newp != NULL) { 1687 error = sysctl_cvt_in(l, &v, newp, newlen, &dnode); 1688 if (error) 1689 goto out; 1690 if (dnode.sysctl_desc != NULL) { 1691 /* 1692 * processes cannot set descriptions above 1693 * securelevel 0. and must be root. blah 1694 * blah blah. a couple more checks are made 1695 * once we find the node we want. 1696 */ 1697 if (l != NULL) { 1698 #ifndef SYSCTL_DISALLOW_CREATE 1699 error = kauth_authorize_system(l->l_cred, 1700 KAUTH_SYSTEM_SYSCTL, 1701 KAUTH_REQ_SYSTEM_SYSCTL_DESC, NULL, 1702 NULL, NULL); 1703 if (error) 1704 goto out; 1705 #else /* SYSCTL_DISALLOW_CREATE */ 1706 error = EPERM; 1707 goto out; 1708 #endif /* SYSCTL_DISALLOW_CREATE */ 1709 } 1710 1711 /* 1712 * find node and try to set the description on it 1713 */ 1714 for (i = 0; i < rnode->sysctl_clen; i++) 1715 if (node[i].sysctl_num == dnode.sysctl_num) 1716 break; 1717 if (i == rnode->sysctl_clen) { 1718 error = ENOENT; 1719 goto out; 1720 } 1721 node = &node[i]; 1722 1723 /* 1724 * did the caller specify a node version? 1725 */ 1726 if (dnode.sysctl_ver != 0 && 1727 dnode.sysctl_ver != node->sysctl_ver) { 1728 error = EINVAL; 1729 goto out; 1730 } 1731 1732 /* 1733 * okay...some rules: 1734 * (1) if setup is done and the tree is 1735 * read-only or the whole system is 1736 * read-only 1737 * (2) no one can set a description on a 1738 * permanent node (it must be set when 1739 * using createv) 1740 * (3) processes cannot *change* a description 1741 * (4) processes *can*, however, set a 1742 * description on a read-only node so that 1743 * one can be created and then described 1744 * in two steps 1745 * anything else come to mind? 1746 */ 1747 if ((sysctl_root.sysctl_flags & CTLFLAG_PERMANENT) && 1748 (!(sysctl_rootof(node)->sysctl_flags & 1749 CTLFLAG_READWRITE) || 1750 !(sysctl_root.sysctl_flags & CTLFLAG_READWRITE))) { 1751 error = EPERM; 1752 goto out; 1753 } 1754 if (node->sysctl_flags & CTLFLAG_PERMANENT) { 1755 error = EPERM; 1756 goto out; 1757 } 1758 if (l != NULL && node->sysctl_desc != NULL) { 1759 error = EPERM; 1760 goto out; 1761 } 1762 1763 /* 1764 * right, let's go ahead. the first step is 1765 * making the description into something the 1766 * node can "own", if need be. 1767 */ 1768 if (l != NULL || 1769 dnode.sysctl_flags & CTLFLAG_OWNDESC) { 1770 char *nd, *k; 1771 1772 k = malloc(MAXDESCLEN, M_TEMP, 1773 M_WAITOK|M_CANFAIL); 1774 if (k == NULL) { 1775 error = ENOMEM; 1776 goto out; 1777 } 1778 error = sysctl_copyinstr(l, dnode.sysctl_desc, 1779 k, MAXDESCLEN, &sz); 1780 if (error) { 1781 free(k, M_TEMP); 1782 goto out; 1783 } 1784 nd = malloc(sz, M_SYSCTLDATA, 1785 M_WAITOK|M_CANFAIL); 1786 if (nd == NULL) { 1787 free(k, M_TEMP); 1788 error = ENOMEM; 1789 goto out; 1790 } 1791 memcpy(nd, k, sz); 1792 dnode.sysctl_flags |= CTLFLAG_OWNDESC; 1793 dnode.sysctl_desc = nd; 1794 free(k, M_TEMP); 1795 } 1796 1797 /* 1798 * now "release" the old description and 1799 * attach the new one. ta-da. 1800 */ 1801 if ((node->sysctl_flags & CTLFLAG_OWNDESC) && 1802 node->sysctl_desc != NULL) 1803 /*XXXUNCONST*/ 1804 free(__UNCONST(node->sysctl_desc), M_SYSCTLDATA); 1805 node->sysctl_desc = dnode.sysctl_desc; 1806 node->sysctl_flags |= 1807 (dnode.sysctl_flags & CTLFLAG_OWNDESC); 1808 1809 /* 1810 * now we "fall out" and into the loop which 1811 * will copy the new description back out for 1812 * those interested parties 1813 */ 1814 } 1815 } 1816 1817 /* 1818 * scan for one description or just retrieve all descriptions 1819 */ 1820 for (i = 0; i < rnode->sysctl_clen; i++) { 1821 /* 1822 * did they ask for the description of only one node? 1823 */ 1824 if (v != -1 && node[i].sysctl_num != dnode.sysctl_num) 1825 continue; 1826 1827 /* 1828 * don't describe "private" nodes to non-suser users 1829 */ 1830 if ((node[i].sysctl_flags & CTLFLAG_PRIVATE) && (l != NULL) && 1831 !(kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SYSCTL, 1832 KAUTH_REQ_SYSTEM_SYSCTL_PRVT, NULL, NULL, NULL))) 1833 continue; 1834 1835 /* 1836 * is this description "valid"? 1837 */ 1838 memset(bf, 0, MAXDESCLEN); 1839 if (node[i].sysctl_desc == NULL) 1840 sz = 1; 1841 else if (copystr(node[i].sysctl_desc, &d->descr_str[0], 1842 MAXDESCLEN - sizeof(*d), &sz) != 0) { 1843 /* 1844 * erase possible partial description 1845 */ 1846 memset(bf, 0, MAXDESCLEN); 1847 sz = 1; 1848 } 1849 1850 /* 1851 * we've got it, stuff it into the caller's buffer 1852 */ 1853 d->descr_num = node[i].sysctl_num; 1854 d->descr_ver = node[i].sysctl_ver; 1855 d->descr_len = sz; /* includes trailing nul */ 1856 sz = (char *)NEXT_DESCR(d) - (char *)d; 1857 if (oldp != NULL && left >= sz) { 1858 error = sysctl_copyout(l, d, oldp, sz); 1859 if (error) 1860 goto out; 1861 left -= sz; 1862 oldp = (void *)__sysc_desc_adv(oldp, d->descr_len); 1863 } 1864 tot += sz; 1865 1866 /* 1867 * if we get this far with v not "unset", they asked 1868 * for a specific node and we found it 1869 */ 1870 if (v != -1) 1871 break; 1872 } 1873 1874 /* 1875 * did we find it after all? 1876 */ 1877 if (v != -1 && tot == 0) 1878 error = ENOENT; 1879 else 1880 *oldlenp = tot; 1881 1882 out: 1883 free(bf, M_TEMP); 1884 return (error); 1885 } 1886 1887 /* 1888 * ******************************************************************** 1889 * Section 3: Create and destroy from inside the kernel 1890 * ******************************************************************** 1891 * sysctl_createv() and sysctl_destroyv() are simpler-to-use 1892 * interfaces for the kernel to fling new entries into the mib and rip 1893 * them out later. In the case of sysctl_createv(), the returned copy 1894 * of the node (see sysctl_create()) will be translated back into a 1895 * pointer to the actual node. 1896 * 1897 * Note that sysctl_createv() will return 0 if the create request 1898 * matches an existing node (ala mkdir -p), and that sysctl_destroyv() 1899 * will return 0 if the node to be destroyed already does not exist 1900 * (aka rm -f) or if it is a parent of other nodes. 1901 * 1902 * This allows two (or more) different subsystems to assert sub-tree 1903 * existence before populating their own nodes, and to remove their 1904 * own nodes without orphaning the others when they are done. 1905 * ******************************************************************** 1906 */ 1907 int 1908 sysctl_createv(struct sysctllog **log, int cflags, 1909 const struct sysctlnode **rnode, const struct sysctlnode **cnode, 1910 int flags, int type, const char *namep, const char *descr, 1911 sysctlfn func, u_quad_t qv, void *newp, size_t newlen, 1912 ...) 1913 { 1914 va_list ap; 1915 int error, ni, namelen, name[CTL_MAXNAME]; 1916 const struct sysctlnode *root, *pnode; 1917 struct sysctlnode nnode, onode, *dnode; 1918 size_t sz; 1919 1920 /* 1921 * where are we putting this? 1922 */ 1923 if (rnode != NULL && *rnode == NULL) { 1924 printf("sysctl_createv: rnode NULL\n"); 1925 return (EINVAL); 1926 } 1927 root = rnode ? *rnode : NULL; 1928 if (cnode != NULL) 1929 *cnode = NULL; 1930 if (cflags != 0) 1931 return (EINVAL); 1932 1933 /* 1934 * what is it? 1935 */ 1936 flags = SYSCTL_VERSION|SYSCTL_TYPE(type)|SYSCTL_FLAGS(flags); 1937 if (log != NULL) 1938 flags &= ~CTLFLAG_PERMANENT; 1939 1940 /* 1941 * where do we put it? 1942 */ 1943 va_start(ap, newlen); 1944 namelen = 0; 1945 ni = -1; 1946 do { 1947 if (++ni == CTL_MAXNAME) 1948 return (ENAMETOOLONG); 1949 name[ni] = va_arg(ap, int); 1950 /* 1951 * sorry, this is not supported from here 1952 */ 1953 if (name[ni] == CTL_CREATESYM) 1954 return (EINVAL); 1955 } while (name[ni] != CTL_EOL && name[ni] != CTL_CREATE); 1956 namelen = ni + (name[ni] == CTL_CREATE ? 1 : 0); 1957 va_end(ap); 1958 1959 /* 1960 * what's it called 1961 */ 1962 if (strlcpy(nnode.sysctl_name, namep, sizeof(nnode.sysctl_name)) >= 1963 sizeof(nnode.sysctl_name)) 1964 return (ENAMETOOLONG); 1965 1966 /* 1967 * cons up the description of the new node 1968 */ 1969 nnode.sysctl_num = name[namelen - 1]; 1970 name[namelen - 1] = CTL_CREATE; 1971 nnode.sysctl_size = newlen; 1972 nnode.sysctl_flags = flags; 1973 if (type == CTLTYPE_NODE) { 1974 nnode.sysctl_csize = 0; 1975 nnode.sysctl_clen = 0; 1976 nnode.sysctl_child = NULL; 1977 if (flags & CTLFLAG_ALIAS) 1978 nnode.sysctl_alias = qv; 1979 } else if (flags & CTLFLAG_IMMEDIATE) { 1980 switch (type) { 1981 case CTLTYPE_BOOL: 1982 nnode.sysctl_bdata = qv; 1983 break; 1984 case CTLTYPE_INT: 1985 nnode.sysctl_idata = qv; 1986 break; 1987 case CTLTYPE_QUAD: 1988 nnode.sysctl_qdata = qv; 1989 break; 1990 default: 1991 return (EINVAL); 1992 } 1993 } else { 1994 nnode.sysctl_data = newp; 1995 } 1996 nnode.sysctl_func = func; 1997 nnode.sysctl_parent = NULL; 1998 nnode.sysctl_ver = 0; 1999 2000 /* 2001 * initialize lock state -- we need locks if the main tree has 2002 * been marked as complete, but since we could be called from 2003 * either there, or from a device driver (say, at device 2004 * insertion), or from an lkm (at lkm load time, say), we 2005 * don't really want to "wait"... 2006 */ 2007 sysctl_lock(true); 2008 2009 /* 2010 * locate the prospective parent of the new node, and if we 2011 * find it, add the new node. 2012 */ 2013 sz = sizeof(onode); 2014 pnode = root; 2015 error = sysctl_locate(NULL, &name[0], namelen - 1, &pnode, &ni); 2016 if (error) { 2017 printf("sysctl_createv: sysctl_locate(%s) returned %d\n", 2018 nnode.sysctl_name, error); 2019 sysctl_unlock(); 2020 return (error); 2021 } 2022 error = sysctl_create(&name[ni], namelen - ni, &onode, &sz, 2023 &nnode, sizeof(nnode), &name[0], NULL, 2024 pnode); 2025 2026 /* 2027 * unfortunately the node we wanted to create is already 2028 * there. if the node that's already there is a reasonable 2029 * facsimile of the node we wanted to create, just pretend 2030 * (for the caller's benefit) that we managed to create the 2031 * node they wanted. 2032 */ 2033 if (error == EEXIST) { 2034 /* name is the same as requested... */ 2035 if (strcmp(nnode.sysctl_name, onode.sysctl_name) == 0 && 2036 /* they want the same function... */ 2037 nnode.sysctl_func == onode.sysctl_func && 2038 /* number is the same as requested, or... */ 2039 (nnode.sysctl_num == onode.sysctl_num || 2040 /* they didn't pick a number... */ 2041 nnode.sysctl_num == CTL_CREATE)) { 2042 /* 2043 * collision here from trying to create 2044 * something that already existed; let's give 2045 * our customers a hand and tell them they got 2046 * what they wanted. 2047 */ 2048 #ifdef SYSCTL_DEBUG_CREATE 2049 printf("cleared\n"); 2050 #endif /* SYSCTL_DEBUG_CREATE */ 2051 error = 0; 2052 } 2053 } 2054 2055 if (error == 0 && 2056 (cnode != NULL || log != NULL || descr != NULL)) { 2057 /* 2058 * sysctl_create() gave us back a copy of the node, 2059 * but we need to know where it actually is... 2060 */ 2061 pnode = root; 2062 error = sysctl_locate(NULL, &name[0], namelen - 1, &pnode, &ni); 2063 2064 /* 2065 * manual scan of last layer so that aliased nodes 2066 * aren't followed. 2067 */ 2068 if (error == 0) { 2069 for (ni = 0; ni < pnode->sysctl_clen; ni++) 2070 if (pnode->sysctl_child[ni].sysctl_num == 2071 onode.sysctl_num) 2072 break; 2073 if (ni < pnode->sysctl_clen) 2074 pnode = &pnode->sysctl_child[ni]; 2075 else 2076 error = ENOENT; 2077 } 2078 2079 /* 2080 * not expecting an error here, but... 2081 */ 2082 if (error == 0) { 2083 if (log != NULL) 2084 sysctl_log_add(log, pnode); 2085 if (cnode != NULL) 2086 *cnode = pnode; 2087 if (descr != NULL) { 2088 /* 2089 * allow first caller to *set* a 2090 * description actually to set it 2091 * 2092 * discard const here so we can attach 2093 * the description 2094 */ 2095 dnode = __UNCONST(pnode); 2096 if (pnode->sysctl_desc != NULL) 2097 /* skip it...we've got one */; 2098 else if (flags & CTLFLAG_OWNDESC) { 2099 size_t l = strlen(descr) + 1; 2100 char *d = malloc(l, M_SYSCTLDATA, 2101 M_WAITOK|M_CANFAIL); 2102 if (d != NULL) { 2103 memcpy(d, descr, l); 2104 dnode->sysctl_desc = d; 2105 dnode->sysctl_flags |= 2106 CTLFLAG_OWNDESC; 2107 } 2108 } else 2109 dnode->sysctl_desc = descr; 2110 } 2111 } else { 2112 printf("sysctl_create succeeded but node not found?!\n"); 2113 /* 2114 * confusing, but the create said it 2115 * succeeded, so... 2116 */ 2117 error = 0; 2118 } 2119 } 2120 2121 /* 2122 * now it should be safe to release the lock state. note that 2123 * the pointer to the newly created node being passed back may 2124 * not be "good" for very long. 2125 */ 2126 sysctl_unlock(); 2127 2128 if (error != 0) { 2129 printf("sysctl_createv: sysctl_create(%s) returned %d\n", 2130 nnode.sysctl_name, error); 2131 #if 0 2132 if (error != ENOENT) 2133 sysctl_dump(&onode); 2134 #endif 2135 } 2136 2137 return (error); 2138 } 2139 2140 int 2141 sysctl_destroyv(struct sysctlnode *rnode, ...) 2142 { 2143 va_list ap; 2144 int error, name[CTL_MAXNAME], namelen, ni; 2145 const struct sysctlnode *pnode, *node; 2146 struct sysctlnode dnode, *onode; 2147 size_t sz; 2148 2149 va_start(ap, rnode); 2150 namelen = 0; 2151 ni = 0; 2152 do { 2153 if (ni == CTL_MAXNAME) 2154 return (ENAMETOOLONG); 2155 name[ni] = va_arg(ap, int); 2156 } while (name[ni++] != CTL_EOL); 2157 namelen = ni - 1; 2158 va_end(ap); 2159 2160 /* 2161 * i can't imagine why we'd be destroying a node when the tree 2162 * wasn't complete, but who knows? 2163 */ 2164 sysctl_lock(true); 2165 2166 /* 2167 * where is it? 2168 */ 2169 node = rnode; 2170 error = sysctl_locate(NULL, &name[0], namelen - 1, &node, &ni); 2171 if (error) { 2172 /* they want it gone and it's not there, so... */ 2173 sysctl_unlock(); 2174 return (error == ENOENT ? 0 : error); 2175 } 2176 2177 /* 2178 * set up the deletion 2179 */ 2180 pnode = node; 2181 node = &dnode; 2182 memset(&dnode, 0, sizeof(dnode)); 2183 dnode.sysctl_flags = SYSCTL_VERSION; 2184 dnode.sysctl_num = name[namelen - 1]; 2185 2186 /* 2187 * we found it, now let's nuke it 2188 */ 2189 name[namelen - 1] = CTL_DESTROY; 2190 sz = 0; 2191 error = sysctl_destroy(&name[namelen - 1], 1, NULL, &sz, 2192 node, sizeof(*node), &name[0], NULL, 2193 pnode); 2194 if (error == ENOTEMPTY) { 2195 /* 2196 * think of trying to delete "foo" when "foo.bar" 2197 * (which someone else put there) is still in 2198 * existence 2199 */ 2200 error = 0; 2201 2202 /* 2203 * dunno who put the description there, but if this 2204 * node can ever be removed, we need to make sure the 2205 * string doesn't go out of context. that means we 2206 * need to find the node that's still there (don't use 2207 * sysctl_locate() because that follows aliasing). 2208 */ 2209 node = pnode->sysctl_child; 2210 for (ni = 0; ni < pnode->sysctl_clen; ni++) 2211 if (node[ni].sysctl_num == dnode.sysctl_num) 2212 break; 2213 node = (ni < pnode->sysctl_clen) ? &node[ni] : NULL; 2214 2215 /* 2216 * if we found it, and this node has a description, 2217 * and this node can be released, and it doesn't 2218 * already own its own description...sigh. :) 2219 */ 2220 if (node != NULL && node->sysctl_desc != NULL && 2221 !(node->sysctl_flags & CTLFLAG_PERMANENT) && 2222 !(node->sysctl_flags & CTLFLAG_OWNDESC)) { 2223 char *d; 2224 2225 sz = strlen(node->sysctl_desc) + 1; 2226 d = malloc(sz, M_SYSCTLDATA, M_WAITOK|M_CANFAIL); 2227 if (d != NULL) { 2228 /* 2229 * discard const so that we can 2230 * re-attach the description 2231 */ 2232 memcpy(d, node->sysctl_desc, sz); 2233 onode = __UNCONST(node); 2234 onode->sysctl_desc = d; 2235 onode->sysctl_flags |= CTLFLAG_OWNDESC; 2236 } else { 2237 /* 2238 * XXX drop the description? be 2239 * afraid? don't care? 2240 */ 2241 } 2242 } 2243 } 2244 2245 sysctl_unlock(); 2246 2247 return (error); 2248 } 2249 2250 /* 2251 * ******************************************************************** 2252 * Deletes an entire n-ary tree. Not recommended unless you know why 2253 * you're doing it. Personally, I don't know why you'd even think 2254 * about it. 2255 * ******************************************************************** 2256 */ 2257 void 2258 sysctl_free(struct sysctlnode *rnode) 2259 { 2260 struct sysctlnode *node, *pnode; 2261 2262 rw_enter(&sysctl_treelock, RW_WRITER); 2263 2264 if (rnode == NULL) 2265 rnode = &sysctl_root; 2266 2267 if (SYSCTL_VERS(rnode->sysctl_flags) != SYSCTL_VERSION) { 2268 printf("sysctl_free: rnode %p wrong version\n", rnode); 2269 rw_exit(&sysctl_treelock); 2270 return; 2271 } 2272 2273 pnode = rnode; 2274 2275 node = pnode->sysctl_child; 2276 do { 2277 while (node != NULL && pnode->sysctl_csize > 0) { 2278 while (node < 2279 &pnode->sysctl_child[pnode->sysctl_clen] && 2280 (SYSCTL_TYPE(node->sysctl_flags) != 2281 CTLTYPE_NODE || 2282 node->sysctl_csize == 0)) { 2283 if (SYSCTL_FLAGS(node->sysctl_flags) & 2284 CTLFLAG_OWNDATA) { 2285 if (node->sysctl_data != NULL) { 2286 free(node->sysctl_data, 2287 M_SYSCTLDATA); 2288 node->sysctl_data = NULL; 2289 } 2290 } 2291 if (SYSCTL_FLAGS(node->sysctl_flags) & 2292 CTLFLAG_OWNDESC) { 2293 if (node->sysctl_desc != NULL) { 2294 /*XXXUNCONST*/ 2295 free(__UNCONST(node->sysctl_desc), 2296 M_SYSCTLDATA); 2297 node->sysctl_desc = NULL; 2298 } 2299 } 2300 node++; 2301 } 2302 if (node < &pnode->sysctl_child[pnode->sysctl_clen]) { 2303 pnode = node; 2304 node = node->sysctl_child; 2305 } else 2306 break; 2307 } 2308 if (pnode->sysctl_child != NULL) 2309 free(pnode->sysctl_child, M_SYSCTLNODE); 2310 pnode->sysctl_clen = 0; 2311 pnode->sysctl_csize = 0; 2312 pnode->sysctl_child = NULL; 2313 node = pnode; 2314 pnode = node->sysctl_parent; 2315 } while (pnode != NULL && node != rnode); 2316 2317 rw_exit(&sysctl_treelock); 2318 } 2319 2320 int 2321 sysctl_log_add(struct sysctllog **logp, const struct sysctlnode *node) 2322 { 2323 int name[CTL_MAXNAME], namelen, i; 2324 const struct sysctlnode *pnode; 2325 struct sysctllog *log; 2326 2327 if (node->sysctl_flags & CTLFLAG_PERMANENT) 2328 return (0); 2329 2330 if (logp == NULL) 2331 return (0); 2332 2333 if (*logp == NULL) { 2334 log = malloc(sizeof(struct sysctllog), 2335 M_SYSCTLDATA, M_WAITOK|M_CANFAIL); 2336 if (log == NULL) { 2337 /* XXX print error message? */ 2338 return (-1); 2339 } 2340 log->log_num = malloc(16 * sizeof(int), 2341 M_SYSCTLDATA, M_WAITOK|M_CANFAIL); 2342 if (log->log_num == NULL) { 2343 /* XXX print error message? */ 2344 free(log, M_SYSCTLDATA); 2345 return (-1); 2346 } 2347 memset(log->log_num, 0, 16 * sizeof(int)); 2348 log->log_root = NULL; 2349 log->log_size = 16; 2350 log->log_left = 16; 2351 *logp = log; 2352 } else 2353 log = *logp; 2354 2355 /* 2356 * check that the root is proper. it's okay to record the 2357 * address of the root of a tree. it's the only thing that's 2358 * guaranteed not to shift around as nodes come and go. 2359 */ 2360 if (log->log_root == NULL) 2361 log->log_root = sysctl_rootof(node); 2362 else if (log->log_root != sysctl_rootof(node)) { 2363 printf("sysctl: log %p root mismatch (%p)\n", 2364 log->log_root, sysctl_rootof(node)); 2365 return (-1); 2366 } 2367 2368 /* 2369 * we will copy out name in reverse order 2370 */ 2371 for (pnode = node, namelen = 0; 2372 pnode != NULL && !(pnode->sysctl_flags & CTLFLAG_ROOT); 2373 pnode = pnode->sysctl_parent) 2374 name[namelen++] = pnode->sysctl_num; 2375 2376 /* 2377 * do we have space? 2378 */ 2379 if (log->log_left < (namelen + 3)) 2380 sysctl_log_realloc(log); 2381 if (log->log_left < (namelen + 3)) 2382 return (-1); 2383 2384 /* 2385 * stuff name in, then namelen, then node type, and finally, 2386 * the version for non-node nodes. 2387 */ 2388 for (i = 0; i < namelen; i++) 2389 log->log_num[--log->log_left] = name[i]; 2390 log->log_num[--log->log_left] = namelen; 2391 log->log_num[--log->log_left] = SYSCTL_TYPE(node->sysctl_flags); 2392 if (log->log_num[log->log_left] != CTLTYPE_NODE) 2393 log->log_num[--log->log_left] = node->sysctl_ver; 2394 else 2395 log->log_num[--log->log_left] = 0; 2396 2397 return (0); 2398 } 2399 2400 void 2401 sysctl_teardown(struct sysctllog **logp) 2402 { 2403 const struct sysctlnode *rnode; 2404 struct sysctlnode node; 2405 struct sysctllog *log; 2406 uint namelen; 2407 int *name, t, v, error, ni; 2408 size_t sz; 2409 2410 if (logp == NULL || *logp == NULL) 2411 return; 2412 log = *logp; 2413 2414 rw_enter(&sysctl_treelock, RW_WRITER); 2415 memset(&node, 0, sizeof(node)); 2416 2417 while (log->log_left < log->log_size) { 2418 KASSERT((log->log_left + 3 < log->log_size) && 2419 (log->log_left + log->log_num[log->log_left + 2] <= 2420 log->log_size)); 2421 v = log->log_num[log->log_left++]; 2422 t = log->log_num[log->log_left++]; 2423 namelen = log->log_num[log->log_left++]; 2424 name = &log->log_num[log->log_left]; 2425 2426 node.sysctl_num = name[namelen - 1]; 2427 node.sysctl_flags = SYSCTL_VERSION|t; 2428 node.sysctl_ver = v; 2429 2430 rnode = log->log_root; 2431 error = sysctl_locate(NULL, &name[0], namelen, &rnode, &ni); 2432 if (error == 0) { 2433 name[namelen - 1] = CTL_DESTROY; 2434 rnode = rnode->sysctl_parent; 2435 sz = 0; 2436 (void)sysctl_destroy(&name[namelen - 1], 1, NULL, 2437 &sz, &node, sizeof(node), 2438 &name[0], NULL, rnode); 2439 } 2440 2441 log->log_left += namelen; 2442 } 2443 2444 KASSERT(log->log_size == log->log_left); 2445 free(log->log_num, M_SYSCTLDATA); 2446 free(log, M_SYSCTLDATA); 2447 *logp = NULL; 2448 2449 rw_exit(&sysctl_treelock); 2450 } 2451 2452 /* 2453 * ******************************************************************** 2454 * old_sysctl -- A routine to bridge old-style internal calls to the 2455 * new infrastructure. 2456 * ******************************************************************** 2457 */ 2458 int 2459 old_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, 2460 void *newp, size_t newlen, struct lwp *l) 2461 { 2462 int error; 2463 size_t oldlen = 0; 2464 size_t savelen; 2465 2466 if (oldlenp) { 2467 oldlen = *oldlenp; 2468 } 2469 savelen = oldlen; 2470 2471 sysctl_lock(newp != NULL); 2472 error = sysctl_dispatch(name, namelen, oldp, &oldlen, 2473 newp, newlen, name, l, NULL); 2474 sysctl_unlock(); 2475 if (error == 0 && oldp != NULL && savelen < oldlen) 2476 error = ENOMEM; 2477 if (oldlenp) { 2478 *oldlenp = oldlen; 2479 } 2480 2481 return (error); 2482 } 2483 2484 /* 2485 * ******************************************************************** 2486 * Section 4: Generic helper routines 2487 * ******************************************************************** 2488 * "helper" routines that can do more finely grained access control, 2489 * construct structures from disparate information, create the 2490 * appearance of more nodes and sub-trees, etc. for example, if 2491 * CTL_PROC wanted a helper function, it could respond to a CTL_QUERY 2492 * with a dynamically created list of nodes that represented the 2493 * currently running processes at that instant. 2494 * ******************************************************************** 2495 */ 2496 2497 /* 2498 * first, a few generic helpers that provide: 2499 * 2500 * sysctl_needfunc() a readonly interface that emits a warning 2501 * sysctl_notavail() returns EOPNOTSUPP (generic error) 2502 * sysctl_null() an empty return buffer with no error 2503 */ 2504 int 2505 sysctl_needfunc(SYSCTLFN_ARGS) 2506 { 2507 int error; 2508 2509 printf("!!SYSCTL_NEEDFUNC!!\n"); 2510 2511 if (newp != NULL || namelen != 0) 2512 return (EOPNOTSUPP); 2513 2514 error = 0; 2515 if (oldp != NULL) 2516 error = sysctl_copyout(l, rnode->sysctl_data, oldp, 2517 MIN(rnode->sysctl_size, *oldlenp)); 2518 *oldlenp = rnode->sysctl_size; 2519 2520 return (error); 2521 } 2522 2523 int 2524 sysctl_notavail(SYSCTLFN_ARGS) 2525 { 2526 2527 if (namelen == 1 && name[0] == CTL_QUERY) 2528 return (sysctl_query(SYSCTLFN_CALL(rnode))); 2529 2530 return (EOPNOTSUPP); 2531 } 2532 2533 int 2534 sysctl_null(SYSCTLFN_ARGS) 2535 { 2536 2537 *oldlenp = 0; 2538 2539 return (0); 2540 } 2541 2542 /* 2543 * ******************************************************************** 2544 * Section 5: The machinery that makes it all go 2545 * ******************************************************************** 2546 * Memory "manglement" routines. Not much to this, eh? 2547 * ******************************************************************** 2548 */ 2549 static int 2550 sysctl_alloc(struct sysctlnode *p, int x) 2551 { 2552 int i; 2553 struct sysctlnode *n; 2554 2555 assert(p->sysctl_child == NULL); 2556 2557 if (x == 1) 2558 n = malloc(sizeof(struct sysctlnode), 2559 M_SYSCTLNODE, M_WAITOK|M_CANFAIL); 2560 else 2561 n = malloc(SYSCTL_DEFSIZE * sizeof(struct sysctlnode), 2562 M_SYSCTLNODE, M_WAITOK|M_CANFAIL); 2563 if (n == NULL) 2564 return (ENOMEM); 2565 2566 if (x == 1) { 2567 memset(n, 0, sizeof(struct sysctlnode)); 2568 p->sysctl_csize = 1; 2569 } else { 2570 memset(n, 0, SYSCTL_DEFSIZE * sizeof(struct sysctlnode)); 2571 p->sysctl_csize = SYSCTL_DEFSIZE; 2572 } 2573 p->sysctl_clen = 0; 2574 2575 for (i = 0; i < p->sysctl_csize; i++) 2576 n[i].sysctl_parent = p; 2577 2578 p->sysctl_child = n; 2579 return (0); 2580 } 2581 2582 static int 2583 sysctl_realloc(struct sysctlnode *p) 2584 { 2585 int i, j; 2586 struct sysctlnode *n; 2587 2588 assert(p->sysctl_csize == p->sysctl_clen); 2589 2590 /* 2591 * how many do we have...how many should we make? 2592 */ 2593 i = p->sysctl_clen; 2594 n = malloc(2 * i * sizeof(struct sysctlnode), M_SYSCTLNODE, 2595 M_WAITOK|M_CANFAIL); 2596 if (n == NULL) 2597 return (ENOMEM); 2598 2599 /* 2600 * move old children over...initialize new children 2601 */ 2602 memcpy(n, p->sysctl_child, i * sizeof(struct sysctlnode)); 2603 memset(&n[i], 0, i * sizeof(struct sysctlnode)); 2604 p->sysctl_csize = 2 * i; 2605 2606 /* 2607 * reattach moved (and new) children to parent; if a moved 2608 * child node has children, reattach the parent pointers of 2609 * grandchildren 2610 */ 2611 for (i = 0; i < p->sysctl_csize; i++) { 2612 n[i].sysctl_parent = p; 2613 if (n[i].sysctl_child != NULL) { 2614 for (j = 0; j < n[i].sysctl_csize; j++) 2615 n[i].sysctl_child[j].sysctl_parent = &n[i]; 2616 } 2617 } 2618 2619 /* 2620 * get out with the old and in with the new 2621 */ 2622 free(p->sysctl_child, M_SYSCTLNODE); 2623 p->sysctl_child = n; 2624 2625 return (0); 2626 } 2627 2628 static int 2629 sysctl_log_realloc(struct sysctllog *log) 2630 { 2631 int *n, s, d; 2632 2633 s = log->log_size * 2; 2634 d = log->log_size; 2635 2636 n = malloc(s * sizeof(int), M_SYSCTLDATA, M_WAITOK|M_CANFAIL); 2637 if (n == NULL) 2638 return (-1); 2639 2640 memset(n, 0, s * sizeof(int)); 2641 memcpy(&n[d], log->log_num, d * sizeof(int)); 2642 free(log->log_num, M_SYSCTLDATA); 2643 log->log_num = n; 2644 if (d) 2645 log->log_left += d; 2646 else 2647 log->log_left = s; 2648 log->log_size = s; 2649 2650 return (0); 2651 } 2652 2653 /* 2654 * ******************************************************************** 2655 * Section 6: Conversion between API versions wrt the sysctlnode 2656 * ******************************************************************** 2657 */ 2658 static int 2659 sysctl_cvt_in(struct lwp *l, int *vp, const void *i, size_t sz, 2660 struct sysctlnode *node) 2661 { 2662 int error, flags; 2663 2664 if (i == NULL || sz < sizeof(flags)) 2665 return (EINVAL); 2666 2667 error = sysctl_copyin(l, i, &flags, sizeof(flags)); 2668 if (error) 2669 return (error); 2670 2671 #if (SYSCTL_VERSION != SYSCTL_VERS_1) 2672 #error sysctl_cvt_in: no support for SYSCTL_VERSION 2673 #endif /* (SYSCTL_VERSION != SYSCTL_VERS_1) */ 2674 2675 if (sz == sizeof(*node) && 2676 SYSCTL_VERS(flags) == SYSCTL_VERSION) { 2677 error = sysctl_copyin(l, i, node, sizeof(*node)); 2678 if (error) 2679 return (error); 2680 *vp = SYSCTL_VERSION; 2681 return (0); 2682 } 2683 2684 return (EINVAL); 2685 } 2686 2687 static int 2688 sysctl_cvt_out(struct lwp *l, int v, const struct sysctlnode *i, 2689 void *ovp, size_t left, size_t *szp) 2690 { 2691 size_t sz = sizeof(*i); 2692 const void *src = i; 2693 int error; 2694 2695 switch (v) { 2696 case SYSCTL_VERS_0: 2697 return (EINVAL); 2698 2699 #if (SYSCTL_VERSION != SYSCTL_VERS_1) 2700 #error sysctl_cvt_out: no support for SYSCTL_VERSION 2701 #endif /* (SYSCTL_VERSION != SYSCTL_VERS_1) */ 2702 2703 case SYSCTL_VERSION: 2704 /* nothing more to do here */ 2705 break; 2706 } 2707 2708 if (ovp != NULL && left >= sz) { 2709 error = sysctl_copyout(l, src, ovp, sz); 2710 if (error) 2711 return (error); 2712 } 2713 2714 if (szp != NULL) 2715 *szp = sz; 2716 2717 return (0); 2718 } 2719