kern_sysctl.c revision 93625
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 93625 2002-04-02 05:50:07Z rwatson $
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(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(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
993int
994sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
995    int *nindx, struct sysctl_req *req)
996{
997	struct sysctl_oid *oid;
998	int indx;
999
1000	oid = SLIST_FIRST(&sysctl__children);
1001	indx = 0;
1002	while (oid && indx < CTL_MAXNAME) {
1003		if (oid->oid_number == name[indx]) {
1004			indx++;
1005			if (oid->oid_kind & CTLFLAG_NOLOCK)
1006				req->lock = 0;
1007			if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1008				if (oid->oid_handler != NULL ||
1009				    indx == namelen) {
1010					*noid = oid;
1011					if (nindx != NULL)
1012						*nindx = indx;
1013					return (0);
1014				}
1015				oid = SLIST_FIRST(
1016				    (struct sysctl_oid_list *)oid->oid_arg1);
1017			} else if (indx == namelen) {
1018				*noid = oid;
1019				if (nindx != NULL)
1020					*nindx = indx;
1021				return (0);
1022			} else {
1023				return (ENOTDIR);
1024			}
1025		} else {
1026			oid = SLIST_NEXT(oid, oid_link);
1027		}
1028	}
1029	return (ENOENT);
1030}
1031
1032/*
1033 * Traverse our tree, and find the right node, execute whatever it points
1034 * to, and return the resulting error code.
1035 */
1036
1037int
1038sysctl_root(SYSCTL_HANDLER_ARGS)
1039{
1040	struct sysctl_oid *oid;
1041	int error, indx;
1042
1043	error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1044	if (error)
1045		return (error);
1046
1047	if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1048		/*
1049		 * You can't call a sysctl when it's a node, but has
1050		 * no handler.  Inform the user that it's a node.
1051		 * The indx may or may not be the same as namelen.
1052		 */
1053		if (oid->oid_handler == NULL)
1054			return (EISDIR);
1055	}
1056
1057	/* Is this sysctl writable? */
1058	if (req->newptr && !(oid->oid_kind & CTLFLAG_WR))
1059		return (EPERM);
1060
1061	KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
1062
1063	/* Is this sysctl sensitive to securelevels? */
1064	if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
1065		error = securelevel_gt(req->td->td_ucred, 0);
1066		if (error)
1067			return (error);
1068	}
1069
1070	/* Is this sysctl writable by only privileged users? */
1071	if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
1072		int flags;
1073
1074		if (oid->oid_kind & CTLFLAG_PRISON)
1075			flags = PRISON_ROOT;
1076		else
1077			flags = 0;
1078		error = suser_cred(req->td->td_ucred, flags);
1079		if (error)
1080			return (error);
1081	}
1082
1083	if (!oid->oid_handler)
1084		return EINVAL;
1085
1086	if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE)
1087		error = oid->oid_handler(oid, (int *)arg1 + indx, arg2 - indx,
1088		    req);
1089	else
1090		error = oid->oid_handler(oid, oid->oid_arg1, oid->oid_arg2,
1091		    req);
1092	return (error);
1093}
1094
1095#ifndef _SYS_SYSPROTO_H_
1096struct sysctl_args {
1097	int	*name;
1098	u_int	namelen;
1099	void	*old;
1100	size_t	*oldlenp;
1101	void	*new;
1102	size_t	newlen;
1103};
1104#endif
1105
1106/*
1107 * MPSAFE
1108 */
1109int
1110__sysctl(struct thread *td, struct sysctl_args *uap)
1111{
1112	int error, name[CTL_MAXNAME];
1113	size_t j;
1114
1115	if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1116		return (EINVAL);
1117
1118 	error = copyin(uap->name, &name, uap->namelen * sizeof(int));
1119 	if (error)
1120		return (error);
1121
1122	mtx_lock(&Giant);
1123
1124	error = userland_sysctl(td, name, uap->namelen,
1125		uap->old, uap->oldlenp, 0,
1126		uap->new, uap->newlen, &j);
1127	if (error && error != ENOMEM)
1128		goto done2;
1129	if (uap->oldlenp) {
1130		int i = copyout(&j, uap->oldlenp, sizeof(j));
1131		if (i)
1132			error = i;
1133	}
1134done2:
1135	mtx_unlock(&Giant);
1136	return (error);
1137}
1138
1139/*
1140 * This is used from various compatibility syscalls too.  That's why name
1141 * must be in kernel space.
1142 */
1143int
1144userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1145    size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval)
1146{
1147	int error = 0;
1148	struct sysctl_req req, req2;
1149
1150	bzero(&req, sizeof req);
1151
1152	req.td = td;
1153
1154	if (oldlenp) {
1155		if (inkernel) {
1156			req.oldlen = *oldlenp;
1157		} else {
1158			error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
1159			if (error)
1160				return (error);
1161		}
1162	}
1163
1164	if (old) {
1165		if (!useracc(old, req.oldlen, VM_PROT_WRITE))
1166			return (EFAULT);
1167		req.oldptr= old;
1168	}
1169
1170	if (new != NULL) {
1171		if (!useracc(new, req.newlen, VM_PROT_READ))
1172			return (EFAULT);
1173		req.newlen = newlen;
1174		req.newptr = new;
1175	}
1176
1177	req.oldfunc = sysctl_old_user;
1178	req.newfunc = sysctl_new_user;
1179	req.lock = 1;
1180
1181	SYSCTL_LOCK();
1182
1183	do {
1184	    req2 = req;
1185	    error = sysctl_root(0, name, namelen, &req2);
1186	} while (error == EAGAIN);
1187
1188	req = req2;
1189	if (req.lock == 2)
1190		vsunlock(req.oldptr, req.oldlen);
1191
1192	SYSCTL_UNLOCK();
1193
1194	if (error && error != ENOMEM)
1195		return (error);
1196
1197	if (retval) {
1198		if (req.oldptr && req.oldidx > req.oldlen)
1199			*retval = req.oldlen;
1200		else
1201			*retval = req.oldidx;
1202	}
1203	return (error);
1204}
1205
1206#ifdef COMPAT_43
1207#include <sys/socket.h>
1208#include <vm/vm_param.h>
1209
1210#define	KINFO_PROC		(0<<8)
1211#define	KINFO_RT		(1<<8)
1212#define	KINFO_VNODE		(2<<8)
1213#define	KINFO_FILE		(3<<8)
1214#define	KINFO_METER		(4<<8)
1215#define	KINFO_LOADAVG		(5<<8)
1216#define	KINFO_CLOCKRATE		(6<<8)
1217
1218/* Non-standard BSDI extension - only present on their 4.3 net-2 releases */
1219#define	KINFO_BSDI_SYSINFO	(101<<8)
1220
1221/*
1222 * XXX this is bloat, but I hope it's better here than on the potentially
1223 * limited kernel stack...  -Peter
1224 */
1225
1226static struct {
1227	int	bsdi_machine;		/* "i386" on BSD/386 */
1228/*      ^^^ this is an offset to the string, relative to the struct start */
1229	char	*pad0;
1230	long	pad1;
1231	long	pad2;
1232	long	pad3;
1233	u_long	pad4;
1234	u_long	pad5;
1235	u_long	pad6;
1236
1237	int	bsdi_ostype;		/* "BSD/386" on BSD/386 */
1238	int	bsdi_osrelease;		/* "1.1" on BSD/386 */
1239	long	pad7;
1240	long	pad8;
1241	char	*pad9;
1242
1243	long	pad10;
1244	long	pad11;
1245	int	pad12;
1246	long	pad13;
1247	quad_t	pad14;
1248	long	pad15;
1249
1250	struct	timeval pad16;
1251	/* we dont set this, because BSDI's uname used gethostname() instead */
1252	int	bsdi_hostname;		/* hostname on BSD/386 */
1253
1254	/* the actual string data is appended here */
1255
1256} bsdi_si;
1257/*
1258 * this data is appended to the end of the bsdi_si structure during copyout.
1259 * The "char *" offsets are relative to the base of the bsdi_si struct.
1260 * This contains "FreeBSD\02.0-BUILT-nnnnnn\0i386\0", and these strings
1261 * should not exceed the length of the buffer here... (or else!! :-)
1262 */
1263static char bsdi_strings[80];	/* It had better be less than this! */
1264
1265#ifndef _SYS_SYSPROTO_H_
1266struct getkerninfo_args {
1267	int	op;
1268	char	*where;
1269	size_t	*size;
1270	int	arg;
1271};
1272#endif
1273
1274/*
1275 * MPSAFE
1276 */
1277int
1278ogetkerninfo(struct thread *td, struct getkerninfo_args *uap)
1279{
1280	int error, name[6];
1281	size_t size;
1282	u_int needed = 0;
1283
1284	mtx_lock(&Giant);
1285
1286	switch (uap->op & 0xff00) {
1287
1288	case KINFO_RT:
1289		name[0] = CTL_NET;
1290		name[1] = PF_ROUTE;
1291		name[2] = 0;
1292		name[3] = (uap->op & 0xff0000) >> 16;
1293		name[4] = uap->op & 0xff;
1294		name[5] = uap->arg;
1295		error = userland_sysctl(td, name, 6, uap->where, uap->size,
1296			0, 0, 0, &size);
1297		break;
1298
1299	case KINFO_VNODE:
1300		name[0] = CTL_KERN;
1301		name[1] = KERN_VNODE;
1302		error = userland_sysctl(td, name, 2, uap->where, uap->size,
1303			0, 0, 0, &size);
1304		break;
1305
1306	case KINFO_PROC:
1307		name[0] = CTL_KERN;
1308		name[1] = KERN_PROC;
1309		name[2] = uap->op & 0xff;
1310		name[3] = uap->arg;
1311		error = userland_sysctl(td, name, 4, uap->where, uap->size,
1312			0, 0, 0, &size);
1313		break;
1314
1315	case KINFO_FILE:
1316		name[0] = CTL_KERN;
1317		name[1] = KERN_FILE;
1318		error = userland_sysctl(td, name, 2, uap->where, uap->size,
1319			0, 0, 0, &size);
1320		break;
1321
1322	case KINFO_METER:
1323		name[0] = CTL_VM;
1324		name[1] = VM_METER;
1325		error = userland_sysctl(td, name, 2, uap->where, uap->size,
1326			0, 0, 0, &size);
1327		break;
1328
1329	case KINFO_LOADAVG:
1330		name[0] = CTL_VM;
1331		name[1] = VM_LOADAVG;
1332		error = userland_sysctl(td, name, 2, uap->where, uap->size,
1333			0, 0, 0, &size);
1334		break;
1335
1336	case KINFO_CLOCKRATE:
1337		name[0] = CTL_KERN;
1338		name[1] = KERN_CLOCKRATE;
1339		error = userland_sysctl(td, name, 2, uap->where, uap->size,
1340			0, 0, 0, &size);
1341		break;
1342
1343	case KINFO_BSDI_SYSINFO: {
1344		/*
1345		 * this is pretty crude, but it's just enough for uname()
1346		 * from BSDI's 1.x libc to work.
1347		 *
1348		 * *size gives the size of the buffer before the call, and
1349		 * the amount of data copied after a successful call.
1350		 * If successful, the return value is the amount of data
1351		 * available, which can be larger than *size.
1352		 *
1353		 * BSDI's 2.x product apparently fails with ENOMEM if *size
1354		 * is too small.
1355		 */
1356
1357		u_int left;
1358		char *s;
1359
1360		bzero((char *)&bsdi_si, sizeof(bsdi_si));
1361		bzero(bsdi_strings, sizeof(bsdi_strings));
1362
1363		s = bsdi_strings;
1364
1365		bsdi_si.bsdi_ostype = (s - bsdi_strings) + sizeof(bsdi_si);
1366		strcpy(s, ostype);
1367		s += strlen(s) + 1;
1368
1369		bsdi_si.bsdi_osrelease = (s - bsdi_strings) + sizeof(bsdi_si);
1370		strcpy(s, osrelease);
1371		s += strlen(s) + 1;
1372
1373		bsdi_si.bsdi_machine = (s - bsdi_strings) + sizeof(bsdi_si);
1374		strcpy(s, machine);
1375		s += strlen(s) + 1;
1376
1377		needed = sizeof(bsdi_si) + (s - bsdi_strings);
1378
1379		if ((uap->where == NULL) || (uap->size == NULL)) {
1380			/* process is asking how much buffer to supply.. */
1381			size = needed;
1382			error = 0;
1383			break;
1384		}
1385
1386		if ((error = copyin(uap->size, &size, sizeof(size))) != 0)
1387			break;
1388
1389		/* if too much buffer supplied, trim it down */
1390		if (size > needed)
1391			size = needed;
1392
1393		/* how much of the buffer is remaining */
1394		left = size;
1395
1396		if ((error = copyout((char *)&bsdi_si, uap->where, left)) != 0)
1397			break;
1398
1399		/* is there any point in continuing? */
1400		if (left > sizeof(bsdi_si)) {
1401			left -= sizeof(bsdi_si);
1402			error = copyout(&bsdi_strings,
1403					uap->where + sizeof(bsdi_si), left);
1404		}
1405		break;
1406	}
1407
1408	default:
1409		error = EOPNOTSUPP;
1410		break;
1411	}
1412	if (error == 0) {
1413		td->td_retval[0] = needed ? needed : size;
1414		if (uap->size) {
1415			error = copyout((caddr_t)&size, (caddr_t)uap->size,
1416				    sizeof(size));
1417		}
1418	}
1419	mtx_unlock(&Giant);
1420	return (error);
1421}
1422#endif /* COMPAT_43 */
1423