kern_sysctl.c (286094) | kern_sysctl.c (287835) |
---|---|
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 --- 22 unchanged lines hidden (view full) --- 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 */ 37 38#include <sys/cdefs.h> | 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 --- 22 unchanged lines hidden (view full) --- 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 */ 37 38#include <sys/cdefs.h> |
39__FBSDID("$FreeBSD: head/sys/kern/kern_sysctl.c 286094 2015-07-30 19:52:43Z mjg $"); | 39__FBSDID("$FreeBSD: head/sys/kern/kern_sysctl.c 287835 2015-09-15 23:06:56Z mjg $"); |
40 41#include "opt_capsicum.h" 42#include "opt_compat.h" 43#include "opt_ktrace.h" 44 45#include <sys/param.h> 46#include <sys/fail.h> 47#include <sys/systm.h> 48#include <sys/capsicum.h> 49#include <sys/kernel.h> 50#include <sys/sysctl.h> 51#include <sys/malloc.h> 52#include <sys/priv.h> 53#include <sys/proc.h> 54#include <sys/jail.h> 55#include <sys/lock.h> 56#include <sys/mutex.h> | 40 41#include "opt_capsicum.h" 42#include "opt_compat.h" 43#include "opt_ktrace.h" 44 45#include <sys/param.h> 46#include <sys/fail.h> 47#include <sys/systm.h> 48#include <sys/capsicum.h> 49#include <sys/kernel.h> 50#include <sys/sysctl.h> 51#include <sys/malloc.h> 52#include <sys/priv.h> 53#include <sys/proc.h> 54#include <sys/jail.h> 55#include <sys/lock.h> 56#include <sys/mutex.h> |
57#include <sys/rmlock.h> |
|
57#include <sys/sbuf.h> 58#include <sys/sx.h> 59#include <sys/sysproto.h> 60#include <sys/uio.h> 61#ifdef KTRACE 62#include <sys/ktrace.h> 63#endif 64 --- 7 unchanged lines hidden (view full) --- 72static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic"); 73static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids"); 74static MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer"); 75 76/* 77 * The sysctllock protects the MIB tree. It also protects sysctl 78 * contexts used with dynamic sysctls. The sysctl_register_oid() and 79 * sysctl_unregister_oid() routines require the sysctllock to already | 58#include <sys/sbuf.h> 59#include <sys/sx.h> 60#include <sys/sysproto.h> 61#include <sys/uio.h> 62#ifdef KTRACE 63#include <sys/ktrace.h> 64#endif 65 --- 7 unchanged lines hidden (view full) --- 73static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic"); 74static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids"); 75static MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer"); 76 77/* 78 * The sysctllock protects the MIB tree. It also protects sysctl 79 * contexts used with dynamic sysctls. The sysctl_register_oid() and 80 * sysctl_unregister_oid() routines require the sysctllock to already |
80 * be held, so the sysctl_xlock() and sysctl_xunlock() routines are | 81 * be held, so the sysctl_wlock() and sysctl_wunlock() routines are |
81 * provided for the few places in the kernel which need to use that 82 * API rather than using the dynamic API. Use of the dynamic API is 83 * strongly encouraged for most code. 84 * 85 * The sysctlmemlock is used to limit the amount of user memory wired for 86 * sysctl requests. This is implemented by serializing any userland 87 * sysctl requests larger than a single page via an exclusive lock. 88 */ | 82 * provided for the few places in the kernel which need to use that 83 * API rather than using the dynamic API. Use of the dynamic API is 84 * strongly encouraged for most code. 85 * 86 * The sysctlmemlock is used to limit the amount of user memory wired for 87 * sysctl requests. This is implemented by serializing any userland 88 * sysctl requests larger than a single page via an exclusive lock. 89 */ |
89static struct sx sysctllock; | 90static struct rmlock sysctllock; |
90static struct sx sysctlmemlock; 91 | 91static struct sx sysctlmemlock; 92 |
92#define SYSCTL_XLOCK() sx_xlock(&sysctllock) 93#define SYSCTL_XUNLOCK() sx_xunlock(&sysctllock) 94#define SYSCTL_SLOCK() sx_slock(&sysctllock) 95#define SYSCTL_SUNLOCK() sx_sunlock(&sysctllock) 96#define SYSCTL_XLOCKED() sx_xlocked(&sysctllock) 97#define SYSCTL_ASSERT_LOCKED() sx_assert(&sysctllock, SA_LOCKED) 98#define SYSCTL_ASSERT_XLOCKED() sx_assert(&sysctllock, SA_XLOCKED) 99#define SYSCTL_ASSERT_SLOCKED() sx_assert(&sysctllock, SA_SLOCKED) 100#define SYSCTL_INIT() sx_init(&sysctllock, "sysctl lock") | 93#define SYSCTL_WLOCK() rm_wlock(&sysctllock) 94#define SYSCTL_WUNLOCK() rm_wunlock(&sysctllock) 95#define SYSCTL_RLOCK(tracker) rm_rlock(&sysctllock, (tracker)) 96#define SYSCTL_RUNLOCK(tracker) rm_runlock(&sysctllock, (tracker)) 97#define SYSCTL_WLOCKED() rm_wowned(&sysctllock) 98#define SYSCTL_ASSERT_LOCKED() rm_assert(&sysctllock, RA_LOCKED) 99#define SYSCTL_ASSERT_WLOCKED() rm_assert(&sysctllock, RA_WLOCKED) 100#define SYSCTL_ASSERT_RLOCKED() rm_assert(&sysctllock, RA_RLOCKED) 101#define SYSCTL_INIT() rm_init_flags(&sysctllock, "sysctl lock", \ 102 RM_SLEEPABLE) |
101#define SYSCTL_SLEEP(ch, wmesg, timo) \ | 103#define SYSCTL_SLEEP(ch, wmesg, timo) \ |
102 sx_sleep(ch, &sysctllock, 0, wmesg, timo) | 104 rm_sleep(ch, &sysctllock, 0, wmesg, timo) |
103 104static int sysctl_root(SYSCTL_HANDLER_ARGS); 105 106/* Root list */ 107struct sysctl_oid_list sysctl__children = SLIST_HEAD_INITIALIZER(&sysctl__children); 108 109static int sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, 110 int recurse); 111static int sysctl_old_kernel(struct sysctl_req *, const void *, size_t); 112static int sysctl_new_kernel(struct sysctl_req *, void *, size_t); 113 | 105 106static int sysctl_root(SYSCTL_HANDLER_ARGS); 107 108/* Root list */ 109struct sysctl_oid_list sysctl__children = SLIST_HEAD_INITIALIZER(&sysctl__children); 110 111static int sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, 112 int recurse); 113static int sysctl_old_kernel(struct sysctl_req *, const void *, size_t); 114static int sysctl_new_kernel(struct sysctl_req *, void *, size_t); 115 |
114static void 115sysctl_lock(bool xlock) 116{ 117 118 if (xlock) 119 SYSCTL_XLOCK(); 120 else 121 SYSCTL_SLOCK(); 122} 123 124static bool 125sysctl_unlock(void) 126{ 127 bool xlocked; 128 129 xlocked = SYSCTL_XLOCKED(); 130 if (xlocked) 131 SYSCTL_XUNLOCK(); 132 else 133 SYSCTL_SUNLOCK(); 134 return (xlocked); 135} 136 | |
137static struct sysctl_oid * 138sysctl_find_oidname(const char *name, struct sysctl_oid_list *list) 139{ 140 struct sysctl_oid *oidp; 141 142 SYSCTL_ASSERT_LOCKED(); 143 SLIST_FOREACH(oidp, list, oid_link) { 144 if (strcmp(oidp->oid_name, name) == 0) { --- 4 unchanged lines hidden (view full) --- 149} 150 151/* 152 * Initialization of the MIB tree. 153 * 154 * Order by number in each list. 155 */ 156void | 116static struct sysctl_oid * 117sysctl_find_oidname(const char *name, struct sysctl_oid_list *list) 118{ 119 struct sysctl_oid *oidp; 120 121 SYSCTL_ASSERT_LOCKED(); 122 SLIST_FOREACH(oidp, list, oid_link) { 123 if (strcmp(oidp->oid_name, name) == 0) { --- 4 unchanged lines hidden (view full) --- 128} 129 130/* 131 * Initialization of the MIB tree. 132 * 133 * Order by number in each list. 134 */ 135void |
157sysctl_xlock(void) | 136sysctl_wlock(void) |
158{ 159 | 137{ 138 |
160 SYSCTL_XLOCK(); | 139 SYSCTL_WLOCK(); |
161} 162 163void | 140} 141 142void |
164sysctl_xunlock(void) | 143sysctl_wunlock(void) |
165{ 166 | 144{ 145 |
167 SYSCTL_XUNLOCK(); | 146 SYSCTL_WUNLOCK(); |
168} 169 170static int 171sysctl_root_handler_locked(struct sysctl_oid *oid, void *arg1, intptr_t arg2, | 147} 148 149static int 150sysctl_root_handler_locked(struct sysctl_oid *oid, void *arg1, intptr_t arg2, |
172 struct sysctl_req *req) | 151 struct sysctl_req *req, struct rm_priotracker *tracker) |
173{ 174 int error; | 152{ 153 int error; |
175 bool xlocked; | |
176 177 if (oid->oid_kind & CTLFLAG_DYN) 178 atomic_add_int(&oid->oid_running, 1); | 154 155 if (oid->oid_kind & CTLFLAG_DYN) 156 atomic_add_int(&oid->oid_running, 1); |
179 xlocked = sysctl_unlock(); | |
180 | 157 |
158 if (tracker != NULL) 159 SYSCTL_RUNLOCK(tracker); 160 else 161 SYSCTL_WUNLOCK(); 162 |
|
181 if (!(oid->oid_kind & CTLFLAG_MPSAFE)) 182 mtx_lock(&Giant); 183 error = oid->oid_handler(oid, arg1, arg2, req); 184 if (!(oid->oid_kind & CTLFLAG_MPSAFE)) 185 mtx_unlock(&Giant); 186 | 163 if (!(oid->oid_kind & CTLFLAG_MPSAFE)) 164 mtx_lock(&Giant); 165 error = oid->oid_handler(oid, arg1, arg2, req); 166 if (!(oid->oid_kind & CTLFLAG_MPSAFE)) 167 mtx_unlock(&Giant); 168 |
187 sysctl_lock(xlocked); | 169 if (tracker != NULL) 170 SYSCTL_RLOCK(tracker); 171 else 172 SYSCTL_WLOCK(); 173 |
188 if (oid->oid_kind & CTLFLAG_DYN) { 189 if (atomic_fetchadd_int(&oid->oid_running, -1) == 1 && 190 (oid->oid_kind & CTLFLAG_DYING) != 0) 191 wakeup(&oid->oid_running); 192 } 193 194 return (error); 195} --- 82 unchanged lines hidden (view full) --- 278 return; 279 req.newlen = strlen(penv); 280 req.newptr = penv; 281 break; 282 default: 283 return; 284 } 285 error = sysctl_root_handler_locked(oidp, oidp->oid_arg1, | 174 if (oid->oid_kind & CTLFLAG_DYN) { 175 if (atomic_fetchadd_int(&oid->oid_running, -1) == 1 && 176 (oid->oid_kind & CTLFLAG_DYING) != 0) 177 wakeup(&oid->oid_running); 178 } 179 180 return (error); 181} --- 82 unchanged lines hidden (view full) --- 264 return; 265 req.newlen = strlen(penv); 266 req.newptr = penv; 267 break; 268 default: 269 return; 270 } 271 error = sysctl_root_handler_locked(oidp, oidp->oid_arg1, |
286 oidp->oid_arg2, &req); | 272 oidp->oid_arg2, &req, NULL); |
287 if (error != 0) 288 printf("Setting sysctl %s failed: %d\n", path + rem, error); 289 if (penv != NULL) 290 freeenv(penv); 291} 292 293void 294sysctl_register_oid(struct sysctl_oid *oidp) 295{ 296 struct sysctl_oid_list *parent = oidp->oid_parent; 297 struct sysctl_oid *p; 298 struct sysctl_oid *q; 299 int oid_number; 300 int timeout = 2; 301 302 /* 303 * First check if another oid with the same name already 304 * exists in the parent's list. 305 */ | 273 if (error != 0) 274 printf("Setting sysctl %s failed: %d\n", path + rem, error); 275 if (penv != NULL) 276 freeenv(penv); 277} 278 279void 280sysctl_register_oid(struct sysctl_oid *oidp) 281{ 282 struct sysctl_oid_list *parent = oidp->oid_parent; 283 struct sysctl_oid *p; 284 struct sysctl_oid *q; 285 int oid_number; 286 int timeout = 2; 287 288 /* 289 * First check if another oid with the same name already 290 * exists in the parent's list. 291 */ |
306 SYSCTL_ASSERT_XLOCKED(); | 292 SYSCTL_ASSERT_WLOCKED(); |
307 p = sysctl_find_oidname(oidp->oid_name, parent); 308 if (p != NULL) { 309 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 310 p->oid_refcnt++; 311 return; 312 } else { 313 printf("can't re-use a leaf (%s)!\n", p->oid_name); 314 return; --- 77 unchanged lines hidden (view full) --- 392} 393 394void 395sysctl_unregister_oid(struct sysctl_oid *oidp) 396{ 397 struct sysctl_oid *p; 398 int error; 399 | 293 p = sysctl_find_oidname(oidp->oid_name, parent); 294 if (p != NULL) { 295 if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 296 p->oid_refcnt++; 297 return; 298 } else { 299 printf("can't re-use a leaf (%s)!\n", p->oid_name); 300 return; --- 77 unchanged lines hidden (view full) --- 378} 379 380void 381sysctl_unregister_oid(struct sysctl_oid *oidp) 382{ 383 struct sysctl_oid *p; 384 int error; 385 |
400 SYSCTL_ASSERT_XLOCKED(); | 386 SYSCTL_ASSERT_WLOCKED(); |
401 error = ENOENT; 402 if (oidp->oid_number == OID_AUTO) { 403 error = EINVAL; 404 } else { 405 SLIST_FOREACH(p, oidp->oid_parent, oid_link) { 406 if (p == oidp) { 407 SLIST_REMOVE(oidp->oid_parent, oidp, 408 sysctl_oid, oid_link); --- 39 unchanged lines hidden (view full) --- 448 449 error = 0; 450 /* 451 * First perform a "dry run" to check if it's ok to remove oids. 452 * XXX FIXME 453 * XXX This algorithm is a hack. But I don't know any 454 * XXX better solution for now... 455 */ | 387 error = ENOENT; 388 if (oidp->oid_number == OID_AUTO) { 389 error = EINVAL; 390 } else { 391 SLIST_FOREACH(p, oidp->oid_parent, oid_link) { 392 if (p == oidp) { 393 SLIST_REMOVE(oidp->oid_parent, oidp, 394 sysctl_oid, oid_link); --- 39 unchanged lines hidden (view full) --- 434 435 error = 0; 436 /* 437 * First perform a "dry run" to check if it's ok to remove oids. 438 * XXX FIXME 439 * XXX This algorithm is a hack. But I don't know any 440 * XXX better solution for now... 441 */ |
456 SYSCTL_XLOCK(); | 442 SYSCTL_WLOCK(); |
457 TAILQ_FOREACH(e, clist, link) { 458 error = sysctl_remove_oid_locked(e->entry, 0, 0); 459 if (error) 460 break; 461 } 462 /* 463 * Restore deregistered entries, either from the end, 464 * or from the place where error occured. 465 * e contains the entry that was not unregistered 466 */ 467 if (error) 468 e1 = TAILQ_PREV(e, sysctl_ctx_list, link); 469 else 470 e1 = TAILQ_LAST(clist, sysctl_ctx_list); 471 while (e1 != NULL) { 472 sysctl_register_oid(e1->entry); 473 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link); 474 } 475 if (error) { | 443 TAILQ_FOREACH(e, clist, link) { 444 error = sysctl_remove_oid_locked(e->entry, 0, 0); 445 if (error) 446 break; 447 } 448 /* 449 * Restore deregistered entries, either from the end, 450 * or from the place where error occured. 451 * e contains the entry that was not unregistered 452 */ 453 if (error) 454 e1 = TAILQ_PREV(e, sysctl_ctx_list, link); 455 else 456 e1 = TAILQ_LAST(clist, sysctl_ctx_list); 457 while (e1 != NULL) { 458 sysctl_register_oid(e1->entry); 459 e1 = TAILQ_PREV(e1, sysctl_ctx_list, link); 460 } 461 if (error) { |
476 SYSCTL_XUNLOCK(); | 462 SYSCTL_WUNLOCK(); |
477 return(EBUSY); 478 } 479 /* Now really delete the entries */ 480 e = TAILQ_FIRST(clist); 481 while (e != NULL) { 482 e1 = TAILQ_NEXT(e, link); 483 error = sysctl_remove_oid_locked(e->entry, 1, 0); 484 if (error) 485 panic("sysctl_remove_oid: corrupt tree, entry: %s", 486 e->entry->oid_name); 487 free(e, M_SYSCTLOID); 488 e = e1; 489 } | 463 return(EBUSY); 464 } 465 /* Now really delete the entries */ 466 e = TAILQ_FIRST(clist); 467 while (e != NULL) { 468 e1 = TAILQ_NEXT(e, link); 469 error = sysctl_remove_oid_locked(e->entry, 1, 0); 470 if (error) 471 panic("sysctl_remove_oid: corrupt tree, entry: %s", 472 e->entry->oid_name); 473 free(e, M_SYSCTLOID); 474 e = e1; 475 } |
490 SYSCTL_XUNLOCK(); | 476 SYSCTL_WUNLOCK(); |
491 return (error); 492} 493 494/* Add an entry to the context */ 495struct sysctl_ctx_entry * 496sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp) 497{ 498 struct sysctl_ctx_entry *e; 499 | 477 return (error); 478} 479 480/* Add an entry to the context */ 481struct sysctl_ctx_entry * 482sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp) 483{ 484 struct sysctl_ctx_entry *e; 485 |
500 SYSCTL_ASSERT_XLOCKED(); | 486 SYSCTL_ASSERT_WLOCKED(); |
501 if (clist == NULL || oidp == NULL) 502 return(NULL); 503 e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK); 504 e->entry = oidp; 505 TAILQ_INSERT_HEAD(clist, e, link); 506 return (e); 507} 508 509/* Find an entry in the context */ 510struct sysctl_ctx_entry * 511sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp) 512{ 513 struct sysctl_ctx_entry *e; 514 | 487 if (clist == NULL || oidp == NULL) 488 return(NULL); 489 e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK); 490 e->entry = oidp; 491 TAILQ_INSERT_HEAD(clist, e, link); 492 return (e); 493} 494 495/* Find an entry in the context */ 496struct sysctl_ctx_entry * 497sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp) 498{ 499 struct sysctl_ctx_entry *e; 500 |
515 SYSCTL_ASSERT_XLOCKED(); | 501 SYSCTL_ASSERT_WLOCKED(); |
516 if (clist == NULL || oidp == NULL) 517 return(NULL); 518 TAILQ_FOREACH(e, clist, link) { 519 if(e->entry == oidp) 520 return(e); 521 } 522 return (e); 523} --- 5 unchanged lines hidden (view full) --- 529 */ 530int 531sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp) 532{ 533 struct sysctl_ctx_entry *e; 534 535 if (clist == NULL || oidp == NULL) 536 return (EINVAL); | 502 if (clist == NULL || oidp == NULL) 503 return(NULL); 504 TAILQ_FOREACH(e, clist, link) { 505 if(e->entry == oidp) 506 return(e); 507 } 508 return (e); 509} --- 5 unchanged lines hidden (view full) --- 515 */ 516int 517sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp) 518{ 519 struct sysctl_ctx_entry *e; 520 521 if (clist == NULL || oidp == NULL) 522 return (EINVAL); |
537 SYSCTL_XLOCK(); | 523 SYSCTL_WLOCK(); |
538 e = sysctl_ctx_entry_find(clist, oidp); 539 if (e != NULL) { 540 TAILQ_REMOVE(clist, e, link); | 524 e = sysctl_ctx_entry_find(clist, oidp); 525 if (e != NULL) { 526 TAILQ_REMOVE(clist, e, link); |
541 SYSCTL_XUNLOCK(); | 527 SYSCTL_WUNLOCK(); |
542 free(e, M_SYSCTLOID); 543 return (0); 544 } else { | 528 free(e, M_SYSCTLOID); 529 return (0); 530 } else { |
545 SYSCTL_XUNLOCK(); | 531 SYSCTL_WUNLOCK(); |
546 return (ENOENT); 547 } 548} 549 550/* 551 * Remove dynamically created sysctl trees. 552 * oidp - top of the tree to be removed 553 * del - if 0 - just deregister, otherwise free up entries as well 554 * recurse - if != 0 traverse the subtree to be deleted 555 */ 556int 557sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse) 558{ 559 int error; 560 | 532 return (ENOENT); 533 } 534} 535 536/* 537 * Remove dynamically created sysctl trees. 538 * oidp - top of the tree to be removed 539 * del - if 0 - just deregister, otherwise free up entries as well 540 * recurse - if != 0 traverse the subtree to be deleted 541 */ 542int 543sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse) 544{ 545 int error; 546 |
561 SYSCTL_XLOCK(); | 547 SYSCTL_WLOCK(); |
562 error = sysctl_remove_oid_locked(oidp, del, recurse); | 548 error = sysctl_remove_oid_locked(oidp, del, recurse); |
563 SYSCTL_XUNLOCK(); | 549 SYSCTL_WUNLOCK(); |
564 return (error); 565} 566 567int 568sysctl_remove_name(struct sysctl_oid *parent, const char *name, 569 int del, int recurse) 570{ 571 struct sysctl_oid *p, *tmp; 572 int error; 573 574 error = ENOENT; | 550 return (error); 551} 552 553int 554sysctl_remove_name(struct sysctl_oid *parent, const char *name, 555 int del, int recurse) 556{ 557 struct sysctl_oid *p, *tmp; 558 int error; 559 560 error = ENOENT; |
575 SYSCTL_XLOCK(); | 561 SYSCTL_WLOCK(); |
576 SLIST_FOREACH_SAFE(p, SYSCTL_CHILDREN(parent), oid_link, tmp) { 577 if (strcmp(p->oid_name, name) == 0) { 578 error = sysctl_remove_oid_locked(p, del, recurse); 579 break; 580 } 581 } | 562 SLIST_FOREACH_SAFE(p, SYSCTL_CHILDREN(parent), oid_link, tmp) { 563 if (strcmp(p->oid_name, name) == 0) { 564 error = sysctl_remove_oid_locked(p, del, recurse); 565 break; 566 } 567 } |
582 SYSCTL_XUNLOCK(); | 568 SYSCTL_WUNLOCK(); |
583 584 return (error); 585} 586 587 588static int 589sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse) 590{ 591 struct sysctl_oid *p, *tmp; 592 int error; 593 | 569 570 return (error); 571} 572 573 574static int 575sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse) 576{ 577 struct sysctl_oid *p, *tmp; 578 int error; 579 |
594 SYSCTL_ASSERT_XLOCKED(); | 580 SYSCTL_ASSERT_WLOCKED(); |
595 if (oidp == NULL) 596 return(EINVAL); 597 if ((oidp->oid_kind & CTLFLAG_DYN) == 0) { 598 printf("can't remove non-dynamic nodes!\n"); 599 return (EINVAL); 600 } 601 /* 602 * WARNING: normal method to do this should be through --- 58 unchanged lines hidden (view full) --- 661 int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr) 662{ 663 struct sysctl_oid *oidp; 664 665 /* You have to hook up somewhere.. */ 666 if (parent == NULL) 667 return(NULL); 668 /* Check if the node already exists, otherwise create it */ | 581 if (oidp == NULL) 582 return(EINVAL); 583 if ((oidp->oid_kind & CTLFLAG_DYN) == 0) { 584 printf("can't remove non-dynamic nodes!\n"); 585 return (EINVAL); 586 } 587 /* 588 * WARNING: normal method to do this should be through --- 58 unchanged lines hidden (view full) --- 647 int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr) 648{ 649 struct sysctl_oid *oidp; 650 651 /* You have to hook up somewhere.. */ 652 if (parent == NULL) 653 return(NULL); 654 /* Check if the node already exists, otherwise create it */ |
669 SYSCTL_XLOCK(); | 655 SYSCTL_WLOCK(); |
670 oidp = sysctl_find_oidname(name, parent); 671 if (oidp != NULL) { 672 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 673 oidp->oid_refcnt++; 674 /* Update the context */ 675 if (clist != NULL) 676 sysctl_ctx_entry_add(clist, oidp); | 656 oidp = sysctl_find_oidname(name, parent); 657 if (oidp != NULL) { 658 if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 659 oidp->oid_refcnt++; 660 /* Update the context */ 661 if (clist != NULL) 662 sysctl_ctx_entry_add(clist, oidp); |
677 SYSCTL_XUNLOCK(); | 663 SYSCTL_WUNLOCK(); |
678 return (oidp); 679 } else { | 664 return (oidp); 665 } else { |
680 SYSCTL_XUNLOCK(); | 666 SYSCTL_WUNLOCK(); |
681 printf("can't re-use a leaf (%s)!\n", name); 682 return (NULL); 683 } 684 } 685 oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO); 686 oidp->oid_parent = parent; 687 SLIST_INIT(&oidp->oid_children); 688 oidp->oid_number = number; --- 6 unchanged lines hidden (view full) --- 695 oidp->oid_fmt = fmt; 696 if (descr != NULL) 697 oidp->oid_descr = strdup(descr, M_SYSCTLOID); 698 /* Update the context, if used */ 699 if (clist != NULL) 700 sysctl_ctx_entry_add(clist, oidp); 701 /* Register this oid */ 702 sysctl_register_oid(oidp); | 667 printf("can't re-use a leaf (%s)!\n", name); 668 return (NULL); 669 } 670 } 671 oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO); 672 oidp->oid_parent = parent; 673 SLIST_INIT(&oidp->oid_children); 674 oidp->oid_number = number; --- 6 unchanged lines hidden (view full) --- 681 oidp->oid_fmt = fmt; 682 if (descr != NULL) 683 oidp->oid_descr = strdup(descr, M_SYSCTLOID); 684 /* Update the context, if used */ 685 if (clist != NULL) 686 sysctl_ctx_entry_add(clist, oidp); 687 /* Register this oid */ 688 sysctl_register_oid(oidp); |
703 SYSCTL_XUNLOCK(); | 689 SYSCTL_WUNLOCK(); |
704 return (oidp); 705} 706 707/* 708 * Rename an existing oid. 709 */ 710void 711sysctl_rename_oid(struct sysctl_oid *oidp, const char *name) 712{ 713 char *newname; 714 char *oldname; 715 716 newname = strdup(name, M_SYSCTLOID); | 690 return (oidp); 691} 692 693/* 694 * Rename an existing oid. 695 */ 696void 697sysctl_rename_oid(struct sysctl_oid *oidp, const char *name) 698{ 699 char *newname; 700 char *oldname; 701 702 newname = strdup(name, M_SYSCTLOID); |
717 SYSCTL_XLOCK(); | 703 SYSCTL_WLOCK(); |
718 oldname = __DECONST(char *, oidp->oid_name); 719 oidp->oid_name = newname; | 704 oldname = __DECONST(char *, oidp->oid_name); 705 oidp->oid_name = newname; |
720 SYSCTL_XUNLOCK(); | 706 SYSCTL_WUNLOCK(); |
721 free(oldname, M_SYSCTLOID); 722} 723 724/* 725 * Reparent an existing oid. 726 */ 727int 728sysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent) 729{ 730 struct sysctl_oid *oidp; 731 | 707 free(oldname, M_SYSCTLOID); 708} 709 710/* 711 * Reparent an existing oid. 712 */ 713int 714sysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent) 715{ 716 struct sysctl_oid *oidp; 717 |
732 SYSCTL_XLOCK(); | 718 SYSCTL_WLOCK(); |
733 if (oid->oid_parent == parent) { | 719 if (oid->oid_parent == parent) { |
734 SYSCTL_XUNLOCK(); | 720 SYSCTL_WUNLOCK(); |
735 return (0); 736 } 737 oidp = sysctl_find_oidname(oid->oid_name, parent); 738 if (oidp != NULL) { | 721 return (0); 722 } 723 oidp = sysctl_find_oidname(oid->oid_name, parent); 724 if (oidp != NULL) { |
739 SYSCTL_XUNLOCK(); | 725 SYSCTL_WUNLOCK(); |
740 return (EEXIST); 741 } 742 sysctl_unregister_oid(oid); 743 oid->oid_parent = parent; 744 oid->oid_number = OID_AUTO; 745 sysctl_register_oid(oid); | 726 return (EEXIST); 727 } 728 sysctl_unregister_oid(oid); 729 oid->oid_parent = parent; 730 oid->oid_number = OID_AUTO; 731 sysctl_register_oid(oid); |
746 SYSCTL_XUNLOCK(); | 732 SYSCTL_WUNLOCK(); |
747 return (0); 748} 749 750/* 751 * Register the kernel's oids on startup. 752 */ 753SET_DECLARE(sysctl_set, struct sysctl_oid); 754 755static void 756sysctl_register_all(void *arg) 757{ 758 struct sysctl_oid **oidp; 759 760 sx_init(&sysctlmemlock, "sysctl mem"); 761 SYSCTL_INIT(); | 733 return (0); 734} 735 736/* 737 * Register the kernel's oids on startup. 738 */ 739SET_DECLARE(sysctl_set, struct sysctl_oid); 740 741static void 742sysctl_register_all(void *arg) 743{ 744 struct sysctl_oid **oidp; 745 746 sx_init(&sysctlmemlock, "sysctl mem"); 747 SYSCTL_INIT(); |
762 SYSCTL_XLOCK(); | 748 SYSCTL_WLOCK(); |
763 SET_FOREACH(oidp, sysctl_set) 764 sysctl_register_oid(*oidp); | 749 SET_FOREACH(oidp, sysctl_set) 750 sysctl_register_oid(*oidp); |
765 SYSCTL_XUNLOCK(); | 751 SYSCTL_WUNLOCK(); |
766} 767SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_FIRST, sysctl_register_all, 0); 768 769/* 770 * "Staff-functions" 771 * 772 * These functions implement a presently undocumented interface 773 * used by the sysctl program to walk the tree, and get the type --- 53 unchanged lines hidden (view full) --- 827 } 828 829 } 830} 831 832static int 833sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS) 834{ | 752} 753SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_FIRST, sysctl_register_all, 0); 754 755/* 756 * "Staff-functions" 757 * 758 * These functions implement a presently undocumented interface 759 * used by the sysctl program to walk the tree, and get the type --- 53 unchanged lines hidden (view full) --- 813 } 814 815 } 816} 817 818static int 819sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS) 820{ |
821 struct rm_priotracker tracker; |
|
835 int error; 836 837 error = priv_check(req->td, PRIV_SYSCTL_DEBUG); 838 if (error) 839 return (error); | 822 int error; 823 824 error = priv_check(req->td, PRIV_SYSCTL_DEBUG); 825 if (error) 826 return (error); |
840 SYSCTL_SLOCK(); | 827 SYSCTL_RLOCK(&tracker); |
841 sysctl_sysctl_debug_dump_node(&sysctl__children, 0); | 828 sysctl_sysctl_debug_dump_node(&sysctl__children, 0); |
842 SYSCTL_SUNLOCK(); | 829 SYSCTL_RUNLOCK(&tracker); |
843 return (ENOENT); 844} 845 846SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD|CTLFLAG_MPSAFE, 847 0, 0, sysctl_sysctl_debug, "-", ""); 848#endif 849 850static int 851sysctl_sysctl_name(SYSCTL_HANDLER_ARGS) 852{ 853 int *name = (int *) arg1; 854 u_int namelen = arg2; 855 int error = 0; 856 struct sysctl_oid *oid; 857 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2; | 830 return (ENOENT); 831} 832 833SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD|CTLFLAG_MPSAFE, 834 0, 0, sysctl_sysctl_debug, "-", ""); 835#endif 836 837static int 838sysctl_sysctl_name(SYSCTL_HANDLER_ARGS) 839{ 840 int *name = (int *) arg1; 841 u_int namelen = arg2; 842 int error = 0; 843 struct sysctl_oid *oid; 844 struct sysctl_oid_list *lsp = &sysctl__children, *lsp2; |
845 struct rm_priotracker tracker; |
|
858 char buf[10]; 859 | 846 char buf[10]; 847 |
860 SYSCTL_SLOCK(); | 848 SYSCTL_RLOCK(&tracker); |
861 while (namelen) { 862 if (!lsp) { 863 snprintf(buf,sizeof(buf),"%d",*name); 864 if (req->oldidx) 865 error = SYSCTL_OUT(req, ".", 1); 866 if (!error) 867 error = SYSCTL_OUT(req, buf, strlen(buf)); 868 if (error) --- 26 unchanged lines hidden (view full) --- 895 896 lsp2 = SYSCTL_CHILDREN(oid); 897 break; 898 } 899 lsp = lsp2; 900 } 901 error = SYSCTL_OUT(req, "", 1); 902 out: | 849 while (namelen) { 850 if (!lsp) { 851 snprintf(buf,sizeof(buf),"%d",*name); 852 if (req->oldidx) 853 error = SYSCTL_OUT(req, ".", 1); 854 if (!error) 855 error = SYSCTL_OUT(req, buf, strlen(buf)); 856 if (error) --- 26 unchanged lines hidden (view full) --- 883 884 lsp2 = SYSCTL_CHILDREN(oid); 885 break; 886 } 887 lsp = lsp2; 888 } 889 error = SYSCTL_OUT(req, "", 1); 890 out: |
903 SYSCTL_SUNLOCK(); | 891 SYSCTL_RUNLOCK(&tracker); |
904 return (error); 905} 906 907/* 908 * XXXRW/JA: Shouldn't return name data for nodes that we don't permit in 909 * capability mode. 910 */ 911static SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD | CTLFLAG_MPSAFE | CTLFLAG_CAPRD, --- 62 unchanged lines hidden (view full) --- 974static int 975sysctl_sysctl_next(SYSCTL_HANDLER_ARGS) 976{ 977 int *name = (int *) arg1; 978 u_int namelen = arg2; 979 int i, j, error; 980 struct sysctl_oid *oid; 981 struct sysctl_oid_list *lsp = &sysctl__children; | 892 return (error); 893} 894 895/* 896 * XXXRW/JA: Shouldn't return name data for nodes that we don't permit in 897 * capability mode. 898 */ 899static SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD | CTLFLAG_MPSAFE | CTLFLAG_CAPRD, --- 62 unchanged lines hidden (view full) --- 962static int 963sysctl_sysctl_next(SYSCTL_HANDLER_ARGS) 964{ 965 int *name = (int *) arg1; 966 u_int namelen = arg2; 967 int i, j, error; 968 struct sysctl_oid *oid; 969 struct sysctl_oid_list *lsp = &sysctl__children; |
970 struct rm_priotracker tracker; |
|
982 int newoid[CTL_MAXNAME]; 983 | 971 int newoid[CTL_MAXNAME]; 972 |
984 SYSCTL_SLOCK(); | 973 SYSCTL_RLOCK(&tracker); |
985 i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid); | 974 i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid); |
986 SYSCTL_SUNLOCK(); | 975 SYSCTL_RUNLOCK(&tracker); |
987 if (i) 988 return (ENOENT); 989 error = SYSCTL_OUT(req, newoid, j * sizeof (int)); 990 return (error); 991} 992 993/* 994 * XXXRW/JA: Shouldn't return next data for nodes that we don't permit in --- 42 unchanged lines hidden (view full) --- 1037} 1038 1039static int 1040sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS) 1041{ 1042 char *p; 1043 int error, oid[CTL_MAXNAME], len = 0; 1044 struct sysctl_oid *op = 0; | 976 if (i) 977 return (ENOENT); 978 error = SYSCTL_OUT(req, newoid, j * sizeof (int)); 979 return (error); 980} 981 982/* 983 * XXXRW/JA: Shouldn't return next data for nodes that we don't permit in --- 42 unchanged lines hidden (view full) --- 1026} 1027 1028static int 1029sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS) 1030{ 1031 char *p; 1032 int error, oid[CTL_MAXNAME], len = 0; 1033 struct sysctl_oid *op = 0; |
1034 struct rm_priotracker tracker; |
|
1045 1046 if (!req->newlen) 1047 return (ENOENT); 1048 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */ 1049 return (ENAMETOOLONG); 1050 1051 p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK); 1052 1053 error = SYSCTL_IN(req, p, req->newlen); 1054 if (error) { 1055 free(p, M_SYSCTL); 1056 return (error); 1057 } 1058 1059 p [req->newlen] = '\0'; 1060 | 1035 1036 if (!req->newlen) 1037 return (ENOENT); 1038 if (req->newlen >= MAXPATHLEN) /* XXX arbitrary, undocumented */ 1039 return (ENAMETOOLONG); 1040 1041 p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK); 1042 1043 error = SYSCTL_IN(req, p, req->newlen); 1044 if (error) { 1045 free(p, M_SYSCTL); 1046 return (error); 1047 } 1048 1049 p [req->newlen] = '\0'; 1050 |
1061 SYSCTL_SLOCK(); | 1051 SYSCTL_RLOCK(&tracker); |
1062 error = name2oid(p, oid, &len, &op); | 1052 error = name2oid(p, oid, &len, &op); |
1063 SYSCTL_SUNLOCK(); | 1053 SYSCTL_RUNLOCK(&tracker); |
1064 1065 free(p, M_SYSCTL); 1066 1067 if (error) 1068 return (error); 1069 1070 error = SYSCTL_OUT(req, oid, len * sizeof *oid); 1071 return (error); --- 6 unchanged lines hidden (view full) --- 1078SYSCTL_PROC(_sysctl, 3, name2oid, 1079 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE 1080 | CTLFLAG_CAPRW, 0, 0, sysctl_sysctl_name2oid, "I", ""); 1081 1082static int 1083sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS) 1084{ 1085 struct sysctl_oid *oid; | 1054 1055 free(p, M_SYSCTL); 1056 1057 if (error) 1058 return (error); 1059 1060 error = SYSCTL_OUT(req, oid, len * sizeof *oid); 1061 return (error); --- 6 unchanged lines hidden (view full) --- 1068SYSCTL_PROC(_sysctl, 3, name2oid, 1069 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE 1070 | CTLFLAG_CAPRW, 0, 0, sysctl_sysctl_name2oid, "I", ""); 1071 1072static int 1073sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS) 1074{ 1075 struct sysctl_oid *oid; |
1076 struct rm_priotracker tracker; |
|
1086 int error; 1087 | 1077 int error; 1078 |
1088 SYSCTL_SLOCK(); | 1079 SYSCTL_RLOCK(&tracker); |
1089 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req); 1090 if (error) 1091 goto out; 1092 1093 if (oid->oid_fmt == NULL) { 1094 error = ENOENT; 1095 goto out; 1096 } 1097 error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind)); 1098 if (error) 1099 goto out; 1100 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1); 1101 out: | 1080 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req); 1081 if (error) 1082 goto out; 1083 1084 if (oid->oid_fmt == NULL) { 1085 error = ENOENT; 1086 goto out; 1087 } 1088 error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind)); 1089 if (error) 1090 goto out; 1091 error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1); 1092 out: |
1102 SYSCTL_SUNLOCK(); | 1093 SYSCTL_RUNLOCK(&tracker); |
1103 return (error); 1104} 1105 1106 1107static SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLFLAG_CAPRD, 1108 sysctl_sysctl_oidfmt, ""); 1109 1110static int 1111sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS) 1112{ 1113 struct sysctl_oid *oid; | 1094 return (error); 1095} 1096 1097 1098static SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLFLAG_CAPRD, 1099 sysctl_sysctl_oidfmt, ""); 1100 1101static int 1102sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS) 1103{ 1104 struct sysctl_oid *oid; |
1105 struct rm_priotracker tracker; |
|
1114 int error; 1115 | 1106 int error; 1107 |
1116 SYSCTL_SLOCK(); | 1108 SYSCTL_RLOCK(&tracker); |
1117 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req); 1118 if (error) 1119 goto out; 1120 1121 if (oid->oid_descr == NULL) { 1122 error = ENOENT; 1123 goto out; 1124 } 1125 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1); 1126 out: | 1109 error = sysctl_find_oid(arg1, arg2, &oid, NULL, req); 1110 if (error) 1111 goto out; 1112 1113 if (oid->oid_descr == NULL) { 1114 error = ENOENT; 1115 goto out; 1116 } 1117 error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1); 1118 out: |
1127 SYSCTL_SUNLOCK(); | 1119 SYSCTL_RUNLOCK(&tracker); |
1128 return (error); 1129} 1130 1131static SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLFLAG_CAPRD, 1132 sysctl_sysctl_oiddescr, ""); 1133 1134/* 1135 * Default "handler" functions. --- 288 unchanged lines hidden (view full) --- 1424 req.newlen = newlen; 1425 req.newptr = new; 1426 } 1427 1428 req.oldfunc = sysctl_old_kernel; 1429 req.newfunc = sysctl_new_kernel; 1430 req.lock = REQ_UNWIRED; 1431 | 1120 return (error); 1121} 1122 1123static SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD|CTLFLAG_MPSAFE|CTLFLAG_CAPRD, 1124 sysctl_sysctl_oiddescr, ""); 1125 1126/* 1127 * Default "handler" functions. --- 288 unchanged lines hidden (view full) --- 1416 req.newlen = newlen; 1417 req.newptr = new; 1418 } 1419 1420 req.oldfunc = sysctl_old_kernel; 1421 req.newfunc = sysctl_new_kernel; 1422 req.lock = REQ_UNWIRED; 1423 |
1432 SYSCTL_SLOCK(); | |
1433 error = sysctl_root(0, name, namelen, &req); | 1424 error = sysctl_root(0, name, namelen, &req); |
1434 SYSCTL_SUNLOCK(); | |
1435 1436 if (req.lock == REQ_WIRED && req.validlen > 0) 1437 vsunlock(req.oldptr, req.validlen); 1438 1439 if (error && error != ENOMEM) 1440 return (error); 1441 1442 if (retval) { --- 160 unchanged lines hidden (view full) --- 1603 * Traverse our tree, and find the right node, execute whatever it points 1604 * to, and return the resulting error code. 1605 */ 1606 1607static int 1608sysctl_root(SYSCTL_HANDLER_ARGS) 1609{ 1610 struct sysctl_oid *oid; | 1425 1426 if (req.lock == REQ_WIRED && req.validlen > 0) 1427 vsunlock(req.oldptr, req.validlen); 1428 1429 if (error && error != ENOMEM) 1430 return (error); 1431 1432 if (retval) { --- 160 unchanged lines hidden (view full) --- 1593 * Traverse our tree, and find the right node, execute whatever it points 1594 * to, and return the resulting error code. 1595 */ 1596 1597static int 1598sysctl_root(SYSCTL_HANDLER_ARGS) 1599{ 1600 struct sysctl_oid *oid; |
1601 struct rm_priotracker tracker; |
|
1611 int error, indx, lvl; 1612 | 1602 int error, indx, lvl; 1603 |
1613 SYSCTL_ASSERT_SLOCKED(); | 1604 SYSCTL_RLOCK(&tracker); |
1614 1615 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req); 1616 if (error) | 1605 1606 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req); 1607 if (error) |
1617 return (error); | 1608 goto out; |
1618 1619 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 1620 /* 1621 * You can't call a sysctl when it's a node, but has 1622 * no handler. Inform the user that it's a node. 1623 * The indx may or may not be the same as namelen. 1624 */ | 1609 1610 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 1611 /* 1612 * You can't call a sysctl when it's a node, but has 1613 * no handler. Inform the user that it's a node. 1614 * The indx may or may not be the same as namelen. 1615 */ |
1625 if (oid->oid_handler == NULL) 1626 return (EISDIR); | 1616 if (oid->oid_handler == NULL) { 1617 error = EISDIR; 1618 goto out; 1619 } |
1627 } 1628 1629 /* Is this sysctl writable? */ | 1620 } 1621 1622 /* Is this sysctl writable? */ |
1630 if (req->newptr && !(oid->oid_kind & CTLFLAG_WR)) 1631 return (EPERM); | 1623 if (req->newptr && !(oid->oid_kind & CTLFLAG_WR)) { 1624 error = EPERM; 1625 goto out; 1626 } |
1632 1633 KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL")); 1634 1635#ifdef CAPABILITY_MODE 1636 /* 1637 * If the process is in capability mode, then don't permit reading or 1638 * writing unless specifically granted for the node. 1639 */ 1640 if (IN_CAPABILITY_MODE(req->td)) { | 1627 1628 KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL")); 1629 1630#ifdef CAPABILITY_MODE 1631 /* 1632 * If the process is in capability mode, then don't permit reading or 1633 * writing unless specifically granted for the node. 1634 */ 1635 if (IN_CAPABILITY_MODE(req->td)) { |
1641 if (req->oldptr && !(oid->oid_kind & CTLFLAG_CAPRD)) 1642 return (EPERM); 1643 if (req->newptr && !(oid->oid_kind & CTLFLAG_CAPWR)) 1644 return (EPERM); | 1636 if ((req->oldptr && !(oid->oid_kind & CTLFLAG_CAPRD)) || 1637 (req->newptr && !(oid->oid_kind & CTLFLAG_CAPWR))) { 1638 error = EPERM; 1639 goto out; 1640 } |
1645 } 1646#endif 1647 1648 /* Is this sysctl sensitive to securelevels? */ 1649 if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) { 1650 lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE; 1651 error = securelevel_gt(req->td->td_ucred, lvl); 1652 if (error) | 1641 } 1642#endif 1643 1644 /* Is this sysctl sensitive to securelevels? */ 1645 if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) { 1646 lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE; 1647 error = securelevel_gt(req->td->td_ucred, lvl); 1648 if (error) |
1653 return (error); | 1649 goto out; |
1654 } 1655 1656 /* Is this sysctl writable by only privileged users? */ 1657 if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) { 1658 int priv; 1659 1660 if (oid->oid_kind & CTLFLAG_PRISON) 1661 priv = PRIV_SYSCTL_WRITEJAIL; 1662#ifdef VIMAGE 1663 else if ((oid->oid_kind & CTLFLAG_VNET) && 1664 prison_owns_vnet(req->td->td_ucred)) 1665 priv = PRIV_SYSCTL_WRITEJAIL; 1666#endif 1667 else 1668 priv = PRIV_SYSCTL_WRITE; 1669 error = priv_check(req->td, priv); 1670 if (error) | 1650 } 1651 1652 /* Is this sysctl writable by only privileged users? */ 1653 if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) { 1654 int priv; 1655 1656 if (oid->oid_kind & CTLFLAG_PRISON) 1657 priv = PRIV_SYSCTL_WRITEJAIL; 1658#ifdef VIMAGE 1659 else if ((oid->oid_kind & CTLFLAG_VNET) && 1660 prison_owns_vnet(req->td->td_ucred)) 1661 priv = PRIV_SYSCTL_WRITEJAIL; 1662#endif 1663 else 1664 priv = PRIV_SYSCTL_WRITE; 1665 error = priv_check(req->td, priv); 1666 if (error) |
1671 return (error); | 1667 goto out; |
1672 } 1673 | 1668 } 1669 |
1674 if (!oid->oid_handler) 1675 return (EINVAL); | 1670 if (!oid->oid_handler) { 1671 error = EINVAL; 1672 goto out; 1673 } |
1676 1677 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 1678 arg1 = (int *)arg1 + indx; 1679 arg2 -= indx; 1680 } else { 1681 arg1 = oid->oid_arg1; 1682 arg2 = oid->oid_arg2; 1683 } 1684#ifdef MAC 1685 error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2, 1686 req); 1687 if (error != 0) | 1674 1675 if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) { 1676 arg1 = (int *)arg1 + indx; 1677 arg2 -= indx; 1678 } else { 1679 arg1 = oid->oid_arg1; 1680 arg2 = oid->oid_arg2; 1681 } 1682#ifdef MAC 1683 error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2, 1684 req); 1685 if (error != 0) |
1688 return (error); | 1686 goto out; |
1689#endif 1690#ifdef VIMAGE 1691 if ((oid->oid_kind & CTLFLAG_VNET) && arg1 != NULL) 1692 arg1 = (void *)(curvnet->vnet_data_base + (uintptr_t)arg1); 1693#endif | 1687#endif 1688#ifdef VIMAGE 1689 if ((oid->oid_kind & CTLFLAG_VNET) && arg1 != NULL) 1690 arg1 = (void *)(curvnet->vnet_data_base + (uintptr_t)arg1); 1691#endif |
1694 error = sysctl_root_handler_locked(oid, arg1, arg2, req); | 1692 error = sysctl_root_handler_locked(oid, arg1, arg2, req, &tracker); |
1695 1696 KFAIL_POINT_ERROR(_debug_fail_point, sysctl_running, error); 1697 | 1693 1694 KFAIL_POINT_ERROR(_debug_fail_point, sysctl_running, error); 1695 |
1696out: 1697 SYSCTL_RUNLOCK(&tracker); |
|
1698 return (error); 1699} 1700 1701#ifndef _SYS_SYSPROTO_H_ 1702struct sysctl_args { 1703 int *name; 1704 u_int namelen; 1705 void *old; --- 83 unchanged lines hidden (view full) --- 1789 sx_xlock(&sysctlmemlock); 1790 } else 1791 memlocked = 0; 1792 CURVNET_SET(TD_TO_VNET(td)); 1793 1794 for (;;) { 1795 req.oldidx = 0; 1796 req.newidx = 0; | 1698 return (error); 1699} 1700 1701#ifndef _SYS_SYSPROTO_H_ 1702struct sysctl_args { 1703 int *name; 1704 u_int namelen; 1705 void *old; --- 83 unchanged lines hidden (view full) --- 1789 sx_xlock(&sysctlmemlock); 1790 } else 1791 memlocked = 0; 1792 CURVNET_SET(TD_TO_VNET(td)); 1793 1794 for (;;) { 1795 req.oldidx = 0; 1796 req.newidx = 0; |
1797 SYSCTL_SLOCK(); | |
1798 error = sysctl_root(0, name, namelen, &req); | 1797 error = sysctl_root(0, name, namelen, &req); |
1799 SYSCTL_SUNLOCK(); | |
1800 if (error != EAGAIN) 1801 break; 1802 kern_yield(PRI_USER); 1803 } 1804 1805 CURVNET_RESTORE(); 1806 1807 if (req.lock == REQ_WIRED && req.validlen > 0) --- 43 unchanged lines hidden --- | 1798 if (error != EAGAIN) 1799 break; 1800 kern_yield(PRI_USER); 1801 } 1802 1803 CURVNET_RESTORE(); 1804 1805 if (req.lock == REQ_WIRED && req.validlen > 0) --- 43 unchanged lines hidden --- |