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