1 /*- 2 * Copyright (c) 1982, 1986, 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Mike Karels at Berkeley Software Design, Inc. 7 * 8 * Quite extensively rewritten by Poul-Henning Kamp of the FreeBSD 9 * project, to make these variables more userfriendly. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the University of 22 * California, Berkeley and its contributors. 23 * 4. Neither the name of the University nor the names of its contributors 24 * may be used to endorse or promote products derived from this software 25 * without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37 * SUCH DAMAGE. 38 * 39 * @(#)kern_sysctl.c 8.4 (Berkeley) 4/14/94 40 * $FreeBSD$ 41 */ 42 43 #include "opt_compat.h" 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/kernel.h> 48 #include <sys/sysctl.h> 49 #include <sys/malloc.h> 50 #include <sys/proc.h> 51 #include <sys/lock.h> 52 #include <sys/mutex.h> 53 #include <sys/sx.h> 54 #include <sys/sysproto.h> 55 #include <vm/vm.h> 56 #include <vm/vm_extern.h> 57 58 static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic"); 59 static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids"); 60 static MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer"); 61 62 /* 63 * Locking - this locks the sysctl tree in memory. 64 */ 65 static struct sx sysctllock; 66 67 #define SYSCTL_LOCK() sx_xlock(&sysctllock) 68 #define SYSCTL_UNLOCK() sx_xunlock(&sysctllock) 69 #define SYSCTL_INIT() sx_init(&sysctllock, "sysctl sysctllock") 70 71 static int sysctl_root(SYSCTL_HANDLER_ARGS); 72 73 struct sysctl_oid_list sysctl__children; /* root list */ 74 75 static struct sysctl_oid * 76 sysctl_find_oidname(const char *name, struct sysctl_oid_list *list) 77 { 78 struct sysctl_oid *oidp; 79 80 SLIST_FOREACH(oidp, list, oid_link) { 81 if (strcmp(oidp->oid_name, name) == 0) { 82 return (oidp); 83 } 84 } 85 return (NULL); 86 } 87 88 /* 89 * Initialization of the MIB tree. 90 * 91 * Order by number in each list. 92 */ 93 94 void 95 sysctl_register_oid(struct sysctl_oid *oidp) 96 { 97 struct sysctl_oid_list *parent = oidp->oid_parent; 98 struct sysctl_oid *p; 99 struct sysctl_oid *q; 100 101 /* 102 * First check if another oid with the same name already 103 * exists in the parent's list. 104 */ 105 p = sysctl_find_oidname(oidp->oid_name, parent); 106 if (p != NULL) { 107 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 108 p->oid_refcnt++; 109 return; 110 } else { 111 printf("can't re-use a leaf (%s)!\n", p->oid_name); 112 return; 113 } 114 } 115 /* 116 * If this oid has a number OID_AUTO, give it a number which 117 * is greater than any current oid. 118 * NOTE: DO NOT change the starting value here, change it in 119 * <sys/sysctl.h>, and make sure it is at least 256 to 120 * accomodate e.g. net.inet.raw as a static sysctl node. 121 */ 122 if (oidp->oid_number == OID_AUTO) { 123 static int newoid = CTL_AUTO_START; 124 125 oidp->oid_number = newoid++; 126 if (newoid == 0x7fffffff) 127 panic("out of oids"); 128 } 129 #if 0 130 else if (oidp->oid_number >= CTL_AUTO_START) { 131 /* do not panic; this happens when unregistering sysctl sets */ 132 printf("static sysctl oid too high: %d", oidp->oid_number); 133 } 134 #endif 135 136 /* 137 * Insert the oid into the parent's list in order. 138 */ 139 q = NULL; 140 SLIST_FOREACH(p, parent, oid_link) { 141 if (oidp->oid_number < p->oid_number) 142 break; 143 q = p; 144 } 145 if (q) 146 SLIST_INSERT_AFTER(q, oidp, oid_link); 147 else 148 SLIST_INSERT_HEAD(parent, oidp, oid_link); 149 } 150 151 void 152 sysctl_unregister_oid(struct sysctl_oid *oidp) 153 { 154 SLIST_REMOVE(oidp->oid_parent, oidp, sysctl_oid, oid_link); 155 } 156 157 /* Initialize a new context to keep track of dynamically added sysctls. */ 158 int 159 sysctl_ctx_init(struct sysctl_ctx_list *c) 160 { 161 162 if (c == NULL) { 163 return (EINVAL); 164 } 165 TAILQ_INIT(c); 166 return (0); 167 } 168 169 /* Free the context, and destroy all dynamic oids registered in this context */ 170 int 171 sysctl_ctx_free(struct sysctl_ctx_list *clist) 172 { 173 struct sysctl_ctx_entry *e, *e1; 174 int error; 175 176 error = 0; 177 /* 178 * First perform a "dry run" to check if it's ok to remove oids. 179 * XXX FIXME 180 * XXX This algorithm is a hack. But I don't know any 181 * XXX better solution for now... 182 */ 183 TAILQ_FOREACH(e, clist, link) { 184 error = sysctl_remove_oid(e->entry, 0, 0); 185 if (error) 186 break; 187 } 188 /* 189 * Restore deregistered entries, either from the end, 190 * or from the place where error occured. 191 * e contains the entry that was not unregistered 192 */ 193 if (error) 194 e1 = TAILQ_PREV(e, sysctl_ctx_list, link); 195 else 196 e1 = TAILQ_LAST(clist, sysctl_ctx_list); 197 while (e1 != NULL) { 198 sysctl_register_oid(e1->entry); 199 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link); 200 } 201 if (error) 202 return(EBUSY); 203 /* Now really delete the entries */ 204 e = TAILQ_FIRST(clist); 205 while (e != NULL) { 206 e1 = TAILQ_NEXT(e, link); 207 error = sysctl_remove_oid(e->entry, 1, 0); 208 if (error) 209 panic("sysctl_remove_oid: corrupt tree, entry: %s", 210 e->entry->oid_name); 211 free(e, M_SYSCTLOID); 212 e = e1; 213 } 214 return (error); 215 } 216 217 /* Add an entry to the context */ 218 struct sysctl_ctx_entry * 219 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp) 220 { 221 struct sysctl_ctx_entry *e; 222 223 if (clist == NULL || oidp == NULL) 224 return(NULL); 225 e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK); 226 e->entry = oidp; 227 TAILQ_INSERT_HEAD(clist, e, link); 228 return (e); 229 } 230 231 /* Find an entry in the context */ 232 struct sysctl_ctx_entry * 233 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp) 234 { 235 struct sysctl_ctx_entry *e; 236 237 if (clist == NULL || oidp == NULL) 238 return(NULL); 239 TAILQ_FOREACH(e, clist, link) { 240 if(e->entry == oidp) 241 return(e); 242 } 243 return (e); 244 } 245 246 /* 247 * Delete an entry from the context. 248 * NOTE: this function doesn't free oidp! You have to remove it 249 * with sysctl_remove_oid(). 250 */ 251 int 252 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp) 253 { 254 struct sysctl_ctx_entry *e; 255 256 if (clist == NULL || oidp == NULL) 257 return (EINVAL); 258 e = sysctl_ctx_entry_find(clist, oidp); 259 if (e != NULL) { 260 TAILQ_REMOVE(clist, e, link); 261 free(e, M_SYSCTLOID); 262 return (0); 263 } else 264 return (ENOENT); 265 } 266 267 /* 268 * Remove dynamically created sysctl trees. 269 * oidp - top of the tree to be removed 270 * del - if 0 - just deregister, otherwise free up entries as well 271 * recurse - if != 0 traverse the subtree to be deleted 272 */ 273 int 274 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse) 275 { 276 struct sysctl_oid *p; 277 int error; 278 279 if (oidp == NULL) 280 return(EINVAL); 281 if ((oidp->oid_kind & CTLFLAG_DYN) == 0) { 282 printf("can't remove non-dynamic nodes!\n"); 283 return (EINVAL); 284 } 285 /* 286 * WARNING: normal method to do this should be through 287 * sysctl_ctx_free(). Use recursing as the last resort 288 * method to purge your sysctl tree of leftovers... 289 * However, if some other code still references these nodes, 290 * it will panic. 291 */ 292 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 293 if (oidp->oid_refcnt == 1) { 294 SLIST_FOREACH(p, SYSCTL_CHILDREN(oidp), oid_link) { 295 if (!recurse) 296 return (ENOTEMPTY); 297 error = sysctl_remove_oid(p, del, recurse); 298 if (error) 299 return (error); 300 } 301 if (del) 302 free(SYSCTL_CHILDREN(oidp), M_SYSCTLOID); 303 } 304 } 305 if (oidp->oid_refcnt > 1 ) { 306 oidp->oid_refcnt--; 307 } else { 308 if (oidp->oid_refcnt == 0) { 309 printf("Warning: bad oid_refcnt=%u (%s)!\n", 310 oidp->oid_refcnt, oidp->oid_name); 311 return (EINVAL); 312 } 313 sysctl_unregister_oid(oidp); 314 if (del) { 315 if (oidp->descr) 316 free((void *)(uintptr_t)(const void *)oidp->descr, M_SYSCTLOID); 317 free((void *)(uintptr_t)(const void *)oidp->oid_name, 318 M_SYSCTLOID); 319 free(oidp, M_SYSCTLOID); 320 } 321 } 322 return (0); 323 } 324 325 /* 326 * Create new sysctls at run time. 327 * clist may point to a valid context initialized with sysctl_ctx_init(). 328 */ 329 struct sysctl_oid * 330 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent, 331 int number, const char *name, int kind, void *arg1, int arg2, 332 int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr) 333 { 334 struct sysctl_oid *oidp; 335 ssize_t len; 336 char *newname; 337 338 /* You have to hook up somewhere.. */ 339 if (parent == NULL) 340 return(NULL); 341 /* Check if the node already exists, otherwise create it */ 342 oidp = sysctl_find_oidname(name, parent); 343 if (oidp != NULL) { 344 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 345 oidp->oid_refcnt++; 346 /* Update the context */ 347 if (clist != NULL) 348 sysctl_ctx_entry_add(clist, oidp); 349 return (oidp); 350 } else { 351 printf("can't re-use a leaf (%s)!\n", name); 352 return (NULL); 353 } 354 } 355 oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO); 356 oidp->oid_parent = parent; 357 SLIST_NEXT(oidp, oid_link) = NULL; 358 oidp->oid_number = number; 359 oidp->oid_refcnt = 1; 360 len = strlen(name); 361 newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK); 362 bcopy(name, newname, len + 1); 363 newname[len] = '\0'; 364 oidp->oid_name = newname; 365 oidp->oid_handler = handler; 366 oidp->oid_kind = CTLFLAG_DYN | kind; 367 if ((kind & CTLTYPE) == CTLTYPE_NODE) { 368 /* Allocate space for children */ 369 SYSCTL_CHILDREN(oidp) = malloc(sizeof(struct sysctl_oid_list), 370 M_SYSCTLOID, M_WAITOK); 371 SLIST_INIT(SYSCTL_CHILDREN(oidp)); 372 } else { 373 oidp->oid_arg1 = arg1; 374 oidp->oid_arg2 = arg2; 375 } 376 oidp->oid_fmt = fmt; 377 if (descr) { 378 int len = strlen(descr) + 1; 379 oidp->descr = malloc(len, M_SYSCTLOID, M_WAITOK); 380 if (oidp->descr) 381 strcpy((char *)(uintptr_t)(const void *)oidp->descr, descr); 382 } 383 /* Update the context, if used */ 384 if (clist != NULL) 385 sysctl_ctx_entry_add(clist, oidp); 386 /* Register this oid */ 387 sysctl_register_oid(oidp); 388 return (oidp); 389 } 390 391 /* 392 * Register the kernel's oids on startup. 393 */ 394 SET_DECLARE(sysctl_set, struct sysctl_oid); 395 396 static void 397 sysctl_register_all(void *arg) 398 { 399 struct sysctl_oid **oidp; 400 401 SYSCTL_INIT(); 402 SET_FOREACH(oidp, sysctl_set) 403 sysctl_register_oid(*oidp); 404 } 405 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0); 406 407 /* 408 * "Staff-functions" 409 * 410 * These functions implement a presently undocumented interface 411 * used by the sysctl program to walk the tree, and get the type 412 * so it can print the value. 413 * This interface is under work and consideration, and should probably 414 * be killed with a big axe by the first person who can find the time. 415 * (be aware though, that the proper interface isn't as obvious as it 416 * may seem, there are various conflicting requirements. 417 * 418 * {0,0} printf the entire MIB-tree. 419 * {0,1,...} return the name of the "..." OID. 420 * {0,2,...} return the next OID. 421 * {0,3} return the OID of the name in "new" 422 * {0,4,...} return the kind & format info for the "..." OID. 423 * {0,5,...} return the description the "..." OID. 424 */ 425 426 static void 427 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i) 428 { 429 int k; 430 struct sysctl_oid *oidp; 431 432 SLIST_FOREACH(oidp, l, oid_link) { 433 434 for (k=0; k<i; k++) 435 printf(" "); 436 437 printf("%d %s ", oidp->oid_number, oidp->oid_name); 438 439 printf("%c%c", 440 oidp->oid_kind & CTLFLAG_RD ? 'R':' ', 441 oidp->oid_kind & CTLFLAG_WR ? 'W':' '); 442 443 if (oidp->oid_handler) 444 printf(" *Handler"); 445 446 switch (oidp->oid_kind & CTLTYPE) { 447 case CTLTYPE_NODE: 448 printf(" Node\n"); 449 if (!oidp->oid_handler) { 450 sysctl_sysctl_debug_dump_node( 451 oidp->oid_arg1, i+2); 452 } 453 break; 454 case CTLTYPE_INT: printf(" Int\n"); break; 455 case CTLTYPE_STRING: printf(" String\n"); break; 456 case CTLTYPE_QUAD: printf(" Quad\n"); break; 457 case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break; 458 default: printf("\n"); 459 } 460 461 } 462 } 463 464 static int 465 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS) 466 { 467 int error; 468 469 error = suser(req->td); 470 if (error) 471 return error; 472 sysctl_sysctl_debug_dump_node(&sysctl__children, 0); 473 return ENOENT; 474 } 475 476 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD, 477 0, 0, sysctl_sysctl_debug, "-", ""); 478 479 static int 480 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS) 481 { 482 int *name = (int *) arg1; 483 u_int namelen = arg2; 484 int error = 0; 485 struct sysctl_oid *oid; 486 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2; 487 char buf[10]; 488 489 while (namelen) { 490 if (!lsp) { 491 snprintf(buf,sizeof(buf),"%d",*name); 492 if (req->oldidx) 493 error = SYSCTL_OUT(req, ".", 1); 494 if (!error) 495 error = SYSCTL_OUT(req, buf, strlen(buf)); 496 if (error) 497 return (error); 498 namelen--; 499 name++; 500 continue; 501 } 502 lsp2 = 0; 503 SLIST_FOREACH(oid, lsp, oid_link) { 504 if (oid->oid_number != *name) 505 continue; 506 507 if (req->oldidx) 508 error = SYSCTL_OUT(req, ".", 1); 509 if (!error) 510 error = SYSCTL_OUT(req, oid->oid_name, 511 strlen(oid->oid_name)); 512 if (error) 513 return (error); 514 515 namelen--; 516 name++; 517 518 if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE) 519 break; 520 521 if (oid->oid_handler) 522 break; 523 524 lsp2 = (struct sysctl_oid_list *)oid->oid_arg1; 525 break; 526 } 527 lsp = lsp2; 528 } 529 return (SYSCTL_OUT(req, "", 1)); 530 } 531 532 SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, ""); 533 534 static int 535 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen, 536 int *next, int *len, int level, struct sysctl_oid **oidpp) 537 { 538 struct sysctl_oid *oidp; 539 540 *len = level; 541 SLIST_FOREACH(oidp, lsp, oid_link) { 542 *next = oidp->oid_number; 543 *oidpp = oidp; 544 545 if (oidp->oid_kind & CTLFLAG_SKIP) 546 continue; 547 548 if (!namelen) { 549 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 550 return 0; 551 if (oidp->oid_handler) 552 /* We really should call the handler here...*/ 553 return 0; 554 lsp = (struct sysctl_oid_list *)oidp->oid_arg1; 555 if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1, 556 len, level+1, oidpp)) 557 return 0; 558 goto next; 559 } 560 561 if (oidp->oid_number < *name) 562 continue; 563 564 if (oidp->oid_number > *name) { 565 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 566 return 0; 567 if (oidp->oid_handler) 568 return 0; 569 lsp = (struct sysctl_oid_list *)oidp->oid_arg1; 570 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, 571 next+1, len, level+1, oidpp)) 572 return (0); 573 goto next; 574 } 575 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 576 continue; 577 578 if (oidp->oid_handler) 579 continue; 580 581 lsp = (struct sysctl_oid_list *)oidp->oid_arg1; 582 if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1, 583 len, level+1, oidpp)) 584 return (0); 585 next: 586 namelen = 1; 587 *len = level; 588 } 589 return 1; 590 } 591 592 static int 593 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS) 594 { 595 int *name = (int *) arg1; 596 u_int namelen = arg2; 597 int i, j, error; 598 struct sysctl_oid *oid; 599 struct sysctl_oid_list *lsp = &sysctl__children; 600 int newoid[CTL_MAXNAME]; 601 602 i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid); 603 if (i) 604 return ENOENT; 605 error = SYSCTL_OUT(req, newoid, j * sizeof (int)); 606 return (error); 607 } 608 609 SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, ""); 610 611 static int 612 name2oid (char *name, int *oid, int *len, struct sysctl_oid **oidpp) 613 { 614 int i; 615 struct sysctl_oid *oidp; 616 struct sysctl_oid_list *lsp = &sysctl__children; 617 char *p; 618 619 if (!*name) 620 return ENOENT; 621 622 p = name + strlen(name) - 1 ; 623 if (*p == '.') 624 *p = '\0'; 625 626 *len = 0; 627 628 for (p = name; *p && *p != '.'; p++) 629 ; 630 i = *p; 631 if (i == '.') 632 *p = '\0'; 633 634 oidp = SLIST_FIRST(lsp); 635 636 while (oidp && *len < CTL_MAXNAME) { 637 if (strcmp(name, oidp->oid_name)) { 638 oidp = SLIST_NEXT(oidp, oid_link); 639 continue; 640 } 641 *oid++ = oidp->oid_number; 642 (*len)++; 643 644 if (!i) { 645 if (oidpp) 646 *oidpp = oidp; 647 return (0); 648 } 649 650 if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE) 651 break; 652 653 if (oidp->oid_handler) 654 break; 655 656 lsp = (struct sysctl_oid_list *)oidp->oid_arg1; 657 oidp = SLIST_FIRST(lsp); 658 name = p+1; 659 for (p = name; *p && *p != '.'; p++) 660 ; 661 i = *p; 662 if (i == '.') 663 *p = '\0'; 664 } 665 return ENOENT; 666 } 667 668 static int 669 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS) 670 { 671 char *p; 672 int error, oid[CTL_MAXNAME], len; 673 struct sysctl_oid *op = 0; 674 675 if (!req->newlen) 676 return ENOENT; 677 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */ 678 return (ENAMETOOLONG); 679 680 p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK); 681 682 error = SYSCTL_IN(req, p, req->newlen); 683 if (error) { 684 free(p, M_SYSCTL); 685 return (error); 686 } 687 688 p [req->newlen] = '\0'; 689 690 error = name2oid(p, oid, &len, &op); 691 692 free(p, M_SYSCTL); 693 694 if (error) 695 return (error); 696 697 error = SYSCTL_OUT(req, oid, len * sizeof *oid); 698 return (error); 699 } 700 701 SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY, 0, 0, 702 sysctl_sysctl_name2oid, "I", ""); 703 704 static int 705 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS) 706 { 707 struct sysctl_oid *oid; 708 int error; 709 710 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req); 711 if (error) 712 return (error); 713 714 if (!oid->oid_fmt) 715 return (ENOENT); 716 error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind)); 717 if (error) 718 return (error); 719 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1); 720 return (error); 721 } 722 723 724 SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD, sysctl_sysctl_oidfmt, ""); 725 726 static int 727 sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS) 728 { 729 struct sysctl_oid *oid; 730 int error; 731 732 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req); 733 if (error) 734 return (error); 735 736 if (!oid->descr) 737 return (ENOENT); 738 error = SYSCTL_OUT(req, oid->descr, strlen(oid->descr) + 1); 739 return (error); 740 } 741 742 SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, ""); 743 744 /* 745 * Default "handler" functions. 746 */ 747 748 /* 749 * Handle an int, signed or unsigned. 750 * Two cases: 751 * a variable: point arg1 at it. 752 * a constant: pass it in arg2. 753 */ 754 755 int 756 sysctl_handle_int(SYSCTL_HANDLER_ARGS) 757 { 758 int tmpout, error = 0; 759 760 /* 761 * Attempt to get a coherent snapshot by making a copy of the data. 762 */ 763 if (arg1) 764 tmpout = *(int *)arg1; 765 else 766 tmpout = arg2; 767 error = SYSCTL_OUT(req, &tmpout, sizeof(int)); 768 769 if (error || !req->newptr) 770 return (error); 771 772 if (!arg1) 773 error = EPERM; 774 else 775 error = SYSCTL_IN(req, arg1, sizeof(int)); 776 return (error); 777 } 778 779 /* 780 * Handle a long, signed or unsigned. arg1 points to it. 781 */ 782 783 int 784 sysctl_handle_long(SYSCTL_HANDLER_ARGS) 785 { 786 int error = 0; 787 long tmpout; 788 789 /* 790 * Attempt to get a coherent snapshot by making a copy of the data. 791 */ 792 if (!arg1) 793 return (EINVAL); 794 tmpout = *(long *)arg1; 795 error = SYSCTL_OUT(req, &tmpout, sizeof(long)); 796 797 if (error || !req->newptr) 798 return (error); 799 800 error = SYSCTL_IN(req, arg1, sizeof(long)); 801 return (error); 802 } 803 804 /* 805 * Handle our generic '\0' terminated 'C' string. 806 * Two cases: 807 * a variable string: point arg1 at it, arg2 is max length. 808 * a constant string: point arg1 at it, arg2 is zero. 809 */ 810 811 int 812 sysctl_handle_string(SYSCTL_HANDLER_ARGS) 813 { 814 int error=0; 815 char *tmparg; 816 size_t outlen; 817 818 /* 819 * Attempt to get a coherent snapshot by copying to a 820 * temporary kernel buffer. 821 */ 822 retry: 823 outlen = strlen((char *)arg1)+1; 824 tmparg = malloc(outlen, M_SYSCTLTMP, M_WAITOK); 825 826 if (strlcpy(tmparg, (char *)arg1, outlen) >= outlen) { 827 free(tmparg, M_SYSCTLTMP); 828 goto retry; 829 } 830 831 error = SYSCTL_OUT(req, tmparg, outlen); 832 free(tmparg, M_SYSCTLTMP); 833 834 if (error || !req->newptr) 835 return (error); 836 837 if ((req->newlen - req->newidx) >= arg2) { 838 error = EINVAL; 839 } else { 840 arg2 = (req->newlen - req->newidx); 841 error = SYSCTL_IN(req, arg1, arg2); 842 ((char *)arg1)[arg2] = '\0'; 843 } 844 845 return (error); 846 } 847 848 /* 849 * Handle any kind of opaque data. 850 * arg1 points to it, arg2 is the size. 851 */ 852 853 int 854 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS) 855 { 856 int error; 857 void *tmparg; 858 859 /* 860 * Attempt to get a coherent snapshot, either by wiring the 861 * user space buffer or copying to a temporary kernel buffer 862 * depending on the size of the data. 863 */ 864 if (arg2 > PAGE_SIZE) { 865 sysctl_wire_old_buffer(req, arg2); 866 error = SYSCTL_OUT(req, arg1, arg2); 867 } else { 868 tmparg = malloc(arg2, M_SYSCTLTMP, M_WAITOK); 869 bcopy(arg1, tmparg, arg2); 870 error = SYSCTL_OUT(req, tmparg, arg2); 871 free(tmparg, M_SYSCTLTMP); 872 } 873 874 if (error || !req->newptr) 875 return (error); 876 877 error = SYSCTL_IN(req, arg1, arg2); 878 879 return (error); 880 } 881 882 /* 883 * Transfer functions to/from kernel space. 884 * XXX: rather untested at this point 885 */ 886 static int 887 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l) 888 { 889 size_t i = 0; 890 891 if (req->oldptr) { 892 i = l; 893 if (req->oldlen <= req->oldidx) 894 i = 0; 895 else 896 if (i > req->oldlen - req->oldidx) 897 i = req->oldlen - req->oldidx; 898 if (i > 0) 899 bcopy(p, (char *)req->oldptr + req->oldidx, i); 900 } 901 req->oldidx += l; 902 if (req->oldptr && i != l) 903 return (ENOMEM); 904 return (0); 905 } 906 907 static int 908 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l) 909 { 910 if (!req->newptr) 911 return 0; 912 if (req->newlen - req->newidx < l) 913 return (EINVAL); 914 bcopy((char *)req->newptr + req->newidx, p, l); 915 req->newidx += l; 916 return (0); 917 } 918 919 int 920 kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old, 921 size_t *oldlenp, void *new, size_t newlen, size_t *retval) 922 { 923 int error = 0; 924 struct sysctl_req req; 925 926 bzero(&req, sizeof req); 927 928 req.td = td; 929 930 if (oldlenp) { 931 req.oldlen = *oldlenp; 932 } 933 934 if (old) { 935 req.oldptr= old; 936 } 937 938 if (new != NULL) { 939 req.newlen = newlen; 940 req.newptr = new; 941 } 942 943 req.oldfunc = sysctl_old_kernel; 944 req.newfunc = sysctl_new_kernel; 945 req.lock = 1; 946 947 SYSCTL_LOCK(); 948 949 error = sysctl_root(0, name, namelen, &req); 950 951 if (req.lock == 2) 952 vsunlock(req.oldptr, req.oldlen); 953 954 SYSCTL_UNLOCK(); 955 956 if (error && error != ENOMEM) 957 return (error); 958 959 if (retval) { 960 if (req.oldptr && req.oldidx > req.oldlen) 961 *retval = req.oldlen; 962 else 963 *retval = req.oldidx; 964 } 965 return (error); 966 } 967 968 int 969 kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp, 970 void *new, size_t newlen, size_t *retval) 971 { 972 int oid[CTL_MAXNAME]; 973 size_t oidlen, plen; 974 int error; 975 976 oid[0] = 0; /* sysctl internal magic */ 977 oid[1] = 3; /* name2oid */ 978 oidlen = sizeof(oid); 979 980 error = kernel_sysctl(td, oid, 2, oid, &oidlen, 981 (void *)name, strlen(name), &plen); 982 if (error) 983 return (error); 984 985 error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp, 986 new, newlen, retval); 987 return (error); 988 } 989 990 /* 991 * Transfer function to/from user space. 992 */ 993 static int 994 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l) 995 { 996 int error = 0; 997 size_t i = 0; 998 999 if (req->lock == 1 && req->oldptr) 1000 WITNESS_SLEEP(1, NULL); 1001 if (req->oldptr) { 1002 i = l; 1003 if (req->oldlen <= req->oldidx) 1004 i = 0; 1005 else 1006 if (i > req->oldlen - req->oldidx) 1007 i = req->oldlen - req->oldidx; 1008 if (i > 0) 1009 error = copyout(p, (char *)req->oldptr + req->oldidx, 1010 i); 1011 } 1012 req->oldidx += l; 1013 if (error) 1014 return (error); 1015 if (req->oldptr && i < l) 1016 return (ENOMEM); 1017 return (0); 1018 } 1019 1020 static int 1021 sysctl_new_user(struct sysctl_req *req, void *p, size_t l) 1022 { 1023 int error; 1024 1025 if (!req->newptr) 1026 return 0; 1027 if (req->newlen - req->newidx < l) 1028 return (EINVAL); 1029 error = copyin((char *)req->newptr + req->newidx, p, l); 1030 req->newidx += l; 1031 return (error); 1032 } 1033 1034 /* 1035 * Wire the user space destination buffer. If set to a value greater than 1036 * zero, the len parameter limits the maximum amount of wired memory. 1037 * 1038 * XXX - The len parameter is currently ignored due to the lack of 1039 * a place to save it in the sysctl_req structure so that the matching 1040 * amount of memory can be unwired in the sysctl exit code. 1041 */ 1042 void 1043 sysctl_wire_old_buffer(struct sysctl_req *req, size_t len) 1044 { 1045 if (req->lock == 1 && req->oldptr && req->oldfunc == sysctl_old_user) { 1046 vslock(req->oldptr, req->oldlen); 1047 req->lock = 2; 1048 } 1049 } 1050 1051 int 1052 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid, 1053 int *nindx, struct sysctl_req *req) 1054 { 1055 struct sysctl_oid *oid; 1056 int indx; 1057 1058 oid = SLIST_FIRST(&sysctl__children); 1059 indx = 0; 1060 while (oid && indx < CTL_MAXNAME) { 1061 if (oid->oid_number == name[indx]) { 1062 indx++; 1063 if (oid->oid_kind & CTLFLAG_NOLOCK) 1064 req->lock = 0; 1065 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 1066 if (oid->oid_handler != NULL || 1067 indx == namelen) { 1068 *noid = oid; 1069 if (nindx != NULL) 1070 *nindx = indx; 1071 return (0); 1072 } 1073 oid = SLIST_FIRST( 1074 (struct sysctl_oid_list *)oid->oid_arg1); 1075 } else if (indx == namelen) { 1076 *noid = oid; 1077 if (nindx != NULL) 1078 *nindx = indx; 1079 return (0); 1080 } else { 1081 return (ENOTDIR); 1082 } 1083 } else { 1084 oid = SLIST_NEXT(oid, oid_link); 1085 } 1086 } 1087 return (ENOENT); 1088 } 1089 1090 /* 1091 * Traverse our tree, and find the right node, execute whatever it points 1092 * to, and return the resulting error code. 1093 */ 1094 1095 static int 1096 sysctl_root(SYSCTL_HANDLER_ARGS) 1097 { 1098 struct sysctl_oid *oid; 1099 int error, indx; 1100 1101 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req); 1102 if (error) 1103 return (error); 1104 1105 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 1106 /* 1107 * You can't call a sysctl when it's a node, but has 1108 * no handler. Inform the user that it's a node. 1109 * The indx may or may not be the same as namelen. 1110 */ 1111 if (oid->oid_handler == NULL) 1112 return (EISDIR); 1113 } 1114 1115 /* Is this sysctl writable? */ 1116 if (req->newptr && !(oid->oid_kind & CTLFLAG_WR)) 1117 return (EPERM); 1118 1119 KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL")); 1120 1121 /* Is this sysctl sensitive to securelevels? */ 1122 if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) { 1123 error = securelevel_gt(req->td->td_ucred, 0); 1124 if (error) 1125 return (error); 1126 } 1127 1128 /* Is this sysctl writable by only privileged users? */ 1129 if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) { 1130 int flags; 1131 1132 if (oid->oid_kind & CTLFLAG_PRISON) 1133 flags = PRISON_ROOT; 1134 else 1135 flags = 0; 1136 error = suser_cred(req->td->td_ucred, flags); 1137 if (error) 1138 return (error); 1139 } 1140 1141 if (!oid->oid_handler) 1142 return EINVAL; 1143 1144 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) 1145 error = oid->oid_handler(oid, (int *)arg1 + indx, arg2 - indx, 1146 req); 1147 else 1148 error = oid->oid_handler(oid, oid->oid_arg1, oid->oid_arg2, 1149 req); 1150 return (error); 1151 } 1152 1153 #ifndef _SYS_SYSPROTO_H_ 1154 struct sysctl_args { 1155 int *name; 1156 u_int namelen; 1157 void *old; 1158 size_t *oldlenp; 1159 void *new; 1160 size_t newlen; 1161 }; 1162 #endif 1163 1164 /* 1165 * MPSAFE 1166 */ 1167 int 1168 __sysctl(struct thread *td, struct sysctl_args *uap) 1169 { 1170 int error, name[CTL_MAXNAME]; 1171 size_t j; 1172 1173 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2) 1174 return (EINVAL); 1175 1176 error = copyin(uap->name, &name, uap->namelen * sizeof(int)); 1177 if (error) 1178 return (error); 1179 1180 mtx_lock(&Giant); 1181 1182 error = userland_sysctl(td, name, uap->namelen, 1183 uap->old, uap->oldlenp, 0, 1184 uap->new, uap->newlen, &j); 1185 if (error && error != ENOMEM) 1186 goto done2; 1187 if (uap->oldlenp) { 1188 int i = copyout(&j, uap->oldlenp, sizeof(j)); 1189 if (i) 1190 error = i; 1191 } 1192 done2: 1193 mtx_unlock(&Giant); 1194 return (error); 1195 } 1196 1197 /* 1198 * This is used from various compatibility syscalls too. That's why name 1199 * must be in kernel space. 1200 */ 1201 int 1202 userland_sysctl(struct thread *td, int *name, u_int namelen, void *old, 1203 size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval) 1204 { 1205 int error = 0; 1206 struct sysctl_req req, req2; 1207 1208 bzero(&req, sizeof req); 1209 1210 req.td = td; 1211 1212 if (oldlenp) { 1213 if (inkernel) { 1214 req.oldlen = *oldlenp; 1215 } else { 1216 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp)); 1217 if (error) 1218 return (error); 1219 } 1220 } 1221 1222 if (old) { 1223 if (!useracc(old, req.oldlen, VM_PROT_WRITE)) 1224 return (EFAULT); 1225 req.oldptr= old; 1226 } 1227 1228 if (new != NULL) { 1229 if (!useracc(new, req.newlen, VM_PROT_READ)) 1230 return (EFAULT); 1231 req.newlen = newlen; 1232 req.newptr = new; 1233 } 1234 1235 req.oldfunc = sysctl_old_user; 1236 req.newfunc = sysctl_new_user; 1237 req.lock = 1; 1238 1239 SYSCTL_LOCK(); 1240 1241 do { 1242 req2 = req; 1243 error = sysctl_root(0, name, namelen, &req2); 1244 } while (error == EAGAIN); 1245 1246 req = req2; 1247 if (req.lock == 2) 1248 vsunlock(req.oldptr, req.oldlen); 1249 1250 SYSCTL_UNLOCK(); 1251 1252 if (error && error != ENOMEM) 1253 return (error); 1254 1255 if (retval) { 1256 if (req.oldptr && req.oldidx > req.oldlen) 1257 *retval = req.oldlen; 1258 else 1259 *retval = req.oldidx; 1260 } 1261 return (error); 1262 } 1263 1264 #ifdef COMPAT_43 1265 #include <sys/socket.h> 1266 #include <vm/vm_param.h> 1267 1268 #define KINFO_PROC (0<<8) 1269 #define KINFO_RT (1<<8) 1270 #define KINFO_VNODE (2<<8) 1271 #define KINFO_FILE (3<<8) 1272 #define KINFO_METER (4<<8) 1273 #define KINFO_LOADAVG (5<<8) 1274 #define KINFO_CLOCKRATE (6<<8) 1275 1276 /* Non-standard BSDI extension - only present on their 4.3 net-2 releases */ 1277 #define KINFO_BSDI_SYSINFO (101<<8) 1278 1279 /* 1280 * XXX this is bloat, but I hope it's better here than on the potentially 1281 * limited kernel stack... -Peter 1282 */ 1283 1284 static struct { 1285 int bsdi_machine; /* "i386" on BSD/386 */ 1286 /* ^^^ this is an offset to the string, relative to the struct start */ 1287 char *pad0; 1288 long pad1; 1289 long pad2; 1290 long pad3; 1291 u_long pad4; 1292 u_long pad5; 1293 u_long pad6; 1294 1295 int bsdi_ostype; /* "BSD/386" on BSD/386 */ 1296 int bsdi_osrelease; /* "1.1" on BSD/386 */ 1297 long pad7; 1298 long pad8; 1299 char *pad9; 1300 1301 long pad10; 1302 long pad11; 1303 int pad12; 1304 long pad13; 1305 quad_t pad14; 1306 long pad15; 1307 1308 struct timeval pad16; 1309 /* we dont set this, because BSDI's uname used gethostname() instead */ 1310 int bsdi_hostname; /* hostname on BSD/386 */ 1311 1312 /* the actual string data is appended here */ 1313 1314 } bsdi_si; 1315 /* 1316 * this data is appended to the end of the bsdi_si structure during copyout. 1317 * The "char *" offsets are relative to the base of the bsdi_si struct. 1318 * This contains "FreeBSD\02.0-BUILT-nnnnnn\0i386\0", and these strings 1319 * should not exceed the length of the buffer here... (or else!! :-) 1320 */ 1321 static char bsdi_strings[80]; /* It had better be less than this! */ 1322 1323 #ifndef _SYS_SYSPROTO_H_ 1324 struct getkerninfo_args { 1325 int op; 1326 char *where; 1327 size_t *size; 1328 int arg; 1329 }; 1330 #endif 1331 1332 /* 1333 * MPSAFE 1334 */ 1335 int 1336 ogetkerninfo(struct thread *td, struct getkerninfo_args *uap) 1337 { 1338 int error, name[6]; 1339 size_t size; 1340 u_int needed = 0; 1341 1342 mtx_lock(&Giant); 1343 1344 switch (uap->op & 0xff00) { 1345 1346 case KINFO_RT: 1347 name[0] = CTL_NET; 1348 name[1] = PF_ROUTE; 1349 name[2] = 0; 1350 name[3] = (uap->op & 0xff0000) >> 16; 1351 name[4] = uap->op & 0xff; 1352 name[5] = uap->arg; 1353 error = userland_sysctl(td, name, 6, uap->where, uap->size, 1354 0, 0, 0, &size); 1355 break; 1356 1357 case KINFO_VNODE: 1358 name[0] = CTL_KERN; 1359 name[1] = KERN_VNODE; 1360 error = userland_sysctl(td, name, 2, uap->where, uap->size, 1361 0, 0, 0, &size); 1362 break; 1363 1364 case KINFO_PROC: 1365 name[0] = CTL_KERN; 1366 name[1] = KERN_PROC; 1367 name[2] = uap->op & 0xff; 1368 name[3] = uap->arg; 1369 error = userland_sysctl(td, name, 4, uap->where, uap->size, 1370 0, 0, 0, &size); 1371 break; 1372 1373 case KINFO_FILE: 1374 name[0] = CTL_KERN; 1375 name[1] = KERN_FILE; 1376 error = userland_sysctl(td, name, 2, uap->where, uap->size, 1377 0, 0, 0, &size); 1378 break; 1379 1380 case KINFO_METER: 1381 name[0] = CTL_VM; 1382 name[1] = VM_METER; 1383 error = userland_sysctl(td, name, 2, uap->where, uap->size, 1384 0, 0, 0, &size); 1385 break; 1386 1387 case KINFO_LOADAVG: 1388 name[0] = CTL_VM; 1389 name[1] = VM_LOADAVG; 1390 error = userland_sysctl(td, name, 2, uap->where, uap->size, 1391 0, 0, 0, &size); 1392 break; 1393 1394 case KINFO_CLOCKRATE: 1395 name[0] = CTL_KERN; 1396 name[1] = KERN_CLOCKRATE; 1397 error = userland_sysctl(td, name, 2, uap->where, uap->size, 1398 0, 0, 0, &size); 1399 break; 1400 1401 case KINFO_BSDI_SYSINFO: { 1402 /* 1403 * this is pretty crude, but it's just enough for uname() 1404 * from BSDI's 1.x libc to work. 1405 * 1406 * *size gives the size of the buffer before the call, and 1407 * the amount of data copied after a successful call. 1408 * If successful, the return value is the amount of data 1409 * available, which can be larger than *size. 1410 * 1411 * BSDI's 2.x product apparently fails with ENOMEM if *size 1412 * is too small. 1413 */ 1414 1415 u_int left; 1416 char *s; 1417 1418 bzero((char *)&bsdi_si, sizeof(bsdi_si)); 1419 bzero(bsdi_strings, sizeof(bsdi_strings)); 1420 1421 s = bsdi_strings; 1422 1423 bsdi_si.bsdi_ostype = (s - bsdi_strings) + sizeof(bsdi_si); 1424 strcpy(s, ostype); 1425 s += strlen(s) + 1; 1426 1427 bsdi_si.bsdi_osrelease = (s - bsdi_strings) + sizeof(bsdi_si); 1428 strcpy(s, osrelease); 1429 s += strlen(s) + 1; 1430 1431 bsdi_si.bsdi_machine = (s - bsdi_strings) + sizeof(bsdi_si); 1432 strcpy(s, machine); 1433 s += strlen(s) + 1; 1434 1435 needed = sizeof(bsdi_si) + (s - bsdi_strings); 1436 1437 if ((uap->where == NULL) || (uap->size == NULL)) { 1438 /* process is asking how much buffer to supply.. */ 1439 size = needed; 1440 error = 0; 1441 break; 1442 } 1443 1444 if ((error = copyin(uap->size, &size, sizeof(size))) != 0) 1445 break; 1446 1447 /* if too much buffer supplied, trim it down */ 1448 if (size > needed) 1449 size = needed; 1450 1451 /* how much of the buffer is remaining */ 1452 left = size; 1453 1454 if ((error = copyout((char *)&bsdi_si, uap->where, left)) != 0) 1455 break; 1456 1457 /* is there any point in continuing? */ 1458 if (left > sizeof(bsdi_si)) { 1459 left -= sizeof(bsdi_si); 1460 error = copyout(&bsdi_strings, 1461 uap->where + sizeof(bsdi_si), left); 1462 } 1463 break; 1464 } 1465 1466 default: 1467 error = EOPNOTSUPP; 1468 break; 1469 } 1470 if (error == 0) { 1471 td->td_retval[0] = needed ? needed : size; 1472 if (uap->size) { 1473 error = copyout(&size, uap->size, sizeof(size)); 1474 } 1475 } 1476 mtx_unlock(&Giant); 1477 return (error); 1478 } 1479 #endif /* COMPAT_43 */ 1480