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