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