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