kern_sysctl.c revision 12819
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 * $Id: kern_sysctl.c,v 1.56 1995/12/07 12:46:52 davidg Exp $ 41 */ 42 43#include <sys/param.h> 44#include <sys/systm.h> 45#include <sys/sysproto.h> 46#include <sys/kernel.h> 47#include <sys/vnode.h> 48#include <sys/unistd.h> 49#include <sys/conf.h> 50#include <sys/sysctl.h> 51#include <sys/malloc.h> 52#include <sys/proc.h> 53 54#include <vm/vm.h> 55#include <vm/vm_param.h> 56#include <vm/vm_extern.h> 57 58/* 59 * Locking and stats 60 */ 61static struct sysctl_lock { 62 int sl_lock; 63 int sl_want; 64 int sl_locked; 65} memlock; 66 67static int sysctl_root SYSCTL_HANDLER_ARGS; 68 69extern struct linker_set sysctl_; 70 71/* 72 * MIB definitions. XXX Very few of these, if any, belong here. 73 */ 74SYSCTL_NODE(, 0, sysctl, CTLFLAG_RW, 0, 75 "Sysctl internal magic"); 76SYSCTL_NODE(, CTL_KERN, kern, CTLFLAG_RW, 0, 77 "High kernel, proc, limits &c"); 78SYSCTL_NODE(, CTL_VM, vm, CTLFLAG_RW, 0, 79 "Virtual memory"); 80SYSCTL_NODE(, CTL_FS, fs, CTLFLAG_RW, 0, 81 "File system"); 82SYSCTL_NODE(, CTL_NET, net, CTLFLAG_RW, 0, 83 "Network, (see socket.h)"); 84SYSCTL_NODE(, CTL_DEBUG, debug, CTLFLAG_RW, 0, 85 "Debugging"); 86SYSCTL_NODE(, CTL_HW, hw, CTLFLAG_RW, 0, 87 "hardware"); 88SYSCTL_NODE(, CTL_MACHDEP, machdep, CTLFLAG_RW, 0, 89 "machine dependent"); 90SYSCTL_NODE(, CTL_USER, user, CTLFLAG_RW, 0, 91 "user-level"); 92 93SYSCTL_STRING(_kern, KERN_OSRELEASE, osrelease, CTLFLAG_RD, osrelease, 0, ""); 94 95SYSCTL_INT(_kern, KERN_OSREV, osrevision, CTLFLAG_RD, 0, BSD, ""); 96 97SYSCTL_STRING(_kern, KERN_VERSION, version, CTLFLAG_RD, version, 0, ""); 98 99SYSCTL_STRING(_kern, KERN_OSTYPE, ostype, CTLFLAG_RD, ostype, 0, ""); 100 101extern int osreldate; 102SYSCTL_INT(_kern, KERN_OSRELDATE, osreldate, CTLFLAG_RD, &osreldate, 0, ""); 103 104SYSCTL_INT(_kern, KERN_MAXPROC, maxproc, CTLFLAG_RD, &maxproc, 0, ""); 105 106SYSCTL_INT(_kern, KERN_MAXPROCPERUID, maxprocperuid, 107 CTLFLAG_RD, &maxprocperuid, 0, ""); 108 109SYSCTL_INT(_kern, KERN_ARGMAX, argmax, CTLFLAG_RD, 0, ARG_MAX, ""); 110 111SYSCTL_INT(_kern, KERN_POSIX1, posix1version, CTLFLAG_RD, 0, _POSIX_VERSION, ""); 112 113SYSCTL_INT(_kern, KERN_NGROUPS, ngroups, CTLFLAG_RD, 0, NGROUPS_MAX, ""); 114 115SYSCTL_INT(_kern, KERN_JOB_CONTROL, job_control, CTLFLAG_RD, 0, 1, ""); 116 117#ifdef _POSIX_SAVED_IDS 118SYSCTL_INT(_kern, KERN_SAVED_IDS, saved_ids, CTLFLAG_RD, 0, 1, ""); 119#else 120SYSCTL_INT(_kern, KERN_SAVED_IDS, saved_ids, CTLFLAG_RD, 0, 0, ""); 121#endif 122 123char kernelname[MAXPATHLEN] = "/kernel"; /* XXX bloat */ 124 125SYSCTL_STRING(_kern, KERN_BOOTFILE, bootfile, 126 CTLFLAG_RW, kernelname, sizeof kernelname, ""); 127 128SYSCTL_INT(_hw, HW_NCPU, ncpu, CTLFLAG_RD, 0, 1, ""); 129 130SYSCTL_INT(_hw, HW_BYTEORDER, byteorder, CTLFLAG_RD, 0, BYTE_ORDER, ""); 131 132SYSCTL_INT(_hw, HW_PAGESIZE, pagesize, CTLFLAG_RD, 0, PAGE_SIZE, ""); 133 134char hostname[MAXHOSTNAMELEN]; 135 136SYSCTL_STRING(_kern, KERN_HOSTNAME, hostname, CTLFLAG_RW, 137 hostname, sizeof(hostname), ""); 138 139int securelevel = -1; 140 141static int 142sysctl_kern_securelvl SYSCTL_HANDLER_ARGS 143{ 144 int error, level; 145 146 level = securelevel; 147 error = sysctl_handle_int(oidp, &level, 0, req); 148 if (error || !req->newptr) 149 return (error); 150 if (level < securelevel && req->p->p_pid != 1) 151 return (EPERM); 152 securelevel = level; 153 return (error); 154} 155 156SYSCTL_PROC(_kern, KERN_SECURELVL, securelevel, CTLTYPE_INT|CTLFLAG_RW, 157 0, 0, sysctl_kern_securelvl, "I", ""); 158 159char domainname[MAXHOSTNAMELEN]; 160SYSCTL_STRING(_kern, KERN_DOMAINNAME, domainname, CTLFLAG_RW, 161 &domainname, sizeof(domainname), ""); 162 163long hostid; 164/* Some trouble here, if sizeof (int) != sizeof (long) */ 165SYSCTL_INT(_kern, KERN_HOSTID, hostid, CTLFLAG_RW, &hostid, 0, ""); 166 167/* 168 * This is really cheating. These actually live in the libc, something 169 * which I'm not quite sure is a good idea anyway, but in order for 170 * getnext and friends to actually work, we define dummies here. 171 */ 172 173SYSCTL_STRING(_user, USER_CS_PATH, cs_path, CTLFLAG_RW, "", 0, ""); 174SYSCTL_INT(_user, USER_BC_BASE_MAX, bc_base_max, CTLFLAG_RW, 0, 0, ""); 175SYSCTL_INT(_user, USER_BC_DIM_MAX, bc_dim_max, CTLFLAG_RW, 0, 0, ""); 176SYSCTL_INT(_user, USER_BC_SCALE_MAX, bc_scale_max, CTLFLAG_RW, 0, 0, ""); 177SYSCTL_INT(_user, USER_BC_STRING_MAX, bc_string_max, CTLFLAG_RW, 0, 0, ""); 178SYSCTL_INT(_user, USER_COLL_WEIGHTS_MAX, coll_weights_max, CTLFLAG_RW, 0, 0, ""); 179SYSCTL_INT(_user, USER_EXPR_NEST_MAX, expr_nest_max, CTLFLAG_RW, 0, 0, ""); 180SYSCTL_INT(_user, USER_LINE_MAX, line_max, CTLFLAG_RW, 0, 0, ""); 181SYSCTL_INT(_user, USER_RE_DUP_MAX, re_dup_max, CTLFLAG_RW, 0, 0, ""); 182SYSCTL_INT(_user, USER_POSIX2_VERSION, posix2_version, CTLFLAG_RW, 0, 0, ""); 183SYSCTL_INT(_user, USER_POSIX2_C_BIND, posix2_c_bind, CTLFLAG_RW, 0, 0, ""); 184SYSCTL_INT(_user, USER_POSIX2_C_DEV, posix2_c_dev, CTLFLAG_RW, 0, 0, ""); 185SYSCTL_INT(_user, USER_POSIX2_CHAR_TERM, posix2_char_term, CTLFLAG_RW, 0, 0, ""); 186SYSCTL_INT(_user, USER_POSIX2_FORT_DEV, posix2_fort_dev, CTLFLAG_RW, 0, 0, ""); 187SYSCTL_INT(_user, USER_POSIX2_FORT_RUN, posix2_fort_run, CTLFLAG_RW, 0, 0, ""); 188SYSCTL_INT(_user, USER_POSIX2_LOCALEDEF, posix2_localedef, CTLFLAG_RW, 0, 0, ""); 189SYSCTL_INT(_user, USER_POSIX2_SW_DEV, posix2_sw_dev, CTLFLAG_RW, 0, 0, ""); 190SYSCTL_INT(_user, USER_POSIX2_UPE, posix2_upe, CTLFLAG_RW, 0, 0, ""); 191SYSCTL_INT(_user, USER_STREAM_MAX, stream_max, CTLFLAG_RW, 0, 0, ""); 192SYSCTL_INT(_user, USER_TZNAME_MAX, tzname_max, CTLFLAG_RW, 0, 0, ""); 193 194 195/* 196 * End of MIB definitions. 197 */ 198 199/* 200 * Initialization of the MIB tree. 201 * 202 * Order by number in each linker_set. 203 */ 204 205static int 206sysctl_order_cmp(const void *a, const void *b) 207{ 208 const struct sysctl_oid **pa, **pb; 209 210 pa = (const struct sysctl_oid **)a; 211 pb = (const struct sysctl_oid **)b; 212 if (*pa == NULL) 213 return (1); 214 if (*pb == NULL) 215 return (-1); 216 return ((*pa)->oid_number - (*pb)->oid_number); 217} 218 219static void 220sysctl_order(void *arg) 221{ 222 int j, k; 223 struct linker_set *l = (struct linker_set *) arg; 224 struct sysctl_oid **oidpp; 225 226 /* First, find the highest oid we have */ 227 j = l->ls_length; 228 oidpp = (struct sysctl_oid **) l->ls_items; 229 for (k = 0; j--; oidpp++) 230 if (*oidpp && (*oidpp)->oid_number > k) 231 k = (*oidpp)->oid_number; 232 233 /* Next, replace all OID_AUTO oids with new numbers */ 234 j = l->ls_length; 235 oidpp = (struct sysctl_oid **) l->ls_items; 236 k += 100; 237 for (; j--; oidpp++) 238 if (*oidpp && (*oidpp)->oid_number == OID_AUTO) 239 (*oidpp)->oid_number = k++; 240 241 /* Finally: sort by oid */ 242 j = l->ls_length; 243 oidpp = (struct sysctl_oid **) l->ls_items; 244 for (; j--; oidpp++) { 245 if (!*oidpp) 246 continue; 247 if ((*oidpp)->oid_arg1 == arg) { 248 *oidpp = 0; 249 continue; 250 } 251 if (((*oidpp)->oid_kind & CTLTYPE) == CTLTYPE_NODE) 252 if (!(*oidpp)->oid_handler) 253 sysctl_order((*oidpp)->oid_arg1); 254 } 255 qsort(l->ls_items, l->ls_length, sizeof l->ls_items[0], 256 sysctl_order_cmp); 257} 258 259SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_order, &sysctl_); 260 261/* 262 * "Staff-functions" 263 * 264 * These functions implement a presently undocumented interface 265 * used by the sysctl program to walk the tree, and get the type 266 * so it can print the value. 267 * This interface is under work and consideration, and should probably 268 * be killed with a big axe by the first person who can find the time. 269 * (be aware though, that the proper interface isn't as obvious as it 270 * may seem, there are various conflicting requirements. 271 * 272 * {0,0} printf the entire MIB-tree. 273 * {0,1,...} return the name of the "..." OID. 274 * {0,2,...} return the next OID. 275 * {0,3} return the OID of the name in "new" 276 * {0,4,...} return the kind & format info for the "..." OID. 277 */ 278 279static void 280sysctl_sysctl_debug_dump_node(struct linker_set *l, int i) 281{ 282 int j, k; 283 struct sysctl_oid **oidpp; 284 285 j = l->ls_length; 286 oidpp = (struct sysctl_oid **) l->ls_items; 287 for (; j--; oidpp++) { 288 289 if (!*oidpp) 290 continue; 291 292 for (k=0; k<i; k++) 293 printf(" "); 294 295 if ((*oidpp)->oid_number > 100) { 296 printf("Junk! %p # %d %s k %x a1 %p a2 %x h %p\n", 297 *oidpp, 298 (*oidpp)->oid_number, (*oidpp)->oid_name, 299 (*oidpp)->oid_kind, (*oidpp)->oid_arg1, 300 (*oidpp)->oid_arg2, (*oidpp)->oid_handler); 301 continue; 302 } 303 printf("%d %s ", (*oidpp)->oid_number, (*oidpp)->oid_name); 304 305 printf("%c%c", 306 (*oidpp)->oid_kind & CTLFLAG_RD ? 'R':' ', 307 (*oidpp)->oid_kind & CTLFLAG_WR ? 'W':' '); 308 309 switch ((*oidpp)->oid_kind & CTLTYPE) { 310 case CTLTYPE_NODE: 311 if ((*oidpp)->oid_handler) { 312 printf(" Node(proc)\n"); 313 } else { 314 printf(" Node\n"); 315 sysctl_sysctl_debug_dump_node( 316 (*oidpp)->oid_arg1, i+2); 317 } 318 break; 319 case CTLTYPE_INT: printf(" Int\n"); break; 320 case CTLTYPE_STRING: printf(" String\n"); break; 321 case CTLTYPE_QUAD: printf(" Quad\n"); break; 322 case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break; 323 default: printf("\n"); 324 } 325 326 } 327} 328 329static int 330sysctl_sysctl_debug SYSCTL_HANDLER_ARGS 331{ 332 sysctl_sysctl_debug_dump_node(&sysctl_, 0); 333 return ENOENT; 334} 335 336SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD, 337 0, 0, sysctl_sysctl_debug, "-", ""); 338 339static int 340sysctl_sysctl_name SYSCTL_HANDLER_ARGS 341{ 342 int *name = (int *) arg1; 343 u_int namelen = arg2; 344 int i, j, error = 0; 345 struct sysctl_oid **oidpp; 346 struct linker_set *lsp = &sysctl_; 347 char buf[10]; 348 349 while (namelen) { 350 if (!lsp) { 351 sprintf(buf,"%d",*name); 352 if (req->oldidx) 353 error = SYSCTL_OUT(req, ".", 1); 354 if (!error) 355 error = SYSCTL_OUT(req, buf, strlen(buf)); 356 if (error) 357 return (error); 358 namelen--; 359 name++; 360 continue; 361 } 362 oidpp = (struct sysctl_oid **) lsp->ls_items; 363 j = lsp->ls_length; 364 lsp = 0; 365 for (i = 0; i < j; i++, oidpp++) { 366 if (*oidpp && ((*oidpp)->oid_number != *name)) 367 continue; 368 369 if (req->oldidx) 370 error = SYSCTL_OUT(req, ".", 1); 371 if (!error) 372 error = SYSCTL_OUT(req, (*oidpp)->oid_name, 373 strlen((*oidpp)->oid_name)); 374 if (error) 375 return (error); 376 377 namelen--; 378 name++; 379 380 if (((*oidpp)->oid_kind & CTLTYPE) != CTLTYPE_NODE) 381 break; 382 383 if ((*oidpp)->oid_handler) 384 break; 385 386 lsp = (struct linker_set*)(*oidpp)->oid_arg1; 387 break; 388 } 389 } 390 return (SYSCTL_OUT(req, "", 1)); 391} 392 393SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, ""); 394 395static int 396sysctl_sysctl_next_ls (struct linker_set *lsp, int *name, u_int namelen, 397 int *next, int *len, int level, struct sysctl_oid **oidp) 398{ 399 int i, j; 400 struct sysctl_oid **oidpp; 401 402 oidpp = (struct sysctl_oid **) lsp->ls_items; 403 j = lsp->ls_length; 404 *len = level; 405 for (i = 0; i < j; i++, oidpp++) { 406 if (!*oidpp) 407 continue; 408 409 *next = (*oidpp)->oid_number; 410 *oidp = *oidpp; 411 412 if (!namelen) { 413 if (((*oidpp)->oid_kind & CTLTYPE) != CTLTYPE_NODE) 414 return 0; 415 if ((*oidpp)->oid_handler) 416 /* We really should call the handler here...*/ 417 return 0; 418 lsp = (struct linker_set*)(*oidpp)->oid_arg1; 419 return (sysctl_sysctl_next_ls (lsp, 0, 0, next+1, 420 len, level+1, oidp)); 421 } 422 423 if ((*oidpp)->oid_number < *name) 424 continue; 425 426 if ((*oidpp)->oid_number > *name) { 427 if (((*oidpp)->oid_kind & CTLTYPE) != CTLTYPE_NODE) 428 return 0; 429 if ((*oidpp)->oid_handler) 430 return 0; 431 lsp = (struct linker_set*)(*oidpp)->oid_arg1; 432 if (!sysctl_sysctl_next_ls (lsp, name+1, namelen-1, 433 next+1, len, level+1, oidp)) 434 return (0); 435 namelen = 1; 436 *len = level; 437 continue; 438 } 439 if (((*oidpp)->oid_kind & CTLTYPE) != CTLTYPE_NODE) 440 continue; 441 442 if ((*oidpp)->oid_handler) 443 continue; 444 445 lsp = (struct linker_set*)(*oidpp)->oid_arg1; 446 if (!sysctl_sysctl_next_ls (lsp, name+1, namelen-1, next+1, 447 len, level+1, oidp)) 448 return (0); 449 namelen = 1; 450 *len = level; 451 } 452 return 1; 453} 454 455static int 456sysctl_sysctl_next SYSCTL_HANDLER_ARGS 457{ 458 int *name = (int *) arg1; 459 u_int namelen = arg2; 460 int i, j, error; 461 struct sysctl_oid *oid; 462 struct linker_set *lsp = &sysctl_; 463 int newoid[CTL_MAXNAME]; 464 465 i = sysctl_sysctl_next_ls (lsp, name, namelen, newoid, &j, 1, &oid); 466 if (i) 467 return ENOENT; 468 error = SYSCTL_OUT(req, newoid, j * sizeof (int)); 469 return (error); 470} 471 472SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, ""); 473 474static int 475name2oid (char *name, int *oid, int *len, struct sysctl_oid **oidp) 476{ 477 int i, j; 478 struct sysctl_oid **oidpp; 479 struct linker_set *lsp = &sysctl_; 480 char *p; 481 482 if (!*name) 483 return ENOENT; 484 485 p = name + strlen(name) - 1 ; 486 if (*p == '.') 487 *p = '\0'; 488 489 *len = 0; 490 491 for (p = name; *p && *p != '.'; p++) 492 ; 493 i = *p; 494 if (i == '.') 495 *p = '\0'; 496 497 j = lsp->ls_length; 498 oidpp = (struct sysctl_oid **) lsp->ls_items; 499 500 while (j-- && *len < CTL_MAXNAME) { 501 if (!*oidpp) 502 continue; 503 if (strcmp(name, (*oidpp)->oid_name)) { 504 oidpp++; 505 continue; 506 } 507 *oid++ = (*oidpp)->oid_number; 508 (*len)++; 509 510 if (!i) { 511 if (oidp) 512 *oidp = *oidpp; 513 return (0); 514 } 515 516 if (((*oidpp)->oid_kind & CTLTYPE) != CTLTYPE_NODE) 517 break; 518 519 if ((*oidpp)->oid_handler) 520 break; 521 522 lsp = (struct linker_set*)(*oidpp)->oid_arg1; 523 j = lsp->ls_length; 524 oidpp = (struct sysctl_oid **)lsp->ls_items; 525 name = p+1; 526 for (p = name; *p && *p != '.'; p++) 527 ; 528 i = *p; 529 if (i == '.') 530 *p = '\0'; 531 } 532 return ENOENT; 533} 534 535static int 536sysctl_sysctl_name2oid SYSCTL_HANDLER_ARGS 537{ 538 char *p; 539 int error, oid[CTL_MAXNAME], len; 540 struct sysctl_oid *op = 0; 541 542 if (!req->newlen) 543 return ENOENT; 544 545 p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK); 546 547 error = SYSCTL_IN(req, p, req->newlen); 548 if (error) { 549 free(p, M_SYSCTL); 550 return (error); 551 } 552 553 p [req->newlen] = '\0'; 554 555 error = name2oid(p, oid, &len, &op); 556 557 free(p, M_SYSCTL); 558 559 if (error) 560 return (error); 561 562 error = SYSCTL_OUT(req, oid, len * sizeof *oid); 563 return (error); 564} 565 566SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW, 0, 0, 567 sysctl_sysctl_name2oid, "I", ""); 568 569static int 570sysctl_sysctl_oidfmt SYSCTL_HANDLER_ARGS 571{ 572 int *name = (int *) arg1, error; 573 u_int namelen = arg2; 574 int indx, j; 575 struct sysctl_oid **oidpp; 576 struct linker_set *lsp = &sysctl_; 577 578 j = lsp->ls_length; 579 oidpp = (struct sysctl_oid **) lsp->ls_items; 580 581 indx = 0; 582 while (j-- && indx < CTL_MAXNAME) { 583 if (*oidpp && ((*oidpp)->oid_number == name[indx])) { 584 indx++; 585 if (((*oidpp)->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 586 if ((*oidpp)->oid_handler) 587 goto found; 588 if (indx == namelen) 589 goto found; 590 lsp = (struct linker_set*)(*oidpp)->oid_arg1; 591 j = lsp->ls_length; 592 oidpp = (struct sysctl_oid **)lsp->ls_items; 593 } else { 594 if (indx != namelen) 595 return EISDIR; 596 goto found; 597 } 598 } else { 599 oidpp++; 600 } 601 } 602 return ENOENT; 603found: 604 if (!(*oidpp)->oid_fmt) 605 return ENOENT; 606 error = SYSCTL_OUT(req, 607 &(*oidpp)->oid_kind, sizeof((*oidpp)->oid_kind)); 608 if (!error) 609 error = SYSCTL_OUT(req, (*oidpp)->oid_fmt, 610 strlen((*oidpp)->oid_fmt)+1); 611 return (error); 612} 613 614 615SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD, sysctl_sysctl_oidfmt, ""); 616 617/* 618 * Default "handler" functions. 619 */ 620 621/* 622 * Handle an integer, signed or unsigned. 623 * Two cases: 624 * a variable: point arg1 at it. 625 * a constant: pass it in arg2. 626 */ 627 628int 629sysctl_handle_int SYSCTL_HANDLER_ARGS 630{ 631 int error = 0; 632 633 if (arg1) 634 error = SYSCTL_OUT(req, arg1, sizeof(int)); 635 else if (arg2) 636 error = SYSCTL_OUT(req, &arg2, sizeof(int)); 637 638 if (error || !req->newptr) 639 return (error); 640 641 if (!arg1) 642 error = EPERM; 643 else 644 error = SYSCTL_IN(req, arg1, sizeof(int)); 645 return (error); 646} 647 648/* 649 * Handle our generic '\0' terminated 'C' string. 650 * Two cases: 651 * a variable string: point arg1 at it, arg2 is max length. 652 * a constant string: point arg1 at it, arg2 is zero. 653 */ 654 655int 656sysctl_handle_string SYSCTL_HANDLER_ARGS 657{ 658 int error=0; 659 660 error = SYSCTL_OUT(req, arg1, strlen((char *)arg1)+1); 661 662 if (error || !req->newptr || !arg2) 663 return (error); 664 665 if ((req->newlen - req->newidx) > arg2) { 666 error = E2BIG; 667 } else { 668 arg2 = (req->newlen - req->newidx); 669 error = SYSCTL_IN(req, arg1, arg2); 670 ((char *)arg1)[arg2] = '\0'; 671 } 672 673 return (error); 674} 675 676/* 677 * Handle any kind of opaque data. 678 * arg1 points to it, arg2 is the size. 679 */ 680 681int 682sysctl_handle_opaque SYSCTL_HANDLER_ARGS 683{ 684 int error; 685 686 error = SYSCTL_OUT(req, arg1, arg2); 687 688 if (error || !req->newptr) 689 return (error); 690 691 error = SYSCTL_IN(req, arg1, arg2); 692 693 return (error); 694} 695 696/* 697 * Transfer functions to/from kernel space. 698 * XXX: rather untested at this point 699 */ 700static int 701sysctl_old_kernel(struct sysctl_req *req, void *p, int l) 702{ 703 int i = 0; 704 705 if (req->oldptr) { 706 i = min(req->oldlen - req->oldidx, l); 707 if (i > 0) 708 bcopy(p, req->oldptr + req->oldidx, i); 709 } 710 req->oldidx += l; 711 if (i != l) 712 return (ENOMEM); 713 return (0); 714 715} 716 717static int 718sysctl_new_kernel(struct sysctl_req *req, void *p, int l) 719{ 720 if (!req->newptr) 721 return 0; 722 if (req->newlen - req->newidx < l) 723 return (EINVAL); 724 bcopy(req->newptr + req->newidx, p, l); 725 req->newidx += l; 726 return (0); 727} 728 729/* 730 * Transfer function to/from user space. 731 */ 732static int 733sysctl_old_user(struct sysctl_req *req, void *p, int l) 734{ 735 int error = 0, i = 0; 736 737 if (req->lock == 1 && req->oldptr) { 738 vslock(req->oldptr, req->oldlen); 739 req->lock = 2; 740 } 741 if (req->oldptr) { 742 i = min(req->oldlen - req->oldidx, l); 743 if (i > 0) 744 error = copyout(p, req->oldptr + req->oldidx, i); 745 } 746 req->oldidx += l; 747 if (error) 748 return (error); 749 if (req->oldptr && i < l) 750 return (ENOMEM); 751 return (0); 752} 753 754static int 755sysctl_new_user(struct sysctl_req *req, void *p, int l) 756{ 757 int error; 758 759 if (!req->newptr) 760 return 0; 761 if (req->newlen - req->newidx < l) 762 return (EINVAL); 763 error = copyin(req->newptr + req->newidx, p, l); 764 req->newidx += l; 765 return (error); 766} 767 768/* 769 * Traverse our tree, and find the right node, execute whatever it points 770 * at, and return the resulting error code. 771 */ 772 773int 774sysctl_root SYSCTL_HANDLER_ARGS 775{ 776 int *name = (int *) arg1; 777 u_int namelen = arg2; 778 int indx, i, j; 779 struct sysctl_oid **oidpp; 780 struct linker_set *lsp = &sysctl_; 781 782 j = lsp->ls_length; 783 oidpp = (struct sysctl_oid **) lsp->ls_items; 784 785 indx = 0; 786 while (j-- && indx < CTL_MAXNAME) { 787 if (*oidpp && ((*oidpp)->oid_number == name[indx])) { 788 indx++; 789 if ((*oidpp)->oid_kind & CTLFLAG_NOLOCK) 790 req->lock = 0; 791 if (((*oidpp)->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 792 if ((*oidpp)->oid_handler) 793 goto found; 794 if (indx == namelen) 795 return ENOENT; 796 lsp = (struct linker_set*)(*oidpp)->oid_arg1; 797 j = lsp->ls_length; 798 oidpp = (struct sysctl_oid **)lsp->ls_items; 799 } else { 800 if (indx != namelen) 801 return EISDIR; 802 goto found; 803 } 804 } else { 805 oidpp++; 806 } 807 } 808 return ENOENT; 809found: 810 811 /* If writing isn't allowed */ 812 if (req->newptr && !((*oidpp)->oid_kind & CTLFLAG_WR)) 813 return (EPERM); 814 815 if (!(*oidpp)->oid_handler) 816 return EINVAL; 817 818 if (((*oidpp)->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 819 i = ((*oidpp)->oid_handler) (*oidpp, 820 name + indx, namelen - indx, 821 req); 822 } else { 823 i = ((*oidpp)->oid_handler) (*oidpp, 824 (*oidpp)->oid_arg1, (*oidpp)->oid_arg2, 825 req); 826 } 827 return (i); 828} 829 830#ifndef _SYS_SYSPROTO_H_ 831struct sysctl_args { 832 int *name; 833 u_int namelen; 834 void *old; 835 size_t *oldlenp; 836 void *new; 837 size_t newlen; 838}; 839#endif 840 841int 842__sysctl(struct proc *p, struct sysctl_args *uap, int *retval) 843{ 844 int error, i, j, name[CTL_MAXNAME]; 845 846 if (uap->namelen > CTL_MAXNAME || uap->namelen < 2) 847 return (EINVAL); 848 849 error = copyin(uap->name, &name, uap->namelen * sizeof(int)); 850 if (error) 851 return (error); 852 853 error = userland_sysctl(p, name, uap->namelen, 854 uap->old, uap->oldlenp, 0, 855 uap->new, uap->newlen, &j); 856 if (error && error != ENOMEM) 857 return (error); 858 if (uap->oldlenp) { 859 i = copyout(&j, uap->oldlenp, sizeof(j)); 860 if (i) 861 return (i); 862 } 863 return (error); 864} 865 866/* 867 * This is used from various compatibility syscalls too. That's why name 868 * must be in kernel space. 869 */ 870int 871userland_sysctl(struct proc *p, int *name, u_int namelen, void *old, size_t *oldlenp, int inkernel, void *new, size_t newlen, int *retval) 872{ 873 int error = 0; 874 struct sysctl_req req; 875 876 bzero(&req, sizeof req); 877 878 req.p = p; 879 880 if (new != NULL && (error = suser(p->p_ucred, &p->p_acflag))) 881 return (error); 882 883 if (oldlenp) { 884 if (inkernel) { 885 req.oldlen = *oldlenp; 886 } else { 887 error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp)); 888 if (error) 889 return (error); 890 } 891 } 892 893 if (old) { 894 if (!useracc(old, req.oldlen, B_WRITE)) 895 return (EFAULT); 896 req.oldptr= old; 897 } 898 899 if (newlen) { 900 if (!useracc(new, req.newlen, B_READ)) 901 return (EFAULT); 902 req.newlen = newlen; 903 req.newptr = new; 904 } 905 906 req.oldfunc = sysctl_old_user; 907 req.newfunc = sysctl_new_user; 908 req.lock = 1; 909 910 /* XXX this should probably be done in a general way */ 911 while (memlock.sl_lock) { 912 memlock.sl_want = 1; 913 (void) tsleep((caddr_t)&memlock, PRIBIO+1, "sysctl", 0); 914 memlock.sl_locked++; 915 } 916 memlock.sl_lock = 1; 917 918 error = sysctl_root(0, name, namelen, &req); 919 920 if (req.lock == 2) 921 vsunlock(req.oldptr, req.oldlen, B_WRITE); 922 923 memlock.sl_lock = 0; 924 925 if (memlock.sl_want) { 926 memlock.sl_want = 0; 927 wakeup((caddr_t)&memlock); 928 } 929 930 if (error && error != ENOMEM) 931 return (error); 932 933 if (retval) { 934 if (req.oldptr && req.oldidx > req.oldlen) 935 *retval = req.oldlen; 936 else 937 *retval = req.oldidx; 938 } 939 return (error); 940} 941 942#ifdef COMPAT_43 943#include <sys/socket.h> 944#define KINFO_PROC (0<<8) 945#define KINFO_RT (1<<8) 946#define KINFO_VNODE (2<<8) 947#define KINFO_FILE (3<<8) 948#define KINFO_METER (4<<8) 949#define KINFO_LOADAVG (5<<8) 950#define KINFO_CLOCKRATE (6<<8) 951 952/* Non-standard BSDI extension - only present on their 4.3 net-2 releases */ 953#define KINFO_BSDI_SYSINFO (101<<8) 954 955/* 956 * XXX this is bloat, but I hope it's better here than on the potentially 957 * limited kernel stack... -Peter 958 */ 959 960static struct { 961 int bsdi_machine; /* "i386" on BSD/386 */ 962/* ^^^ this is an offset to the string, relative to the struct start */ 963 char *pad0; 964 long pad1; 965 long pad2; 966 long pad3; 967 u_long pad4; 968 u_long pad5; 969 u_long pad6; 970 971 int bsdi_ostype; /* "BSD/386" on BSD/386 */ 972 int bsdi_osrelease; /* "1.1" on BSD/386 */ 973 long pad7; 974 long pad8; 975 char *pad9; 976 977 long pad10; 978 long pad11; 979 int pad12; 980 long pad13; 981 quad_t pad14; 982 long pad15; 983 984 struct timeval pad16; 985 /* we dont set this, because BSDI's uname used gethostname() instead */ 986 int bsdi_hostname; /* hostname on BSD/386 */ 987 988 /* the actual string data is appended here */ 989 990} bsdi_si; 991/* 992 * this data is appended to the end of the bsdi_si structure during copyout. 993 * The "char *" offsets are relative to the base of the bsdi_si struct. 994 * This contains "FreeBSD\02.0-BUILT-nnnnnn\0i386\0", and these strings 995 * should not exceed the length of the buffer here... (or else!! :-) 996 */ 997static char bsdi_strings[80]; /* It had better be less than this! */ 998 999#ifndef _SYS_SYSPROTO_H_ 1000struct getkerninfo_args { 1001 int op; 1002 char *where; 1003 int *size; 1004 int arg; 1005}; 1006#endif 1007 1008int 1009ogetkerninfo(struct proc *p, struct getkerninfo_args *uap, int *retval) 1010{ 1011 int error, name[6]; 1012 u_int size; 1013 1014 switch (uap->op & 0xff00) { 1015 1016 case KINFO_RT: 1017 name[0] = CTL_NET; 1018 name[1] = PF_ROUTE; 1019 name[2] = 0; 1020 name[3] = (uap->op & 0xff0000) >> 16; 1021 name[4] = uap->op & 0xff; 1022 name[5] = uap->arg; 1023 error = userland_sysctl(p, name, 6, uap->where, uap->size, 1024 0, 0, 0, &size); 1025 break; 1026 1027 case KINFO_VNODE: 1028 name[0] = CTL_KERN; 1029 name[1] = KERN_VNODE; 1030 error = userland_sysctl(p, name, 2, uap->where, uap->size, 1031 0, 0, 0, &size); 1032 break; 1033 1034 case KINFO_PROC: 1035 name[0] = CTL_KERN; 1036 name[1] = KERN_PROC; 1037 name[2] = uap->op & 0xff; 1038 name[3] = uap->arg; 1039 error = userland_sysctl(p, name, 4, uap->where, uap->size, 1040 0, 0, 0, &size); 1041 break; 1042 1043 case KINFO_FILE: 1044 name[0] = CTL_KERN; 1045 name[1] = KERN_FILE; 1046 error = userland_sysctl(p, name, 2, uap->where, uap->size, 1047 0, 0, 0, &size); 1048 break; 1049 1050 case KINFO_METER: 1051 name[0] = CTL_VM; 1052 name[1] = VM_METER; 1053 error = userland_sysctl(p, name, 2, uap->where, uap->size, 1054 0, 0, 0, &size); 1055 break; 1056 1057 case KINFO_LOADAVG: 1058 name[0] = CTL_VM; 1059 name[1] = VM_LOADAVG; 1060 error = userland_sysctl(p, name, 2, uap->where, uap->size, 1061 0, 0, 0, &size); 1062 break; 1063 1064 case KINFO_CLOCKRATE: 1065 name[0] = CTL_KERN; 1066 name[1] = KERN_CLOCKRATE; 1067 error = userland_sysctl(p, name, 2, uap->where, uap->size, 1068 0, 0, 0, &size); 1069 break; 1070 1071 case KINFO_BSDI_SYSINFO: { 1072 /* 1073 * this is pretty crude, but it's just enough for uname() 1074 * from BSDI's 1.x libc to work. 1075 * 1076 * In particular, it doesn't return the same results when 1077 * the supplied buffer is too small. BSDI's version apparently 1078 * will return the amount copied, and set the *size to how 1079 * much was needed. The emulation framework here isn't capable 1080 * of that, so we just set both to the amount copied. 1081 * BSDI's 2.x product apparently fails with ENOMEM in this 1082 * scenario. 1083 */ 1084 1085 u_int needed; 1086 u_int left; 1087 char *s; 1088 1089 bzero((char *)&bsdi_si, sizeof(bsdi_si)); 1090 bzero(bsdi_strings, sizeof(bsdi_strings)); 1091 1092 s = bsdi_strings; 1093 1094 bsdi_si.bsdi_ostype = (s - bsdi_strings) + sizeof(bsdi_si); 1095 strcpy(s, ostype); 1096 s += strlen(s) + 1; 1097 1098 bsdi_si.bsdi_osrelease = (s - bsdi_strings) + sizeof(bsdi_si); 1099 strcpy(s, osrelease); 1100 s += strlen(s) + 1; 1101 1102 bsdi_si.bsdi_machine = (s - bsdi_strings) + sizeof(bsdi_si); 1103 strcpy(s, machine); 1104 s += strlen(s) + 1; 1105 1106 needed = sizeof(bsdi_si) + (s - bsdi_strings); 1107 1108 if (uap->where == NULL) { 1109 /* process is asking how much buffer to supply.. */ 1110 size = needed; 1111 error = 0; 1112 break; 1113 } 1114 1115 1116 /* if too much buffer supplied, trim it down */ 1117 if (size > needed) 1118 size = needed; 1119 1120 /* how much of the buffer is remaining */ 1121 left = size; 1122 1123 if ((error = copyout((char *)&bsdi_si, uap->where, left)) != 0) 1124 break; 1125 1126 /* is there any point in continuing? */ 1127 if (left > sizeof(bsdi_si)) { 1128 left -= sizeof(bsdi_si); 1129 error = copyout(&bsdi_strings, 1130 uap->where + sizeof(bsdi_si), left); 1131 } 1132 break; 1133 } 1134 1135 default: 1136 return (EOPNOTSUPP); 1137 } 1138 if (error) 1139 return (error); 1140 *retval = size; 1141 if (uap->size) 1142 error = copyout((caddr_t)&size, (caddr_t)uap->size, 1143 sizeof(size)); 1144 return (error); 1145} 1146#endif /* COMPAT_43 */ 1147