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