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