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