kern_sysctl.c revision 80339
11541Srgrimes/*-
21541Srgrimes * Copyright (c) 1982, 1986, 1989, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * This code is derived from software contributed to Berkeley by
61541Srgrimes * Mike Karels at Berkeley Software Design, Inc.
71541Srgrimes *
812623Sphk * Quite extensively rewritten by Poul-Henning Kamp of the FreeBSD
912623Sphk * project, to make these variables more userfriendly.
1012623Sphk *
111541Srgrimes * Redistribution and use in source and binary forms, with or without
121541Srgrimes * modification, are permitted provided that the following conditions
131541Srgrimes * are met:
141541Srgrimes * 1. Redistributions of source code must retain the above copyright
151541Srgrimes *    notice, this list of conditions and the following disclaimer.
161541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
171541Srgrimes *    notice, this list of conditions and the following disclaimer in the
181541Srgrimes *    documentation and/or other materials provided with the distribution.
191541Srgrimes * 3. All advertising materials mentioning features or use of this software
201541Srgrimes *    must display the following acknowledgement:
211541Srgrimes *	This product includes software developed by the University of
221541Srgrimes *	California, Berkeley and its contributors.
231541Srgrimes * 4. Neither the name of the University nor the names of its contributors
241541Srgrimes *    may be used to endorse or promote products derived from this software
251541Srgrimes *    without specific prior written permission.
261541Srgrimes *
271541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
281541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
291541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
301541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
311541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
321541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
331541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
341541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
351541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
361541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
371541Srgrimes * SUCH DAMAGE.
381541Srgrimes *
391541Srgrimes *	@(#)kern_sysctl.c	8.4 (Berkeley) 4/14/94
4050477Speter * $FreeBSD: head/sys/kern/kern_sysctl.c 80339 2001-07-25 17:21:18Z roam $
411541Srgrimes */
421541Srgrimes
4331778Seivind#include "opt_compat.h"
4431778Seivind
451541Srgrimes#include <sys/param.h>
4648274Speter#include <sys/systm.h>
4748274Speter#include <sys/kernel.h>
481541Srgrimes#include <sys/sysctl.h>
4912623Sphk#include <sys/malloc.h>
5012662Sdg#include <sys/proc.h>
5115103Sphk#include <sys/sysproto.h>
5212645Sbde#include <vm/vm.h>
5312662Sdg#include <vm/vm_extern.h>
5412645Sbde
5530354Sphkstatic MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
5663212Sabialstatic MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
5730309Sphk
5812429Sphk/*
5912429Sphk * Locking and stats
6012429Sphk */
6112429Sphkstatic struct sysctl_lock {
6212429Sphk	int	sl_lock;
6312429Sphk	int	sl_want;
6412429Sphk	int	sl_locked;
6512429Sphk} memlock;
6612429Sphk
6762573Sphkstatic int sysctl_root(SYSCTL_HANDLER_ARGS);
6812429Sphk
6944078Sdfrstruct sysctl_oid_list sysctl__children; /* root list */
7012152Sphk
7163212Sabialstatic struct sysctl_oid *
7263212Sabialsysctl_find_oidname(const char *name, struct sysctl_oid_list *list)
7363212Sabial{
7463212Sabial	struct sysctl_oid *oidp;
7563212Sabial
7663212Sabial	SLIST_FOREACH(oidp, list, oid_link) {
7763212Sabial		if (strcmp(oidp->oid_name, name) == 0) {
7863212Sabial			return (oidp);
7963212Sabial		}
8063212Sabial	}
8163212Sabial	return (NULL);
8263212Sabial}
8363212Sabial
8412623Sphk/*
8512623Sphk * Initialization of the MIB tree.
8612623Sphk *
8744078Sdfr * Order by number in each list.
8812623Sphk */
8912429Sphk
9080338Sroamvoid
9180338Sroamsysctl_register_oid(struct sysctl_oid *oidp)
9212152Sphk{
9344078Sdfr	struct sysctl_oid_list *parent = oidp->oid_parent;
9444078Sdfr	struct sysctl_oid *p;
9544078Sdfr	struct sysctl_oid *q;
9612197Sbde
9744078Sdfr	/*
9863212Sabial	 * First check if another oid with the same name already
9963212Sabial	 * exists in the parent's list.
10063212Sabial	 */
10163212Sabial	p = sysctl_find_oidname(oidp->oid_name, parent);
10263212Sabial	if (p != NULL) {
10363212Sabial		if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
10463212Sabial			p->oid_refcnt++;
10563212Sabial			return;
10663212Sabial		} else {
10763212Sabial			printf("can't re-use a leaf (%s)!\n", p->oid_name);
10863212Sabial			return;
10963212Sabial		}
11063212Sabial	}
11163212Sabial	/*
11244078Sdfr	 * If this oid has a number OID_AUTO, give it a number which
11380339Sroam	 * is greater than any current oid.
11480339Sroam	 * NOTE: DO NOT change the starting value here, change it in
11580339Sroam	 * <sys/sysctl.h>, and make sure it is at least 256 to
11680339Sroam	 * accomodate e.g. net.inet.raw as a static sysctl node.
11744078Sdfr	 */
11844078Sdfr	if (oidp->oid_number == OID_AUTO) {
11980339Sroam		static int newoid = CTL_AUTO_START;
12071510Smckusick
12171510Smckusick		oidp->oid_number = newoid++;
12271510Smckusick		if (newoid == 0x7fffffff)
12371510Smckusick			panic("out of oids");
12480339Sroam	} else if (oidp->oid_number >= CTL_AUTO_START) {
12580339Sroam		panic("static sysctl oid too high: %d", oidp->oid_number);
12644078Sdfr	}
12744078Sdfr
12844078Sdfr	/*
12944078Sdfr	 * Insert the oid into the parent's list in order.
13044078Sdfr	 */
13144078Sdfr	q = NULL;
13244078Sdfr	SLIST_FOREACH(p, parent, oid_link) {
13344078Sdfr		if (oidp->oid_number < p->oid_number)
13444078Sdfr			break;
13544078Sdfr		q = p;
13644078Sdfr	}
13744078Sdfr	if (q)
13844078Sdfr		SLIST_INSERT_AFTER(q, oidp, oid_link);
13944078Sdfr	else
14044078Sdfr		SLIST_INSERT_HEAD(parent, oidp, oid_link);
14112152Sphk}
14212131Sphk
14380338Sroamvoid
14480338Sroamsysctl_unregister_oid(struct sysctl_oid *oidp)
14512152Sphk{
14660938Sjake	SLIST_REMOVE(oidp->oid_parent, oidp, sysctl_oid, oid_link);
14744078Sdfr}
14812152Sphk
14963212Sabial/* Initialize a new context to keep track of dynamically added sysctls. */
15063212Sabialint
15163212Sabialsysctl_ctx_init(struct sysctl_ctx_list *c)
15263212Sabial{
15363212Sabial
15463212Sabial	if (c == NULL) {
15563212Sabial		return (EINVAL);
15663212Sabial	}
15763212Sabial	TAILQ_INIT(c);
15863212Sabial	return (0);
15963212Sabial}
16063212Sabial
16163212Sabial/* Free the context, and destroy all dynamic oids registered in this context */
16263212Sabialint
16363212Sabialsysctl_ctx_free(struct sysctl_ctx_list *clist)
16463212Sabial{
16563212Sabial	struct sysctl_ctx_entry *e, *e1;
16663212Sabial	int error;
16763212Sabial
16863212Sabial	error = 0;
16963212Sabial	/*
17063212Sabial	 * First perform a "dry run" to check if it's ok to remove oids.
17163212Sabial	 * XXX FIXME
17263212Sabial	 * XXX This algorithm is a hack. But I don't know any
17363212Sabial	 * XXX better solution for now...
17463212Sabial	 */
17563212Sabial	TAILQ_FOREACH(e, clist, link) {
17663212Sabial		error = sysctl_remove_oid(e->entry, 0, 0);
17763212Sabial		if (error)
17863212Sabial			break;
17963212Sabial	}
18063212Sabial	/*
18163212Sabial	 * Restore deregistered entries, either from the end,
18263212Sabial	 * or from the place where error occured.
18363212Sabial	 * e contains the entry that was not unregistered
18463212Sabial	 */
18563212Sabial	if (error)
18663212Sabial		e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
18763212Sabial	else
18863212Sabial		e1 = TAILQ_LAST(clist, sysctl_ctx_list);
18963212Sabial	while (e1 != NULL) {
19063212Sabial		sysctl_register_oid(e1->entry);
19163212Sabial		e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
19263212Sabial	}
19363212Sabial	if (error)
19463212Sabial		return(EBUSY);
19563212Sabial	/* Now really delete the entries */
19663212Sabial	e = TAILQ_FIRST(clist);
19763212Sabial	while (e != NULL) {
19863212Sabial		e1 = TAILQ_NEXT(e, link);
19963212Sabial		error = sysctl_remove_oid(e->entry, 1, 0);
20063212Sabial		if (error)
20163212Sabial			panic("sysctl_remove_oid: corrupt tree, entry: %s",
20263212Sabial			    e->entry->oid_name);
20363212Sabial		free(e, M_SYSCTLOID);
20463212Sabial		e = e1;
20563212Sabial	}
20663212Sabial	return (error);
20763212Sabial}
20863212Sabial
20963212Sabial/* Add an entry to the context */
21063212Sabialstruct sysctl_ctx_entry *
21163212Sabialsysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
21263212Sabial{
21363212Sabial	struct sysctl_ctx_entry *e;
21463212Sabial
21563212Sabial	if (clist == NULL || oidp == NULL)
21663212Sabial		return(NULL);
21763978Speter	e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
21863212Sabial	e->entry = oidp;
21963212Sabial	TAILQ_INSERT_HEAD(clist, e, link);
22063212Sabial	return (e);
22163212Sabial}
22263212Sabial
22363212Sabial/* Find an entry in the context */
22463212Sabialstruct sysctl_ctx_entry *
22563212Sabialsysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
22663212Sabial{
22763212Sabial	struct sysctl_ctx_entry *e;
22863212Sabial
22963212Sabial	if (clist == NULL || oidp == NULL)
23063212Sabial		return(NULL);
23171999Sphk	TAILQ_FOREACH(e, clist, link) {
23263212Sabial		if(e->entry == oidp)
23363212Sabial			return(e);
23463212Sabial	}
23563212Sabial	return (e);
23663212Sabial}
23763212Sabial
23844078Sdfr/*
23963212Sabial * Delete an entry from the context.
24063212Sabial * NOTE: this function doesn't free oidp! You have to remove it
24163212Sabial * with sysctl_remove_oid().
24263212Sabial */
24363212Sabialint
24463212Sabialsysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
24563212Sabial{
24663212Sabial	struct sysctl_ctx_entry *e;
24763212Sabial
24863212Sabial	if (clist == NULL || oidp == NULL)
24963212Sabial		return (EINVAL);
25063212Sabial	e = sysctl_ctx_entry_find(clist, oidp);
25163212Sabial	if (e != NULL) {
25263212Sabial		TAILQ_REMOVE(clist, e, link);
25363212Sabial		free(e, M_SYSCTLOID);
25463212Sabial		return (0);
25563212Sabial	} else
25663212Sabial		return (ENOENT);
25763212Sabial}
25863212Sabial
25963212Sabial/*
26063212Sabial * Remove dynamically created sysctl trees.
26163212Sabial * oidp - top of the tree to be removed
26263212Sabial * del - if 0 - just deregister, otherwise free up entries as well
26363212Sabial * recurse - if != 0 traverse the subtree to be deleted
26463212Sabial */
26563212Sabialint
26663212Sabialsysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
26763212Sabial{
26863212Sabial	struct sysctl_oid *p;
26963212Sabial	int error;
27063212Sabial
27163212Sabial	if (oidp == NULL)
27263212Sabial		return(EINVAL);
27363212Sabial	if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
27463212Sabial		printf("can't remove non-dynamic nodes!\n");
27563212Sabial		return (EINVAL);
27663212Sabial	}
27763212Sabial	/*
27863212Sabial	 * WARNING: normal method to do this should be through
27963212Sabial	 * sysctl_ctx_free(). Use recursing as the last resort
28063212Sabial	 * method to purge your sysctl tree of leftovers...
28163212Sabial	 * However, if some other code still references these nodes,
28263212Sabial	 * it will panic.
28363212Sabial	 */
28463212Sabial	if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
28563212Sabial		if (oidp->oid_refcnt == 1) {
28663212Sabial			SLIST_FOREACH(p, SYSCTL_CHILDREN(oidp), oid_link) {
28763212Sabial				if (!recurse)
28863212Sabial					return (ENOTEMPTY);
28963212Sabial				error = sysctl_remove_oid(p, del, recurse);
29063212Sabial				if (error)
29163212Sabial					return (error);
29263212Sabial			}
29363212Sabial			if (del)
29463212Sabial				free(SYSCTL_CHILDREN(oidp), M_SYSCTLOID);
29563212Sabial		}
29663212Sabial	}
29763212Sabial	if (oidp->oid_refcnt > 1 ) {
29863212Sabial		oidp->oid_refcnt--;
29963212Sabial	} else {
30063212Sabial		if (oidp->oid_refcnt == 0) {
30163212Sabial			printf("Warning: bad oid_refcnt=%u (%s)!\n",
30263212Sabial				oidp->oid_refcnt, oidp->oid_name);
30363212Sabial			return (EINVAL);
30463212Sabial		}
30563212Sabial		sysctl_unregister_oid(oidp);
30663212Sabial		if (del) {
30763978Speter			free((void *)(uintptr_t)(const void *)oidp->oid_name,
30863978Speter			     M_SYSCTLOID);
30963212Sabial			free(oidp, M_SYSCTLOID);
31063212Sabial		}
31163212Sabial	}
31263212Sabial	return (0);
31363212Sabial}
31463212Sabial
31563212Sabial/*
31663212Sabial * Create new sysctls at run time.
31763212Sabial * clist may point to a valid context initialized with sysctl_ctx_init().
31863212Sabial */
31963212Sabialstruct sysctl_oid *
32063212Sabialsysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
32170679Sjhb	int number, const char *name, int kind, void *arg1, int arg2,
32270679Sjhb	int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
32363212Sabial{
32463212Sabial	struct sysctl_oid *oidp;
32563212Sabial	ssize_t len;
32663978Speter	char *newname;
32763212Sabial
32863212Sabial	/* You have to hook up somewhere.. */
32963212Sabial	if (parent == NULL)
33063212Sabial		return(NULL);
33163212Sabial	/* Check if the node already exists, otherwise create it */
33263212Sabial	oidp = sysctl_find_oidname(name, parent);
33363212Sabial	if (oidp != NULL) {
33463212Sabial		if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
33563212Sabial			oidp->oid_refcnt++;
33663212Sabial			/* Update the context */
33763212Sabial			if (clist != NULL)
33863212Sabial				sysctl_ctx_entry_add(clist, oidp);
33963212Sabial			return (oidp);
34063212Sabial		} else {
34163212Sabial			printf("can't re-use a leaf (%s)!\n", name);
34263212Sabial			return (NULL);
34363212Sabial		}
34463212Sabial	}
34569781Sdwmalone	oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
34663212Sabial	oidp->oid_parent = parent;
34763212Sabial	SLIST_NEXT(oidp, oid_link) = NULL;
34863212Sabial	oidp->oid_number = number;
34963212Sabial	oidp->oid_refcnt = 1;
35063212Sabial	len = strlen(name);
35163978Speter	newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
35263978Speter	bcopy(name, newname, len + 1);
35363978Speter	newname[len] = '\0';
35463978Speter	oidp->oid_name = newname;
35563212Sabial	oidp->oid_handler = handler;
35663212Sabial	oidp->oid_kind = CTLFLAG_DYN | kind;
35763212Sabial	if ((kind & CTLTYPE) == CTLTYPE_NODE) {
35863212Sabial		/* Allocate space for children */
35963212Sabial		SYSCTL_CHILDREN(oidp) = malloc(sizeof(struct sysctl_oid_list),
36063212Sabial		    M_SYSCTLOID, M_WAITOK);
36163212Sabial		SLIST_INIT(SYSCTL_CHILDREN(oidp));
36263212Sabial	} else {
36363212Sabial		oidp->oid_arg1 = arg1;
36463212Sabial		oidp->oid_arg2 = arg2;
36563212Sabial	}
36663212Sabial	oidp->oid_fmt = fmt;
36763212Sabial	/* Update the context, if used */
36863212Sabial	if (clist != NULL)
36963212Sabial		sysctl_ctx_entry_add(clist, oidp);
37063212Sabial	/* Register this oid */
37163212Sabial	sysctl_register_oid(oidp);
37263212Sabial	return (oidp);
37363212Sabial}
37463212Sabial
37563212Sabial/*
37644078Sdfr * Register the kernel's oids on startup.
37744078Sdfr */
37878161SpeterSET_DECLARE(sysctl_set, struct sysctl_oid);
37912152Sphk
38080338Sroamstatic void
38180338Sroamsysctl_register_all(void *arg)
38238869Sbde{
38378161Speter	struct sysctl_oid **oidp;
38478161Speter
38578161Speter	SET_FOREACH(oidp, sysctl_set)
38678161Speter		sysctl_register_oid(*oidp);
38738869Sbde}
38844078SdfrSYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0);
38944078Sdfr
39012623Sphk/*
39112623Sphk * "Staff-functions"
39212623Sphk *
39312650Sphk * These functions implement a presently undocumented interface
39412650Sphk * used by the sysctl program to walk the tree, and get the type
39512650Sphk * so it can print the value.
39612650Sphk * This interface is under work and consideration, and should probably
39712650Sphk * be killed with a big axe by the first person who can find the time.
39812650Sphk * (be aware though, that the proper interface isn't as obvious as it
39912650Sphk * may seem, there are various conflicting requirements.
40012650Sphk *
40112623Sphk * {0,0}	printf the entire MIB-tree.
40212623Sphk * {0,1,...}	return the name of the "..." OID.
40342467Sphk * {0,2,...}	return the next OID.
40412623Sphk * {0,3}	return the OID of the name in "new"
40512650Sphk * {0,4,...}	return the kind & format info for the "..." OID.
40612623Sphk */
40712623Sphk
40812152Sphkstatic void
40944078Sdfrsysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
41012152Sphk{
41144078Sdfr	int k;
41244078Sdfr	struct sysctl_oid *oidp;
41312152Sphk
41444078Sdfr	SLIST_FOREACH(oidp, l, oid_link) {
41512152Sphk
41612152Sphk		for (k=0; k<i; k++)
41712152Sphk			printf(" ");
41812152Sphk
41944078Sdfr		printf("%d %s ", oidp->oid_number, oidp->oid_name);
42012152Sphk
42112152Sphk		printf("%c%c",
42244078Sdfr			oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
42344078Sdfr			oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
42412152Sphk
42544078Sdfr		if (oidp->oid_handler)
42615241Sphk			printf(" *Handler");
42715241Sphk
42844078Sdfr		switch (oidp->oid_kind & CTLTYPE) {
42912243Sphk			case CTLTYPE_NODE:
43015241Sphk				printf(" Node\n");
43144078Sdfr				if (!oidp->oid_handler) {
43212152Sphk					sysctl_sysctl_debug_dump_node(
43344078Sdfr						oidp->oid_arg1, i+2);
43412152Sphk				}
43512152Sphk				break;
43612152Sphk			case CTLTYPE_INT:    printf(" Int\n"); break;
43712152Sphk			case CTLTYPE_STRING: printf(" String\n"); break;
43812152Sphk			case CTLTYPE_QUAD:   printf(" Quad\n"); break;
43912152Sphk			case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
44012152Sphk			default:	     printf("\n");
44112152Sphk		}
44212152Sphk
44312152Sphk	}
44412152Sphk}
44512152Sphk
44612152Sphkstatic int
44762573Sphksysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
44812152Sphk{
44944078Sdfr	sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
45012152Sphk	return ENOENT;
45112152Sphk}
45212152Sphk
45312152SphkSYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
45412623Sphk	0, 0, sysctl_sysctl_debug, "-", "");
45512152Sphk
45612623Sphkstatic int
45762573Sphksysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
45812623Sphk{
45912623Sphk	int *name = (int *) arg1;
46012623Sphk	u_int namelen = arg2;
46144078Sdfr	int error = 0;
46244078Sdfr	struct sysctl_oid *oid;
46344972Sphk	struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
46412623Sphk	char buf[10];
46512131Sphk
46612623Sphk	while (namelen) {
46712623Sphk		if (!lsp) {
46841514Sarchie			snprintf(buf,sizeof(buf),"%d",*name);
46912623Sphk			if (req->oldidx)
47012623Sphk				error = SYSCTL_OUT(req, ".", 1);
47112623Sphk			if (!error)
47212623Sphk				error = SYSCTL_OUT(req, buf, strlen(buf));
47312623Sphk			if (error)
47412623Sphk				return (error);
47512623Sphk			namelen--;
47612623Sphk			name++;
47712623Sphk			continue;
47812623Sphk		}
47944972Sphk		lsp2 = 0;
48044078Sdfr		SLIST_FOREACH(oid, lsp, oid_link) {
48144078Sdfr			if (oid->oid_number != *name)
48212623Sphk				continue;
48312131Sphk
48412623Sphk			if (req->oldidx)
48512623Sphk				error = SYSCTL_OUT(req, ".", 1);
48612623Sphk			if (!error)
48744078Sdfr				error = SYSCTL_OUT(req, oid->oid_name,
48844078Sdfr					strlen(oid->oid_name));
48912623Sphk			if (error)
49012623Sphk				return (error);
49112623Sphk
49212623Sphk			namelen--;
49312623Sphk			name++;
49412623Sphk
49544972Sphk			if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
49612623Sphk				break;
49712623Sphk
49844078Sdfr			if (oid->oid_handler)
49912623Sphk				break;
50012623Sphk
50144972Sphk			lsp2 = (struct sysctl_oid_list *)oid->oid_arg1;
50212623Sphk			break;
50312623Sphk		}
50444972Sphk		lsp = lsp2;
50512623Sphk	}
50612623Sphk	return (SYSCTL_OUT(req, "", 1));
50712623Sphk}
50812623Sphk
50912623SphkSYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, "");
51012623Sphk
51112623Sphkstatic int
51263978Spetersysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
51344078Sdfr	int *next, int *len, int level, struct sysctl_oid **oidpp)
51412623Sphk{
51544078Sdfr	struct sysctl_oid *oidp;
51612623Sphk
51712623Sphk	*len = level;
51844078Sdfr	SLIST_FOREACH(oidp, lsp, oid_link) {
51944078Sdfr		*next = oidp->oid_number;
52044078Sdfr		*oidpp = oidp;
52112623Sphk
52212623Sphk		if (!namelen) {
52344078Sdfr			if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
52412623Sphk				return 0;
52544078Sdfr			if (oidp->oid_handler)
52612623Sphk				/* We really should call the handler here...*/
52712623Sphk				return 0;
52844078Sdfr			lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
52963978Speter			if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
53044078Sdfr				len, level+1, oidpp))
53115241Sphk				return 0;
53215241Sphk			goto next;
53312623Sphk		}
53412623Sphk
53544078Sdfr		if (oidp->oid_number < *name)
53612623Sphk			continue;
53712623Sphk
53844078Sdfr		if (oidp->oid_number > *name) {
53944078Sdfr			if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
54012623Sphk				return 0;
54144078Sdfr			if (oidp->oid_handler)
54212623Sphk				return 0;
54344078Sdfr			lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
54463978Speter			if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
54544078Sdfr				next+1, len, level+1, oidpp))
54612623Sphk				return (0);
54715241Sphk			goto next;
54812623Sphk		}
54944078Sdfr		if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
55012623Sphk			continue;
55112623Sphk
55244078Sdfr		if (oidp->oid_handler)
55312623Sphk			continue;
55412623Sphk
55544078Sdfr		lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
55663978Speter		if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
55744078Sdfr			len, level+1, oidpp))
55812623Sphk			return (0);
55915241Sphk	next:
56012623Sphk		namelen = 1;
56112623Sphk		*len = level;
56212623Sphk	}
56312623Sphk	return 1;
56412623Sphk}
56512623Sphk
56612623Sphkstatic int
56762573Sphksysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
56812623Sphk{
56912623Sphk	int *name = (int *) arg1;
57012623Sphk	u_int namelen = arg2;
57112623Sphk	int i, j, error;
57212623Sphk	struct sysctl_oid *oid;
57344078Sdfr	struct sysctl_oid_list *lsp = &sysctl__children;
57412623Sphk	int newoid[CTL_MAXNAME];
57512623Sphk
57663978Speter	i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
57712623Sphk	if (i)
57812623Sphk		return ENOENT;
57912650Sphk	error = SYSCTL_OUT(req, newoid, j * sizeof (int));
58012623Sphk	return (error);
58112623Sphk}
58212623Sphk
58312623SphkSYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, "");
58412623Sphk
58512623Sphkstatic int
58644078Sdfrname2oid (char *name, int *oid, int *len, struct sysctl_oid **oidpp)
58712623Sphk{
58844078Sdfr	int i;
58944078Sdfr	struct sysctl_oid *oidp;
59044078Sdfr	struct sysctl_oid_list *lsp = &sysctl__children;
59112623Sphk	char *p;
59212623Sphk
59312623Sphk	if (!*name)
59412623Sphk		return ENOENT;
59512623Sphk
59612623Sphk	p = name + strlen(name) - 1 ;
59712623Sphk	if (*p == '.')
59812623Sphk		*p = '\0';
59912623Sphk
60012623Sphk	*len = 0;
60112623Sphk
60212623Sphk	for (p = name; *p && *p != '.'; p++)
60312623Sphk		;
60412623Sphk	i = *p;
60512623Sphk	if (i == '.')
60612623Sphk		*p = '\0';
60712623Sphk
60844078Sdfr	oidp = SLIST_FIRST(lsp);
60912623Sphk
61044078Sdfr	while (oidp && *len < CTL_MAXNAME) {
61144078Sdfr		if (strcmp(name, oidp->oid_name)) {
61244078Sdfr			oidp = SLIST_NEXT(oidp, oid_link);
61312623Sphk			continue;
61412623Sphk		}
61544078Sdfr		*oid++ = oidp->oid_number;
61612623Sphk		(*len)++;
61712623Sphk
61812623Sphk		if (!i) {
61944078Sdfr			if (oidpp)
62044078Sdfr				*oidpp = oidp;
62112623Sphk			return (0);
62212623Sphk		}
62312623Sphk
62444078Sdfr		if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
62512623Sphk			break;
62612623Sphk
62744078Sdfr		if (oidp->oid_handler)
62812623Sphk			break;
62912623Sphk
63044078Sdfr		lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
63144078Sdfr		oidp = SLIST_FIRST(lsp);
63212623Sphk		name = p+1;
63312623Sphk		for (p = name; *p && *p != '.'; p++)
63412623Sphk				;
63512623Sphk		i = *p;
63612623Sphk		if (i == '.')
63712623Sphk			*p = '\0';
63812623Sphk	}
63912623Sphk	return ENOENT;
64012623Sphk}
64112623Sphk
64212623Sphkstatic int
64362573Sphksysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
64412623Sphk{
64512623Sphk	char *p;
64612623Sphk	int error, oid[CTL_MAXNAME], len;
64712623Sphk	struct sysctl_oid *op = 0;
64812623Sphk
64912623Sphk	if (!req->newlen)
65012623Sphk		return ENOENT;
65145140Sphk	if (req->newlen >= MAXPATHLEN)	/* XXX arbitrary, undocumented */
65245140Sphk		return (ENAMETOOLONG);
65312623Sphk
65412623Sphk	p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
65512623Sphk
65612623Sphk	error = SYSCTL_IN(req, p, req->newlen);
65712623Sphk	if (error) {
65812623Sphk		free(p, M_SYSCTL);
65912623Sphk		return (error);
66012623Sphk	}
66112623Sphk
66212623Sphk	p [req->newlen] = '\0';
66312623Sphk
66412623Sphk	error = name2oid(p, oid, &len, &op);
66512623Sphk
66612623Sphk	free(p, M_SYSCTL);
66712623Sphk
66812623Sphk	if (error)
66912623Sphk		return (error);
67012623Sphk
67112650Sphk	error = SYSCTL_OUT(req, oid, len * sizeof *oid);
67212623Sphk	return (error);
67312623Sphk}
67412623Sphk
67512910SphkSYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY, 0, 0,
67612623Sphk	sysctl_sysctl_name2oid, "I", "");
67712623Sphk
67812623Sphkstatic int
67962573Sphksysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
68012623Sphk{
68144078Sdfr	struct sysctl_oid *oid;
68253977Sgreen	int error;
68312623Sphk
68453977Sgreen	error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
68553977Sgreen	if (error)
68653977Sgreen		return (error);
68712623Sphk
68844078Sdfr	if (!oid->oid_fmt)
68953977Sgreen		return (ENOENT);
69053977Sgreen	error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
69153977Sgreen	if (error)
69253977Sgreen		return (error);
69353977Sgreen	error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
69412650Sphk	return (error);
69512623Sphk}
69612623Sphk
69742467Sphk
69812623SphkSYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD, sysctl_sysctl_oidfmt, "");
69912623Sphk
70012243Sphk/*
70112623Sphk * Default "handler" functions.
70212623Sphk */
70312623Sphk
70412623Sphk/*
70542095Sdfr * Handle an int, signed or unsigned.
70612243Sphk * Two cases:
70712243Sphk *     a variable:  point arg1 at it.
70812243Sphk *     a constant:  pass it in arg2.
70912243Sphk */
71012243Sphk
71111865Sphkint
71262573Sphksysctl_handle_int(SYSCTL_HANDLER_ARGS)
71311863Sphk{
71412243Sphk	int error = 0;
71511863Sphk
71612243Sphk	if (arg1)
71712243Sphk		error = SYSCTL_OUT(req, arg1, sizeof(int));
71820506Sbde	else
71912243Sphk		error = SYSCTL_OUT(req, &arg2, sizeof(int));
72011863Sphk
72112243Sphk	if (error || !req->newptr)
72212243Sphk		return (error);
72311863Sphk
72412243Sphk	if (!arg1)
72512243Sphk		error = EPERM;
72612243Sphk	else
72712243Sphk		error = SYSCTL_IN(req, arg1, sizeof(int));
72812243Sphk	return (error);
72911863Sphk}
73011863Sphk
73112243Sphk/*
73245140Sphk * Handle a long, signed or unsigned.  arg1 points to it.
73338517Sdfr */
73438517Sdfr
73538517Sdfrint
73662573Sphksysctl_handle_long(SYSCTL_HANDLER_ARGS)
73738517Sdfr{
73838517Sdfr	int error = 0;
73938517Sdfr
74045140Sphk	if (!arg1)
74145140Sphk		return (EINVAL);
74242095Sdfr	error = SYSCTL_OUT(req, arg1, sizeof(long));
74338517Sdfr
74438517Sdfr	if (error || !req->newptr)
74538517Sdfr		return (error);
74638517Sdfr
74745140Sphk	error = SYSCTL_IN(req, arg1, sizeof(long));
74838517Sdfr	return (error);
74938517Sdfr}
75038517Sdfr
75138517Sdfr/*
75212243Sphk * Handle our generic '\0' terminated 'C' string.
75312243Sphk * Two cases:
75412243Sphk * 	a variable string:  point arg1 at it, arg2 is max length.
75512243Sphk * 	a constant string:  point arg1 at it, arg2 is zero.
75612243Sphk */
75712243Sphk
75811865Sphkint
75962573Sphksysctl_handle_string(SYSCTL_HANDLER_ARGS)
76011863Sphk{
76112243Sphk	int error=0;
76211863Sphk
76312297Sphk	error = SYSCTL_OUT(req, arg1, strlen((char *)arg1)+1);
76411863Sphk
76545140Sphk	if (error || !req->newptr)
76612243Sphk		return (error);
76711863Sphk
76845140Sphk	if ((req->newlen - req->newidx) >= arg2) {
76945140Sphk		error = EINVAL;
77012243Sphk	} else {
77112243Sphk		arg2 = (req->newlen - req->newidx);
77212243Sphk		error = SYSCTL_IN(req, arg1, arg2);
77312243Sphk		((char *)arg1)[arg2] = '\0';
77411863Sphk	}
77512131Sphk
77612131Sphk	return (error);
77711863Sphk}
77811863Sphk
77912243Sphk/*
78012243Sphk * Handle any kind of opaque data.
78112243Sphk * arg1 points to it, arg2 is the size.
78212243Sphk */
78312243Sphk
78411865Sphkint
78562573Sphksysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
78611863Sphk{
78712243Sphk	int error;
78812243Sphk
78912243Sphk	error = SYSCTL_OUT(req, arg1, arg2);
79012243Sphk
79112243Sphk	if (error || !req->newptr)
79212243Sphk		return (error);
79312243Sphk
79412243Sphk	error = SYSCTL_IN(req, arg1, arg2);
79512243Sphk
79612243Sphk	return (error);
79712243Sphk}
79812243Sphk
79912260Sphk/*
80012260Sphk * Transfer functions to/from kernel space.
80112260Sphk * XXX: rather untested at this point
80212260Sphk */
80312260Sphkstatic int
80438517Sdfrsysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
80512243Sphk{
80638517Sdfr	size_t i = 0;
80712260Sphk
80812260Sphk	if (req->oldptr) {
80938517Sdfr		i = l;
81073971Stmm		if (req->oldlen <= req->oldidx)
81173971Stmm			i = 0;
81273971Stmm		else
81373971Stmm			if (i > req->oldlen - req->oldidx)
81473971Stmm				i = req->oldlen - req->oldidx;
81512260Sphk		if (i > 0)
81617971Sbde			bcopy(p, (char *)req->oldptr + req->oldidx, i);
81712243Sphk	}
81812260Sphk	req->oldidx += l;
81916282Snate	if (req->oldptr && i != l)
82011863Sphk		return (ENOMEM);
82112260Sphk	return (0);
82212243Sphk}
82312243Sphk
82412260Sphkstatic int
82538517Sdfrsysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
82612243Sphk{
82712260Sphk	if (!req->newptr)
82812260Sphk		return 0;
82912260Sphk	if (req->newlen - req->newidx < l)
83011863Sphk		return (EINVAL);
83117971Sbde	bcopy((char *)req->newptr + req->newidx, p, l);
83212243Sphk	req->newidx += l;
83312131Sphk	return (0);
83411863Sphk}
83511863Sphk
83616282Snateint
83776834Sjlemonkernel_sysctl(struct proc *p, int *name, u_int namelen, void *old,
83876834Sjlemon    size_t *oldlenp, void *new, size_t newlen, size_t *retval)
83916282Snate{
84016282Snate	int error = 0;
84116282Snate	struct sysctl_req req;
84216282Snate
84316282Snate	bzero(&req, sizeof req);
84416282Snate
84516282Snate	req.p = p;
84616282Snate
84716282Snate	if (oldlenp) {
84816282Snate		req.oldlen = *oldlenp;
84916282Snate	}
85016282Snate
85116282Snate	if (old) {
85216282Snate		req.oldptr= old;
85316282Snate	}
85416282Snate
85577646Sdd	if (new != NULL) {
85616282Snate		req.newlen = newlen;
85716282Snate		req.newptr = new;
85816282Snate	}
85916282Snate
86016282Snate	req.oldfunc = sysctl_old_kernel;
86116282Snate	req.newfunc = sysctl_new_kernel;
86216282Snate	req.lock = 1;
86316282Snate
86416282Snate	/* XXX this should probably be done in a general way */
86516282Snate	while (memlock.sl_lock) {
86616282Snate		memlock.sl_want = 1;
86716282Snate		(void) tsleep((caddr_t)&memlock, PRIBIO+1, "sysctl", 0);
86816282Snate		memlock.sl_locked++;
86916282Snate	}
87016282Snate	memlock.sl_lock = 1;
87116282Snate
87216282Snate	error = sysctl_root(0, name, namelen, &req);
87316282Snate
87416282Snate	if (req.lock == 2)
87557975Sphk		vsunlock(req.oldptr, req.oldlen);
87616282Snate
87716282Snate	memlock.sl_lock = 0;
87816282Snate
87916282Snate	if (memlock.sl_want) {
88016282Snate		memlock.sl_want = 0;
88116282Snate		wakeup((caddr_t)&memlock);
88216282Snate	}
88316282Snate
88416282Snate	if (error && error != ENOMEM)
88516282Snate		return (error);
88616282Snate
88716282Snate	if (retval) {
88816282Snate		if (req.oldptr && req.oldidx > req.oldlen)
88916282Snate			*retval = req.oldlen;
89016282Snate		else
89116282Snate			*retval = req.oldidx;
89216282Snate	}
89316282Snate	return (error);
89416282Snate}
89516282Snate
89676834Sjlemonint
89776834Sjlemonkernel_sysctlbyname(struct proc *p, char *name, void *old, size_t *oldlenp,
89876834Sjlemon    void *new, size_t newlen, size_t *retval)
89976834Sjlemon{
90076834Sjlemon        int oid[CTL_MAXNAME];
90178620Smjacob        size_t oidlen, plen;
90278620Smjacob	int error;
90376834Sjlemon
90476834Sjlemon	oid[0] = 0;		/* sysctl internal magic */
90576834Sjlemon	oid[1] = 3;		/* name2oid */
90676834Sjlemon	oidlen = sizeof(oid);
90776834Sjlemon
90876834Sjlemon	error = kernel_sysctl(p, oid, 2, oid, &oidlen,
90976834Sjlemon	    (void *)name, strlen(name), &plen);
91076834Sjlemon	if (error)
91176834Sjlemon		return (error);
91276834Sjlemon
91376834Sjlemon	error = kernel_sysctl(p, oid, plen / sizeof(int), old, oldlenp,
91476834Sjlemon	    new, newlen, retval);
91576834Sjlemon	return (error);
91676834Sjlemon}
91776834Sjlemon
91812260Sphk/*
91912260Sphk * Transfer function to/from user space.
92012260Sphk */
92112260Sphkstatic int
92238517Sdfrsysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
92312243Sphk{
92438517Sdfr	int error = 0;
92538517Sdfr	size_t i = 0;
92612243Sphk
92712429Sphk	if (req->lock == 1 && req->oldptr) {
92812429Sphk		vslock(req->oldptr, req->oldlen);
92912429Sphk		req->lock = 2;
93012429Sphk	}
93112260Sphk	if (req->oldptr) {
93238517Sdfr		i = l;
93373971Stmm		if (req->oldlen <= req->oldidx)
93473971Stmm			i = 0;
93573971Stmm		else
93673971Stmm			if (i > req->oldlen - req->oldidx)
93773971Stmm				i = req->oldlen - req->oldidx;
93812260Sphk		if (i > 0)
93917971Sbde			error = copyout(p, (char *)req->oldptr + req->oldidx,
94017971Sbde					i);
94112260Sphk	}
94212260Sphk	req->oldidx += l;
94312243Sphk	if (error)
94412243Sphk		return (error);
94512260Sphk	if (req->oldptr && i < l)
94612243Sphk		return (ENOMEM);
94712260Sphk	return (0);
94812243Sphk}
94912243Sphk
95012260Sphkstatic int
95138517Sdfrsysctl_new_user(struct sysctl_req *req, void *p, size_t l)
95212243Sphk{
95312285Sphk	int error;
95412260Sphk
95512260Sphk	if (!req->newptr)
95612260Sphk		return 0;
95712260Sphk	if (req->newlen - req->newidx < l)
95812243Sphk		return (EINVAL);
95917971Sbde	error = copyin((char *)req->newptr + req->newidx, p, l);
96012243Sphk	req->newidx += l;
96112243Sphk	return (error);
96212243Sphk}
96312243Sphk
9641541Srgrimesint
96553977Sgreensysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
96653977Sgreen    int *nindx, struct sysctl_req *req)
96712131Sphk{
96844078Sdfr	struct sysctl_oid *oid;
96953977Sgreen	int indx;
97012131Sphk
97153977Sgreen	oid = SLIST_FIRST(&sysctl__children);
97212131Sphk	indx = 0;
97344078Sdfr	while (oid && indx < CTL_MAXNAME) {
97444078Sdfr		if (oid->oid_number == name[indx]) {
97512131Sphk			indx++;
97644078Sdfr			if (oid->oid_kind & CTLFLAG_NOLOCK)
97712429Sphk				req->lock = 0;
97844078Sdfr			if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
97953977Sgreen				if (oid->oid_handler != NULL ||
98053977Sgreen				    indx == namelen) {
98153977Sgreen					*noid = oid;
98253977Sgreen					if (nindx != NULL)
98353977Sgreen						*nindx = indx;
98453977Sgreen					return (0);
98553977Sgreen				}
98653977Sgreen				oid = SLIST_FIRST(
98753977Sgreen				    (struct sysctl_oid_list *)oid->oid_arg1);
98853977Sgreen			} else if (indx == namelen) {
98953977Sgreen				*noid = oid;
99053977Sgreen				if (nindx != NULL)
99153977Sgreen					*nindx = indx;
99253977Sgreen				return (0);
99312131Sphk			} else {
99453977Sgreen				return (ENOTDIR);
99512131Sphk			}
99612131Sphk		} else {
99744078Sdfr			oid = SLIST_NEXT(oid, oid_link);
99812131Sphk		}
99912131Sphk	}
100053977Sgreen	return (ENOENT);
100153977Sgreen}
100253977Sgreen
100353977Sgreen/*
100453977Sgreen * Traverse our tree, and find the right node, execute whatever it points
100553977Sgreen * to, and return the resulting error code.
100653977Sgreen */
100753977Sgreen
100853977Sgreenint
100962573Sphksysctl_root(SYSCTL_HANDLER_ARGS)
101053977Sgreen{
101153977Sgreen	struct sysctl_oid *oid;
101253977Sgreen	int error, indx;
101353977Sgreen
101453977Sgreen	error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
101553977Sgreen	if (error)
101653977Sgreen		return (error);
101753977Sgreen
101853977Sgreen	if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
101953977Sgreen		/*
102053977Sgreen		 * You can't call a sysctl when it's a node, but has
102153977Sgreen		 * no handler.  Inform the user that it's a node.
102253977Sgreen		 * The indx may or may not be the same as namelen.
102353977Sgreen		 */
102453977Sgreen		if (oid->oid_handler == NULL)
102553977Sgreen			return (EISDIR);
102653977Sgreen	}
102753977Sgreen
102812131Sphk	/* If writing isn't allowed */
102944078Sdfr	if (req->newptr && (!(oid->oid_kind & CTLFLAG_WR) ||
103044078Sdfr	    ((oid->oid_kind & CTLFLAG_SECURE) && securelevel > 0)))
103112131Sphk		return (EPERM);
103212131Sphk
103312910Sphk	/* Most likely only root can write */
103444078Sdfr	if (!(oid->oid_kind & CTLFLAG_ANYBODY) &&
103512910Sphk	    req->newptr && req->p &&
103653977Sgreen	    (error = suser_xxx(0, req->p,
103746155Sphk	    (oid->oid_kind & CTLFLAG_PRISON) ? PRISON_ROOT : 0)))
103853977Sgreen		return (error);
103912910Sphk
104044078Sdfr	if (!oid->oid_handler)
104112131Sphk		return EINVAL;
104212131Sphk
104353977Sgreen	if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE)
104453977Sgreen		error = oid->oid_handler(oid, (int *)arg1 + indx, arg2 - indx,
104553977Sgreen		    req);
104653977Sgreen	else
104753977Sgreen		error = oid->oid_handler(oid, oid->oid_arg1, oid->oid_arg2,
104853977Sgreen		    req);
104953977Sgreen	return (error);
105012131Sphk}
105112131Sphk
105212221Sbde#ifndef _SYS_SYSPROTO_H_
105312171Sphkstruct sysctl_args {
105412171Sphk	int	*name;
105512171Sphk	u_int	namelen;
105612171Sphk	void	*old;
105712171Sphk	size_t	*oldlenp;
105812171Sphk	void	*new;
105912171Sphk	size_t	newlen;
106012171Sphk};
106112221Sbde#endif
106212171Sphk
106312131Sphkint
106430994Sphk__sysctl(struct proc *p, struct sysctl_args *uap)
10651541Srgrimes{
106638517Sdfr	int error, i, name[CTL_MAXNAME];
106738517Sdfr	size_t j;
10681541Srgrimes
10691541Srgrimes	if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
10701541Srgrimes		return (EINVAL);
107111863Sphk
10723308Sphk 	error = copyin(uap->name, &name, uap->namelen * sizeof(int));
10733308Sphk 	if (error)
10741541Srgrimes		return (error);
10751541Srgrimes
107612260Sphk	error = userland_sysctl(p, name, uap->namelen,
107712171Sphk		uap->old, uap->oldlenp, 0,
107812260Sphk		uap->new, uap->newlen, &j);
107912260Sphk	if (error && error != ENOMEM)
108012260Sphk		return (error);
108112260Sphk	if (uap->oldlenp) {
108212260Sphk		i = copyout(&j, uap->oldlenp, sizeof(j));
108312260Sphk		if (i)
108412260Sphk			return (i);
108512260Sphk	}
108612260Sphk	return (error);
108712171Sphk}
108812171Sphk
108912171Sphk/*
109012171Sphk * This is used from various compatibility syscalls too.  That's why name
109112171Sphk * must be in kernel space.
109212171Sphk */
109312171Sphkint
109480338Sroamuserland_sysctl(struct proc *p, int *name, u_int namelen, void *old,
109580338Sroam    size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval)
109612171Sphk{
109712429Sphk	int error = 0;
109816159Sphk	struct sysctl_req req, req2;
109912171Sphk
110012243Sphk	bzero(&req, sizeof req);
110112243Sphk
110212285Sphk	req.p = p;
110312285Sphk
110412171Sphk	if (oldlenp) {
110512171Sphk		if (inkernel) {
110612243Sphk			req.oldlen = *oldlenp;
110712171Sphk		} else {
110812260Sphk			error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
110912171Sphk			if (error)
111012171Sphk				return (error);
111112171Sphk		}
111212171Sphk	}
111312171Sphk
111412243Sphk	if (old) {
111552644Sphk		if (!useracc(old, req.oldlen, VM_PROT_WRITE))
111612243Sphk			return (EFAULT);
111712243Sphk		req.oldptr= old;
111812243Sphk	}
111912131Sphk
112077646Sdd	if (new != NULL) {
112152644Sphk		if (!useracc(new, req.newlen, VM_PROT_READ))
112212243Sphk			return (EFAULT);
112312243Sphk		req.newlen = newlen;
112412243Sphk		req.newptr = new;
112511863Sphk	}
112612131Sphk
112712243Sphk	req.oldfunc = sysctl_old_user;
112812243Sphk	req.newfunc = sysctl_new_user;
112912429Sphk	req.lock = 1;
113011863Sphk
113112429Sphk	/* XXX this should probably be done in a general way */
113212429Sphk	while (memlock.sl_lock) {
113312429Sphk		memlock.sl_want = 1;
113412429Sphk		(void) tsleep((caddr_t)&memlock, PRIBIO+1, "sysctl", 0);
113512429Sphk		memlock.sl_locked++;
113612429Sphk	}
113712429Sphk	memlock.sl_lock = 1;
113812429Sphk
113916159Sphk	do {
114016159Sphk	    req2 = req;
114116159Sphk	    error = sysctl_root(0, name, namelen, &req2);
114216159Sphk	} while (error == EAGAIN);
114312243Sphk
114416159Sphk	req = req2;
114512429Sphk	if (req.lock == 2)
114657975Sphk		vsunlock(req.oldptr, req.oldlen);
114712429Sphk
114812429Sphk	memlock.sl_lock = 0;
114912429Sphk
115012429Sphk	if (memlock.sl_want) {
115112429Sphk		memlock.sl_want = 0;
115212429Sphk		wakeup((caddr_t)&memlock);
115312429Sphk	}
115412429Sphk
115512260Sphk	if (error && error != ENOMEM)
115612260Sphk		return (error);
115712260Sphk
115812260Sphk	if (retval) {
115912260Sphk		if (req.oldptr && req.oldidx > req.oldlen)
116012243Sphk			*retval = req.oldlen;
116112260Sphk		else
116212260Sphk			*retval = req.oldidx;
116311863Sphk	}
116412260Sphk	return (error);
11651541Srgrimes}
11661541Srgrimes
11671541Srgrimes#ifdef COMPAT_43
11681541Srgrimes#include <sys/socket.h>
116915103Sphk#include <vm/vm_param.h>
117015103Sphk
11711541Srgrimes#define	KINFO_PROC		(0<<8)
11721541Srgrimes#define	KINFO_RT		(1<<8)
11731541Srgrimes#define	KINFO_VNODE		(2<<8)
11741541Srgrimes#define	KINFO_FILE		(3<<8)
11751541Srgrimes#define	KINFO_METER		(4<<8)
11761541Srgrimes#define	KINFO_LOADAVG		(5<<8)
11771541Srgrimes#define	KINFO_CLOCKRATE		(6<<8)
11781541Srgrimes
11799455Speter/* Non-standard BSDI extension - only present on their 4.3 net-2 releases */
11809455Speter#define	KINFO_BSDI_SYSINFO	(101<<8)
11819455Speter
11829455Speter/*
11839455Speter * XXX this is bloat, but I hope it's better here than on the potentially
11849455Speter * limited kernel stack...  -Peter
11859455Speter */
11869455Speter
118712819Sphkstatic struct {
11889455Speter	int	bsdi_machine;		/* "i386" on BSD/386 */
11899455Speter/*      ^^^ this is an offset to the string, relative to the struct start */
11909455Speter	char	*pad0;
11919455Speter	long	pad1;
11929455Speter	long	pad2;
11939455Speter	long	pad3;
11949455Speter	u_long	pad4;
11959455Speter	u_long	pad5;
11969455Speter	u_long	pad6;
11979455Speter
11989455Speter	int	bsdi_ostype;		/* "BSD/386" on BSD/386 */
11999455Speter	int	bsdi_osrelease;		/* "1.1" on BSD/386 */
12009455Speter	long	pad7;
12019455Speter	long	pad8;
12029455Speter	char	*pad9;
12039455Speter
12049455Speter	long	pad10;
12059455Speter	long	pad11;
12069455Speter	int	pad12;
12079455Speter	long	pad13;
12089455Speter	quad_t	pad14;
12099455Speter	long	pad15;
12109455Speter
12119455Speter	struct	timeval pad16;
12129455Speter	/* we dont set this, because BSDI's uname used gethostname() instead */
12139455Speter	int	bsdi_hostname;		/* hostname on BSD/386 */
12149455Speter
12159455Speter	/* the actual string data is appended here */
12169455Speter
12179455Speter} bsdi_si;
12189455Speter/*
12199455Speter * this data is appended to the end of the bsdi_si structure during copyout.
12209455Speter * The "char *" offsets are relative to the base of the bsdi_si struct.
12219455Speter * This contains "FreeBSD\02.0-BUILT-nnnnnn\0i386\0", and these strings
12229455Speter * should not exceed the length of the buffer here... (or else!! :-)
12239455Speter */
122412819Sphkstatic char bsdi_strings[80];	/* It had better be less than this! */
12259455Speter
122612221Sbde#ifndef _SYS_SYSPROTO_H_
12271541Srgrimesstruct getkerninfo_args {
12281541Srgrimes	int	op;
12291541Srgrimes	char	*where;
123038864Sbde	size_t	*size;
12311541Srgrimes	int	arg;
12321541Srgrimes};
123312221Sbde#endif
12341541Srgrimes
12351549Srgrimesint
123630994Sphkogetkerninfo(struct proc *p, struct getkerninfo_args *uap)
12371541Srgrimes{
123812171Sphk	int error, name[6];
123938517Sdfr	size_t size;
12401541Srgrimes
12411541Srgrimes	switch (uap->op & 0xff00) {
12421541Srgrimes
12431541Srgrimes	case KINFO_RT:
124412171Sphk		name[0] = CTL_NET;
124512171Sphk		name[1] = PF_ROUTE;
124612171Sphk		name[2] = 0;
124712171Sphk		name[3] = (uap->op & 0xff0000) >> 16;
124812171Sphk		name[4] = uap->op & 0xff;
124912171Sphk		name[5] = uap->arg;
125012171Sphk		error = userland_sysctl(p, name, 6, uap->where, uap->size,
125112429Sphk			0, 0, 0, &size);
12521541Srgrimes		break;
12531541Srgrimes
12541541Srgrimes	case KINFO_VNODE:
125512171Sphk		name[0] = CTL_KERN;
125612171Sphk		name[1] = KERN_VNODE;
125712171Sphk		error = userland_sysctl(p, name, 2, uap->where, uap->size,
125812429Sphk			0, 0, 0, &size);
12591541Srgrimes		break;
12601541Srgrimes
12611541Srgrimes	case KINFO_PROC:
126212171Sphk		name[0] = CTL_KERN;
126312171Sphk		name[1] = KERN_PROC;
126412171Sphk		name[2] = uap->op & 0xff;
126512171Sphk		name[3] = uap->arg;
126612171Sphk		error = userland_sysctl(p, name, 4, uap->where, uap->size,
126712429Sphk			0, 0, 0, &size);
12681541Srgrimes		break;
12691541Srgrimes
12701541Srgrimes	case KINFO_FILE:
127112171Sphk		name[0] = CTL_KERN;
127212171Sphk		name[1] = KERN_FILE;
127312171Sphk		error = userland_sysctl(p, name, 2, uap->where, uap->size,
127412429Sphk			0, 0, 0, &size);
12751541Srgrimes		break;
12761541Srgrimes
12771541Srgrimes	case KINFO_METER:
127812171Sphk		name[0] = CTL_VM;
127912171Sphk		name[1] = VM_METER;
128012171Sphk		error = userland_sysctl(p, name, 2, uap->where, uap->size,
128112429Sphk			0, 0, 0, &size);
12821541Srgrimes		break;
12831541Srgrimes
12841541Srgrimes	case KINFO_LOADAVG:
128512171Sphk		name[0] = CTL_VM;
128612171Sphk		name[1] = VM_LOADAVG;
128712171Sphk		error = userland_sysctl(p, name, 2, uap->where, uap->size,
128812429Sphk			0, 0, 0, &size);
12891541Srgrimes		break;
12901541Srgrimes
12911541Srgrimes	case KINFO_CLOCKRATE:
129212171Sphk		name[0] = CTL_KERN;
129312171Sphk		name[1] = KERN_CLOCKRATE;
129412171Sphk		error = userland_sysctl(p, name, 2, uap->where, uap->size,
129512429Sphk			0, 0, 0, &size);
12961541Srgrimes		break;
12971541Srgrimes
12989455Speter	case KINFO_BSDI_SYSINFO: {
12999455Speter		/*
13009455Speter		 * this is pretty crude, but it's just enough for uname()
13019455Speter		 * from BSDI's 1.x libc to work.
13029455Speter		 *
13039455Speter		 * In particular, it doesn't return the same results when
13049455Speter		 * the supplied buffer is too small.  BSDI's version apparently
13059455Speter		 * will return the amount copied, and set the *size to how
13069455Speter		 * much was needed.  The emulation framework here isn't capable
13079455Speter		 * of that, so we just set both to the amount copied.
13089455Speter		 * BSDI's 2.x product apparently fails with ENOMEM in this
13099455Speter		 * scenario.
13109455Speter		 */
13119455Speter
13129455Speter		u_int needed;
13139455Speter		u_int left;
13149455Speter		char *s;
13159455Speter
13169455Speter		bzero((char *)&bsdi_si, sizeof(bsdi_si));
13179455Speter		bzero(bsdi_strings, sizeof(bsdi_strings));
13189455Speter
13199455Speter		s = bsdi_strings;
13209455Speter
13219455Speter		bsdi_si.bsdi_ostype = (s - bsdi_strings) + sizeof(bsdi_si);
13229455Speter		strcpy(s, ostype);
13239455Speter		s += strlen(s) + 1;
13249455Speter
13259455Speter		bsdi_si.bsdi_osrelease = (s - bsdi_strings) + sizeof(bsdi_si);
13269455Speter		strcpy(s, osrelease);
13279455Speter		s += strlen(s) + 1;
13289455Speter
13299455Speter		bsdi_si.bsdi_machine = (s - bsdi_strings) + sizeof(bsdi_si);
13309455Speter		strcpy(s, machine);
13319455Speter		s += strlen(s) + 1;
13329455Speter
13339455Speter		needed = sizeof(bsdi_si) + (s - bsdi_strings);
13349455Speter
13359455Speter		if (uap->where == NULL) {
13369455Speter			/* process is asking how much buffer to supply.. */
13379455Speter			size = needed;
13389455Speter			error = 0;
13399455Speter			break;
13409455Speter		}
13419455Speter
13429455Speter
13439455Speter		/* if too much buffer supplied, trim it down */
13449455Speter		if (size > needed)
13459455Speter			size = needed;
13469455Speter
13479455Speter		/* how much of the buffer is remaining */
13489455Speter		left = size;
13499455Speter
13509455Speter		if ((error = copyout((char *)&bsdi_si, uap->where, left)) != 0)
13519455Speter			break;
13529455Speter
13539455Speter		/* is there any point in continuing? */
13549455Speter		if (left > sizeof(bsdi_si)) {
13559455Speter			left -= sizeof(bsdi_si);
13569455Speter			error = copyout(&bsdi_strings,
13579455Speter					uap->where + sizeof(bsdi_si), left);
13589455Speter		}
13599455Speter		break;
13609455Speter	}
13619455Speter
13621541Srgrimes	default:
13631541Srgrimes		return (EOPNOTSUPP);
13641541Srgrimes	}
13651541Srgrimes	if (error)
13661541Srgrimes		return (error);
136730994Sphk	p->p_retval[0] = size;
13681541Srgrimes	if (uap->size)
13691541Srgrimes		error = copyout((caddr_t)&size, (caddr_t)uap->size,
13701541Srgrimes		    sizeof(size));
13711541Srgrimes	return (error);
13721541Srgrimes}
13731541Srgrimes#endif /* COMPAT_43 */
1374