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