Deleted Added
sdiff udiff text old ( 286094 ) new ( 287835 )
full compact
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 $");
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/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
80 * be held, so the sysctl_xlock() and sysctl_xunlock() 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 */
89static struct sx sysctllock;
90static struct sx sysctlmemlock;
91
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")
101#define SYSCTL_SLEEP(ch, wmesg, timo) \
102 sx_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
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
157sysctl_xlock(void)
158{
159
160 SYSCTL_XLOCK();
161}
162
163void
164sysctl_xunlock(void)
165{
166
167 SYSCTL_XUNLOCK();
168}
169
170static int
171sysctl_root_handler_locked(struct sysctl_oid *oid, void *arg1, intptr_t arg2,
172 struct sysctl_req *req)
173{
174 int error;
175 bool xlocked;
176
177 if (oid->oid_kind & CTLFLAG_DYN)
178 atomic_add_int(&oid->oid_running, 1);
179 xlocked = sysctl_unlock();
180
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
187 sysctl_lock(xlocked);
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,
286 oidp->oid_arg2, &req);
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 */
306 SYSCTL_ASSERT_XLOCKED();
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
400 SYSCTL_ASSERT_XLOCKED();
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 */
456 SYSCTL_XLOCK();
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) {
476 SYSCTL_XUNLOCK();
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 }
490 SYSCTL_XUNLOCK();
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
500 SYSCTL_ASSERT_XLOCKED();
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
515 SYSCTL_ASSERT_XLOCKED();
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);
537 SYSCTL_XLOCK();
538 e = sysctl_ctx_entry_find(clist, oidp);
539 if (e != NULL) {
540 TAILQ_REMOVE(clist, e, link);
541 SYSCTL_XUNLOCK();
542 free(e, M_SYSCTLOID);
543 return (0);
544 } else {
545 SYSCTL_XUNLOCK();
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
561 SYSCTL_XLOCK();
562 error = sysctl_remove_oid_locked(oidp, del, recurse);
563 SYSCTL_XUNLOCK();
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;
575 SYSCTL_XLOCK();
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 }
582 SYSCTL_XUNLOCK();
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
594 SYSCTL_ASSERT_XLOCKED();
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 */
669 SYSCTL_XLOCK();
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);
677 SYSCTL_XUNLOCK();
678 return (oidp);
679 } else {
680 SYSCTL_XUNLOCK();
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);
703 SYSCTL_XUNLOCK();
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);
717 SYSCTL_XLOCK();
718 oldname = __DECONST(char *, oidp->oid_name);
719 oidp->oid_name = newname;
720 SYSCTL_XUNLOCK();
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
732 SYSCTL_XLOCK();
733 if (oid->oid_parent == parent) {
734 SYSCTL_XUNLOCK();
735 return (0);
736 }
737 oidp = sysctl_find_oidname(oid->oid_name, parent);
738 if (oidp != NULL) {
739 SYSCTL_XUNLOCK();
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);
746 SYSCTL_XUNLOCK();
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();
762 SYSCTL_XLOCK();
763 SET_FOREACH(oidp, sysctl_set)
764 sysctl_register_oid(*oidp);
765 SYSCTL_XUNLOCK();
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{
835 int error;
836
837 error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
838 if (error)
839 return (error);
840 SYSCTL_SLOCK();
841 sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
842 SYSCTL_SUNLOCK();
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;
858 char buf[10];
859
860 SYSCTL_SLOCK();
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:
903 SYSCTL_SUNLOCK();
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;
982 int newoid[CTL_MAXNAME];
983
984 SYSCTL_SLOCK();
985 i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
986 SYSCTL_SUNLOCK();
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;
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
1061 SYSCTL_SLOCK();
1062 error = name2oid(p, oid, &len, &op);
1063 SYSCTL_SUNLOCK();
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;
1086 int error;
1087
1088 SYSCTL_SLOCK();
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:
1102 SYSCTL_SUNLOCK();
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;
1114 int error;
1115
1116 SYSCTL_SLOCK();
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:
1127 SYSCTL_SUNLOCK();
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
1432 SYSCTL_SLOCK();
1433 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;
1611 int error, indx, lvl;
1612
1613 SYSCTL_ASSERT_SLOCKED();
1614
1615 error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1616 if (error)
1617 return (error);
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 */
1625 if (oid->oid_handler == NULL)
1626 return (EISDIR);
1627 }
1628
1629 /* Is this sysctl writable? */
1630 if (req->newptr && !(oid->oid_kind & CTLFLAG_WR))
1631 return (EPERM);
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)) {
1641 if (req->oldptr && !(oid->oid_kind & CTLFLAG_CAPRD))
1642 return (EPERM);
1643 if (req->newptr && !(oid->oid_kind & CTLFLAG_CAPWR))
1644 return (EPERM);
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)
1653 return (error);
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)
1671 return (error);
1672 }
1673
1674 if (!oid->oid_handler)
1675 return (EINVAL);
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)
1688 return (error);
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
1694 error = sysctl_root_handler_locked(oid, arg1, arg2, req);
1695
1696 KFAIL_POINT_ERROR(_debug_fail_point, sysctl_running, error);
1697
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);
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 ---